diff --git a/src/de/steamwar/misslewars/items/CustomItem.java b/src/de/steamwar/misslewars/items/CustomItem.java index cb2aba8..cfcebda 100644 --- a/src/de/steamwar/misslewars/items/CustomItem.java +++ b/src/de/steamwar/misslewars/items/CustomItem.java @@ -64,20 +64,15 @@ public class CustomItem extends SpecialItem { public static void init() { File itemsFolder = new File(MissileWars.getPlugin().getDataFolder(), "items"); - if (!itemsFolder.exists() || !itemsFolder.canRead() || !itemsFolder.isDirectory()) { - throw new SecurityException("Items could not be loaded"); - } + if (!itemsFolder.exists() || !itemsFolder.canRead() || !itemsFolder.isDirectory()) throw new SecurityException("Items could not be loaded"); for (File itemFile : Objects.requireNonNull(itemsFolder.listFiles())) { if (!itemFile.canRead() || !itemFile.isFile()) continue; try { JsonObject jsonObject = new JsonParser().parse(new FileReader(itemFile)).getAsJsonObject(); new CustomItem(new ScriptedItem(jsonObject)); - } catch (JsonSyntaxException e) { + } catch (JsonSyntaxException | IOException e) { e.printStackTrace(); - throw new SecurityException("Item JSON error"); - } catch (IOException e) { - e.printStackTrace(); - throw new SecurityException("Corrupt Item"); + throw new SecurityException("Item JSON error", e); } } } diff --git a/src/de/steamwar/misslewars/items/Missile.java b/src/de/steamwar/misslewars/items/Missile.java index 09ecb5a..b97d18b 100644 --- a/src/de/steamwar/misslewars/items/Missile.java +++ b/src/de/steamwar/misslewars/items/Missile.java @@ -96,9 +96,7 @@ public class Missile extends SpecialItem { if (index > size) index = size; StringBuilder st = new StringBuilder(); st.append("§8[§e"); - if (index > 0) { - st.append(repeat(index)); - } + if (index > 0) st.append(repeat(index)); st.append("§7"); st.append(repeat(size - index)); st.append("§8]"); @@ -108,9 +106,7 @@ public class Missile extends SpecialItem { private String repeat(int count) { if (count == 0) return ""; StringBuilder st = new StringBuilder(); - for (int i = 0; i < count; i++) { - st.append("="); - } + for (int i = 0; i < count; i++) st.append("="); return st.toString(); } @@ -128,13 +124,9 @@ public class Missile extends SpecialItem { AffineTransform aT = new AffineTransform(); double yaw = (p.getLocation().getYaw() + 360f) % 360; - if (yaw > 45 && yaw <= 135) { - aT = aT.rotateY(270); - } else if (yaw > 135 && yaw <= 225) { - aT = aT.rotateY(180); - } else if (yaw > 225 && yaw <= 315) { - aT = aT.rotateY(90); - } + if (yaw > 45 && yaw <= 135) aT = aT.rotateY(270); + else if (yaw > 135 && yaw <= 225) aT = aT.rotateY(180); + else if (yaw > 225 && yaw <= 315) aT = aT.rotateY(90); v = v.subtract(dimensions.getX()/2, dimensions.getY() + 2, -2).subtract(offset); v = aT.apply(v.toVector3()).toBlockPoint(); @@ -155,9 +147,7 @@ public class Missile extends SpecialItem { public static void init() { File missileFolder = new File(MissileWars.getPlugin().getDataFolder(), "missiles"); - if (!missileFolder.exists() || !missileFolder.canRead() || !missileFolder.isDirectory()) { - throw new SecurityException("Missiles could not be loaded"); - } + if (!missileFolder.exists() || !missileFolder.canRead() || !missileFolder.isDirectory()) throw new SecurityException("Missiles could not be loaded"); for (File missileFile : Objects.requireNonNull(missileFolder.listFiles())) { if (!missileFile.canRead() || !missileFile.isFile()) continue; new Missile(missileFile); diff --git a/src/de/steamwar/misslewars/items/SpecialItem.java b/src/de/steamwar/misslewars/items/SpecialItem.java index eb34945..f776115 100644 --- a/src/de/steamwar/misslewars/items/SpecialItem.java +++ b/src/de/steamwar/misslewars/items/SpecialItem.java @@ -42,11 +42,8 @@ public abstract class SpecialItem { private static List missileItems = new ArrayList<>(); SpecialItem() { - if (this.isMissile()) { - missileItems.add(this); - } else { - supportItems.add(this); - } + if (this.isMissile()) missileItems.add(this); + else supportItems.add(this); } private String materialName = null; @@ -78,21 +75,18 @@ public abstract class SpecialItem { } private static boolean handleUse(ItemStack item, Player player, List items) { - for (SpecialItem specialItem : items) { + for (SpecialItem specialItem : items) if (item.isSimilar(specialItem.getItem())) return specialItem.handleUse(player); - } return false; } public static void handleThrow(ProjectileLaunchEvent e) { String name = e.getEntity().getClass().getName().toLowerCase(); for (SpecialItem specialItem : supportItems) { - if (specialItem.materialName == null) { + if (specialItem.materialName == null) specialItem.materialName = specialItem.getItem().getType().name().toLowerCase(); - } - if (name.contains(specialItem.materialName)) { + if (name.contains(specialItem.materialName)) specialItem.handleThrow(e.getEntity()); - } } } @@ -100,11 +94,8 @@ public abstract class SpecialItem { String name = e.getEntity().getClass().getName().toLowerCase(); Location location = null; - if (e.getHitEntity() != null) { - location = e.getHitEntity().getLocation(); - } else if (e.getHitBlock() != null) { - location = e.getHitBlock().getLocation(); - } + if (e.getHitEntity() != null) location = e.getHitEntity().getLocation(); + else if (e.getHitBlock() != null) location = e.getHitBlock().getLocation(); if (location == null) return; for (SpecialItem specialItem : supportItems) { @@ -115,11 +106,8 @@ public abstract class SpecialItem { } public static ItemStack getRandomItem() { - if (random.nextDouble() > Config.MissileChance) { - return supportItems.get(random.nextInt(supportItems.size())).getItem(); - } else { - return missileItems.get(random.nextInt(missileItems.size())).getItem(); - } + if (random.nextDouble() > Config.MissileChance) return supportItems.get(random.nextInt(supportItems.size())).getItem(); + else return missileItems.get(random.nextInt(missileItems.size())).getItem(); } } diff --git a/src/de/steamwar/misslewars/scripts/LocationType.java b/src/de/steamwar/misslewars/scripts/LocationType.java index 1ed8325..5566c56 100644 --- a/src/de/steamwar/misslewars/scripts/LocationType.java +++ b/src/de/steamwar/misslewars/scripts/LocationType.java @@ -20,10 +20,8 @@ package de.steamwar.misslewars.scripts; public enum LocationType { - STATIC, DYNAMIC, DEFAULT, CUSTOM - } diff --git a/src/de/steamwar/misslewars/scripts/RunnableScript.java b/src/de/steamwar/misslewars/scripts/RunnableScript.java index 897e71d..df046b7 100644 --- a/src/de/steamwar/misslewars/scripts/RunnableScript.java +++ b/src/de/steamwar/misslewars/scripts/RunnableScript.java @@ -20,7 +20,5 @@ package de.steamwar.misslewars.scripts; public abstract class RunnableScript { - public abstract boolean execute(RunnableScriptEvent runnableScriptEvent); - } diff --git a/src/de/steamwar/misslewars/scripts/RunnableScriptEvent.java b/src/de/steamwar/misslewars/scripts/RunnableScriptEvent.java index 5404675..d303d77 100644 --- a/src/de/steamwar/misslewars/scripts/RunnableScriptEvent.java +++ b/src/de/steamwar/misslewars/scripts/RunnableScriptEvent.java @@ -39,14 +39,10 @@ public class RunnableScriptEvent { public Location getLocation() { // Custom location - if (locationType == LocationType.CUSTOM && customLocation != null) { - return customLocation; - } + if (locationType == LocationType.CUSTOM && customLocation != null) return customLocation; // Static initial Location - if (locationType == LocationType.STATIC) { - return location; - } + if (locationType == LocationType.STATIC) return location; // Dynamic Location if entity is not null if (locationType == LocationType.DYNAMIC) { @@ -56,9 +52,7 @@ public class RunnableScriptEvent { // Default Location is static if EventType is onClick otherwise dynamic if (eventType == ScriptedItem.EventType.onClick) return location; - if (entity != null) { - return entity.getLocation(); - } + if (entity != null) return entity.getLocation(); return location; } diff --git a/src/de/steamwar/misslewars/scripts/Script.java b/src/de/steamwar/misslewars/scripts/Script.java index 6b987fb..9a3c23e 100644 --- a/src/de/steamwar/misslewars/scripts/Script.java +++ b/src/de/steamwar/misslewars/scripts/Script.java @@ -76,13 +76,6 @@ public class Script { runnableScriptList.add(runnableScript); } - @Override - public String toString() { - return "Script{" + - "runnableScriptList=" + runnableScriptList + - '}'; - } - public static Script parseScript(JsonArray jsonArray) { Script script = new Script(); jsonArray.forEach(jsonElement -> { @@ -94,11 +87,9 @@ public class Script { } private static RunnableScript parseScriptSnippet(JsonObject jsonObject) { - if (!jsonObject.has("type")) { + if (!jsonObject.has("type")) return null; - } - String type = jsonObject.getAsJsonPrimitive("type").getAsString(); - switch (type.toLowerCase()) { + switch (jsonObject.getAsJsonPrimitive("type").getAsString().toLowerCase()) { case "delay": return new DelayScript(jsonObject); case "filter": diff --git a/src/de/steamwar/misslewars/scripts/ScriptedItem.java b/src/de/steamwar/misslewars/scripts/ScriptedItem.java index c992aad..de7bf3d 100644 --- a/src/de/steamwar/misslewars/scripts/ScriptedItem.java +++ b/src/de/steamwar/misslewars/scripts/ScriptedItem.java @@ -62,33 +62,25 @@ public class ScriptedItem { public ScriptedItem(JsonObject jsonObject) { itemStack = createItemStack(jsonObject); - if (jsonObject.has("entityName")) { + if (jsonObject.has("entityName")) entityName = jsonObject.get("entityName").getAsString(); - } for (EventType eventType : EventType.values()) { String eventString = "EVENT." + eventType.name(); - if (!jsonObject.has(eventString)) continue; - if (!jsonObject.get(eventString).isJsonArray()) continue; + if (!jsonObject.has(eventString) || !jsonObject.get(eventString).isJsonArray()) continue; scriptMap.put(eventType, Script.parseScript(jsonObject.getAsJsonArray(eventString))); } } private ItemStack createItemStack(JsonObject jsonObject) { - String type = jsonObject.getAsJsonPrimitive("type").getAsString(); - String name = jsonObject.getAsJsonPrimitive("name").getAsString(); - int amount = jsonObject.getAsJsonPrimitive("amount").getAsInt(); - - ItemStack itemStack = new ItemStack(Material.valueOf(type), amount); + ItemStack itemStack = new ItemStack(Material.valueOf(jsonObject.getAsJsonPrimitive("type").getAsString()), jsonObject.getAsJsonPrimitive("amount").getAsInt()); ItemMeta itemMeta = itemStack.getItemMeta(); if (itemMeta == null) return itemStack; - itemMeta.setDisplayName(name); + itemMeta.setDisplayName(jsonObject.getAsJsonPrimitive("name").getAsString()); if (jsonObject.has("lore")) { List lore = new ArrayList<>(); - jsonObject.getAsJsonArray("lore").forEach(jsonElement -> { - lore.add(jsonElement.getAsString()); - }); + jsonObject.getAsJsonArray("lore").forEach(jsonElement -> lore.add(jsonElement.getAsString())); itemMeta.setLore(lore); } diff --git a/src/de/steamwar/misslewars/scripts/function/ScriptBooleanFunction.java b/src/de/steamwar/misslewars/scripts/function/ScriptBooleanFunction.java index 8f877ee..92d7c30 100644 --- a/src/de/steamwar/misslewars/scripts/function/ScriptBooleanFunction.java +++ b/src/de/steamwar/misslewars/scripts/function/ScriptBooleanFunction.java @@ -22,7 +22,5 @@ package de.steamwar.misslewars.scripts.function; import de.steamwar.misslewars.scripts.RunnableScriptEvent; public interface ScriptBooleanFunction { - boolean execute(RunnableScriptEvent runnableScriptEvent); - } diff --git a/src/de/steamwar/misslewars/scripts/function/ScriptVoidDoubleFunction.java b/src/de/steamwar/misslewars/scripts/function/ScriptVoidDoubleFunction.java index 26eaf8a..25a7f90 100644 --- a/src/de/steamwar/misslewars/scripts/function/ScriptVoidDoubleFunction.java +++ b/src/de/steamwar/misslewars/scripts/function/ScriptVoidDoubleFunction.java @@ -22,7 +22,5 @@ package de.steamwar.misslewars.scripts.function; import de.steamwar.misslewars.scripts.RunnableScriptEvent; public interface ScriptVoidDoubleFunction { - void execute(RunnableScriptEvent runnableScriptEvent, double... doubles); - } diff --git a/src/de/steamwar/misslewars/scripts/function/ScriptVoidFunction.java b/src/de/steamwar/misslewars/scripts/function/ScriptVoidFunction.java index 5c2df8f..2404a42 100644 --- a/src/de/steamwar/misslewars/scripts/function/ScriptVoidFunction.java +++ b/src/de/steamwar/misslewars/scripts/function/ScriptVoidFunction.java @@ -22,7 +22,5 @@ package de.steamwar.misslewars.scripts.function; import de.steamwar.misslewars.scripts.RunnableScriptEvent; public interface ScriptVoidFunction { - void execute(RunnableScriptEvent runnableScriptEvent); - } diff --git a/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java b/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java index ea111ba..98fd631 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java @@ -47,11 +47,8 @@ public class DelayScript extends RunnableScript { public DelayScript(JsonObject delay) { JsonPrimitive jsonPrimitive = delay.getAsJsonPrimitive("time"); - if (jsonPrimitive.isString()) { - delayTime = delayMap.getOrDefault(jsonPrimitive.getAsString().toLowerCase(), 0); - } else if (jsonPrimitive.isNumber()) { - delayTime = jsonPrimitive.getAsInt(); - } + if (jsonPrimitive.isString()) delayTime = delayMap.getOrDefault(jsonPrimitive.getAsString().toLowerCase(), 0); + else if (jsonPrimitive.isNumber()) delayTime = jsonPrimitive.getAsInt(); } @Override diff --git a/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java b/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java index 7869761..fc0a521 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java @@ -40,15 +40,11 @@ public class FilterScript extends RunnableScript { Location location = runnableScriptEvent.getLocation(); int bz = MissileWars.getBlueTeam().getPortalZ(); int rz = MissileWars.getRedTeam().getPortalZ(); - - int offset = sign(bz - rz) * 5; + int offset = (int) Math.signum(bz - rz) * 5; int blockZ = location.getBlockZ(); - if (offset > 0) { - return (blockZ > bz - offset) || (blockZ < rz + offset); - } else { - return (blockZ < bz - offset) || (blockZ > rz + offset); - } + if (offset > 0) return (blockZ > bz - offset) || (blockZ < rz + offset); + else return (blockZ < bz - offset) || (blockZ > rz + offset); }); filterMap.put("nearspawn", runnableScriptEvent -> { Location location = runnableScriptEvent.getLocation(); @@ -56,29 +52,19 @@ public class FilterScript extends RunnableScript { }); } - private static int sign(int i) { - if (i < 0) return -1; - return i > 0 ? 1 : 0; - } - private boolean inverted = false; private ScriptBooleanFunction filter; public FilterScript(JsonObject filter) { this.filter = filterMap.getOrDefault(getString(filter, "filter", "").toLowerCase(), null); - if (filter.has("invert")) { - inverted = filter.getAsJsonPrimitive("invert").getAsBoolean(); - } + if (filter.has("invert")) inverted = filter.getAsJsonPrimitive("invert").getAsBoolean(); } @Override public boolean execute(RunnableScriptEvent runnableScriptEvent) { if (filter == null) return true; - if (inverted) { - return !filter.execute(runnableScriptEvent); - } else { - return filter.execute(runnableScriptEvent); - } + if (inverted) return !filter.execute(runnableScriptEvent); + else return filter.execute(runnableScriptEvent); } } diff --git a/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java b/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java index 92e35b7..3d8bd71 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java @@ -24,9 +24,10 @@ import de.steamwar.misslewars.scripts.RunnableScript; import de.steamwar.misslewars.scripts.RunnableScriptEvent; import de.steamwar.misslewars.scripts.ScriptedItem; import de.steamwar.misslewars.scripts.function.ScriptVoidFunction; +import de.steamwar.misslewars.scripts.utils.ScriptShortcut; import org.bukkit.entity.Arrow; import org.bukkit.entity.Fireball; -import org.bukkit.entity.Player; +import org.bukkit.entity.Projectile; import static de.steamwar.misslewars.scripts.utils.EntityUtils.setFireballOptions; import static de.steamwar.misslewars.scripts.utils.EntityUtils.setProjectileOptions; @@ -36,23 +37,26 @@ public class LaunchScript extends RunnableScript { private ScriptVoidFunction launch = null; public LaunchScript(JsonObject launch) { - switch (launch.getAsJsonPrimitive("entity").getAsString().toLowerCase()) { + ScriptShortcut scriptShortcut = getEntity(launch.getAsJsonPrimitive("entity").getAsString()); + if (scriptShortcut == null) return; + + this.launch = runnableScriptEvent -> { + Projectile projectile = runnableScriptEvent.getPlayer().launchProjectile(scriptShortcut.entityClass); + scriptShortcut.consumer.accept(launch, projectile, runnableScriptEvent); + }; + } + + private ScriptShortcut getEntity(String name) { + switch (name.toLowerCase()) { case "fireball": - this.launch = runnableScriptEvent -> { - Fireball fireball = runnableScriptEvent.getPlayer().launchProjectile(Fireball.class); - setFireballOptions(fireball, launch); - fireball.setDirection(runnableScriptEvent.getLocation().getDirection()); - }; - break; + return new ScriptShortcut<>(Fireball.class, (jsonObject, entity, runnableScriptEvent) -> { + setFireballOptions(entity, jsonObject); + entity.setDirection(runnableScriptEvent.getLocation().getDirection()); + }); case "arrow": - this.launch = runnableScriptEvent -> { - Arrow arrow = runnableScriptEvent.getPlayer().launchProjectile(Arrow.class); - setProjectileOptions(arrow, launch); - }; - break; - default: - break; + return new ScriptShortcut<>(Arrow.class, (jsonObject, entity, runnableScriptEvent) -> setProjectileOptions(entity, jsonObject)); } + return null; } @Override diff --git a/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java b/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java index a7a7852..b2abed8 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java @@ -44,9 +44,7 @@ public class LocationScript extends RunnableScript { locationTypeMap.put("default", LocationType.DEFAULT); locationMap.put("offsetentity", (runnableScriptEvent, doubles) -> { - if (runnableScriptEvent.entity == null) { - return; - } + if (runnableScriptEvent.entity == null) return; Location location1 = runnableScriptEvent.entity.getLocation(); runnableScriptEvent.setCustomLocation(location1.getX() + doubles[0], location1.getY() + doubles[1], location1.getZ() + doubles[2]); }); @@ -54,9 +52,7 @@ public class LocationScript extends RunnableScript { Location location1 = runnableScriptEvent.getLocation(); runnableScriptEvent.setCustomLocation(location1.getX() + doubles[0], location1.getY() + doubles[1], location1.getZ() + doubles[2]); }); - ScriptVoidDoubleFunction absoluteLocation = (runnableScriptEvent, doubles) -> { - runnableScriptEvent.setCustomLocation(doubles[0], doubles[1], doubles[2]); - }; + ScriptVoidDoubleFunction absoluteLocation = (runnableScriptEvent, doubles) -> runnableScriptEvent.setCustomLocation(doubles[0], doubles[1], doubles[2]); locationMap.put("absolut", absoluteLocation); locationMap.put("fix", absoluteLocation); locationMap.put("fixed", absoluteLocation); @@ -65,9 +61,7 @@ public class LocationScript extends RunnableScript { private LocationType locationType = null; private ScriptVoidDoubleFunction locationExecutor = null; - private double x = 0; - private double y = 0; - private double z = 0; + private double x, y, z = 0; public LocationScript(JsonObject location) { if (location.has("location")) { diff --git a/src/de/steamwar/misslewars/scripts/implemented/PasteScript.java b/src/de/steamwar/misslewars/scripts/implemented/PasteScript.java index 3a68772..e4c81ba 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/PasteScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/PasteScript.java @@ -50,11 +50,8 @@ public class PasteScript extends RunnableScript { private final Clipboard clipboard; private final BlockVector3 centeredOffset; - private boolean centered; - private boolean ignoreAir; - private int xOffset = 0; - private int yOffset = 0; - private int zOffset = 0; + private boolean centered, ignoreAir; + private int xOffset, yOffset, zOffset = 0; public PasteScript(JsonObject paste) { String schemFileName = paste.getAsJsonPrimitive("schem").getAsString(); @@ -70,23 +67,21 @@ public class PasteScript extends RunnableScript { centered = getBoolean(paste, "centered", false); ignoreAir = getBoolean(paste, "ignoreAir", false); - if (paste.has("offset")) { - JsonArray jsonArray = paste.getAsJsonArray("offset"); - if (jsonArray.size() == 3) { - xOffset = jsonArray.get(0).getAsInt(); - yOffset = jsonArray.get(1).getAsInt(); - zOffset = jsonArray.get(2).getAsInt(); - } - } + if (paste.has("offset")) + return; + JsonArray jsonArray = paste.getAsJsonArray("offset"); + if (jsonArray.size() == 3) + return; + xOffset = jsonArray.get(0).getAsInt(); + yOffset = jsonArray.get(1).getAsInt(); + zOffset = jsonArray.get(2).getAsInt(); } @Override public boolean execute(RunnableScriptEvent runnableScriptEvent) { Location location = runnableScriptEvent.getLocation(); BlockVector3 paste = BlockVector3.at(location.getX() + xOffset, location.getY() + yOffset, location.getZ() + zOffset); - if (centered) { - paste = paste.subtract(centeredOffset); - } + if (centered) paste = paste.subtract(centeredOffset); EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(world, -1); Operations.completeBlindly(new ClipboardHolder(clipboard).createPaste(editSession).ignoreAirBlocks(ignoreAir).to(paste).build()); diff --git a/src/de/steamwar/misslewars/scripts/implemented/PotionScript.java b/src/de/steamwar/misslewars/scripts/implemented/PotionScript.java index 3f3d651..0156391 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/PotionScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/PotionScript.java @@ -41,9 +41,8 @@ public class PotionScript extends RunnableScript { boolean icon = getBoolean(potion, "icon", true); PotionEffectType potionEffectType = PotionEffectType.getByName(potion.getAsJsonPrimitive("potion").getAsString()); - if (potionEffectType == null) { + if (potionEffectType == null) return; - } potionEffect = new PotionEffect(potionEffectType, duration, amplifier, ambient, particles, icon); } diff --git a/src/de/steamwar/misslewars/scripts/implemented/RemoveScript.java b/src/de/steamwar/misslewars/scripts/implemented/RemoveScript.java index 63530b0..6f2f3f1 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/RemoveScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/RemoveScript.java @@ -26,9 +26,7 @@ import org.bukkit.entity.Player; public class RemoveScript extends RunnableScript { - public RemoveScript(JsonObject jsonObject) { - - } + public RemoveScript(JsonObject jsonObject) {} @Override public boolean execute(RunnableScriptEvent runnableScriptEvent) { diff --git a/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java b/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java index 65d3fda..44bccf5 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java @@ -23,28 +23,32 @@ import com.google.gson.JsonObject; import de.steamwar.misslewars.scripts.RunnableScript; import de.steamwar.misslewars.scripts.RunnableScriptEvent; import de.steamwar.misslewars.scripts.function.ScriptVoidFunction; +import de.steamwar.misslewars.scripts.utils.ScriptShortcut; +import org.bukkit.entity.Entity; import org.bukkit.entity.TNTPrimed; -import java.util.HashMap; -import java.util.Map; - -import static de.steamwar.misslewars.scripts.utils.EntityUtils.*; +import static de.steamwar.misslewars.scripts.utils.EntityUtils.setTNTPrimedOptions; public class SummonScript extends RunnableScript { private ScriptVoidFunction summon = null; public SummonScript(JsonObject summon) { - switch (summon.getAsJsonPrimitive("entity").getAsString().toLowerCase()) { + ScriptShortcut scriptShortcut = getEntity(summon.getAsJsonPrimitive("entity").getAsString()); + if (scriptShortcut == null) return; + + this.summon = runnableScriptEvent -> { + Entity entity = runnableScriptEvent.entity.getWorld().spawn(runnableScriptEvent.getLocation(), scriptShortcut.entityClass); + scriptShortcut.consumer.accept(summon, entity, runnableScriptEvent); + }; + } + + private ScriptShortcut getEntity(String name) { + switch (name.toLowerCase()) { case "tntprimed": - this.summon = runnableScriptEvent -> { - TNTPrimed tnt = runnableScriptEvent.entity.getWorld().spawn(runnableScriptEvent.getLocation(), TNTPrimed.class); - setTNTPrimedOptions(tnt, summon); - }; - break; - default: - break; + return new ScriptShortcut<>(TNTPrimed.class, (jsonObject, entity, runnableScriptEvent) -> setTNTPrimedOptions(entity, jsonObject)); } + return null; } @Override diff --git a/src/de/steamwar/misslewars/scripts/utils/EntityUtils.java b/src/de/steamwar/misslewars/scripts/utils/EntityUtils.java index 8e9d66a..eb044f1 100644 --- a/src/de/steamwar/misslewars/scripts/utils/EntityUtils.java +++ b/src/de/steamwar/misslewars/scripts/utils/EntityUtils.java @@ -47,7 +47,6 @@ public class EntityUtils { setEntityOptions(explosive, jsonObject); } - public static void setFireballOptions(Fireball fireball, JsonObject jsonObject) { setProjectileOptions(fireball, jsonObject); setExplosiveOptions(fireball, jsonObject); diff --git a/src/de/steamwar/misslewars/scripts/utils/JsonUtils.java b/src/de/steamwar/misslewars/scripts/utils/JsonUtils.java index 31501bb..1f9b8c1 100644 --- a/src/de/steamwar/misslewars/scripts/utils/JsonUtils.java +++ b/src/de/steamwar/misslewars/scripts/utils/JsonUtils.java @@ -22,11 +22,6 @@ public class JsonUtils { return defaultValue; } - public static double getDouble(JsonObject jsonObject, String key, double defaultValue) { - if (jsonObject.has(key)) return jsonObject.getAsJsonPrimitive(key).getAsDouble(); - return defaultValue; - } - public static float getFloat(JsonObject jsonObject, String key, float defaultValue) { if (jsonObject.has(key)) return jsonObject.getAsJsonPrimitive(key).getAsFloat(); return defaultValue; diff --git a/src/de/steamwar/misslewars/scripts/utils/ScriptShortcut.java b/src/de/steamwar/misslewars/scripts/utils/ScriptShortcut.java new file mode 100644 index 0000000..1dfcd4e --- /dev/null +++ b/src/de/steamwar/misslewars/scripts/utils/ScriptShortcut.java @@ -0,0 +1,33 @@ +/* + This file is a part of the SteamWar software. + + Copyright (C) 2020 SteamWar.de-Serverteam + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +package de.steamwar.misslewars.scripts.utils; + +import com.google.gson.JsonObject; +import de.steamwar.misslewars.scripts.RunnableScriptEvent; + +public class ScriptShortcut { + public Class entityClass; + public TriConsumer consumer; + + public ScriptShortcut(Class entityClass, TriConsumer consumer) { + this.entityClass = entityClass; + this.consumer = consumer; + } +} diff --git a/src/de/steamwar/misslewars/scripts/utils/TriConsumer.java b/src/de/steamwar/misslewars/scripts/utils/TriConsumer.java new file mode 100644 index 0000000..7cc9718 --- /dev/null +++ b/src/de/steamwar/misslewars/scripts/utils/TriConsumer.java @@ -0,0 +1,24 @@ +/* + This file is a part of the SteamWar software. + + Copyright (C) 2020 SteamWar.de-Serverteam + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +package de.steamwar.misslewars.scripts.utils; + +public interface TriConsumer { + void accept(T t, R r, K k); +}