-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Correctly drop own BlockState drops when breaking (#63)
blocks: Fix drop of own BlockState when breaking
- Loading branch information
Showing
5 changed files
with
205 additions
and
35 deletions.
There are no files selected for viewing
36 changes: 20 additions & 16 deletions
36
src/main/java/com/nitnelave/CreeperHeal/block/CreeperBanner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,43 @@ | ||
package com.nitnelave.CreeperHeal.block; | ||
|
||
import com.nitnelave.CreeperHeal.config.CreeperConfig; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.block.Banner; | ||
import org.bukkit.inventory.ItemStack; | ||
import org.bukkit.inventory.meta.BannerMeta; | ||
|
||
/** | ||
* Banner implementation of CreeperBlock. | ||
* | ||
* | ||
* @author drexplosionpd | ||
* | ||
*/ | ||
public class CreeperBanner extends CreeperBlock | ||
{ | ||
|
||
/* | ||
* Constructor. | ||
*/ | ||
protected CreeperBanner(Banner banner) | ||
CreeperBanner(Banner banner) | ||
{ | ||
super(banner); | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* | ||
* @see com.nitnelave.CreeperHeal.block.CreeperBlock#update() | ||
* @see com.nitnelave.CreeperHeal.block.Replaceable#drop(boolean) | ||
*/ | ||
@Override | ||
public void update() | ||
public boolean drop(boolean forced) | ||
{ | ||
super.update(); | ||
Banner state = (Banner) getBlock().getState(); | ||
Banner banner = (Banner) blockState; | ||
state.setBaseColor(banner.getBaseColor()); | ||
state.setPatterns(banner.getPatterns()); | ||
|
||
state.setData(banner.getData()); | ||
state.update(true); | ||
if (forced || CreeperConfig.shouldDrop()) | ||
{ | ||
ItemStack itemStack = new ItemStack(blockState.getType()); | ||
BannerMeta bannerMeta = ((BannerMeta) Bukkit.getItemFactory().getItemMeta(blockState.getType())); | ||
bannerMeta.setPatterns(((Banner) blockState).getPatterns()); | ||
itemStack.setItemMeta(bannerMeta); | ||
blockState.getWorld().dropItemNaturally(blockState.getLocation().add(0.5, 0.5, 0.5), itemStack); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/main/java/com/nitnelave/CreeperHeal/block/CreeperJukebox.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.nitnelave.CreeperHeal.block; | ||
|
||
import com.nitnelave.CreeperHeal.CreeperHeal; | ||
import com.nitnelave.CreeperHeal.config.CreeperConfig; | ||
import com.nitnelave.CreeperHeal.config.WCfgVal; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.Material; | ||
import org.bukkit.block.Jukebox; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.HandlerList; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.entity.ItemSpawnEvent; | ||
|
||
/** | ||
* Jukebox implementation of CreeperBlock. | ||
* | ||
* @author Jikoo | ||
*/ | ||
public class CreeperJukebox extends CreeperBlock | ||
{ | ||
|
||
/* | ||
* Constructor. | ||
*/ | ||
CreeperJukebox(Jukebox blockState) | ||
{ | ||
super(blockState); | ||
|
||
} | ||
|
||
/* | ||
* In Minecraft 1.13, discs inserted into jukeboxes preserve item meta. Craftbukkit does not properly handle this. | ||
* The only way to "prevent" the disc dropping is to remove it immediately after it spawns. | ||
* | ||
* @see com.nitnelave.CreeperHeal.block.Replaceable#remove() | ||
*/ | ||
@Override | ||
public void remove() | ||
{ | ||
Jukebox jukebox = ((Jukebox) blockState); | ||
|
||
Listener recordDropListener = null; | ||
|
||
if (!CreeperConfig.getWorld(getWorld()).getBool(WCfgVal.DROP_CHEST_CONTENTS)) | ||
{ | ||
recordDropListener = new RecordDropListener(jukebox.getPlaying()); | ||
Bukkit.getPluginManager().registerEvents(recordDropListener, CreeperHeal.getInstance()); | ||
} | ||
|
||
super.remove(); | ||
|
||
if (recordDropListener != null) { | ||
HandlerList.unregisterAll(recordDropListener); | ||
} | ||
} | ||
|
||
/* | ||
* @see com.nitnelave.CreeperHeal.block.Replaceable#update() | ||
*/ | ||
@Override | ||
public void update() | ||
{ | ||
if (CreeperConfig.getWorld(getWorld()).getBool(WCfgVal.DROP_CHEST_CONTENTS)) | ||
blockState.getBlock().setType(Material.JUKEBOX); | ||
else | ||
super.update(); | ||
} | ||
|
||
private class RecordDropListener implements Listener { | ||
|
||
private final Material record; | ||
|
||
private RecordDropListener(Material record) { | ||
this.record = record; | ||
} | ||
|
||
@EventHandler(ignoreCancelled = true) | ||
public void onItemSpawn(ItemSpawnEvent event) { | ||
if (event.getEntity().getItemStack().getType() == record) { | ||
event.setCancelled(true); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/main/java/com/nitnelave/CreeperHeal/block/CreeperShulkerBox.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.nitnelave.CreeperHeal.block; | ||
|
||
import com.nitnelave.CreeperHeal.config.CreeperConfig; | ||
import com.nitnelave.CreeperHeal.config.WCfgVal; | ||
import org.bukkit.Location; | ||
import org.bukkit.block.BlockState; | ||
import org.bukkit.block.ShulkerBox; | ||
import org.bukkit.inventory.InventoryHolder; | ||
import org.bukkit.inventory.ItemStack; | ||
|
||
/** | ||
* Shulker box implementation of CreeperBlock. | ||
* | ||
* @author Jikoo | ||
*/ | ||
public class CreeperShulkerBox extends CreeperBlock | ||
{ | ||
|
||
private final ItemStack[] contents; | ||
|
||
CreeperShulkerBox(ShulkerBox blockState) | ||
{ | ||
super(blockState); | ||
this.contents = blockState.getInventory().getContents(); | ||
} | ||
|
||
/* | ||
* @see com.nitnelave.CreeperHeal.block.Replaceable#drop(boolean) | ||
*/ | ||
@Override | ||
public boolean drop(boolean forced) | ||
{ | ||
Location location = blockState.getLocation().add(0.5, 0.5, 0.5); | ||
if (forced || CreeperConfig.shouldDrop()) | ||
{ | ||
// Drop shulker with contents inside | ||
ItemStack itemStack = new ItemStack(blockState.getType()); | ||
blockState.getWorld().dropItemNaturally(location, itemStack); | ||
} | ||
// Always drop container contents | ||
for (ItemStack itemStack : contents) { | ||
if (itemStack == null) | ||
continue; | ||
blockState.getWorld().dropItemNaturally(location, itemStack); | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public void update() | ||
{ | ||
super.update(); | ||
|
||
if (CreeperConfig.getWorld(getWorld()).getBool(WCfgVal.DROP_CHEST_CONTENTS)) | ||
return; | ||
|
||
BlockState newState = blockState.getBlock().getState(); | ||
if (newState instanceof InventoryHolder) | ||
((InventoryHolder) newState).getInventory().setContents(contents); | ||
} | ||
|
||
} |