SteamWar/MissileWars
Archiviert
13
0

Update2.0 #22

Manuell gemergt
YoyoNow hat 32 Commits von Update2.0 nach master 2020-12-20 13:52:31 +01:00 zusammengeführt
19 geänderte Dateien mit 356 neuen und 298 gelöschten Zeilen
Nur Änderungen aus Commit eaefc39fbc werden angezeigt - Alle Commits anzeigen

Datei anzeigen

@ -38,7 +38,7 @@ public class ItemCountdown extends StateDependent {
super(EnumSet.of(FightState.FIGHTING)); super(EnumSet.of(FightState.FIGHTING));
} }
private void run(){ private void run() {
int itemCount = Math.max(MissileWars.getBlueTeam().size(), MissileWars.getRedTeam().size()); int itemCount = Math.max(MissileWars.getBlueTeam().size(), MissileWars.getRedTeam().size());
for (int i = 0; i < itemCount; i++) { for (int i = 0; i < itemCount; i++) {

Datei anzeigen

@ -19,8 +19,8 @@
package de.steamwar.misslewars.scripts; package de.steamwar.misslewars.scripts;
public interface RunnableScript { public abstract class RunnableScript {
boolean execute(RunnableScriptEvent runnableScriptEvent); public abstract boolean execute(RunnableScriptEvent runnableScriptEvent);
} }

Datei anzeigen

@ -19,8 +19,10 @@
package de.steamwar.misslewars.scripts; package de.steamwar.misslewars.scripts;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import de.steamwar.misslewars.MissileWars; import de.steamwar.misslewars.MissileWars;
import de.steamwar.misslewars.scripts.implemented.DelayScript; import de.steamwar.misslewars.scripts.implemented.*;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import java.util.ArrayList; 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;
}
}
} }

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
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;
}
}
}

Datei anzeigen

@ -70,7 +70,7 @@ public class ScriptedItem {
String eventString = "EVENT." + eventType.name(); String eventString = "EVENT." + eventType.name();
if (!jsonObject.has(eventString)) continue; if (!jsonObject.has(eventString)) continue;
if (!jsonObject.get(eventString).isJsonArray()) continue; if (!jsonObject.get(eventString).isJsonArray()) continue;
scriptMap.put(eventType, ScriptParser.parseScript(jsonObject.getAsJsonArray(eventString))); scriptMap.put(eventType, Script.parseScript(jsonObject.getAsJsonArray(eventString)));
} }
} }

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.misslewars.scripts.function;
import de.steamwar.misslewars.scripts.RunnableScriptEvent;
public interface ScriptBooleanFunction {
boolean execute(RunnableScriptEvent runnableScriptEvent);
}

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.misslewars.scripts.function;
import de.steamwar.misslewars.scripts.RunnableScriptEvent;
public interface ScriptVoidDoubleFunction {
void execute(RunnableScriptEvent runnableScriptEvent, double... doubles);
}

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.misslewars.scripts.function;
import de.steamwar.misslewars.scripts.RunnableScriptEvent;
public interface ScriptVoidFunction {
void execute(RunnableScriptEvent runnableScriptEvent);
}

Datei anzeigen

