From ff1f631afd50e445fe9584065c98dbab94b3a626 Mon Sep 17 00:00:00 2001 From: CJ Burkey Date: Tue, 4 Jan 2022 02:02:32 -0500 Subject: [PATCH] Add preventAdjacent list to config and fix config warning --- .../java/com/cjburkey/claimchunk/Utils.java | 1 + .../config/ClaimChunkWorldProfile.java | 22 +++++++++++++++- .../event/WorldProfileEventHandler.java | 18 +++++++------ .../com/cjburkey/claimchunk/lib/Metrics.java | 21 ++++++++-------- .../smartcommand/CCBukkitCommand.java | 25 +++++++++++++------ 5 files changed, 61 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/cjburkey/claimchunk/Utils.java b/src/main/java/com/cjburkey/claimchunk/Utils.java index 8276e15b..f3c091f2 100644 --- a/src/main/java/com/cjburkey/claimchunk/Utils.java +++ b/src/main/java/com/cjburkey/claimchunk/Utils.java @@ -1,6 +1,7 @@ package com.cjburkey.claimchunk; import lombok.Getter; + import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.TextComponent; diff --git a/src/main/java/com/cjburkey/claimchunk/config/ClaimChunkWorldProfile.java b/src/main/java/com/cjburkey/claimchunk/config/ClaimChunkWorldProfile.java index 4a88e3ae..faf2c1ef 100644 --- a/src/main/java/com/cjburkey/claimchunk/config/ClaimChunkWorldProfile.java +++ b/src/main/java/com/cjburkey/claimchunk/config/ClaimChunkWorldProfile.java @@ -68,7 +68,7 @@ public class ClaimChunkWorldProfile { public SpreadProfile pistonExtend = new SpreadProfile("allow_piston"); /** A set of blocks for which to deny neighboring (same) block placement. */ - public HashSet preventAdjacent = + public final HashSet preventAdjacent = new HashSet<>(Arrays.asList(Material.CHEST, Material.TRAPPED_CHEST)); /** A set of commands that should be denied for un-owning players in claimed chunks. */ @@ -443,6 +443,9 @@ public void toCCConfig(@NotNull CCConfig config) { // Piston protection configs pistonExtend.toCCConfig(config); + // Change types of adjacent blocks to check, empty this list to stop checking + config.setList("_.preventAdjacent", preventAdjacent); + // Command blocking configs config.setList("claimedChunks.other.blockedCmds", blockedCmdsInDiffClaimed); config.setList("claimedChunks.owned.blockedCmds", blockedCmdsInOwnClaimed); @@ -524,6 +527,23 @@ public void fromCCConfig(@NotNull CCConfig config) { // Load piston protection properties pistonExtend.fromCCConfig(config); + // Load list of adjacent block types to check + preventAdjacent.clear(); + config.getStrList("_.preventAdjacent").stream() + .map( + blockType -> { + Material material = Material.getMaterial(blockType); + if (material == null) { + Utils.warn( + "Material type \"%s\" not found when loading from" + + " preventAdjacent, this one will be removed!", + blockType); + } + return material; + }) + .filter(Objects::nonNull) + .forEach(preventAdjacent::add); + // Load blocked commands for unowned claimed chunks // (`getCommands()` will verify the commands actually exist) blockedCmdsInDiffClaimed.clear(); diff --git a/src/main/java/com/cjburkey/claimchunk/event/WorldProfileEventHandler.java b/src/main/java/com/cjburkey/claimchunk/event/WorldProfileEventHandler.java index bdc448ad..528a6488 100644 --- a/src/main/java/com/cjburkey/claimchunk/event/WorldProfileEventHandler.java +++ b/src/main/java/com/cjburkey/claimchunk/event/WorldProfileEventHandler.java @@ -549,8 +549,12 @@ public void onLiquidAndDragonEggSpread(BlockFromToEvent event) { // Prevent dragon egg teleportation if interaction is disabled in this chunk if (blockType == Material.DRAGON_EGG) { // Get the profile and chunk information - ClaimChunkWorldProfile profile = claimChunk.getProfileManager().getProfile(event.getBlock().getWorld().getName()); - boolean isClaimed = claimChunk.getChunkHandler().isClaimed(event.getBlock().getChunk()); + ClaimChunkWorldProfile profile = + claimChunk + .getProfileManager() + .getProfile(event.getBlock().getWorld().getName()); + boolean isClaimed = + claimChunk.getChunkHandler().isClaimed(event.getBlock().getChunk()); // Cancel this event if a non-access player wouldn't have access to this dragon egg. // (We can't check if a player did it, I don't believe?) @@ -719,7 +723,7 @@ private void onEntityEvent( if (!profile.canAccessEntity(chunkOwner != null, isOwnerOrAccess, entity, accessType) && (chunkOwner == null || canOthersAccessPlysChunks( - chunkOwner, claimChunk.getServer(), profile))) { + chunkOwner, claimChunk.getServer(), profile))) { // cancel event cancel.run(); @@ -770,7 +774,7 @@ private void onBlockAdjacentCheck( && neighborOwner != chunkOwner && !isOwnerOrAccess && canOthersAccessPlysChunks( - chunkOwner, claimChunk.getServer(), profile)) { + chunkOwner, claimChunk.getServer(), profile)) { // cancel event cancel.run(); @@ -842,7 +846,7 @@ private boolean onBlockEvent( if (profile.enabled && (chunkOwner == null || canOthersAccessPlysChunks( - chunkOwner, claimChunk.getServer(), profile)) + chunkOwner, claimChunk.getServer(), profile)) && !profile.canAccessBlock( chunkOwner != null, isOwnerOrAccess, @@ -933,7 +937,7 @@ private void onExplosionEvent(@NotNull World world, @NotNull Collection b UUID owner = chunkHandler.getOwner(c); return (owner == null || canOthersAccessPlysChunks( - owner, claimChunk.getServer(), worldProfile)) + owner, claimChunk.getServer(), worldProfile)) && !worldProfile.getBlockAccess( owner != null, worldName, block.getType()) .allowExplosion; @@ -1032,7 +1036,7 @@ private void onPistonAction( // If we find any protected chunks, we're gonna have to check further if (targetChunkOwner != null && canOthersAccessPlysChunks( - targetChunkOwner, claimChunk.getServer(), profile)) { + targetChunkOwner, claimChunk.getServer(), profile)) { onlineOfflineProtection = false; break; } diff --git a/src/main/java/com/cjburkey/claimchunk/lib/Metrics.java b/src/main/java/com/cjburkey/claimchunk/lib/Metrics.java index 8aeddf4a..9d4c7953 100644 --- a/src/main/java/com/cjburkey/claimchunk/lib/Metrics.java +++ b/src/main/java/com/cjburkey/claimchunk/lib/Metrics.java @@ -35,6 +35,7 @@ import javax.net.ssl.HttpsURLConnection; +@SuppressWarnings("ALL") public class Metrics { private final Plugin plugin; @@ -62,16 +63,16 @@ public Metrics(JavaPlugin plugin, int serviceId) { config.addDefault("logResponseStatusText", false); // Inform the server owners about bStats config.options() - .header( - "bStats (https://bStats.org) collects some basic information for plugin" - + " authors, like how\n" - + "many people use their plugin and their total player count. It's" - + " recommended to keep bStats\n" - + "enabled, but if you're not comfortable with this, you can turn" - + " this setting off. There is no\n" - + "performance penalty associated with having metrics enabled, and" - + " data sent to bStats is fully\n" - + "anonymous.") + .setHeader( + Arrays.asList( + "bStats (https://bStats.org) collects some basic information" + + " for plugin authors, like how many people use their" + + " plugin and their total player count.", + "It's recommended to keep bStats enabled, but if you're not" + + " comfortable with this, you can turn this setting off.", + "There is no performance penalty associated with having metrics" + + " enabled or disabled, and data sent to bStats is fully" + + " anonymous.")) .copyDefaults(true); try { config.save(configFile); diff --git a/src/main/java/com/cjburkey/claimchunk/smartcommand/CCBukkitCommand.java b/src/main/java/com/cjburkey/claimchunk/smartcommand/CCBukkitCommand.java index 5deaf1f8..98a63ba2 100644 --- a/src/main/java/com/cjburkey/claimchunk/smartcommand/CCBukkitCommand.java +++ b/src/main/java/com/cjburkey/claimchunk/smartcommand/CCBukkitCommand.java @@ -7,7 +7,6 @@ import org.bukkit.command.*; import org.bukkit.command.defaults.BukkitCommand; -import org.bukkit.plugin.PluginManager; import org.jetbrains.annotations.NotNull; import java.lang.reflect.Field; @@ -58,10 +57,16 @@ public boolean execute( return baseCommand.onCommand(sender, this, commandLabel, args); } - private static Object getPrivateField(Object object, String field)throws SecurityException, - NoSuchFieldException, IllegalArgumentException, IllegalAccessException { + private static Object getPrivateField(Object object, String field) + throws SecurityException, NoSuchFieldException, IllegalArgumentException, + IllegalAccessException { Class clazz = object.getClass(); - Field objectField = field.equals("commandMap") ? clazz.getDeclaredField(field) : field.equals("knownCommands") ? clazz.getSuperclass().getDeclaredField(field) : null; + Field objectField = + field.equals("commandMap") + ? clazz.getDeclaredField(field) + : field.equals("knownCommands") + ? clazz.getSuperclass().getDeclaredField(field) + : null; Objects.requireNonNull(objectField).setAccessible(true); Object result = objectField.get(object); objectField.setAccessible(false); @@ -70,19 +75,23 @@ private static Object getPrivateField(Object object, String field)throws Securit public void removeFromMap() { try { - Object result = getPrivateField(claimChunk.getServer().getPluginManager(), "commandMap"); + Object result = + getPrivateField(claimChunk.getServer().getPluginManager(), "commandMap"); SimpleCommandMap commandMap = (SimpleCommandMap) result; Object map = getPrivateField(commandMap, "knownCommands"); @SuppressWarnings("unchecked") HashMap knownCommands = (HashMap) map; knownCommands.remove(this.getName()); - for (String alias : this.getAliases()){ - if(knownCommands.containsKey(alias) && knownCommands.get(alias).toString().contains(claimChunk.getName())){ + for (String alias : this.getAliases()) { + if (knownCommands.containsKey(alias) + && knownCommands.get(alias).toString().contains(claimChunk.getName())) { knownCommands.remove(alias); } } } catch (Exception e) { - Utils.err("Failed to unregister command! If you are reloading, updates to permissions won't appear until a server reboot."); + Utils.err( + "Failed to unregister command! If you are reloading, updates to permissions" + + " won't appear until a server reboot."); if (Utils.getDebugEnableOverride()) { e.printStackTrace(); }