From 3b817b8f2bef5a3c68baa39a61d2ad6787780f82 Mon Sep 17 00:00:00 2001 From: jojo Date: Fri, 23 Oct 2020 16:00:15 +0200 Subject: [PATCH 01/31] Add ExplodingArrows --- src/de/steamwar/misslewars/MissileWars.java | 1 + src/de/steamwar/misslewars/items/Arrows.java | 2 +- .../misslewars/items/ExplodingArrows.java | 40 ++++++++++++ .../misslewars/listener/ArrowListener.java | 64 +++++++++++++++++++ .../misslewars/listener/ItemListener.java | 1 - 5 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 src/de/steamwar/misslewars/items/ExplodingArrows.java create mode 100644 src/de/steamwar/misslewars/listener/ArrowListener.java diff --git a/src/de/steamwar/misslewars/MissileWars.java b/src/de/steamwar/misslewars/MissileWars.java index 29e3ec8..9cf4913 100644 --- a/src/de/steamwar/misslewars/MissileWars.java +++ b/src/de/steamwar/misslewars/MissileWars.java @@ -60,6 +60,7 @@ public class MissileWars extends JavaPlugin { new PortalDestructListener(); new WaitingListener(); new FightListener(); + new ArrowListener(); new ChatListener(); getCommand("spectate").setExecutor(new CommandSpectate()); diff --git a/src/de/steamwar/misslewars/items/Arrows.java b/src/de/steamwar/misslewars/items/Arrows.java index 38668f3..5b3bdfc 100644 --- a/src/de/steamwar/misslewars/items/Arrows.java +++ b/src/de/steamwar/misslewars/items/Arrows.java @@ -25,7 +25,7 @@ import org.bukkit.inventory.ItemStack; public class Arrows extends SpecialItem { - private final ItemStack item = createItem(Material.ARROW, "§ePfeile", 3); + private final ItemStack item = createItem(Material.ARROW, "§ePfeil", 3); @Override public ItemStack getItem() { diff --git a/src/de/steamwar/misslewars/items/ExplodingArrows.java b/src/de/steamwar/misslewars/items/ExplodingArrows.java new file mode 100644 index 0000000..ebabcfa --- /dev/null +++ b/src/de/steamwar/misslewars/items/ExplodingArrows.java @@ -0,0 +1,40 @@ +/* + 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.items; + +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; + +public class ExplodingArrows extends SpecialItem { + + private final ItemStack item = createItem(Material.SPECTRAL_ARROW, "§eExplodierender Pfeil", 1); + + @Override + public ItemStack getItem() { + return item; + } + + @Override + public boolean handleUse(Player p) { + return false; + } + +} diff --git a/src/de/steamwar/misslewars/listener/ArrowListener.java b/src/de/steamwar/misslewars/listener/ArrowListener.java new file mode 100644 index 0000000..5a806d7 --- /dev/null +++ b/src/de/steamwar/misslewars/listener/ArrowListener.java @@ -0,0 +1,64 @@ +/* + 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.listener; + +import de.steamwar.misslewars.FightState; +import org.bukkit.Location; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Projectile; +import org.bukkit.entity.TNTPrimed; +import org.bukkit.event.EventHandler; +import org.bukkit.event.entity.ProjectileHitEvent; + +import java.util.EnumSet; + +public class ArrowListener extends BasicListener { + + public ArrowListener() { + super(EnumSet.of(FightState.FIGHTING)); + } + + @EventHandler + public void onHit(ProjectileHitEvent e) { + Projectile projectile = e.getEntity(); + if (projectile.getType() != EntityType.SPECTRAL_ARROW) { + return; + } + + Location location = getLocation(e); + if (location == null) return; + + TNTPrimed tnt = e.getEntity().getWorld().spawn(location, TNTPrimed.class); + tnt.setYield(1.0F); + tnt.setIsIncendiary(false); + tnt.setFuseTicks(0); + } + + private Location getLocation(ProjectileHitEvent e) { + if (e.getHitEntity() != null) { + return e.getHitEntity().getLocation(); + } + if (e.getHitBlock() != null) { + return e.getHitBlock().getLocation(); + } + return null; + } + +} diff --git a/src/de/steamwar/misslewars/listener/ItemListener.java b/src/de/steamwar/misslewars/listener/ItemListener.java index 14e6ec6..bb9032a 100644 --- a/src/de/steamwar/misslewars/listener/ItemListener.java +++ b/src/de/steamwar/misslewars/listener/ItemListener.java @@ -31,7 +31,6 @@ import com.sk89q.worldedit.world.World; import de.steamwar.misslewars.Config; import de.steamwar.misslewars.FightState; import de.steamwar.misslewars.MissileWars; -import de.steamwar.misslewars.items.Missile; import de.steamwar.misslewars.items.SpecialItem; import org.bukkit.Bukkit; import org.bukkit.Location; -- 2.39.2 From 4039c179ed85a00b1daa9617e816fdf2e0ee50be Mon Sep 17 00:00:00 2001 From: jojo Date: Sat, 24 Oct 2020 12:51:54 +0200 Subject: [PATCH 02/31] Add json Config Idea Fix ArrowListener explosive yield --- src/de/steamwar/misslewars/items/arrows.json | 16 ++++++++++++++++ .../misslewars/listener/ArrowListener.java | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 src/de/steamwar/misslewars/items/arrows.json diff --git a/src/de/steamwar/misslewars/items/arrows.json b/src/de/steamwar/misslewars/items/arrows.json new file mode 100644 index 0000000..f2d3ddc --- /dev/null +++ b/src/de/steamwar/misslewars/items/arrows.json @@ -0,0 +1,16 @@ +{ + "type": "ARROW", + "name": "§ePfeil", + "lore": [], + "amount": 3, + "EVENT.onThrow": [ + + ], + "EVENT.onHit": [ + {"type": "delay", "time": 0}, + {"type": "paste", "schem": ""} + ], + "EVENT.onClick": [ + + ] +} \ No newline at end of file diff --git a/src/de/steamwar/misslewars/listener/ArrowListener.java b/src/de/steamwar/misslewars/listener/ArrowListener.java index 5a806d7..6d5fcf0 100644 --- a/src/de/steamwar/misslewars/listener/ArrowListener.java +++ b/src/de/steamwar/misslewars/listener/ArrowListener.java @@ -46,7 +46,7 @@ public class ArrowListener extends BasicListener { if (location == null) return; TNTPrimed tnt = e.getEntity().getWorld().spawn(location, TNTPrimed.class); - tnt.setYield(1.0F); + tnt.setYield(2.0F); tnt.setIsIncendiary(false); tnt.setFuseTicks(0); } -- 2.39.2 From 4f9078ab3281c80224f5b81ac852614e86f39f88 Mon Sep 17 00:00:00 2001 From: jojo Date: Sat, 31 Oct 2020 17:21:10 +0100 Subject: [PATCH 03/31] Add Support Item softcoded --- .../misslewars/countdowns/ItemCountdown.java | 2 - .../misslewars/items/SpecialItem.java | 5 +- src/de/steamwar/misslewars/items/arrows.json | 80 +++++++++++--- .../misslewars/scripts/RunnableScript.java | 40 +++++++ .../steamwar/misslewars/scripts/Script.java | 49 +++++++++ .../misslewars/scripts/ScriptParser.java | 64 +++++++++++ .../misslewars/scripts/ScriptedItem.java | 102 ++++++++++++++++++ .../scripts/implemented/DelayScript.java | 73 +++++++++++++ .../scripts/implemented/FilterScript.java | 82 ++++++++++++++ .../scripts/implemented/LaunchScript.java | 79 ++++++++++++++ .../scripts/implemented/PasteScript.java | 100 +++++++++++++++++ .../scripts/implemented/PotionScript.java | 72 +++++++++++++ .../scripts/implemented/SoundScript.java | 53 +++++++++ .../scripts/implemented/SummonScript.java | 62 +++++++++++ .../misslewars/scripts/utils/EntityUtils.java | 79 ++++++++++++++ 15 files changed, 924 insertions(+), 18 deletions(-) create mode 100644 src/de/steamwar/misslewars/scripts/RunnableScript.java create mode 100644 src/de/steamwar/misslewars/scripts/Script.java create mode 100644 src/de/steamwar/misslewars/scripts/ScriptParser.java create mode 100644 src/de/steamwar/misslewars/scripts/ScriptedItem.java create mode 100644 src/de/steamwar/misslewars/scripts/implemented/DelayScript.java create mode 100644 src/de/steamwar/misslewars/scripts/implemented/FilterScript.java create mode 100644 src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java create mode 100644 src/de/steamwar/misslewars/scripts/implemented/PasteScript.java create mode 100644 src/de/steamwar/misslewars/scripts/implemented/PotionScript.java create mode 100644 src/de/steamwar/misslewars/scripts/implemented/SoundScript.java create mode 100644 src/de/steamwar/misslewars/scripts/implemented/SummonScript.java create mode 100644 src/de/steamwar/misslewars/scripts/utils/EntityUtils.java diff --git a/src/de/steamwar/misslewars/countdowns/ItemCountdown.java b/src/de/steamwar/misslewars/countdowns/ItemCountdown.java index f5050ee..4bb8e12 100644 --- a/src/de/steamwar/misslewars/countdowns/ItemCountdown.java +++ b/src/de/steamwar/misslewars/countdowns/ItemCountdown.java @@ -29,12 +29,10 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.scheduler.BukkitTask; import java.util.EnumSet; -import java.util.Random; public class ItemCountdown extends StateDependent { private BukkitTask task; - private Random random = new Random(); public ItemCountdown() { super(EnumSet.of(FightState.FIGHTING)); diff --git a/src/de/steamwar/misslewars/items/SpecialItem.java b/src/de/steamwar/misslewars/items/SpecialItem.java index b66c04c..147bc38 100644 --- a/src/de/steamwar/misslewars/items/SpecialItem.java +++ b/src/de/steamwar/misslewars/items/SpecialItem.java @@ -20,13 +20,14 @@ package de.steamwar.misslewars.items; import org.bukkit.Material; -import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; -import java.util.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; public abstract class SpecialItem { diff --git a/src/de/steamwar/misslewars/items/arrows.json b/src/de/steamwar/misslewars/items/arrows.json index f2d3ddc..69c6246 100644 --- a/src/de/steamwar/misslewars/items/arrows.json +++ b/src/de/steamwar/misslewars/items/arrows.json @@ -1,16 +1,68 @@ -{ - "type": "ARROW", - "name": "§ePfeil", - "lore": [], - "amount": 3, - "EVENT.onThrow": [ +[ + { + "type": "ARROW", + "name": "§ePfeil", + "lore": [], + "amount": 3, + "EVENT.onThrow": [ - ], - "EVENT.onHit": [ - {"type": "delay", "time": 0}, - {"type": "paste", "schem": ""} - ], - "EVENT.onClick": [ + ], + "EVENT.onHit": [ - ] -} \ No newline at end of file + ], + "EVENT.onClick": [ + + ] + }, + { + "type": "SPECTRAL_ARROW", + "name": "§eExplodierender Pfeil", + "lore": [], + "amount": 1, + "EVENT.onHit": [ + {"type": "summon", "entity": "TNTPrimed", "incendiary": false, "fuse": 0, "yield": 2.0} + ] + }, + { + "type": "FIRE_CHARGE", + "name": "§eFeuerball", + "lore": [], + "amount": 1, + "EVENT.onClick": [ + {"type": "launch", "entity": "Fireball", "incendiary": false, "bounce": false, "yield": 3.0, "velocity": 2}, + {"type": "sound", "sound": "ITEM_FIRECHARGE_USE", "volume": 100, "pitch": 1} + ] + }, + { + "type": "SLIME_BALL", + "name": "§aLanding Pad", + "lore": [], + "amount": 1, + "EVENT.onClick": [ + {"type": "potion", "potion": "SLOW_FALLING", "duration": 2, "amplifier": 1, "ambient": false, "particles": false, "icon": false}, + {"type": "paste", "schem": "landingPad.schem", "centered": true, "offset": [0, -5, 0], "ignoreAir": true} + ] + }, + { + "type": "EGG", + "name": "§eMine", + "lore": [], + "amount": 1, + "EVENT.onThrow": [ + {"type": "delay", "time": "CONFIG.MineFlyTime"}, + {"type": "filter", "filter": "nearPortal", "invert": true}, + {"type": "paste", "schem": "mine.schem", "centered": true, "offset": [0, 0, 0], "ignoreAir": true} + ] + }, + { + "type": "SNOWBALL", + "name": "§aSchild", + "lore": [], + "amount": 1, + "EVENT.onThrow": [ + {"type": "delay", "time": "CONFIG.ShieldFlyTime"}, + {"type": "filter", "filter": "nearPortal", "invert": true}, + {"type": "paste", "schem": "shield.schem", "centered": true, "offset": [0, 0, 0], "ignoreAir": true} + ] + } +] \ No newline at end of file diff --git a/src/de/steamwar/misslewars/scripts/RunnableScript.java b/src/de/steamwar/misslewars/scripts/RunnableScript.java new file mode 100644 index 0000000..1ae8fd2 --- /dev/null +++ b/src/de/steamwar/misslewars/scripts/RunnableScript.java @@ -0,0 +1,40 @@ +/* + 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 org.bukkit.entity.Entity; + +public interface RunnableScript { + + class RunnableScriptEvent { + + public final ScriptedItem.EventType eventType; + public final Entity entity; + + public RunnableScriptEvent(ScriptedItem.EventType eventType, Entity entity) { + this.eventType = eventType; + this.entity = entity; + } + + } + + boolean execute(RunnableScriptEvent runnableScriptEvent); + +} diff --git a/src/de/steamwar/misslewars/scripts/Script.java b/src/de/steamwar/misslewars/scripts/Script.java new file mode 100644 index 0000000..b4a9136 --- /dev/null +++ b/src/de/steamwar/misslewars/scripts/Script.java @@ -0,0 +1,49 @@ +/* + 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 de.steamwar.misslewars.MissileWars; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +import java.util.ArrayList; +import java.util.List; + +public class Script { + + private List runnableScriptList = new ArrayList<>(); + + public void execute(RunnableScript.RunnableScriptEvent runnableScriptEvent) { + Bukkit.getScheduler().runTaskLater(MissileWars.getPlugin(), () -> { + int i = 0; + while (i < runnableScriptList.size()) { + if (!runnableScriptList.get(i).execute(runnableScriptEvent)) { + break; + } + i++; + } + }, 0); + } + + public void add(RunnableScript runnableScript) { + runnableScriptList.add(runnableScript); + } + +} diff --git a/src/de/steamwar/misslewars/scripts/ScriptParser.java b/src/de/steamwar/misslewars/scripts/ScriptParser.java new file mode 100644 index 0000000..346fb77 --- /dev/null +++ b/src/de/steamwar/misslewars/scripts/ScriptParser.java @@ -0,0 +1,64 @@ +/* + 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 -> { + if (!jsonArray.isJsonObject()) return; + 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 "launch": + return new LaunchScript(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 new file mode 100644 index 0000000..c256c04 --- /dev/null +++ b/src/de/steamwar/misslewars/scripts/ScriptedItem.java @@ -0,0 +1,102 @@ +/* + 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 org.bukkit.Material; +import org.bukkit.entity.Entity; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class ScriptedItem { + + // "type": Material name (STRING) + // "name": Item name (STRING) + // "lore": Lore array (OPTIONAL STRING.ARRAY) + // "amount": Item amount (OPTIONAL [default 1] INT) + // "EVENT.": Event (OPTIONAL JSONobject.ARRAY) + // - onClick + // - onHit + // - onThrow + + public enum EventType { + onHit("onHit"), + onThrow("onThrow"), + onClick("onClick"); + + String name; + + EventType(String name) { + this.name = name; + } + } + + private Map scriptMap = new HashMap<>(); + + private ItemStack itemStack; + + public ScriptedItem(JsonObject jsonObject) { + itemStack = createItemStack(jsonObject); + + for (EventType eventType : EventType.values()) { + String eventString = "EVENT." + eventType.name(); + if (!jsonObject.has(eventString)) continue; + if (!jsonObject.get(eventString).isJsonArray()) continue; + scriptMap.put(eventType, ScriptParser.parseScript(jsonObject.getAsJsonArray(eventString))); + } + } + + private ItemStack createItemStack(JsonObject jsonObject) { + String type = jsonObject.getAsJsonPrimitive("type").getAsString(); + String name = jsonObject.getAsJsonPrimitive("name").getAsString(); + int amount = jsonObject.getAsJsonPrimitive("amount").getAsInt(); + + ItemStack itemStack = new ItemStack(Material.valueOf(type), amount); + ItemMeta itemMeta = itemStack.getItemMeta(); + if (itemMeta == null) return itemStack; + itemMeta.setDisplayName(name); + + if (jsonObject.has("lore")) { + List lore = new ArrayList<>(); + jsonObject.getAsJsonArray("lore").forEach(jsonElement -> { + if (!jsonElement.isJsonPrimitive()) { + return; + } + lore.add(jsonElement.getAsString()); + }); + itemMeta.setLore(lore); + } + + itemStack.setItemMeta(itemMeta); + return itemStack; + } + + public void execute(EventType eventType, Entity entity) { + if (!scriptMap.containsKey(eventType)) return; + scriptMap.get(eventType).execute(new RunnableScript.RunnableScriptEvent(eventType, entity)); + } + +} diff --git a/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java b/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java new file mode 100644 index 0000000..137dba4 --- /dev/null +++ b/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java @@ -0,0 +1,73 @@ +/* + 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.implemented; + +import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; +import de.steamwar.misslewars.Config; +import de.steamwar.misslewars.scripts.RunnableScript; + +public class DelayScript implements RunnableScript { + + 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.shiledflytime": + 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 { + delayTime = jsonPrimitive.getAsInt(); + } + } + + @Override + public boolean execute(RunnableScriptEvent runnableScriptEvent) { + try { + Thread.sleep(delayTime); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + return true; + } + +} diff --git a/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java b/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java new file mode 100644 index 0000000..50246d5 --- /dev/null +++ b/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java @@ -0,0 +1,82 @@ +/* + 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.implemented; + +import com.google.gson.JsonObject; +import de.steamwar.misslewars.MissileWars; +import de.steamwar.misslewars.scripts.RunnableScript; +import org.bukkit.Location; + +public class FilterScript implements RunnableScript { + + private interface Filter { + + boolean filter(RunnableScriptEvent runnableScriptEvent); + + } + + private boolean inverted = false; + private Filter filter = null; + + public FilterScript(JsonObject filter) { + switch (filter.getAsJsonPrimitive("filter").getAsString().toLowerCase()) { + case "nearportal": + this.filter = runnableScriptEvent -> { + Location location = runnableScriptEvent.entity.getLocation(); + int bz = MissileWars.getBlueTeam().getPortalZ(); + int rz = MissileWars.getRedTeam().getPortalZ(); + + int offset = sign(bz - rz) * 5; + + int blockZ = location.getBlockZ(); + if (offset > 0) { + if (blockZ > bz - offset) return false; + if (blockZ < rz + offset) return false; + } else { + if (blockZ < bz - offset) return false; + if (blockZ > rz + offset) return false; + } + return true; + }; + break; + default: + break; + } + if (filter.has("invert")) { + inverted = filter.getAsJsonPrimitive("invert").getAsBoolean(); + } + } + + @Override + public boolean execute(RunnableScriptEvent runnableScriptEvent) { + if (filter == null) return true; + if (inverted) { + return !filter.filter(runnableScriptEvent); + } else { + return filter.filter(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 new file mode 100644 index 0000000..1af6fe4 --- /dev/null +++ b/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java @@ -0,0 +1,79 @@ +/* + 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.implemented; + +import com.google.gson.JsonObject; +import de.steamwar.misslewars.scripts.RunnableScript; +import de.steamwar.misslewars.scripts.ScriptedItem; +import org.bukkit.entity.*; + +import static de.steamwar.misslewars.scripts.utils.EntityUtils.*; + +public class LaunchScript implements RunnableScript { + + private interface Launch { + + void launch(RunnableScriptEvent runnableScriptEvent); + + } + + private Launch launch = null; + + public LaunchScript(JsonObject launch) { + switch (launch.getAsJsonPrimitive("entity").getAsString().toLowerCase()) { + case "fireball": + 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); + + fireball.setDirection(player.getLocation().getDirection()); + }; + break; + case "arrow": + this.launch = runnableScriptEvent -> { + Player player = (Player) runnableScriptEvent.entity; + Arrow arrow = player.launchProjectile(Arrow.class); + + setVelocity(arrow, launch); + setGlowing(arrow, launch); + setGravity(arrow, launch); + }; + break; + default: + break; + } + } + + @Override + public boolean execute(RunnableScriptEvent runnableScriptEvent) { + if (launch == null) return false; + if (runnableScriptEvent.eventType != ScriptedItem.EventType.onClick) return true; + launch.launch(runnableScriptEvent); + return true; + } + +} diff --git a/src/de/steamwar/misslewars/scripts/implemented/PasteScript.java b/src/de/steamwar/misslewars/scripts/implemented/PasteScript.java new file mode 100644 index 0000000..742d0a4 --- /dev/null +++ b/src/de/steamwar/misslewars/scripts/implemented/PasteScript.java @@ -0,0 +1,100 @@ +/* + 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.implemented; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.sk89q.worldedit.EditSession; +import com.sk89q.worldedit.WorldEdit; +import com.sk89q.worldedit.bukkit.BukkitWorld; +import com.sk89q.worldedit.extent.clipboard.Clipboard; +import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats; +import com.sk89q.worldedit.function.operation.Operations; +import com.sk89q.worldedit.math.BlockVector3; +import com.sk89q.worldedit.session.ClipboardHolder; +import com.sk89q.worldedit.world.World; +import de.steamwar.misslewars.MissileWars; +import de.steamwar.misslewars.scripts.RunnableScript; +import org.bukkit.Bukkit; +import org.bukkit.Location; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.Objects; + +public class PasteScript implements 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 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"; + } + + File schemFile = new File(MissileWars.getPlugin().getDataFolder(), schemFileName); + try { + clipboard = Objects.requireNonNull(ClipboardFormats.findByFile(schemFile)).getReader(new FileInputStream(schemFile)).read(); + } catch (IOException e) { + throw new SecurityException("Could not load shield", e); + } + 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(); + } + if (paste.has("offset")) { + JsonArray jsonArray = paste.getAsJsonArray("offset"); + if (jsonArray.size() == 3) { + xOffset = jsonArray.get(0).getAsInt(); + yOffset = jsonArray.get(1).getAsInt(); + zOffset = jsonArray.get(2).getAsInt(); + } + } + } + + @Override + public boolean execute(RunnableScriptEvent runnableScriptEvent) { + Location location = runnableScriptEvent.entity.getLocation(); + BlockVector3 paste = BlockVector3.at(location.getX() + xOffset, location.getY() + yOffset, location.getZ() + zOffset); + if (centered) { + paste = paste.subtract(centeredOffset); + } + + EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(world, -1); + Operations.completeBlindly(new ClipboardHolder(clipboard).createPaste(editSession).ignoreAirBlocks(ignoreAir).to(paste).build()); + editSession.flushSession(); + return true; + } + +} diff --git a/src/de/steamwar/misslewars/scripts/implemented/PotionScript.java b/src/de/steamwar/misslewars/scripts/implemented/PotionScript.java new file mode 100644 index 0000000..26f574f --- /dev/null +++ b/src/de/steamwar/misslewars/scripts/implemented/PotionScript.java @@ -0,0 +1,72 @@ +/* + 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.implemented; + +import com.google.gson.JsonObject; +import de.steamwar.misslewars.scripts.RunnableScript; +import de.steamwar.misslewars.scripts.ScriptedItem; +import org.bukkit.entity.Player; +import org.bukkit.potion.PotionEffect; +import org.bukkit.potion.PotionEffectType; + +public class PotionScript implements 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(); + } + + PotionEffectType potionEffectType = PotionEffectType.getByName(potion.getAsJsonPrimitive("potion").getAsString()); + if (potionEffectType == null) { + return; + } + potionEffect = new PotionEffect(potionEffectType, duration, amplifier, ambient, particles, icon); + } + + @Override + public boolean execute(RunnableScriptEvent runnableScriptEvent) { + if (potionEffect == null) return false; + if (runnableScriptEvent.eventType != ScriptedItem.EventType.onClick) return true; + Player player = (Player) runnableScriptEvent.entity; + player.addPotionEffect(potionEffect); + return true; + } + +} diff --git a/src/de/steamwar/misslewars/scripts/implemented/SoundScript.java b/src/de/steamwar/misslewars/scripts/implemented/SoundScript.java new file mode 100644 index 0000000..eead78a --- /dev/null +++ b/src/de/steamwar/misslewars/scripts/implemented/SoundScript.java @@ -0,0 +1,53 @@ +/* + 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.implemented; + +import com.google.gson.JsonObject; +import de.steamwar.misslewars.scripts.RunnableScript; +import de.steamwar.misslewars.scripts.ScriptedItem; +import org.bukkit.Sound; +import org.bukkit.entity.Player; + +public class SoundScript implements RunnableScript { + + private Sound sound; + private float volume = 100; + private float pitch = 1; + + 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(); + } + } + + @Override + public boolean execute(RunnableScriptEvent runnableScriptEvent) { + if (sound == null) return false; + if (runnableScriptEvent.eventType != ScriptedItem.EventType.onClick) return true; + Player player = (Player) runnableScriptEvent.entity; + player.playSound(player.getLocation(), sound, volume, pitch); + return true; + } + +} diff --git a/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java b/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java new file mode 100644 index 0000000..7af36af --- /dev/null +++ b/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java @@ -0,0 +1,62 @@ +/* + 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.implemented; + +import com.google.gson.JsonObject; +import de.steamwar.misslewars.scripts.RunnableScript; +import org.bukkit.entity.Entity; +import org.bukkit.entity.TNTPrimed; + +import static de.steamwar.misslewars.scripts.utils.EntityUtils.*; + +public class SummonScript implements RunnableScript { + + private interface Summon { + + void summon(Entity entity); + + } + + private Summon summon = null; + + public SummonScript(JsonObject summon) { + switch (summon.getAsJsonPrimitive("entity").getAsString().toLowerCase()) { + case "tntprimed": + this.summon = entity -> { + TNTPrimed tnt = entity.getWorld().spawn(entity.getLocation(), TNTPrimed.class); + + setYield(tnt, summon); + setIncendiary(tnt, summon); + setFuseTime(tnt, summon); + }; + break; + default: + break; + } + } + + @Override + public boolean execute(RunnableScriptEvent runnableScriptEvent) { + if (summon == null) return false; + summon.summon(runnableScriptEvent.entity); + return true; + } + +} diff --git a/src/de/steamwar/misslewars/scripts/utils/EntityUtils.java b/src/de/steamwar/misslewars/scripts/utils/EntityUtils.java new file mode 100644 index 0000000..d3025fe --- /dev/null +++ b/src/de/steamwar/misslewars/scripts/utils/EntityUtils.java @@ -0,0 +1,79 @@ +/* + 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.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; + +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)); + } + } + + public static void setGlowing(Entity entity, JsonObject jsonObject) { + if (jsonObject.has("glowing")) { + boolean incendiary = jsonObject.getAsJsonPrimitive("glowing").getAsBoolean(); + entity.setGlowing(incendiary); + } + } + + public static void setGravity(Entity entity, JsonObject jsonObject) { + if (jsonObject.has("gravity")) { + boolean incendiary = jsonObject.getAsJsonPrimitive("gravity").getAsBoolean(); + entity.setGravity(incendiary); + } + } + + public static void setYield(Explosive explosive, JsonObject jsonObject) { + if (jsonObject.has("yield")) { + float yield = jsonObject.getAsJsonPrimitive("yield").getAsFloat(); + explosive.setYield(yield); + } + } + + public static void setIncendiary(Explosive explosive, JsonObject jsonObject) { + if (jsonObject.has("incendiary")) { + boolean incendiary = jsonObject.getAsJsonPrimitive("incendiary").getAsBoolean(); + explosive.setIsIncendiary(incendiary); + } + } + + 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); + } + } + +} -- 2.39.2 From e8d3974b81944b28ddc11a756cabd1c9c87ded5a Mon Sep 17 00:00:00 2001 From: jojo Date: Sat, 31 Oct 2020 17:59:41 +0100 Subject: [PATCH 04/31] Add Support Item softcoded --- src/de/steamwar/misslewars/MissileWars.java | 6 +- src/de/steamwar/misslewars/items/Arrows.java | 39 --------- .../misslewars/items/ExplodingArrows.java | 40 --------- .../steamwar/misslewars/items/Fireball.java | 47 ----------- src/de/steamwar/misslewars/items/Item.java | 83 +++++++++++++++++++ .../steamwar/misslewars/items/LandingPad.java | 79 ------------------ src/de/steamwar/misslewars/items/Mine.java | 39 --------- src/de/steamwar/misslewars/items/Shield.java | 39 --------- .../misslewars/items/SpecialItem.java | 33 ++++++++ .../misslewars/listener/ItemListener.java | 51 ++---------- .../misslewars/scripts/RunnableScript.java | 5 +- .../misslewars/scripts/ScriptedItem.java | 17 ++-- .../scripts/implemented/FilterScript.java | 2 +- .../scripts/implemented/LaunchScript.java | 2 +- .../scripts/implemented/PasteScript.java | 2 +- .../scripts/implemented/SummonScript.java | 9 +- 16 files changed, 145 insertions(+), 348 deletions(-) delete mode 100644 src/de/steamwar/misslewars/items/Arrows.java delete mode 100644 src/de/steamwar/misslewars/items/ExplodingArrows.java delete mode 100644 src/de/steamwar/misslewars/items/Fireball.java create mode 100644 src/de/steamwar/misslewars/items/Item.java delete mode 100644 src/de/steamwar/misslewars/items/LandingPad.java delete mode 100644 src/de/steamwar/misslewars/items/Mine.java delete mode 100644 src/de/steamwar/misslewars/items/Shield.java diff --git a/src/de/steamwar/misslewars/MissileWars.java b/src/de/steamwar/misslewars/MissileWars.java index 9cf4913..bd1c699 100644 --- a/src/de/steamwar/misslewars/MissileWars.java +++ b/src/de/steamwar/misslewars/MissileWars.java @@ -71,11 +71,7 @@ public class MissileWars extends JavaPlugin { FightScoreboard.init(); Missile.init(); - new Arrows(); - new Fireball(); - new Shield(); - new Mine(); - new LandingPad(); + Item.init(); StateDependent.setupState(fightState); } diff --git a/src/de/steamwar/misslewars/items/Arrows.java b/src/de/steamwar/misslewars/items/Arrows.java deleted file mode 100644 index 5b3bdfc..0000000 --- a/src/de/steamwar/misslewars/items/Arrows.java +++ /dev/null @@ -1,39 +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.items; - -import org.bukkit.Material; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; - -public class Arrows extends SpecialItem { - - private final ItemStack item = createItem(Material.ARROW, "§ePfeil", 3); - - @Override - public ItemStack getItem() { - return item; - } - - @Override - public boolean handleUse(Player p) { - return false; - } -} diff --git a/src/de/steamwar/misslewars/items/ExplodingArrows.java b/src/de/steamwar/misslewars/items/ExplodingArrows.java deleted file mode 100644 index ebabcfa..0000000 --- a/src/de/steamwar/misslewars/items/ExplodingArrows.java +++ /dev/null @@ -1,40 +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.items; - -import org.bukkit.Material; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; - -public class ExplodingArrows extends SpecialItem { - - private final ItemStack item = createItem(Material.SPECTRAL_ARROW, "§eExplodierender Pfeil", 1); - - @Override - public ItemStack getItem() { - return item; - } - - @Override - public boolean handleUse(Player p) { - return false; - } - -} diff --git a/src/de/steamwar/misslewars/items/Fireball.java b/src/de/steamwar/misslewars/items/Fireball.java deleted file mode 100644 index 2fb7d75..0000000 --- a/src/de/steamwar/misslewars/items/Fireball.java +++ /dev/null @@ -1,47 +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.items; - -import org.bukkit.Material; -import org.bukkit.Sound; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; - -public class Fireball extends SpecialItem { - - private final ItemStack item = createItem(Material.FIRE_CHARGE, "§eFeuerball", 1); - - @Override - public ItemStack getItem() { - return item; - } - - @Override - public boolean handleUse(Player p) { - org.bukkit.entity.Fireball fb = p.launchProjectile(org.bukkit.entity.Fireball.class); - fb.setVelocity(fb.getVelocity().multiply(2)); - fb.setDirection(p.getLocation().getDirection()); - p.playSound(p.getLocation(), Sound.ITEM_FIRECHARGE_USE, 100, 1); - fb.setIsIncendiary(true); - fb.setBounce(false); - fb.setYield(3f); - return true; - } -} diff --git a/src/de/steamwar/misslewars/items/Item.java b/src/de/steamwar/misslewars/items/Item.java new file mode 100644 index 0000000..513913a --- /dev/null +++ b/src/de/steamwar/misslewars/items/Item.java @@ -0,0 +1,83 @@ +/* + 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.items; + +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.google.gson.JsonSyntaxException; +import de.steamwar.misslewars.MissileWars; +import de.steamwar.misslewars.scripts.ScriptedItem; +import org.bukkit.Location; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.Objects; + +public class Item extends SpecialItem { + + private ScriptedItem scriptedItem; + + public Item(File item) { + try { + JsonObject jsonObject = new JsonParser().parse(new FileReader(item)).getAsJsonObject(); + scriptedItem = new ScriptedItem(jsonObject); + } catch (JsonSyntaxException e) { + throw new SecurityException("Item JSON error"); + } catch (IOException e) { + throw new SecurityException("Corrupt Item"); + } + } + + @Override + public ItemStack getItem() { + return scriptedItem.getItemStack(); + } + + @Override + public boolean handleUse(Player p) { + return scriptedItem.execute(ScriptedItem.EventType.onClick, p, p.getLocation()); + } + + @Override + public void handleThrow(Entity e) { + scriptedItem.execute(ScriptedItem.EventType.onThrow, e, e.getLocation()); + } + + @Override + public void handleHit(Entity e, Location l) { + scriptedItem.execute(ScriptedItem.EventType.onHit, e, l); + } + + public static void init() { + File itemsFolder = new File(MissileWars.getPlugin().getDataFolder(), "items"); + if (!itemsFolder.exists() || !itemsFolder.canRead() || !itemsFolder.isDirectory()) { + throw new SecurityException("Items could not be loaded"); + } + for (File itemFile : Objects.requireNonNull(itemsFolder.listFiles())) { + if (!itemFile.canRead() || !itemFile.isFile()) continue; + new Item(itemFile); + } + } + +} diff --git a/src/de/steamwar/misslewars/items/LandingPad.java b/src/de/steamwar/misslewars/items/LandingPad.java deleted file mode 100644 index db7e781..0000000 --- a/src/de/steamwar/misslewars/items/LandingPad.java +++ /dev/null @@ -1,79 +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.items; - -import com.sk89q.worldedit.EditSession; -import com.sk89q.worldedit.WorldEdit; -import com.sk89q.worldedit.bukkit.BukkitWorld; -import com.sk89q.worldedit.extent.clipboard.Clipboard; -import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats; -import com.sk89q.worldedit.function.operation.Operations; -import com.sk89q.worldedit.math.BlockVector3; -import com.sk89q.worldedit.session.ClipboardHolder; -import com.sk89q.worldedit.world.World; -import de.steamwar.misslewars.MissileWars; -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; -import org.bukkit.potion.PotionEffect; -import org.bukkit.potion.PotionEffectType; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.util.Objects; - -public class LandingPad extends SpecialItem { - - private final ItemStack item = createItem(Material.SLIME_BALL, "§aLanding Pad", 1); - private static final World world = new BukkitWorld(Bukkit.getWorlds().get(0)); - private static final File landingPad = new File(MissileWars.getPlugin().getDataFolder(), "landingPad.schem"); - private final Clipboard clipboard; - private final BlockVector3 offset; - - { - try { - clipboard = Objects.requireNonNull(ClipboardFormats.findByFile(landingPad)).getReader(new FileInputStream(landingPad)).read(); - } catch (IOException e) { - throw new SecurityException("Could not load landingPad", e); - } - - offset = clipboard.getRegion().getMinimumPoint().subtract(clipboard.getOrigin()).add(clipboard.getDimensions().divide(2)); - } - - @Override - public ItemStack getItem() { - return item; - } - - @Override - public boolean handleUse(Player p) { - p.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_FALLING, 2, 1, false, false, false)); - Location l = p.getLocation(); - BlockVector3 paste = BlockVector3.at(l.getX(), l.getY() - 5, l.getZ()).subtract(offset); - - EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(world, -1); - Operations.completeBlindly(new ClipboardHolder(clipboard).createPaste(editSession).ignoreAirBlocks(true).to(paste).build()); - editSession.flushSession(); - return true; - } -} diff --git a/src/de/steamwar/misslewars/items/Mine.java b/src/de/steamwar/misslewars/items/Mine.java deleted file mode 100644 index f87d9a1..0000000 --- a/src/de/steamwar/misslewars/items/Mine.java +++ /dev/null @@ -1,39 +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.items; - -import org.bukkit.Material; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; - -public class Mine extends SpecialItem { - - private final ItemStack item = createItem(Material.EGG, "§eMine", 1); - - @Override - public ItemStack getItem() { - return item; - } - - @Override - public boolean handleUse(Player p) { - return false; - } -} diff --git a/src/de/steamwar/misslewars/items/Shield.java b/src/de/steamwar/misslewars/items/Shield.java deleted file mode 100644 index b954660..0000000 --- a/src/de/steamwar/misslewars/items/Shield.java +++ /dev/null @@ -1,39 +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.items; - -import org.bukkit.Material; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; - -public class Shield extends SpecialItem { - - private final ItemStack item = createItem(Material.SNOWBALL, "§aSchild", 1); - - @Override - public ItemStack getItem() { - return item; - } - - @Override - public boolean handleUse(Player p) { - return false; - } -} diff --git a/src/de/steamwar/misslewars/items/SpecialItem.java b/src/de/steamwar/misslewars/items/SpecialItem.java index d32fdae..24ad493 100644 --- a/src/de/steamwar/misslewars/items/SpecialItem.java +++ b/src/de/steamwar/misslewars/items/SpecialItem.java @@ -20,8 +20,12 @@ package de.steamwar.misslewars.items; import de.steamwar.misslewars.Config; +import org.bukkit.Location; import org.bukkit.Material; +import org.bukkit.entity.Entity; import org.bukkit.entity.Player; +import org.bukkit.event.entity.ProjectileHitEvent; +import org.bukkit.event.entity.ProjectileLaunchEvent; import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; @@ -41,12 +45,16 @@ public abstract class SpecialItem { if (this.isMissile()) { missileItems.add(this); } else { + materialName = getItem().getType().name().toLowerCase(); supportItems.add(this); } } + private String materialName = ""; public abstract ItemStack getItem(); public abstract boolean handleUse(Player p); + public void handleThrow(Entity e) {} + public void handleHit(Entity e, Location l) {} public boolean isMissile() { return false; } @@ -77,6 +85,31 @@ public abstract class SpecialItem { return false; } + public static void handleThrow(ProjectileLaunchEvent e) { + String name = e.getEntity().getClass().getName().toLowerCase(); + for (SpecialItem specialItem : supportItems) { + if (name.contains(specialItem.materialName)) { + specialItem.handleThrow(e.getEntity()); + } + } + } + + public static void handleHit(ProjectileHitEvent e) { + String name = e.getEntity().getClass().getName().toLowerCase(); + Location location = null; + if (e.getHitEntity() != null) { + location = e.getHitEntity().getLocation(); + } else if (e.getHitBlock() != null) { + location = e.getHitBlock().getLocation(); + } + if (location == null) return; + for (SpecialItem specialItem : supportItems) { + if (name.contains(specialItem.materialName)) { + specialItem.handleHit(e.getEntity(), location); + } + } + } + public static ItemStack getRandomItem() { if (random.nextDouble() > Config.MissileChance) { return supportItems.get(random.nextInt(supportItems.size())).getItem(); diff --git a/src/de/steamwar/misslewars/listener/ItemListener.java b/src/de/steamwar/misslewars/listener/ItemListener.java index bb9032a..0545df2 100644 --- a/src/de/steamwar/misslewars/listener/ItemListener.java +++ b/src/de/steamwar/misslewars/listener/ItemListener.java @@ -38,6 +38,7 @@ import org.bukkit.entity.Egg; import org.bukkit.entity.Snowball; import org.bukkit.event.EventHandler; import org.bukkit.event.block.Action; +import org.bukkit.event.entity.ProjectileHitEvent; import org.bukkit.event.entity.ProjectileLaunchEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.ItemStack; @@ -86,10 +87,10 @@ public class ItemListener extends BasicListener { if (item == null) return; - if(e.getAction() != Action.RIGHT_CLICK_BLOCK && e.getAction() != Action.RIGHT_CLICK_AIR) + if (e.getAction() != Action.RIGHT_CLICK_BLOCK && e.getAction() != Action.RIGHT_CLICK_AIR) return; - if(SpecialItem.handleUse(item, e.getPlayer())){ + if (SpecialItem.handleUse(item, e.getPlayer())){ item.setAmount(item.getAmount()-1); e.getPlayer().updateInventory(); e.setCancelled(true); @@ -98,50 +99,12 @@ public class ItemListener extends BasicListener { @EventHandler public void onThrow(ProjectileLaunchEvent e) { - if (e.getEntity() instanceof Snowball) { - Bukkit.getScheduler().runTaskLater(MissileWars.getPlugin(), () -> { - Location l = e.getEntity().getLocation(); - if (!validSpawn(l)) return; - BlockVector3 paste = BlockVector3.at(l.getX(), l.getY(), l.getZ()).subtract(offsetShield); - - EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(world, -1); - Operations.completeBlindly(new ClipboardHolder(clipboardShield).createPaste(editSession).ignoreAirBlocks(true).to(paste).build()); - editSession.flushSession(); - }, Config.ShieldFlyTime); - } - if (e.getEntity() instanceof Egg) { - Bukkit.getScheduler().runTaskLater(MissileWars.getPlugin(), () -> { - Location l = e.getEntity().getLocation(); - if (!validSpawn(l)) return; - BlockVector3 paste = BlockVector3.at(l.getX(), l.getY(), l.getZ()).subtract(offsetMine); - - EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(world, -1); - Operations.completeBlindly(new ClipboardHolder(clipboardMine).createPaste(editSession).ignoreAirBlocks(true).to(paste).build()); - editSession.flushSession(); - }, Config.ShieldFlyTime); - } + SpecialItem.handleThrow(e); } - private boolean validSpawn(Location location) { - int bz = MissileWars.getBlueTeam().getPortalZ(); - int rz = MissileWars.getRedTeam().getPortalZ(); - - int offset = sign(bz - rz) * 5; - - int blockZ = location.getBlockZ(); - if (offset > 0) { - if (blockZ > bz - offset) return false; - if (blockZ < rz + offset) return false; - } else { - if (blockZ < bz - offset) return false; - if (blockZ > rz + offset) return false; - } - return true; - } - - private int sign(int i) { - if (i < 0) return -1; - return i > 0 ? 1 : 0; + @EventHandler + public void onHit(ProjectileHitEvent e) { + SpecialItem.handleHit(e); } } diff --git a/src/de/steamwar/misslewars/scripts/RunnableScript.java b/src/de/steamwar/misslewars/scripts/RunnableScript.java index 1ae8fd2..ca4e4ee 100644 --- a/src/de/steamwar/misslewars/scripts/RunnableScript.java +++ b/src/de/steamwar/misslewars/scripts/RunnableScript.java @@ -19,6 +19,7 @@ package de.steamwar.misslewars.scripts; +import org.bukkit.Location; import org.bukkit.entity.Entity; public interface RunnableScript { @@ -27,10 +28,12 @@ public interface RunnableScript { public final ScriptedItem.EventType eventType; public final Entity entity; + public final Location location; - public RunnableScriptEvent(ScriptedItem.EventType eventType, Entity entity) { + public RunnableScriptEvent(ScriptedItem.EventType eventType, Entity entity, Location location) { this.eventType = eventType; this.entity = entity; + this.location = location; } } diff --git a/src/de/steamwar/misslewars/scripts/ScriptedItem.java b/src/de/steamwar/misslewars/scripts/ScriptedItem.java index c256c04..6c13ac6 100644 --- a/src/de/steamwar/misslewars/scripts/ScriptedItem.java +++ b/src/de/steamwar/misslewars/scripts/ScriptedItem.java @@ -21,15 +21,13 @@ package de.steamwar.misslewars.scripts; import com.google.gson.JsonArray; import com.google.gson.JsonObject; +import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; public class ScriptedItem { @@ -94,9 +92,14 @@ public class ScriptedItem { return itemStack; } - public void execute(EventType eventType, Entity entity) { - if (!scriptMap.containsKey(eventType)) return; - scriptMap.get(eventType).execute(new RunnableScript.RunnableScriptEvent(eventType, entity)); + 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)); + return true; + } + + public ItemStack getItemStack() { + return itemStack; } } diff --git a/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java b/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java index 50246d5..f0a80a7 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java @@ -39,7 +39,7 @@ public class FilterScript implements RunnableScript { switch (filter.getAsJsonPrimitive("filter").getAsString().toLowerCase()) { case "nearportal": this.filter = runnableScriptEvent -> { - Location location = runnableScriptEvent.entity.getLocation(); + Location location = runnableScriptEvent.location; int bz = MissileWars.getBlueTeam().getPortalZ(); int rz = MissileWars.getRedTeam().getPortalZ(); diff --git a/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java b/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java index 1af6fe4..3215f30 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java @@ -50,7 +50,7 @@ public class LaunchScript implements RunnableScript { setGlowing(fireball, launch); setGravity(fireball, launch); - fireball.setDirection(player.getLocation().getDirection()); + fireball.setDirection(runnableScriptEvent.location.getDirection()); }; break; case "arrow": diff --git a/src/de/steamwar/misslewars/scripts/implemented/PasteScript.java b/src/de/steamwar/misslewars/scripts/implemented/PasteScript.java index 742d0a4..92d7fa7 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/PasteScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/PasteScript.java @@ -85,7 +85,7 @@ public class PasteScript implements RunnableScript { @Override public boolean execute(RunnableScriptEvent runnableScriptEvent) { - Location location = runnableScriptEvent.entity.getLocation(); + Location location = runnableScriptEvent.location; BlockVector3 paste = BlockVector3.at(location.getX() + xOffset, location.getY() + yOffset, location.getZ() + zOffset); if (centered) { paste = paste.subtract(centeredOffset); diff --git a/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java b/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java index 7af36af..c650ce5 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java @@ -21,7 +21,6 @@ package de.steamwar.misslewars.scripts.implemented; import com.google.gson.JsonObject; import de.steamwar.misslewars.scripts.RunnableScript; -import org.bukkit.entity.Entity; import org.bukkit.entity.TNTPrimed; import static de.steamwar.misslewars.scripts.utils.EntityUtils.*; @@ -30,7 +29,7 @@ public class SummonScript implements RunnableScript { private interface Summon { - void summon(Entity entity); + void summon(RunnableScriptEvent event); } @@ -39,8 +38,8 @@ public class SummonScript implements RunnableScript { public SummonScript(JsonObject summon) { switch (summon.getAsJsonPrimitive("entity").getAsString().toLowerCase()) { case "tntprimed": - this.summon = entity -> { - TNTPrimed tnt = entity.getWorld().spawn(entity.getLocation(), TNTPrimed.class); + this.summon = runnableScriptEvent -> { + TNTPrimed tnt = runnableScriptEvent.entity.getWorld().spawn(runnableScriptEvent.location, TNTPrimed.class); setYield(tnt, summon); setIncendiary(tnt, summon); @@ -55,7 +54,7 @@ public class SummonScript implements RunnableScript { @Override public boolean execute(RunnableScriptEvent runnableScriptEvent) { if (summon == null) return false; - summon.summon(runnableScriptEvent.entity); + summon.summon(runnableScriptEvent); return true; } -- 2.39.2 From 6f1ab2be8b1e423bb4e2f65a364241e0d24492c6 Mon Sep 17 00:00:00 2001 From: jojo Date: Sat, 31 Oct 2020 21:57:03 +0100 Subject: [PATCH 05/31] Add Support Item softcoded --- src/de/steamwar/misslewars/MissileWars.java | 1 - src/de/steamwar/misslewars/items/Item.java | 7 ++ .../misslewars/items/SpecialItem.java | 10 ++- src/de/steamwar/misslewars/items/arrows.json | 68 ------------------- .../misslewars/listener/ArrowListener.java | 64 ----------------- .../misslewars/listener/ItemListener.java | 45 ------------ .../misslewars/scripts/RunnableScript.java | 10 ++- .../steamwar/misslewars/scripts/Script.java | 51 +++++++++++--- .../misslewars/scripts/ScriptParser.java | 3 +- .../misslewars/scripts/ScriptedItem.java | 9 +++ .../scripts/implemented/DelayScript.java | 11 ++- .../scripts/implemented/FilterScript.java | 12 ++-- .../scripts/implemented/LaunchScript.java | 2 +- .../scripts/implemented/PasteScript.java | 2 +- .../scripts/implemented/RemoveScript.java | 20 ++++++ .../scripts/implemented/SummonScript.java | 2 +- 16 files changed, 111 insertions(+), 206 deletions(-) delete mode 100644 src/de/steamwar/misslewars/items/arrows.json delete mode 100644 src/de/steamwar/misslewars/listener/ArrowListener.java create mode 100644 src/de/steamwar/misslewars/scripts/implemented/RemoveScript.java diff --git a/src/de/steamwar/misslewars/MissileWars.java b/src/de/steamwar/misslewars/MissileWars.java index bd1c699..1cb7d32 100644 --- a/src/de/steamwar/misslewars/MissileWars.java +++ b/src/de/steamwar/misslewars/MissileWars.java @@ -60,7 +60,6 @@ public class MissileWars extends JavaPlugin { new PortalDestructListener(); new WaitingListener(); new FightListener(); - new ArrowListener(); new ChatListener(); getCommand("spectate").setExecutor(new CommandSpectate()); diff --git a/src/de/steamwar/misslewars/items/Item.java b/src/de/steamwar/misslewars/items/Item.java index 513913a..f142177 100644 --- a/src/de/steamwar/misslewars/items/Item.java +++ b/src/de/steamwar/misslewars/items/Item.java @@ -41,10 +41,13 @@ public class Item extends SpecialItem { 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"); } } @@ -80,4 +83,8 @@ public class Item extends SpecialItem { } } + public ScriptedItem getScriptedItem() { + return scriptedItem; + } + } diff --git a/src/de/steamwar/misslewars/items/SpecialItem.java b/src/de/steamwar/misslewars/items/SpecialItem.java index 24ad493..80e511f 100644 --- a/src/de/steamwar/misslewars/items/SpecialItem.java +++ b/src/de/steamwar/misslewars/items/SpecialItem.java @@ -45,12 +45,11 @@ public abstract class SpecialItem { if (this.isMissile()) { missileItems.add(this); } else { - materialName = getItem().getType().name().toLowerCase(); supportItems.add(this); } } - private String materialName = ""; + private String materialName = null; public abstract ItemStack getItem(); public abstract boolean handleUse(Player p); public void handleThrow(Entity e) {} @@ -88,6 +87,9 @@ public abstract class SpecialItem { public static void handleThrow(ProjectileLaunchEvent e) { String name = e.getEntity().getClass().getName().toLowerCase(); for (SpecialItem specialItem : supportItems) { + if (specialItem.materialName == null) { + specialItem.materialName = specialItem.getItem().getType().name().toLowerCase(); + } if (name.contains(specialItem.materialName)) { specialItem.handleThrow(e.getEntity()); } @@ -96,6 +98,7 @@ public abstract class SpecialItem { public static void handleHit(ProjectileHitEvent e) { String name = e.getEntity().getClass().getName().toLowerCase(); + Location location = null; if (e.getHitEntity() != null) { location = e.getHitEntity().getLocation(); @@ -103,8 +106,9 @@ public abstract class SpecialItem { location = e.getHitBlock().getLocation(); } if (location == null) return; + for (SpecialItem specialItem : supportItems) { - if (name.contains(specialItem.materialName)) { + if (name.contains(((Item) specialItem).getScriptedItem().getEntityName())) { specialItem.handleHit(e.getEntity(), location); } } diff --git a/src/de/steamwar/misslewars/items/arrows.json b/src/de/steamwar/misslewars/items/arrows.json deleted file mode 100644 index 69c6246..0000000 --- a/src/de/steamwar/misslewars/items/arrows.json +++ /dev/null @@ -1,68 +0,0 @@ -[ - { - "type": "ARROW", - "name": "§ePfeil", - "lore": [], - "amount": 3, - "EVENT.onThrow": [ - - ], - "EVENT.onHit": [ - - ], - "EVENT.onClick": [ - - ] - }, - { - "type": "SPECTRAL_ARROW", - "name": "§eExplodierender Pfeil", - "lore": [], - "amount": 1, - "EVENT.onHit": [ - {"type": "summon", "entity": "TNTPrimed", "incendiary": false, "fuse": 0, "yield": 2.0} - ] - }, - { - "type": "FIRE_CHARGE", - "name": "§eFeuerball", - "lore": [], - "amount": 1, - "EVENT.onClick": [ - {"type": "launch", "entity": "Fireball", "incendiary": false, "bounce": false, "yield": 3.0, "velocity": 2}, - {"type": "sound", "sound": "ITEM_FIRECHARGE_USE", "volume": 100, "pitch": 1} - ] - }, - { - "type": "SLIME_BALL", - "name": "§aLanding Pad", - "lore": [], - "amount": 1, - "EVENT.onClick": [ - {"type": "potion", "potion": "SLOW_FALLING", "duration": 2, "amplifier": 1, "ambient": false, "particles": false, "icon": false}, - {"type": "paste", "schem": "landingPad.schem", "centered": true, "offset": [0, -5, 0], "ignoreAir": true} - ] - }, - { - "type": "EGG", - "name": "§eMine", - "lore": [], - "amount": 1, - "EVENT.onThrow": [ - {"type": "delay", "time": "CONFIG.MineFlyTime"}, - {"type": "filter", "filter": "nearPortal", "invert": true}, - {"type": "paste", "schem": "mine.schem", "centered": true, "offset": [0, 0, 0], "ignoreAir": true} - ] - }, - { - "type": "SNOWBALL", - "name": "§aSchild", - "lore": [], - "amount": 1, - "EVENT.onThrow": [ - {"type": "delay", "time": "CONFIG.ShieldFlyTime"}, - {"type": "filter", "filter": "nearPortal", "invert": true}, - {"type": "paste", "schem": "shield.schem", "centered": true, "offset": [0, 0, 0], "ignoreAir": true} - ] - } -] \ No newline at end of file diff --git a/src/de/steamwar/misslewars/listener/ArrowListener.java b/src/de/steamwar/misslewars/listener/ArrowListener.java deleted file mode 100644 index 6d5fcf0..0000000 --- a/src/de/steamwar/misslewars/listener/ArrowListener.java +++ /dev/null @@ -1,64 +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.listener; - -import de.steamwar.misslewars.FightState; -import org.bukkit.Location; -import org.bukkit.entity.EntityType; -import org.bukkit.entity.Projectile; -import org.bukkit.entity.TNTPrimed; -import org.bukkit.event.EventHandler; -import org.bukkit.event.entity.ProjectileHitEvent; - -import java.util.EnumSet; - -public class ArrowListener extends BasicListener { - - public ArrowListener() { - super(EnumSet.of(FightState.FIGHTING)); - } - - @EventHandler - public void onHit(ProjectileHitEvent e) { - Projectile projectile = e.getEntity(); - if (projectile.getType() != EntityType.SPECTRAL_ARROW) { - return; - } - - Location location = getLocation(e); - if (location == null) return; - - TNTPrimed tnt = e.getEntity().getWorld().spawn(location, TNTPrimed.class); - tnt.setYield(2.0F); - tnt.setIsIncendiary(false); - tnt.setFuseTicks(0); - } - - private Location getLocation(ProjectileHitEvent e) { - if (e.getHitEntity() != null) { - return e.getHitEntity().getLocation(); - } - if (e.getHitBlock() != null) { - return e.getHitBlock().getLocation(); - } - return null; - } - -} diff --git a/src/de/steamwar/misslewars/listener/ItemListener.java b/src/de/steamwar/misslewars/listener/ItemListener.java index 0545df2..6d78e12 100644 --- a/src/de/steamwar/misslewars/listener/ItemListener.java +++ b/src/de/steamwar/misslewars/listener/ItemListener.java @@ -19,23 +19,8 @@ package de.steamwar.misslewars.listener; -import com.sk89q.worldedit.EditSession; -import com.sk89q.worldedit.WorldEdit; -import com.sk89q.worldedit.bukkit.BukkitWorld; -import com.sk89q.worldedit.extent.clipboard.Clipboard; -import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats; -import com.sk89q.worldedit.function.operation.Operations; -import com.sk89q.worldedit.math.BlockVector3; -import com.sk89q.worldedit.session.ClipboardHolder; -import com.sk89q.worldedit.world.World; -import de.steamwar.misslewars.Config; import de.steamwar.misslewars.FightState; -import de.steamwar.misslewars.MissileWars; import de.steamwar.misslewars.items.SpecialItem; -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.entity.Egg; -import org.bukkit.entity.Snowball; import org.bukkit.event.EventHandler; import org.bukkit.event.block.Action; import org.bukkit.event.entity.ProjectileHitEvent; @@ -43,40 +28,10 @@ import org.bukkit.event.entity.ProjectileLaunchEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.ItemStack; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; import java.util.EnumSet; -import java.util.Objects; public class ItemListener extends BasicListener { - private static final File shield = new File(MissileWars.getPlugin().getDataFolder(), "shield.schem"); - private static final File mine = new File(MissileWars.getPlugin().getDataFolder(), "mine.schem"); - private static final World world = new BukkitWorld(Bukkit.getWorlds().get(0)); - private static final Clipboard clipboardShield; - private static final BlockVector3 offsetShield; - - private static final Clipboard clipboardMine; - private static final BlockVector3 offsetMine; - - static { - try { - clipboardShield = Objects.requireNonNull(ClipboardFormats.findByFile(shield)).getReader(new FileInputStream(shield)).read(); - } catch (IOException e) { - throw new SecurityException("Could not load shield", e); - } - - try { - clipboardMine = Objects.requireNonNull(ClipboardFormats.findByFile(mine)).getReader(new FileInputStream(mine)).read(); - } catch (IOException e) { - throw new SecurityException("Could not load mine", e); - } - - offsetShield = clipboardShield.getRegion().getMinimumPoint().subtract(clipboardShield.getOrigin()).add(clipboardShield.getDimensions().divide(2)); - offsetMine = clipboardMine.getRegion().getMinimumPoint().subtract(clipboardMine.getOrigin()).add(clipboardMine.getDimensions().divide(2)); - } - public ItemListener() { super(EnumSet.of(FightState.FIGHTING)); } diff --git a/src/de/steamwar/misslewars/scripts/RunnableScript.java b/src/de/steamwar/misslewars/scripts/RunnableScript.java index ca4e4ee..3d72337 100644 --- a/src/de/steamwar/misslewars/scripts/RunnableScript.java +++ b/src/de/steamwar/misslewars/scripts/RunnableScript.java @@ -28,7 +28,7 @@ public interface RunnableScript { public final ScriptedItem.EventType eventType; public final Entity entity; - public final Location location; + private final Location location; public RunnableScriptEvent(ScriptedItem.EventType eventType, Entity entity, Location location) { this.eventType = eventType; @@ -36,6 +36,14 @@ public interface RunnableScript { this.location = location; } + public Location getLocation() { + if (eventType == ScriptedItem.EventType.onClick) return location; + if (entity != null) { + return entity.getLocation(); + } + return location; + } + } boolean execute(RunnableScriptEvent runnableScriptEvent); diff --git a/src/de/steamwar/misslewars/scripts/Script.java b/src/de/steamwar/misslewars/scripts/Script.java index b4a9136..c68abb1 100644 --- a/src/de/steamwar/misslewars/scripts/Script.java +++ b/src/de/steamwar/misslewars/scripts/Script.java @@ -20,8 +20,8 @@ package de.steamwar.misslewars.scripts; import de.steamwar.misslewars.MissileWars; +import de.steamwar.misslewars.scripts.implemented.DelayScript; import org.bukkit.Bukkit; -import org.bukkit.entity.Player; import java.util.ArrayList; import java.util.List; @@ -30,20 +30,55 @@ public class Script { private List runnableScriptList = new ArrayList<>(); - public void execute(RunnableScript.RunnableScriptEvent runnableScriptEvent) { - Bukkit.getScheduler().runTaskLater(MissileWars.getPlugin(), () -> { - int i = 0; - while (i < runnableScriptList.size()) { - if (!runnableScriptList.get(i).execute(runnableScriptEvent)) { + private class ScriptExecutor { + + private int index = 0; + + private final RunnableScript.RunnableScriptEvent runnableScriptEvent; + + public ScriptExecutor(RunnableScript.RunnableScriptEvent runnableScriptEvent) { + this.runnableScriptEvent = runnableScriptEvent; + } + + public void start() { + resume(); + } + + private void resume() { + while (index < runnableScriptList.size()) { + RunnableScript runnableScript = runnableScriptList.get(index); + if (runnableScript instanceof DelayScript) { + index++; + resumeLater(((DelayScript) runnableScript).getDelayTime()); break; } - i++; + if (!runnableScript.execute(runnableScriptEvent)) { + index = runnableScriptList.size(); + return; + } + index++; } - }, 0); + } + + private void resumeLater(int delayTime) { + Bukkit.getScheduler().runTaskLater(MissileWars.getPlugin(), this::resume, delayTime); + } + + } + + public void execute(RunnableScript.RunnableScriptEvent runnableScriptEvent) { + new ScriptExecutor(runnableScriptEvent).start(); } public void add(RunnableScript runnableScript) { runnableScriptList.add(runnableScript); } + @Override + public String toString() { + return "Script{" + + "runnableScriptList=" + runnableScriptList + + '}'; + } + } diff --git a/src/de/steamwar/misslewars/scripts/ScriptParser.java b/src/de/steamwar/misslewars/scripts/ScriptParser.java index 346fb77..67c7a47 100644 --- a/src/de/steamwar/misslewars/scripts/ScriptParser.java +++ b/src/de/steamwar/misslewars/scripts/ScriptParser.java @@ -28,7 +28,6 @@ public class ScriptParser { public static Script parseScript(JsonArray jsonArray) { Script script = new Script(); jsonArray.forEach(jsonElement -> { - if (!jsonArray.isJsonObject()) return; RunnableScript runnableScript = parseScriptSnippet((JsonObject) jsonElement); if (runnableScript == null) return; script.add(runnableScript); @@ -46,6 +45,8 @@ public class ScriptParser { return new DelayScript(jsonObject); case "filter": return new FilterScript(jsonObject); + case "remove": + return new RemoveScript(jsonObject); case "launch": return new LaunchScript(jsonObject); case "paste": diff --git a/src/de/steamwar/misslewars/scripts/ScriptedItem.java b/src/de/steamwar/misslewars/scripts/ScriptedItem.java index 6c13ac6..b98fefb 100644 --- a/src/de/steamwar/misslewars/scripts/ScriptedItem.java +++ b/src/de/steamwar/misslewars/scripts/ScriptedItem.java @@ -53,12 +53,17 @@ public class ScriptedItem { } private Map scriptMap = new HashMap<>(); + private String entityName = "---"; private ItemStack itemStack; public ScriptedItem(JsonObject jsonObject) { itemStack = createItemStack(jsonObject); + if (jsonObject.has("entityName")) { + entityName = jsonObject.get("entityName").getAsString(); + } + for (EventType eventType : EventType.values()) { String eventString = "EVENT." + eventType.name(); if (!jsonObject.has(eventString)) continue; @@ -102,4 +107,8 @@ public class ScriptedItem { return itemStack; } + public String getEntityName() { + return entityName; + } + } diff --git a/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java b/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java index 137dba4..60247eb 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java @@ -33,7 +33,7 @@ public class DelayScript implements RunnableScript { if (jsonPrimitive.isString()) { switch (jsonPrimitive.getAsString().toLowerCase()) { case "config.mineflytime": - case "config.shiledflytime": + case "config.shieldflytime": delayTime = Config.ShieldFlyTime; break; case "config.endtime": @@ -62,12 +62,11 @@ public class DelayScript implements RunnableScript { @Override public boolean execute(RunnableScriptEvent runnableScriptEvent) { - try { - Thread.sleep(delayTime); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } return true; } + public int getDelayTime() { + return delayTime; + } + } diff --git a/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java b/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java index f0a80a7..ce1e88b 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java @@ -39,7 +39,7 @@ public class FilterScript implements RunnableScript { switch (filter.getAsJsonPrimitive("filter").getAsString().toLowerCase()) { case "nearportal": this.filter = runnableScriptEvent -> { - Location location = runnableScriptEvent.location; + Location location = runnableScriptEvent.getLocation(); int bz = MissileWars.getBlueTeam().getPortalZ(); int rz = MissileWars.getRedTeam().getPortalZ(); @@ -47,13 +47,13 @@ public class FilterScript implements RunnableScript { int blockZ = location.getBlockZ(); if (offset > 0) { - if (blockZ > bz - offset) return false; - if (blockZ < rz + offset) return false; + if (blockZ > bz - offset) return true; + if (blockZ < rz + offset) return true; } else { - if (blockZ < bz - offset) return false; - if (blockZ > rz + offset) return false; + if (blockZ < bz - offset) return true; + if (blockZ > rz + offset) return true; } - return true; + return false; }; break; default: diff --git a/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java b/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java index 3215f30..5a7a943 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java @@ -50,7 +50,7 @@ public class LaunchScript implements RunnableScript { setGlowing(fireball, launch); setGravity(fireball, launch); - fireball.setDirection(runnableScriptEvent.location.getDirection()); + fireball.setDirection(runnableScriptEvent.getLocation().getDirection()); }; break; case "arrow": diff --git a/src/de/steamwar/misslewars/scripts/implemented/PasteScript.java b/src/de/steamwar/misslewars/scripts/implemented/PasteScript.java index 92d7fa7..d303778 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/PasteScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/PasteScript.java @@ -85,7 +85,7 @@ public class PasteScript implements RunnableScript { @Override public boolean execute(RunnableScriptEvent runnableScriptEvent) { - Location location = runnableScriptEvent.location; + Location location = runnableScriptEvent.getLocation(); BlockVector3 paste = BlockVector3.at(location.getX() + xOffset, location.getY() + yOffset, location.getZ() + zOffset); if (centered) { paste = paste.subtract(centeredOffset); diff --git a/src/de/steamwar/misslewars/scripts/implemented/RemoveScript.java b/src/de/steamwar/misslewars/scripts/implemented/RemoveScript.java new file mode 100644 index 0000000..478da9d --- /dev/null +++ b/src/de/steamwar/misslewars/scripts/implemented/RemoveScript.java @@ -0,0 +1,20 @@ +package de.steamwar.misslewars.scripts.implemented; + +import com.google.gson.JsonObject; +import de.steamwar.misslewars.scripts.RunnableScript; +import org.bukkit.entity.Player; + +public class RemoveScript implements RunnableScript { + + public RemoveScript(JsonObject jsonObject) { + + } + + @Override + public boolean execute(RunnableScriptEvent runnableScriptEvent) { + if (runnableScriptEvent.entity instanceof Player) return true; + runnableScriptEvent.entity.remove(); + return true; + } + +} diff --git a/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java b/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java index c650ce5..0b98da7 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java @@ -39,7 +39,7 @@ public class SummonScript implements RunnableScript { switch (summon.getAsJsonPrimitive("entity").getAsString().toLowerCase()) { case "tntprimed": this.summon = runnableScriptEvent -> { - TNTPrimed tnt = runnableScriptEvent.entity.getWorld().spawn(runnableScriptEvent.location, TNTPrimed.class); + TNTPrimed tnt = runnableScriptEvent.entity.getWorld().spawn(runnableScriptEvent.getLocation(), TNTPrimed.class); setYield(tnt, summon); setIncendiary(tnt, summon); -- 2.39.2 From b7ef14f814a5898b1a1e9cd2fd3c9165bb406825 Mon Sep 17 00:00:00 2001 From: jojo Date: Sat, 31 Oct 2020 21:57:27 +0100 Subject: [PATCH 06/31] Add Support Item softcoded --- .../scripts/implemented/RemoveScript.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/de/steamwar/misslewars/scripts/implemented/RemoveScript.java b/src/de/steamwar/misslewars/scripts/implemented/RemoveScript.java index 478da9d..fc5d5a7 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/RemoveScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/RemoveScript.java @@ -1,3 +1,22 @@ +/* + 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.implemented; import com.google.gson.JsonObject; -- 2.39.2 From 090185558fbc6c3160c19689bc56764bb1c4491a Mon Sep 17 00:00:00 2001 From: jojo Date: Sun, 1 Nov 2020 09:35:35 +0100 Subject: [PATCH 07/31] 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; + } + +} -- 2.39.2 From 99b2c644898e171610ca5cb12a8f67a8808cca71 Mon Sep 17 00:00:00 2001 From: jojo Date: Sun, 1 Nov 2020 10:32:08 +0100 Subject: [PATCH 08/31] Fix Design --- src/de/steamwar/misslewars/MissileWars.java | 2 +- .../items/{Item.java => CustomItem.java} | 27 ++++---- .../misslewars/items/SpecialItem.java | 2 +- .../misslewars/scripts/LocationType.java | 10 +++ .../misslewars/scripts/RunnableScript.java | 66 ------------------- .../scripts/RunnableScriptEvent.java | 58 ++++++++++++++++ .../steamwar/misslewars/scripts/Script.java | 6 +- .../misslewars/scripts/ScriptedItem.java | 5 +- .../scripts/implemented/DelayScript.java | 1 + .../scripts/implemented/FilterScript.java | 1 + .../scripts/implemented/LaunchScript.java | 1 + .../scripts/implemented/LocationScript.java | 3 +- .../scripts/implemented/PasteScript.java | 3 +- .../scripts/implemented/PotionScript.java | 1 + .../scripts/implemented/RemoveScript.java | 1 + .../scripts/implemented/SoundScript.java | 1 + .../scripts/implemented/SummonScript.java | 1 + 17 files changed, 98 insertions(+), 91 deletions(-) rename src/de/steamwar/misslewars/items/{Item.java => CustomItem.java} (79%) create mode 100644 src/de/steamwar/misslewars/scripts/LocationType.java create mode 100644 src/de/steamwar/misslewars/scripts/RunnableScriptEvent.java 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.*; -- 2.39.2 From d030763ba16ba0e247c8b99f0a2e5177a2c0fac1 Mon Sep 17 00:00:00 2001 From: jojo Date: Sun, 1 Nov 2020 11:00:14 +0100 Subject: [PATCH 09/31] Fix License Optimize Imports --- .../misslewars/scripts/LocationType.java | 19 +++++++++++++++++++ .../scripts/RunnableScriptEvent.java | 19 +++++++++++++++++++ .../misslewars/scripts/ScriptedItem.java | 6 ++++-- .../scripts/implemented/LocationScript.java | 19 +++++++++++++++++++ 4 files changed, 61 insertions(+), 2 deletions(-) diff --git a/src/de/steamwar/misslewars/scripts/LocationType.java b/src/de/steamwar/misslewars/scripts/LocationType.java index b462650..1ed8325 100644 --- a/src/de/steamwar/misslewars/scripts/LocationType.java +++ b/src/de/steamwar/misslewars/scripts/LocationType.java @@ -1,3 +1,22 @@ +/* + 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; public enum LocationType { diff --git a/src/de/steamwar/misslewars/scripts/RunnableScriptEvent.java b/src/de/steamwar/misslewars/scripts/RunnableScriptEvent.java index fe3eb4d..8190edb 100644 --- a/src/de/steamwar/misslewars/scripts/RunnableScriptEvent.java +++ b/src/de/steamwar/misslewars/scripts/RunnableScriptEvent.java @@ -1,3 +1,22 @@ +/* + 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 org.bukkit.Location; diff --git a/src/de/steamwar/misslewars/scripts/ScriptedItem.java b/src/de/steamwar/misslewars/scripts/ScriptedItem.java index 6c13949..21fb226 100644 --- a/src/de/steamwar/misslewars/scripts/ScriptedItem.java +++ b/src/de/steamwar/misslewars/scripts/ScriptedItem.java @@ -19,7 +19,6 @@ package de.steamwar.misslewars.scripts; -import com.google.gson.JsonArray; import com.google.gson.JsonObject; import org.bukkit.Location; import org.bukkit.Material; @@ -27,7 +26,10 @@ import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; public class ScriptedItem { diff --git a/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java b/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java index faa3169..d77e13d 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java @@ -1,3 +1,22 @@ +/* + 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.implemented; import com.google.gson.JsonObject; -- 2.39.2 From 5cf8037f2f5cfc67c7f75dccf9d2dc6638ca379f Mon Sep 17 00:00:00 2001 From: jojo Date: Sun, 1 Nov 2020 12:12:28 +0100 Subject: [PATCH 10/31] Optimize FilterScript --- .../misslewars/scripts/implemented/FilterScript.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java b/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java index 9088c4e..e9fd427 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java @@ -48,13 +48,10 @@ public class FilterScript implements RunnableScript { int blockZ = location.getBlockZ(); if (offset > 0) { - if (blockZ > bz - offset) return true; - if (blockZ < rz + offset) return true; + return (blockZ > bz - offset) || (blockZ < rz + offset); } else { - if (blockZ < bz - offset) return true; - if (blockZ > rz + offset) return true; + return (blockZ < bz - offset) || (blockZ > rz + offset); } - return false; }; break; default: -- 2.39.2 From eaefc39fbcad785605b7ffaf316711ce449a121d Mon Sep 17 00:00:00 2001 From: jojo Date: Sun, 1 Nov 2020 13:24:11 +0100 Subject: [PATCH 11/31] Optimize Code --- .../misslewars/countdowns/ItemCountdown.java | 2 +- .../misslewars/scripts/RunnableScript.java | 4 +- .../steamwar/misslewars/scripts/Script.java | 43 ++++++++- .../misslewars/scripts/ScriptParser.java | 67 ------------- .../misslewars/scripts/ScriptedItem.java | 2 +- .../function/ScriptBooleanFunction.java | 28 ++++++ .../function/ScriptVoidDoubleFunction.java | 28 ++++++ .../scripts/function/ScriptVoidFunction.java | 28 ++++++ .../scripts/implemented/DelayScript.java | 43 ++++----- .../scripts/implemented/FilterScript.java | 66 +++++++------ .../scripts/implemented/LaunchScript.java | 34 +++---- .../scripts/implemented/LocationScript.java | 94 +++++++++---------- .../scripts/implemented/PasteScript.java | 20 ++-- .../scripts/implemented/PotionScript.java | 31 ++---- .../scripts/implemented/RemoveScript.java | 2 +- .../scripts/implemented/SoundScript.java | 17 ++-- .../scripts/implemented/SummonScript.java | 21 ++--- .../misslewars/scripts/utils/EntityUtils.java | 64 +++++-------- .../misslewars/scripts/utils/JsonUtils.java | 60 ++++++++++++ 19 files changed, 356 insertions(+), 298 deletions(-) delete mode 100644 src/de/steamwar/misslewars/scripts/ScriptParser.java create mode 100644 src/de/steamwar/misslewars/scripts/function/ScriptBooleanFunction.java create mode 100644 src/de/steamwar/misslewars/scripts/function/ScriptVoidDoubleFunction.java create mode 100644 src/de/steamwar/misslewars/scripts/function/ScriptVoidFunction.java create mode 100644 src/de/steamwar/misslewars/scripts/utils/JsonUtils.java 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()); + } + +} -- 2.39.2 From e4130214d3fb7a142175cbbde909833a4bbd83e9 Mon Sep 17 00:00:00 2001 From: jojo Date: Sun, 1 Nov 2020 13:26:21 +0100 Subject: [PATCH 12/31] Optimize Code --- src/de/steamwar/misslewars/scripts/Script.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/steamwar/misslewars/scripts/Script.java b/src/de/steamwar/misslewars/scripts/Script.java index 883a9d5..6b987fb 100644 --- a/src/de/steamwar/misslewars/scripts/Script.java +++ b/src/de/steamwar/misslewars/scripts/Script.java @@ -72,7 +72,7 @@ public class Script { new ScriptExecutor(runnableScriptEvent).start(); } - public void add(RunnableScript runnableScript) { + private void add(RunnableScript runnableScript) { runnableScriptList.add(runnableScript); } -- 2.39.2 From 25f2b274064a65cd6ff82271893fc78fcffd0c02 Mon Sep 17 00:00:00 2001 From: jojo Date: Sun, 1 Nov 2020 14:02:07 +0100 Subject: [PATCH 13/31] Optimize Code --- src/de/steamwar/misslewars/scripts/RunnableScriptEvent.java | 5 +++++ .../misslewars/scripts/implemented/LaunchScript.java | 6 ++---- .../misslewars/scripts/implemented/PotionScript.java | 4 +--- .../misslewars/scripts/implemented/SoundScript.java | 2 +- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/de/steamwar/misslewars/scripts/RunnableScriptEvent.java b/src/de/steamwar/misslewars/scripts/RunnableScriptEvent.java index 8190edb..5404675 100644 --- a/src/de/steamwar/misslewars/scripts/RunnableScriptEvent.java +++ b/src/de/steamwar/misslewars/scripts/RunnableScriptEvent.java @@ -21,6 +21,7 @@ package de.steamwar.misslewars.scripts; import org.bukkit.Location; import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; public class RunnableScriptEvent { @@ -74,4 +75,8 @@ public class RunnableScriptEvent { this.customLocation = new Location(location.getWorld(), x, y, z, yaw, pitch); } + public Player getPlayer() { + return (Player) entity; + } + } diff --git a/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java b/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java index ce036b1..92e35b7 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java @@ -39,16 +39,14 @@ public class LaunchScript extends RunnableScript { switch (launch.getAsJsonPrimitive("entity").getAsString().toLowerCase()) { case "fireball": this.launch = runnableScriptEvent -> { - Player player = (Player) runnableScriptEvent.entity; - Fireball fireball = player.launchProjectile(Fireball.class); + Fireball fireball = runnableScriptEvent.getPlayer().launchProjectile(Fireball.class); setFireballOptions(fireball, launch); fireball.setDirection(runnableScriptEvent.getLocation().getDirection()); }; break; case "arrow": this.launch = runnableScriptEvent -> { - Player player = (Player) runnableScriptEvent.entity; - Arrow arrow = player.launchProjectile(Arrow.class); + Arrow arrow = runnableScriptEvent.getPlayer().launchProjectile(Arrow.class); setProjectileOptions(arrow, launch); }; break; diff --git a/src/de/steamwar/misslewars/scripts/implemented/PotionScript.java b/src/de/steamwar/misslewars/scripts/implemented/PotionScript.java index 14bdbdd..3f3d651 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/PotionScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/PotionScript.java @@ -23,7 +23,6 @@ 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; import org.bukkit.potion.PotionEffectType; @@ -52,8 +51,7 @@ public class PotionScript extends RunnableScript { public boolean execute(RunnableScriptEvent runnableScriptEvent) { if (potionEffect == null) return false; if (runnableScriptEvent.eventType != ScriptedItem.EventType.onClick) return true; - Player player = (Player) runnableScriptEvent.entity; - player.addPotionEffect(potionEffect); + runnableScriptEvent.getPlayer().addPotionEffect(potionEffect); return true; } diff --git a/src/de/steamwar/misslewars/scripts/implemented/SoundScript.java b/src/de/steamwar/misslewars/scripts/implemented/SoundScript.java index f7abd69..fd30196 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/SoundScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/SoundScript.java @@ -43,7 +43,7 @@ public class SoundScript extends RunnableScript { public boolean execute(RunnableScriptEvent runnableScriptEvent) { if (sound == null) return false; if (runnableScriptEvent.eventType != ScriptedItem.EventType.onClick) return true; - Player player = (Player) runnableScriptEvent.entity; + Player player = runnableScriptEvent.getPlayer(); player.playSound(player.getLocation(), sound, volume, pitch); return true; } -- 2.39.2 From 948d472f406edb448a16db4148413fcc91af83c3 Mon Sep 17 00:00:00 2001 From: jojo Date: Fri, 4 Dec 2020 21:47:14 +0100 Subject: [PATCH 14/31] Add DeathListener Add Config.test Add MWTeam color to tablist --- pom.xml | 7 +++++++ src/de/steamwar/misslewars/Config.java | 9 +++++++++ src/de/steamwar/misslewars/MWTeam.java | 13 +++++++------ .../steamwar/misslewars/listener/DeathListener.java | 4 ++++ 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index 226acf5..d6ed03e 100644 --- a/pom.xml +++ b/pom.xml @@ -53,5 +53,12 @@ system ${main.basedir}/lib/WorldEdit-1.15.jar + + steamwar + SpigotCore + 1.0 + system + ${main.basedir}/lib/SpigotCore.jar + \ No newline at end of file diff --git a/src/de/steamwar/misslewars/Config.java b/src/de/steamwar/misslewars/Config.java index 4e4ed04..27073cc 100644 --- a/src/de/steamwar/misslewars/Config.java +++ b/src/de/steamwar/misslewars/Config.java @@ -50,6 +50,8 @@ public class Config { public static final double MissileChance; + private static final int EventKampfID; + static { File configfile = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "config.yml"); if (!configfile.exists()) { @@ -83,5 +85,12 @@ public class Config { assert blue != null; BluePortalZ = blue.getInt("PortalZ"); BlueSpawn = new Location(Bukkit.getWorlds().get(0), blue.getDouble("SpawnX"), blue.getDouble("SpawnY"), blue.getDouble("SpawnZ"), (float)blue.getDouble("SpawnYaw"), (float)blue.getDouble("SpawnPitch")); + + EventKampfID = Integer.parseInt(System.getProperty("fightID", "0")); } + + public static boolean test() { + return EventKampfID == -1; + } + } diff --git a/src/de/steamwar/misslewars/MWTeam.java b/src/de/steamwar/misslewars/MWTeam.java index 72ba713..906adc4 100644 --- a/src/de/steamwar/misslewars/MWTeam.java +++ b/src/de/steamwar/misslewars/MWTeam.java @@ -19,10 +19,9 @@ package de.steamwar.misslewars; -import org.bukkit.ChatColor; -import org.bukkit.GameMode; -import org.bukkit.Location; -import org.bukkit.Material; +import de.steamwar.comms.packets.TablistNamePacket; +import de.steamwar.sql.SteamwarUser; +import org.bukkit.*; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; @@ -95,7 +94,7 @@ public class MWTeam { } public void teamScoreboard(Objective objective) { - players.forEach(p -> objective.getScore(getPrefix() + p.getName()).setScore(1)); + players.forEach(p -> objective.getScore(getColorCode() + p.getName()).setScore(1)); } public int size() { @@ -119,6 +118,8 @@ public class MWTeam { p.setDisplayName(color + p.getName()); if (MissileWars.getFightState() == FightState.WAITING && !enemy().players.isEmpty()) MissileWars.startRound(); + if (!Config.test()) + Bukkit.getScheduler().runTaskLater(MissileWars.getPlugin(), () -> new TablistNamePacket(SteamwarUser.get(p.getUniqueId()).getId(), color + p.getName()).send(p), 5); } public void leave(Player p) { @@ -137,7 +138,7 @@ public class MWTeam { return MissileWars.getRedTeam(); } - public String getPrefix(){ + public String getColorCode(){ return "§" + color.getChar(); } diff --git a/src/de/steamwar/misslewars/listener/DeathListener.java b/src/de/steamwar/misslewars/listener/DeathListener.java index 4c12226..735dcc4 100644 --- a/src/de/steamwar/misslewars/listener/DeathListener.java +++ b/src/de/steamwar/misslewars/listener/DeathListener.java @@ -28,11 +28,14 @@ import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.player.PlayerRespawnEvent; +import org.bukkit.util.Vector; import java.util.EnumSet; public class DeathListener extends BasicListener { + private static Vector ZERO = new Vector(0, 0, 0); + public DeathListener() { super(EnumSet.allOf(FightState.class)); } @@ -52,6 +55,7 @@ public class DeathListener extends BasicListener { return; e.setRespawnLocation(team.getSpawn()); + e.getPlayer().setVelocity(ZERO); new SpawnPlatformCreator(p); } } -- 2.39.2 From ea3181cc03dd49bb57b677357bdb0e8a00ffaefb Mon Sep 17 00:00:00 2001 From: jojo Date: Fri, 4 Dec 2020 22:42:04 +0100 Subject: [PATCH 15/31] Remove lines --- .../steamwar/misslewars/items/CustomItem.java | 11 ++---- src/de/steamwar/misslewars/items/Missile.java | 22 ++++-------- .../misslewars/items/SpecialItem.java | 30 +++++----------- .../misslewars/scripts/LocationType.java | 2 -- .../misslewars/scripts/RunnableScript.java | 2 -- .../scripts/RunnableScriptEvent.java | 12 ++----- .../steamwar/misslewars/scripts/Script.java | 13 ++----- .../misslewars/scripts/ScriptedItem.java | 18 +++------- .../function/ScriptBooleanFunction.java | 2 -- .../function/ScriptVoidDoubleFunction.java | 2 -- .../scripts/function/ScriptVoidFunction.java | 2 -- .../scripts/implemented/DelayScript.java | 7 ++-- .../scripts/implemented/FilterScript.java | 26 ++++---------- .../scripts/implemented/LaunchScript.java | 34 +++++++++++-------- .../scripts/implemented/LocationScript.java | 12 ++----- .../scripts/implemented/PasteScript.java | 27 ++++++--------- .../scripts/implemented/PotionScript.java | 3 +- .../scripts/implemented/RemoveScript.java | 4 +-- .../scripts/implemented/SummonScript.java | 28 ++++++++------- .../misslewars/scripts/utils/EntityUtils.java | 1 - .../misslewars/scripts/utils/JsonUtils.java | 5 --- .../scripts/utils/ScriptShortcut.java | 33 ++++++++++++++++++ .../misslewars/scripts/utils/TriConsumer.java | 24 +++++++++++++ 23 files changed, 144 insertions(+), 176 deletions(-) create mode 100644 src/de/steamwar/misslewars/scripts/utils/ScriptShortcut.java create mode 100644 src/de/steamwar/misslewars/scripts/utils/TriConsumer.java diff --git a/src/de/steamwar/misslewars/items/CustomItem.java b/src/de/steamwar/misslewars/items/CustomItem.java index cb2aba8..cfcebda 100644 --- a/src/de/steamwar/misslewars/items/CustomItem.java +++ b/src/de/steamwar/misslewars/items/CustomItem.java @@ -64,20 +64,15 @@ public class CustomItem extends SpecialItem { public static void init() { File itemsFolder = new File(MissileWars.getPlugin().getDataFolder(), "items"); - if (!itemsFolder.exists() || !itemsFolder.canRead() || !itemsFolder.isDirectory()) { - throw new SecurityException("Items could not be loaded"); - } + if (!itemsFolder.exists() || !itemsFolder.canRead() || !itemsFolder.isDirectory()) throw new SecurityException("Items could not be loaded"); for (File itemFile : Objects.requireNonNull(itemsFolder.listFiles())) { if (!itemFile.canRead() || !itemFile.isFile()) continue; try { JsonObject jsonObject = new JsonParser().parse(new FileReader(itemFile)).getAsJsonObject(); new CustomItem(new ScriptedItem(jsonObject)); - } catch (JsonSyntaxException e) { + } catch (JsonSyntaxException | IOException e) { e.printStackTrace(); - throw new SecurityException("Item JSON error"); - } catch (IOException e) { - e.printStackTrace(); - throw new SecurityException("Corrupt Item"); + throw new SecurityException("Item JSON error", e); } } } diff --git a/src/de/steamwar/misslewars/items/Missile.java b/src/de/steamwar/misslewars/items/Missile.java index 09ecb5a..b97d18b 100644 --- a/src/de/steamwar/misslewars/items/Missile.java +++ b/src/de/steamwar/misslewars/items/Missile.java @@ -96,9 +96,7 @@ public class Missile extends SpecialItem { if (index > size) index = size; StringBuilder st = new StringBuilder(); st.append("§8[§e"); - if (index > 0) { - st.append(repeat(index)); - } + if (index > 0) st.append(repeat(index)); st.append("§7"); st.append(repeat(size - index)); st.append("§8]"); @@ -108,9 +106,7 @@ public class Missile extends SpecialItem { private String repeat(int count) { if (count == 0) return ""; StringBuilder st = new StringBuilder(); - for (int i = 0; i < count; i++) { - st.append("="); - } + for (int i = 0; i < count; i++) st.append("="); return st.toString(); } @@ -128,13 +124,9 @@ public class Missile extends SpecialItem { AffineTransform aT = new AffineTransform(); double yaw = (p.getLocation().getYaw() + 360f) % 360; - if (yaw > 45 && yaw <= 135) { - aT = aT.rotateY(270); - } else if (yaw > 135 && yaw <= 225) { - aT = aT.rotateY(180); - } else if (yaw > 225 && yaw <= 315) { - aT = aT.rotateY(90); - } + if (yaw > 45 && yaw <= 135) aT = aT.rotateY(270); + else if (yaw > 135 && yaw <= 225) aT = aT.rotateY(180); + else if (yaw > 225 && yaw <= 315) aT = aT.rotateY(90); v = v.subtract(dimensions.getX()/2, dimensions.getY() + 2, -2).subtract(offset); v = aT.apply(v.toVector3()).toBlockPoint(); @@ -155,9 +147,7 @@ public class Missile extends SpecialItem { public static void init() { File missileFolder = new File(MissileWars.getPlugin().getDataFolder(), "missiles"); - if (!missileFolder.exists() || !missileFolder.canRead() || !missileFolder.isDirectory()) { - throw new SecurityException("Missiles could not be loaded"); - } + if (!missileFolder.exists() || !missileFolder.canRead() || !missileFolder.isDirectory()) throw new SecurityException("Missiles could not be loaded"); for (File missileFile : Objects.requireNonNull(missileFolder.listFiles())) { if (!missileFile.canRead() || !missileFile.isFile()) continue; new Missile(missileFile); diff --git a/src/de/steamwar/misslewars/items/SpecialItem.java b/src/de/steamwar/misslewars/items/SpecialItem.java index eb34945..f776115 100644 --- a/src/de/steamwar/misslewars/items/SpecialItem.java +++ b/src/de/steamwar/misslewars/items/SpecialItem.java @@ -42,11 +42,8 @@ public abstract class SpecialItem { private static List missileItems = new ArrayList<>(); SpecialItem() { - if (this.isMissile()) { - missileItems.add(this); - } else { - supportItems.add(this); - } + if (this.isMissile()) missileItems.add(this); + else supportItems.add(this); } private String materialName = null; @@ -78,21 +75,18 @@ public abstract class SpecialItem { } private static boolean handleUse(ItemStack item, Player player, List items) { - for (SpecialItem specialItem : items) { + for (SpecialItem specialItem : items) if (item.isSimilar(specialItem.getItem())) return specialItem.handleUse(player); - } return false; } public static void handleThrow(ProjectileLaunchEvent e) { String name = e.getEntity().getClass().getName().toLowerCase(); for (SpecialItem specialItem : supportItems) { - if (specialItem.materialName == null) { + if (specialItem.materialName == null) specialItem.materialName = specialItem.getItem().getType().name().toLowerCase(); - } - if (name.contains(specialItem.materialName)) { + if (name.contains(specialItem.materialName)) specialItem.handleThrow(e.getEntity()); - } } } @@ -100,11 +94,8 @@ public abstract class SpecialItem { String name = e.getEntity().getClass().getName().toLowerCase(); Location location = null; - if (e.getHitEntity() != null) { - location = e.getHitEntity().getLocation(); - } else if (e.getHitBlock() != null) { - location = e.getHitBlock().getLocation(); - } + if (e.getHitEntity() != null) location = e.getHitEntity().getLocation(); + else if (e.getHitBlock() != null) location = e.getHitBlock().getLocation(); if (location == null) return; for (SpecialItem specialItem : supportItems) { @@ -115,11 +106,8 @@ public abstract class SpecialItem { } public static ItemStack getRandomItem() { - if (random.nextDouble() > Config.MissileChance) { - return supportItems.get(random.nextInt(supportItems.size())).getItem(); - } else { - return missileItems.get(random.nextInt(missileItems.size())).getItem(); - } + if (random.nextDouble() > Config.MissileChance) return supportItems.get(random.nextInt(supportItems.size())).getItem(); + else return missileItems.get(random.nextInt(missileItems.size())).getItem(); } } diff --git a/src/de/steamwar/misslewars/scripts/LocationType.java b/src/de/steamwar/misslewars/scripts/LocationType.java index 1ed8325..5566c56 100644 --- a/src/de/steamwar/misslewars/scripts/LocationType.java +++ b/src/de/steamwar/misslewars/scripts/LocationType.java @@ -20,10 +20,8 @@ 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 897e71d..df046b7 100644 --- a/src/de/steamwar/misslewars/scripts/RunnableScript.java +++ b/src/de/steamwar/misslewars/scripts/RunnableScript.java @@ -20,7 +20,5 @@ package de.steamwar.misslewars.scripts; public abstract class RunnableScript { - public abstract boolean execute(RunnableScriptEvent runnableScriptEvent); - } diff --git a/src/de/steamwar/misslewars/scripts/RunnableScriptEvent.java b/src/de/steamwar/misslewars/scripts/RunnableScriptEvent.java index 5404675..d303d77 100644 --- a/src/de/steamwar/misslewars/scripts/RunnableScriptEvent.java +++ b/src/de/steamwar/misslewars/scripts/RunnableScriptEvent.java @@ -39,14 +39,10 @@ public class RunnableScriptEvent { public Location getLocation() { // Custom location - if (locationType == LocationType.CUSTOM && customLocation != null) { - return customLocation; - } + if (locationType == LocationType.CUSTOM && customLocation != null) return customLocation; // Static initial Location - if (locationType == LocationType.STATIC) { - return location; - } + if (locationType == LocationType.STATIC) return location; // Dynamic Location if entity is not null if (locationType == LocationType.DYNAMIC) { @@ -56,9 +52,7 @@ public class RunnableScriptEvent { // Default Location is static if EventType is onClick otherwise dynamic if (eventType == ScriptedItem.EventType.onClick) return location; - if (entity != null) { - return entity.getLocation(); - } + if (entity != null) return entity.getLocation(); return location; } diff --git a/src/de/steamwar/misslewars/scripts/Script.java b/src/de/steamwar/misslewars/scripts/Script.java index 6b987fb..9a3c23e 100644 --- a/src/de/steamwar/misslewars/scripts/Script.java +++ b/src/de/steamwar/misslewars/scripts/Script.java @@ -76,13 +76,6 @@ public class Script { runnableScriptList.add(runnableScript); } - @Override - public String toString() { - return "Script{" + - "runnableScriptList=" + runnableScriptList + - '}'; - } - public static Script parseScript(JsonArray jsonArray) { Script script = new Script(); jsonArray.forEach(jsonElement -> { @@ -94,11 +87,9 @@ public class Script { } private static RunnableScript parseScriptSnippet(JsonObject jsonObject) { - if (!jsonObject.has("type")) { + if (!jsonObject.has("type")) return null; - } - String type = jsonObject.getAsJsonPrimitive("type").getAsString(); - switch (type.toLowerCase()) { + switch (jsonObject.getAsJsonPrimitive("type").getAsString().toLowerCase()) { case "delay": return new DelayScript(jsonObject); case "filter": diff --git a/src/de/steamwar/misslewars/scripts/ScriptedItem.java b/src/de/steamwar/misslewars/scripts/ScriptedItem.java index c992aad..de7bf3d 100644 --- a/src/de/steamwar/misslewars/scripts/ScriptedItem.java +++ b/src/de/steamwar/misslewars/scripts/ScriptedItem.java @@ -62,33 +62,25 @@ public class ScriptedItem { public ScriptedItem(JsonObject jsonObject) { itemStack = createItemStack(jsonObject); - if (jsonObject.has("entityName")) { + if (jsonObject.has("entityName")) entityName = jsonObject.get("entityName").getAsString(); - } for (EventType eventType : EventType.values()) { String eventString = "EVENT." + eventType.name(); - if (!jsonObject.has(eventString)) continue; - if (!jsonObject.get(eventString).isJsonArray()) continue; + if (!jsonObject.has(eventString) || !jsonObject.get(eventString).isJsonArray()) continue; scriptMap.put(eventType, Script.parseScript(jsonObject.getAsJsonArray(eventString))); } } private ItemStack createItemStack(JsonObject jsonObject) { - String type = jsonObject.getAsJsonPrimitive("type").getAsString(); - String name = jsonObject.getAsJsonPrimitive("name").getAsString(); - int amount = jsonObject.getAsJsonPrimitive("amount").getAsInt(); - - ItemStack itemStack = new ItemStack(Material.valueOf(type), amount); + ItemStack itemStack = new ItemStack(Material.valueOf(jsonObject.getAsJsonPrimitive("type").getAsString()), jsonObject.getAsJsonPrimitive("amount").getAsInt()); ItemMeta itemMeta = itemStack.getItemMeta(); if (itemMeta == null) return itemStack; - itemMeta.setDisplayName(name); + itemMeta.setDisplayName(jsonObject.getAsJsonPrimitive("name").getAsString()); if (jsonObject.has("lore")) { List lore = new ArrayList<>(); - jsonObject.getAsJsonArray("lore").forEach(jsonElement -> { - lore.add(jsonElement.getAsString()); - }); + jsonObject.getAsJsonArray("lore").forEach(jsonElement -> lore.add(jsonElement.getAsString())); itemMeta.setLore(lore); } diff --git a/src/de/steamwar/misslewars/scripts/function/ScriptBooleanFunction.java b/src/de/steamwar/misslewars/scripts/function/ScriptBooleanFunction.java index 8f877ee..92d7c30 100644 --- a/src/de/steamwar/misslewars/scripts/function/ScriptBooleanFunction.java +++ b/src/de/steamwar/misslewars/scripts/function/ScriptBooleanFunction.java @@ -22,7 +22,5 @@ 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 index 26eaf8a..25a7f90 100644 --- a/src/de/steamwar/misslewars/scripts/function/ScriptVoidDoubleFunction.java +++ b/src/de/steamwar/misslewars/scripts/function/ScriptVoidDoubleFunction.java @@ -22,7 +22,5 @@ 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 index 5c2df8f..2404a42 100644 --- a/src/de/steamwar/misslewars/scripts/function/ScriptVoidFunction.java +++ b/src/de/steamwar/misslewars/scripts/function/ScriptVoidFunction.java @@ -22,7 +22,5 @@ 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 ea111ba..98fd631 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java @@ -47,11 +47,8 @@ public class DelayScript extends RunnableScript { public DelayScript(JsonObject delay) { JsonPrimitive jsonPrimitive = delay.getAsJsonPrimitive("time"); - if (jsonPrimitive.isString()) { - delayTime = delayMap.getOrDefault(jsonPrimitive.getAsString().toLowerCase(), 0); - } else if (jsonPrimitive.isNumber()) { - delayTime = jsonPrimitive.getAsInt(); - } + if (jsonPrimitive.isString()) delayTime = delayMap.getOrDefault(jsonPrimitive.getAsString().toLowerCase(), 0); + else if (jsonPrimitive.isNumber()) delayTime = jsonPrimitive.getAsInt(); } @Override diff --git a/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java b/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java index 7869761..fc0a521 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java @@ -40,15 +40,11 @@ public class FilterScript extends RunnableScript { Location location = runnableScriptEvent.getLocation(); int bz = MissileWars.getBlueTeam().getPortalZ(); int rz = MissileWars.getRedTeam().getPortalZ(); - - int offset = sign(bz - rz) * 5; + int offset = (int) Math.signum(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); - } + 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(); @@ -56,29 +52,19 @@ public class FilterScript extends RunnableScript { }); } - 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")) { - inverted = filter.getAsJsonPrimitive("invert").getAsBoolean(); - } + if (filter.has("invert")) inverted = filter.getAsJsonPrimitive("invert").getAsBoolean(); } @Override public boolean execute(RunnableScriptEvent runnableScriptEvent) { if (filter == null) return true; - if (inverted) { - return !filter.execute(runnableScriptEvent); - } else { - return filter.execute(runnableScriptEvent); - } + if (inverted) return !filter.execute(runnableScriptEvent); + else return filter.execute(runnableScriptEvent); } } diff --git a/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java b/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java index 92e35b7..3d8bd71 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java @@ -24,9 +24,10 @@ 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.utils.ScriptShortcut; import org.bukkit.entity.Arrow; import org.bukkit.entity.Fireball; -import org.bukkit.entity.Player; +import org.bukkit.entity.Projectile; import static de.steamwar.misslewars.scripts.utils.EntityUtils.setFireballOptions; import static de.steamwar.misslewars.scripts.utils.EntityUtils.setProjectileOptions; @@ -36,23 +37,26 @@ public class LaunchScript extends RunnableScript { private ScriptVoidFunction launch = null; public LaunchScript(JsonObject launch) { - switch (launch.getAsJsonPrimitive("entity").getAsString().toLowerCase()) { + ScriptShortcut scriptShortcut = getEntity(launch.getAsJsonPrimitive("entity").getAsString()); + if (scriptShortcut == null) return; + + this.launch = runnableScriptEvent -> { + Projectile projectile = runnableScriptEvent.getPlayer().launchProjectile(scriptShortcut.entityClass); + scriptShortcut.consumer.accept(launch, projectile, runnableScriptEvent); + }; + } + + private ScriptShortcut getEntity(String name) { + switch (name.toLowerCase()) { case "fireball": - this.launch = runnableScriptEvent -> { - Fireball fireball = runnableScriptEvent.getPlayer().launchProjectile(Fireball.class); - setFireballOptions(fireball, launch); - fireball.setDirection(runnableScriptEvent.getLocation().getDirection()); - }; - break; + return new ScriptShortcut<>(Fireball.class, (jsonObject, entity, runnableScriptEvent) -> { + setFireballOptions(entity, jsonObject); + entity.setDirection(runnableScriptEvent.getLocation().getDirection()); + }); case "arrow": - this.launch = runnableScriptEvent -> { - Arrow arrow = runnableScriptEvent.getPlayer().launchProjectile(Arrow.class); - setProjectileOptions(arrow, launch); - }; - break; - default: - break; + return new ScriptShortcut<>(Arrow.class, (jsonObject, entity, runnableScriptEvent) -> setProjectileOptions(entity, jsonObject)); } + return null; } @Override diff --git a/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java b/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java index a7a7852..b2abed8 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java @@ -44,9 +44,7 @@ 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; Location location1 = runnableScriptEvent.entity.getLocation(); runnableScriptEvent.setCustomLocation(location1.getX() + doubles[0], location1.getY() + doubles[1], location1.getZ() + doubles[2]); }); @@ -54,9 +52,7 @@ public class LocationScript extends RunnableScript { 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]); - }; + ScriptVoidDoubleFunction absoluteLocation = (runnableScriptEvent, doubles) -> runnableScriptEvent.setCustomLocation(doubles[0], doubles[1], doubles[2]); locationMap.put("absolut", absoluteLocation); locationMap.put("fix", absoluteLocation); locationMap.put("fixed", absoluteLocation); @@ -65,9 +61,7 @@ public class LocationScript extends RunnableScript { private LocationType locationType = null; private ScriptVoidDoubleFunction locationExecutor = null; - private double x = 0; - private double y = 0; - private double z = 0; + private double x, y, z = 0; public LocationScript(JsonObject location) { if (location.has("location")) { diff --git a/src/de/steamwar/misslewars/scripts/implemented/PasteScript.java b/src/de/steamwar/misslewars/scripts/implemented/PasteScript.java index 3a68772..e4c81ba 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/PasteScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/PasteScript.java @@ -50,11 +50,8 @@ public class PasteScript extends RunnableScript { private final Clipboard clipboard; private final BlockVector3 centeredOffset; - private boolean centered; - private boolean ignoreAir; - private int xOffset = 0; - private int yOffset = 0; - private int zOffset = 0; + private boolean centered, ignoreAir; + private int xOffset, yOffset, zOffset = 0; public PasteScript(JsonObject paste) { String schemFileName = paste.getAsJsonPrimitive("schem").getAsString(); @@ -70,23 +67,21 @@ public class PasteScript extends RunnableScript { centered = getBoolean(paste, "centered", false); ignoreAir = getBoolean(paste, "ignoreAir", false); - if (paste.has("offset")) { - JsonArray jsonArray = paste.getAsJsonArray("offset"); - if (jsonArray.size() == 3) { - xOffset = jsonArray.get(0).getAsInt(); - yOffset = jsonArray.get(1).getAsInt(); - zOffset = jsonArray.get(2).getAsInt(); - } - } + if (paste.has("offset")) + return; + JsonArray jsonArray = paste.getAsJsonArray("offset"); + if (jsonArray.size() == 3) + return; + xOffset = jsonArray.get(0).getAsInt(); + yOffset = jsonArray.get(1).getAsInt(); + zOffset = jsonArray.get(2).getAsInt(); } @Override public boolean execute(RunnableScriptEvent runnableScriptEvent) { Location location = runnableScriptEvent.getLocation(); BlockVector3 paste = BlockVector3.at(location.getX() + xOffset, location.getY() + yOffset, location.getZ() + zOffset); - if (centered) { - paste = paste.subtract(centeredOffset); - } + if (centered) paste = paste.subtract(centeredOffset); EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(world, -1); Operations.completeBlindly(new ClipboardHolder(clipboard).createPaste(editSession).ignoreAirBlocks(ignoreAir).to(paste).build()); diff --git a/src/de/steamwar/misslewars/scripts/implemented/PotionScript.java b/src/de/steamwar/misslewars/scripts/implemented/PotionScript.java index 3f3d651..0156391 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/PotionScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/PotionScript.java @@ -41,9 +41,8 @@ public class PotionScript extends RunnableScript { boolean icon = getBoolean(potion, "icon", true); PotionEffectType potionEffectType = PotionEffectType.getByName(potion.getAsJsonPrimitive("potion").getAsString()); - if (potionEffectType == null) { + if (potionEffectType == null) return; - } potionEffect = new PotionEffect(potionEffectType, duration, amplifier, ambient, particles, icon); } diff --git a/src/de/steamwar/misslewars/scripts/implemented/RemoveScript.java b/src/de/steamwar/misslewars/scripts/implemented/RemoveScript.java index 63530b0..6f2f3f1 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/RemoveScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/RemoveScript.java @@ -26,9 +26,7 @@ import org.bukkit.entity.Player; public class RemoveScript extends RunnableScript { - public RemoveScript(JsonObject jsonObject) { - - } + public RemoveScript(JsonObject jsonObject) {} @Override public boolean execute(RunnableScriptEvent runnableScriptEvent) { diff --git a/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java b/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java index 65d3fda..44bccf5 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java @@ -23,28 +23,32 @@ 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.utils.ScriptShortcut; +import org.bukkit.entity.Entity; 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.setTNTPrimedOptions; public class SummonScript extends RunnableScript { private ScriptVoidFunction summon = null; public SummonScript(JsonObject summon) { - switch (summon.getAsJsonPrimitive("entity").getAsString().toLowerCase()) { + ScriptShortcut scriptShortcut = getEntity(summon.getAsJsonPrimitive("entity").getAsString()); + if (scriptShortcut == null) return; + + this.summon = runnableScriptEvent -> { + Entity entity = runnableScriptEvent.entity.getWorld().spawn(runnableScriptEvent.getLocation(), scriptShortcut.entityClass); + scriptShortcut.consumer.accept(summon, entity, runnableScriptEvent); + }; + } + + private ScriptShortcut getEntity(String name) { + switch (name.toLowerCase()) { case "tntprimed": - this.summon = runnableScriptEvent -> { - TNTPrimed tnt = runnableScriptEvent.entity.getWorld().spawn(runnableScriptEvent.getLocation(), TNTPrimed.class); - setTNTPrimedOptions(tnt, summon); - }; - break; - default: - break; + return new ScriptShortcut<>(TNTPrimed.class, (jsonObject, entity, runnableScriptEvent) -> setTNTPrimedOptions(entity, jsonObject)); } + return null; } @Override diff --git a/src/de/steamwar/misslewars/scripts/utils/EntityUtils.java b/src/de/steamwar/misslewars/scripts/utils/EntityUtils.java index 8e9d66a..eb044f1 100644 --- a/src/de/steamwar/misslewars/scripts/utils/EntityUtils.java +++ b/src/de/steamwar/misslewars/scripts/utils/EntityUtils.java @@ -47,7 +47,6 @@ public class EntityUtils { setEntityOptions(explosive, jsonObject); } - public static void setFireballOptions(Fireball fireball, JsonObject jsonObject) { setProjectileOptions(fireball, jsonObject); setExplosiveOptions(fireball, jsonObject); diff --git a/src/de/steamwar/misslewars/scripts/utils/JsonUtils.java b/src/de/steamwar/misslewars/scripts/utils/JsonUtils.java index 31501bb..1f9b8c1 100644 --- a/src/de/steamwar/misslewars/scripts/utils/JsonUtils.java +++ b/src/de/steamwar/misslewars/scripts/utils/JsonUtils.java @@ -22,11 +22,6 @@ public class JsonUtils { 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; diff --git a/src/de/steamwar/misslewars/scripts/utils/ScriptShortcut.java b/src/de/steamwar/misslewars/scripts/utils/ScriptShortcut.java new file mode 100644 index 0000000..1dfcd4e --- /dev/null +++ b/src/de/steamwar/misslewars/scripts/utils/ScriptShortcut.java @@ -0,0 +1,33 @@ +/* + 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.utils; + +import com.google.gson.JsonObject; +import de.steamwar.misslewars.scripts.RunnableScriptEvent; + +public class ScriptShortcut { + public Class entityClass; + public TriConsumer consumer; + + public ScriptShortcut(Class entityClass, TriConsumer consumer) { + this.entityClass = entityClass; + this.consumer = consumer; + } +} diff --git a/src/de/steamwar/misslewars/scripts/utils/TriConsumer.java b/src/de/steamwar/misslewars/scripts/utils/TriConsumer.java new file mode 100644 index 0000000..7cc9718 --- /dev/null +++ b/src/de/steamwar/misslewars/scripts/utils/TriConsumer.java @@ -0,0 +1,24 @@ +/* + 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.utils; + +public interface TriConsumer { + void accept(T t, R r, K k); +} -- 2.39.2 From 7dd543dab58e881716906680179da98571e9154c Mon Sep 17 00:00:00 2001 From: jojo Date: Fri, 18 Dec 2020 23:19:10 +0100 Subject: [PATCH 16/31] Add Basic challenge functions --- src/de/steamwar/misslewars/Config.java | 19 +++++++++++++++---- src/de/steamwar/misslewars/MWTeam.java | 2 -- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/de/steamwar/misslewars/Config.java b/src/de/steamwar/misslewars/Config.java index 27073cc..da66400 100644 --- a/src/de/steamwar/misslewars/Config.java +++ b/src/de/steamwar/misslewars/Config.java @@ -26,6 +26,7 @@ import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import java.io.File; +import java.util.UUID; import java.util.logging.Level; public class Config { @@ -50,7 +51,8 @@ public class Config { public static final double MissileChance; - private static final int EventKampfID; + public static final UUID BlueLeader; + public static final UUID RedLeader; static { File configfile = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "config.yml"); @@ -86,11 +88,20 @@ public class Config { BluePortalZ = blue.getInt("PortalZ"); BlueSpawn = new Location(Bukkit.getWorlds().get(0), blue.getDouble("SpawnX"), blue.getDouble("SpawnY"), blue.getDouble("SpawnZ"), (float)blue.getDouble("SpawnYaw"), (float)blue.getDouble("SpawnPitch")); - EventKampfID = Integer.parseInt(System.getProperty("fightID", "0")); + String blueLeader = System.getProperty("blueLeader", null); + String redLeader = System.getProperty("redLeader", null); + if(blueLeader != null) + BlueLeader = UUID.fromString(blueLeader); + else + BlueLeader = null; + if(redLeader != null) + RedLeader = UUID.fromString(redLeader); + else + RedLeader = null; } - public static boolean test() { - return EventKampfID == -1; + public boolean isChallenge() { + return BlueLeader != null && RedLeader != null; } } diff --git a/src/de/steamwar/misslewars/MWTeam.java b/src/de/steamwar/misslewars/MWTeam.java index 906adc4..82be00a 100644 --- a/src/de/steamwar/misslewars/MWTeam.java +++ b/src/de/steamwar/misslewars/MWTeam.java @@ -118,8 +118,6 @@ public class MWTeam { p.setDisplayName(color + p.getName()); if (MissileWars.getFightState() == FightState.WAITING && !enemy().players.isEmpty()) MissileWars.startRound(); - if (!Config.test()) - Bukkit.getScheduler().runTaskLater(MissileWars.getPlugin(), () -> new TablistNamePacket(SteamwarUser.get(p.getUniqueId()).getId(), color + p.getName()).send(p), 5); } public void leave(Player p) { -- 2.39.2 From e7d27fc1338be000859a3ddc5272f2fc305d21d7 Mon Sep 17 00:00:00 2001 From: jojo Date: Fri, 18 Dec 2020 23:33:29 +0100 Subject: [PATCH 17/31] Add CommandInvite Add CommandSpectate disable while challenge Add openInvitation HashSet --- src/de/steamwar/misslewars/Config.java | 2 +- src/de/steamwar/misslewars/MWTeam.java | 12 ++++-- src/de/steamwar/misslewars/MissileWars.java | 1 + .../misslewars/commands/CommandInvite.java | 43 +++++++++++++++++++ .../misslewars/commands/CommandSpectate.java | 9 +++- src/plugin.yml | 3 +- 6 files changed, 63 insertions(+), 7 deletions(-) create mode 100644 src/de/steamwar/misslewars/commands/CommandInvite.java diff --git a/src/de/steamwar/misslewars/Config.java b/src/de/steamwar/misslewars/Config.java index da66400..b56c7cc 100644 --- a/src/de/steamwar/misslewars/Config.java +++ b/src/de/steamwar/misslewars/Config.java @@ -100,7 +100,7 @@ public class Config { RedLeader = null; } - public boolean isChallenge() { + public static boolean isChallenge() { return BlueLeader != null && RedLeader != null; } diff --git a/src/de/steamwar/misslewars/MWTeam.java b/src/de/steamwar/misslewars/MWTeam.java index 82be00a..59e189b 100644 --- a/src/de/steamwar/misslewars/MWTeam.java +++ b/src/de/steamwar/misslewars/MWTeam.java @@ -19,9 +19,10 @@ package de.steamwar.misslewars; -import de.steamwar.comms.packets.TablistNamePacket; -import de.steamwar.sql.SteamwarUser; -import org.bukkit.*; +import org.bukkit.ChatColor; +import org.bukkit.GameMode; +import org.bukkit.Location; +import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; @@ -30,8 +31,10 @@ import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.scoreboard.Objective; import org.bukkit.scoreboard.Team; +import java.util.HashSet; import java.util.LinkedList; import java.util.Objects; +import java.util.Set; public class MWTeam { private static final ItemStack bow = new ItemStack(Material.BOW); @@ -52,7 +55,8 @@ public class MWTeam { private final Location spawn; private final int portalZ; - private LinkedList players = new LinkedList<>(); + private final LinkedList players = new LinkedList<>(); + private final Set openInvitations = new HashSet<>(); MWTeam(ChatColor color, Location spawn, String teamName, int portalZ) { this.teamName = teamName; diff --git a/src/de/steamwar/misslewars/MissileWars.java b/src/de/steamwar/misslewars/MissileWars.java index f48e70a..ad90c0c 100644 --- a/src/de/steamwar/misslewars/MissileWars.java +++ b/src/de/steamwar/misslewars/MissileWars.java @@ -62,6 +62,7 @@ public class MissileWars extends JavaPlugin { new FightListener(); new ChatListener(); getCommand("spectate").setExecutor(new CommandSpectate()); + getCommand("invite").setExecutor(new CommandSpectate()); new WaitingCountdown(); new ItemCountdown(); diff --git a/src/de/steamwar/misslewars/commands/CommandInvite.java b/src/de/steamwar/misslewars/commands/CommandInvite.java new file mode 100644 index 0000000..8126ca8 --- /dev/null +++ b/src/de/steamwar/misslewars/commands/CommandInvite.java @@ -0,0 +1,43 @@ +/* + * + * 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.commands; + +import de.steamwar.misslewars.Config; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class CommandInvite implements CommandExecutor { + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (!(sender instanceof Player)) return false; + Player player = (Player) sender; + if (!Config.isChallenge()) { + player.sendMessage("§cDieser Command ist deaktiviert."); + return false; + } + return false; + } + +} diff --git a/src/de/steamwar/misslewars/commands/CommandSpectate.java b/src/de/steamwar/misslewars/commands/CommandSpectate.java index 6efa06f..4a8c1f2 100644 --- a/src/de/steamwar/misslewars/commands/CommandSpectate.java +++ b/src/de/steamwar/misslewars/commands/CommandSpectate.java @@ -19,6 +19,7 @@ package de.steamwar.misslewars.commands; +import de.steamwar.misslewars.Config; import de.steamwar.misslewars.MWTeam; import de.steamwar.misslewars.MissileWars; import org.bukkit.GameMode; @@ -31,8 +32,12 @@ public class CommandSpectate implements CommandExecutor { @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - if(!(sender instanceof Player)) return false; + if (!(sender instanceof Player)) return false; Player player = (Player) sender; + if (Config.isChallenge()) { + player.sendMessage("§cDieser Command ist deaktiviert."); + return false; + } MWTeam mwTeam = MissileWars.getTeam(player); if (mwTeam == null) return false; @@ -42,6 +47,8 @@ public class CommandSpectate implements CommandExecutor { } MissileWars.leave(player); player.setGameMode(GameMode.SPECTATOR); + player.getInventory().clear(); + player.updateInventory(); return true; } diff --git a/src/plugin.yml b/src/plugin.yml index 452b135..c95edb1 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -11,4 +11,5 @@ depend: - WorldEdit - SpigotCore commands: - spectate: \ No newline at end of file + spectate: + invite: \ No newline at end of file -- 2.39.2 From 828406220fb0ef16a5b3c188ebc6ea8d7d7aa3be Mon Sep 17 00:00:00 2001 From: jojo Date: Sat, 19 Dec 2020 11:38:38 +0100 Subject: [PATCH 18/31] Add CommandAccept Add CommandDecline Add CommandInvite Add InvitationSystem --- src/de/steamwar/misslewars/MWTeam.java | 23 +++++++- src/de/steamwar/misslewars/MissileWars.java | 21 +++++++- .../misslewars/commands/CommandAccept.java | 53 ++++++++++++++++++ .../misslewars/commands/CommandDecline.java | 54 +++++++++++++++++++ .../misslewars/commands/CommandInvite.java | 50 ++++++++++++++++- .../listener/ConnectionListener.java | 2 + src/plugin.yml | 4 +- 7 files changed, 203 insertions(+), 4 deletions(-) create mode 100644 src/de/steamwar/misslewars/commands/CommandAccept.java create mode 100644 src/de/steamwar/misslewars/commands/CommandDecline.java diff --git a/src/de/steamwar/misslewars/MWTeam.java b/src/de/steamwar/misslewars/MWTeam.java index 59e189b..111e30e 100644 --- a/src/de/steamwar/misslewars/MWTeam.java +++ b/src/de/steamwar/misslewars/MWTeam.java @@ -19,6 +19,7 @@ package de.steamwar.misslewars; +import de.steamwar.misslewars.items.Missile; import org.bukkit.ChatColor; import org.bukkit.GameMode; import org.bukkit.Location; @@ -132,6 +133,17 @@ public class MWTeam { if (players.isEmpty() && MissileWars.getFightState() == FightState.FIGHTING) MissileWars.end(WinReasons.NO_ENEMY, enemy()); } + + public void invitePlayer(Player p) { + if (enemy().openInvitations.contains(p)) return; + + openInvitations.add(p); + } + + public void acceptInvite(Player p) { + removeInvitations(p); + join(p); + } private MWTeam enemy() { if (this == MissileWars.getRedTeam()) @@ -144,12 +156,21 @@ public class MWTeam { return "§" + color.getChar(); } - public boolean hasPlayer (Player p) { + public boolean hasPlayer(Player p) { return players.contains(p); } + public boolean hasInvite(Player p) { + return openInvitations.contains(p); + } + public String getColoredName() { return color.toString() + teamName; } + public static void removeInvitations(Player p) { + MissileWars.getRedTeam().openInvitations.remove(p); + MissileWars.getBlueTeam().openInvitations.remove(p); + } + } diff --git a/src/de/steamwar/misslewars/MissileWars.java b/src/de/steamwar/misslewars/MissileWars.java index ad90c0c..f28d455 100644 --- a/src/de/steamwar/misslewars/MissileWars.java +++ b/src/de/steamwar/misslewars/MissileWars.java @@ -19,6 +19,9 @@ package de.steamwar.misslewars; +import de.steamwar.misslewars.commands.CommandAccept; +import de.steamwar.misslewars.commands.CommandDecline; +import de.steamwar.misslewars.commands.CommandInvite; import de.steamwar.misslewars.commands.CommandSpectate; import de.steamwar.misslewars.countdowns.EndCountdown; import de.steamwar.misslewars.countdowns.ItemCountdown; @@ -29,6 +32,7 @@ import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.GameMode; import org.bukkit.Sound; +import org.bukkit.command.CommandExecutor; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; @@ -62,7 +66,14 @@ public class MissileWars extends JavaPlugin { new FightListener(); new ChatListener(); getCommand("spectate").setExecutor(new CommandSpectate()); - getCommand("invite").setExecutor(new CommandSpectate()); + + // Invitation Commands + CommandInvite commandInvite = new CommandInvite(); + getCommand("invite").setExecutor(commandInvite); + getCommand("invite").setTabCompleter(commandInvite); + + getCommand("accept").setExecutor(new CommandAccept()); + getCommand("decline").setExecutor(new CommandDecline()); new WaitingCountdown(); new ItemCountdown(); @@ -137,6 +148,14 @@ public class MissileWars extends JavaPlugin { return null; } + public static MWTeam getInvitation(Player p) { + if(blueTeam.hasInvite(p)) + return blueTeam; + if(redTeam.hasInvite(p)) + return redTeam; + return null; + } + public static void join(Player p) { if (MissileWars.getRedTeam().size() < MissileWars.getBlueTeam().size()) { MissileWars.getRedTeam().join(p); diff --git a/src/de/steamwar/misslewars/commands/CommandAccept.java b/src/de/steamwar/misslewars/commands/CommandAccept.java new file mode 100644 index 0000000..26383e4 --- /dev/null +++ b/src/de/steamwar/misslewars/commands/CommandAccept.java @@ -0,0 +1,53 @@ +/* + * + * 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.commands; + +import de.steamwar.misslewars.Config; +import de.steamwar.misslewars.MWTeam; +import de.steamwar.misslewars.MissileWars; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class CommandAccept implements CommandExecutor { + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (!(sender instanceof Player)) return false; + Player player = (Player) sender; + if (!Config.isChallenge()) { + player.sendMessage("§cDieser Command ist deaktiviert."); + return false; + } + + MWTeam teamInvitation = MissileWars.getInvitation(player); + if (teamInvitation == null) { + player.sendMessage("§cDu wurdest nicht eingeladen."); + return false; + } + + teamInvitation.acceptInvite(player); + return false; + } + +} diff --git a/src/de/steamwar/misslewars/commands/CommandDecline.java b/src/de/steamwar/misslewars/commands/CommandDecline.java new file mode 100644 index 0000000..e64afab --- /dev/null +++ b/src/de/steamwar/misslewars/commands/CommandDecline.java @@ -0,0 +1,54 @@ +/* + * + * 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.commands; + +import de.steamwar.misslewars.Config; +import de.steamwar.misslewars.MWTeam; +import de.steamwar.misslewars.MissileWars; +import de.steamwar.misslewars.items.Missile; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class CommandDecline implements CommandExecutor { + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (!(sender instanceof Player)) return false; + Player player = (Player) sender; + if (!Config.isChallenge()) { + player.sendMessage("§cDieser Command ist deaktiviert."); + return false; + } + + MWTeam teamInvitation = MissileWars.getInvitation(player); + if (teamInvitation == null) { + player.sendMessage("§cDu wurdest nicht eingeladen."); + return false; + } + + MWTeam.removeInvitations(player); + return false; + } + +} diff --git a/src/de/steamwar/misslewars/commands/CommandInvite.java b/src/de/steamwar/misslewars/commands/CommandInvite.java index 8126ca8..d272fc2 100644 --- a/src/de/steamwar/misslewars/commands/CommandInvite.java +++ b/src/de/steamwar/misslewars/commands/CommandInvite.java @@ -22,12 +22,19 @@ package de.steamwar.misslewars.commands; import de.steamwar.misslewars.Config; +import de.steamwar.misslewars.MissileWars; +import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.command.TabCompleter; import org.bukkit.entity.Player; -public class CommandInvite implements CommandExecutor { +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +public class CommandInvite implements CommandExecutor, TabCompleter { @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { @@ -37,7 +44,48 @@ public class CommandInvite implements CommandExecutor { player.sendMessage("§cDieser Command ist deaktiviert."); return false; } + + if (!Config.RedLeader.equals(player.getUniqueId()) && !Config.BlueLeader.equals(player.getUniqueId())) { + player.sendMessage("§cDu kannst keine Spieler einladen."); + return false; + } + if (MissileWars.getTeam(player) == null) { + player.sendMessage("§cDu kannst keine Spieler einladen."); + return false; + } + + if (args.length != 1) { + player.sendMessage("§c/invite "); + return false; + } + + Player invitedPlayer = Bukkit.getPlayer(args[0]); + if (MissileWars.getTeam(invitedPlayer) != null) { + player.sendMessage("§cDieser Spieler ist bereits in einem Team."); + return false; + } + if (MissileWars.getInvitation(invitedPlayer) != null) { + player.sendMessage("§cDieser Spieler wurde bereits eingeladen."); + return false; + } + + MissileWars.getTeam(player).invitePlayer(invitedPlayer); + invitedPlayer.sendMessage("§7Du wurdest von §6" + player.getName() + "§7 in das Team §6" + MissileWars.getTeam(player).getColoredName() + "§7 eingeladen."); + invitedPlayer.sendMessage("§8/§6accept §8- §7Zum akzeptieren."); + invitedPlayer.sendMessage("§8/§6decline §8- §7Zum ablehnen."); return false; } + @Override + public List onTabComplete(CommandSender sender, Command command, String label, String[] args) { + if (args.length != 1) return new ArrayList<>(); + return Bukkit.getOnlinePlayers() + .stream() + .filter(p -> MissileWars.getTeam(p) != null) + .filter(p -> MissileWars.getInvitation(p) != null) + .map(Player::getName) + .filter(s -> s.startsWith(args[0])) + .collect(Collectors.toList()); + } + } diff --git a/src/de/steamwar/misslewars/listener/ConnectionListener.java b/src/de/steamwar/misslewars/listener/ConnectionListener.java index 7c556ed..549393f 100644 --- a/src/de/steamwar/misslewars/listener/ConnectionListener.java +++ b/src/de/steamwar/misslewars/listener/ConnectionListener.java @@ -20,6 +20,7 @@ package de.steamwar.misslewars.listener; import de.steamwar.misslewars.FightState; +import de.steamwar.misslewars.MWTeam; import de.steamwar.misslewars.MissileWars; import org.bukkit.GameMode; import org.bukkit.event.EventHandler; @@ -42,6 +43,7 @@ public class ConnectionListener extends BasicListener{ @EventHandler public void onLeave(PlayerQuitEvent e) { + MWTeam.removeInvitations(e.getPlayer()); MissileWars.leave(e.getPlayer()); } diff --git a/src/plugin.yml b/src/plugin.yml index c95edb1..b05dc54 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -12,4 +12,6 @@ depend: - SpigotCore commands: spectate: - invite: \ No newline at end of file + invite: + accept: + decline: \ No newline at end of file -- 2.39.2 From c720e463f919537bc4141bdfc29a7f23c0b21b9e Mon Sep 17 00:00:00 2001 From: jojo Date: Sat, 19 Dec 2020 12:04:45 +0100 Subject: [PATCH 19/31] 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; + } + } -- 2.39.2 From f7db2ab377927a79d59dab6e03ecceb0e828d437 Mon Sep 17 00:00:00 2001 From: jojo Date: Sat, 19 Dec 2020 12:29:45 +0100 Subject: [PATCH 20/31] Improve Code Complexity --- src/de/steamwar/misslewars/scripts/Script.java | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/src/de/steamwar/misslewars/scripts/Script.java b/src/de/steamwar/misslewars/scripts/Script.java index 9a3c23e..73e4b67 100644 --- a/src/de/steamwar/misslewars/scripts/Script.java +++ b/src/de/steamwar/misslewars/scripts/Script.java @@ -35,41 +35,31 @@ public class Script { private class ScriptExecutor { private int index = 0; - private final RunnableScriptEvent runnableScriptEvent; public ScriptExecutor(RunnableScriptEvent runnableScriptEvent) { this.runnableScriptEvent = runnableScriptEvent; - } - - public void start() { resume(); } private void resume() { while (index < runnableScriptList.size()) { - RunnableScript runnableScript = runnableScriptList.get(index); + RunnableScript runnableScript = runnableScriptList.get(index++); if (runnableScript instanceof DelayScript) { - index++; - resumeLater(((DelayScript) runnableScript).getDelayTime()); - break; + Bukkit.getScheduler().runTaskLater(MissileWars.getPlugin(), this::resume, ((DelayScript) runnableScript).getDelayTime()); + return; } if (!runnableScript.execute(runnableScriptEvent)) { index = runnableScriptList.size(); return; } - index++; } } - private void resumeLater(int delayTime) { - Bukkit.getScheduler().runTaskLater(MissileWars.getPlugin(), this::resume, delayTime); - } - } public void execute(RunnableScriptEvent runnableScriptEvent) { - new ScriptExecutor(runnableScriptEvent).start(); + new ScriptExecutor(runnableScriptEvent); } private void add(RunnableScript runnableScript) { -- 2.39.2 From 01a422f96d6353a01f05b4c8827889b21ca21581 Mon Sep 17 00:00:00 2001 From: jojo Date: Sat, 19 Dec 2020 12:56:06 +0100 Subject: [PATCH 21/31] Improve Code Complexity --- .../misslewars/scripts/LocationType.java | 27 --------------- .../misslewars/scripts/RunnableScript.java | 4 +-- .../scripts/RunnableScriptEvent.java | 11 ++++--- .../steamwar/misslewars/scripts/Script.java | 6 +--- .../misslewars/scripts/ScriptFunction.java | 26 +++++++++++++++ .../misslewars/scripts/ScriptedItem.java | 26 +++++++-------- .../function/ScriptDoubleFunction.java | 26 --------------- .../scripts/function/ScriptFunction.java | 26 --------------- .../scripts/implemented/DelayScript.java | 2 +- .../scripts/implemented/FilterScript.java | 12 +++---- .../scripts/implemented/LaunchScript.java | 8 ++--- .../scripts/implemented/LocationScript.java | 19 +++++------ .../scripts/implemented/PasteScript.java | 2 +- .../scripts/implemented/PotionScript.java | 2 +- .../scripts/implemented/RemoveScript.java | 2 +- .../scripts/implemented/SoundScript.java | 2 +- .../scripts/implemented/SummonScript.java | 8 ++--- .../misslewars/scripts/utils/EntityUtils.java | 17 ++++++++++ .../misslewars/scripts/utils/JsonUtils.java | 12 +++---- .../scripts/utils/ScriptShortcut.java | 33 ------------------- .../misslewars/scripts/utils/TriConsumer.java | 24 -------------- 21 files changed, 95 insertions(+), 200 deletions(-) delete mode 100644 src/de/steamwar/misslewars/scripts/LocationType.java create mode 100644 src/de/steamwar/misslewars/scripts/ScriptFunction.java delete mode 100644 src/de/steamwar/misslewars/scripts/function/ScriptDoubleFunction.java delete mode 100644 src/de/steamwar/misslewars/scripts/function/ScriptFunction.java delete mode 100644 src/de/steamwar/misslewars/scripts/utils/ScriptShortcut.java delete mode 100644 src/de/steamwar/misslewars/scripts/utils/TriConsumer.java diff --git a/src/de/steamwar/misslewars/scripts/LocationType.java b/src/de/steamwar/misslewars/scripts/LocationType.java deleted file mode 100644 index 5566c56..0000000 --- a/src/de/steamwar/misslewars/scripts/LocationType.java +++ /dev/null @@ -1,27 +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; - -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 df046b7..a5d784d 100644 --- a/src/de/steamwar/misslewars/scripts/RunnableScript.java +++ b/src/de/steamwar/misslewars/scripts/RunnableScript.java @@ -19,6 +19,6 @@ package de.steamwar.misslewars.scripts; -public abstract class RunnableScript { - public abstract boolean execute(RunnableScriptEvent runnableScriptEvent); +public interface RunnableScript { + boolean execute(RunnableScriptEvent runnableScriptEvent); } diff --git a/src/de/steamwar/misslewars/scripts/RunnableScriptEvent.java b/src/de/steamwar/misslewars/scripts/RunnableScriptEvent.java index d303d77..38d1bd4 100644 --- a/src/de/steamwar/misslewars/scripts/RunnableScriptEvent.java +++ b/src/de/steamwar/misslewars/scripts/RunnableScriptEvent.java @@ -25,6 +25,13 @@ import org.bukkit.entity.Player; public class RunnableScriptEvent { + public enum LocationType { + STATIC, + DYNAMIC, + DEFAULT, + CUSTOM + } + public final ScriptedItem.EventType eventType; public final Entity entity; private final Location location; @@ -61,10 +68,6 @@ public class RunnableScriptEvent { 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 73e4b67..0887c54 100644 --- a/src/de/steamwar/misslewars/scripts/Script.java +++ b/src/de/steamwar/misslewars/scripts/Script.java @@ -62,16 +62,12 @@ public class Script { new ScriptExecutor(runnableScriptEvent); } - private void add(RunnableScript runnableScript) { - runnableScriptList.add(runnableScript); - } - 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); + script.runnableScriptList.add(runnableScript); }); return script; } diff --git a/src/de/steamwar/misslewars/scripts/ScriptFunction.java b/src/de/steamwar/misslewars/scripts/ScriptFunction.java new file mode 100644 index 0000000..18685f8 --- /dev/null +++ b/src/de/steamwar/misslewars/scripts/ScriptFunction.java @@ -0,0 +1,26 @@ +/* + * + * 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; + +public interface ScriptFunction { + boolean execute(RunnableScriptEvent runnableScriptEvent, double... doubles); +} diff --git a/src/de/steamwar/misslewars/scripts/ScriptedItem.java b/src/de/steamwar/misslewars/scripts/ScriptedItem.java index de7bf3d..8f6aa75 100644 --- a/src/de/steamwar/misslewars/scripts/ScriptedItem.java +++ b/src/de/steamwar/misslewars/scripts/ScriptedItem.java @@ -20,6 +20,7 @@ package de.steamwar.misslewars.scripts; import com.google.gson.JsonObject; +import de.steamwar.misslewars.scripts.utils.JsonUtils; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.entity.Entity; @@ -31,6 +32,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import static de.steamwar.misslewars.scripts.utils.JsonUtils.getString; + public class ScriptedItem { // "type": Material name (STRING) @@ -43,27 +46,20 @@ public class ScriptedItem { // - onThrow public enum EventType { - onHit("onHit"), - onThrow("onThrow"), - onClick("onClick"); - - String name; - - EventType(String name) { - this.name = name; - } + onHit, + onThrow, + onClick } private Map scriptMap = new HashMap<>(); - private String entityName = "---"; + private String entityName = ""; private ItemStack itemStack; public ScriptedItem(JsonObject jsonObject) { itemStack = createItemStack(jsonObject); - if (jsonObject.has("entityName")) - entityName = jsonObject.get("entityName").getAsString(); + getString(jsonObject, "entityName", string -> entityName = string); for (EventType eventType : EventType.values()) { String eventString = "EVENT." + eventType.name(); @@ -72,11 +68,11 @@ public class ScriptedItem { } } - private ItemStack createItemStack(JsonObject jsonObject) { - ItemStack itemStack = new ItemStack(Material.valueOf(jsonObject.getAsJsonPrimitive("type").getAsString()), jsonObject.getAsJsonPrimitive("amount").getAsInt()); + private static ItemStack createItemStack(JsonObject jsonObject) { + ItemStack itemStack = new ItemStack(Material.valueOf(getString(jsonObject, "type", "")), JsonUtils.getInt(jsonObject, "amount", 1)); ItemMeta itemMeta = itemStack.getItemMeta(); if (itemMeta == null) return itemStack; - itemMeta.setDisplayName(jsonObject.getAsJsonPrimitive("name").getAsString()); + getString(jsonObject, "name", itemMeta::setDisplayName); if (jsonObject.has("lore")) { List lore = new ArrayList<>(); diff --git a/src/de/steamwar/misslewars/scripts/function/ScriptDoubleFunction.java b/src/de/steamwar/misslewars/scripts/function/ScriptDoubleFunction.java deleted file mode 100644 index abafda1..0000000 --- a/src/de/steamwar/misslewars/scripts/function/ScriptDoubleFunction.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 ScriptDoubleFunction { - boolean execute(RunnableScriptEvent runnableScriptEvent, double... doubles); -} diff --git a/src/de/steamwar/misslewars/scripts/function/ScriptFunction.java b/src/de/steamwar/misslewars/scripts/function/ScriptFunction.java deleted file mode 100644 index 9874e01..0000000 --- a/src/de/steamwar/misslewars/scripts/function/ScriptFunction.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 ScriptFunction { - boolean execute(RunnableScriptEvent runnableScriptEvent); -} diff --git a/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java b/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java index 744f616..4ba8c25 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java @@ -28,7 +28,7 @@ import de.steamwar.misslewars.scripts.RunnableScriptEvent; import java.util.HashMap; import java.util.Map; -public class DelayScript extends RunnableScript { +public class DelayScript implements RunnableScript { private static final Map delayMap = new HashMap<>(); diff --git a/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java b/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java index e89899d..a65ba13 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java @@ -23,8 +23,7 @@ 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.ScriptFunction; -import de.steamwar.misslewars.scripts.utils.JsonUtils; +import de.steamwar.misslewars.scripts.ScriptFunction; import org.bukkit.Location; import java.util.HashMap; @@ -33,12 +32,12 @@ 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 { +public class FilterScript implements RunnableScript { private static final Map filterMap = new HashMap<>(); static { - filterMap.put("nearportal", runnableScriptEvent -> { + filterMap.put("nearportal", (runnableScriptEvent, doubles) -> { Location location = runnableScriptEvent.getLocation(); int bz = MissileWars.getBlueTeam().getPortalZ(); int rz = MissileWars.getRedTeam().getPortalZ(); @@ -48,7 +47,7 @@ public class FilterScript extends RunnableScript { if (offset > 0) return (blockZ > bz - offset) || (blockZ < rz + offset); else return (blockZ < bz - offset) || (blockZ > rz + offset); }); - filterMap.put("nearspawn", runnableScriptEvent -> { + filterMap.put("nearspawn", (runnableScriptEvent, doubles) -> { Location location = runnableScriptEvent.getLocation(); return MissileWars.getBlueTeam().getSpawn().distance(location) < 3 || MissileWars.getRedTeam().getSpawn().distance(location) < 3; }); @@ -65,8 +64,7 @@ public class FilterScript extends RunnableScript { @Override public boolean execute(RunnableScriptEvent runnableScriptEvent) { if (filter == null) return true; - if (inverted) return !filter.execute(runnableScriptEvent); - else return filter.execute(runnableScriptEvent); + return filter.execute(runnableScriptEvent) ^ inverted; } } diff --git a/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java b/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java index 550ef70..6e29ad1 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java @@ -23,12 +23,12 @@ 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.ScriptFunction; +import de.steamwar.misslewars.scripts.ScriptFunction; import de.steamwar.misslewars.scripts.utils.EntityUtils; -import de.steamwar.misslewars.scripts.utils.ScriptShortcut; +import de.steamwar.misslewars.scripts.utils.EntityUtils.ScriptShortcut; import org.bukkit.entity.Projectile; -public class LaunchScript extends RunnableScript { +public class LaunchScript implements RunnableScript { private ScriptFunction launch = null; @@ -36,7 +36,7 @@ public class LaunchScript extends RunnableScript { ScriptShortcut scriptShortcut = EntityUtils.getEntity(launch.getAsJsonPrimitive("entity").getAsString(), EntityUtils.EntityType.Projectile); if (scriptShortcut == null) return; - this.launch = runnableScriptEvent -> { + this.launch = (runnableScriptEvent, doubles) -> { Projectile projectile = runnableScriptEvent.getPlayer().launchProjectile(scriptShortcut.entityClass); scriptShortcut.consumer.accept(launch, projectile, runnableScriptEvent); return false; diff --git a/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java b/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java index 5fa280a..aad7ea5 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java @@ -20,11 +20,10 @@ 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 de.steamwar.misslewars.scripts.function.ScriptDoubleFunction; -import de.steamwar.misslewars.scripts.utils.JsonUtils; +import de.steamwar.misslewars.scripts.RunnableScriptEvent.LocationType; +import de.steamwar.misslewars.scripts.ScriptFunction; import org.bukkit.Location; import java.util.HashMap; @@ -33,10 +32,10 @@ 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 { +public class LocationScript implements RunnableScript { private static final Map locationTypeMap = new HashMap<>(); - private static final Map locationMap = new HashMap<>(); + private static final Map locationMap = new HashMap<>(); static { locationTypeMap.put("static", LocationType.STATIC); @@ -47,16 +46,16 @@ public class LocationScript extends RunnableScript { locationMap.put("offsetentity", (runnableScriptEvent, doubles) -> { 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]); + runnableScriptEvent.setCustomLocation(location1.getX() + doubles[0], location1.getY() + doubles[1], location1.getZ() + doubles[2], 0, 0); 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]); + runnableScriptEvent.setCustomLocation(location1.getX() + doubles[0], location1.getY() + doubles[1], location1.getZ() + doubles[2], 0, 0); return false; }); - ScriptDoubleFunction absoluteLocation = (runnableScriptEvent, doubles) -> { - runnableScriptEvent.setCustomLocation(doubles[0], doubles[1], doubles[2]); + ScriptFunction absoluteLocation = (runnableScriptEvent, doubles) -> { + runnableScriptEvent.setCustomLocation(doubles[0], doubles[1], doubles[2], 0, 0); return false; }; locationMap.put("absolute", absoluteLocation); @@ -65,7 +64,7 @@ public class LocationScript extends RunnableScript { } private LocationType locationType = null; - private ScriptDoubleFunction locationExecutor = null; + private ScriptFunction locationExecutor = null; private double x, y, z = 0; diff --git a/src/de/steamwar/misslewars/scripts/implemented/PasteScript.java b/src/de/steamwar/misslewars/scripts/implemented/PasteScript.java index e4c81ba..db1b2cb 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/PasteScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/PasteScript.java @@ -43,7 +43,7 @@ import java.util.Objects; import static de.steamwar.misslewars.scripts.utils.JsonUtils.getBoolean; -public class PasteScript extends RunnableScript { +public class PasteScript implements RunnableScript { private static final World world = new BukkitWorld(Bukkit.getWorlds().get(0)); diff --git a/src/de/steamwar/misslewars/scripts/implemented/PotionScript.java b/src/de/steamwar/misslewars/scripts/implemented/PotionScript.java index b8de2b3..34777dc 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/PotionScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/PotionScript.java @@ -29,7 +29,7 @@ import org.bukkit.potion.PotionEffectType; import static de.steamwar.misslewars.scripts.utils.JsonUtils.getBoolean; import static de.steamwar.misslewars.scripts.utils.JsonUtils.getInt; -public class PotionScript extends RunnableScript { +public class PotionScript implements RunnableScript { private PotionEffect potionEffect = null; diff --git a/src/de/steamwar/misslewars/scripts/implemented/RemoveScript.java b/src/de/steamwar/misslewars/scripts/implemented/RemoveScript.java index 6f2f3f1..ede587e 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 extends RunnableScript { +public class RemoveScript implements 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 fd30196..f7f9455 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/SoundScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/SoundScript.java @@ -27,7 +27,7 @@ import de.steamwar.misslewars.scripts.utils.JsonUtils; import org.bukkit.Sound; import org.bukkit.entity.Player; -public class SoundScript extends RunnableScript { +public class SoundScript implements RunnableScript { private Sound sound; private float volume; diff --git a/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java b/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java index bc9cba4..f2db2be 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java @@ -22,12 +22,12 @@ 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.ScriptFunction; +import de.steamwar.misslewars.scripts.ScriptFunction; import de.steamwar.misslewars.scripts.utils.EntityUtils; -import de.steamwar.misslewars.scripts.utils.ScriptShortcut; +import de.steamwar.misslewars.scripts.utils.EntityUtils.ScriptShortcut; import org.bukkit.entity.Entity; -public class SummonScript extends RunnableScript { +public class SummonScript implements RunnableScript { private ScriptFunction summon = null; @@ -35,7 +35,7 @@ public class SummonScript extends RunnableScript { ScriptShortcut scriptShortcut = EntityUtils.getEntity(summon.getAsJsonPrimitive("entity").getAsString(), EntityUtils.EntityType.Normal); if (scriptShortcut == null) return; - this.summon = runnableScriptEvent -> { + this.summon = (runnableScriptEvent, doubles) -> { Entity entity = runnableScriptEvent.entity.getWorld().spawn(runnableScriptEvent.getLocation(), scriptShortcut.entityClass); scriptShortcut.consumer.accept(summon, entity, runnableScriptEvent); return false; diff --git a/src/de/steamwar/misslewars/scripts/utils/EntityUtils.java b/src/de/steamwar/misslewars/scripts/utils/EntityUtils.java index af70bda..823751e 100644 --- a/src/de/steamwar/misslewars/scripts/utils/EntityUtils.java +++ b/src/de/steamwar/misslewars/scripts/utils/EntityUtils.java @@ -20,6 +20,7 @@ package de.steamwar.misslewars.scripts.utils; import com.google.gson.JsonObject; +import de.steamwar.misslewars.scripts.RunnableScriptEvent; import org.bukkit.entity.*; import static de.steamwar.misslewars.scripts.utils.JsonUtils.*; @@ -79,4 +80,20 @@ public class EntityUtils { return null; } + public static class ScriptShortcut { + + public Class entityClass; + public TriConsumer consumer; + + public ScriptShortcut(Class entityClass, TriConsumer consumer) { + this.entityClass = entityClass; + this.consumer = consumer; + } + + } + + public interface TriConsumer { + void accept(T t, R r, K k); + } + } diff --git a/src/de/steamwar/misslewars/scripts/utils/JsonUtils.java b/src/de/steamwar/misslewars/scripts/utils/JsonUtils.java index 1f9b8c1..f0a703a 100644 --- a/src/de/steamwar/misslewars/scripts/utils/JsonUtils.java +++ b/src/de/steamwar/misslewars/scripts/utils/JsonUtils.java @@ -13,23 +13,19 @@ public class JsonUtils { } public static boolean getBoolean(JsonObject jsonObject, String key, boolean defaultValue) { - if (jsonObject.has(key)) return jsonObject.getAsJsonPrimitive(key).getAsBoolean(); - return defaultValue; + return jsonObject.has(key) ? jsonObject.getAsJsonPrimitive(key).getAsBoolean() : defaultValue; } public static int getInt(JsonObject jsonObject, String key, int defaultValue) { - if (jsonObject.has(key)) return jsonObject.getAsJsonPrimitive(key).getAsInt(); - return defaultValue; + return jsonObject.has(key) ? jsonObject.getAsJsonPrimitive(key).getAsInt() : defaultValue; } public static float getFloat(JsonObject jsonObject, String key, float defaultValue) { - if (jsonObject.has(key)) return jsonObject.getAsJsonPrimitive(key).getAsFloat(); - return defaultValue; + return jsonObject.has(key) ? jsonObject.getAsJsonPrimitive(key).getAsFloat() : defaultValue; } public static String getString(JsonObject jsonObject, String key, String defaultValue) { - if (jsonObject.has(key)) return jsonObject.getAsJsonPrimitive(key).getAsString(); - return defaultValue; + return jsonObject.has(key) ? jsonObject.getAsJsonPrimitive(key).getAsString() : defaultValue; } public static void getBoolean(JsonObject jsonObject, String key, Consumer booleanConsumer) { diff --git a/src/de/steamwar/misslewars/scripts/utils/ScriptShortcut.java b/src/de/steamwar/misslewars/scripts/utils/ScriptShortcut.java deleted file mode 100644 index 1dfcd4e..0000000 --- a/src/de/steamwar/misslewars/scripts/utils/ScriptShortcut.java +++ /dev/null @@ -1,33 +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.utils; - -import com.google.gson.JsonObject; -import de.steamwar.misslewars.scripts.RunnableScriptEvent; - -public class ScriptShortcut { - public Class entityClass; - public TriConsumer consumer; - - public ScriptShortcut(Class entityClass, TriConsumer consumer) { - this.entityClass = entityClass; - this.consumer = consumer; - } -} diff --git a/src/de/steamwar/misslewars/scripts/utils/TriConsumer.java b/src/de/steamwar/misslewars/scripts/utils/TriConsumer.java deleted file mode 100644 index 7cc9718..0000000 --- a/src/de/steamwar/misslewars/scripts/utils/TriConsumer.java +++ /dev/null @@ -1,24 +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.utils; - -public interface TriConsumer { - void accept(T t, R r, K k); -} -- 2.39.2 From f1e19126a991e4f1c7a1b0242f72e84439c7bd46 Mon Sep 17 00:00:00 2001 From: jojo Date: Sat, 19 Dec 2020 13:04:49 +0100 Subject: [PATCH 22/31] Improve Code Complexity --- .../misslewars/scripts/RunnableScript.java | 4 +++ .../scripts/RunnableScriptEvent.java | 5 +--- .../misslewars/scripts/ScriptFunction.java | 26 ------------------- .../scripts/implemented/FilterScript.java | 1 - .../scripts/implemented/LaunchScript.java | 1 - .../scripts/implemented/LocationScript.java | 1 - .../scripts/implemented/SummonScript.java | 1 - 7 files changed, 5 insertions(+), 34 deletions(-) delete mode 100644 src/de/steamwar/misslewars/scripts/ScriptFunction.java diff --git a/src/de/steamwar/misslewars/scripts/RunnableScript.java b/src/de/steamwar/misslewars/scripts/RunnableScript.java index a5d784d..4f9f688 100644 --- a/src/de/steamwar/misslewars/scripts/RunnableScript.java +++ b/src/de/steamwar/misslewars/scripts/RunnableScript.java @@ -21,4 +21,8 @@ package de.steamwar.misslewars.scripts; public interface RunnableScript { boolean execute(RunnableScriptEvent runnableScriptEvent); + + interface ScriptFunction { + boolean execute(RunnableScriptEvent runnableScriptEvent, double... doubles); + } } diff --git a/src/de/steamwar/misslewars/scripts/RunnableScriptEvent.java b/src/de/steamwar/misslewars/scripts/RunnableScriptEvent.java index 38d1bd4..8758835 100644 --- a/src/de/steamwar/misslewars/scripts/RunnableScriptEvent.java +++ b/src/de/steamwar/misslewars/scripts/RunnableScriptEvent.java @@ -52,10 +52,7 @@ public class RunnableScriptEvent { 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; - } + if (locationType == LocationType.DYNAMIC) return entity != null ? entity.getLocation() : location; // Default Location is static if EventType is onClick otherwise dynamic if (eventType == ScriptedItem.EventType.onClick) return location; diff --git a/src/de/steamwar/misslewars/scripts/ScriptFunction.java b/src/de/steamwar/misslewars/scripts/ScriptFunction.java deleted file mode 100644 index 18685f8..0000000 --- a/src/de/steamwar/misslewars/scripts/ScriptFunction.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; - -public interface ScriptFunction { - boolean execute(RunnableScriptEvent runnableScriptEvent, double... doubles); -} diff --git a/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java b/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java index a65ba13..85a6de2 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java @@ -23,7 +23,6 @@ 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.ScriptFunction; import org.bukkit.Location; import java.util.HashMap; diff --git a/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java b/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java index 6e29ad1..24481a0 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java @@ -23,7 +23,6 @@ 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.ScriptFunction; import de.steamwar.misslewars.scripts.utils.EntityUtils; import de.steamwar.misslewars.scripts.utils.EntityUtils.ScriptShortcut; import org.bukkit.entity.Projectile; diff --git a/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java b/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java index aad7ea5..af2dfaa 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java @@ -23,7 +23,6 @@ import com.google.gson.JsonObject; import de.steamwar.misslewars.scripts.RunnableScript; import de.steamwar.misslewars.scripts.RunnableScriptEvent; import de.steamwar.misslewars.scripts.RunnableScriptEvent.LocationType; -import de.steamwar.misslewars.scripts.ScriptFunction; import org.bukkit.Location; import java.util.HashMap; diff --git a/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java b/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java index f2db2be..84cbe13 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java @@ -22,7 +22,6 @@ 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.ScriptFunction; import de.steamwar.misslewars.scripts.utils.EntityUtils; import de.steamwar.misslewars.scripts.utils.EntityUtils.ScriptShortcut; import org.bukkit.entity.Entity; -- 2.39.2 From 08ebd8c7f3c74639682036265d71eb862028319f Mon Sep 17 00:00:00 2001 From: jojo Date: Sat, 19 Dec 2020 13:40:20 +0100 Subject: [PATCH 23/31] Improve Code Complexity --- .../misslewars/scripts/RunnableScriptEvent.java | 6 ++++++ src/de/steamwar/misslewars/scripts/Script.java | 17 ++++------------- .../scripts/implemented/DelayScript.java | 10 +++++----- .../scripts/implemented/LocationScript.java | 4 +--- 4 files changed, 16 insertions(+), 21 deletions(-) diff --git a/src/de/steamwar/misslewars/scripts/RunnableScriptEvent.java b/src/de/steamwar/misslewars/scripts/RunnableScriptEvent.java index 8758835..8459f55 100644 --- a/src/de/steamwar/misslewars/scripts/RunnableScriptEvent.java +++ b/src/de/steamwar/misslewars/scripts/RunnableScriptEvent.java @@ -37,6 +37,7 @@ public class RunnableScriptEvent { private final Location location; private Location customLocation; private LocationType locationType = LocationType.DEFAULT; + Script.ScriptExecutor scriptExecutor = null; public RunnableScriptEvent(ScriptedItem.EventType eventType, Entity entity, Location location) { this.eventType = eventType; @@ -73,4 +74,9 @@ public class RunnableScriptEvent { return (Player) entity; } + public void resumeScriptExecution() { + if (scriptExecutor == null) return; + scriptExecutor.resume(); + } + } diff --git a/src/de/steamwar/misslewars/scripts/Script.java b/src/de/steamwar/misslewars/scripts/Script.java index 0887c54..8534c34 100644 --- a/src/de/steamwar/misslewars/scripts/Script.java +++ b/src/de/steamwar/misslewars/scripts/Script.java @@ -21,9 +21,7 @@ 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.*; -import org.bukkit.Bukkit; import java.util.ArrayList; import java.util.List; @@ -32,27 +30,20 @@ public class Script { private List runnableScriptList = new ArrayList<>(); - private class ScriptExecutor { + class ScriptExecutor { private int index = 0; private final RunnableScriptEvent runnableScriptEvent; public ScriptExecutor(RunnableScriptEvent runnableScriptEvent) { this.runnableScriptEvent = runnableScriptEvent; + runnableScriptEvent.scriptExecutor = this; resume(); } - private void resume() { + void resume() { while (index < runnableScriptList.size()) { - RunnableScript runnableScript = runnableScriptList.get(index++); - if (runnableScript instanceof DelayScript) { - Bukkit.getScheduler().runTaskLater(MissileWars.getPlugin(), this::resume, ((DelayScript) runnableScript).getDelayTime()); - return; - } - if (!runnableScript.execute(runnableScriptEvent)) { - index = runnableScriptList.size(); - return; - } + if (!runnableScriptList.get(index++).execute(runnableScriptEvent)) return; } } diff --git a/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java b/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java index 4ba8c25..5698f88 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java @@ -22,8 +22,11 @@ package de.steamwar.misslewars.scripts.implemented; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; import de.steamwar.misslewars.Config; +import de.steamwar.misslewars.MissileWars; import de.steamwar.misslewars.scripts.RunnableScript; import de.steamwar.misslewars.scripts.RunnableScriptEvent; +import org.bukkit.Bukkit; +import org.bukkit.scheduler.BukkitTask; import java.util.HashMap; import java.util.Map; @@ -53,11 +56,8 @@ public class DelayScript implements RunnableScript { @Override public boolean execute(RunnableScriptEvent runnableScriptEvent) { - return true; - } - - public int getDelayTime() { - return delayTime; + Bukkit.getScheduler().runTaskLater(MissileWars.getPlugin(), runnableScriptEvent::resumeScriptExecution, delayTime); + return false; } } diff --git a/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java b/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java index af2dfaa..68959c8 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java @@ -83,9 +83,7 @@ public class LocationScript implements RunnableScript { @Override public boolean execute(RunnableScriptEvent runnableScriptEvent) { runnableScriptEvent.setLocationType(locationType); - if (locationExecutor != null) { - locationExecutor.execute(runnableScriptEvent, x, y, z); - } + if (locationExecutor != null) locationExecutor.execute(runnableScriptEvent, x, y, z); return true; } -- 2.39.2 From cc8dcefb9741b533f4545e9ded05770e1552e97b Mon Sep 17 00:00:00 2001 From: jojo Date: Sat, 19 Dec 2020 13:43:21 +0100 Subject: [PATCH 24/31] Optimize Imports --- src/de/steamwar/misslewars/scripts/implemented/DelayScript.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java b/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java index 5698f88..f77b985 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/DelayScript.java @@ -26,7 +26,6 @@ import de.steamwar.misslewars.MissileWars; import de.steamwar.misslewars.scripts.RunnableScript; import de.steamwar.misslewars.scripts.RunnableScriptEvent; import org.bukkit.Bukkit; -import org.bukkit.scheduler.BukkitTask; import java.util.HashMap; import java.util.Map; -- 2.39.2 From c15f4a90dc831091dfa1366ecbdeb7486dfaf15f Mon Sep 17 00:00:00 2001 From: jojo Date: Sat, 19 Dec 2020 13:50:03 +0100 Subject: [PATCH 25/31] Optimize CommandInvite Optimize CommandInviteHandler --- src/de/steamwar/misslewars/MissileWars.java | 8 ++- .../misslewars/commands/CommandDecline.java | 54 ------------------- .../misslewars/commands/CommandInvite.java | 15 +++--- ...dAccept.java => CommandInviteHandler.java} | 8 ++- 4 files changed, 18 insertions(+), 67 deletions(-) delete mode 100644 src/de/steamwar/misslewars/commands/CommandDecline.java rename src/de/steamwar/misslewars/commands/{CommandAccept.java => CommandInviteHandler.java} (88%) diff --git a/src/de/steamwar/misslewars/MissileWars.java b/src/de/steamwar/misslewars/MissileWars.java index f28d455..e97696b 100644 --- a/src/de/steamwar/misslewars/MissileWars.java +++ b/src/de/steamwar/misslewars/MissileWars.java @@ -19,8 +19,7 @@ package de.steamwar.misslewars; -import de.steamwar.misslewars.commands.CommandAccept; -import de.steamwar.misslewars.commands.CommandDecline; +import de.steamwar.misslewars.commands.CommandInviteHandler; import de.steamwar.misslewars.commands.CommandInvite; import de.steamwar.misslewars.commands.CommandSpectate; import de.steamwar.misslewars.countdowns.EndCountdown; @@ -32,7 +31,6 @@ import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.GameMode; import org.bukkit.Sound; -import org.bukkit.command.CommandExecutor; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; @@ -72,8 +70,8 @@ public class MissileWars extends JavaPlugin { getCommand("invite").setExecutor(commandInvite); getCommand("invite").setTabCompleter(commandInvite); - getCommand("accept").setExecutor(new CommandAccept()); - getCommand("decline").setExecutor(new CommandDecline()); + getCommand("accept").setExecutor(new CommandInviteHandler()); + getCommand("decline").setExecutor(new CommandInviteHandler()); new WaitingCountdown(); new ItemCountdown(); diff --git a/src/de/steamwar/misslewars/commands/CommandDecline.java b/src/de/steamwar/misslewars/commands/CommandDecline.java deleted file mode 100644 index e64afab..0000000 --- a/src/de/steamwar/misslewars/commands/CommandDecline.java +++ /dev/null @@ -1,54 +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.commands; - -import de.steamwar.misslewars.Config; -import de.steamwar.misslewars.MWTeam; -import de.steamwar.misslewars.MissileWars; -import de.steamwar.misslewars.items.Missile; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class CommandDecline implements CommandExecutor { - - @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - if (!(sender instanceof Player)) return false; - Player player = (Player) sender; - if (!Config.isChallenge()) { - player.sendMessage("§cDieser Command ist deaktiviert."); - return false; - } - - MWTeam teamInvitation = MissileWars.getInvitation(player); - if (teamInvitation == null) { - player.sendMessage("§cDu wurdest nicht eingeladen."); - return false; - } - - MWTeam.removeInvitations(player); - return false; - } - -} diff --git a/src/de/steamwar/misslewars/commands/CommandInvite.java b/src/de/steamwar/misslewars/commands/CommandInvite.java index d272fc2..b869f13 100644 --- a/src/de/steamwar/misslewars/commands/CommandInvite.java +++ b/src/de/steamwar/misslewars/commands/CommandInvite.java @@ -22,7 +22,9 @@ package de.steamwar.misslewars.commands; import de.steamwar.misslewars.Config; +import de.steamwar.misslewars.MWTeam; import de.steamwar.misslewars.MissileWars; +import de.steamwar.misslewars.items.Missile; import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -45,11 +47,8 @@ public class CommandInvite implements CommandExecutor, TabCompleter { return false; } - if (!Config.RedLeader.equals(player.getUniqueId()) && !Config.BlueLeader.equals(player.getUniqueId())) { - player.sendMessage("§cDu kannst keine Spieler einladen."); - return false; - } - if (MissileWars.getTeam(player) == null) { + MWTeam team = MissileWars.getTeam(player); + if (!Config.RedLeader.equals(player.getUniqueId()) && !Config.BlueLeader.equals(player.getUniqueId()) || team == null) { player.sendMessage("§cDu kannst keine Spieler einladen."); return false; } @@ -60,6 +59,10 @@ public class CommandInvite implements CommandExecutor, TabCompleter { } Player invitedPlayer = Bukkit.getPlayer(args[0]); + if (invitedPlayer == null) { + player.sendMessage("§cDieser Spieler ist nicht online."); + return false; + } if (MissileWars.getTeam(invitedPlayer) != null) { player.sendMessage("§cDieser Spieler ist bereits in einem Team."); return false; @@ -69,7 +72,7 @@ public class CommandInvite implements CommandExecutor, TabCompleter { return false; } - MissileWars.getTeam(player).invitePlayer(invitedPlayer); + team.invitePlayer(invitedPlayer); invitedPlayer.sendMessage("§7Du wurdest von §6" + player.getName() + "§7 in das Team §6" + MissileWars.getTeam(player).getColoredName() + "§7 eingeladen."); invitedPlayer.sendMessage("§8/§6accept §8- §7Zum akzeptieren."); invitedPlayer.sendMessage("§8/§6decline §8- §7Zum ablehnen."); diff --git a/src/de/steamwar/misslewars/commands/CommandAccept.java b/src/de/steamwar/misslewars/commands/CommandInviteHandler.java similarity index 88% rename from src/de/steamwar/misslewars/commands/CommandAccept.java rename to src/de/steamwar/misslewars/commands/CommandInviteHandler.java index 26383e4..4e2a9fb 100644 --- a/src/de/steamwar/misslewars/commands/CommandAccept.java +++ b/src/de/steamwar/misslewars/commands/CommandInviteHandler.java @@ -29,7 +29,7 @@ import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -public class CommandAccept implements CommandExecutor { +public class CommandInviteHandler implements CommandExecutor { @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { @@ -46,7 +46,11 @@ public class CommandAccept implements CommandExecutor { return false; } - teamInvitation.acceptInvite(player); + if (command.getName().equals("accept")) { + teamInvitation.acceptInvite(player); + } else { + MWTeam.removeInvitations(player); + } return false; } -- 2.39.2 From cb3e1d5c711d36b8a259fe2a6256725fadf36077 Mon Sep 17 00:00:00 2001 From: jojo Date: Sat, 19 Dec 2020 14:08:08 +0100 Subject: [PATCH 26/31] Optimize CodeStyle --- .../misslewars/scripts/RunnableScript.java | 7 +++++++ src/de/steamwar/misslewars/scripts/Script.java | 3 +-- .../scripts/implemented/FilterScript.java | 3 +-- .../scripts/implemented/LaunchScript.java | 4 +--- .../scripts/implemented/LocationScript.java | 3 +-- .../scripts/implemented/SoundScript.java | 14 +++++++------- .../scripts/implemented/SummonScript.java | 4 +--- 7 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/de/steamwar/misslewars/scripts/RunnableScript.java b/src/de/steamwar/misslewars/scripts/RunnableScript.java index 4f9f688..5328bc3 100644 --- a/src/de/steamwar/misslewars/scripts/RunnableScript.java +++ b/src/de/steamwar/misslewars/scripts/RunnableScript.java @@ -19,10 +19,17 @@ package de.steamwar.misslewars.scripts; +import java.util.function.UnaryOperator; + public interface RunnableScript { boolean execute(RunnableScriptEvent runnableScriptEvent); interface ScriptFunction { boolean execute(RunnableScriptEvent runnableScriptEvent, double... doubles); } + + default boolean defaultExecution(ScriptFunction scriptFunction, boolean nullReturn, UnaryOperator returnValue, RunnableScriptEvent runnableScriptEvent, double... doubles) { + if (scriptFunction == null) return nullReturn; + return returnValue.apply(scriptFunction.execute(runnableScriptEvent, doubles)); + } } diff --git a/src/de/steamwar/misslewars/scripts/Script.java b/src/de/steamwar/misslewars/scripts/Script.java index 8534c34..0b8ac11 100644 --- a/src/de/steamwar/misslewars/scripts/Script.java +++ b/src/de/steamwar/misslewars/scripts/Script.java @@ -64,8 +64,7 @@ public class Script { } private static RunnableScript parseScriptSnippet(JsonObject jsonObject) { - if (!jsonObject.has("type")) - return null; + if (!jsonObject.has("type")) return null; switch (jsonObject.getAsJsonPrimitive("type").getAsString().toLowerCase()) { case "delay": return new DelayScript(jsonObject); diff --git a/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java b/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java index 85a6de2..4cf250c 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/FilterScript.java @@ -62,8 +62,7 @@ public class FilterScript implements RunnableScript { @Override public boolean execute(RunnableScriptEvent runnableScriptEvent) { - if (filter == null) return true; - return filter.execute(runnableScriptEvent) ^ inverted; + return defaultExecution(filter, true, b -> b ^ inverted, runnableScriptEvent); } } diff --git a/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java b/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java index 24481a0..83b44ed 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/LaunchScript.java @@ -44,10 +44,8 @@ public class LaunchScript implements RunnableScript { @Override public boolean execute(RunnableScriptEvent runnableScriptEvent) { - if (launch == null) return false; if (runnableScriptEvent.eventType != ScriptedItem.EventType.onClick) return true; - launch.execute(runnableScriptEvent); - return true; + return defaultExecution(launch, false, b -> true, runnableScriptEvent); } } diff --git a/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java b/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java index 68959c8..1fb272e 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/LocationScript.java @@ -83,8 +83,7 @@ public class LocationScript implements RunnableScript { @Override public boolean execute(RunnableScriptEvent runnableScriptEvent) { runnableScriptEvent.setLocationType(locationType); - if (locationExecutor != null) locationExecutor.execute(runnableScriptEvent, x, y, z); - return true; + return defaultExecution(locationExecutor, true, b -> true, runnableScriptEvent, x, y, z); } } diff --git a/src/de/steamwar/misslewars/scripts/implemented/SoundScript.java b/src/de/steamwar/misslewars/scripts/implemented/SoundScript.java index f7f9455..32e40bb 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/SoundScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/SoundScript.java @@ -23,9 +23,10 @@ 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; + +import static de.steamwar.misslewars.scripts.utils.JsonUtils.getFloat; +import static de.steamwar.misslewars.scripts.utils.JsonUtils.getString; public class SoundScript implements RunnableScript { @@ -34,17 +35,16 @@ public class SoundScript implements RunnableScript { private float pitch; public SoundScript(JsonObject sound) { - JsonUtils.getString(sound, "sound", value -> this.sound = Sound.valueOf(value)); - volume = JsonUtils.getFloat(sound, "volume", 100); - pitch = JsonUtils.getFloat(sound, "pitch", 1); + getString(sound, "sound", value -> this.sound = Sound.valueOf(value)); + volume = getFloat(sound, "volume", 100); + pitch = getFloat(sound, "pitch", 1); } @Override public boolean execute(RunnableScriptEvent runnableScriptEvent) { if (sound == null) return false; if (runnableScriptEvent.eventType != ScriptedItem.EventType.onClick) return true; - Player player = runnableScriptEvent.getPlayer(); - player.playSound(player.getLocation(), sound, volume, pitch); + runnableScriptEvent.getPlayer().playSound(runnableScriptEvent.getPlayer().getLocation(), sound, volume, pitch); return true; } diff --git a/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java b/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java index 84cbe13..a85ea3a 100644 --- a/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java +++ b/src/de/steamwar/misslewars/scripts/implemented/SummonScript.java @@ -43,9 +43,7 @@ public class SummonScript implements RunnableScript { @Override public boolean execute(RunnableScriptEvent runnableScriptEvent) { - if (summon == null) return false; - summon.execute(runnableScriptEvent); - return true; + return defaultExecution(summon, false, b -> true, runnableScriptEvent); } } -- 2.39.2 From 5120249d57004290a2046a0cecccb644a8cd9a17 Mon Sep 17 00:00:00 2001 From: jojo Date: Sat, 19 Dec 2020 14:30:34 +0100 Subject: [PATCH 27/31] Fix JoinListener --- src/de/steamwar/misslewars/listener/JoinListener.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/de/steamwar/misslewars/listener/JoinListener.java b/src/de/steamwar/misslewars/listener/JoinListener.java index 83a9bd7..e9c648a 100644 --- a/src/de/steamwar/misslewars/listener/JoinListener.java +++ b/src/de/steamwar/misslewars/listener/JoinListener.java @@ -19,6 +19,7 @@ package de.steamwar.misslewars.listener; +import de.steamwar.misslewars.Config; import de.steamwar.misslewars.FightState; import de.steamwar.misslewars.MissileWars; import org.bukkit.event.EventHandler; @@ -35,6 +36,7 @@ public class JoinListener extends BasicListener { @EventHandler(priority = EventPriority.HIGHEST) public void onJoin(PlayerJoinEvent e){ + if (Config.isChallenge()) return; MissileWars.join(e.getPlayer()); e.setJoinMessage("§a» " + e.getPlayer().getDisplayName()); } -- 2.39.2 From b591f8608e8dd5877db542e9b6e543aa574c93fe Mon Sep 17 00:00:00 2001 From: jojo Date: Sat, 19 Dec 2020 15:29:28 +0100 Subject: [PATCH 28/31] Fix JoinListener --- src/de/steamwar/misslewars/listener/JoinListener.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/steamwar/misslewars/listener/JoinListener.java b/src/de/steamwar/misslewars/listener/JoinListener.java index e9c648a..7469e5a 100644 --- a/src/de/steamwar/misslewars/listener/JoinListener.java +++ b/src/de/steamwar/misslewars/listener/JoinListener.java @@ -36,9 +36,9 @@ public class JoinListener extends BasicListener { @EventHandler(priority = EventPriority.HIGHEST) public void onJoin(PlayerJoinEvent e){ + e.setJoinMessage("§a» " + e.getPlayer().getDisplayName()); if (Config.isChallenge()) return; MissileWars.join(e.getPlayer()); - e.setJoinMessage("§a» " + e.getPlayer().getDisplayName()); } } -- 2.39.2 From 401fc90c4dad6b37375816f38be7ab963d262106 Mon Sep 17 00:00:00 2001 From: jojo Date: Sat, 19 Dec 2020 20:57:20 +0100 Subject: [PATCH 29/31] Improve System --- src/de/steamwar/misslewars/MWTeam.java | 1 - src/de/steamwar/misslewars/MissileWars.java | 6 +++--- ...{CommandInviteHandler.java => CommandAcceptDecline.java} | 2 +- src/de/steamwar/misslewars/commands/CommandInvite.java | 1 - 4 files changed, 4 insertions(+), 6 deletions(-) rename src/de/steamwar/misslewars/commands/{CommandInviteHandler.java => CommandAcceptDecline.java} (96%) diff --git a/src/de/steamwar/misslewars/MWTeam.java b/src/de/steamwar/misslewars/MWTeam.java index 111e30e..310dac4 100644 --- a/src/de/steamwar/misslewars/MWTeam.java +++ b/src/de/steamwar/misslewars/MWTeam.java @@ -19,7 +19,6 @@ package de.steamwar.misslewars; -import de.steamwar.misslewars.items.Missile; import org.bukkit.ChatColor; import org.bukkit.GameMode; import org.bukkit.Location; diff --git a/src/de/steamwar/misslewars/MissileWars.java b/src/de/steamwar/misslewars/MissileWars.java index e97696b..b5571b6 100644 --- a/src/de/steamwar/misslewars/MissileWars.java +++ b/src/de/steamwar/misslewars/MissileWars.java @@ -19,7 +19,7 @@ package de.steamwar.misslewars; -import de.steamwar.misslewars.commands.CommandInviteHandler; +import de.steamwar.misslewars.commands.CommandAcceptDecline; import de.steamwar.misslewars.commands.CommandInvite; import de.steamwar.misslewars.commands.CommandSpectate; import de.steamwar.misslewars.countdowns.EndCountdown; @@ -70,8 +70,8 @@ public class MissileWars extends JavaPlugin { getCommand("invite").setExecutor(commandInvite); getCommand("invite").setTabCompleter(commandInvite); - getCommand("accept").setExecutor(new CommandInviteHandler()); - getCommand("decline").setExecutor(new CommandInviteHandler()); + getCommand("accept").setExecutor(new CommandAcceptDecline()); + getCommand("decline").setExecutor(new CommandAcceptDecline()); new WaitingCountdown(); new ItemCountdown(); diff --git a/src/de/steamwar/misslewars/commands/CommandInviteHandler.java b/src/de/steamwar/misslewars/commands/CommandAcceptDecline.java similarity index 96% rename from src/de/steamwar/misslewars/commands/CommandInviteHandler.java rename to src/de/steamwar/misslewars/commands/CommandAcceptDecline.java index 4e2a9fb..668edb2 100644 --- a/src/de/steamwar/misslewars/commands/CommandInviteHandler.java +++ b/src/de/steamwar/misslewars/commands/CommandAcceptDecline.java @@ -29,7 +29,7 @@ import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -public class CommandInviteHandler implements CommandExecutor { +public class CommandAcceptDecline implements CommandExecutor { @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { diff --git a/src/de/steamwar/misslewars/commands/CommandInvite.java b/src/de/steamwar/misslewars/commands/CommandInvite.java index b869f13..0e01215 100644 --- a/src/de/steamwar/misslewars/commands/CommandInvite.java +++ b/src/de/steamwar/misslewars/commands/CommandInvite.java @@ -24,7 +24,6 @@ package de.steamwar.misslewars.commands; import de.steamwar.misslewars.Config; import de.steamwar.misslewars.MWTeam; import de.steamwar.misslewars.MissileWars; -import de.steamwar.misslewars.items.Missile; import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; -- 2.39.2 From f021d5430096826c1d0c14863a599ee93eb4db0c Mon Sep 17 00:00:00 2001 From: jojo Date: Sun, 20 Dec 2020 12:08:38 +0100 Subject: [PATCH 30/31] Improve System --- src/de/steamwar/misslewars/commands/CommandAcceptDecline.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/steamwar/misslewars/commands/CommandAcceptDecline.java b/src/de/steamwar/misslewars/commands/CommandAcceptDecline.java index 668edb2..cc8def7 100644 --- a/src/de/steamwar/misslewars/commands/CommandAcceptDecline.java +++ b/src/de/steamwar/misslewars/commands/CommandAcceptDecline.java @@ -46,7 +46,7 @@ public class CommandAcceptDecline implements CommandExecutor { return false; } - if (command.getName().equals("accept")) { + if (command.getName().equalsIgnoreCase("accept")) { teamInvitation.acceptInvite(player); } else { MWTeam.removeInvitations(player); -- 2.39.2 From 064c4466ac1d0b345fe4fd588c7d6cb1c7e6728d Mon Sep 17 00:00:00 2001 From: jojo Date: Sun, 20 Dec 2020 13:51:27 +0100 Subject: [PATCH 31/31] Make final --- src/de/steamwar/misslewars/listener/DeathListener.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/steamwar/misslewars/listener/DeathListener.java b/src/de/steamwar/misslewars/listener/DeathListener.java index 735dcc4..c82f2f2 100644 --- a/src/de/steamwar/misslewars/listener/DeathListener.java +++ b/src/de/steamwar/misslewars/listener/DeathListener.java @@ -34,7 +34,7 @@ import java.util.EnumSet; public class DeathListener extends BasicListener { - private static Vector ZERO = new Vector(0, 0, 0); + private static final Vector ZERO = new Vector(0, 0, 0); public DeathListener() { super(EnumSet.allOf(FightState.class)); -- 2.39.2