Events are defined statically in normal classes and added handler for extensions to add their built-ins

This commit is contained in:
gnembon committed 2020-09-08 23:47:31 -03:00
1 parent 815c75b827
commit 3105d5bb71
11 files changed
+126 -113

No files matched your search

+18
View File
@@ -1,5 +1,6 @@
package carpet; package carpet;
import carpet.script.CarpetExpression;
import carpet.settings.SettingsManager; import carpet.settings.SettingsManager;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
@@ -65,4 +66,21 @@ public interface CarpetExtension
default Map<String, String> canHasTranslations(String lang) { return null;} default Map<String, String> 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) {}
} }
@@ -335,7 +335,7 @@ public class ScriptCommand
private static int listEvents(ServerCommandSource source) private static int listEvents(ServerCommandSource source)
{ {
Messenger.m(source, "w Lists ALL event handlers:"); 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; boolean shownEvent = false;
for (CarpetEventServer.Callback c: event.handler.callList) for (CarpetEventServer.Callback c: event.handler.callList)
@@ -65,14 +65,14 @@ public abstract class Entity_scarpetEventsMixin implements EntityInterface
@Inject(method = "tick", at = @At("HEAD")) @Inject(method = "tick", at = @At("HEAD"))
private void onTickCall(CallbackInfo ci) private void onTickCall(CallbackInfo ci)
{ {
events.onEvent(EntityEventsGroup.EntityEventType.ON_TICK); events.onEvent(EntityEventsGroup.Event.ON_TICK);
} }
@Inject(method = "remove", at = @At("HEAD")) @Inject(method = "remove", at = @At("HEAD"))
private void onRemove(CallbackInfo ci) private void onRemove(CallbackInfo ci)
{ {
if (!removed) events.onEvent(EntityEventsGroup.EntityEventType.ON_REMOVED); if (!removed) events.onEvent(EntityEventsGroup.Event.ON_REMOVED);
} }
@@ -23,6 +23,6 @@ public abstract class FallingBlockEntity_scarpetEventsMixin extends Entity
private void onTickCall(CallbackInfo ci) private void onTickCall(CallbackInfo ci)
{ {
// calling extra on_tick because falling blocks do not fall back to super tick call // 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);
} }
} }
@@ -34,7 +34,7 @@ public abstract class LivingEntity_scarpetEventsMixin extends Entity implements
@Inject(method = "onDeath", at = @At("HEAD")) @Inject(method = "onDeath", at = @At("HEAD"))
private void onDeathCall(DamageSource damageSource_1, CallbackInfo ci) 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( @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) 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 // this is not applicable since its not a playr for sure
//if (entity instanceof ServerPlayerEntity && PLAYER_TAKES_DAMAGE.isNeeded()) //if (entity instanceof ServerPlayerEntity && PLAYER_TAKES_DAMAGE.isNeeded())
//{ //{
@@ -32,7 +32,7 @@ public abstract class PlayerEntity_scarpetEventsMixin extends LivingEntity
private void playerTakingDamage(DamageSource source, float amount, CallbackInfo ci) private void playerTakingDamage(DamageSource source, float amount, CallbackInfo ci)
{ {
// version of LivingEntity_scarpetEventsMixin::entityTakingDamage // 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()) if (PLAYER_TAKES_DAMAGE.isNeeded())
{ {
PLAYER_TAKES_DAMAGE.onDamage(this, amount, source); PLAYER_TAKES_DAMAGE.onDamage(this, amount, source);
@@ -15,7 +15,6 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.Vec3d;
import net.minecraft.util.registry.RegistryKey; import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraft.world.dimension.DimensionType;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.At;
@@ -72,7 +71,7 @@ public abstract class ServerPlayerEntity_scarpetEventMixin extends PlayerEntity
@Inject(method = "onDeath", at = @At("HEAD")) @Inject(method = "onDeath", at = @At("HEAD"))
private void onDeathEvent(DamageSource source, CallbackInfo ci) 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()) if (PLAYER_DIES.isNeeded())
{ {
PLAYER_DIES.onPlayerEvent((ServerPlayerEntity) (Object)this); PLAYER_DIES.onPlayerEvent((ServerPlayerEntity) (Object)this);
@@ -208,9 +208,10 @@ public class CarpetEventServer
} }
} }
public enum Event public static class Event
{ {
TICK("tick", 0, true) public static final Map<String, Event> byName = new HashMap<>();
public static final Event TICK = new Event("tick", 0, true)
{ {
@Override @Override
public void onTick() public void onTick()
@@ -220,8 +221,8 @@ public class CarpetEventServer
withWorld(CarpetServer.minecraft_server.getWorld(World.OVERWORLD)) 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 @Override
public void onTick() public void onTick()
@@ -231,8 +232,8 @@ public class CarpetEventServer
withWorld(CarpetServer.minecraft_server.getWorld(World.NETHER)) 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 @Override
public void onTick() public void onTick()
@@ -242,8 +243,8 @@ public class CarpetEventServer
withWorld(CarpetServer.minecraft_server.getWorld(World.END)) 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 @Override
public void onChunkGenerated(ServerWorld world, Chunk chunk) public void onChunkGenerated(ServerWorld world, Chunk chunk)
@@ -258,33 +259,33 @@ public class CarpetEventServer
}, () -> CarpetServer.minecraft_server.getCommandSource().withWorld(world) }, () -> 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 @Override
public void onPlayerEvent(ServerPlayerEntity player) 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 @Override
public void onPlayerEvent(ServerPlayerEntity player) 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 @Override
public void onPlayerEvent(ServerPlayerEntity player) 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 @Override
public void onMountControls(ServerPlayerEntity player, float strafeSpeed, float forwardSpeed, boolean jumping, boolean sneaking) 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)) ((c, t) -> new NumericValue(sneaking))
), player::getCommandSource); ), 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 @Override
public void onItemAction(ServerPlayerEntity player, Hand enumhand, ItemStack itemstack) public void onItemAction(ServerPlayerEntity player, Hand enumhand, ItemStack itemstack)
@@ -313,8 +314,8 @@ public class CarpetEventServer
); );
}, player::getCommandSource); }, 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 @Override
public void onBlockAction(ServerPlayerEntity player, BlockPos blockpos, Direction facing) public void onBlockAction(ServerPlayerEntity player, BlockPos blockpos, Direction facing)
@@ -328,8 +329,8 @@ public class CarpetEventServer
); );
}, player::getCommandSource); }, 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 @Override
public void onBlockHit(ServerPlayerEntity player, Hand enumhand, BlockHitResult hitRes)//ItemStack itemstack, Hand enumhand, BlockPos blockpos, Direction enumfacing, Vec3d vec3d) 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::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 @Override
public void onBlockHit(ServerPlayerEntity player, Hand enumhand, BlockHitResult hitRes) public void onBlockHit(ServerPlayerEntity player, Hand enumhand, BlockHitResult hitRes)
@@ -378,8 +379,8 @@ public class CarpetEventServer
); );
}, player::getCommandSource); }, 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 @Override
public void onBlockPlaced(ServerPlayerEntity player, BlockPos pos, Hand enumhand, ItemStack itemstack) 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)) ((c, t) -> new BlockValue(null, player.getServerWorld(), pos))
), player::getCommandSource); ), 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 @Override
public void onBlockBroken(ServerPlayerEntity player, BlockPos pos, BlockState previousBS) public void onBlockBroken(ServerPlayerEntity player, BlockPos pos, BlockState previousBS)
@@ -402,8 +403,8 @@ public class CarpetEventServer
((c, t) -> new BlockValue(previousBS, player.getServerWorld(), pos)) ((c, t) -> new BlockValue(previousBS, player.getServerWorld(), pos))
), player::getCommandSource); ), 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 @Override
public void onEntityAction(ServerPlayerEntity player, Entity entity, Hand enumhand) 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")) ((c, t) -> new StringValue(enumhand==Hand.MAIN_HAND?"mainhand":"offhand"))
), player::getCommandSource); ), 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 @Override
public void onEntityAction(ServerPlayerEntity player, Entity entity, Hand enumhand) public void onEntityAction(ServerPlayerEntity player, Entity entity, Hand enumhand)
@@ -425,41 +426,41 @@ public class CarpetEventServer
((c, t) -> new EntityValue(entity)) ((c, t) -> new EntityValue(entity))
), player::getCommandSource); ), 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 @Override
public void onPlayerEvent(ServerPlayerEntity player) public void onPlayerEvent(ServerPlayerEntity player)
{ {
handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource); 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 @Override
public void onPlayerEvent(ServerPlayerEntity player) public void onPlayerEvent(ServerPlayerEntity player)
{ {
handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource); 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 @Override
public void onPlayerEvent(ServerPlayerEntity player) public void onPlayerEvent(ServerPlayerEntity player)
{ {
handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource); 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 @Override
public void onPlayerEvent(ServerPlayerEntity player) public void onPlayerEvent(ServerPlayerEntity player)
{ {
handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource); 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 @Override
public void onItemAction(ServerPlayerEntity player, Hand enumhand, ItemStack itemstack) public void onItemAction(ServerPlayerEntity player, Hand enumhand, ItemStack itemstack)
@@ -474,8 +475,8 @@ public class CarpetEventServer
); );
}, player::getCommandSource); }, 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 @Override
public void onItemAction(ServerPlayerEntity player, Hand enumhand, ItemStack itemstack) public void onItemAction(ServerPlayerEntity player, Hand enumhand, ItemStack itemstack)
@@ -490,24 +491,24 @@ public class CarpetEventServer
); );
}, player::getCommandSource); }, 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 @Override
public void onPlayerEvent(ServerPlayerEntity player) public void onPlayerEvent(ServerPlayerEntity player)
{ {
handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource); 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 @Override
public void onPlayerEvent(ServerPlayerEntity player) public void onPlayerEvent(ServerPlayerEntity player)
{ {
handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource); 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 @Override
public void onRecipeSelected(ServerPlayerEntity player, Identifier recipe, boolean fullStack) public void onRecipeSelected(ServerPlayerEntity player, Identifier recipe, boolean fullStack)
@@ -521,8 +522,8 @@ public class CarpetEventServer
); );
}, player::getCommandSource); }, 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 @Override
public void onSlotSwitch(ServerPlayerEntity player, int from, int to) public void onSlotSwitch(ServerPlayerEntity player, int from, int to)
@@ -537,16 +538,16 @@ public class CarpetEventServer
); );
}, player::getCommandSource); }, 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 @Override
public void onPlayerEvent(ServerPlayerEntity player) public void onPlayerEvent(ServerPlayerEntity player)
{ {
handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource); 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 @Override
public void onDamage(Entity target, float amount, DamageSource source) public void onDamage(Entity target, float amount, DamageSource source)
@@ -561,8 +562,8 @@ public class CarpetEventServer
); );
}, target::getCommandSource); }, 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 @Override
public void onDamage(Entity target, float amount, DamageSource source) public void onDamage(Entity target, float amount, DamageSource source)
@@ -576,24 +577,24 @@ public class CarpetEventServer
); );
}, () -> source.getAttacker().getCommandSource()); }, () -> source.getAttacker().getCommandSource());
} }
}, };
PLAYER_DIES("player_dies", 1, false) public static final Event PLAYER_DIES = new Event("player_dies", 1, false)
{ {
@Override @Override
public void onPlayerEvent(ServerPlayerEntity player) public void onPlayerEvent(ServerPlayerEntity player)
{ {
handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource); 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 @Override
public void onPlayerEvent(ServerPlayerEntity player) public void onPlayerEvent(ServerPlayerEntity player)
{ {
handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource); 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 @Override
public void onDimensionChange(ServerPlayerEntity player, Vec3d from, Vec3d to, RegistryKey<World> fromDim, RegistryKey<World> dimTo) public void onDimensionChange(ServerPlayerEntity player, Vec3d from, Vec3d to, RegistryKey<World> fromDim, RegistryKey<World> dimTo)
@@ -612,15 +613,15 @@ public class CarpetEventServer
((c, t) -> toDimStr) ((c, t) -> toDimStr)
), player::getCommandSource); ), player::getCommandSource);
} }
}, };
PLAYER_CONNECTS("player_connects", 1, false) { public static final Event PLAYER_CONNECTS = new Event("player_connects", 1, false) {
@Override @Override
public void onPlayerEvent(ServerPlayerEntity player) public void onPlayerEvent(ServerPlayerEntity player)
{ {
handler.call( () -> Collections.singletonList(((c, t) -> new EntityValue(player))), player::getCommandSource); 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 @Override
public void onPlayerMessage(ServerPlayerEntity player, String message) public void onPlayerMessage(ServerPlayerEntity player, String message)
{ {
@@ -629,8 +630,8 @@ public class CarpetEventServer
((c, t) -> new StringValue(message)) ((c, t) -> new StringValue(message))
), player::getCommandSource); ), player::getCommandSource);
} }
}, };
STATISTICS("statistic", 4, false) public static final Event STATISTICS = new Event("statistic", 4, false)
{ {
private <T> Identifier getStatId(Stat<T> stat) private <T> Identifier getStatId(Stat<T> stat)
{ {
@@ -653,8 +654,8 @@ public class CarpetEventServer
((c, t) -> new NumericValue(amount)) ((c, t) -> new NumericValue(amount))
), player::getCommandSource); ), player::getCommandSource);
} }
}, };
LIGHTNING("lightning", 2, true) public static final Event LIGHTNING = new Event("lightning", 2, true)
{ {
@Override @Override
public void onWorldEventFlag(ServerWorld world, BlockPos pos, int flag) public void onWorldEventFlag(ServerWorld world, BlockPos pos, int flag)
@@ -666,18 +667,12 @@ public class CarpetEventServer
), () -> CarpetServer.minecraft_server.getCommandSource().withWorld(world) ), () -> CarpetServer.minecraft_server.getCommandSource().withWorld(world)
); );
} }
}, };
;
// on projectile thrown (arrow from bows, crossbows, tridents, snoballs, e-pearls // on projectile thrown (arrow from bows, crossbows, tridents, snoballs, e-pearls
public final String name; public final String name;
public static final Map<String, Event> byName = new HashMap<String, Event>(){{
for (Event e: Event.values())
{
put(e.name, e);
}
}};
public final CallbackList handler; public final CallbackList handler;
public final boolean globalOnly; public final boolean globalOnly;
Event(String name, int reqArgs, boolean isGlobalOnly) Event(String name, int reqArgs, boolean isGlobalOnly)
@@ -685,6 +680,7 @@ public class CarpetEventServer
this.name = name; this.name = name;
this.handler = new CallbackList(reqArgs); this.handler = new CallbackList(reqArgs);
this.globalOnly = isGlobalOnly; this.globalOnly = isGlobalOnly;
byName.put(name, this);
} }
public boolean isNeeded() public boolean isNeeded()
{ {
@@ -717,7 +713,7 @@ public class CarpetEventServer
public CarpetEventServer(MinecraftServer server) public CarpetEventServer(MinecraftServer server)
{ {
this.server = server; this.server = server;
for (Event e: Event.values()) for (Event e: Event.byName.values())
e.handler.callList.clear(); e.handler.callList.clear();
} }
@@ -177,14 +177,16 @@ public class CarpetExpression
private final ServerCommandSource source; private final ServerCommandSource source;
private final BlockPos origin; private final BlockPos origin;
private final Expression expr; private final Expression expr;
// these are for extensions
public Expression getExpr() {return expr;} public Expression getExpr() {return expr;}
public ServerCommandSource getSource() {return source;}
public BlockPos getOrigin() {return origin;}
private static long tickStart = 0L; private static long tickStart = 0L;
// dummy entity for dummy requirements in the loot tables (see snowball) // dummy entity for dummy requirements in the loot tables (see snowball)
private static FallingBlockEntity DUMMY_ENTITY = new FallingBlockEntity(EntityType.FALLING_BLOCK, null); private static FallingBlockEntity DUMMY_ENTITY = new FallingBlockEntity(EntityType.FALLING_BLOCK, null);
public static final String MARKER_STRING = "__scarpet_marker"; public static final String MARKER_STRING = "__scarpet_marker";
private static boolean stopAll = false;
private static final Map<String, Direction> DIRECTION_MAP = Arrays.stream(Direction.values()).collect(Collectors.toMap(Direction::getName, (direction) -> direction)); private static final Map<String, Direction> DIRECTION_MAP = Arrays.stream(Direction.values()).collect(Collectors.toMap(Direction::getName, (direction) -> direction));
private LazyValue booleanStateTest( private LazyValue booleanStateTest(
@@ -3640,7 +3642,6 @@ public class CarpetExpression
this.expr = new Expression(expression); this.expr = new Expression(expression);
this.expr.asAModule(module); this.expr.asAModule(module);
API_BlockManipulation(); API_BlockManipulation();
API_EntityManipulation(); API_EntityManipulation();
API_InventoryManipulation(); API_InventoryManipulation();
@@ -3649,6 +3650,7 @@ public class CarpetExpression
API_Scoreboard(); API_Scoreboard();
API_Interapperability(); API_Interapperability();
API_GaemMonitoring(); API_GaemMonitoring();
CarpetServer.extensions.forEach(e -> e.scarpetApi(this));
} }
public boolean fillAndScanCommand(ScriptHost host, int x, int y, int z) public boolean fillAndScanCommand(ScriptHost host, int x, int y, int z)
@@ -12,24 +12,22 @@ import net.minecraft.entity.damage.DamageSource;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
public class EntityEventsGroup public class EntityEventsGroup
{ {
private final Map<EntityEventType, Map<String, CarpetEventServer.ScheduledCall>> actions; private final Map<Event, Map<String, CarpetEventServer.ScheduledCall>> actions;
private Entity entity; private final Entity entity;
public EntityEventsGroup(Entity e) public EntityEventsGroup(Entity e)
{ {
actions = new EnumMap<>(EntityEventType.class); actions = new HashMap<>();
entity = e; 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 if (actions.isEmpty()) return; // most of the cases, trying to be nice
Map<String, CarpetEventServer.ScheduledCall> actionSet = actions.get(type); Map<String, CarpetEventServer.ScheduledCall> actionSet = actions.get(type);
@@ -49,7 +47,7 @@ public class EntityEventsGroup
if (actionSet.isEmpty()) actions.remove(type); if (actionSet.isEmpty()) actions.remove(type);
} }
public void addEvent(EntityEventType type, CarpetContext cc, FunctionValue fun, List<Value> extraargs) public void addEvent(Event type, CarpetContext cc, FunctionValue fun, List<Value> extraargs)
{ {
if (fun != null) 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<String, Event> byName = new HashMap<>();
public static final Event ON_DEATH = new Event("on_death", 1)
{ {
@Override @Override
public List<Value> makeArgs(Entity entity, Object... providedArgs) public List<Value> makeArgs(Entity entity, Object... providedArgs)
@@ -79,10 +78,10 @@ public class EntityEventsGroup
new StringValue((String)providedArgs[0]) new StringValue((String)providedArgs[0])
); );
} }
}, };
ON_REMOVED("on_removed", 0), public static final Event ON_REMOVED = new Event("on_removed", 0);
ON_TICK("on_tick", 0), public static final Event ON_TICK = new Event("on_tick", 0);
ON_DAMAGE("on_damaged", 3) public static final Event ON_DAMAGE = new Event("on_damaged", 3)
{ {
@Override @Override
public List<Value> makeArgs(Entity entity, Object... providedArgs) public List<Value> makeArgs(Entity entity, Object... providedArgs)
@@ -98,14 +97,13 @@ public class EntityEventsGroup
} }
}; };
public static final Map<String, EntityEventType> byName = Arrays.stream(EntityEventType.values()).collect(Collectors.toMap(ev -> ev.id, ev -> ev));
public final int argcount; public final int argcount;
public final String id; public final String id;
EntityEventType(String identifier, int args) Event(String identifier, int args)
{ {
id = identifier; id = identifier;
argcount = args+1; // entity is not extra argcount = args+1; // entity is not extra
byName.put(identifier, this);
} }
public CarpetEventServer.ScheduledCall create(CarpetContext cc, FunctionValue function, List<Value> extraArgs) public CarpetEventServer.ScheduledCall create(CarpetContext cc, FunctionValue function, List<Value> extraArgs)
{ {
@@ -1154,7 +1154,7 @@ public class EntityValue extends Value
public void setEvent(CarpetContext cc, String eventName, FunctionValue fun, List<Value> args) public void setEvent(CarpetContext cc, String eventName, FunctionValue fun, List<Value> args)
{ {
EntityEventsGroup.EntityEventType event = EntityEventsGroup.EntityEventType.byName.get(eventName); EntityEventsGroup.Event event = EntityEventsGroup.Event.byName.get(eventName);
if (event == null) if (event == null)
throw new InternalExpressionException("Unknown entity event: " + eventName); throw new InternalExpressionException("Unknown entity event: " + eventName);
((EntityInterface)entity).getEventContainer().addEvent(event, cc, fun, args); ((EntityInterface)entity).getEventContainer().addEvent(event, cc, fun, args);