From 090185558fbc6c3160c19689bc56764bb1c4491a Mon Sep 17 00:00:00 2001 From: jojo Date: Sun, 1 Nov 2020 09:35:35 +0100 Subject: [PATCH] Add LocationScript --- .../misslewars/scripts/RunnableScript.java | 41 ++++++++++ .../misslewars/scripts/ScriptParser.java | 2 + .../scripts/implemented/DelayScript.java | 2 +- .../scripts/implemented/LocationScript.java | 76 +++++++++++++++++++ 4 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 src/de/steamwar/misslewars/scripts/implemented/LocationScript.java diff --git a/src/de/steamwar/misslewars/scripts/RunnableScript.java b/src/de/steamwar/misslewars/scripts/RunnableScript.java index 3d72337..d7eacc5 100644 --- a/src/de/steamwar/misslewars/scripts/RunnableScript.java +++ b/src/de/steamwar/misslewars/scripts/RunnableScript.java @@ -24,11 +24,22 @@ import org.bukkit.entity.Entity; public interface RunnableScript { + enum LocationType { + + STATIC, + DYNAMIC, + DEFAULT, + CUSTOM + + } + class RunnableScriptEvent { public final ScriptedItem.EventType eventType; public final Entity entity; private final Location location; + private Location customLocation; + private LocationType locationType = LocationType.DEFAULT; public RunnableScriptEvent(ScriptedItem.EventType eventType, Entity entity, Location location) { this.eventType = eventType; @@ -37,6 +48,23 @@ public interface RunnableScript { } public Location getLocation() { + // Custom location + if (locationType == LocationType.CUSTOM && customLocation != null) { + return customLocation; + } + + // Static initial Location + if (locationType == LocationType.STATIC) { + return location; + } + + // Dynamic Location if entity is not null + if (locationType == LocationType.DYNAMIC) { + if (entity != null) return entity.getLocation(); + return location; + } + + // Default Location is static if EventType is onClick otherwise dynamic if (eventType == ScriptedItem.EventType.onClick) return location; if (entity != null) { return entity.getLocation(); @@ -44,6 +72,19 @@ public interface RunnableScript { return location; } + public void setLocationType(LocationType locationType) { + if (locationType == null) return; + this.locationType = locationType; + } + + public void setCustomLocation(double x, double y, double z) { + setCustomLocation(x, y, z, 0, 0); + } + + public void setCustomLocation(double x, double y, double z, float pitch, float yaw) { + this.customLocation = new Location(location.getWorld(), x, y, z, yaw, pitch); + } + } boolean execute(RunnableScriptEvent runnableScriptEvent); diff --git a/src/de/steamwar/misslewars/scripts/ScriptParser.java b/src/de/steamwar/misslewars/scripts/ScriptParser.java index 67c7a47..659f86b 100644 --- a/src/de/steamwar/misslewars/scripts/ScriptParser.java +++ b/src/de/steamwar/misslewars/scripts/ScriptParser.java @@ -49,6 +49,8 @@ public class ScriptParser { return new RemoveScript(jsonObject); case "launch": return new LaunchScript(jsonObject); + case "location": + return new LocationScript(jsonObject); case "paste": return new PasteScript(jsonObject); case "potion": diff --git a/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java b/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java index 60247eb..4684639 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java @@ -55,7 +55,7 @@ public class DelayScript implements RunnableScript { default: break; } - } else { + } else if (jsonPrimitive.isNumber()) { delayTime = jsonPrimitive.getAsInt(); } } diff --git a/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java b/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java new file mode 100644 index 0000000..f99006b --- /dev/null +++ b/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java @@ -0,0 +1,76 @@ +package de.steamwar.misslewars.scripts.implemented; + +import com.google.gson.JsonObject; +import de.steamwar.misslewars.scripts.RunnableScript; +import org.bukkit.Location; + +public class LocationScript implements RunnableScript { + + private LocationType locationType = null; + private LocationExecutor locationExecutor = null; + + private interface LocationExecutor { + + void location(RunnableScriptEvent runnableScriptEvent); + + } + + 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; + } + 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; + } + } + } + + @Override + public boolean execute(RunnableScriptEvent runnableScriptEvent) { + if (locationType == null) return false; + runnableScriptEvent.setLocationType(locationType); + if (locationExecutor != null) { + locationExecutor.location(runnableScriptEvent); + } + return true; + } + +}