diff --git a/docs/scarpet/api/BlocksAndWorldAccess.md b/docs/scarpet/api/BlocksAndWorldAccess.md index 17f16470..522faff2 100644 --- a/docs/scarpet/api/BlocksAndWorldAccess.md +++ b/docs/scarpet/api/BlocksAndWorldAccess.md @@ -712,3 +712,10 @@ It returns a `map` with a report indicating how many chunks were affected, and h * `layer_count_`: number of chunks for which a `` generation step has been performed * `layer_time_`: cumulative time for all chunks spent on generating `` step +### add_chunk_ticket(pos, type, radius) + +Adds a chunk ticket at a position, which makes the game to keep the designated area centered around +`pos` with radius of `radius` loaded for a predefined amount of ticks, defined by `type`. Allowed types +are `portal`: 300 ticks, `teleport`: 5 ticks, and `unknown`: 1 tick. Radius can be from 1 to 32 ticks. + +This function is tentative - will likely change when chunk ticket API is properly fleshed out. \ No newline at end of file diff --git a/docs/scarpet/resources/editors/idea/2.txt b/docs/scarpet/resources/editors/idea/2.txt index e62bf3e0..81803965 100644 --- a/docs/scarpet/resources/editors/idea/2.txt +++ b/docs/scarpet/resources/editors/idea/2.txt @@ -1,4 +1,5 @@ air +add_chunk_ticket biome blast_resistance block diff --git a/docs/scarpet/resources/editors/npp/scarpet.xml b/docs/scarpet/resources/editors/npp/scarpet.xml index 8491a743..da7e1bf4 100644 --- a/docs/scarpet/resources/editors/npp/scarpet.xml +++ b/docs/scarpet/resources/editors/npp/scarpet.xml @@ -56,6 +56,7 @@ blocks_movement block_sound material map_colour property block_properties block_data poi set_poi place_item set_biome biome loaded_status generation_status inhabited_time spawn_potential chunk_tickets structure_eligibility structures structure_references set_structure reset_chunk reload_chunk + add_chunk_ticket scoreboard scoreboard_add scoreboard_remove scoreboard_display diff --git a/src/main/java/carpet/script/CarpetExpression.java b/src/main/java/carpet/script/CarpetExpression.java index 370b5faf..2e155927 100644 --- a/src/main/java/carpet/script/CarpetExpression.java +++ b/src/main/java/carpet/script/CarpetExpression.java @@ -88,6 +88,7 @@ import net.minecraft.server.MinecraftServer; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.world.ChunkTicket; +import net.minecraft.server.world.ChunkTicketType; import net.minecraft.server.world.ServerWorld; import net.minecraft.sound.BlockSoundGroup; import net.minecraft.sound.SoundCategory; @@ -1383,7 +1384,35 @@ public class CarpetExpression return (_c, _t) -> ret; }); + this.expr.addLazyFunction("add_chunk_ticket", -1, (c, t, lv) -> + { + CarpetContext cc = (CarpetContext)c; + BlockArgument locator = BlockArgument.findIn(cc, lv, 0); + BlockPos pos = locator.block.getPos(); + if (lv.size() != locator.offset+2) throw new InternalExpressionException("'add_chunk_ticket' requires block position, ticket type and radius"); + String type = lv.get(locator.offset).evalValue(c).getString(); + ChunkTicketType ticket = ticketTypes.get(type.toLowerCase(Locale.ROOT)); + if (ticket == null) throw new InternalExpressionException("Unknown ticket type: "+type); + int radius = NumericValue.asNumber(lv.get(locator.offset+1).evalValue(c)).getInt(); + if (radius < 1 || radius > 32) throw new InternalExpressionException("Ticket radius should be between 1 and 32 chunks"); + // due to types we will wing it: + ChunkPos target = new ChunkPos(pos); + if (ticket == ChunkTicketType.PORTAL) + cc.s.getWorld().getChunkManager().addTicket(ChunkTicketType.PORTAL, target, radius, pos); + else if (ticket == ChunkTicketType.POST_TELEPORT) + cc.s.getWorld().getChunkManager().addTicket(ChunkTicketType.POST_TELEPORT, target, radius, 1); + else + cc.s.getWorld().getChunkManager().addTicket(ChunkTicketType.UNKNOWN, target, radius, target); + Value ret = new NumericValue(ticket.getExpiryTicks()); + return (_c, _t) -> ret; + }); + } + private Map> ticketTypes = new HashMap>(){{ + put("portal", ChunkTicketType.PORTAL); + put("teleport", ChunkTicketType.POST_TELEPORT); + put("unknown", ChunkTicketType.UNKNOWN); + }}; private static String getScoreboardKeyFromValue(Value keyValue) {