Skip to content

Commit

Permalink
Merge pull request #33 from darksaid98/fix/head-duplication
Browse files Browse the repository at this point in the history
fix: towny head duplication glitch
  • Loading branch information
kiranhart committed Jul 15, 2024
2 parents 1ac9796 + a79c809 commit c057796
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions src/main/java/ca/tweetzy/skulls/listeners/SkullBlockListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@
import ca.tweetzy.skulls.hooks.WorldGuardHook;
import ca.tweetzy.skulls.impl.PlacedSkullLocation;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.entity.Item;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockDropItemEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;

import java.util.UUID;

Expand Down Expand Up @@ -62,25 +65,32 @@ public void onSkullPlace(final BlockPlaceEvent event) {
}

@EventHandler()
public void onSkullBreak(final BlockBreakEvent event) {
public void onSkullDrop(final BlockDropItemEvent event) {
if (event.isCancelled()) return;
if (!WorldGuardHook.isAllowedBreak(event.getPlayer(), event.getBlock())) {
event.setCancelled(true);

final BlockState block = event.getBlockState();

if (!(block instanceof org.bukkit.block.Skull))
return;

if (block.getType() != CompMaterial.PLAYER_HEAD.parseMaterial() && block.getType() != CompMaterial.PLAYER_WALL_HEAD.parseMaterial())
return;
}

final Block block = event.getBlock();
if (!Skulls.getSkullManager().getPlacedSkulls().containsKey(block.getLocation()))
return;

if (block.getType() == CompMaterial.PLAYER_HEAD.parseMaterial() || block.getType() == CompMaterial.PLAYER_WALL_HEAD.parseMaterial()) {
if (!Skulls.getSkullManager().getPlacedSkulls().containsKey(block.getLocation())) return;
final PlacedSkull placedSkull = Skulls.getSkullManager().getPlacedSkulls().get(block.getLocation());
Skulls.getSkullManager().removePlacedSkull(placedSkull);

final PlacedSkull placedSkull = Skulls.getSkullManager().getPlacedSkulls().get(block.getLocation());
Skulls.getSkullManager().removePlacedSkull(placedSkull);
final Skull skull = Skulls.getSkullManager().getSkull(placedSkull.getSkullId());

event.setDropItems(false);
final Skull skull = Skulls.getSkullManager().getSkull(placedSkull.getSkullId());
block.getWorld().dropItemNaturally(block.getLocation(), skull.getItemStack());
}
for (Item item : event.getItems()) {
ItemStack itemStack = item.getItemStack();
if (!(itemStack.getItemMeta() instanceof SkullMeta))
continue;

item.setItemStack(skull.getItemStack());
break;
}
}
}

0 comments on commit c057796

Please sign in to comment.