Skip to content

Commit

Permalink
fix issues with end portal frame interactions in banned-blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Feb 8, 2024
1 parent b78a725 commit 7485470
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,33 @@
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockCanBuildEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerInteractEvent;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.stream.Collectors;

public class PreventPlacingBannedBlocks implements AnarchyExploitFixesModule, Listener {

private final Set<Material> bannedItems = new HashSet<>();
private final Set<Material> bannedMaterial;

public PreventPlacingBannedBlocks() {
shouldEnable();
AnarchyExploitFixes.getConfiguration().getList("illegals.ban-specific-blocks.banned-blocks", List.of(
this.bannedMaterial = AnarchyExploitFixes.getConfiguration().getList("illegals.ban-specific-blocks.banned-blocks", List.of(
"CHAIN_COMMAND_BLOCK", "COMMAND_BLOCK", "COMMAND_BLOCK_MINECART", "REPEATING_COMMAND_BLOCK",
"BEDROCK", "BARRIER", "STRUCTURE_BLOCK", "STRUCTURE_VOID", "END_PORTAL_FRAME", "END_PORTAL", "NETHER_PORTAL", "LIGHT"
)).forEach(configuredBlock -> {
try {
Material bannedMaterial = Material.valueOf(configuredBlock);
this.bannedItems.add(bannedMaterial);
} catch (IllegalArgumentException e) {
LogUtil.materialNotRecognized(Level.WARNING, name(), configuredBlock);
}
});
)).stream()
.map(confMaterial -> {
try {
return Material.valueOf(confMaterial);
} catch (IllegalArgumentException e) {
LogUtil.materialNotRecognized(Level.WARNING, name(), confMaterial);
return null;
}
})
.collect(Collectors.toCollection(HashSet::new));
}

@Override
Expand Down Expand Up @@ -62,17 +64,15 @@ public void disable() {
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onBlockPlace(BlockPlaceEvent event) {
if (bannedItems.contains(event.getBlock().getType())) {
event.setCancelled(true);
event.setBuild(false);
}
}
private void onInteract(PlayerInteractEvent event) {
if (!bannedMaterial.contains(event.getMaterial())) return;

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onBlockPlace(BlockCanBuildEvent event) {
if (bannedItems.contains(event.getMaterial())) {
event.setBuildable(false);
// This is a workaround because BlockPlaceEvent reports that players were placing end_portal_frames
// when they were actually just putting eyes into them.
if (event.getClickedBlock() != null && event.getClickedBlock().getType() == Material.END_PORTAL_FRAME) {
if (event.getItem() != null && event.getItem().getType() == Material.ENDER_EYE) return;
}

event.setCancelled(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void enable() {

@Override
public boolean shouldEnable() {
return AnarchyExploitFixes.getConfiguration().getBoolean("patches.crash-exploits.prevent-fast-world-teleport-crash.enable", true);
return AnarchyExploitFixes.getConfiguration().getBoolean("patches.crash-exploits.prevent-fast-world-teleport-crash.enable", false);
}

@Override
Expand All @@ -65,7 +65,7 @@ private void onTeleport(EntityTeleportEvent event) {
if (recentWorldChangers.contains(event.getEntity().getUniqueId())) {
event.setCancelled(true);
if (logIsEnabled) LogUtil.moduleLog(Level.INFO, name(), "Cancelled too fast world teleport of entity: " +
event.getEntityType().name() + " at \n" +
event.getEntityType().name() + " at " +
"x: "+event.getEntity().getLocation().getX() + ", " +
"y: "+event.getEntity().getLocation().getY() + ", " +
"z: "+event.getEntity().getLocation().getZ() + ", " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void onStructureGrow(StructureGrowEvent event) {
for (final BlockState blockState : event.getBlocks()) {
if (MaterialUtil.isIndestructible(world.getBlockAt(blockState.getLocation()).getType())) {
event.setCancelled(true);
LogUtil.moduleLog(Level.INFO, name(), "Prevented permanent block break by growing a structure at\n" +
LogUtil.moduleLog(Level.INFO, name(), "Prevented permanent block break by growing a structure at " +
"x: "+blockState.getLocation().getX() + ", " +
"y: "+blockState.getLocation().getY() + ", " +
"z: "+blockState.getLocation().getZ() + ", " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ public class EntityUtil {

public static final Set<EntityType> BOATS = Stream.of(
EntityType.BOAT,
EntityType.CHEST_BOAT
).collect(Collectors.toCollection(HashSet::new));
EntityType.CHEST_BOAT)
.collect(Collectors.toCollection(HashSet::new));

public static final Set<EntityType> ITEM_FRAMES = Stream.of(
EntityType.ITEM_FRAME,
EntityType.GLOW_ITEM_FRAME
).collect(Collectors.toCollection(HashSet::new));
EntityType.GLOW_ITEM_FRAME)
.collect(Collectors.toCollection(HashSet::new));
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,26 @@
import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule;
import me.moomoo.anarchyexploitfixes.utils.LogUtil;
import me.moomoo.anarchyexploitfixes.utils.models.ExpiringSet;
import org.bukkit.Material;
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.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.PlayerInventory;

import java.time.Duration;
import java.util.*;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.stream.Collectors;

public class PreventPlacingBannedBlocks implements AnarchyExploitFixesModule, Listener {

private final ExpiringSet<UUID> playersPuttingEyesInFrames;
private final Set<Material> bannedMaterial = new HashSet<>();
private final Set<Material> bannedMaterial;
private final Material ENDER_EYE, END_PORTAL_FRAME;

public PreventPlacingBannedBlocks() {
shouldEnable();
this.playersPuttingEyesInFrames = new ExpiringSet<>(Duration.ofSeconds(1));

// default list entries based on version
List<String> defaults;
Expand All @@ -39,14 +36,17 @@ public PreventPlacingBannedBlocks() {
defaults = Arrays.asList("BEDROCK", "BARRIER", "COMMAND", "STRUCTURE_BLOCK", "ENDER_PORTAL_FRAME");
}

for (String configuredBlock : AnarchyExploitFixes.getConfiguration().getList("illegals.ban-specific-blocks.banned-blocks", defaults)) {
try {
Material bannedMaterial = Material.valueOf(configuredBlock);
this.bannedMaterial.add(bannedMaterial);
} catch (IllegalArgumentException e) {
LogUtil.materialNotRecognized(Level.WARNING, name(), configuredBlock);
}
}
this.bannedMaterial = AnarchyExploitFixes.getConfiguration().getList("illegals.ban-specific-blocks.banned-blocks", defaults)
.stream()
.map(confMaterial -> {
try {
return Material.valueOf(confMaterial);
} catch (IllegalArgumentException e) {
LogUtil.materialNotRecognized(Level.WARNING, name(), confMaterial);
return null;
}
})
.collect(Collectors.toCollection(HashSet::new));

this.ENDER_EYE = XMaterial.ENDER_EYE.parseMaterial();
this.END_PORTAL_FRAME = XMaterial.END_PORTAL_FRAME.parseMaterial();
Expand All @@ -73,30 +73,16 @@ public boolean shouldEnable() {
return AnarchyExploitFixes.getConfiguration().getBoolean("illegals.ban-specific-blocks.prevent-placing", true);
}

// Try to work around a bug in older versions, where it looks like the player placed
// an END_PORTAL_FRAME for the plugin when listening to BlockPlaceEvent.
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
private void onPlaceEyeInEndFrame(PlayerInteractEvent event) {
if (!bannedMaterial.contains(END_PORTAL_FRAME)) return; // If Frame is not banned, no need to continue
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return;
if (event.getClickedBlock().getType() != END_PORTAL_FRAME) return;
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onInteract(PlayerInteractEvent event) {
if (!bannedMaterial.contains(event.getMaterial())) return;

final PlayerInventory playerInventory = event.getPlayer().getInventory();
if (
playerInventory.getItemInMainHand().getType() == ENDER_EYE
|| playerInventory.getItemInOffHand().getType() == ENDER_EYE
) {
playersPuttingEyesInFrames.add(event.getPlayer().getUniqueId());
// This is a workaround because BlockPlaceEvent reports that players were placing end_portal_frames
// when they were actually just putting eyes into them.
if (event.getClickedBlock() != null && event.getClickedBlock().getType() == END_PORTAL_FRAME) {
if (event.getItem() != null && event.getItem().getType() == ENDER_EYE) return;
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onBlockPlace(BlockPlaceEvent event) {
if (
bannedMaterial.contains(event.getBlock().getType())
&& !playersPuttingEyesInFrames.contains(event.getPlayer().getUniqueId())
) {
event.setCancelled(true);
}
event.setCancelled(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private void onRedstonePowerTrapdoor(BlockRedstoneEvent event) {
trapdoorActivationCache.put(trapdoorLoc, activationCount);
if (activationCount > trapdoorActivationLimit) {
block.setType(AIR);
if (logIsEnabled) LogUtil.moduleLog(Level.WARNING, name(), "Prevented possible trapdoor crash at\n" +
if (logIsEnabled) LogUtil.moduleLog(Level.WARNING, name(), "Prevented possible trapdoor crash at " +
"x: "+trapdoorLoc.getX() + ", " +
"y: "+trapdoorLoc.getY() + ", " +
"z: "+trapdoorLoc.getZ() + ", " +
Expand All @@ -95,7 +95,7 @@ private void onBlockPlace(BlockPlaceEvent event) {

if (MaterialUtil.TRAPDOORS.contains(block.getRelative(BlockFace.DOWN).getType())) {
event.setCancelled(true);
if (logIsEnabled) LogUtil.moduleLog(Level.WARNING, name(), "Prevented possible trapdoor crash at\n" +
if (logIsEnabled) LogUtil.moduleLog(Level.WARNING, name(), "Prevented possible trapdoor crash at " +
"x: "+block.getLocation().getX() + ", " +
"y: "+block.getLocation().getY() + ", " +
"z: "+block.getLocation().getZ() + ", " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private void onTeleport(EntityTeleportEvent event) {
if (recentWorldChangers.contains(event.getEntity().getUniqueId())) {
event.setCancelled(true);
if (logIsEnabled) LogUtil.moduleLog(Level.INFO, name(), "Cancelled too fast world teleport of entity: " +
event.getEntityType().name() + " at \n" +
event.getEntityType().name() + " at " +
"x: "+event.getEntity().getLocation().getX() + ", " +
"y: "+event.getEntity().getLocation().getY() + ", " +
"z: "+event.getEntity().getLocation().getZ() + ", " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void onStructureGrow(StructureGrowEvent event) {
for (final BlockState blockState : event.getBlocks()) {
if (MaterialUtil.INDESTRUCTIBLES.contains(world.getBlockAt(blockState.getLocation()).getType())) {
event.setCancelled(true);
LogUtil.moduleLog(Level.INFO, name(), "Prevented permanent block break by growing a structure at\n" +
LogUtil.moduleLog(Level.INFO, name(), "Prevented permanent block break by growing a structure at " +
"x: "+blockState.getLocation().getX() + ", " +
"y: "+blockState.getLocation().getY() + ", " +
"z: "+blockState.getLocation().getZ() + ", " +
Expand Down

0 comments on commit 7485470

Please sign in to comment.