Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
JWJUN233233 committed Feb 17, 2024
1 parent f1a56be commit 7a84b7a
Showing 1 changed file with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
import io.github.thebusybiscuit.slimefun4.implementation.items.cargo.CargoNode;
import javax.annotation.Nonnull;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Container;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Directional;
import org.bukkit.block.data.Rotatable;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector;

/**
* This {@link Listener} is solely responsible for preventing Cargo Nodes from being placed
Expand All @@ -25,16 +31,38 @@ public CargoNodeListener(@Nonnull Slimefun plugin) {

@EventHandler(ignoreCancelled = true)
public void onCargoNodePlace(BlockPlaceEvent e) {
Block b = e.getBlock();
if (isCargoNode(e.getItemInHand())) {
Block b = e.getBlock();

// || !e.getBlockReplacedState().getType().isAir() 这会导致#832
if (isCargoNode(e.getItemInHand()) && (b.getY() != e.getBlockAgainst().getY())) {
Slimefun.getLocalization().sendMessage(e.getPlayer(), "machines.CARGO_NODES.must-be-placed", true);
e.setCancelled(true);
// || !e.getBlockReplacedState().getType().isAir() 这会导致#832
Block against = e.getBlock();
BlockData blockData = b.getBlockData();
BlockFace blockFace = null;
Vector vector;
if (blockData instanceof Directional directional){
blockFace = directional.getFacing();
}
else if (blockData instanceof Rotatable rotatable){
blockFace = rotatable.getRotation();
}
if (blockFace == null){
vector = new Vector();
}
else {
vector = blockFace.getOppositeFace().getDirection();
}
Block realAgainst = against.getWorld().getBlockAt(against.getX() + (int) vector.getX(), against.getY() + (int) vector.getY(), against.getZ() + (int) vector.getZ());
if (!isContainer(realAgainst)) {
Slimefun.getLocalization().sendMessage(e.getPlayer(), "machines.CARGO_NODES.must-be-placed", true);
e.setCancelled(true);
}
}
}

private boolean isCargoNode(@Nonnull ItemStack item) {
return SlimefunItem.getByItem(item) instanceof CargoNode;
}
private boolean isContainer(@Nonnull Block block) {
return block.getState() instanceof Container || Slimefun.getDatabaseManager().getBlockDataController().getBlockData(block.getLocation()) != null;
}
}

0 comments on commit 7a84b7a

Please sign in to comment.