diff --git a/src/de/steamwar/misslewars/countdowns/ItemCountdown.java b/src/de/steamwar/misslewars/countdowns/ItemCountdown.java
index 4bb8e12..e656667 100644
--- a/src/de/steamwar/misslewars/countdowns/ItemCountdown.java
+++ b/src/de/steamwar/misslewars/countdowns/ItemCountdown.java
@@ -38,7 +38,7 @@ public class ItemCountdown extends StateDependent {
super(EnumSet.of(FightState.FIGHTING));
}
- private void run(){
+ private void run() {
int itemCount = Math.max(MissileWars.getBlueTeam().size(), MissileWars.getRedTeam().size());
for (int i = 0; i < itemCount; i++) {
diff --git a/src/de/steamwar/misslewars/scripts/RunnableScript.java b/src/de/steamwar/misslewars/scripts/RunnableScript.java
index 4156a98..897e71d 100644
--- a/src/de/steamwar/misslewars/scripts/RunnableScript.java
+++ b/src/de/steamwar/misslewars/scripts/RunnableScript.java
@@ -19,8 +19,8 @@
package de.steamwar.misslewars.scripts;
-public interface RunnableScript {
+public abstract class RunnableScript {
- boolean execute(RunnableScriptEvent runnableScriptEvent);
+ public abstract boolean execute(RunnableScriptEvent runnableScriptEvent);
}
diff --git a/src/de/steamwar/misslewars/scripts/Script.java b/src/de/steamwar/misslewars/scripts/Script.java
index 6401742..883a9d5 100644
--- a/src/de/steamwar/misslewars/scripts/Script.java
+++ b/src/de/steamwar/misslewars/scripts/Script.java
@@ -19,8 +19,10 @@
package de.steamwar.misslewars.scripts;
+import com.google.gson.JsonArray;
+import com.google.gson.JsonObject;
import de.steamwar.misslewars.MissileWars;
-import de.steamwar.misslewars.scripts.implemented.DelayScript;
+import de.steamwar.misslewars.scripts.implemented.*;
import org.bukkit.Bukkit;
import java.util.ArrayList;
@@ -81,4 +83,43 @@ public class Script {
'}';
}
+ public static Script parseScript(JsonArray jsonArray) {
+ Script script = new Script();
+ jsonArray.forEach(jsonElement -> {
+ RunnableScript runnableScript = parseScriptSnippet((JsonObject) jsonElement);
+ if (runnableScript == null) return;
+ script.add(runnableScript);
+ });
+ return script;
+ }
+
+ private static RunnableScript parseScriptSnippet(JsonObject jsonObject) {
+ if (!jsonObject.has("type")) {
+ return null;
+ }
+ String type = jsonObject.getAsJsonPrimitive("type").getAsString();
+ switch (type.toLowerCase()) {
+ case "delay":
+ return new DelayScript(jsonObject);
+ case "filter":
+ return new FilterScript(jsonObject);
+ case "remove":
+ return new RemoveScript(jsonObject);
+ case "launch":
+ return new LaunchScript(jsonObject);
+ case "location":
+ return new LocationScript(jsonObject);
+ case "paste":
+ return new PasteScript(jsonObject);
+ case "potion":
+ return new PotionScript(jsonObject);
+ case "sound":
+ return new SoundScript(jsonObject);
+ case "summon":
+ return new SummonScript(jsonObject);
+ default:
+ return null;
+ }
+ }
+
}
diff --git a/src/de/steamwar/misslewars/scripts/ScriptParser.java b/src/de/steamwar/misslewars/scripts/ScriptParser.java
deleted file mode 100644
index 659f86b..0000000
--- a/src/de/steamwar/misslewars/scripts/ScriptParser.java
+++ /dev/null
@@ -1,67 +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;
-
-import com.google.gson.JsonArray;
-import com.google.gson.JsonObject;
-import de.steamwar.misslewars.scripts.implemented.*;
-
-public class ScriptParser {
-
- public static Script parseScript(JsonArray jsonArray) {
- Script script = new Script();
- jsonArray.forEach(jsonElement -> {
- RunnableScript runnableScript = parseScriptSnippet((JsonObject) jsonElement);
- if (runnableScript == null) return;
- script.add(runnableScript);
- });
- return script;
- }
-
- private static RunnableScript parseScriptSnippet(JsonObject jsonObject) {
- if (!jsonObject.has("type")) {
- return null;
- }
- String type = jsonObject.getAsJsonPrimitive("type").getAsString();
- switch (type.toLowerCase()) {
- case "delay":
- return new DelayScript(jsonObject);
- case "filter":
- return new FilterScript(jsonObject);
- case "remove":
- return new RemoveScript(jsonObject);
- case "launch":
- return new LaunchScript(jsonObject);
- case "location":
- return new LocationScript(jsonObject);
- case "paste":
- return new PasteScript(jsonObject);
- case "potion":
- return new PotionScript(jsonObject);
- case "sound":
- return new SoundScript(jsonObject);
- case "summon":
- return new SummonScript(jsonObject);
- default:
- return null;
- }
- }
-
-}
diff --git a/src/de/steamwar/misslewars/scripts/ScriptedItem.java b/src/de/steamwar/misslewars/scripts/ScriptedItem.java
index 21fb226..c992aad 100644
--- a/src/de/steamwar/misslewars/scripts/ScriptedItem.java
+++ b/src/de/steamwar/misslewars/scripts/ScriptedItem.java
@@ -70,7 +70,7 @@ public class ScriptedItem {
String eventString = "EVENT." + eventType.name();
if (!jsonObject.has(eventString)) continue;
if (!jsonObject.get(eventString).isJsonArray()) continue;
- scriptMap.put(eventType, ScriptParser.parseScript(jsonObject.getAsJsonArray(eventString)));
+ scriptMap.put(eventType, Script.parseScript(jsonObject.getAsJsonArray(eventString)));
}
}
diff --git a/src/de/steamwar/misslewars/scripts/function/ScriptBooleanFunction.java b/src/de/steamwar/misslewars/scripts/function/ScriptBooleanFunction.java
new file mode 100644
index 0000000..8f877ee
--- /dev/null
+++ b/src/de/steamwar/misslewars/scripts/function/ScriptBooleanFunction.java
@@ -0,0 +1,28 @@
+/*
+ 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 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
new file mode 100644
index 0000000..26eaf8a
--- /dev/null
+++ b/src/de/steamwar/misslewars/scripts/function/ScriptVoidDoubleFunction.java
@@ -0,0 +1,28 @@
+/*
+ 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/function/ScriptVoidFunction.java b/src/de/steamwar/misslewars/scripts/function/ScriptVoidFunction.java
new file mode 100644
index 0000000..5c2df8f
--- /dev/null
+++ b/src/de/steamwar/misslewars/scripts/function/ScriptVoidFunction.java
@@ -0,0 +1,28 @@
+/*
+ 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 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 2188864..ea111ba 100644
--- a/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java
+++ b/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java
@@ -25,37 +25,30 @@ import de.steamwar.misslewars.Config;
import de.steamwar.misslewars.scripts.RunnableScript;
import de.steamwar.misslewars.scripts.RunnableScriptEvent;
-public class DelayScript implements RunnableScript {
+import java.util.HashMap;
+import java.util.Map;
+
+public class DelayScript extends RunnableScript {
+
+ private static Map delayMap = new HashMap<>();
+
+ static {
+ delayMap.put("config.mineflytime", Config.ShieldFlyTime);
+ delayMap.put("config.shieldflytime", Config.ShieldFlyTime);
+ delayMap.put("config.endtime", Config.EndTime);
+ delayMap.put("config.waitingtime", Config.WaitingTime);
+ delayMap.put("config.itemtime", Config.ItemTime);
+ delayMap.put("config.platformtime", Config.PlatformTime);
+
+ delayMap.put("config.tick", 1);
+ }
private int delayTime = 0;
public DelayScript(JsonObject delay) {
JsonPrimitive jsonPrimitive = delay.getAsJsonPrimitive("time");
if (jsonPrimitive.isString()) {
- switch (jsonPrimitive.getAsString().toLowerCase()) {
- case "config.mineflytime":
- case "config.shieldflytime":
- delayTime = Config.ShieldFlyTime;
- break;
- case "config.endtime":
- delayTime = Config.EndTime;
- break;
- case "config.waitingtime":
- delayTime = Config.WaitingTime;
- break;
- case "config.itemtime":
- delayTime = Config.ItemTime;
- break;
- case "config.platformtime":
- delayTime = Config.PlatformTime;
- break;
-
- case "tick":
- delayTime = 1;
- break;
- default:
- break;
- }
+ delayTime = delayMap.getOrDefault(jsonPrimitive.getAsString().toLowerCase(), 0);
} else if (jsonPrimitive.isNumber()) {
delayTime = jsonPrimitive.getAsInt();
}
diff --git a/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java b/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java
index e9fd427..7869761 100644
--- a/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java
+++ b/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java
@@ -23,40 +23,49 @@ 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 org.bukkit.Location;
-public class FilterScript implements RunnableScript {
+import java.util.HashMap;
+import java.util.Map;
- private interface Filter {
+import static de.steamwar.misslewars.scripts.utils.JsonUtils.getString;
- boolean filter(RunnableScriptEvent runnableScriptEvent);
+public class FilterScript extends RunnableScript {
+ private static Map filterMap = new HashMap<>();
+
+ static {
+ filterMap.put("nearportal", runnableScriptEvent -> {
+ Location location = runnableScriptEvent.getLocation();
+ int bz = MissileWars.getBlueTeam().getPortalZ();
+ int rz = MissileWars.getRedTeam().getPortalZ();
+
+ int offset = sign(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);
+ }
+ });
+ filterMap.put("nearspawn", runnableScriptEvent -> {
+ Location location = runnableScriptEvent.getLocation();
+ return MissileWars.getBlueTeam().getSpawn().distance(location) < 3 || MissileWars.getRedTeam().getSpawn().distance(location) < 3;
+ });
+ }
+
+ private static int sign(int i) {
+ if (i < 0) return -1;
+ return i > 0 ? 1 : 0;
}
private boolean inverted = false;
- private Filter filter = null;
+ private ScriptBooleanFunction filter;
public FilterScript(JsonObject filter) {
- switch (filter.getAsJsonPrimitive("filter").getAsString().toLowerCase()) {
- case "nearportal":
- this.filter = runnableScriptEvent -> {
- Location location = runnableScriptEvent.getLocation();
- int bz = MissileWars.getBlueTeam().getPortalZ();
- int rz = MissileWars.getRedTeam().getPortalZ();
-
- int offset = sign(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);
- }
- };
- break;
- default:
- break;
- }
+ this.filter = filterMap.getOrDefault(getString(filter, "filter", "").toLowerCase(), null);
if (filter.has("invert")) {
inverted = filter.getAsJsonPrimitive("invert").getAsBoolean();
}
@@ -66,15 +75,10 @@ public class FilterScript implements RunnableScript {
public boolean execute(RunnableScriptEvent runnableScriptEvent) {
if (filter == null) return true;
if (inverted) {
- return !filter.filter(runnableScriptEvent);
+ return !filter.execute(runnableScriptEvent);
} else {
- return filter.filter(runnableScriptEvent);
+ return filter.execute(runnableScriptEvent);
}
}
- private int sign(int i) {
- if (i < 0) return -1;
- return i > 0 ? 1 : 0;
- }
-
}
diff --git a/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java b/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java
index 51958dd..ce036b1 100644
--- a/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java
+++ b/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java
@@ -23,19 +23,17 @@ import com.google.gson.JsonObject;
import de.steamwar.misslewars.scripts.RunnableScript;
import de.steamwar.misslewars.scripts.RunnableScriptEvent;
import de.steamwar.misslewars.scripts.ScriptedItem;
-import org.bukkit.entity.*;
+import de.steamwar.misslewars.scripts.function.ScriptVoidFunction;
+import org.bukkit.entity.Arrow;
+import org.bukkit.entity.Fireball;
+import org.bukkit.entity.Player;
-import static de.steamwar.misslewars.scripts.utils.EntityUtils.*;
+import static de.steamwar.misslewars.scripts.utils.EntityUtils.setFireballOptions;
+import static de.steamwar.misslewars.scripts.utils.EntityUtils.setProjectileOptions;
-public class LaunchScript implements RunnableScript {
+public class LaunchScript extends RunnableScript {
- private interface Launch {
-
- void launch(RunnableScriptEvent runnableScriptEvent);
-
- }
-
- private Launch launch = null;
+ private ScriptVoidFunction launch = null;
public LaunchScript(JsonObject launch) {
switch (launch.getAsJsonPrimitive("entity").getAsString().toLowerCase()) {
@@ -43,14 +41,7 @@ public class LaunchScript implements RunnableScript {
this.launch = runnableScriptEvent -> {
Player player = (Player) runnableScriptEvent.entity;
Fireball fireball = player.launchProjectile(Fireball.class);
-
- setVelocity(fireball, launch);
- setYield(fireball, launch);
- setIncendiary(fireball, launch);
- setBounce(fireball, launch);
- setGlowing(fireball, launch);
- setGravity(fireball, launch);
-
+ setFireballOptions(fireball, launch);
fireball.setDirection(runnableScriptEvent.getLocation().getDirection());
};
break;
@@ -58,10 +49,7 @@ public class LaunchScript implements RunnableScript {
this.launch = runnableScriptEvent -> {
Player player = (Player) runnableScriptEvent.entity;
Arrow arrow = player.launchProjectile(Arrow.class);
-
- setVelocity(arrow, launch);
- setGlowing(arrow, launch);
- setGravity(arrow, launch);
+ setProjectileOptions(arrow, launch);
};
break;
default:
@@ -73,7 +61,7 @@ public class LaunchScript implements RunnableScript {
public boolean execute(RunnableScriptEvent runnableScriptEvent) {
if (launch == null) return false;
if (runnableScriptEvent.eventType != ScriptedItem.EventType.onClick) return true;
- launch.launch(runnableScriptEvent);
+ launch.execute(runnableScriptEvent);
return true;
}
diff --git a/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java b/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java
index d77e13d..a7a7852 100644
--- a/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java
+++ b/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java
@@ -23,64 +23,62 @@ 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.utils.JsonUtils;
import org.bukkit.Location;
-public class LocationScript implements RunnableScript {
+import java.util.HashMap;
+import java.util.Map;
+
+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<>();
+
+ static {
+ locationTypeMap.put("static", LocationType.STATIC);
+ locationTypeMap.put("dynamic", LocationType.DYNAMIC);
+ locationTypeMap.put("custom", LocationType.CUSTOM);
+ locationTypeMap.put("default", LocationType.DEFAULT);
+
+ locationMap.put("offsetentity", (runnableScriptEvent, doubles) -> {
+ if (runnableScriptEvent.entity == null) {
+ return;
+ }
+ Location location1 = runnableScriptEvent.entity.getLocation();
+ runnableScriptEvent.setCustomLocation(location1.getX() + doubles[0], location1.getY() + doubles[1], location1.getZ() + doubles[2]);
+ });
+ locationMap.put("offsetlocation", (runnableScriptEvent, doubles) -> {
+ 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]);
+ };
+ locationMap.put("absolut", absoluteLocation);
+ locationMap.put("fix", absoluteLocation);
+ locationMap.put("fixed", absoluteLocation);
+ }
private LocationType locationType = null;
- private LocationExecutor locationExecutor = null;
+ private ScriptVoidDoubleFunction locationExecutor = null;
- private interface LocationExecutor {
-
- void location(RunnableScriptEvent runnableScriptEvent);
-
- }
+ private double x = 0;
+ private double y = 0;
+ private double z = 0;
public LocationScript(JsonObject location) {
if (location.has("location")) {
JsonObject jsonObject = location.getAsJsonObject("location");
- double x = jsonObject.getAsJsonPrimitive("x").getAsDouble();
- double y = jsonObject.getAsJsonPrimitive("y").getAsDouble();
- double z = jsonObject.getAsJsonPrimitive("z").getAsDouble();
- switch (jsonObject.getAsJsonPrimitive("type").getAsString().toLowerCase()) {
- case "absolute":
- case "fix":
- case "fixed":
- locationExecutor = runnableScriptEvent -> runnableScriptEvent.setCustomLocation(x, y, z);
- break;
- case "offsetEntity":
- locationExecutor = runnableScriptEvent -> {
- if (runnableScriptEvent.entity == null) {
- return;
- }
- Location location1 = runnableScriptEvent.entity.getLocation();
- runnableScriptEvent.setCustomLocation(location1.getX() + x, location1.getY() + y, location1.getZ() + z);
- };
- break;
- case "offsetLocation":
- locationExecutor = runnableScriptEvent -> {
- Location location1 = runnableScriptEvent.getLocation();
- runnableScriptEvent.setCustomLocation(location1.getX() + x, location1.getY() + y, location1.getZ() + z);
- };
- break;
- }
+ JsonUtils.getDouble(jsonObject, "x", value -> x = value);
+ JsonUtils.getDouble(jsonObject, "y", value -> y = value);
+ JsonUtils.getDouble(jsonObject, "z", value -> z = value);
+ locationExecutor = locationMap.getOrDefault(getString(jsonObject, "type", "").toLowerCase(), null);
locationType = LocationType.CUSTOM;
} else if (location.has("locationType")) {
- switch (location.getAsJsonPrimitive("locationType").getAsString().toLowerCase()) {
- case "static":
- locationType = LocationType.STATIC;
- break;
- case "dynamic":
- locationType = LocationType.DYNAMIC;
- break;
- case "custom":
- locationType = LocationType.CUSTOM;
- break;
- case "default":
- default:
- locationType = LocationType.DEFAULT;
- break;
- }
+ locationType = locationTypeMap.getOrDefault(getString(location, "locationType", "").toLowerCase(), LocationType.DEFAULT);
}
}
@@ -88,7 +86,7 @@ public class LocationScript implements RunnableScript {
public boolean execute(RunnableScriptEvent runnableScriptEvent) {
runnableScriptEvent.setLocationType(locationType);
if (locationExecutor != null) {
- locationExecutor.location(runnableScriptEvent);
+ locationExecutor.execute(runnableScriptEvent, x, y, z);
}
return true;
}
diff --git a/src/de/steamwar/misslewars/scripts/implemented/PasteScript.java b/src/de/steamwar/misslewars/scripts/implemented/PasteScript.java
index 7ddc2f7..3a68772 100644
--- a/src/de/steamwar/misslewars/scripts/implemented/PasteScript.java
+++ b/src/de/steamwar/misslewars/scripts/implemented/PasteScript.java
@@ -41,24 +41,24 @@ import java.io.FileInputStream;
import java.io.IOException;
import java.util.Objects;
-public class PasteScript implements RunnableScript {
+import static de.steamwar.misslewars.scripts.utils.JsonUtils.getBoolean;
+
+public class PasteScript extends RunnableScript {
private static final World world = new BukkitWorld(Bukkit.getWorlds().get(0));
private final Clipboard clipboard;
private final BlockVector3 centeredOffset;
- private boolean centered = false;
- private boolean ignoreAir = false;
+ private boolean centered;
+ private boolean ignoreAir;
private int xOffset = 0;
private int yOffset = 0;
private int zOffset = 0;
public PasteScript(JsonObject paste) {
String schemFileName = paste.getAsJsonPrimitive("schem").getAsString();
- if (!schemFileName.endsWith(".schem")) {
- schemFileName += ".schem";
- }
+ if (!schemFileName.endsWith(".schem")) schemFileName += ".schem";
File schemFile = new File(MissileWars.getPlugin().getDataFolder(), schemFileName);
try {
@@ -68,12 +68,8 @@ public class PasteScript implements RunnableScript {
}
centeredOffset = clipboard.getRegion().getMinimumPoint().subtract(clipboard.getOrigin()).add(clipboard.getDimensions().divide(2));
- if (paste.has("centered")) {
- centered = paste.getAsJsonPrimitive("centered").getAsBoolean();
- }
- if (paste.has("ignoreAir")) {
- ignoreAir = paste.getAsJsonPrimitive("ignoreAir").getAsBoolean();
- }
+ centered = getBoolean(paste, "centered", false);
+ ignoreAir = getBoolean(paste, "ignoreAir", false);
if (paste.has("offset")) {
JsonArray jsonArray = paste.getAsJsonArray("offset");
if (jsonArray.size() == 3) {
diff --git a/src/de/steamwar/misslewars/scripts/implemented/PotionScript.java b/src/de/steamwar/misslewars/scripts/implemented/PotionScript.java
index a3da206..14bdbdd 100644
--- a/src/de/steamwar/misslewars/scripts/implemented/PotionScript.java
+++ b/src/de/steamwar/misslewars/scripts/implemented/PotionScript.java
@@ -27,32 +27,19 @@ import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
-public class PotionScript implements RunnableScript {
+import static de.steamwar.misslewars.scripts.utils.JsonUtils.getBoolean;
+import static de.steamwar.misslewars.scripts.utils.JsonUtils.getInt;
+
+public class PotionScript extends RunnableScript {
private PotionEffect potionEffect = null;
public PotionScript(JsonObject potion) {
- int duration = 1;
- int amplifier = 1;
- boolean ambient = true;
- boolean particles = true;
- boolean icon = true;
-
- if (potion.has("duration")) {
- duration = potion.getAsJsonPrimitive("duration").getAsInt();
- }
- if (potion.has("amplifier")) {
- amplifier = potion.getAsJsonPrimitive("amplifier").getAsInt();
- }
- if (potion.has("ambient")) {
- ambient = potion.getAsJsonPrimitive("ambient").getAsBoolean();
- }
- if (potion.has("particles")) {
- particles = potion.getAsJsonPrimitive("particles").getAsBoolean();
- }
- if (potion.has("icon")) {
- icon = potion.getAsJsonPrimitive("icon").getAsBoolean();
- }
+ int duration = getInt(potion, "duration", 1);
+ int amplifier = getInt(potion, "amplifier", 1);
+ boolean ambient = getBoolean(potion, "ambient", true);
+ boolean particles = getBoolean(potion, "particles", true);
+ boolean icon = getBoolean(potion, "icon", true);
PotionEffectType potionEffectType = PotionEffectType.getByName(potion.getAsJsonPrimitive("potion").getAsString());
if (potionEffectType == null) {
diff --git a/src/de/steamwar/misslewars/scripts/implemented/RemoveScript.java b/src/de/steamwar/misslewars/scripts/implemented/RemoveScript.java
index 30eb0e6..63530b0 100644
--- a/src/de/steamwar/misslewars/scripts/implemented/RemoveScript.java
+++ b/src/de/steamwar/misslewars/scripts/implemented/RemoveScript.java
@@ -24,7 +24,7 @@ import de.steamwar.misslewars.scripts.RunnableScript;
import de.steamwar.misslewars.scripts.RunnableScriptEvent;
import org.bukkit.entity.Player;
-public class RemoveScript implements RunnableScript {
+public class RemoveScript extends RunnableScript {
public RemoveScript(JsonObject jsonObject) {
diff --git a/src/de/steamwar/misslewars/scripts/implemented/SoundScript.java b/src/de/steamwar/misslewars/scripts/implemented/SoundScript.java
index 1fd3914..f7abd69 100644
--- a/src/de/steamwar/misslewars/scripts/implemented/SoundScript.java
+++ b/src/de/steamwar/misslewars/scripts/implemented/SoundScript.java
@@ -23,23 +23,20 @@ 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.utils.JsonUtils;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
-public class SoundScript implements RunnableScript {
+public class SoundScript extends RunnableScript {
private Sound sound;
- private float volume = 100;
- private float pitch = 1;
+ private float volume;
+ private float pitch;
public SoundScript(JsonObject sound) {
- this.sound = Sound.valueOf(sound.getAsJsonPrimitive("sound").getAsString());
- if (sound.has("volume")) {
- volume = sound.getAsJsonPrimitive("volume").getAsFloat();
- }
- if (sound.has("pitch")) {
- pitch = sound.getAsJsonPrimitive("pitch").getAsFloat();
- }
+ JsonUtils.getString(sound, "sound", value -> this.sound = Sound.valueOf(value));
+ volume = JsonUtils.getFloat(sound, "volume", 100);
+ pitch = JsonUtils.getFloat(sound, "pitch", 1);
}
@Override
diff --git a/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java b/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java
index 0193e9e..65d3fda 100644
--- a/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java
+++ b/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java
@@ -22,29 +22,24 @@ 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 org.bukkit.entity.TNTPrimed;
+import java.util.HashMap;
+import java.util.Map;
+
import static de.steamwar.misslewars.scripts.utils.EntityUtils.*;
-public class SummonScript implements RunnableScript {
+public class SummonScript extends RunnableScript {
- private interface Summon {
-
- void summon(RunnableScriptEvent event);
-
- }
-
- private Summon summon = null;
+ private ScriptVoidFunction summon = null;
public SummonScript(JsonObject summon) {
switch (summon.getAsJsonPrimitive("entity").getAsString().toLowerCase()) {
case "tntprimed":
this.summon = runnableScriptEvent -> {
TNTPrimed tnt = runnableScriptEvent.entity.getWorld().spawn(runnableScriptEvent.getLocation(), TNTPrimed.class);
-
- setYield(tnt, summon);
- setIncendiary(tnt, summon);
- setFuseTime(tnt, summon);
+ setTNTPrimedOptions(tnt, summon);
};
break;
default:
@@ -55,7 +50,7 @@ public class SummonScript implements RunnableScript {
@Override
public boolean execute(RunnableScriptEvent runnableScriptEvent) {
if (summon == null) return false;
- summon.summon(runnableScriptEvent);
+ summon.execute(runnableScriptEvent);
return true;
}
diff --git a/src/de/steamwar/misslewars/scripts/utils/EntityUtils.java b/src/de/steamwar/misslewars/scripts/utils/EntityUtils.java
index d3025fe..8e9d66a 100644
--- a/src/de/steamwar/misslewars/scripts/utils/EntityUtils.java
+++ b/src/de/steamwar/misslewars/scripts/utils/EntityUtils.java
@@ -20,60 +20,42 @@
package de.steamwar.misslewars.scripts.utils;
import com.google.gson.JsonObject;
-import org.bukkit.entity.Entity;
-import org.bukkit.entity.Explosive;
-import org.bukkit.entity.Projectile;
-import org.bukkit.entity.TNTPrimed;
+import org.bukkit.entity.*;
+
+import static de.steamwar.misslewars.scripts.utils.JsonUtils.*;
public class EntityUtils {
- public static void setVelocity(Entity entity, JsonObject jsonObject) {
- if (jsonObject.has("velocity")) {
- double velocity = jsonObject.getAsJsonPrimitive("velocity").getAsDouble();
- entity.setVelocity(entity.getVelocity().multiply(velocity));
- }
+ private EntityUtils() {
+ throw new IllegalStateException("Utility class");
}
- public static void setGlowing(Entity entity, JsonObject jsonObject) {
- if (jsonObject.has("glowing")) {
- boolean incendiary = jsonObject.getAsJsonPrimitive("glowing").getAsBoolean();
- entity.setGlowing(incendiary);
- }
+ public static void setEntityOptions(Entity entity, JsonObject jsonObject) {
+ getDouble(jsonObject, "velocity", aDouble -> entity.setVelocity(entity.getVelocity().multiply(aDouble)));
+ getBoolean(jsonObject, "glowing", entity::setGlowing);
+ getBoolean(jsonObject, "gravity", entity::setGravity);
}
- public static void setGravity(Entity entity, JsonObject jsonObject) {
- if (jsonObject.has("gravity")) {
- boolean incendiary = jsonObject.getAsJsonPrimitive("gravity").getAsBoolean();
- entity.setGravity(incendiary);
- }
+ public static void setProjectileOptions(Projectile projectile, JsonObject jsonObject) {
+ getBoolean(jsonObject, "bounce", projectile::setBounce);
+ setEntityOptions(projectile, jsonObject);
}
- public static void setYield(Explosive explosive, JsonObject jsonObject) {
- if (jsonObject.has("yield")) {
- float yield = jsonObject.getAsJsonPrimitive("yield").getAsFloat();
- explosive.setYield(yield);
- }
+ public static void setExplosiveOptions(Explosive explosive, JsonObject jsonObject) {
+ getFloat(jsonObject, "yield", explosive::setYield);
+ getBoolean(jsonObject, "incendiary", explosive::setIsIncendiary);
+ setEntityOptions(explosive, jsonObject);
}
- public static void setIncendiary(Explosive explosive, JsonObject jsonObject) {
- if (jsonObject.has("incendiary")) {
- boolean incendiary = jsonObject.getAsJsonPrimitive("incendiary").getAsBoolean();
- explosive.setIsIncendiary(incendiary);
- }
+
+ public static void setFireballOptions(Fireball fireball, JsonObject jsonObject) {
+ setProjectileOptions(fireball, jsonObject);
+ setExplosiveOptions(fireball, jsonObject);
}
- public static void setBounce(Projectile projectile, JsonObject jsonObject) {
- if (jsonObject.has("bounce")) {
- boolean bounce = jsonObject.getAsJsonPrimitive("bounce").getAsBoolean();
- projectile.setBounce(bounce);
- }
- }
-
- public static void setFuseTime(TNTPrimed tntPrimed, JsonObject jsonObject) {
- if (jsonObject.has("fuse")) {
- int fuseTime = jsonObject.getAsJsonPrimitive("fuse").getAsInt();
- tntPrimed.setFuseTicks(fuseTime);
- }
+ public static void setTNTPrimedOptions(TNTPrimed tntPrimed, JsonObject jsonObject) {
+ getInt(jsonObject, "fuse", tntPrimed::setFuseTicks);
+ setExplosiveOptions(tntPrimed, jsonObject);
}
}
diff --git a/src/de/steamwar/misslewars/scripts/utils/JsonUtils.java b/src/de/steamwar/misslewars/scripts/utils/JsonUtils.java
new file mode 100644
index 0000000..31501bb
--- /dev/null
+++ b/src/de/steamwar/misslewars/scripts/utils/JsonUtils.java
@@ -0,0 +1,60 @@
+package de.steamwar.misslewars.scripts.utils;
+
+import com.google.gson.JsonObject;
+
+import java.util.function.Consumer;
+import java.util.function.DoubleConsumer;
+import java.util.function.IntConsumer;
+
+public class JsonUtils {
+
+ private JsonUtils() {
+ throw new IllegalStateException("Utility class");
+ }
+
+ public static boolean getBoolean(JsonObject jsonObject, String key, boolean defaultValue) {
+ if (jsonObject.has(key)) return jsonObject.getAsJsonPrimitive(key).getAsBoolean();
+ return defaultValue;
+ }
+
+ public static int getInt(JsonObject jsonObject, String key, int defaultValue) {
+ if (jsonObject.has(key)) return jsonObject.getAsJsonPrimitive(key).getAsInt();
+ 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;
+ }
+
+ public static String getString(JsonObject jsonObject, String key, String defaultValue) {
+ if (jsonObject.has(key)) return jsonObject.getAsJsonPrimitive(key).getAsString();
+ return defaultValue;
+ }
+
+ public static void getBoolean(JsonObject jsonObject, String key, Consumer booleanConsumer) {
+ if (jsonObject.has(key)) booleanConsumer.accept(jsonObject.getAsJsonPrimitive(key).getAsBoolean());
+ }
+
+ public static void getInt(JsonObject jsonObject, String key, IntConsumer booleanConsumer) {
+ if (jsonObject.has(key)) booleanConsumer.accept(jsonObject.getAsJsonPrimitive(key).getAsInt());
+ }
+
+ public static void getDouble(JsonObject jsonObject, String key, DoubleConsumer booleanConsumer) {
+ if (jsonObject.has(key)) booleanConsumer.accept(jsonObject.getAsJsonPrimitive(key).getAsDouble());
+ }
+
+ public static void getFloat(JsonObject jsonObject, String key, Consumer booleanConsumer) {
+ if (jsonObject.has(key)) booleanConsumer.accept(jsonObject.getAsJsonPrimitive(key).getAsFloat());
+ }
+
+ public static void getString(JsonObject jsonObject, String key, Consumer booleanConsumer) {
+ if (jsonObject.has(key)) booleanConsumer.accept(jsonObject.getAsJsonPrimitive(key).getAsString());
+ }
+
+}