Files
SteamWar/MissileWars/src/de/steamwar/misslewars/scripts/Script.java
T
2025-10-23 17:56:43 +02:00

98 lines
3.3 KiB
Java

/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2025 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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.misslewars.scripts;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import de.steamwar.misslewars.scripts.implemented.*;
import java.util.ArrayList;
import java.util.List;
public class Script {
private List<RunnableScript> runnableScriptList = new ArrayList<>();
class ScriptExecutor {
private int index = 0;
private final RunnableScriptEvent runnableScriptEvent;
public ScriptExecutor(RunnableScriptEvent runnableScriptEvent) {
this.runnableScriptEvent = runnableScriptEvent;
runnableScriptEvent.scriptExecutor = this;
resume();
}
void resume() {
while (index < runnableScriptList.size()) {
if (!runnableScriptList.get(index++).execute(runnableScriptEvent)) return;
}
}
}
public void execute(RunnableScriptEvent runnableScriptEvent) {
new ScriptExecutor(runnableScriptEvent);
}
public static Script parseScript(JsonArray jsonArray) {
Script script = new Script();
jsonArray.forEach(jsonElement -> {
RunnableScript runnableScript = parseScriptSnippet((JsonObject) jsonElement);
if (runnableScript == null) return;
script.runnableScriptList.add(runnableScript);
});
return script;
}
private static RunnableScript parseScriptSnippet(JsonObject jsonObject) {
if (!jsonObject.has("type")) return null;
switch (jsonObject.getAsJsonPrimitive("type").getAsString().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);
case "slowmo":
return new SlowMoScript(jsonObject);
case "cooldown":
return new CooldownScript(jsonObject);
case "randomplayer":
return new RandomPlayerScript(jsonObject);
default:
return null;
}
}
}