Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>groupId</groupId>
<artifactId>Explosion-Control</artifactId>
<version>1.0.3</version>
<version>1.0.4-SNAPSHOT</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
Expand Down
37 changes: 24 additions & 13 deletions src/main/java/com/johnymuffin/beta/tntcontrol/ExplosionControl.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,25 +20,38 @@ public class ExplosionControl extends JavaPlugin {

private ExplosionControl plugin;

private ArrayList<Material> blockWhitelist = new ArrayList<>(Arrays.asList(Material.DIRT,
private ExplosionControlSettings settings;

public static final Set<Material> 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<Material> blockCheckBlacklist = new ArrayList<>(Arrays.asList(Material.DIODE_BLOCK_OFF, Material.DIODE_BLOCK_ON,
public static final Set<Material> 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);
}
Expand All @@ -47,11 +64,5 @@ public static void logInfo(String s) {
log.info("[" + pdf.getName() + "] " + s);
}

public ArrayList<Material> getBlockWhitelist() {
return this.blockWhitelist;
}

public ArrayList<Material> getBlockCheckBlacklist() {
return this.blockCheckBlacklist;
}
}
public ExplosionControlSettings getSettings() { return settings; }
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -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<Block> 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<Material> 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
Expand All @@ -76,4 +132,4 @@ public void onEntityDamageByBlock(EntityDamageByBlockEvent event) {
if (event.getCause() == EntityDamageEvent.DamageCause.BLOCK_EXPLOSION)
event.setCancelled(true);
}
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}