From c720e463f919537bc4141bdfc29a7f23c0b21b9e Mon Sep 17 00:00:00 2001 From: jojo Date: Sat, 19 Dec 2020 12:04:45 +0100 Subject: [PATCH] Improve Code Style --- ...unction.java => ScriptDoubleFunction.java} | 4 +-- ...oleanFunction.java => ScriptFunction.java} | 2 +- .../function/ScriptVoidDoubleFunction.java | 26 ------------------- .../scripts/implemented/DelayScript.java | 2 +- .../scripts/implemented/FilterScript.java | 12 +++++---- .../scripts/implemented/LaunchScript.java | 26 ++++--------------- .../scripts/implemented/LocationScript.java | 26 ++++++++++++------- .../scripts/implemented/PotionScript.java | 3 +-- .../scripts/implemented/SummonScript.java | 19 ++++---------- .../misslewars/scripts/utils/EntityUtils.java | 22 ++++++++++++++++ 10 files changed, 60 insertions(+), 82 deletions(-) rename src/de/steamwar/misslewars/scripts/function/{ScriptVoidFunction.java => ScriptDoubleFunction.java} (88%) rename src/de/steamwar/misslewars/scripts/function/{ScriptBooleanFunction.java => ScriptFunction.java} (95%) delete mode 100644 src/de/steamwar/misslewars/scripts/function/ScriptVoidDoubleFunction.java diff --git a/src/de/steamwar/misslewars/scripts/function/ScriptVoidFunction.java b/src/de/steamwar/misslewars/scripts/function/ScriptDoubleFunction.java similarity index 88% rename from src/de/steamwar/misslewars/scripts/function/ScriptVoidFunction.java rename to src/de/steamwar/misslewars/scripts/function/ScriptDoubleFunction.java index 2404a42..abafda1 100644 --- a/src/de/steamwar/misslewars/scripts/function/ScriptVoidFunction.java +++ b/src/de/steamwar/misslewars/scripts/function/ScriptDoubleFunction.java @@ -21,6 +21,6 @@ package de.steamwar.misslewars.scripts.function; import de.steamwar.misslewars.scripts.RunnableScriptEvent; -public interface ScriptVoidFunction { - void execute(RunnableScriptEvent runnableScriptEvent); +public interface ScriptDoubleFunction { + boolean execute(RunnableScriptEvent runnableScriptEvent, double... doubles); } diff --git a/src/de/steamwar/misslewars/scripts/function/ScriptBooleanFunction.java b/src/de/steamwar/misslewars/scripts/function/ScriptFunction.java similarity index 95% rename from src/de/steamwar/misslewars/scripts/function/ScriptBooleanFunction.java rename to src/de/steamwar/misslewars/scripts/function/ScriptFunction.java index 92d7c30..9874e01 100644 --- a/src/de/steamwar/misslewars/scripts/function/ScriptBooleanFunction.java +++ b/src/de/steamwar/misslewars/scripts/function/ScriptFunction.java @@ -21,6 +21,6 @@ package de.steamwar.misslewars.scripts.function; import de.steamwar.misslewars.scripts.RunnableScriptEvent; -public interface ScriptBooleanFunction { +public interface ScriptFunction { boolean execute(RunnableScriptEvent runnableScriptEvent); } diff --git a/src/de/steamwar/misslewars/scripts/function/ScriptVoidDoubleFunction.java b/src/de/steamwar/misslewars/scripts/function/ScriptVoidDoubleFunction.java deleted file mode 100644 index 25a7f90..0000000 --- a/src/de/steamwar/misslewars/scripts/function/ScriptVoidDoubleFunction.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - 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.function; - -import de.steamwar.misslewars.scripts.RunnableScriptEvent; - -public interface ScriptVoidDoubleFunction { - void execute(RunnableScriptEvent runnableScriptEvent, double... doubles); -} diff --git a/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java b/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java index 98fd631..744f616 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java @@ -30,7 +30,7 @@ import java.util.Map; public class DelayScript extends RunnableScript { - private static Map delayMap = new HashMap<>(); + private static final Map delayMap = new HashMap<>(); static { delayMap.put("config.mineflytime", Config.ShieldFlyTime); diff --git a/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java b/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java index fc0a521..e89899d 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java @@ -23,17 +23,19 @@ import com.google.gson.JsonObject; import de.steamwar.misslewars.MissileWars; import de.steamwar.misslewars.scripts.RunnableScript; import de.steamwar.misslewars.scripts.RunnableScriptEvent; -import de.steamwar.misslewars.scripts.function.ScriptBooleanFunction; +import de.steamwar.misslewars.scripts.function.ScriptFunction; +import de.steamwar.misslewars.scripts.utils.JsonUtils; import org.bukkit.Location; import java.util.HashMap; import java.util.Map; +import static de.steamwar.misslewars.scripts.utils.JsonUtils.getBoolean; import static de.steamwar.misslewars.scripts.utils.JsonUtils.getString; public class FilterScript extends RunnableScript { - private static Map filterMap = new HashMap<>(); + private static final Map filterMap = new HashMap<>(); static { filterMap.put("nearportal", runnableScriptEvent -> { @@ -52,12 +54,12 @@ public class FilterScript extends RunnableScript { }); } - private boolean inverted = false; - private ScriptBooleanFunction filter; + private boolean inverted; + private ScriptFunction filter; public FilterScript(JsonObject filter) { this.filter = filterMap.getOrDefault(getString(filter, "filter", "").toLowerCase(), null); - if (filter.has("invert")) inverted = filter.getAsJsonPrimitive("invert").getAsBoolean(); + inverted = getBoolean(filter, "invert", false); } @Override diff --git a/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java b/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java index 3d8bd71..550ef70 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java @@ -23,42 +23,26 @@ import com.google.gson.JsonObject; 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.function.ScriptFunction; +import de.steamwar.misslewars.scripts.utils.EntityUtils; import de.steamwar.misslewars.scripts.utils.ScriptShortcut; -import org.bukkit.entity.Arrow; -import org.bukkit.entity.Fireball; import org.bukkit.entity.Projectile; -import static de.steamwar.misslewars.scripts.utils.EntityUtils.setFireballOptions; -import static de.steamwar.misslewars.scripts.utils.EntityUtils.setProjectileOptions; - public class LaunchScript extends RunnableScript { - private ScriptVoidFunction launch = null; + private ScriptFunction launch = null; public LaunchScript(JsonObject launch) { - ScriptShortcut scriptShortcut = getEntity(launch.getAsJsonPrimitive("entity").getAsString()); + ScriptShortcut scriptShortcut = EntityUtils.getEntity(launch.getAsJsonPrimitive("entity").getAsString(), EntityUtils.EntityType.Projectile); if (scriptShortcut == null) return; this.launch = runnableScriptEvent -> { Projectile projectile = runnableScriptEvent.getPlayer().launchProjectile(scriptShortcut.entityClass); scriptShortcut.consumer.accept(launch, projectile, runnableScriptEvent); + return false; }; } - private ScriptShortcut getEntity(String name) { - switch (name.toLowerCase()) { - case "fireball": - return new ScriptShortcut<>(Fireball.class, (jsonObject, entity, runnableScriptEvent) -> { - setFireballOptions(entity, jsonObject); - entity.setDirection(runnableScriptEvent.getLocation().getDirection()); - }); - case "arrow": - return new ScriptShortcut<>(Arrow.class, (jsonObject, entity, runnableScriptEvent) -> setProjectileOptions(entity, jsonObject)); - } - return null; - } - @Override public boolean execute(RunnableScriptEvent runnableScriptEvent) { if (launch == null) return false; diff --git a/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java b/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java index b2abed8..5fa280a 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java @@ -23,19 +23,20 @@ import com.google.gson.JsonObject; import de.steamwar.misslewars.scripts.LocationType; import de.steamwar.misslewars.scripts.RunnableScript; import de.steamwar.misslewars.scripts.RunnableScriptEvent; -import de.steamwar.misslewars.scripts.function.ScriptVoidDoubleFunction; +import de.steamwar.misslewars.scripts.function.ScriptDoubleFunction; import de.steamwar.misslewars.scripts.utils.JsonUtils; import org.bukkit.Location; import java.util.HashMap; import java.util.Map; +import static de.steamwar.misslewars.scripts.utils.JsonUtils.getDouble; import static de.steamwar.misslewars.scripts.utils.JsonUtils.getString; public class LocationScript extends RunnableScript { - private static Map locationTypeMap = new HashMap<>(); - private static Map locationMap = new HashMap<>(); + private static final Map locationTypeMap = new HashMap<>(); + private static final Map locationMap = new HashMap<>(); static { locationTypeMap.put("static", LocationType.STATIC); @@ -44,31 +45,36 @@ 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 false; Location location1 = runnableScriptEvent.entity.getLocation(); runnableScriptEvent.setCustomLocation(location1.getX() + doubles[0], location1.getY() + doubles[1], location1.getZ() + doubles[2]); + return false; }); locationMap.put("offsetlocation", (runnableScriptEvent, doubles) -> { Location location1 = runnableScriptEvent.getLocation(); runnableScriptEvent.setCustomLocation(location1.getX() + doubles[0], location1.getY() + doubles[1], location1.getZ() + doubles[2]); + return false; }); - ScriptVoidDoubleFunction absoluteLocation = (runnableScriptEvent, doubles) -> runnableScriptEvent.setCustomLocation(doubles[0], doubles[1], doubles[2]); - locationMap.put("absolut", absoluteLocation); + ScriptDoubleFunction absoluteLocation = (runnableScriptEvent, doubles) -> { + runnableScriptEvent.setCustomLocation(doubles[0], doubles[1], doubles[2]); + return false; + }; + locationMap.put("absolute", absoluteLocation); locationMap.put("fix", absoluteLocation); locationMap.put("fixed", absoluteLocation); } private LocationType locationType = null; - private ScriptVoidDoubleFunction locationExecutor = null; + private ScriptDoubleFunction locationExecutor = null; private double x, y, z = 0; public LocationScript(JsonObject location) { if (location.has("location")) { JsonObject jsonObject = location.getAsJsonObject("location"); - JsonUtils.getDouble(jsonObject, "x", value -> x = value); - JsonUtils.getDouble(jsonObject, "y", value -> y = value); - JsonUtils.getDouble(jsonObject, "z", value -> z = value); + getDouble(jsonObject, "x", value -> x = value); + getDouble(jsonObject, "y", value -> y = value); + getDouble(jsonObject, "z", value -> z = value); locationExecutor = locationMap.getOrDefault(getString(jsonObject, "type", "").toLowerCase(), null); locationType = LocationType.CUSTOM; } else if (location.has("locationType")) { diff --git a/src/de/steamwar/misslewars/scripts/implemented/PotionScript.java b/src/de/steamwar/misslewars/scripts/implemented/PotionScript.java index 0156391..b8de2b3 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/PotionScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/PotionScript.java @@ -41,8 +41,7 @@ public class PotionScript extends RunnableScript { boolean icon = getBoolean(potion, "icon", true); PotionEffectType potionEffectType = PotionEffectType.getByName(potion.getAsJsonPrimitive("potion").getAsString()); - if (potionEffectType == null) - return; + if (potionEffectType == null) return; potionEffect = new PotionEffect(potionEffectType, duration, amplifier, ambient, particles, icon); } diff --git a/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java b/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java index 44bccf5..bc9cba4 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java @@ -22,35 +22,26 @@ package de.steamwar.misslewars.scripts.implemented; 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.function.ScriptFunction; +import de.steamwar.misslewars.scripts.utils.EntityUtils; import de.steamwar.misslewars.scripts.utils.ScriptShortcut; import org.bukkit.entity.Entity; -import org.bukkit.entity.TNTPrimed; - -import static de.steamwar.misslewars.scripts.utils.EntityUtils.setTNTPrimedOptions; public class SummonScript extends RunnableScript { - private ScriptVoidFunction summon = null; + private ScriptFunction summon = null; public SummonScript(JsonObject summon) { - ScriptShortcut scriptShortcut = getEntity(summon.getAsJsonPrimitive("entity").getAsString()); + ScriptShortcut scriptShortcut = EntityUtils.getEntity(summon.getAsJsonPrimitive("entity").getAsString(), EntityUtils.EntityType.Normal); if (scriptShortcut == null) return; this.summon = runnableScriptEvent -> { Entity entity = runnableScriptEvent.entity.getWorld().spawn(runnableScriptEvent.getLocation(), scriptShortcut.entityClass); scriptShortcut.consumer.accept(summon, entity, runnableScriptEvent); + return false; }; } - private ScriptShortcut getEntity(String name) { - switch (name.toLowerCase()) { - case "tntprimed": - return new ScriptShortcut<>(TNTPrimed.class, (jsonObject, entity, runnableScriptEvent) -> setTNTPrimedOptions(entity, jsonObject)); - } - return null; - } - @Override public boolean execute(RunnableScriptEvent runnableScriptEvent) { if (summon == null) return false; diff --git a/src/de/steamwar/misslewars/scripts/utils/EntityUtils.java b/src/de/steamwar/misslewars/scripts/utils/EntityUtils.java index eb044f1..af70bda 100644 --- a/src/de/steamwar/misslewars/scripts/utils/EntityUtils.java +++ b/src/de/steamwar/misslewars/scripts/utils/EntityUtils.java @@ -57,4 +57,26 @@ public class EntityUtils { setExplosiveOptions(tntPrimed, jsonObject); } + public enum EntityType { + Projectile, + Normal + } + + public static ScriptShortcut getEntity(String name, EntityType entityType) { + switch (name.toLowerCase()) { + case "tntprimed": + if (entityType != EntityType.Normal) return null; + return new ScriptShortcut<>(TNTPrimed.class, (jsonObject, entity, runnableScriptEvent) -> setTNTPrimedOptions(entity, jsonObject)); + + case "fireball": + return new ScriptShortcut<>(Fireball.class, (jsonObject, entity, runnableScriptEvent) -> { + setFireballOptions(entity, jsonObject); + entity.setDirection(runnableScriptEvent.getLocation().getDirection()); + }); + case "arrow": + return new ScriptShortcut<>(Arrow.class, (jsonObject, entity, runnableScriptEvent) -> setProjectileOptions(entity, jsonObject)); + } + return null; + } + }