Add MissileWars module

This commit is contained in:
2024-08-04 23:09:23 +02:00
parent ca143f2412
commit 391b8c3854
58 changed files with 4305 additions and 0 deletions
@@ -0,0 +1,35 @@
/*
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;
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<Boolean> returnValue, RunnableScriptEvent runnableScriptEvent, double... doubles) {
if (scriptFunction == null) return nullReturn;
return returnValue.apply(scriptFunction.execute(runnableScriptEvent, doubles));
}
}
@@ -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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.misslewars.scripts;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
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;
private Location customLocation;
private LocationType locationType = LocationType.DEFAULT;
Script.ScriptExecutor scriptExecutor = null;
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) return entity != null ? entity.getLocation() : 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, float pitch, float yaw) {
this.customLocation = new Location(location.getWorld(), x, y, z, yaw, pitch);
}
public Player getPlayer() {
return (Player) entity;
}
public void resumeScriptExecution() {
if (scriptExecutor == null) return;
scriptExecutor.resume();
}
}
@@ -0,0 +1,97 @@
/*
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;
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;
}
}
}
@@ -0,0 +1,127 @@
/*
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;
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;
import org.bukkit.entity.LingeringPotion;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import java.util.ArrayList;
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)
// "name": Item name (STRING)
// "lore": Lore array (OPTIONAL STRING.ARRAY)
// "amount": Item amount (OPTIONAL [default 1] INT)
// "potion": Object with key value pairs for PotionType and Object containing duration and amplifier (OPTIONAL OBJECT)
// "uses": Uses of Item (OPTIONAL INT)
// "EVENT.<eventName>": Event (OPTIONAL JSONobject.ARRAY)
// - onClick
// - onHit
// - onThrow
public enum EventType {
onHit,
onThrow,
onClick
}
private Map<EventType, Script> scriptMap = new HashMap<>();
private String entityName = "";
private ItemStack itemStack;
public ScriptedItem(JsonObject jsonObject) {
itemStack = createItemStack(jsonObject);
getString(jsonObject, "entityName", string -> entityName = string);
for (EventType eventType : EventType.values()) {
String eventString = "EVENT." + eventType.name();
if (!jsonObject.has(eventString) || !jsonObject.get(eventString).isJsonArray()) continue;
scriptMap.put(eventType, Script.parseScript(jsonObject.getAsJsonArray(eventString)));
}
}
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;
getString(jsonObject, "name", itemMeta::setDisplayName);
if (jsonObject.has("lore")) {
List<String> lore = new ArrayList<>();
jsonObject.getAsJsonArray("lore").forEach(jsonElement -> lore.add(jsonElement.getAsString()));
itemMeta.setLore(lore);
}
if (jsonObject.has("potion") && itemMeta instanceof PotionMeta) {
JsonObject potionObject = jsonObject.getAsJsonObject("potion");
potionObject.entrySet().forEach(stringJsonElementEntry -> {
String key = stringJsonElementEntry.getKey();
JsonObject currentObject = stringJsonElementEntry.getValue().getAsJsonObject();
PotionEffectType potionEffectType = PotionEffectType.getByName(key);
if (potionEffectType == null) return;
int duration = JsonUtils.getInt(currentObject, "duration", 0);
int amplifier = JsonUtils.getInt(currentObject, "amplifier", 0);
PotionMeta potionMeta = (PotionMeta) itemMeta;
potionMeta.addCustomEffect(new PotionEffect(potionEffectType, duration, amplifier), true);
});
}
if (jsonObject.has("uses") && itemMeta instanceof Damageable) {
int uses = jsonObject.getAsJsonPrimitive("uses").getAsInt();
Damageable damageable = (Damageable) itemMeta;
damageable.setDamage(itemStack.getType().getMaxDurability() - uses);
}
itemStack.setItemMeta(itemMeta);
return itemStack;
}
public boolean execute(EventType eventType, Entity entity, Location location) {
if (!scriptMap.containsKey(eventType)) return false;
scriptMap.get(eventType).execute(new RunnableScriptEvent(eventType, entity, location));
return true;
}
public ItemStack getItemStack() {
return itemStack;
}
public String getEntityName() {
return entityName;
}
}
@@ -0,0 +1,31 @@
package de.steamwar.misslewars.scripts.implemented;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import de.steamwar.misslewars.MissileWars;
import de.steamwar.misslewars.scripts.RunnableScript;
import de.steamwar.misslewars.scripts.RunnableScriptEvent;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
public class CooldownScript implements RunnableScript {
private int cooldown = 0;
public CooldownScript(JsonObject object) {
JsonPrimitive primitive = object.getAsJsonPrimitive("cooldown");
if(primitive.isNumber()) cooldown = primitive.getAsInt();
}
@Override
public boolean execute(RunnableScriptEvent runnableScriptEvent) {
Material mainHand = runnableScriptEvent.getPlayer().getInventory().getItemInMainHand().getType();
Material offHand = runnableScriptEvent.getPlayer().getInventory().getItemInOffHand().getType();
Bukkit.getScheduler().runTaskLater(MissileWars.getPlugin(), () -> {
if (mainHand != Material.AIR) runnableScriptEvent.getPlayer().setCooldown(mainHand, cooldown);
if (offHand != Material.AIR) runnableScriptEvent.getPlayer().setCooldown(offHand, cooldown);
}, 1);
return true;
}
}
@@ -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 <https://www.gnu.org/licenses/>.
*/
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 java.util.HashMap;
import java.util.Map;
public class DelayScript implements RunnableScript {
private static final Map<String, Integer> 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()) delayTime = delayMap.getOrDefault(jsonPrimitive.getAsString().toLowerCase(), 0);
else if (jsonPrimitive.isNumber()) delayTime = jsonPrimitive.getAsInt();
}
@Override
public boolean execute(RunnableScriptEvent runnableScriptEvent) {
Bukkit.getScheduler().runTaskLater(MissileWars.getPlugin(), runnableScriptEvent::resumeScriptExecution, delayTime);
return false;
}
}
@@ -0,0 +1,68 @@
/*
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.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;
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 implements RunnableScript {
private static final Map<String, ScriptFunction> filterMap = new HashMap<>();
static {
filterMap.put("nearportal", (runnableScriptEvent, doubles) -> {
Location location = runnableScriptEvent.getLocation();
int bz = MissileWars.getBlueTeam().getPortalZ();
int rz = MissileWars.getRedTeam().getPortalZ();
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);
});
filterMap.put("nearspawn", (runnableScriptEvent, doubles) -> {
Location location = runnableScriptEvent.getLocation();
return MissileWars.getBlueTeam().getSpawn().distance(location) < 3 || MissileWars.getRedTeam().getSpawn().distance(location) < 3;
});
}
private boolean inverted;
private ScriptFunction filter;
public FilterScript(JsonObject filter) {
this.filter = filterMap.getOrDefault(getString(filter, "filter", "").toLowerCase(), null);
inverted = getBoolean(filter, "invert", false);
}
@Override
public boolean execute(RunnableScriptEvent runnableScriptEvent) {
return defaultExecution(filter, true, b -> b ^ inverted, runnableScriptEvent);
}
}
@@ -0,0 +1,51 @@
/*
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.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 de.steamwar.misslewars.scripts.utils.EntityUtils;
import de.steamwar.misslewars.scripts.utils.EntityUtils.ScriptShortcut;
import org.bukkit.entity.Projectile;
public class LaunchScript implements RunnableScript {
private ScriptFunction launch = null;
public LaunchScript(JsonObject launch) {
ScriptShortcut<Projectile> scriptShortcut = EntityUtils.getEntity(launch.getAsJsonPrimitive("entity").getAsString(), EntityUtils.EntityType.Projectile);
if (scriptShortcut == null) return;
this.launch = (runnableScriptEvent, doubles) -> {
Projectile projectile = runnableScriptEvent.getPlayer().launchProjectile(scriptShortcut.entityClass);
scriptShortcut.consumer.accept(launch, projectile, runnableScriptEvent);
return false;
};
}
@Override
public boolean execute(RunnableScriptEvent runnableScriptEvent) {
if (runnableScriptEvent.eventType != ScriptedItem.EventType.onClick) return true;
return defaultExecution(launch, false, b -> true, runnableScriptEvent);
}
}
@@ -0,0 +1,89 @@
/*
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.implemented;
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 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 implements RunnableScript {
private static final Map<String, LocationType> locationTypeMap = new HashMap<>();
private static final Map<String, ScriptFunction> 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 false;
Location location1 = runnableScriptEvent.entity.getLocation();
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], 0, 0);
return false;
});
ScriptFunction absoluteLocation = (runnableScriptEvent, doubles) -> {
runnableScriptEvent.setCustomLocation(doubles[0], doubles[1], doubles[2], 0, 0);
return false;
};
locationMap.put("absolute", absoluteLocation);
locationMap.put("fix", absoluteLocation);
locationMap.put("fixed", absoluteLocation);
}
private LocationType locationType = null;
private ScriptFunction locationExecutor = null;
private double x, y, z = 0;
public LocationScript(JsonObject location) {
if (location.has("location")) {
JsonObject jsonObject = location.getAsJsonObject("location");
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")) {
locationType = locationTypeMap.getOrDefault(getString(location, "locationType", "").toLowerCase(), LocationType.DEFAULT);
}
}
@Override
public boolean execute(RunnableScriptEvent runnableScriptEvent) {
runnableScriptEvent.setLocationType(locationType);
return defaultExecution(locationExecutor, true, b -> true, runnableScriptEvent, x, y, z);
}
}
@@ -0,0 +1,92 @@
/*
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.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 de.steamwar.misslewars.scripts.RunnableScriptEvent;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Objects;
import static de.steamwar.misslewars.scripts.utils.JsonUtils.getBoolean;
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, ignoreAir;
private int xOffset, yOffset, 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 " + schemFileName, e);
}
centeredOffset = clipboard.getRegion().getMinimumPoint().subtract(clipboard.getOrigin()).add(clipboard.getDimensions().divide(2));
centered = getBoolean(paste, "centered", false);
ignoreAir = getBoolean(paste, "ignoreAir", false);
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);
EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(world, -1);
Operations.completeBlindly(new ClipboardHolder(clipboard).createPaste(editSession).ignoreAirBlocks(ignoreAir).to(paste).build());
editSession.flushSession();
return true;
}
}
@@ -0,0 +1,56 @@
/*
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.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.potion.PotionEffect;
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 implements RunnableScript {
private PotionEffect potionEffect = null;
public PotionScript(JsonObject potion) {
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) 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;
runnableScriptEvent.getPlayer().addPotionEffect(potionEffect);
return true;
}
}
@@ -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 <https://www.gnu.org/licenses/>.
* /
*/
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.entity.Player;
import java.util.Random;
public class RandomPlayerScript implements RunnableScript {
private Random random = new Random();
public RandomPlayerScript(JsonObject __) {
}
@Override
public boolean execute(RunnableScriptEvent runnableScriptEvent) {
int size = MissileWars.getBlueTeam().getPlayers().size() + MissileWars.getRedTeam().getPlayers().size();
if (size == 0) return true;
int index = random.nextInt(size);
Player player;
if (index >= MissileWars.getBlueTeam().getPlayers().size()) {
player = MissileWars.getRedTeam().getPlayers().get(index - MissileWars.getBlueTeam().getPlayers().size());
} else {
player = MissileWars.getBlueTeam().getPlayers().get(index);
}
runnableScriptEvent.setLocationType(RunnableScriptEvent.LocationType.CUSTOM);
runnableScriptEvent.setCustomLocation(player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ(), player.getLocation().getPitch(), player.getLocation().getYaw());
return true;
}
}
@@ -0,0 +1,38 @@
/*
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.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 {
public RemoveScript(JsonObject jsonObject) {}
@Override
public boolean execute(RunnableScriptEvent runnableScriptEvent) {
if (runnableScriptEvent.entity instanceof Player) return true;
runnableScriptEvent.entity.remove();
return true;
}
}
@@ -0,0 +1,44 @@
/*
*
* 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.implemented;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import de.steamwar.misslewars.scripts.RunnableScript;
import de.steamwar.misslewars.scripts.RunnableScriptEvent;
import de.steamwar.misslewars.slowmo.SlowMoRunner;
public class SlowMoScript implements RunnableScript {
private int slowMoTime = 0;
public SlowMoScript(JsonObject jsonObject) {
JsonPrimitive jsonPrimitive = jsonObject.getAsJsonPrimitive("time");
if (jsonPrimitive.isNumber()) slowMoTime = jsonPrimitive.getAsInt();
}
@Override
public boolean execute(RunnableScriptEvent runnableScriptEvent) {
SlowMoRunner.addSlowMoTime(slowMoTime);
return true;
}
}
@@ -0,0 +1,51 @@
/*
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.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 static de.steamwar.misslewars.scripts.utils.JsonUtils.getFloat;
import static de.steamwar.misslewars.scripts.utils.JsonUtils.getString;
public class SoundScript implements RunnableScript {
private Sound sound;
private float volume;
private float pitch;
public SoundScript(JsonObject sound) {
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;
runnableScriptEvent.getPlayer().playSound(runnableScriptEvent.getPlayer().getLocation(), sound, volume, pitch);
return true;
}
}
@@ -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 <https://www.gnu.org/licenses/>.
*/
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.utils.EntityUtils;
import de.steamwar.misslewars.scripts.utils.EntityUtils.ScriptShortcut;
import org.bukkit.entity.Entity;
public class SummonScript implements RunnableScript {
private ScriptFunction summon = null;
public SummonScript(JsonObject summon) {
ScriptShortcut<Entity> scriptShortcut = EntityUtils.getEntity(summon.getAsJsonPrimitive("entity").getAsString(), EntityUtils.EntityType.Normal);
if (scriptShortcut == null) return;
this.summon = (runnableScriptEvent, doubles) -> {
Entity entity = runnableScriptEvent.entity.getWorld().spawn(runnableScriptEvent.getLocation(), scriptShortcut.entityClass);
scriptShortcut.consumer.accept(summon, entity, runnableScriptEvent);
return false;
};
}
@Override
public boolean execute(RunnableScriptEvent runnableScriptEvent) {
return defaultExecution(summon, false, b -> true, runnableScriptEvent);
}
}
@@ -0,0 +1,99 @@
/*
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;
import org.bukkit.entity.*;
import static de.steamwar.misslewars.scripts.utils.JsonUtils.*;
public class EntityUtils {
private EntityUtils() {
throw new IllegalStateException("Utility class");
}
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 setProjectileOptions(Projectile projectile, JsonObject jsonObject) {
getBoolean(jsonObject, "bounce", projectile::setBounce);
setEntityOptions(projectile, jsonObject);
}
public static void setExplosiveOptions(Explosive explosive, JsonObject jsonObject) {
getFloat(jsonObject, "yield", explosive::setYield);
getBoolean(jsonObject, "incendiary", explosive::setIsIncendiary);
setEntityOptions(explosive, jsonObject);
}
public static void setFireballOptions(Fireball fireball, JsonObject jsonObject) {
setProjectileOptions(fireball, jsonObject);
setExplosiveOptions(fireball, jsonObject);
}
public static void setTNTPrimedOptions(TNTPrimed tntPrimed, JsonObject jsonObject) {
getInt(jsonObject, "fuse", tntPrimed::setFuseTicks);
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;
}
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);
}
}
@@ -0,0 +1,51 @@
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) {
return jsonObject.has(key) ? jsonObject.getAsJsonPrimitive(key).getAsBoolean() : defaultValue;
}
public static int getInt(JsonObject jsonObject, String key, int defaultValue) {
return jsonObject.has(key) ? jsonObject.getAsJsonPrimitive(key).getAsInt() : defaultValue;
}
public static float getFloat(JsonObject jsonObject, String key, float defaultValue) {
return jsonObject.has(key) ? jsonObject.getAsJsonPrimitive(key).getAsFloat() : defaultValue;
}
public static String getString(JsonObject jsonObject, String key, String defaultValue) {
return jsonObject.has(key) ? jsonObject.getAsJsonPrimitive(key).getAsString() : defaultValue;
}
public static void getBoolean(JsonObject jsonObject, String key, Consumer<Boolean> 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<Float> booleanConsumer) {
if (jsonObject.has(key)) booleanConsumer.accept(jsonObject.getAsJsonPrimitive(key).getAsFloat());
}
public static void getString(JsonObject jsonObject, String key, Consumer<String> booleanConsumer) {
if (jsonObject.has(key)) booleanConsumer.accept(jsonObject.getAsJsonPrimitive(key).getAsString());
}
}