Merge pull request #653 from CocoTheOwner/rawCommands

Raw commands in Entities
This commit is contained in:
Dan 2021-09-23 10:57:27 -04:00 committed by GitHub
commit 5078becbb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 137 additions and 8 deletions

View File

@ -39,7 +39,7 @@ import org.bukkit.block.data.BlockData;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@Decree(name = "what", origin = DecreeOrigin.PLAYER, studio = true, description = "Iris What?", aliases = "w") @Decree(name = "what", origin = DecreeOrigin.PLAYER, studio = true, description = "Iris What?")
public class CommandWhat implements DecreeExecutor { public class CommandWhat implements DecreeExecutor {
@Decree(description = "What is in my hand?", origin = DecreeOrigin.PLAYER) @Decree(description = "What is in my hand?", origin = DecreeOrigin.PLAYER)
public void hand() { public void hand() {
@ -62,7 +62,7 @@ public class CommandWhat implements DecreeExecutor {
} }
} }
@Decree(description = "What biome am i in?", origin = DecreeOrigin.PLAYER, aliases = "b") @Decree(description = "What biome am i in?", origin = DecreeOrigin.PLAYER)
public void biome() { public void biome() {
try { try {
IrisBiome b = engine().getBiome(player().getLocation().getBlockX(), player().getLocation().getBlockY(), player().getLocation().getBlockZ()); IrisBiome b = engine().getBiome(player().getLocation().getBlockX(), player().getLocation().getBlockY(), player().getLocation().getBlockZ());

View File

@ -0,0 +1,50 @@
package com.volmit.iris.engine.object;
import com.volmit.iris.Iris;
import com.volmit.iris.engine.object.annotations.ArrayType;
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 set of Iris commands")
@Data
public class IrisCommand {
@ArrayType(min = 1, type = String.class)
@Desc("List of commands. Iris replaces {x} {y} and {z} with the location of the entity spawn")
private KList<String> 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);
}
}
}
}

View File

@ -0,0 +1,66 @@
package com.volmit.iris.engine.object;
import com.volmit.iris.engine.object.annotations.*;
import com.volmit.iris.util.collection.KList;
import com.volmit.iris.util.math.RNG;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.bukkit.Location;
import org.bukkit.entity.Player;
@Snippet("command-registry")
@Accessors(chain = true)
@NoArgsConstructor
@Desc("Represents a casting location for a command")
@Data
public class IrisCommandRegistry {
@ArrayType(min = 1, type = IrisCommand.class)
@Desc("Run commands, at the exact location of the player")
private KList<IrisCommand> rawCommands = new KList<>();
@DependsOn({"rawCommands"})
@MinNumber(-8)
@MaxNumber(8)
@Desc("The alt x, usually represents motion if the particle count is zero. Otherwise an offset.")
private double commandOffsetX = 0;
@DependsOn({"rawCommands"})
@MinNumber(-8)
@MaxNumber(8)
@Desc("The alt y, usually represents motion if the particle count is zero. Otherwise an offset.")
private double commandOffsetY = 0;
@DependsOn({"rawCommands"})
@MinNumber(-8)
@MaxNumber(8)
@Desc("The alt z, usually represents motion if the particle count is zero. Otherwise an offset.")
private double commandOffsetZ = 0;
@DependsOn({"rawCommands"})
@Desc("Randomize the altX from -altX to altX")
private boolean commandRandomAltX = true;
@DependsOn({"rawCommands"})
@Desc("Randomize the altY from -altY to altY")
private boolean commandRandomAltY = false;
@DependsOn({"rawCommands"})
@Desc("Randomize the altZ from -altZ to altZ")
private boolean commandRandomAltZ = true;
@DependsOn({"rawCommands"})
@Desc("Randomize location for all separate commands (true), or run all on the same location (false)")
private boolean commandAllRandomLocations = true;
public void run(Player p) {
if (rawCommands.isNotEmpty()) {
Location part = p.getLocation().clone().add(
commandRandomAltX ? RNG.r.d(-commandOffsetX, commandOffsetX) : commandOffsetX,
commandRandomAltY ? RNG.r.d(-commandOffsetY, commandOffsetY) : commandOffsetY,
commandRandomAltZ ? RNG.r.d(-commandOffsetZ, commandOffsetZ) : commandOffsetZ);
for (IrisCommand rawCommand : rawCommands) {
rawCommand.run(part);
if (commandAllRandomLocations) {
part = p.getLocation().clone().add(
commandRandomAltX ? RNG.r.d(-commandOffsetX, commandOffsetX) : commandOffsetX,
commandRandomAltY ? RNG.r.d(-commandOffsetY, commandOffsetY) : commandOffsetY,
commandRandomAltZ ? RNG.r.d(-commandOffsetZ, commandOffsetZ) : commandOffsetZ);
}
}
}
}
}

View File

@ -21,12 +21,8 @@ package com.volmit.iris.engine.object;
import com.volmit.iris.Iris; import com.volmit.iris.Iris;
import com.volmit.iris.engine.data.cache.AtomicCache; import com.volmit.iris.engine.data.cache.AtomicCache;
import com.volmit.iris.engine.framework.Engine; import com.volmit.iris.engine.framework.Engine;
import com.volmit.iris.engine.object.annotations.DependsOn; import com.volmit.iris.engine.object.annotations.*;
import com.volmit.iris.engine.object.annotations.Desc; import com.volmit.iris.util.collection.KList;
import com.volmit.iris.engine.object.annotations.MaxNumber;
import com.volmit.iris.engine.object.annotations.MinNumber;
import com.volmit.iris.engine.object.annotations.Required;
import com.volmit.iris.engine.object.annotations.Snippet;
import com.volmit.iris.util.math.RNG; import com.volmit.iris.util.math.RNG;
import com.volmit.iris.util.scheduling.ChronoLatch; import com.volmit.iris.util.scheduling.ChronoLatch;
import com.volmit.iris.util.scheduling.J; import com.volmit.iris.util.scheduling.J;
@ -151,6 +147,10 @@ public class IrisEffect {
@MinNumber(1) @MinNumber(1)
@Desc("The chance is 1 in CHANCE per interval") @Desc("The chance is 1 in CHANCE per interval")
private int chance = 50; private int chance = 50;
@ArrayType(min = 1, type = IrisCommandRegistry.class)
@Desc("Run commands, with configurable location parameters")
private IrisCommandRegistry commandRegistry = null;
public boolean canTick() { public boolean canTick() {
return latch.aquire(() -> new ChronoLatch(interval)).flip(); return latch.aquire(() -> new ChronoLatch(interval)).flip();
@ -221,6 +221,10 @@ public class IrisEffect {
} }
} }
if (commandRegistry != null) {
commandRegistry.run(p);
}
if (potionStrength > -1) { if (potionStrength > -1) {
if (p.hasPotionEffect(getRealType())) { if (p.hasPotionEffect(getRealType())) {
PotionEffect e = p.getPotionEffect(getRealType()); PotionEffect e = p.getPotionEffect(getRealType());

View File

@ -176,6 +176,10 @@ public class IrisEntity extends IrisRegistrant {
@RegistryListResource(IrisScript.class) @RegistryListResource(IrisScript.class)
private KList<String> postSpawnScripts = new KList<>(); private KList<String> postSpawnScripts = new KList<>();
@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<IrisCommand> rawCommands = new KList<>();
public Entity spawn(Engine gen, Location at) { public Entity spawn(Engine gen, Location at) {
return spawn(gen, at, new RNG(at.hashCode())); return spawn(gen, at, new RNG(at.hashCode()));
} }
@ -357,6 +361,11 @@ public class IrisEntity extends IrisRegistrant {
} }
} }
if (rawCommands.isNotEmpty()) {
final Location fat = at;
rawCommands.forEach(r -> r.run(fat));
}
Location finalAt1 = at; Location finalAt1 = at;
J.s(() -> { J.s(() -> {