Added __on_chunk_unloaded scarpet event (#1451)

* Fixes #1403

* Use concrete type
This commit is contained in:
Luca Bertozzi authored and GitHub committed 2022-05-27 20:24:25 +02:00
1 parent d088ee477e
commit 8c7dfa133e
3 files changed
+27

No files matched your search

+3
View File
@@ -103,6 +103,9 @@ this should not happen, but let you be warned.
Called right after a chunk at a given coordinate is loaded. All newly generated chunks are considered loaded as well. Called right after a chunk at a given coordinate is loaded. All newly generated chunks are considered loaded as well.
`x` and `z` correspond to the lowest x and z coordinates in the chunk. `x` and `z` correspond to the lowest x and z coordinates in the chunk.
### `__on_chunk_unloaded(x, z)`
Called right before a chunk at the given coordinates is unloaded. `x` and `z` correspond to the lowest x and z coordinates in the chunk.
### `__on_lightning(block, mode)` ### `__on_lightning(block, mode)`
Triggered right after a lightning strikes. Lightning entity as well as potential horseman trap would Triggered right after a lightning strikes. Lightning entity as well as potential horseman trap would
already be spawned at that point. `mode` is `true` if the lightning did cause a trap to spawn. already be spawned at that point. `mode` is `true` if the lightning did cause a trap to spawn.
@@ -26,6 +26,8 @@ import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
import static carpet.script.CarpetEventServer.Event.EXPLOSION; import static carpet.script.CarpetEventServer.Event.EXPLOSION;
import static carpet.script.CarpetEventServer.Event.LIGHTNING; import static carpet.script.CarpetEventServer.Event.LIGHTNING;
import static carpet.script.CarpetEventServer.Event.CHUNK_UNLOADED;
@Mixin(ServerLevel.class) @Mixin(ServerLevel.class)
public class ServerLevel_scarpetMixin implements ServerWorldInterface public class ServerLevel_scarpetMixin implements ServerWorldInterface
@@ -50,6 +52,16 @@ public class ServerLevel_scarpetMixin implements ServerWorldInterface
EXPLOSION.onExplosion((ServerLevel) (Object)this, entity, null, d, e, f, g, bl, null, null, destructionType); EXPLOSION.onExplosion((ServerLevel) (Object)this, entity, null, d, e, f, g, bl, null, null, destructionType);
} }
@Inject(method = "unload", at = @At("HEAD"))
private void handleChunkUnload(LevelChunk levelChunk, CallbackInfo ci)
{
if (CHUNK_UNLOADED.isNeeded())
{
ServerLevel level = (ServerLevel)((Object)this);
CHUNK_UNLOADED.onChunkEvent(level, levelChunk.getPos(), false);
}
}
@Final @Final
@Shadow @Shadow
private ServerLevelData serverLevelData; private ServerLevelData serverLevelData;
@@ -467,6 +467,18 @@ public class CarpetEventServer
} }
}; };
public static final Event CHUNK_UNLOADED = new Event("chunk_unloaded", 2, true)
{
@Override
public void onChunkEvent(ServerLevel world, ChunkPos chPos, boolean generated)
{
handler.call(
() -> Arrays.asList(new NumericValue(chPos.x << 4), new NumericValue(chPos.z << 4)),
() -> CarpetServer.minecraft_server.createCommandSourceStack().withLevel(world)
);
}
};
public static final Event PLAYER_JUMPS = new Event("player_jumps", 1, false) public static final Event PLAYER_JUMPS = new Event("player_jumps", 1, false)
{ {
@Override @Override