Skip to content

Commit

Permalink
added extra checks for block underneath the anvil
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan8or committed Dec 16, 2023
1 parent b8c67dd commit 68cf769
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion src/main/java/online/umbcraft/libraries/dupes/AnvilDupe.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
import online.umbcraft.libraries.GoldenDupes;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Item;
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.BlockBreakEvent;
import org.bukkit.event.block.BlockPistonExtendEvent;
import org.bukkit.event.block.BlockPistonRetractEvent;
import org.bukkit.event.inventory.*;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector;

import java.util.*;

Expand Down Expand Up @@ -41,6 +45,7 @@ public void onAnvilUse(final InventoryClickEvent e) {
return;

Location l = e.getClickedInventory().getLocation();
Location blockBelow = l.add(new Vector(0,-1,0));
ItemStack toDupe = e.getCurrentItem();
Player p = (Player) e.getWhoClicked();

Expand All @@ -52,12 +57,15 @@ public void onAnvilUse(final InventoryClickEvent e) {
if(l.getBlock().getType() != Material.DAMAGED_ANVIL)
return;

// preventing the anvil or the block beneath the anvil from being broken / pushed
anvilsInUse.add(l);
anvilsInUse.add(blockBelow);

// check if anvil was destroyed by next tick
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, () ->
{
anvilsInUse.remove(l);
anvilsInUse.remove(blockBelow);

if (l.getBlock().getType() == Material.AIR && toDupe != null) {
Item dropped = p.getWorld().dropItem(p.getLocation(), dupe(toDupe, toDupe.getAmount()));
Expand All @@ -68,11 +76,40 @@ public void onAnvilUse(final InventoryClickEvent e) {

@EventHandler(priority = EventPriority.NORMAL)
public void onBreakInUseAnvil(final BlockBreakEvent e) {
if(e.getBlock().getType() != Material.DAMAGED_ANVIL)
if(anvilsInUse.isEmpty())
return;

Location l = e.getBlock().getLocation();

if(anvilsInUse.contains(e.getBlock().getLocation())) {
e.setCancelled(true);
}
}

@EventHandler(priority = EventPriority.NORMAL)
public void onPistonPush(final BlockPistonExtendEvent e) {
if(!isPistoningSafe(e.getBlocks()))
e.setCancelled(true);
}

@EventHandler(priority = EventPriority.NORMAL)
public void onPistonPull(final BlockPistonRetractEvent e) {
if(!isPistoningSafe(e.getBlocks()))
e.setCancelled(true);
}

private boolean isPistoningSafe(List<Block> blocks) {
if(anvilsInUse.isEmpty())
return true;

for(Location anvilBlock : anvilsInUse) {
boolean involvesAnvil = blocks.stream()
.map(Block::getLocation)
.anyMatch((l) -> l.equals(anvilBlock));

if(involvesAnvil)
return false;
}
return true;
}
}

0 comments on commit 68cf769

Please sign in to comment.