added exception bounds

This commit is contained in:
ImproperIssues
2023-09-08 22:14:06 -07:00
parent b53e914757
commit 082c05a0f4
4 changed files with 172 additions and 0 deletions

View File

@@ -4,6 +4,8 @@ import io.github.itzispyder.explosionscontrol.commands.BukkitCommand;
import io.github.itzispyder.explosionscontrol.commands.commands.ConfigWorldCommand;
import io.github.itzispyder.explosionscontrol.commands.commands.UpdateWorldsCommand;
import io.github.itzispyder.explosionscontrol.data.ExplosionConfig;
import io.github.itzispyder.explosionscontrol.data.JsonSerializable;
import io.github.itzispyder.explosionscontrol.data.PluginConfig;
import io.github.itzispyder.explosionscontrol.events.ExplosionListener;
import io.github.itzispyder.explosionscontrol.events.InventoryListener;
import io.github.itzispyder.explosionscontrol.utils.Text;
@@ -18,6 +20,7 @@ public final class ExplosionsControl extends JavaPlugin {
public static final Logger logger = Bukkit.getLogger();
public static final PluginManager pm = Bukkit.getPluginManager();
public static final String starter = Text.color("&7[&6Ex&eC&7]&r ");
public static final PluginConfig config = JsonSerializable.load(PluginConfig.PATH, PluginConfig.class, new PluginConfig());
public static ExplosionsControl instance;
@Override
@@ -25,6 +28,7 @@ public final class ExplosionsControl extends JavaPlugin {
instance = this;
this.init();
ExplosionConfig.updateAllWorlds();
config.save();
}
@Override

View File

@@ -0,0 +1,96 @@
package io.github.itzispyder.explosionscontrol.data;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.github.itzispyder.explosionscontrol.utils.FileValidationUtils;
import org.bukkit.Bukkit;
import java.io.*;
public interface JsonSerializable<T> {
File getFile();
default String serialize(boolean pretty) {
Gson gson;
if (pretty) {
gson = new GsonBuilder().setPrettyPrinting().create();
}
else {
gson = new Gson();
}
try {
String json = gson.toJson(this);
if (json == null) {
throw new IllegalStateException("json parse failed for " + this.getClass().getSimpleName());
}
return json;
}
catch (Exception ex) {
return "{}";
}
}
default T deserialize(String json) {
Gson gson = new Gson();
try {
JsonSerializable<T> v = gson.fromJson(json, this.getClass());
if (v == null) {
throw new IllegalStateException("json parse failed");
}
return (T)v;
}
catch (Exception ex) {
return null;
}
}
default void save() {
String json = serialize(true);
File f = getFile();
if (FileValidationUtils.validate(f)) {
try {
FileWriter fw = new FileWriter(f);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(json);
bw.close();
}
catch (Exception ex) {
ex.printStackTrace();
Bukkit.getLogger().warning(ex.getMessage());
}
}
}
default <O> O getOrDef(O val, O def) {
return val != null ? val : def;
}
static <T extends JsonSerializable<?>> T load(File file, Class<T> jsonSerializable, T fallback) {
if (FileValidationUtils.validate(file)) {
try {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
Gson gson = new Gson();
T t = gson.fromJson(br, jsonSerializable);
if (t == null) {
throw new IllegalStateException("json parse failed!");
}
return t;
}
catch (Exception ex) {
ex.printStackTrace();
Bukkit.getLogger().warning(ex.getMessage());
}
}
return fallback;
}
static <T extends JsonSerializable<?>> T load(String path, Class<T> jsonSerializable, T fallback) {
return load(new File(path), jsonSerializable, fallback);
}
}

View File

@@ -0,0 +1,55 @@
package io.github.itzispyder.explosionscontrol.data;
import java.io.File;
public class PluginConfig implements JsonSerializable<PluginConfig> {
public static final String PATH = "plugins/ExplosionsControl/config.json";
public static final int MAX_Y = 319;
public static final int MIN_Y = -63;
private int minYLevel, maxYLevel;
public PluginConfig() {
this.maxYLevel = 319;
this.minYLevel = -63;
}
@Override
public File getFile() {
return new File(PATH);
}
public int getMinYLevel() {
return getOrDef(minYLevel, MIN_Y);
}
public void setMinYLevel(int minYLevel) {
this.minYLevel = minYLevel;
fixExplosionLevels();
}
public int getMaxYLevel() {
return getOrDef(maxYLevel, MAX_Y);
}
public void setMaxYLevel(int maxYLevel) {
this.maxYLevel = maxYLevel;
fixExplosionLevels();
}
public boolean isOOB(int yLevel) {
return yLevel > maxYLevel || yLevel < minYLevel;
}
public void fixExplosionLevels() {
if (minYLevel >= maxYLevel) {
minYLevel = maxYLevel - 1;
}
if (maxYLevel < minYLevel) {
maxYLevel = minYLevel + 1;
}
maxYLevel = Math.min(MAX_Y, maxYLevel);
minYLevel = Math.max(MIN_Y, minYLevel);
}
}

View File

@@ -1,5 +1,6 @@
package io.github.itzispyder.explosionscontrol.events;
import io.github.itzispyder.explosionscontrol.ExplosionsControl;
import io.github.itzispyder.explosionscontrol.data.ExplosionConfig;
import io.github.itzispyder.explosionscontrol.data.Mode;
import io.github.itzispyder.explosionscontrol.utils.SoundPlayer;
@@ -46,6 +47,10 @@ public class ExplosionListener implements Listener {
catch (Exception ignore) {}
}
private boolean isOOB(Location loc) {
return ExplosionsControl.config.isOOB(loc.getBlockY());
}
@EventHandler
private void onEntityDamage(EntityDamageEvent e) {
try {
@@ -60,6 +65,10 @@ public class ExplosionListener implements Listener {
ExplosionConfig config = ExplosionConfig.load(world);
EntityDamageEvent.DamageCause cause = e.getCause();
if (isOOB(ent.getLocation())) {
return;
}
if (cause == EntityDamageEvent.DamageCause.BLOCK_EXPLOSION) {
e.setCancelled(config.getBlockMode() == Mode.NONE);
}
@@ -82,6 +91,10 @@ public class ExplosionListener implements Listener {
World world = ent.getWorld();
ExplosionConfig config = ExplosionConfig.load(world);
if (isOOB(ent.getLocation())) {
return;
}
switch (ent.getType()) {
case CREEPER -> determineOutcome(config.getCreeperMode(), e);
case MINECART_TNT -> determineOutcome(config.getMinecartMode(), e);
@@ -97,6 +110,10 @@ public class ExplosionListener implements Listener {
World world = block.getWorld();
ExplosionConfig config = ExplosionConfig.load(world);
if (isOOB(block.getLocation())) {
return;
}
determineOutcome(config.getBlockMode(), e);
}