From b73f9798e41bf922b4d5b151d4db917084409593 Mon Sep 17 00:00:00 2001 From: CocoTheOwner Date: Tue, 21 Sep 2021 19:45:32 +0200 Subject: [PATCH] Introduction if IrisCommands --- .../iris/engine/object/IrisCommand.java | 48 +++++++++++++++++++ .../volmit/iris/engine/object/IrisEntity.java | 14 ++---- 2 files changed, 52 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/volmit/iris/engine/object/IrisCommand.java diff --git a/src/main/java/com/volmit/iris/engine/object/IrisCommand.java b/src/main/java/com/volmit/iris/engine/object/IrisCommand.java new file mode 100644 index 000000000..88d6285a1 --- /dev/null +++ b/src/main/java/com/volmit/iris/engine/object/IrisCommand.java @@ -0,0 +1,48 @@ +package com.volmit.iris.engine.object; + +import com.volmit.iris.Iris; +import com.volmit.iris.engine.object.annotations.Desc; +import com.volmit.iris.engine.object.annotations.RegistryListResource; +import com.volmit.iris.engine.object.annotations.Snippet; +import com.volmit.iris.util.collection.KList; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.experimental.Accessors; +import org.bukkit.Bukkit; +import org.bukkit.Location; + +@Snippet("command") +@Accessors(chain = true) +@NoArgsConstructor +@Desc("Represents a color") +@Data +public class IrisCommand { + + @Desc("List of commands. Iris replaces {x} {y} and {z} with the location of the entity spawn") + private KList commands = new KList<>(); + + @Desc("The delay for running the command. Instant by default") + private long delay = 0; + + @Desc("If this should be repeated (indefinitely, cannot be cancelled). This does not persist with server-restarts, so it only repeats when the chunk is generated.") + private boolean repeat = false; + + @Desc("The delay between repeats, in server ticks (by default 100, so 5 seconds)") + private long repeatDelay = 100; + + + public void run(Location at) { + for (String command : commands) { + command = (command.startsWith("/") ? command.replaceFirst("/", "") : command) + .replaceAll("\\Q{x}\\E", String.valueOf(at.getBlockX())) + .replaceAll("\\Q{y}\\E", String.valueOf(at.getBlockY())) + .replaceAll("\\Q{z}\\E", String.valueOf(at.getBlockZ())); + final String finalCommand = command; + if (repeat) { + Bukkit.getScheduler().scheduleSyncRepeatingTask(Iris.instance, () -> Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), finalCommand), delay, repeatDelay); + } else { + Bukkit.getScheduler().scheduleSyncDelayedTask(Iris.instance, () -> Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), finalCommand), delay); + } + } + } +} diff --git a/src/main/java/com/volmit/iris/engine/object/IrisEntity.java b/src/main/java/com/volmit/iris/engine/object/IrisEntity.java index 658a5afa3..ca7f2ce62 100644 --- a/src/main/java/com/volmit/iris/engine/object/IrisEntity.java +++ b/src/main/java/com/volmit/iris/engine/object/IrisEntity.java @@ -176,9 +176,9 @@ public class IrisEntity extends IrisRegistrant { @RegistryListResource(IrisScript.class) private KList postSpawnScripts = new KList<>(); - @ArrayType(min = 1, type = String.class) + @ArrayType(min = 1, type = IrisCommand.class) @Desc("Run raw commands when this entity is spawned. Use {x}, {y}, and {z} for location. /summon pig {x} {y} {z}") - private KList rawCommands = new KList<>(); + private KList rawCommands = new KList<>(); public Entity spawn(Engine gen, Location at) { return spawn(gen, at, new RNG(at.hashCode())); @@ -362,14 +362,8 @@ public class IrisEntity extends IrisRegistrant { } if (rawCommands.isNotEmpty()) { - synchronized (this) { - for (String rawCommand : rawCommands) { - rawCommand.replaceAll("\\Q{x}\\E", String.valueOf(at.getBlockX())); - rawCommand.replaceAll("\\Q{y}\\E", String.valueOf(at.getBlockY())); - rawCommand.replaceAll("\\Q{z}\\E", String.valueOf(at.getBlockZ())); - Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), rawCommand); - } - } + final Location fat = at; + rawCommands.forEach(r -> r.run(fat)); } Location finalAt1 = at;