@ -25,37 +25,30 @@ import de.steamwar.misslewars.Config;
import de.steamwar.misslewars.scripts.RunnableScript; import de.steamwar.misslewars.scripts.RunnableScript;
import de.steamwar.misslewars.scripts.RunnableScriptEvent; 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<String, Integer> 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; private int delayTime = 0;
public DelayScript(JsonObject delay) { public DelayScript(JsonObject delay) {
JsonPrimitive jsonPrimitive = delay.getAsJsonPrimitive("time"); JsonPrimitive jsonPrimitive = delay.getAsJsonPrimitive("time");
if (jsonPrimitive.isString()) { if (jsonPrimitive.isString()) {
switch (jsonPrimitive.getAsString().toLowerCase()) { delayTime = delayMap.getOrDefault(jsonPrimitive.getAsString().toLowerCase(), 0);
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;
}
} else if (jsonPrimitive.isNumber()) { } else if (jsonPrimitive.isNumber()) {
delayTime = jsonPrimitive.getAsInt(); delayTime = jsonPrimitive.getAsInt();
} }

Datei anzeigen

@ -23,23 +23,20 @@ import com.google.gson.JsonObject;
import de.steamwar.misslewars.MissileWars; import de.steamwar.misslewars.MissileWars;
import de.steamwar.misslewars.scripts.RunnableScript; import de.steamwar.misslewars.scripts.RunnableScript;
import de.steamwar.misslewars.scripts.RunnableScriptEvent; import de.steamwar.misslewars.scripts.RunnableScriptEvent;
import de.steamwar.misslewars.scripts.function.ScriptBooleanFunction;
import org.bukkit.Location; 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<String, ScriptBooleanFunction> filterMap = new HashMap<>();
private boolean inverted = false; static {
private Filter filter = null; filterMap.put("nearportal", runnableScriptEvent -> {
public FilterScript(JsonObject filter) {
switch (filter.getAsJsonPrimitive("filter").getAsString().toLowerCase()) {
case "nearportal":
this.filter = runnableScriptEvent -> {
Location location = runnableScriptEvent.getLocation(); Location location = runnableScriptEvent.getLocation();
int bz = MissileWars.getBlueTeam().getPortalZ(); int bz = MissileWars.getBlueTeam().getPortalZ();
int rz = MissileWars.getRedTeam().getPortalZ(); int rz = MissileWars.getRedTeam().getPortalZ();
@ -52,11 +49,23 @@ public class FilterScript implements RunnableScript {
} else { } else {
return (blockZ < bz - offset) || (blockZ > rz + offset); return (blockZ < bz - offset) || (blockZ > rz + offset);
} }
}; });
Review

If Else oder ||?

If Else oder ||?
break; filterMap.put("nearspawn", runnableScriptEvent -> {
default: Location location = runnableScriptEvent.getLocation();
break; return MissileWars.getBlueTeam().getSpawn().distance(location) < 3 || MissileWars.getRedTeam().getSpawn().distance(location) < 3;
Review

Selbiges.

Selbiges.
});
} }
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")) { if (filter.has("invert")) {
inverted = filter.getAsJsonPrimitive("invert").getAsBoolean(); inverted = filter.getAsJsonPrimitive("invert").getAsBoolean();
} }
@ -66,15 +75,10 @@ public class FilterScript implements RunnableScript {
public boolean execute(RunnableScriptEvent runnableScriptEvent) { public boolean execute(RunnableScriptEvent runnableScriptEvent) {
if (filter == null) return true; if (filter == null) return true;
if (inverted) { if (inverted) {
return !filter.filter(runnableScriptEvent); return !filter.execute(runnableScriptEvent);
} else { } else {
return filter.filter(runnableScriptEvent); return filter.execute(runnableScriptEvent);
} }
} }
private int sign(int i) {
if (i < 0) return -1;
return i > 0 ? 1 : 0;
}
} }

Datei anzeigen

