From 8b7e9143da937e57cc5ecae4b375a534d55c2871 Mon Sep 17 00:00:00 2001 From: gnembon Date: Sat, 14 Sep 2019 23:05:20 -0300 Subject: [PATCH] added in_dimension to the API --- .../java/carpet/script/CarpetEventServer.java | 16 +++--- .../java/carpet/script/CarpetExpression.java | 53 +++++++++++++++++-- .../java/carpet/script/value/BlockValue.java | 9 ++-- .../java/carpet/script/value/EntityValue.java | 6 +-- 4 files changed, 66 insertions(+), 18 deletions(-) diff --git a/src/main/java/carpet/script/CarpetEventServer.java b/src/main/java/carpet/script/CarpetEventServer.java index 461dde8f..fb352c64 100644 --- a/src/main/java/carpet/script/CarpetEventServer.java +++ b/src/main/java/carpet/script/CarpetEventServer.java @@ -302,7 +302,7 @@ public class CarpetEventServer { eventHandlers.get("player_clicks_block").call( () -> Arrays.asList( ((c, t) -> new EntityValue(player)), - ((c, t) -> new BlockValue(null, player.world, blockpos)), + ((c, t) -> new BlockValue(null, player.getServerWorld(), blockpos)), ((c, t) -> new StringValue(facing.getName())) ), player::getCommandSource); } @@ -319,7 +319,7 @@ public class CarpetEventServer ((c, t) -> new EntityValue(player)), ((c, t) -> ListValue.fromItemStack(itemstack)), ((c, t) -> new StringValue(enumhand == Hand.MAIN_HAND ? "mainhand" : "offhand")), - ((c, t) -> new BlockValue(null, player.world, blockpos)), + ((c, t) -> new BlockValue(null, player.getServerWorld(), blockpos)), ((c, t) -> new StringValue(enumfacing.getName())), ((c, t) -> ListValue.of( new NumericValue(vec3d.x), @@ -330,11 +330,11 @@ public class CarpetEventServer }, player::getCommandSource); } - public void onBlockBroken(PlayerEntity player, BlockPos pos, BlockState previousBS) + public void onBlockBroken(ServerPlayerEntity player, BlockPos pos, BlockState previousBS) { eventHandlers.get("player_breaks_block").call( () -> Arrays.asList( ((c, t) -> new EntityValue(player)), - ((c, t) -> new BlockValue(previousBS, player.getEntityWorld(), pos)) + ((c, t) -> new BlockValue(previousBS, player.getServerWorld(), pos)) ), player::getCommandSource); } @@ -355,22 +355,22 @@ public class CarpetEventServer ), player::getCommandSource); } - public void onStartSneaking(PlayerEntity player) + public void onStartSneaking(ServerPlayerEntity player) { eventHandlers.get("player_starts_sneaking").call( () -> Arrays.asList(((c, t) -> new EntityValue(player))), player::getCommandSource); } - public void onStopSneaking(PlayerEntity player) + public void onStopSneaking(ServerPlayerEntity player) { eventHandlers.get("player_stops_sneaking").call( () -> Arrays.asList(((c, t) -> new EntityValue(player))), player::getCommandSource); } - public void onStartSprinting(PlayerEntity player) + public void onStartSprinting(ServerPlayerEntity player) { eventHandlers.get("player_starts_sprinting").call( () -> Arrays.asList(((c, t) -> new EntityValue(player))), player::getCommandSource); } - public void onStopSprinting(PlayerEntity player) + public void onStopSprinting(ServerPlayerEntity player) { eventHandlers.get("player_stops_sprinting").call( () -> Arrays.asList(((c, t) -> new EntityValue(player))), player::getCommandSource); } diff --git a/src/main/java/carpet/script/CarpetExpression.java b/src/main/java/carpet/script/CarpetExpression.java index 70558a8c..a9434a36 100644 --- a/src/main/java/carpet/script/CarpetExpression.java +++ b/src/main/java/carpet/script/CarpetExpression.java @@ -60,7 +60,6 @@ import net.minecraft.sound.SoundCategory; import net.minecraft.state.StateFactory; import net.minecraft.state.property.Property; import net.minecraft.text.LiteralText; -import net.minecraft.text.TranslatableText; import net.minecraft.util.Clearable; import net.minecraft.util.Identifier; import net.minecraft.util.math.BlockPos; @@ -74,6 +73,7 @@ import net.minecraft.world.LightType; import net.minecraft.world.World; import net.minecraft.world.biome.Biome; import net.minecraft.world.chunk.Chunk; +import net.minecraft.world.dimension.DimensionType; import net.minecraft.world.loot.context.LootContext; import net.minecraft.world.loot.context.LootContextParameters; import org.apache.commons.lang3.tuple.Pair; @@ -797,7 +797,7 @@ public class CarpetExpression this.expr.addLazyFunction("set", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext)c; - World world = cc.s.getWorld(); + ServerWorld world = cc.s.getWorld(); if (lv.size() < 2 || lv.size() % 2 == 1) throw new InternalExpressionException("set block should have at least 2 params and odd attributes"); BlockValue.LocatorResult targetLocator = BlockValue.fromParams(cc, lv, 0); @@ -1981,7 +1981,7 @@ public class CarpetExpression { BlockPos center = BlockValue.fromParams((CarpetContext) c, lv,0).block.getPos(); - World world = ((CarpetContext) c).s.getWorld(); + ServerWorld world = ((CarpetContext) c).s.getWorld(); List neighbours = new ArrayList<>(); neighbours.add(new BlockValue(null, world, center.up())); @@ -2679,6 +2679,53 @@ public class CarpetExpression return (cc, tt) -> retval; }); + this.expr.addLazyFunction("in_dimension", 2, (c, t, lv) -> { + ServerCommandSource outerSource = ((CarpetContext)c).s; + Value dimensionValue = lv.get(0).evalValue(c); + ServerCommandSource innerSource = outerSource; + if (dimensionValue instanceof EntityValue) + { + innerSource = outerSource.withWorld((ServerWorld) ((EntityValue)dimensionValue).getEntity().getEntityWorld()); + } + else if (dimensionValue instanceof BlockValue) + { + BlockValue bv = (BlockValue)dimensionValue; + if (bv.getWorld() != null) + { + innerSource = outerSource.withWorld(bv.getWorld()); + } + else + { + throw new InternalExpressionException("in_dimension accepts only world-localized block arguments"); + } + } + else + { + String dimString = dimensionValue.getString().toLowerCase(Locale.ROOT); + switch (dimString) + { + case "nether": + case "the_nether": + innerSource = outerSource.withWorld(outerSource.getMinecraftServer().getWorld(DimensionType.THE_NETHER)); + break; + case "end": + case "the_end": + innerSource = outerSource.withWorld(outerSource.getMinecraftServer().getWorld(DimensionType.THE_END)); + break; + case "overworld": + case "over_world": + innerSource = outerSource.withWorld(outerSource.getMinecraftServer().getWorld(DimensionType.OVERWORLD)); + break; + default: + throw new InternalExpressionException("Incorrect dimension string: "+dimString); + } + } + ((CarpetContext) c).s = innerSource; + Value retval = lv.get(1).evalValue(c); + ((CarpetContext) c).s = outerSource; + return (cc, tt) -> retval; + }); + this.expr.addLazyFunction("plop", 4, (c, t, lv) ->{ BlockValue.LocatorResult locator = BlockValue.fromParams((CarpetContext)c, lv, 0); if (lv.size() <= locator.offset) diff --git a/src/main/java/carpet/script/value/BlockValue.java b/src/main/java/carpet/script/value/BlockValue.java index 7e7de4c5..9818e8ee 100644 --- a/src/main/java/carpet/script/value/BlockValue.java +++ b/src/main/java/carpet/script/value/BlockValue.java @@ -12,6 +12,7 @@ import net.minecraft.block.Blocks; import net.minecraft.item.ItemPlacementContext; import net.minecraft.item.ItemStack; import net.minecraft.nbt.CompoundTag; +import net.minecraft.server.world.ServerWorld; import net.minecraft.util.Hand; import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.math.BlockPos; @@ -32,7 +33,7 @@ public class BlockValue extends Value public static final BlockValue NULL = new BlockValue(null, null, null); private BlockState blockState; private BlockPos pos; - private World world; + private ServerWorld world; private CompoundTag data; public static BlockValue fromCoords(CarpetContext c, int x, int y, int z) @@ -212,7 +213,7 @@ public class BlockValue extends Value } - public BlockValue(BlockState state, World world, BlockPos position) + public BlockValue(BlockState state, ServerWorld world, BlockPos position) { this.world = world; blockState = state; @@ -220,7 +221,7 @@ public class BlockValue extends Value data = null; } - public BlockValue(BlockState state, World world, BlockPos position, CompoundTag nbt) + public BlockValue(BlockState state, ServerWorld world, BlockPos position, CompoundTag nbt) { this.world = world; blockState = state; @@ -258,6 +259,8 @@ public class BlockValue extends Value return pos; } + public ServerWorld getWorld() { return world;} + public static class LocatorResult { diff --git a/src/main/java/carpet/script/value/EntityValue.java b/src/main/java/carpet/script/value/EntityValue.java index dc7ffb71..e357fa10 100644 --- a/src/main/java/carpet/script/value/EntityValue.java +++ b/src/main/java/carpet/script/value/EntityValue.java @@ -7,12 +7,10 @@ import carpet.helpers.Tracer; import carpet.script.CarpetContext; import carpet.script.Fluff; import carpet.script.exception.InternalExpressionException; -import carpet.settings.CarpetSettings; import com.mojang.brigadier.StringReader; import com.mojang.brigadier.exceptions.CommandSyntaxException; import net.minecraft.client.network.packet.EntityPassengersSetS2CPacket; import net.minecraft.client.network.packet.EntityPositionS2CPacket; -import net.minecraft.client.network.packet.EntityVelocityUpdateS2CPacket; import net.minecraft.command.EntitySelector; import net.minecraft.command.EntitySelectorReader; import net.minecraft.command.arguments.NbtPathArgumentType; @@ -234,7 +232,7 @@ public class EntityValue extends Value put("home", (e, a) -> { if (e instanceof MobEntity) { - return (((MobEntity) e).getWalkTargetRange () > 0)?new BlockValue(null, e.getEntityWorld(), ((MobEntityWithAi) e).getWalkTarget()):Value.FALSE; + return (((MobEntity) e).getWalkTargetRange () > 0)?new BlockValue(null, (ServerWorld) e.getEntityWorld(), ((MobEntityWithAi) e).getWalkTarget()):Value.FALSE; } return Value.NULL; }); @@ -379,7 +377,7 @@ public class EntityValue extends Value switch (hitres.getType()) { case MISS: return Value.NULL; - case BLOCK: return new BlockValue(null, e.getEntityWorld(), ((BlockHitResult)hitres).getBlockPos() ); + case BLOCK: return new BlockValue(null, (ServerWorld) e.getEntityWorld(), ((BlockHitResult)hitres).getBlockPos() ); case ENTITY: return new EntityValue(((EntityHitResult)hitres).getEntity()); } return Value.NULL;