From a7ae41a9df7409229aed11a72fccdffd1c887685 Mon Sep 17 00:00:00 2001 From: gnembon Date: Thu, 18 Jul 2019 09:31:42 -0300 Subject: [PATCH] spawn and properties --- .../java/carpet/script/CarpetExpression.java | 80 ++++++++++++++++++- src/main/java/carpet/script/Expression.java | 8 +- .../java/carpet/script/value/BlockValue.java | 9 ++- .../java/carpet/script/value/EntityValue.java | 56 +++++++------ 4 files changed, 119 insertions(+), 34 deletions(-) diff --git a/src/main/java/carpet/script/CarpetExpression.java b/src/main/java/carpet/script/CarpetExpression.java index 042c0ebc..71f6aa01 100644 --- a/src/main/java/carpet/script/CarpetExpression.java +++ b/src/main/java/carpet/script/CarpetExpression.java @@ -35,8 +35,11 @@ import net.minecraft.entity.Entity; import net.minecraft.entity.EntityType; import net.minecraft.entity.EquipmentSlot; import net.minecraft.entity.ItemEntity; +import net.minecraft.entity.LightningEntity; import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.SpawnType; import net.minecraft.entity.decoration.ArmorStandEntity; +import net.minecraft.entity.mob.MobEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.BlockItem; import net.minecraft.item.ItemStack; @@ -53,6 +56,8 @@ 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.hit.BlockHitResult; import net.minecraft.util.hit.EntityHitResult; @@ -73,6 +78,7 @@ import org.apache.commons.lang3.tuple.Pair; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Locale; @@ -599,7 +605,7 @@ public class CarpetExpression throw new InternalExpressionException("Block requires at least one parameter"); } CompoundTag tag = BlockValue.fromParams(cc, lv, 0, true).block.getData(); - if (tag.isEmpty()) + if (tag == null) return (c_, t_) -> Value.NULL; Value retval = new NBTSerializableValue(tag); return (c_, t_) -> retval; @@ -783,7 +789,8 @@ public class CarpetExpression return (c_, t_) -> Value.FALSE; CarpetSettings.impendingFillSkipUpdates = !CarpetSettings.fillUpdates; BlockPos targetPos = targetLocator.block.getPos(); - cc.s.getWorld().setBlockState(targetPos, sourceBlockState, 2); + Clearable.clear(world.getBlockEntity(targetPos)); + world.setBlockState(targetPos, sourceBlockState, 2); if ( data != null) { @@ -935,15 +942,28 @@ public class CarpetExpression { BlockValue.LocatorResult locator = BlockValue.fromParams((CarpetContext) c, lv, 0); BlockState state = locator.block.getBlockState(); + if (lv.size() <= locator.offset) + throw new InternalExpressionException("property requires to specify a property to query"); String tag = lv.get(locator.offset).evalValue(c).getString(); StateFactory states = state.getBlock().getStateFactory(); Property property = states.getProperty(tag); if (property == null) return LazyValue.NULL; - Value retval = new StringValue(state.get(property).toString()); + Value retval = new StringValue(state.get(property).toString().toLowerCase(Locale.ROOT)); return (_c, _t) -> retval; }); + this.expr.addLazyFunction("block_properties", -1, (c, t, lv) -> + { + BlockValue.LocatorResult locator = BlockValue.fromParams((CarpetContext) c, lv, 0); + BlockState state = locator.block.getBlockState(); + StateFactory states = state.getBlock().getStateFactory(); + Value res = ListValue.wrap(states.getProperties().stream().map( + p -> new StringValue(p.getName())).collect(Collectors.toList()) + ); + return (_c, _t) -> res; + }); + /*this.expr.addLazyFunction("trace_block", -1, (c, t, lv) -> { Value entityValue = lv.get(0).evalValue(c); if (!(entityValue instanceof EntityValue)) @@ -1597,6 +1617,60 @@ public class CarpetExpression return (cc, tt) -> finalVar; }); + this.expr.addLazyFunction("spawn", -1, (c, t, lv) -> { + CarpetContext cc = (CarpetContext)c; + if (lv.size() < 2) + throw new InternalExpressionException("spawn function takes mob name, and position to spawn"); + String entityString = lv.get(0).evalValue(c).getString(); + Identifier entityId; + try + { + entityId = Identifier.fromCommandInput(new StringReader(entityString)); + EntityType type = Registry.ENTITY_TYPE.getOrEmpty(entityId).orElse(null); + if (type == null || !type.isSummonable()) + return LazyValue.NULL; + } + catch (CommandSyntaxException exception) + { + return LazyValue.NULL; + } + + BlockValue.VectorLocator position = BlockValue.locateVec(cc, lv, 1); + if (position.fromBlock) + position.vec = position.vec.subtract(0, 0.5, 0); + CompoundTag tag = new CompoundTag(); + boolean hasTag = false; + if (lv.size() > position.offset) + { + hasTag = true; + tag = new NBTSerializableValue(lv.get(position.offset).evalValue(c).getString()).getTag(); + } + tag.putString("id", entityId.toString()); + Vec3d vec3d = position.vec; + + if (EntityType.getId(EntityType.LIGHTNING_BOLT).equals(entityId)) { + LightningEntity lightningEntity_1 = new LightningEntity(cc.s.getWorld(), vec3d.x, vec3d.y, vec3d.z, false); + cc.s.getWorld().addLightning(lightningEntity_1); + cc.s.sendFeedback(new TranslatableText("commands.summon.success", lightningEntity_1.getDisplayName()), true); + return LazyValue.NULL; + } else { + ServerWorld serverWorld = cc.s.getWorld(); + Entity entity_1 = EntityType.loadEntityWithPassengers(tag, serverWorld, (entity_1x) -> { + entity_1x.setPositionAndAngles(vec3d.x, vec3d.y, vec3d.z, entity_1x.yaw, entity_1x.pitch); + return !serverWorld.method_18768(entity_1x) ? null : entity_1x; + }); + if (entity_1 == null) { + return LazyValue.NULL; + } else { + if (!hasTag && entity_1 instanceof MobEntity) { + ((MobEntity)entity_1).initialize(serverWorld, serverWorld.getLocalDifficulty(new BlockPos(entity_1)), SpawnType.COMMAND, null, null); + } + Value res = new EntityValue(entity_1); + return (_c, _t) -> res; + } + } + }); + this.expr.addLazyFunction("entity_id", 1, (c, t, lv) -> { Value who = lv.get(0).evalValue(c); diff --git a/src/main/java/carpet/script/Expression.java b/src/main/java/carpet/script/Expression.java index 88ac9cac..2d2f01fb 100644 --- a/src/main/java/carpet/script/Expression.java +++ b/src/main/java/carpet/script/Expression.java @@ -691,10 +691,10 @@ public class Expression implements Cloneable { throw exit; } - catch (Exception exc) - { - throw new ExpressionException(e, t, "Error while evaluating expression: "+exc.getMessage()); - } + //catch (Exception exc) + //{ + // throw new ExpressionException(e, t, "Error while evaluating expression: "+exc.getMessage()); + //} } }); } diff --git a/src/main/java/carpet/script/value/BlockValue.java b/src/main/java/carpet/script/value/BlockValue.java index 8473a460..460abe00 100644 --- a/src/main/java/carpet/script/value/BlockValue.java +++ b/src/main/java/carpet/script/value/BlockValue.java @@ -78,7 +78,7 @@ public class BlockValue extends Value Value v1 = params.get(0 + offset).evalValue(c); if (v1 instanceof BlockValue) { - return new VectorLocator(new Vec3d(((BlockValue) v1).getPos()).add(0.5,0.5,0.5), 1+offset); + return (new VectorLocator(new Vec3d(((BlockValue) v1).getPos()).add(0.5,0.5,0.5), 1+offset)).fromBlock(); } if (v1 instanceof ListValue) { @@ -270,6 +270,7 @@ public class BlockValue extends Value public int offset; public double yaw; public double pitch; + public boolean fromBlock; VectorLocator(Vec3d v, int o) { vec = v; @@ -284,6 +285,12 @@ public class BlockValue extends Value yaw = y; pitch = p; } + + public VectorLocator fromBlock() + { + fromBlock = true; + return this; + } } public enum SpecificDirection { diff --git a/src/main/java/carpet/script/value/EntityValue.java b/src/main/java/carpet/script/value/EntityValue.java index 2b73e5fa..a0ea7f9e 100644 --- a/src/main/java/carpet/script/value/EntityValue.java +++ b/src/main/java/carpet/script/value/EntityValue.java @@ -4,6 +4,7 @@ import carpet.fakes.MobEntityInterface; import carpet.script.exception.InternalExpressionException; import com.mojang.brigadier.StringReader; import com.mojang.brigadier.exceptions.CommandSyntaxException; +import net.minecraft.client.network.packet.EntityPositionS2CPacket; import net.minecraft.client.network.packet.EntityVelocityUpdateS2CPacket; import net.minecraft.command.EntitySelector; import net.minecraft.command.EntitySelectorReader; @@ -26,6 +27,7 @@ import net.minecraft.nbt.Tag; import net.minecraft.predicate.entity.EntityPredicates; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.server.world.ServerWorld; import net.minecraft.text.LiteralText; import net.minecraft.util.Identifier; import net.minecraft.util.math.BlockPos; @@ -356,6 +358,21 @@ public class EntityValue extends Value featureModifiers.get(what).accept(entity, toWhat); } + private static void updatePosition(Entity e) + { + if (e instanceof ServerPlayerEntity) + ((ServerPlayerEntity)e).networkHandler.requestTeleport(e.x, e.y, e.z, e.yaw, e.pitch); + else + ((ServerWorld)e.getEntityWorld()).method_14178().sendToNearbyPlayers(e,new EntityPositionS2CPacket(e)); + } + + private static void updateVelocity(Entity e) + { + ((ServerWorld)e.getEntityWorld()).method_14178().sendToNearbyPlayers(e, new EntityVelocityUpdateS2CPacket(e)); + } + + + private static Map> featureModifiers = new HashMap>() {{ put("remove", (entity, value) -> entity.remove()); put("health", (e, v) -> { if (e instanceof LivingEntity) ((LivingEntity) e).setHealth((float) NumericValue.asNumber(v).getDouble()); }); @@ -375,8 +392,7 @@ public class EntityValue extends Value e.yaw = (float) NumericValue.asNumber(coords.get(3)).getDouble(); e.prevYaw = e.yaw; e.setPosition(e.x, e.y, e.z); - if (e instanceof ServerPlayerEntity) - ((ServerPlayerEntity)e).networkHandler.requestTeleport(e.x, e.y, e.z, e.yaw, e.pitch); + updatePosition(e); }); put("pos", (e, v) -> { @@ -389,43 +405,37 @@ public class EntityValue extends Value e.y = NumericValue.asNumber(coords.get(1)).getDouble(); e.z = NumericValue.asNumber(coords.get(2)).getDouble(); e.setPosition(e.x, e.y, e.z); - if (e instanceof ServerPlayerEntity) - ((ServerPlayerEntity)e).networkHandler.requestTeleport(e.x, e.y, e.z, e.yaw, e.pitch); + updatePosition(e); }); put("x", (e, v) -> { e.x = NumericValue.asNumber(v).getDouble(); e.setPosition(e.x, e.y, e.z); - if (e instanceof ServerPlayerEntity) - ((ServerPlayerEntity)e).networkHandler.requestTeleport(e.x, e.y, e.z, e.yaw, e.pitch); + updatePosition(e); }); put("y", (e, v) -> { e.y = NumericValue.asNumber(v).getDouble(); e.setPosition(e.x, e.y, e.z); - if (e instanceof ServerPlayerEntity) - ((ServerPlayerEntity)e).networkHandler.requestTeleport(e.x, e.y, e.z, e.yaw, e.pitch); + updatePosition(e); }); put("z", (e, v) -> { e.z = NumericValue.asNumber(v).getDouble(); e.setPosition(e.x, e.y, e.z); - if (e instanceof ServerPlayerEntity) - ((ServerPlayerEntity)e).networkHandler.requestTeleport(e.x, e.y, e.z, e.yaw, e.pitch); + updatePosition(e); }); put("pitch", (e, v) -> { e.pitch = (float) NumericValue.asNumber(v).getDouble(); e.prevPitch = e.pitch; - if (e instanceof ServerPlayerEntity) - ((ServerPlayerEntity)e).networkHandler.requestTeleport(e.x, e.y, e.z, e.yaw, e.pitch); + updatePosition(e); }); put("yaw", (e, v) -> { e.yaw = (float) NumericValue.asNumber(v).getDouble(); e.prevYaw = e.yaw; - if (e instanceof ServerPlayerEntity) - ((ServerPlayerEntity)e).networkHandler.requestTeleport(e.x, e.y, e.z, e.yaw, e.pitch); + updatePosition(e); }); //"look" //"turn" @@ -442,8 +452,7 @@ public class EntityValue extends Value e.y += NumericValue.asNumber(coords.get(1)).getDouble(); e.z += NumericValue.asNumber(coords.get(2)).getDouble(); e.setPosition(e.x, e.y, e.z); - if (e instanceof ServerPlayerEntity) - ((ServerPlayerEntity)e).networkHandler.requestTeleport(e.y, e.y, e.z, e.yaw, e.pitch); + updatePosition(e); }); put("motion", (e, v) -> @@ -458,29 +467,25 @@ public class EntityValue extends Value NumericValue.asNumber(coords.get(1)).getDouble(), NumericValue.asNumber(coords.get(2)).getDouble() ); - if (e instanceof ServerPlayerEntity) - ((ServerPlayerEntity)e).networkHandler.sendPacket(new EntityVelocityUpdateS2CPacket(e)); + updateVelocity(e); }); put("motion_x", (e, v) -> { Vec3d velocity = e.getVelocity(); e.setVelocity(NumericValue.asNumber(v).getDouble(), velocity.y, velocity.z); - if (e instanceof ServerPlayerEntity) - ((ServerPlayerEntity)e).networkHandler.sendPacket(new EntityVelocityUpdateS2CPacket(e)); + updateVelocity(e); }); put("motion_y", (e, v) -> { Vec3d velocity = e.getVelocity(); e.setVelocity(velocity.x, NumericValue.asNumber(v).getDouble(), velocity.z); - if (e instanceof ServerPlayerEntity) - ((ServerPlayerEntity)e).networkHandler.sendPacket(new EntityVelocityUpdateS2CPacket(e)); + updateVelocity(e); }); put("motion_z", (e, v) -> { Vec3d velocity = e.getVelocity(); e.setVelocity(velocity.x, velocity.y, NumericValue.asNumber(v).getDouble()); - if (e instanceof ServerPlayerEntity) - ((ServerPlayerEntity)e).networkHandler.sendPacket(new EntityVelocityUpdateS2CPacket(e)); + updateVelocity(e); }); put("accelerate", (e, v) -> @@ -495,8 +500,7 @@ public class EntityValue extends Value NumericValue.asNumber(coords.get(1)).getDouble(), NumericValue.asNumber(coords.get(2)).getDouble() ); - if (e instanceof ServerPlayerEntity) - ((ServerPlayerEntity)e).networkHandler.sendPacket(new EntityVelocityUpdateS2CPacket(e)); + updateVelocity(e); }); put("custom_name", (e, v) -> {