diff --git a/pom.xml b/pom.xml index 1f178c2..c9f27c3 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ groupId Explosion-Control - 1.0.3 + 1.0.4-SNAPSHOT 8 diff --git a/src/main/java/com/johnymuffin/beta/tntcontrol/ExplosionControl.java b/src/main/java/com/johnymuffin/beta/tntcontrol/ExplosionControl.java index 40a0662..7dbeda4 100644 --- a/src/main/java/com/johnymuffin/beta/tntcontrol/ExplosionControl.java +++ b/src/main/java/com/johnymuffin/beta/tntcontrol/ExplosionControl.java @@ -1,8 +1,12 @@ package com.johnymuffin.beta.tntcontrol; -import java.util.ArrayList; +import java.io.File; import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; import java.util.logging.Logger; + +import com.johnymuffin.beta.tntcontrol.config.ExplosionControlSettings; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.plugin.Plugin; @@ -16,25 +20,38 @@ public class ExplosionControl extends JavaPlugin { private ExplosionControl plugin; - private ArrayList blockWhitelist = new ArrayList<>(Arrays.asList(Material.DIRT, + private ExplosionControlSettings settings; + + public static final Set blockWhitelist = new HashSet<>(Arrays.asList(Material.DIRT, Material.STONE, Material.GRAVEL, Material.COBBLESTONE, Material.COAL_ORE, Material.REDSTONE_ORE, Material.GLOWING_REDSTONE_ORE, Material.IRON_ORE, Material.DIAMOND_ORE, Material.GOLD_ORE, Material.LAPIS_ORE, Material.GRASS, Material.LONG_GRASS, Material.SAND, Material.SANDSTONE, Material.SAPLING, Material.LOG, Material.LEAVES, Material.DEAD_BUSH, Material.YELLOW_FLOWER, Material.RED_ROSE, Material.RED_MUSHROOM, Material.BROWN_MUSHROOM, Material.TORCH, Material.SNOW, - Material.CACTUS, Material.CLAY, Material.SUGAR_CANE_BLOCK, Material.ICE)); + Material.CACTUS, Material.CLAY, Material.SUGAR_CANE_BLOCK, Material.ICE, Material.TNT, Material.FIRE, + Material.LAVA, Material.WATER, Material.STATIONARY_LAVA, Material.STATIONARY_WATER)); - private ArrayList blockCheckBlacklist = new ArrayList<>(Arrays.asList(Material.DIODE_BLOCK_OFF, Material.DIODE_BLOCK_ON, + public static final Set blockBlacklist = new HashSet<>(Arrays.asList(Material.DIODE_BLOCK_OFF, Material.DIODE_BLOCK_ON, Material.STONE_BUTTON, Material.LEVER, Material.RAILS, Material.DETECTOR_RAIL, Material.POWERED_RAIL, Material.REDSTONE_WIRE, Material.REDSTONE_TORCH_OFF, Material.REDSTONE_TORCH_ON, Material.SIGN_POST, Material.WOOD_PLATE, Material.STONE_PLATE, Material.WOODEN_DOOR, Material.IRON_DOOR_BLOCK, Material.CAKE_BLOCK, - Material.WALL_SIGN, Material.TRAP_DOOR, Material.LADDER)); + Material.WALL_SIGN, Material.TRAP_DOOR, Material.LADDER, Material.SUGAR_CANE_BLOCK, Material.WATER, Material.STATIONARY_WATER, + Material.LAVA, Material.STATIONARY_LAVA, Material.SAPLING)); public void onEnable() { log = getServer().getLogger(); this.plugin = this; pdf = this.plugin.getDescription(); log.info("[" + pdf.getName() + "] Is loading"); + + File dataFolder = plugin.getDataFolder(); + if (!dataFolder.exists()) { + dataFolder.mkdir(); + } + + File configFile = new File(dataFolder, "settings.yml"); + settings = new ExplosionControlSettings(configFile); + ExplosionControlListener explosionControlListener = new ExplosionControlListener(this.plugin); Bukkit.getServer().getPluginManager().registerEvents(explosionControlListener, (Plugin)this.plugin); } @@ -47,11 +64,5 @@ public static void logInfo(String s) { log.info("[" + pdf.getName() + "] " + s); } - public ArrayList getBlockWhitelist() { - return this.blockWhitelist; - } - - public ArrayList getBlockCheckBlacklist() { - return this.blockCheckBlacklist; - } -} \ No newline at end of file + public ExplosionControlSettings getSettings() { return settings; } +} diff --git a/src/main/java/com/johnymuffin/beta/tntcontrol/ExplosionControlListener.java b/src/main/java/com/johnymuffin/beta/tntcontrol/ExplosionControlListener.java index 2594235..1080b7b 100644 --- a/src/main/java/com/johnymuffin/beta/tntcontrol/ExplosionControlListener.java +++ b/src/main/java/com/johnymuffin/beta/tntcontrol/ExplosionControlListener.java @@ -1,6 +1,8 @@ package com.johnymuffin.beta.tntcontrol; import java.util.ArrayList; + +import com.johnymuffin.beta.tntcontrol.config.ExplosionControlSettings; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; @@ -11,11 +13,36 @@ import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityExplodeEvent; +import static com.johnymuffin.beta.tntcontrol.ExplosionControl.blockBlacklist; +import static com.johnymuffin.beta.tntcontrol.ExplosionControl.blockWhitelist; + public class ExplosionControlListener implements Listener { private ExplosionControl plugin; + private long lastResetTime = System.currentTimeMillis(); + private int explosionCount = 0; + + private ExplosionControlSettings settings; + + private final boolean tntChainingEnabled; + private final int tntChainingMaximum; + private final long tntChainingDurationMilliseconds; + + private final boolean tntBehaviorCancelNonWhitelist; + private final boolean tntBehaviorUseTNTWhitelist; + private final boolean tntBehaviorIgnoreSingleBlockPopOff; public ExplosionControlListener(ExplosionControl plugin) { this.plugin = plugin; + settings = plugin.getSettings(); + + // Load Config Values + tntChainingEnabled = settings.getConfigBoolean("settings.tnt-chaining.enabled"); + tntChainingMaximum = settings.getConfigInteger("settings.tnt-chaining.maximum"); + tntChainingDurationMilliseconds = settings.getConfigLong("settings.tnt-chaining.duration-milliseconds"); + + tntBehaviorCancelNonWhitelist = settings.getConfigBoolean("settings.tnt-behavior.cancel-entire-explosion-if-non-whitelisted.enabled"); + tntBehaviorUseTNTWhitelist = settings.getConfigBoolean("settings.tnt-behavior.use-tnt-whitelist.enabled"); + tntBehaviorIgnoreSingleBlockPopOff = settings.getConfigBoolean("settings.tnt-behavior.ignore-single-if-block-pop-off.enabled"); } @EventHandler @@ -29,40 +56,69 @@ public void onExplode(EntityExplodeEvent event) { event.setCancelled(true); return; } + + if(tntChainingEnabled){ + long currentTime = System.currentTimeMillis(); + + if (currentTime - lastResetTime >= tntChainingDurationMilliseconds) { + lastResetTime = currentTime; + explosionCount = 0; + } + } + ArrayList blocksToRemove = new ArrayList<>(); for (Block b : event.blockList()) { -// if (b.getLocation().getBlockY() >= 128) { -// blocksToRemove.add(b); -// continue; -// } - if (!this.plugin.getBlockWhitelist().contains(b.getType())){ - blocksToRemove.add(b); - continue; - } + Material type = b.getType(); - ArrayList blockCheckBlacklist = this.plugin.getBlockCheckBlacklist(); - if(blockCheckBlacklist.contains(b.getRelative(0, 1, 0).getType())){ - blocksToRemove.add(b); - continue; + if(tntBehaviorCancelNonWhitelist){ + if (!blockWhitelist.contains(type)){ + event.setCancelled(true); + return; + } } - if(blockCheckBlacklist.contains(b.getRelative(1, 0, 0).getType())){ - blocksToRemove.add(b); - continue; + else if(tntBehaviorUseTNTWhitelist){ + if (!blockWhitelist.contains(type)){ + blocksToRemove.add(b); + continue; + } } - if(blockCheckBlacklist.contains(b.getRelative(-1, 0, 0).getType())){ - blocksToRemove.add(b); - continue; + + if(tntBehaviorIgnoreSingleBlockPopOff) { + if(blockBlacklist.contains(b.getRelative(0, 1, 0).getType())){ + blocksToRemove.add(b); + continue; + } + if(blockBlacklist.contains(b.getRelative(1, 0, 0).getType())){ + blocksToRemove.add(b); + continue; + } + if(blockBlacklist.contains(b.getRelative(-1, 0, 0).getType())){ + blocksToRemove.add(b); + continue; + } + if(blockBlacklist.contains(b.getRelative(0, 0, 1).getType())){ + blocksToRemove.add(b); + continue; + } + if(blockBlacklist.contains(b.getRelative(0, 0, -1).getType())){ + blocksToRemove.add(b); + } } - if(blockCheckBlacklist.contains(b.getRelative(0, 0, 1).getType())){ - blocksToRemove.add(b); - continue; + + if(tntChainingEnabled && type == Material.TNT){ + if (explosionCount >= tntChainingMaximum) { + blocksToRemove.add(b); + } + else{ + explosionCount++; + } } - if(blockCheckBlacklist.contains(b.getRelative(0, 0, 1).getType())){ + else if(type == Material.TNT){ blocksToRemove.add(b); } } - for (Block b : blocksToRemove) - event.blockList().remove(b); + + event.blockList().removeAll(blocksToRemove); } @EventHandler @@ -76,4 +132,4 @@ public void onEntityDamageByBlock(EntityDamageByBlockEvent event) { if (event.getCause() == EntityDamageEvent.DamageCause.BLOCK_EXPLOSION) event.setCancelled(true); } -} \ No newline at end of file +} diff --git a/src/main/java/com/johnymuffin/beta/tntcontrol/config/ExplosionControlSettings.java b/src/main/java/com/johnymuffin/beta/tntcontrol/config/ExplosionControlSettings.java new file mode 100644 index 0000000..5ea0a4e --- /dev/null +++ b/src/main/java/com/johnymuffin/beta/tntcontrol/config/ExplosionControlSettings.java @@ -0,0 +1,95 @@ +package com.johnymuffin.beta.tntcontrol.config; + +import org.bukkit.util.config.Configuration; + +import java.io.File; + +public class ExplosionControlSettings extends Configuration { + + public ExplosionControlSettings(File settingsFile) { + super(settingsFile); + this.reload(); + } + + private void write() { + // Main + generateConfigOption("config-version", 1); + + // TNT Chaining + generateConfigOption("settings.tnt-chaining.enabled", true); + generateConfigOption("settings.tnt-chaining.maximum", 20); + generateConfigOption("settings.tnt-chaining.duration-milliseconds", 1000); + + // TNT Behaviors + generateConfigOption("settings.tnt-behavior.cancel-entire-explosion-if-non-whitelisted.enabled", true); + generateConfigOption("settings.tnt-behavior.use-tnt-whitelist.enabled", true); + generateConfigOption("settings.tnt-behavior.ignore-single-if-block-pop-off.enabled", true); + } + + + public void generateConfigOption(String key, Object defaultValue) { + if (this.getProperty(key) == null) { + this.setProperty(key, defaultValue); + } + final Object value = this.getProperty(key); + this.removeProperty(key); + this.setProperty(key, value); + } + + + //Getters Start + public Object getConfigOption(String key) { + return this.getProperty(key); + } + + public String getConfigString(String key) { + return String.valueOf(getConfigOption(key)); + } + + public Integer getConfigInteger(String key) { + return Integer.valueOf(getConfigString(key)); + } + + public Long getConfigLong(String key) { + return Long.valueOf(getConfigString(key)); + } + + public Double getConfigDouble(String key) { + return Double.valueOf(getConfigString(key)); + } + + public Boolean getConfigBoolean(String key) { + return Boolean.valueOf(getConfigString(key)); + } + //Getters End + + + public Long getConfigLongOption(String key) { + if (this.getConfigOption(key) == null) { + return null; + } + return Long.valueOf(String.valueOf(this.getProperty(key))); + } + + + private boolean convertToNewAddress(String newKey, String oldKey) { + if (this.getString(newKey) != null) { + return false; + } + if (this.getString(oldKey) == null) { + return false; + } + System.out.println("Converting Config: " + oldKey + " to " + newKey); + Object value = this.getProperty(oldKey); + this.setProperty(newKey, value); + this.removeProperty(oldKey); + return true; + } + + + private void reload() { + this.load(); + this.write(); + this.save(); + } +}