changes to entity load handler keeping it backwards compatible
This commit is contained in:
1 parent
85d60b283f
commit
448fde1f36
4 files changed
+83
-13
No files matched your search
@@ -23,6 +23,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
|
||||
|
||||
import static carpet.script.CarpetEventServer.Event.ENTITY_HANDLER;
|
||||
import static carpet.script.CarpetEventServer.Event.ENTITY_LOAD;
|
||||
import static carpet.script.CarpetEventServer.Event.EXPLOSION;
|
||||
import static carpet.script.CarpetEventServer.Event.LIGHTNING;
|
||||
@@ -43,24 +44,50 @@ public class ServerWorld_scarpetMixin implements ServerWorldInterface
|
||||
if (LIGHTNING.isNeeded()) LIGHTNING.onWorldEventFlag((ServerWorld) (Object)this, blockPos, bl2?1:0);
|
||||
}
|
||||
|
||||
|
||||
private ThreadLocal<Boolean> entityJustLoaded = ThreadLocal.withInitial(() -> false);
|
||||
|
||||
@Inject(method = "loadEntityUnchecked", at = @At(
|
||||
value = "INVOKE",
|
||||
target = "Lnet/minecraft/server/world/ServerChunkManager;loadEntity(Lnet/minecraft/entity/Entity;)V"
|
||||
))
|
||||
private void onEntityAddedToWorld(Entity entity, CallbackInfo ci)
|
||||
{
|
||||
CarpetEventServer.Event event = ENTITY_LOAD.get(entity.getType());
|
||||
boolean justLoaded = entityJustLoaded.get();
|
||||
entityJustLoaded.set(false);
|
||||
CarpetEventServer.Event event = ENTITY_HANDLER.get(entity.getType());
|
||||
if (event != null)
|
||||
{
|
||||
if (event.isNeeded())
|
||||
{
|
||||
event.onEntityAction(entity);
|
||||
event.onEntityAction(entity, !justLoaded);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
CarpetSettings.LOG.error("Failed to handle entity "+entity.getType().getTranslationKey());
|
||||
}
|
||||
|
||||
// deprecated usage
|
||||
event = ENTITY_LOAD.get(entity.getType());
|
||||
if (event != null)
|
||||
{
|
||||
if (event.isNeeded())
|
||||
{
|
||||
event.onEntityAction(entity, true);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
CarpetSettings.LOG.error("Failed to handle entity "+entity.getType().getTranslationKey());
|
||||
}
|
||||
}
|
||||
|
||||
// 1.16only
|
||||
@Inject(method = "loadEntity", at= @At("HEAD"))
|
||||
private void onSideLoaded(Entity entity, CallbackInfoReturnable<Boolean> cir)
|
||||
{
|
||||
entityJustLoaded.set(true);
|
||||
}
|
||||
|
||||
@Inject(method = "createExplosion", at = @At("HEAD"))
|
||||
|
||||
@@ -926,18 +926,20 @@ public class CarpetEventServer
|
||||
}
|
||||
};
|
||||
|
||||
@Deprecated
|
||||
public static String getEntityLoadEventName(EntityType<? extends Entity> et)
|
||||
{
|
||||
return "entity_loaded_" + ValueConversions.of(Registry.ENTITY_TYPE.getId(et)).getString();
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static final Map<EntityType<? extends Entity>, Event> ENTITY_LOAD= new HashMap<EntityType<? extends Entity>, Event>() {{
|
||||
EntityType.get("zombie");
|
||||
Registry.ENTITY_TYPE.forEach(et -> {
|
||||
put(et, new Event(getEntityLoadEventName(et), 1, true, false)
|
||||
{
|
||||
@Override
|
||||
public void onEntityAction(Entity entity)
|
||||
public void onEntityAction(Entity entity, boolean created)
|
||||
{
|
||||
handler.call(
|
||||
() -> Collections.singletonList(new EntityValue(entity)),
|
||||
@@ -948,6 +950,28 @@ public class CarpetEventServer
|
||||
});
|
||||
}};
|
||||
|
||||
public static String getEntityHandlerEventName(EntityType<? extends Entity> et)
|
||||
{
|
||||
return "entity_handler_" + ValueConversions.of(Registry.ENTITY_TYPE.getId(et)).getString();
|
||||
}
|
||||
|
||||
public static final Map<EntityType<? extends Entity>, Event> ENTITY_HANDLER= new HashMap<EntityType<? extends Entity>, Event>() {{
|
||||
EntityType.get("zombie");
|
||||
Registry.ENTITY_TYPE.forEach(et -> {
|
||||
put(et, new Event(getEntityHandlerEventName(et), 2, true, false)
|
||||
{
|
||||
@Override
|
||||
public void onEntityAction(Entity entity, boolean created)
|
||||
{
|
||||
handler.call(
|
||||
() -> Arrays.asList(new EntityValue(entity), BooleanValue.of(created)),
|
||||
() -> CarpetServer.minecraft_server.getCommandSource().withWorld((ServerWorld) entity.world).withLevel(CarpetSettings.runPermissionLevel)
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
}};
|
||||
|
||||
// on projectile thrown (arrow from bows, crossbows, tridents, snoballs, e-pearls
|
||||
|
||||
public final String name;
|
||||
@@ -1038,7 +1062,7 @@ public class CarpetEventServer
|
||||
public void onBlockPlaced(ServerPlayerEntity player, BlockPos pos, Hand enumhand, ItemStack itemstack) { }
|
||||
public void onEntityHandAction(ServerPlayerEntity player, Entity entity, Hand enumhand) { }
|
||||
public void onHandAction(ServerPlayerEntity player, Hand enumhand) { }
|
||||
public void onEntityAction(Entity entity) { }
|
||||
public void onEntityAction(Entity entity, boolean created) { }
|
||||
public void onDimensionChange(ServerPlayerEntity player, Vec3d from, Vec3d to, RegistryKey<World> fromDim, RegistryKey<World> dimTo) {}
|
||||
public void onDamage(Entity target, float amount, DamageSource source) { }
|
||||
public void onRecipeSelected(ServerPlayerEntity player, Identifier recipe, boolean fullStack) {}
|
||||
|
||||
@@ -275,10 +275,21 @@ public class Entities {
|
||||
if (funArg.function == null)
|
||||
{
|
||||
types.forEach(et -> events.removeBuiltInEvent(CarpetEventServer.Event.getEntityLoadEventName(et), (CarpetScriptHost) c.host));
|
||||
types.forEach(et -> events.removeBuiltInEvent(CarpetEventServer.Event.getEntityHandlerEventName(et), (CarpetScriptHost) c.host));
|
||||
}
|
||||
else
|
||||
{
|
||||
types.forEach(et -> events.addBuiltInEvent(CarpetEventServer.Event.getEntityLoadEventName(et), c.host, funArg.function, funArg.args));
|
||||
///compat
|
||||
int argno = funArg.function.getArguments().size() - funArg.args.size();
|
||||
if (argno == 1)
|
||||
{
|
||||
c.host.issueDeprecation("entity_load_handler() with single argument callback");
|
||||
types.forEach(et -> events.addBuiltInEvent(CarpetEventServer.Event.getEntityLoadEventName(et), c.host, funArg.function, funArg.args));
|
||||
}
|
||||
else
|
||||
{
|
||||
types.forEach(et -> events.addBuiltInEvent(CarpetEventServer.Event.getEntityHandlerEventName(et), c.host, funArg.function, funArg.args));
|
||||
}
|
||||
}
|
||||
return new NumericValue(types.size());
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user