12
0

Improve Code Complexity

Dieser Commit ist enthalten in:
jojo 2020-12-19 12:56:06 +01:00
Ursprung f7db2ab377
Commit 01a422f96d
21 geänderte Dateien mit 95 neuen und 200 gelöschten Zeilen

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.misslewars.scripts;
public enum LocationType {
STATIC,
DYNAMIC,
DEFAULT,
CUSTOM
}

Datei anzeigen

@ -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);
}

Datei anzeigen

@ -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);
}

Datei anzeigen

@ -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;
}

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
* /
*/
package de.steamwar.misslewars.scripts;
public interface ScriptFunction {
boolean execute(RunnableScriptEvent runnableScriptEvent, double... doubles);
}

Datei anzeigen

@ -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<EventType, Script> 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<String> lore = new ArrayList<>();

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.misslewars.scripts.function;
import de.steamwar.misslewars.scripts.RunnableScriptEvent;
public interface ScriptDoubleFunction {
boolean execute(RunnableScriptEvent runnableScriptEvent, double... doubles);
}

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.misslewars.scripts.function;
import de.steamwar.misslewars.scripts.RunnableScriptEvent;
public interface ScriptFunction {
boolean execute(RunnableScriptEvent runnableScriptEvent);
}

Datei anzeigen

@ -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<String, Integer> delayMap = new HashMap<>();

Datei anzeigen

@ -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<String, ScriptFunction> 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;
}
}

Datei anzeigen

@ -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<Projectile> 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;

Datei anzeigen

@ -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<String, LocationType> locationTypeMap = new HashMap<>();
private static final Map<String, ScriptDoubleFunction> locationMap = new HashMap<>();
private static final Map<String, ScriptFunction> 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;

Datei anzeigen

@ -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));

Datei anzeigen

@ -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;

Datei anzeigen

@ -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) {}

Datei anzeigen

@ -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;

Datei anzeigen

@ -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<Entity> 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;

Datei anzeigen

@ -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<T> {
public Class<T> entityClass;
public TriConsumer<JsonObject, T, RunnableScriptEvent> consumer;
public ScriptShortcut(Class<T> entityClass, TriConsumer<JsonObject, T, RunnableScriptEvent> consumer) {
this.entityClass = entityClass;
this.consumer = consumer;
}
}
public interface TriConsumer<T, R, K> {
void accept(T t, R r, K k);
}
}

Datei anzeigen

@ -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<Boolean> booleanConsumer) {

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.misslewars.scripts.utils;
import com.google.gson.JsonObject;
import de.steamwar.misslewars.scripts.RunnableScriptEvent;
public class ScriptShortcut<T> {
public Class<T> entityClass;
public TriConsumer<JsonObject, T, RunnableScriptEvent> consumer;
public ScriptShortcut(Class<T> entityClass, TriConsumer<JsonObject, T, RunnableScriptEvent> consumer) {
this.entityClass = entityClass;
this.consumer = consumer;
}
}

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.misslewars.scripts.utils;
public interface TriConsumer<T, R, K> {
void accept(T t, R r, K k);
}