Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.inventory.InventoryClickEvent;
Expand Down Expand Up @@ -165,10 +166,20 @@ public void onInteract(PlayerInteractEvent e) {
}
}

@EventHandler
/** ItemsAdder breaks the furniture even when the tool click was cancelled, so working the bench would destroy it. */
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
public void onToolBreak(FurnitureBreakEvent e) {
if (!isStationFurniture(e)) return;
Player breaker = e.getPlayer();
if (breaker != null && isStationTool(breaker.getInventory().getItemInMainHand())) {
e.setCancelled(true);
}
}

/** Runs at MONITOR so a break cancelled by any other listener keeps its bench and project. */
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onFurnitureBreak(FurnitureBreakEvent e) {
if (e.getNamespacedID() == null) return;
if (!e.getNamespacedID().equalsIgnoreCase(furnitureId())) return;
if (!isStationFurniture(e)) return;

Location loc = e.getBukkitEntity().getLocation().getBlock().getLocation();
WoodStation station = get(loc);
Expand Down Expand Up @@ -338,9 +349,7 @@ private void handleLeftClick(PlayerInteractEvent e) {
return;
}

NBTItem nbt = NBTItem.get(hand);
if (!nbt.hasType()) return;
CraftingHit hit = HitLoader.getByTool(nbt.getType() + "." + nbt.getString("MMOITEMS_ITEM_ID"));
CraftingHit hit = matchHit(hand);
if (hit == null) return;

e.setCancelled(true);
Expand Down Expand Up @@ -422,6 +431,24 @@ private boolean isBranding(ItemStack item) {
return TLibs.getItemAPI().getChecker().checkItemWithPath(item, Cache.brandingTool);
}

private boolean isStationFurniture(FurnitureBreakEvent e) {
return e.getNamespacedID() != null && e.getNamespacedID().equalsIgnoreCase(furnitureId());
}

/** Resolves the MMOItems TYPE.ID of a held tool to its hit, or null when it is not a hit tool. */
private CraftingHit matchHit(ItemStack item) {
if (item == null || item.getType().isAir()) return null;
NBTItem nbt = NBTItem.get(item);
if (!nbt.hasType()) return null;
return HitLoader.getByTool(nbt.getType() + "." + nbt.getString("MMOITEMS_ITEM_ID"));
}

/** True for the branding tool and every configured hit tool. */
private boolean isStationTool(ItemStack item) {
if (item == null || item.getType().isAir()) return false;
return isBranding(item) || matchHit(item) != null;
}

private WoodMaterial matchMaterial(ItemStack item) {
if (item == null || item.getType().isAir()) return null;
for (WoodMaterial material : MaterialLoader.get().values()) {
Expand Down