From 08ebd8c7f3c74639682036265d71eb862028319f Mon Sep 17 00:00:00 2001 From: jojo Date: Sat, 19 Dec 2020 13:40:20 +0100 Subject: [PATCH] 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; }