@ -23,19 +23,17 @@ import com.google.gson.JsonObject;
import de.steamwar.misslewars.scripts.RunnableScript; import de.steamwar.misslewars.scripts.RunnableScript;
import de.steamwar.misslewars.scripts.RunnableScriptEvent; import de.steamwar.misslewars.scripts.RunnableScriptEvent;
import de.steamwar.misslewars.scripts.ScriptedItem; 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 { private ScriptVoidFunction launch = null;
void launch(RunnableScriptEvent runnableScriptEvent);
}
private Launch launch = null;
public LaunchScript(JsonObject launch) { public LaunchScript(JsonObject launch) {
switch (launch.getAsJsonPrimitive("entity").getAsString().toLowerCase()) { switch (launch.getAsJsonPrimitive("entity").getAsString().toLowerCase()) {
@ -43,14 +41,7 @@ public class LaunchScript implements RunnableScript {
this.launch = runnableScriptEvent -> { this.launch = runnableScriptEvent -> {
Player player = (Player) runnableScriptEvent.entity; Player player = (Player) runnableScriptEvent.entity;
Fireball fireball = player.launchProjectile(Fireball.class); Fireball fireball = player.launchProjectile(Fireball.class);
setFireballOptions(fireball, launch);
setVelocity(fireball, launch);
setYield(fireball, launch);
setIncendiary(fireball, launch);
setBounce(fireball, launch);
setGlowing(fireball, launch);
setGravity(fireball, launch);
fireball.setDirection(runnableScriptEvent.getLocation().getDirection()); fireball.setDirection(runnableScriptEvent.getLocation().getDirection());
}; };
break; break;
@ -58,10 +49,7 @@ public class LaunchScript implements RunnableScript {
this.launch = runnableScriptEvent -> { this.launch = runnableScriptEvent -> {
Player player = (Player) runnableScriptEvent.entity; Player player = (Player) runnableScriptEvent.entity;
Arrow arrow = player.launchProjectile(Arrow.class); Arrow arrow = player.launchProjectile(Arrow.class);
setProjectileOptions(arrow, launch);
setVelocity(arrow, launch);
setGlowing(arrow, launch);
setGravity(arrow, launch);
}; };
break; break;
default: default:
@ -73,7 +61,7 @@ public class LaunchScript implements RunnableScript {
public boolean execute(RunnableScriptEvent runnableScriptEvent) { public boolean execute(RunnableScriptEvent runnableScriptEvent) {
if (launch == null) return false; if (launch == null) return false;
if (runnableScriptEvent.eventType != ScriptedItem.EventType.onClick) return true; if (runnableScriptEvent.eventType != ScriptedItem.EventType.onClick) return true;
launch.launch(runnableScriptEvent); launch.execute(runnableScriptEvent);
return true; return true;
} }

Datei anzeigen

@ -23,64 +23,62 @@ import com.google.gson.JsonObject;
import de.steamwar.misslewars.scripts.LocationType; import de.steamwar.misslewars.scripts.LocationType;
import de.steamwar.misslewars.scripts.RunnableScript; import de.steamwar.misslewars.scripts.RunnableScript;
import de.steamwar.misslewars.scripts.RunnableScriptEvent; import de.steamwar.misslewars.scripts.RunnableScriptEvent;
import de.steamwar.misslewars.scripts.function.ScriptVoidDoubleFunction;
import de.steamwar.misslewars.scripts.utils.JsonUtils;
import org.bukkit.Location; import org.bukkit.Location;
public class LocationScript implements RunnableScript { import java.util.HashMap;
import java.util.Map;
private LocationType locationType = null; import static de.steamwar.misslewars.scripts.utils.JsonUtils.getString;
private LocationExecutor locationExecutor = null;
private interface LocationExecutor { public class LocationScript extends RunnableScript {
void location(RunnableScriptEvent runnableScriptEvent); private static Map<String, LocationType> locationTypeMap = new HashMap<>();
private static Map<String, ScriptVoidDoubleFunction> locationMap = new HashMap<>();
} static {
locationTypeMap.put("static", LocationType.STATIC);
locationTypeMap.put("dynamic", LocationType.DYNAMIC);
locationTypeMap.put("custom", LocationType.CUSTOM);
locationTypeMap.put("default", LocationType.DEFAULT);
public LocationScript(JsonObject location) { locationMap.put("offsetentity", (runnableScriptEvent, doubles) -> {
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) { if (runnableScriptEvent.entity == null) {
return; return;
} }
Location location1 = runnableScriptEvent.entity.getLocation(); Location location1 = runnableScriptEvent.entity.getLocation();
runnableScriptEvent.setCustomLocation(location1.getX() + x, location1.getY() + y, location1.getZ() + z); runnableScriptEvent.setCustomLocation(location1.getX() + doubles[0], location1.getY() + doubles[1], location1.getZ() + doubles[2]);
}; });
break; locationMap.put("offsetlocation", (runnableScriptEvent, doubles) -> {
case "offsetLocation":
locationExecutor = runnableScriptEvent -> {
Location location1 = runnableScriptEvent.getLocation(); Location location1 = runnableScriptEvent.getLocation();
runnableScriptEvent.setCustomLocation(location1.getX() + x, location1.getY() + y, location1.getZ() + z); 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]);
}; };
break; locationMap.put("absolut", absoluteLocation);
locationMap.put("fix", absoluteLocation);
locationMap.put("fixed", absoluteLocation);
} }
private LocationType locationType = null;
private ScriptVoidDoubleFunction locationExecutor = null;
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");
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; locationType = LocationType.CUSTOM;
} else if (location.has("locationType")) { } else if (location.has("locationType")) {
switch (location.getAsJsonPrimitive("locationType").getAsString().toLowerCase()) { locationType = locationTypeMap.getOrDefault(getString(location, "locationType", "").toLowerCase(), LocationType.DEFAULT);
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;
}
} }
} }
@ -88,7 +86,7 @@ public class LocationScript implements RunnableScript {
public boolean execute(RunnableScriptEvent runnableScriptEvent) { public boolean execute(RunnableScriptEvent runnableScriptEvent) {
runnableScriptEvent.setLocationType(locationType); runnableScriptEvent.setLocationType(locationType);
if (locationExecutor != null) { if (locationExecutor != null) {
locationExecutor.location(runnableScriptEvent); locationExecutor.execute(runnableScriptEvent, x, y, z);
} }
return true; return true;
} }

Datei anzeigen

@ -41,24 +41,24 @@ import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.Objects; 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 static final World world = new BukkitWorld(Bukkit.getWorlds().get(0));
private final Clipboard clipboard; private final Clipboard clipboard;
private final BlockVector3 centeredOffset; private final BlockVector3 centeredOffset;
private boolean centered = false; private boolean centered;
private boolean ignoreAir = false; private boolean ignoreAir;
private int xOffset = 0; private int xOffset = 0;
private int yOffset = 0; private int yOffset = 0;
private int zOffset = 0; private int zOffset = 0;
public PasteScript(JsonObject paste) { public PasteScript(JsonObject paste) {
String schemFileName = paste.getAsJsonPrimitive("schem").getAsString(); String schemFileName = paste.getAsJsonPrimitive("schem").getAsString();
if (!schemFileName.endsWith(".schem")) { if (!schemFileName.endsWith(".schem")) schemFileName += ".schem";
schemFileName += ".schem";
}
File schemFile = new File(MissileWars.getPlugin().getDataFolder(), schemFileName); File schemFile = new File(MissileWars.getPlugin().getDataFolder(), schemFileName);
try { try {
@ -68,12 +68,8 @@ public class PasteScript implements RunnableScript {
} }
centeredOffset = clipboard.getRegion().getMinimumPoint().subtract(clipboard.getOrigin()).add(clipboard.getDimensions().divide(2)); centeredOffset = clipboard.getRegion().getMinimumPoint().subtract(clipboard.getOrigin()).add(clipboard.getDimensions().divide(2));
if (paste.has("centered")) { centered = getBoolean(paste, "centered", false);
centered = paste.getAsJsonPrimitive("centered").getAsBoolean(); ignoreAir = getBoolean(paste, "ignoreAir", false);
}
if (paste.has("ignoreAir")) {
ignoreAir = paste.getAsJsonPrimitive("ignoreAir").getAsBoolean();
}
if (paste.has("offset")) { if (paste.has("offset")) {
JsonArray jsonArray = paste.getAsJsonArray("offset"); JsonArray jsonArray = paste.getAsJsonArray("offset");
if (jsonArray.size() == 3) { if (jsonArray.size() == 3) {

Datei anzeigen

@ -27,32 +27,19 @@ import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType; 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; private PotionEffect potionEffect = null;
public PotionScript(JsonObject potion) { public PotionScript(JsonObject potion) {
int duration = 1; int duration = getInt(potion, "duration", 1);
int amplifier = 1; int amplifier = getInt(potion, "amplifier", 1);
boolean ambient = true; boolean ambient = getBoolean(potion, "ambient", true);
boolean particles = true; boolean particles = getBoolean(potion, "particles", true);
boolean icon = true; boolean icon = getBoolean(potion, "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();
}
PotionEffectType potionEffectType = PotionEffectType.getByName(potion.getAsJsonPrimitive("potion").getAsString()); PotionEffectType potionEffectType = PotionEffectType.getByName(potion.getAsJsonPrimitive("potion").getAsString());
if (potionEffectType == null) { if (potionEffectType == null) {

Datei anzeigen

@ -24,7 +24,7 @@ import de.steamwar.misslewars.scripts.RunnableScript;
import de.steamwar.misslewars.scripts.RunnableScriptEvent; import de.steamwar.misslewars.scripts.RunnableScriptEvent;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public class RemoveScript implements RunnableScript { public class RemoveScript extends RunnableScript {
public RemoveScript(JsonObject jsonObject) { public RemoveScript(JsonObject jsonObject) {

Datei anzeigen

@ -23,23 +23,20 @@ import com.google.gson.JsonObject;
import de.steamwar.misslewars.scripts.RunnableScript; import de.steamwar.misslewars.scripts.RunnableScript;
import de.steamwar.misslewars.scripts.RunnableScriptEvent; import de.steamwar.misslewars.scripts.RunnableScriptEvent;
import de.steamwar.misslewars.scripts.ScriptedItem; import de.steamwar.misslewars.scripts.ScriptedItem;
import de.steamwar.misslewars.scripts.utils.JsonUtils;
import org.bukkit.Sound; import org.bukkit.Sound;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public class SoundScript implements RunnableScript { public class SoundScript extends RunnableScript {
private Sound sound; private Sound sound;
private float volume = 100; private float volume;
private float pitch = 1; private float pitch;
public SoundScript(JsonObject sound) { public SoundScript(JsonObject sound) {
this.sound = Sound.valueOf(sound.getAsJsonPrimitive("sound").getAsString()); JsonUtils.getString(sound, "sound", value -> this.sound = Sound.valueOf(value));
if (sound.has("volume")) { volume = JsonUtils.getFloat(sound, "volume", 100);
volume = sound.getAsJsonPrimitive("volume").getAsFloat(); pitch = JsonUtils.getFloat(sound, "pitch", 1);
}
if (sound.has("pitch")) {
pitch = sound.getAsJsonPrimitive("pitch").getAsFloat();
}
} }
@Override @Override

Datei anzeigen

@ -22,29 +22,24 @@ package de.steamwar.misslewars.scripts.implemented;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import de.steamwar.misslewars.scripts.RunnableScript; import de.steamwar.misslewars.scripts.RunnableScript;
import de.steamwar.misslewars.scripts.RunnableScriptEvent; import de.steamwar.misslewars.scripts.RunnableScriptEvent;
import de.steamwar.misslewars.scripts.function.ScriptVoidFunction;
import org.bukkit.entity.TNTPrimed; 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.*;
public class SummonScript implements RunnableScript { public class SummonScript extends RunnableScript {
private interface Summon { private ScriptVoidFunction summon = null;
void summon(RunnableScriptEvent event);
}
private Summon summon = null;
public SummonScript(JsonObject summon) { public SummonScript(JsonObject summon) {
switch (summon.getAsJsonPrimitive("entity").getAsString().toLowerCase()) { switch (summon.getAsJsonPrimitive("entity").getAsString().toLowerCase()) {
case "tntprimed": case "tntprimed":
Review

Warum ist hier dann ein Switch und kein If?

Warum ist hier dann ein Switch und kein If?
Review

Um es später noch zu erweitern

Um es später noch zu erweitern
this.summon = runnableScriptEvent -> { this.summon = runnableScriptEvent -> {
TNTPrimed tnt = runnableScriptEvent.entity.getWorld().spawn(runnableScriptEvent.getLocation(), TNTPrimed.class); TNTPrimed tnt = runnableScriptEvent.entity.getWorld().spawn(runnableScriptEvent.getLocation(), TNTPrimed.class);
setTNTPrimedOptions(tnt, summon);
setYield(tnt, summon);
setIncendiary(tnt, summon);
setFuseTime(tnt, summon);
}; };
break; break;
default: default:
@ -55,7 +50,7 @@ public class SummonScript implements RunnableScript {
@Override @Override
public boolean execute(RunnableScriptEvent runnableScriptEvent) { public boolean execute(RunnableScriptEvent runnableScriptEvent) {
if (summon == null) return false; if (summon == null) return false;
summon.summon(runnableScriptEvent); summon.execute(runnableScriptEvent);
return true; return true;
} }

Datei anzeigen

@ -20,60 +20,42 @@
package de.steamwar.misslewars.scripts.utils; package de.steamwar.misslewars.scripts.utils;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import org.bukkit.entity.Entity; import org.bukkit.entity.*;
import org.bukkit.entity.Explosive;
import org.bukkit.entity.Projectile; import static de.steamwar.misslewars.scripts.utils.JsonUtils.*;
import org.bukkit.entity.TNTPrimed;
public class EntityUtils { public class EntityUtils {
public static void setVelocity(Entity entity, JsonObject jsonObject) { private EntityUtils() {
if (jsonObject.has("velocity")) { throw new IllegalStateException("Utility class");
double velocity = jsonObject.getAsJsonPrimitive("velocity").getAsDouble();
entity.setVelocity(entity.getVelocity().multiply(velocity));
}
} }
public static void setGlowing(Entity entity, JsonObject jsonObject) { public static void setEntityOptions(Entity entity, JsonObject jsonObject) {
if (jsonObject.has("glowing")) { getDouble(jsonObject, "velocity", aDouble -> entity.setVelocity(entity.getVelocity().multiply(aDouble)));
boolean incendiary = jsonObject.getAsJsonPrimitive("glowing").getAsBoolean(); getBoolean(jsonObject, "glowing", entity::setGlowing);
entity.setGlowing(incendiary); getBoolean(jsonObject, "gravity", entity::setGravity);
}
} }
public static void setGravity(Entity entity, JsonObject jsonObject) { public static void setProjectileOptions(Projectile projectile, JsonObject jsonObject) {
if (jsonObject.has("gravity")) { getBoolean(jsonObject, "bounce", projectile::setBounce);
boolean incendiary = jsonObject.getAsJsonPrimitive("gravity").getAsBoolean(); setEntityOptions(projectile, jsonObject);
entity.setGravity(incendiary);
}
} }
public static void setYield(Explosive explosive, JsonObject jsonObject) { public static void setExplosiveOptions(Explosive explosive, JsonObject jsonObject) {
if (jsonObject.has("yield")) { getFloat(jsonObject, "yield", explosive::setYield);
float yield = jsonObject.getAsJsonPrimitive("yield").getAsFloat(); getBoolean(jsonObject, "incendiary", explosive::setIsIncendiary);
explosive.setYield(yield); setEntityOptions(explosive, jsonObject);
}
} }
public static void setIncendiary(Explosive explosive, JsonObject jsonObject) {
if (jsonObject.has("incendiary")) { public static void setFireballOptions(Fireball fireball, JsonObject jsonObject) {
boolean incendiary = jsonObject.getAsJsonPrimitive("incendiary").getAsBoolean(); setProjectileOptions(fireball, jsonObject);
explosive.setIsIncendiary(incendiary); setExplosiveOptions(fireball, jsonObject);
}
} }
public static void setBounce(Projectile projectile, JsonObject jsonObject) { public static void setTNTPrimedOptions(TNTPrimed tntPrimed, JsonObject jsonObject) {
if (jsonObject.has("bounce")) { getInt(jsonObject, "fuse", tntPrimed::setFuseTicks);
boolean bounce = jsonObject.getAsJsonPrimitive("bounce").getAsBoolean(); setExplosiveOptions(tntPrimed, jsonObject);
projectile.setBounce(bounce);
}
}
public static void setFuseTime(TNTPrimed tntPrimed, JsonObject jsonObject) {
if (jsonObject.has("fuse")) {
int fuseTime = jsonObject.getAsJsonPrimitive("fuse").getAsInt();
tntPrimed.setFuseTicks(fuseTime);
}
} }
} }

Datei anzeigen

@ -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<Boolean> 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<Float> booleanConsumer) {
if (jsonObject.has(key)) booleanConsumer.accept(jsonObject.getAsJsonPrimitive(key).getAsFloat());
}
public static void getString(JsonObject jsonObject, String key, Consumer<String> booleanConsumer) {
if (jsonObject.has(key)) booleanConsumer.accept(jsonObject.getAsJsonPrimitive(key).getAsString());
}
}