diff --git a/src/main/java/carpet/script/CarpetExpression.java b/src/main/java/carpet/script/CarpetExpression.java index 088fed23..38600480 100644 --- a/src/main/java/carpet/script/CarpetExpression.java +++ b/src/main/java/carpet/script/CarpetExpression.java @@ -1,6 +1,7 @@ package carpet.script; import carpet.CarpetServer; +import carpet.script.value.NBTSerializableValue; import carpet.script.value.NullValue; import carpet.settings.CarpetSettings; import carpet.helpers.FeatureGenerator; @@ -26,7 +27,12 @@ import net.minecraft.block.entity.BlockEntity; import net.minecraft.enchantment.Enchantments; import net.minecraft.entity.EntityType; import net.minecraft.entity.EquipmentSlot; +import net.minecraft.entity.ItemEntity; +import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.ai.brain.task.LookTargetUtil; import net.minecraft.entity.decoration.ArmorStandEntity; +import net.minecraft.entity.passive.AbstractTraderEntity; +import net.minecraft.entity.passive.VillagerEntity; import net.minecraft.item.Items; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.command.arguments.ParticleArgumentType; @@ -556,7 +562,7 @@ public class CarpetExpression if (e == null) throw new InternalExpressionException("Null entity"); Value retval = ListValue.of(new NumericValue(e.x), new NumericValue(e.y), new NumericValue(e.z)); - return(c_, t_) -> retval; + return (c_, t_) -> retval; } else { @@ -607,7 +613,8 @@ public class CarpetExpression genericStateTest(c, "blast_resistance", lv, (s, p, w) -> new NumericValue(s.getBlock().getBlastResistance()))); - this.expr.addLazyFunction("top", -1, (c, t, lv) -> { + this.expr.addLazyFunction("top", -1, (c, t, lv) -> + { String type = lv.get(0).evalValue(c).getString().toLowerCase(Locale.ROOT); Heightmap.Type htype; switch (type) @@ -718,8 +725,8 @@ public class CarpetExpression return (c_, t_) -> retval; }); - this.expr.addLazyFunction("destroy", -1, (c, t, lv) -> { - + this.expr.addLazyFunction("destroy", -1, (c, t, lv) -> + { CarpetContext cc = (CarpetContext)c; ServerWorld world = cc.s.getWorld(); BlockValue.LocatorResult locator = BlockValue.fromParams(cc, lv, 0); @@ -756,8 +763,8 @@ public class CarpetExpression return (c_, t_) -> Value.TRUE; }); - this.expr.addLazyFunction("harvest", -1, (c, t, lv) -> { - + this.expr.addLazyFunction("harvest", -1, (c, t, lv) -> + { CarpetContext cc = (CarpetContext)c; World world = cc.s.getWorld(); Value entityValue = lv.get(0).evalValue(cc); @@ -776,7 +783,7 @@ public class CarpetExpression ItemStack itemstack2 = player.getMainHandStack(); itemstack2.postMine(player.world, state, where, player); ItemStack itemstack1 = itemstack2.isEmpty() ? ItemStack.EMPTY : itemstack2.copy(); - state.getBlock().afterBreak(world, player, where,state, be, itemstack1); + state.getBlock().afterBreak(world, player, where, state, be, itemstack1); world.playLevelEvent(null, 2001, where, Block.getRawIdFromState(state)); return (c_, t_) -> Value.TRUE; }); @@ -789,11 +796,11 @@ public class CarpetExpression stateStringQuery(c, "block_sound", lv, (s, p) -> BlockInfo.soundName.get(s.getSoundGroup()))); - this.expr.addLazyFunction("material",-1, (c, t, lv) -> + this.expr.addLazyFunction("material", -1, (c, t, lv) -> stateStringQuery(c, "material", lv, (s, p) -> BlockInfo.materialName.get(s.getMaterial()))); - this.expr.addLazyFunction("map_colour", -1, (c, t, lv) -> + this.expr.addLazyFunction("map_colour", -1, (c, t, lv) -> stateStringQuery(c, "map_colour", lv, (s, p) -> BlockInfo.mapColourName.get(s.getTopMaterialColor(((CarpetContext)c).s.getWorld(), p)))); @@ -807,8 +814,104 @@ public class CarpetExpression if (property == null) return LazyValue.NULL; Value retval = new StringValue(state.get(property).toString()); - return (_c, _t ) -> retval; + return (_c, _t) -> retval; }); + + } + + public void API_InventoryManipulation() + { + //inventory_get(, ) -> item_triple + this.expr.addLazyFunction("inventory_get", -1, (c, t, lv) -> { + //long stime = System.nanoTime(); + CarpetContext cc = (CarpetContext) c; + NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0); + if (lv.size() == inventoryLocator.offset) + { + List fullInventory = new ArrayList<>(); + for (int i = 0, maxi = inventoryLocator.inventory.getInvSize(); i < maxi; i++) + { + fullInventory.add(ListValue.fromItemStack(inventoryLocator.inventory.getInvStack(i))); + } + Value res = ListValue.wrap(fullInventory); + //long etime = System.nanoTime(); + //CarpetSettings.LOG.error("took "+(etime-stime)/1000); + return (_c, _t) -> res; + } + int slot = (int)NumericValue.asNumber(lv.get(inventoryLocator.offset).evalValue(c)).getLong(); + if (slot < 0 || slot > inventoryLocator.inventory.getInvSize()) + throw new InternalExpressionException("Inventory has "+inventoryLocator.inventory.getInvSize()+" slots available"); + Value res = ListValue.fromItemStack(inventoryLocator.inventory.getInvStack(slot)); + //long etime = System.nanoTime(); + //CarpetSettings.LOG.error("took "+(etime-stime)/1000); + return (_c, _t) -> res; + }); + + //inventory_drop(, , ) -> entity_item (and sets slot) or null if cannot + this.expr.addLazyFunction("inventory_drop", -1, (c, t, lv) -> { + //long stime = System.nanoTime(); + CarpetContext cc = (CarpetContext) c; + NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0); + if (lv.size() == inventoryLocator.offset) + throw new InternalExpressionException("slot number is required for inventory_drop"); + int slot = (int)NumericValue.asNumber(lv.get(inventoryLocator.offset).evalValue(c)).getLong(); + if (slot < 0 || slot > inventoryLocator.inventory.getInvSize()) + throw new InternalExpressionException("Inventory has "+inventoryLocator.inventory.getInvSize()+" slots available"); + int amount = 0; + if (lv.size() > inventoryLocator.offset+1) + amount = (int)NumericValue.asNumber(lv.get(inventoryLocator.offset+1).evalValue(c)).getLong(); + if (amount < 0) + throw new InternalExpressionException("Cannot throw negative number of items"); + ItemStack stack = inventoryLocator.inventory.getInvStack(slot); + if (stack == null || stack.isEmpty()) + return (_c, _t) -> Value.ZERO; + if (amount == 0) + amount = stack.getCount(); + ItemStack droppedStack = inventoryLocator.inventory.takeInvStack(slot, amount); + if (droppedStack.isEmpty()) + { + return (_c, _t) -> Value.ZERO; + } + Object owner = inventoryLocator.owner; + ItemEntity item; + if (owner instanceof PlayerEntity) + item = ((PlayerEntity) owner).dropItem(droppedStack, false, true); + else if (owner instanceof LivingEntity) + { + LivingEntity villager = (LivingEntity)owner; + // stolen from LookTargetUtil.give((VillagerEntity)owner, droppedStack, (LivingEntity) owner); + double double_1 = villager.y - 0.30000001192092896D + (double)villager.getStandingEyeHeight(); + item = new ItemEntity(villager.world, villager.x, double_1, villager.z, droppedStack); + Vec3d vec3d_1 = villager.getRotationVec(1.0F).normalize().multiply(0.3);// new Vec3d(0, 0.3, 0); + item.setVelocity(vec3d_1); + item.setToDefaultPickupDelay(); + } + else + { + Vec3d point = new Vec3d(inventoryLocator.position); + item = new ItemEntity(cc.s.getWorld(), point.x+0.5, point.y+0.5, point.z+0.5, droppedStack); + item.setToDefaultPickupDelay(); + } + inventoryLocator.inventory.markDirty(); + cc.s.getWorld().spawnEntity(item); + Value res = new NumericValue(item.getStack().getCount()); + return (_c, _t) -> res; + }); + + //inventory_find(, or null (first empty slot), ) -> or null + //inventory_set(, , , ) + //inventory_remove(, , ) -> bool + + + + /* + if (slot > -1) + { + if (slot >= inv.getInvSize()) + throw new InternalExpressionException("Inventory of "+e+" has "+inv.getInvSize()+" slots only"); + itemstack = inv.getInvStack(slot); + } + */ } /** @@ -2051,6 +2154,7 @@ public class CarpetExpression API_BlockManipulation(); API_EntityManipulation(); + API_InventoryManipulation(); API_IteratingOverAreasOfBlocks(); API_AuxiliaryAspects(); } diff --git a/src/main/java/carpet/script/Expression.java b/src/main/java/carpet/script/Expression.java index 9515d8bb..51ea5072 100644 --- a/src/main/java/carpet/script/Expression.java +++ b/src/main/java/carpet/script/Expression.java @@ -19,6 +19,7 @@ import carpet.script.value.ListValue; import carpet.script.value.NumericValue; import carpet.script.value.StringValue; import carpet.script.value.Value; +import carpet.settings.CarpetSettings; import java.math.BigInteger; import java.util.ArrayList; @@ -1833,6 +1834,21 @@ public class Expression implements Cloneable return LazyListValue.range(from, to, step); }); + addBinaryFunction("get", (v1, v2) -> { + if (v1 instanceof LazyListValue || !(v1 instanceof ListValue)) + { + throw new InternalExpressionException("First argument of element should be a list"); + } + List items = ((ListValue)v1).getItems(); + long index = NumericValue.asNumber(v2).getLong(); + int numitems = items.size(); + long range = abs(index)/numitems; + index += (range+2)*numitems; + index = index % numitems; + return items.get((int)index); + }); + + //Deprecated addBinaryFunction("element", (v1, v2) -> { if (v1 instanceof LazyListValue || !(v1 instanceof ListValue)) { diff --git a/src/main/java/carpet/script/value/EntityValue.java b/src/main/java/carpet/script/value/EntityValue.java index 9117b02e..f51c0598 100644 --- a/src/main/java/carpet/script/value/EntityValue.java +++ b/src/main/java/carpet/script/value/EntityValue.java @@ -4,35 +4,33 @@ 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.EntityVelocityUpdateS2CPacket; +import net.minecraft.command.EntitySelector; +import net.minecraft.command.EntitySelectorReader; +import net.minecraft.command.arguments.NbtPathArgumentType; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityPose; import net.minecraft.entity.EntityType; import net.minecraft.entity.EquipmentSlot; import net.minecraft.entity.ItemEntity; import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.ai.goal.GoToWalkTargetGoal; +import net.minecraft.entity.ai.goal.Goal; import net.minecraft.entity.effect.StatusEffect; import net.minecraft.entity.effect.StatusEffectInstance; -import net.minecraft.nbt.Tag; -import net.minecraft.server.command.ServerCommandSource; -import net.minecraft.command.EntitySelector; -import net.minecraft.command.EntitySelectorReader; -import net.minecraft.command.arguments.NbtPathArgumentType; -import net.minecraft.entity.mob.MobEntityWithAi; // EntityCreature; -import net.minecraft.entity.mob.MobEntity; //LivingEntity; -import net.minecraft.entity.ai.goal.Goal; -import net.minecraft.entity.ai.goal.GoToWalkTargetGoal; +import net.minecraft.entity.mob.MobEntity; +import net.minecraft.entity.mob.MobEntityWithAi; import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.server.network.ServerPlayerEntity; -import net.minecraft.item.ItemStack; import net.minecraft.nbt.CompoundTag; -import net.minecraft.client.network.packet.EntityVelocityUpdateS2CPacket; -import net.minecraft.potion.Potion; +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.text.LiteralText; import net.minecraft.util.Identifier; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d; import net.minecraft.util.registry.Registry; -import net.minecraft.text.LiteralText; import org.apache.commons.lang3.tuple.Pair; import java.util.ArrayList; @@ -64,7 +62,7 @@ public class EntityValue extends Value { return entitySelector.getEntities(source); } - entitySelector = new EntitySelectorReader(new StringReader(selector), true).build(); + entitySelector = new EntitySelectorReader(new StringReader(selector), true).read(); selectorCache.put(selector, entitySelector); return entitySelector.getEntities(source); } @@ -104,6 +102,21 @@ public class EntityValue extends Value @Override public Value in(Value v) { + if (v instanceof ListValue) + { + List values = ((ListValue) v).getItems(); + String what = values.get(0).getString(); + Value arg = null; + if (values.size() == 2) + { + arg = values.get(1); + } + else if (values.size() > 2) + { + arg = ListValue.wrap(values.subList(1,values.size())); + } + return this.get(what, arg); + } String what = v.getString(); return this.get(what, null); } @@ -139,6 +152,7 @@ public class EntityValue extends Value put("feet", EquipmentSlot.FEET); }}; private static Map> featureAccessors = new HashMap>() {{ + //put("test", (e, a) -> a == null ? Value.NULL : new StringValue(a.getString())); put("removed", (entity, arg) -> new NumericValue(entity.removed)); put("uuid",(e, a) -> new StringValue(e.getUuidAsString())); put("id",(e, a) -> new NumericValue(e.getEntityId())); @@ -273,19 +287,14 @@ public class EntityValue extends Value if (where == null) throw new InternalExpressionException("Unknown inventory slot: "+a.getString()); if (e instanceof LivingEntity) - { - ItemStack itemstack = ((LivingEntity)e).getEquippedStack(where); - if (!itemstack.isEmpty()) - { - return ListValue.of( - new StringValue(Registry.ITEM.getId(itemstack.getItem()).getPath()), - new NumericValue(itemstack.getCount()), - new StringValue(itemstack.toTag(new CompoundTag()).toString()) - ); - } - } + return ListValue.fromItemStack(((LivingEntity)e).getEquippedStack(where)); return Value.NULL; + }); + put("selected_slot", (e, a) -> { + if (e instanceof PlayerEntity) + return new NumericValue(((PlayerEntity) e).inventory.selectedSlot); + return null; }); put("nbt",(e, a) -> { diff --git a/src/main/java/carpet/script/value/ListValue.java b/src/main/java/carpet/script/value/ListValue.java index e8cfe02f..a8b20d72 100644 --- a/src/main/java/carpet/script/value/ListValue.java +++ b/src/main/java/carpet/script/value/ListValue.java @@ -1,6 +1,9 @@ package carpet.script.value; import carpet.script.exception.InternalExpressionException; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.util.registry.Registry; import java.util.ArrayList; import java.util.Arrays; @@ -44,6 +47,19 @@ public class ListValue extends Value items = new ArrayList<>(); items.addAll(list); } + + public static Value fromItemStack(ItemStack itemstack) + { + if (itemstack == null || itemstack.isEmpty()) + return Value.NULL; + return ListValue.of( + new StringValue(Registry.ITEM.getId(itemstack.getItem()).getPath()), + new NumericValue(itemstack.getCount()), + new NBTSerializableValue(itemstack) + ); + } + + public static ListValue wrap(List list) { ListValue created = new ListValue(); diff --git a/src/main/java/carpet/script/value/NBTSerializableValue.java b/src/main/java/carpet/script/value/NBTSerializableValue.java new file mode 100644 index 00000000..1feac122 --- /dev/null +++ b/src/main/java/carpet/script/value/NBTSerializableValue.java @@ -0,0 +1,123 @@ +package carpet.script.value; + +import carpet.script.CarpetContext; +import carpet.script.LazyValue; +import carpet.script.exception.InternalExpressionException; +import net.minecraft.block.entity.HopperBlockEntity; +import net.minecraft.entity.Entity; +import net.minecraft.entity.passive.AbstractTraderEntity; +import net.minecraft.entity.passive.VillagerEntity; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.inventory.Inventory; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.util.math.BlockPos; + +import java.util.List; +import java.util.function.Supplier; + +public class NBTSerializableValue extends Value +{ + private Object what; + private String nbtString = null; + private CompoundTag nbtTag = null; + private Supplier nbtSupplier; + + public NBTSerializableValue(ItemStack stack) + { + what = stack; + nbtSupplier = () -> ((ItemStack)what).toTag(new CompoundTag()); + } + + public static InventoryLocator locateInventory(CarpetContext c, List params, int offset) + { + try + { + Inventory inv = null; + Value v1 = params.get(0 + offset).evalValue(c); + if (v1 instanceof EntityValue) + { + Entity e = ((EntityValue) v1).getEntity(); + if (e instanceof PlayerEntity) inv = ((PlayerEntity) e).inventory; + else if (e instanceof Inventory) inv = (Inventory)e; + else if (e instanceof VillagerEntity) inv = ((VillagerEntity) e).getInventory(); + + if (inv == null) + throw new InternalExpressionException("Entity "+e+" has no inventory"); + + return new InventoryLocator(e, e.getBlockPos(), inv, offset+1); + } + else if (v1 instanceof BlockValue) + { + BlockPos pos = ((BlockValue) v1).getPos(); + if (pos == null) + throw new InternalExpressionException("Block to acess inventory needs to be positioned in the world"); + inv = HopperBlockEntity.getInventoryAt(c.s.getWorld(), pos); + if (inv == null) + throw new InternalExpressionException("Block at "+pos+" has no inventory"); + return new InventoryLocator(pos, pos, inv, offset+1); + } + else if (v1 instanceof ListValue) + { + List args = ((ListValue) v1).getItems(); + BlockPos pos = new BlockPos( + NumericValue.asNumber(args.get(0)).getDouble(), + NumericValue.asNumber(args.get(1)).getDouble(), + NumericValue.asNumber(args.get(2)).getDouble()); + inv = HopperBlockEntity.getInventoryAt(c.s.getWorld(), pos); + if (inv == null) + throw new InternalExpressionException("Block at "+pos+" has no inventory"); + return new InventoryLocator(pos, pos, inv, offset+1); + } + BlockPos pos = new BlockPos( + NumericValue.asNumber(v1).getDouble(), + NumericValue.asNumber(params.get(1 + offset).evalValue(c)).getDouble(), + NumericValue.asNumber(params.get(2 + offset).evalValue(c)).getDouble()); + inv = HopperBlockEntity.getInventoryAt(c.s.getWorld(), pos); + if (inv == null) + throw new InternalExpressionException("Block at "+pos+" has no inventory"); + return new InventoryLocator(pos, pos, inv, offset+3); + } + catch (IndexOutOfBoundsException e) + { + throw new InternalExpressionException("Inventory should be defined either by three coordinates, a block value, or an entity"); + } + } + + private CompoundTag getTag() + { + if (nbtTag == null) + nbtTag = nbtSupplier.get(); + return nbtTag; + } + + + @Override + public String getString() + { + if (nbtString == null) + nbtString = getTag().toString(); + return nbtString; + } + + @Override + public boolean getBoolean() + { + return true; + } + + public static class InventoryLocator + { + public Object owner; + public BlockPos position; + public Inventory inventory; + public int offset; + InventoryLocator(Object owner, BlockPos pos, Inventory i, int o) + { + this.owner = owner; + position = pos; + inventory = i; + offset = o; + } + } +}