diff --git a/src/de/steamwar/misslewars/MissileWars.java b/src/de/steamwar/misslewars/MissileWars.java index 1cb7d32..f48e70a 100644 --- a/src/de/steamwar/misslewars/MissileWars.java +++ b/src/de/steamwar/misslewars/MissileWars.java @@ -70,7 +70,7 @@ public class MissileWars extends JavaPlugin { FightScoreboard.init(); Missile.init(); - Item.init(); + CustomItem.init(); StateDependent.setupState(fightState); } diff --git a/src/de/steamwar/misslewars/items/Item.java b/src/de/steamwar/misslewars/items/CustomItem.java similarity index 79% rename from src/de/steamwar/misslewars/items/Item.java rename to src/de/steamwar/misslewars/items/CustomItem.java index f142177..cb2aba8 100644 --- a/src/de/steamwar/misslewars/items/Item.java +++ b/src/de/steamwar/misslewars/items/CustomItem.java @@ -34,22 +34,12 @@ import java.io.FileReader; import java.io.IOException; import java.util.Objects; -public class Item extends SpecialItem { +public class CustomItem extends SpecialItem { private ScriptedItem scriptedItem; - public Item(File item) { - try { - JsonObject jsonObject = new JsonParser().parse(new FileReader(item)).getAsJsonObject(); - System.out.println(jsonObject); - scriptedItem = new ScriptedItem(jsonObject); - } catch (JsonSyntaxException e) { - e.printStackTrace(); - throw new SecurityException("Item JSON error"); - } catch (IOException e) { - e.printStackTrace(); - throw new SecurityException("Corrupt Item"); - } + public CustomItem(ScriptedItem scriptedItem) { + this.scriptedItem = scriptedItem; } @Override @@ -79,7 +69,16 @@ public class Item extends SpecialItem { } for (File itemFile : Objects.requireNonNull(itemsFolder.listFiles())) { if (!itemFile.canRead() || !itemFile.isFile()) continue; - new Item(itemFile); + try { + JsonObject jsonObject = new JsonParser().parse(new FileReader(itemFile)).getAsJsonObject(); + new CustomItem(new ScriptedItem(jsonObject)); + } catch (JsonSyntaxException e) { + e.printStackTrace(); + throw new SecurityException("Item JSON error"); + } catch (IOException e) { + e.printStackTrace(); + throw new SecurityException("Corrupt Item"); + } } } diff --git a/src/de/steamwar/misslewars/items/SpecialItem.java b/src/de/steamwar/misslewars/items/SpecialItem.java index 80e511f..eb34945 100644 --- a/src/de/steamwar/misslewars/items/SpecialItem.java +++ b/src/de/steamwar/misslewars/items/SpecialItem.java @@ -108,7 +108,7 @@ public abstract class SpecialItem { if (location == null) return; for (SpecialItem specialItem : supportItems) { - if (name.contains(((Item) specialItem).getScriptedItem().getEntityName())) { + if (name.contains(((CustomItem) specialItem).getScriptedItem().getEntityName())) { specialItem.handleHit(e.getEntity(), location); } } diff --git a/src/de/steamwar/misslewars/scripts/LocationType.java b/src/de/steamwar/misslewars/scripts/LocationType.java new file mode 100644 index 0000000..b462650 --- /dev/null +++ b/src/de/steamwar/misslewars/scripts/LocationType.java @@ -0,0 +1,10 @@ +package de.steamwar.misslewars.scripts; + +public enum LocationType { + + STATIC, + DYNAMIC, + DEFAULT, + CUSTOM + +} diff --git a/src/de/steamwar/misslewars/scripts/RunnableScript.java b/src/de/steamwar/misslewars/scripts/RunnableScript.java index d7eacc5..4156a98 100644 --- a/src/de/steamwar/misslewars/scripts/RunnableScript.java +++ b/src/de/steamwar/misslewars/scripts/RunnableScript.java @@ -19,74 +19,8 @@ package de.steamwar.misslewars.scripts; -import org.bukkit.Location; -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; - this.entity = entity; - this.location = location; - } - - 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(); - } - 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/RunnableScriptEvent.java b/src/de/steamwar/misslewars/scripts/RunnableScriptEvent.java new file mode 100644 index 0000000..fe3eb4d --- /dev/null +++ b/src/de/steamwar/misslewars/scripts/RunnableScriptEvent.java @@ -0,0 +1,58 @@ +package de.steamwar.misslewars.scripts; + +import org.bukkit.Location; +import org.bukkit.entity.Entity; + +public 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; + this.entity = entity; + this.location = location; + } + + 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(); + } + 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); + } + +} diff --git a/src/de/steamwar/misslewars/scripts/Script.java b/src/de/steamwar/misslewars/scripts/Script.java index c68abb1..6401742 100644 --- a/src/de/steamwar/misslewars/scripts/Script.java +++ b/src/de/steamwar/misslewars/scripts/Script.java @@ -34,9 +34,9 @@ public class Script { private int index = 0; - private final RunnableScript.RunnableScriptEvent runnableScriptEvent; + private final RunnableScriptEvent runnableScriptEvent; - public ScriptExecutor(RunnableScript.RunnableScriptEvent runnableScriptEvent) { + public ScriptExecutor(RunnableScriptEvent runnableScriptEvent) { this.runnableScriptEvent = runnableScriptEvent; } @@ -66,7 +66,7 @@ public class Script { } - public void execute(RunnableScript.RunnableScriptEvent runnableScriptEvent) { + public void execute(RunnableScriptEvent runnableScriptEvent) { new ScriptExecutor(runnableScriptEvent).start(); } diff --git a/src/de/steamwar/misslewars/scripts/ScriptedItem.java b/src/de/steamwar/misslewars/scripts/ScriptedItem.java index b98fefb..6c13949 100644 --- a/src/de/steamwar/misslewars/scripts/ScriptedItem.java +++ b/src/de/steamwar/misslewars/scripts/ScriptedItem.java @@ -85,9 +85,6 @@ public class ScriptedItem { if (jsonObject.has("lore")) { List lore = new ArrayList<>(); jsonObject.getAsJsonArray("lore").forEach(jsonElement -> { - if (!jsonElement.isJsonPrimitive()) { - return; - } lore.add(jsonElement.getAsString()); }); itemMeta.setLore(lore); @@ -99,7 +96,7 @@ public class ScriptedItem { public boolean execute(EventType eventType, Entity entity, Location location) { if (!scriptMap.containsKey(eventType)) return false; - scriptMap.get(eventType).execute(new RunnableScript.RunnableScriptEvent(eventType, entity, location)); + scriptMap.get(eventType).execute(new RunnableScriptEvent(eventType, entity, location)); return true; } diff --git a/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java b/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java index 4684639..2188864 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java @@ -23,6 +23,7 @@ import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; import de.steamwar.misslewars.Config; import de.steamwar.misslewars.scripts.RunnableScript; +import de.steamwar.misslewars.scripts.RunnableScriptEvent; public class DelayScript implements RunnableScript { diff --git a/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java b/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java index ce1e88b..9088c4e 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java @@ -22,6 +22,7 @@ package de.steamwar.misslewars.scripts.implemented; import com.google.gson.JsonObject; import de.steamwar.misslewars.MissileWars; import de.steamwar.misslewars.scripts.RunnableScript; +import de.steamwar.misslewars.scripts.RunnableScriptEvent; import org.bukkit.Location; public class FilterScript implements RunnableScript { diff --git a/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java b/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java index 5a7a943..51958dd 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java @@ -21,6 +21,7 @@ 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.ScriptedItem; import org.bukkit.entity.*; diff --git a/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java b/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java index f99006b..faa3169 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java @@ -1,7 +1,9 @@ package de.steamwar.misslewars.scripts.implemented; import com.google.gson.JsonObject; +import de.steamwar.misslewars.scripts.LocationType; import de.steamwar.misslewars.scripts.RunnableScript; +import de.steamwar.misslewars.scripts.RunnableScriptEvent; import org.bukkit.Location; public class LocationScript implements RunnableScript { @@ -65,7 +67,6 @@ public class LocationScript implements RunnableScript { @Override public boolean execute(RunnableScriptEvent runnableScriptEvent) { - if (locationType == null) return false; runnableScriptEvent.setLocationType(locationType); if (locationExecutor != null) { locationExecutor.location(runnableScriptEvent); diff --git a/src/de/steamwar/misslewars/scripts/implemented/PasteScript.java b/src/de/steamwar/misslewars/scripts/implemented/PasteScript.java index d303778..7ddc2f7 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/PasteScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/PasteScript.java @@ -32,6 +32,7 @@ import com.sk89q.worldedit.session.ClipboardHolder; import com.sk89q.worldedit.world.World; import de.steamwar.misslewars.MissileWars; import de.steamwar.misslewars.scripts.RunnableScript; +import de.steamwar.misslewars.scripts.RunnableScriptEvent; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -63,7 +64,7 @@ public class PasteScript implements RunnableScript { try { clipboard = Objects.requireNonNull(ClipboardFormats.findByFile(schemFile)).getReader(new FileInputStream(schemFile)).read(); } catch (IOException e) { - throw new SecurityException("Could not load shield", e); + throw new SecurityException("Could not load " + schemFileName, e); } centeredOffset = clipboard.getRegion().getMinimumPoint().subtract(clipboard.getOrigin()).add(clipboard.getDimensions().divide(2)); diff --git a/src/de/steamwar/misslewars/scripts/implemented/PotionScript.java b/src/de/steamwar/misslewars/scripts/implemented/PotionScript.java index 26f574f..a3da206 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/PotionScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/PotionScript.java @@ -21,6 +21,7 @@ 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.ScriptedItem; import org.bukkit.entity.Player; import org.bukkit.potion.PotionEffect; diff --git a/src/de/steamwar/misslewars/scripts/implemented/RemoveScript.java b/src/de/steamwar/misslewars/scripts/implemented/RemoveScript.java index fc5d5a7..30eb0e6 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/RemoveScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/RemoveScript.java @@ -21,6 +21,7 @@ package de.steamwar.misslewars.scripts.implemented; import com.google.gson.JsonObject; import de.steamwar.misslewars.scripts.RunnableScript; +import de.steamwar.misslewars.scripts.RunnableScriptEvent; import org.bukkit.entity.Player; public class RemoveScript implements RunnableScript { diff --git a/src/de/steamwar/misslewars/scripts/implemented/SoundScript.java b/src/de/steamwar/misslewars/scripts/implemented/SoundScript.java index eead78a..1fd3914 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/SoundScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/SoundScript.java @@ -21,6 +21,7 @@ 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.ScriptedItem; import org.bukkit.Sound; import org.bukkit.entity.Player; diff --git a/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java b/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java index 0b98da7..0193e9e 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java @@ -21,6 +21,7 @@ package de.steamwar.misslewars.scripts.implemented; import com.google.gson.JsonObject; import de.steamwar.misslewars.scripts.RunnableScript; +import de.steamwar.misslewars.scripts.RunnableScriptEvent; import org.bukkit.entity.TNTPrimed; import static de.steamwar.misslewars.scripts.utils.EntityUtils.*;