SteamWar/MissileWars
Archiviert
13
0

Improve Code Complexity

Dieser Commit ist enthalten in:
jojo 2020-12-19 13:40:20 +01:00
Ursprung f1e19126a9
Commit 08ebd8c7f3
4 geänderte Dateien mit 16 neuen und 21 gelöschten Zeilen

Datei anzeigen

@ -37,6 +37,7 @@ public class RunnableScriptEvent {
private final Location location; private final Location location;
private Location customLocation; private Location customLocation;
private LocationType locationType = LocationType.DEFAULT; private LocationType locationType = LocationType.DEFAULT;
Script.ScriptExecutor scriptExecutor = null;
public RunnableScriptEvent(ScriptedItem.EventType eventType, Entity entity, Location location) { public RunnableScriptEvent(ScriptedItem.EventType eventType, Entity entity, Location location) {
this.eventType = eventType; this.eventType = eventType;
@ -73,4 +74,9 @@ public class RunnableScriptEvent {
return (Player) entity; return (Player) entity;
} }
public void resumeScriptExecution() {
if (scriptExecutor == null) return;
scriptExecutor.resume();
}
} }

Datei anzeigen

@ -21,9 +21,7 @@ package de.steamwar.misslewars.scripts;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import de.steamwar.misslewars.MissileWars;
import de.steamwar.misslewars.scripts.implemented.*; import de.steamwar.misslewars.scripts.implemented.*;
import org.bukkit.Bukkit;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -32,27 +30,20 @@ public class Script {
private List<RunnableScript> runnableScriptList = new ArrayList<>(); private List<RunnableScript> runnableScriptList = new ArrayList<>();
private class ScriptExecutor { class ScriptExecutor {
private int index = 0; private int index = 0;
private final RunnableScriptEvent runnableScriptEvent; private final RunnableScriptEvent runnableScriptEvent;
public ScriptExecutor(RunnableScriptEvent runnableScriptEvent) { public ScriptExecutor(RunnableScriptEvent runnableScriptEvent) {
this.runnableScriptEvent = runnableScriptEvent; this.runnableScriptEvent = runnableScriptEvent;
runnableScriptEvent.scriptExecutor = this;
resume(); resume();
} }
private void resume() { void resume() {
while (index < runnableScriptList.size()) { while (index < runnableScriptList.size()) {
RunnableScript runnableScript = runnableScriptList.get(index++); if (!runnableScriptList.get(index++).execute(runnableScriptEvent)) return;
if (runnableScript instanceof DelayScript) {
Bukkit.getScheduler().runTaskLater(MissileWars.getPlugin(), this::resume, ((DelayScript) runnableScript).getDelayTime());
return;
}
if (!runnableScript.execute(runnableScriptEvent)) {
index = runnableScriptList.size();
return;
}
} }
} }

Datei anzeigen

@ -22,8 +22,11 @@ package de.steamwar.misslewars.scripts.implemented;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive; import com.google.gson.JsonPrimitive;
import de.steamwar.misslewars.Config; import de.steamwar.misslewars.Config;
import de.steamwar.misslewars.MissileWars;
import de.steamwar.misslewars.scripts.RunnableScript; import de.steamwar.misslewars.scripts.RunnableScript;
import de.steamwar.misslewars.scripts.RunnableScriptEvent; import de.steamwar.misslewars.scripts.RunnableScriptEvent;
import org.bukkit.Bukkit;
import org.bukkit.scheduler.BukkitTask;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -53,11 +56,8 @@ public class DelayScript implements RunnableScript {
@Override @Override
public boolean execute(RunnableScriptEvent runnableScriptEvent) { public boolean execute(RunnableScriptEvent runnableScriptEvent) {
return true; Bukkit.getScheduler().runTaskLater(MissileWars.getPlugin(), runnableScriptEvent::resumeScriptExecution, delayTime);
} return false;
public int getDelayTime() {
return delayTime;
} }
} }

Datei anzeigen

@ -83,9 +83,7 @@ public class LocationScript implements RunnableScript {
@Override @Override
public boolean execute(RunnableScriptEvent runnableScriptEvent) { public boolean execute(RunnableScriptEvent runnableScriptEvent) {
runnableScriptEvent.setLocationType(locationType); runnableScriptEvent.setLocationType(locationType);
if (locationExecutor != null) { if (locationExecutor != null) locationExecutor.execute(runnableScriptEvent, x, y, z);
locationExecutor.execute(runnableScriptEvent, x, y, z);
}
return true; return true;
} }