Skip to content

Commit

Permalink
Keeps Head Name and Lore when broken with water
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelGodOfwar committed Jan 23, 2021
1 parent 52b53cd commit 80d82eb
Showing 1 changed file with 42 additions and 36 deletions.
Expand Up @@ -75,15 +75,12 @@ public void onBlockDropItemEvent(BlockDropItemEvent event) {
}

}
/** Prevents player from breaking playerhead by water logging them */
/** Retains name loses lore */
/** Prevents player from removing playerhead NBT by water logging them */
@EventHandler()
public void onPlayerBucketEmpty(PlayerBucketEmptyEvent event) {
Block block = event.getBlock();
@Nonnull BlockState blockState2 = block.getState();
Material blockType2 = blockState2.getType();
if (blockType2 != Material.PLAYER_HEAD && blockType2 != Material.PLAYER_WALL_HEAD) return;
@Nonnull BlockState blockState = block.getState();
Location loc = block.getLocation();
@Nonnull BlockState blockState = block.getState();
Material blockType = blockState.getType();
if (blockType != Material.PLAYER_HEAD && blockType != Material.PLAYER_WALL_HEAD) return;
TileState skullState = (TileState) blockState;
Expand All @@ -96,46 +93,55 @@ public void onPlayerBucketEmpty(PlayerBucketEmptyEvent event) {
@Nonnull ItemStack itemstack = stackArray[0];
if (itemstack.getType() == Material.PLAYER_HEAD) {
@Nullable ItemMeta meta = itemstack.getItemMeta();
if (meta == null) return; // This shouldn't happen
if (meta == null) {
logWarn("meta=null");
return; // This shouldn't happen
}
meta.setDisplayName(name);
if (lore != null) meta.setLore(Arrays.asList(lore));
itemstack.setItemMeta(meta);

block.getWorld().dropItemNaturally(block.getLocation(), itemstack);
block.getDrops().clear();
block.setType(Material.AIR);
}
//event.setCancelled(true);
blockState.update(true, true);

BlockState blockS = block.getWorld().getBlockAt(loc).getState();
blockS.update(true, true);
}

/** Prevents player from breaking playerhead using running water */
/** Retains name loses lore */
/** Prevents player from removing playerhead NBT using running water */
@EventHandler
public void onLiquidFlow(BlockFromToEvent event) {
if (event.isCancelled()) {
return;
}
Block block = event.getToBlock();
if (block.getType() == Material.PLAYER_HEAD || block.getType() == Material.PLAYER_WALL_HEAD) {
@Nonnull BlockState blockState = block.getState();
Material blockType = blockState.getType();
if (blockType != Material.PLAYER_HEAD && blockType != Material.PLAYER_WALL_HEAD) return;
TileState skullState = (TileState) blockState;
@Nonnull PersistentDataContainer skullPDC = skullState.getPersistentDataContainer();
@Nullable String name = skullPDC.get(NAME_KEY, PersistentDataType.STRING);
@Nullable String[] lore = skullPDC.get(LORE_KEY, LORE_PDT);
if (name == null) return;
Collection<ItemStack> drops = block.getDrops();
ItemStack[] stackArray = drops.toArray(new ItemStack[0]);
@Nonnull ItemStack itemstack = stackArray[0];
if (itemstack.getType() == Material.PLAYER_HEAD) {
@Nullable ItemMeta meta = itemstack.getItemMeta();
if (meta == null) return; // This shouldn't happen
meta.setDisplayName(name);
if (lore != null) meta.setLore(Arrays.asList(lore));
itemstack.setItemMeta(meta);
}
//event.setCancelled(true);
} else {
return;
Location loc = block.getLocation();

@Nonnull BlockState blockState = block.getState();
Material blockType = blockState.getType();
if (blockType != Material.PLAYER_HEAD && blockType != Material.PLAYER_WALL_HEAD) return;
TileState skullState = (TileState) blockState;
@Nonnull PersistentDataContainer skullPDC = skullState.getPersistentDataContainer();
@Nullable String name = skullPDC.get(NAME_KEY, PersistentDataType.STRING);
@Nullable String[] lore = skullPDC.get(LORE_KEY, LORE_PDT);
if (name == null) return;
Collection<ItemStack> drops = block.getDrops();
ItemStack[] stackArray = drops.toArray(new ItemStack[0]);
@Nonnull ItemStack itemstack = stackArray[0];
if (itemstack.getType() == Material.PLAYER_HEAD) {
@Nullable ItemMeta meta = itemstack.getItemMeta();
if (meta == null) return; // This shouldn't happen
meta.setDisplayName(name);
if (lore != null) meta.setLore(Arrays.asList(lore));
itemstack.setItemMeta(meta);

block.getWorld().dropItemNaturally(block.getLocation(), itemstack);
block.getDrops().clear();
block.setType(Material.AIR);
event.setCancelled(true);
}

BlockState blockS = block.getWorld().getBlockAt(loc).getState();
blockS.update(true, true);
}

@Override
Expand Down

0 comments on commit 80d82eb

Please sign in to comment.