inventories
This commit is contained in:
1 parent
a514a5410b
commit
037321ccfd
5 files changed
+304
-36
No files matched your search
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user