Skip to content

Commit

Permalink
improve trapdoor crash prevention
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Jan 22, 2024
1 parent 242e536 commit 462f6f3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.block.BlockRedstoneEvent;

import java.time.Duration;
Expand Down Expand Up @@ -76,8 +78,28 @@ private void onRedstonePowerTrapdoor(BlockRedstoneEvent event) {
trapdoorActivationCache.put(trapdoorLoc, activationCount);
if (activationCount > trapdoorActivationLimit) {
block.setType(AIR);
if (logIsEnabled) LogUtil.moduleLog(Level.WARNING, name(), "Prevented trapdoor crash at: " + trapdoorLoc);
if (logIsEnabled) LogUtil.moduleLog(Level.WARNING, name(), "Prevented possible trapdoor crash at\n" +
"x: "+trapdoorLoc.getX() + ", " +
"y: "+trapdoorLoc.getY() + ", " +
"z: "+trapdoorLoc.getX() + ", " +
"world: "+trapdoorLoc.getWorld().getName());
}
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onBlockPlace(BlockPlaceEvent event) {
Block block = event.getBlock();
if (block == null) return;
if (!MaterialUtil.REDSTONE.contains(block.getType())) return;

if (MaterialUtil.TRAPDOORS.contains(block.getRelative(BlockFace.DOWN).getType())) {
if (logIsEnabled) LogUtil.moduleLog(Level.WARNING, name(), "Prevented possible trapdoor crash at\n" +
"x: "+block.getLocation().getX() + ", " +
"y: "+block.getLocation().getY() + ", " +
"z: "+block.getLocation().getX() + ", " +
"world: "+block.getLocation().getWorld().getName());
event.setCancelled(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ public class MaterialUtil {
.map(XMaterial::parseMaterial)
.collect(Collectors.toCollection(HashSet::new));

public static final Set<Material> REDSTONE = Stream.of(
XMaterial.REDSTONE,
XMaterial.REDSTONE_WIRE
).filter(XMaterial::isSupported)
.map(XMaterial::parseMaterial)
.collect(Collectors.toCollection(HashSet::new));

public static final Set<Material> INDESTRUCTIBLES = Stream.of(
XMaterial.BEDROCK,
XMaterial.END_PORTAL_FRAME,
Expand Down

0 comments on commit 462f6f3

Please sign in to comment.