From 3105d5bb719338a511eeac7ee3a3500c6a562f88 Mon Sep 17 00:00:00 2001 From: gnembon Date: Tue, 8 Sep 2020 23:47:31 -0300 Subject: [PATCH] Events are defined statically in normal classes and added handler for extensions to add their built-ins --- src/main/java/carpet/CarpetExtension.java | 18 ++ .../java/carpet/commands/ScriptCommand.java | 2 +- .../mixins/Entity_scarpetEventsMixin.java | 4 +- ...FallingBlockEntity_scarpetEventsMixin.java | 2 +- .../LivingEntity_scarpetEventsMixin.java | 4 +- .../PlayerEntity_scarpetEventsMixin.java | 2 +- .../ServerPlayerEntity_scarpetEventMixin.java | 3 +- .../java/carpet/script/CarpetEventServer.java | 164 +++++++++--------- .../java/carpet/script/CarpetExpression.java | 8 +- .../java/carpet/script/EntityEventsGroup.java | 30 ++-- .../java/carpet/script/value/EntityValue.java | 2 +- 11 files changed, 126 insertions(+), 113 deletions(-) diff --git a/src/main/java/carpet/CarpetExtension.java b/src/main/java/carpet/CarpetExtension.java index b0bd5baf..3a49951a 100644 --- a/src/main/java/carpet/CarpetExtension.java +++ b/src/main/java/carpet/CarpetExtension.java @@ -1,5 +1,6 @@ package carpet; +import carpet.script.CarpetExpression; import carpet.settings.SettingsManager; import com.mojang.brigadier.CommandDispatcher; import net.minecraft.server.MinecraftServer; @@ -65,4 +66,21 @@ public interface CarpetExtension default Map canHasTranslations(String lang) { return null;} + /** + * Handles each call that creates / parses the scarpet expression. + * Extensions can add their own built-in functions here. + * + * Events such as generic events or entity events, can be added statically + * by creating new events as + * + * CarpetEventServer.Event class: to handle `__on_foo()` type of call definitions + * + * or + * + * EntityEventsGroup.Event class: to handle `entity_event('foo', ...)` type of events + * + * @param expression: Passed CarpetExpression to add built-in functions to + */ + default void scarpetApi(CarpetExpression expression) {} + } diff --git a/src/main/java/carpet/commands/ScriptCommand.java b/src/main/java/carpet/commands/ScriptCommand.java index 529156fd..589fcf0e 100644 --- a/src/main/java/carpet/commands/ScriptCommand.java +++ b/src/main/java/carpet/commands/ScriptCommand.java @@ -335,7 +335,7 @@ public class ScriptCommand private static int listEvents(ServerCommandSource source) { Messenger.m(source, "w Lists ALL event handlers:"); - for (CarpetEventServer.Event event: CarpetEventServer.Event.values()) + for (CarpetEventServer.Event event: CarpetEventServer.Event.byName.values()) { boolean shownEvent = false; for (CarpetEventServer.Callback c: event.handler.callList) diff --git a/src/main/java/carpet/mixins/Entity_scarpetEventsMixin.java b/src/main/java/carpet/mixins/Entity_scarpetEventsMixin.java index 879fbc04..c68be88b 100644 --- a/src/main/java/carpet/mixins/Entity_scarpetEventsMixin.java +++ b/src/main/java/carpet/mixins/Entity_scarpetEventsMixin.java @@ -65,14 +65,14 @@ public abstract class Entity_scarpetEventsMixin implements EntityInterface @Inject(method = "tick", at = @At("HEAD")) private void onTickCall(CallbackInfo ci) { - events.onEvent(EntityEventsGroup.EntityEventType.ON_TICK); + events.onEvent(EntityEventsGroup.Event.ON_TICK); } @Inject(method = "remove", at = @At("HEAD")) private void onRemove(CallbackInfo ci) { - if (!removed) events.onEvent(EntityEventsGroup.EntityEventType.ON_REMOVED); + if (!removed) events.onEvent(EntityEventsGroup.Event.ON_REMOVED); } diff --git a/src/main/java/carpet/mixins/FallingBlockEntity_scarpetEventsMixin.java b/src/main/java/carpet/mixins/FallingBlockEntity_scarpetEventsMixin.java index be1c211b..0855cfda 100644 --- a/src/main/java/carpet/mixins/FallingBlockEntity_scarpetEventsMixin.java +++ b/src/main/java/carpet/mixins/FallingBlockEntity_scarpetEventsMixin.java @@ -23,6 +23,6 @@ public abstract class FallingBlockEntity_scarpetEventsMixin extends Entity private void onTickCall(CallbackInfo ci) { // calling extra on_tick because falling blocks do not fall back to super tick call - ((EntityInterface)this).getEventContainer().onEvent(EntityEventsGroup.EntityEventType.ON_TICK); + ((EntityInterface)this).getEventContainer().onEvent(EntityEventsGroup.Event.ON_TICK); } } diff --git a/src/main/java/carpet/mixins/LivingEntity_scarpetEventsMixin.java b/src/main/java/carpet/mixins/LivingEntity_scarpetEventsMixin.java index 07933c28..24ddb8b9 100644 --- a/src/main/java/carpet/mixins/LivingEntity_scarpetEventsMixin.java +++ b/src/main/java/carpet/mixins/LivingEntity_scarpetEventsMixin.java @@ -34,7 +34,7 @@ public abstract class LivingEntity_scarpetEventsMixin extends Entity implements @Inject(method = "onDeath", at = @At("HEAD")) private void onDeathCall(DamageSource damageSource_1, CallbackInfo ci) { - ((EntityInterface)this).getEventContainer().onEvent(EntityEventsGroup.EntityEventType.ON_DEATH, damageSource_1.name); + ((EntityInterface)this).getEventContainer().onEvent(EntityEventsGroup.Event.ON_DEATH, damageSource_1.name); } @Inject(method = "applyDamage", locals = LocalCapture.CAPTURE_FAILHARD, at = @At( @@ -44,7 +44,7 @@ public abstract class LivingEntity_scarpetEventsMixin extends Entity implements )) private void entityTakingDamage(DamageSource source, float amount, CallbackInfo ci) { - ((EntityInterface)this).getEventContainer().onEvent(EntityEventsGroup.EntityEventType.ON_DAMAGE, amount, source); + ((EntityInterface)this).getEventContainer().onEvent(EntityEventsGroup.Event.ON_DAMAGE, amount, source); // this is not applicable since its not a playr for sure //if (entity instanceof ServerPlayerEntity && PLAYER_TAKES_DAMAGE.isNeeded()) //{ diff --git a/src/main/java/carpet/mixins/PlayerEntity_scarpetEventsMixin.java b/src/main/java/carpet/mixins/PlayerEntity_scarpetEventsMixin.java index a09d95d9..6d7d5771 100644 --- a/src/main/java/carpet/mixins/PlayerEntity_scarpetEventsMixin.java +++ b/src/main/java/carpet/mixins/PlayerEntity_scarpetEventsMixin.java @@ -32,7 +32,7 @@ public abstract class PlayerEntity_scarpetEventsMixin extends LivingEntity private void playerTakingDamage(DamageSource source, float amount, CallbackInfo ci) { // version of LivingEntity_scarpetEventsMixin::entityTakingDamage - ((EntityInterface)this).getEventContainer().onEvent(EntityEventsGroup.EntityEventType.ON_DAMAGE, amount, source); + ((EntityInterface)this).getEventContainer().onEvent(EntityEventsGroup.Event.ON_DAMAGE, amount, source); if (PLAYER_TAKES_DAMAGE.isNeeded()) { PLAYER_TAKES_DAMAGE.onDamage(this, amount, source); diff --git a/src/main/java/carpet/mixins/ServerPlayerEntity_scarpetEventMixin.java b/src/main/java/carpet/mixins/ServerPlayerEntity_scarpetEventMixin.java index ade0ec8d..b142079b 100644 --- a/src/main/java/carpet/mixins/ServerPlayerEntity_scarpetEventMixin.java +++ b/src/main/java/carpet/mixins/ServerPlayerEntity_scarpetEventMixin.java @@ -15,7 +15,6 @@ import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d; import net.minecraft.util.registry.RegistryKey; import net.minecraft.world.World; -import net.minecraft.world.dimension.DimensionType; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; @@ -72,7 +71,7 @@ public abstract class ServerPlayerEntity_scarpetEventMixin extends PlayerEntity @Inject(method = "onDeath", at = @At("HEAD")) private void onDeathEvent(DamageSource source, CallbackInfo ci) { - ((EntityInterface)this).getEventContainer().onEvent(EntityEventsGroup.EntityEventType.ON_DEATH, source.name); + ((EntityInterface)this).getEventContainer().onEvent(EntityEventsGroup.Event.ON_DEATH, source.name); if (PLAYER_DIES.isNeeded()) { PLAYER_DIES.onPlayerEvent((ServerPlayerEntity) (Object)this); diff --git a/src/main/java/carpet/script/CarpetEventServer.java b/src/main/java/carpet/script/CarpetEventServer.java index f9a4cd73..fcf095a8 100644 --- a/src/main/java/carpet/script/CarpetEventServer.java +++ b/src/main/java/carpet/script/CarpetEventServer.java @@ -208,9 +208,10 @@ public class CarpetEventServer } } - public enum Event + public static class Event { - TICK("tick", 0, true) + public static final Map byName = new HashMap<>(); + public static final Event TICK = new Event("tick", 0, true) { @Override public void onTick() @@ -220,8 +221,8 @@ public class CarpetEventServer withWorld(CarpetServer.minecraft_server.getWorld(World.OVERWORLD)) ); } - }, - NETHER_TICK("tick_nether", 0, true) + }; + public static final Event NETHER_TICK = new Event("tick_nether", 0, true) { @Override public void onTick() @@ -231,8 +232,8 @@ public class CarpetEventServer withWorld(CarpetServer.minecraft_server.getWorld(World.NETHER)) ); } - }, - ENDER_TICK("tick_ender", 0, true) + }; + public static final Event ENDER_TICK = new Event("tick_ender", 0, true) { @Override public void onTick() @@ -242,8 +243,8 @@ public class CarpetEventServer withWorld(CarpetServer.minecraft_server.getWorld(World.END)) ); } - }, - CHUNK_GENERATED("chunk_generated", 2, true) + }; + public static final Event CHUNK_GENERATED = new Event("chunk_generated", 2, true) { @Override public void onChunkGenerated(ServerWorld world, Chunk chunk) @@ -258,33 +259,33 @@ public class CarpetEventServer }, () -> CarpetServer.minecraft_server.getCommandSource().withWorld(world) ); } - }, + }; - PLAYER_JUMPS("player_jumps", 1, false) + public static final Event PLAYER_JUMPS = new Event("player_jumps", 1, false) { @Override public void onPlayerEvent(ServerPlayerEntity player) { - handler.call( () -> Arrays.asList(((c, t) -> new EntityValue(player))), player::getCommandSource); + handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource); } - }, - PLAYER_DEPLOYS_ELYTRA("player_deploys_elytra", 1, false) + }; + public static final Event PLAYER_DEPLOYS_ELYTRA = new Event("player_deploys_elytra", 1, false) { @Override public void onPlayerEvent(ServerPlayerEntity player) { - handler.call( () -> Arrays.asList(((c, t) -> new EntityValue(player))), player::getCommandSource); + handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource); } - }, - PLAYER_WAKES_UP("player_wakes_up", 1, false) + }; + public static final Event PLAYER_WAKES_UP = new Event("player_wakes_up", 1, false) { @Override public void onPlayerEvent(ServerPlayerEntity player) { - handler.call( () -> Arrays.asList(((c, t) -> new EntityValue(player))), player::getCommandSource); + handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource); } - }, - PLAYER_RIDES("player_rides", 5, false) + }; + public static final Event PLAYER_RIDES = new Event("player_rides", 5, false) { @Override public void onMountControls(ServerPlayerEntity player, float strafeSpeed, float forwardSpeed, boolean jumping, boolean sneaking) @@ -297,8 +298,8 @@ public class CarpetEventServer ((c, t) -> new NumericValue(sneaking)) ), player::getCommandSource); } - }, - PLAYER_USES_ITEM("player_uses_item", 3, false) + }; + public static final Event PLAYER_USES_ITEM = new Event("player_uses_item", 3, false) { @Override public void onItemAction(ServerPlayerEntity player, Hand enumhand, ItemStack itemstack) @@ -313,8 +314,8 @@ public class CarpetEventServer ); }, player::getCommandSource); } - }, - PLAYER_CLICKS_BLOCK("player_clicks_block", 3, false) + }; + public static final Event PLAYER_CLICKS_BLOCK = new Event("player_clicks_block", 3, false) { @Override public void onBlockAction(ServerPlayerEntity player, BlockPos blockpos, Direction facing) @@ -328,8 +329,8 @@ public class CarpetEventServer ); }, player::getCommandSource); } - }, - PLAYER_RIGHT_CLICKS_BLOCK("player_right_clicks_block", 6, false) + }; + public static final Event PLAYER_RIGHT_CLICKS_BLOCK = new Event("player_right_clicks_block", 6, false) { @Override public void onBlockHit(ServerPlayerEntity player, Hand enumhand, BlockHitResult hitRes)//ItemStack itemstack, Hand enumhand, BlockPos blockpos, Direction enumfacing, Vec3d vec3d) @@ -354,8 +355,8 @@ public class CarpetEventServer ); }, player::getCommandSource); } - }, - PLAYER_INTERACTS_WITH_BLOCK("player_interacts_with_block", 5, false) + }; + public static final Event PLAYER_INTERACTS_WITH_BLOCK = new Event("player_interacts_with_block", 5, false) { @Override public void onBlockHit(ServerPlayerEntity player, Hand enumhand, BlockHitResult hitRes) @@ -378,8 +379,8 @@ public class CarpetEventServer ); }, player::getCommandSource); } - }, - PLAYER_PLACES_BLOCK("player_places_block", 4, false) + }; + public static final Event PLAYER_PLACES_BLOCK = new Event("player_places_block", 4, false) { @Override public void onBlockPlaced(ServerPlayerEntity player, BlockPos pos, Hand enumhand, ItemStack itemstack) @@ -391,8 +392,8 @@ public class CarpetEventServer ((c, t) -> new BlockValue(null, player.getServerWorld(), pos)) ), player::getCommandSource); } - }, - PLAYER_BREAK_BLOCK("player_breaks_block", 2, false) + }; + public static final Event PLAYER_BREAK_BLOCK = new Event("player_breaks_block", 2, false) { @Override public void onBlockBroken(ServerPlayerEntity player, BlockPos pos, BlockState previousBS) @@ -402,8 +403,8 @@ public class CarpetEventServer ((c, t) -> new BlockValue(previousBS, player.getServerWorld(), pos)) ), player::getCommandSource); } - }, - PLAYER_INTERACTS_WITH_ENTITY("player_interacts_with_entity", 3, false) + }; + public static final Event PLAYER_INTERACTS_WITH_ENTITY = new Event("player_interacts_with_entity", 3, false) { @Override public void onEntityAction(ServerPlayerEntity player, Entity entity, Hand enumhand) @@ -414,8 +415,8 @@ public class CarpetEventServer ((c, t) -> new StringValue(enumhand==Hand.MAIN_HAND?"mainhand":"offhand")) ), player::getCommandSource); } - }, - PLAYER_ATTACKS_ENTITY("player_attacks_entity", 2, false) + }; + public static final Event PLAYER_ATTACKS_ENTITY = new Event("player_attacks_entity", 2, false) { @Override public void onEntityAction(ServerPlayerEntity player, Entity entity, Hand enumhand) @@ -425,41 +426,41 @@ public class CarpetEventServer ((c, t) -> new EntityValue(entity)) ), player::getCommandSource); } - }, - PLAYER_STARTS_SNEAKING("player_starts_sneaking", 1, false) + }; + public static final Event PLAYER_STARTS_SNEAKING = new Event("player_starts_sneaking", 1, false) { @Override public void onPlayerEvent(ServerPlayerEntity player) { handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource); } - }, - PLAYER_STOPS_SNEAKING("player_stops_sneaking", 1, false) + }; + public static final Event PLAYER_STOPS_SNEAKING = new Event("player_stops_sneaking", 1, false) { @Override public void onPlayerEvent(ServerPlayerEntity player) { handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource); } - }, - PLAYER_STARTS_SPRINTING("player_starts_sprinting", 1, false) + }; + public static final Event PLAYER_STARTS_SPRINTING = new Event("player_starts_sprinting", 1, false) { @Override public void onPlayerEvent(ServerPlayerEntity player) { handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource); } - }, - PLAYER_STOPS_SPRINTING("player_stops_sprinting", 1, false) + }; + public static final Event PLAYER_STOPS_SPRINTING = new Event("player_stops_sprinting", 1, false) { @Override public void onPlayerEvent(ServerPlayerEntity player) { handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource); } - }, + }; - PLAYER_RELEASED_ITEM("player_releases_item", 3, false) + public static final Event PLAYER_RELEASED_ITEM = new Event("player_releases_item", 3, false) { @Override public void onItemAction(ServerPlayerEntity player, Hand enumhand, ItemStack itemstack) @@ -474,8 +475,8 @@ public class CarpetEventServer ); }, player::getCommandSource); } - }, - PLAYER_FINISHED_USING_ITEM("player_finishes_using_item", 3, false) + }; + public static final Event PLAYER_FINISHED_USING_ITEM = new Event("player_finishes_using_item", 3, false) { @Override public void onItemAction(ServerPlayerEntity player, Hand enumhand, ItemStack itemstack) @@ -490,24 +491,24 @@ public class CarpetEventServer ); }, player::getCommandSource); } - }, - PLAYER_DROPS_ITEM("player_drops_item", 1, false) + }; + public static final Event PLAYER_DROPS_ITEM = new Event("player_drops_item", 1, false) { @Override public void onPlayerEvent(ServerPlayerEntity player) { handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource); } - }, - PLAYER_DROPS_STACK("player_drops_stack", 1, false) + }; + public static final Event PLAYER_DROPS_STACK = new Event("player_drops_stack", 1, false) { @Override public void onPlayerEvent(ServerPlayerEntity player) { handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource); } - }, - PLAYER_CHOOSES_RECIPE("player_chooses_recipe", 3, false) + }; + public static final Event PLAYER_CHOOSES_RECIPE = new Event("player_chooses_recipe", 3, false) { @Override public void onRecipeSelected(ServerPlayerEntity player, Identifier recipe, boolean fullStack) @@ -521,8 +522,8 @@ public class CarpetEventServer ); }, player::getCommandSource); } - }, - PLAYER_SWITCHES_SLOT("player_switches_slot", 3, false) + }; + public static final Event PLAYER_SWITCHES_SLOT = new Event("player_switches_slot", 3, false) { @Override public void onSlotSwitch(ServerPlayerEntity player, int from, int to) @@ -537,16 +538,16 @@ public class CarpetEventServer ); }, player::getCommandSource); } - }, - PLAYER_SWAPS_HANDS("player_swaps_hands", 1, false) + }; + public static final Event PLAYER_SWAPS_HANDS = new Event("player_swaps_hands", 1, false) { @Override public void onPlayerEvent(ServerPlayerEntity player) { handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource); } - }, - PLAYER_TAKES_DAMAGE("player_takes_damage", 4, false) + }; + public static final Event PLAYER_TAKES_DAMAGE = new Event("player_takes_damage", 4, false) { @Override public void onDamage(Entity target, float amount, DamageSource source) @@ -561,8 +562,8 @@ public class CarpetEventServer ); }, target::getCommandSource); } - }, - PLAYER_DEALS_DAMAGE("player_deals_damage", 3, false) + }; + public static final Event PLAYER_DEALS_DAMAGE = new Event("player_deals_damage", 3, false) { @Override public void onDamage(Entity target, float amount, DamageSource source) @@ -576,24 +577,24 @@ public class CarpetEventServer ); }, () -> source.getAttacker().getCommandSource()); } - }, - PLAYER_DIES("player_dies", 1, false) + }; + public static final Event PLAYER_DIES = new Event("player_dies", 1, false) { @Override public void onPlayerEvent(ServerPlayerEntity player) { handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource); } - }, - PLAYER_RESPAWNS("player_respawns", 1, false) + }; + public static final Event PLAYER_RESPAWNS = new Event("player_respawns", 1, false) { @Override public void onPlayerEvent(ServerPlayerEntity player) { handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource); } - }, - PLAYER_CHANGES_DIMENSION("player_changes_dimension", 5, false) + }; + public static final Event PLAYER_CHANGES_DIMENSION = new Event("player_changes_dimension", 5, false) { @Override public void onDimensionChange(ServerPlayerEntity player, Vec3d from, Vec3d to, RegistryKey fromDim, RegistryKey dimTo) @@ -612,15 +613,15 @@ public class CarpetEventServer ((c, t) -> toDimStr) ), player::getCommandSource); } - }, - PLAYER_CONNECTS("player_connects", 1, false) { + }; + public static final Event PLAYER_CONNECTS = new Event("player_connects", 1, false) { @Override public void onPlayerEvent(ServerPlayerEntity player) { handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource); } - }, - PLAYER_DISCONNECTS("player_disconnects", 2, false) { + }; + public static final Event PLAYER_DISCONNECTS = new Event("player_disconnects", 2, false) { @Override public void onPlayerMessage(ServerPlayerEntity player, String message) { @@ -629,8 +630,8 @@ public class CarpetEventServer ((c, t) -> new StringValue(message)) ), player::getCommandSource); } - }, - STATISTICS("statistic", 4, false) + }; + public static final Event STATISTICS = new Event("statistic", 4, false) { private Identifier getStatId(Stat stat) { @@ -653,8 +654,8 @@ public class CarpetEventServer ((c, t) -> new NumericValue(amount)) ), player::getCommandSource); } - }, - LIGHTNING("lightning", 2, true) + }; + public static final Event LIGHTNING = new Event("lightning", 2, true) { @Override public void onWorldEventFlag(ServerWorld world, BlockPos pos, int flag) @@ -666,18 +667,12 @@ public class CarpetEventServer ), () -> CarpetServer.minecraft_server.getCommandSource().withWorld(world) ); } - }, - ; + }; // on projectile thrown (arrow from bows, crossbows, tridents, snoballs, e-pearls public final String name; - public static final Map byName = new HashMap(){{ - for (Event e: Event.values()) - { - put(e.name, e); - } - }}; + public final CallbackList handler; public final boolean globalOnly; Event(String name, int reqArgs, boolean isGlobalOnly) @@ -685,6 +680,7 @@ public class CarpetEventServer this.name = name; this.handler = new CallbackList(reqArgs); this.globalOnly = isGlobalOnly; + byName.put(name, this); } public boolean isNeeded() { @@ -717,7 +713,7 @@ public class CarpetEventServer public CarpetEventServer(MinecraftServer server) { this.server = server; - for (Event e: Event.values()) + for (Event e: Event.byName.values()) e.handler.callList.clear(); } diff --git a/src/main/java/carpet/script/CarpetExpression.java b/src/main/java/carpet/script/CarpetExpression.java index 45af0217..87ef120d 100644 --- a/src/main/java/carpet/script/CarpetExpression.java +++ b/src/main/java/carpet/script/CarpetExpression.java @@ -177,14 +177,16 @@ public class CarpetExpression private final ServerCommandSource source; private final BlockPos origin; private final Expression expr; + // these are for extensions public Expression getExpr() {return expr;} + public ServerCommandSource getSource() {return source;} + public BlockPos getOrigin() {return origin;} + private static long tickStart = 0L; // dummy entity for dummy requirements in the loot tables (see snowball) private static FallingBlockEntity DUMMY_ENTITY = new FallingBlockEntity(EntityType.FALLING_BLOCK, null); public static final String MARKER_STRING = "__scarpet_marker"; - private static boolean stopAll = false; - private static final Map DIRECTION_MAP = Arrays.stream(Direction.values()).collect(Collectors.toMap(Direction::getName, (direction) -> direction)); private LazyValue booleanStateTest( @@ -3640,7 +3642,6 @@ public class CarpetExpression this.expr = new Expression(expression); this.expr.asAModule(module); - API_BlockManipulation(); API_EntityManipulation(); API_InventoryManipulation(); @@ -3649,6 +3650,7 @@ public class CarpetExpression API_Scoreboard(); API_Interapperability(); API_GaemMonitoring(); + CarpetServer.extensions.forEach(e -> e.scarpetApi(this)); } public boolean fillAndScanCommand(ScriptHost host, int x, int y, int z) diff --git a/src/main/java/carpet/script/EntityEventsGroup.java b/src/main/java/carpet/script/EntityEventsGroup.java index 8c9d914d..4776505e 100644 --- a/src/main/java/carpet/script/EntityEventsGroup.java +++ b/src/main/java/carpet/script/EntityEventsGroup.java @@ -12,24 +12,22 @@ import net.minecraft.entity.damage.DamageSource; import java.util.Arrays; import java.util.Collections; -import java.util.EnumMap; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.stream.Collectors; public class EntityEventsGroup { - private final Map> actions; - private Entity entity; + private final Map> actions; + private final Entity entity; public EntityEventsGroup(Entity e) { - actions = new EnumMap<>(EntityEventType.class); + actions = new HashMap<>(); entity = e; } - public void onEvent(EntityEventType type, Object... args) + public void onEvent(Event type, Object... args) { if (actions.isEmpty()) return; // most of the cases, trying to be nice Map actionSet = actions.get(type); @@ -49,7 +47,7 @@ public class EntityEventsGroup if (actionSet.isEmpty()) actions.remove(type); } - public void addEvent(EntityEventType type, CarpetContext cc, FunctionValue fun, List extraargs) + public void addEvent(Event type, CarpetContext cc, FunctionValue fun, List extraargs) { if (fun != null) { @@ -67,9 +65,10 @@ public class EntityEventsGroup } - public enum EntityEventType + public static class Event { - ON_DEATH("on_death", 1) + public static final Map byName = new HashMap<>(); + public static final Event ON_DEATH = new Event("on_death", 1) { @Override public List makeArgs(Entity entity, Object... providedArgs) @@ -79,10 +78,10 @@ public class EntityEventsGroup new StringValue((String)providedArgs[0]) ); } - }, - ON_REMOVED("on_removed", 0), - ON_TICK("on_tick", 0), - ON_DAMAGE("on_damaged", 3) + }; + public static final Event ON_REMOVED = new Event("on_removed", 0); + public static final Event ON_TICK = new Event("on_tick", 0); + public static final Event ON_DAMAGE = new Event("on_damaged", 3) { @Override public List makeArgs(Entity entity, Object... providedArgs) @@ -98,14 +97,13 @@ public class EntityEventsGroup } }; - public static final Map byName = Arrays.stream(EntityEventType.values()).collect(Collectors.toMap(ev -> ev.id, ev -> ev)); - public final int argcount; public final String id; - EntityEventType(String identifier, int args) + Event(String identifier, int args) { id = identifier; argcount = args+1; // entity is not extra + byName.put(identifier, this); } public CarpetEventServer.ScheduledCall create(CarpetContext cc, FunctionValue function, List extraArgs) { diff --git a/src/main/java/carpet/script/value/EntityValue.java b/src/main/java/carpet/script/value/EntityValue.java index c9e25556..4b0850a8 100644 --- a/src/main/java/carpet/script/value/EntityValue.java +++ b/src/main/java/carpet/script/value/EntityValue.java @@ -1154,7 +1154,7 @@ public class EntityValue extends Value public void setEvent(CarpetContext cc, String eventName, FunctionValue fun, List args) { - EntityEventsGroup.EntityEventType event = EntityEventsGroup.EntityEventType.byName.get(eventName); + EntityEventsGroup.Event event = EntityEventsGroup.Event.byName.get(eventName); if (event == null) throw new InternalExpressionException("Unknown entity event: " + eventName); ((EntityInterface)entity).getEventContainer().addEvent(event, cc, fun, args);