inventories

This commit is contained in:
gnembon committed 2019-06-24 09:15:54 -03:00
1 parent a514a5410b
commit 037321ccfd
5 files changed
+299 -31

No files matched your search

@@ -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;
@@ -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);
@@ -809,6 +816,102 @@ public class CarpetExpression
Value retval = new StringValue(state.get(property).toString());
return (_c, _t) -> retval;
});
}
public void API_InventoryManipulation()
{
//inventory_get(<b, e>, <n>) -> 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<Value> 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(<b, e>, <n>, <amount=1, 0-whatever's there>) -> 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(<b, e>, <item> or null (first empty slot), <start_from=0> ) -> <N> or null
//inventory_set(<b,e>, <n>, <count>, <item>)
//inventory_remove(<b, e>, <item>, <amount=1>) -> 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();
}
@@ -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<Value> 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))
{
@@ -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<Value> 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<String, BiFunction<Entity, Value, Value>> featureAccessors = new HashMap<String, BiFunction<Entity, Value, Value>>() {{
//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) -> {
@@ -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<Value> list)
{
ListValue created = new ListValue();
@@ -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<CompoundTag> nbtSupplier;
public NBTSerializableValue(ItemStack stack)
{
what = stack;
nbtSupplier = () -> ((ItemStack)what).toTag(new CompoundTag());
}
public static InventoryLocator locateInventory(CarpetContext c, List<LazyValue> 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<Value> 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;
}
}
}