better inventory and nbt support. control flow statements default to nulls

This commit is contained in:
gnembon committed 2019-06-29 20:00:15 -03:00
1 parent b79b942819
commit 83a8f35094
7 files changed
+182 -33

No files matched your search

@@ -6,10 +6,12 @@ import carpet.script.exception.InternalExpressionException;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.command.arguments.BlockArgumentParser;
import net.minecraft.block.Blocks;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
@@ -31,6 +33,7 @@ public class BlockValue extends Value
private BlockState blockState;
private BlockPos pos;
private World world;
private CompoundTag data;
public static BlockValue fromCoords(CarpetContext c, int x, int y, int z)
{
@@ -48,7 +51,10 @@ public class BlockValue extends Value
BlockArgumentParser blockstateparser = (new BlockArgumentParser(new StringReader(str), false)).parse(true);
if (blockstateparser.getBlockState() != null)
{
bv = new BlockValue(blockstateparser.getBlockState(), null, null);
CompoundTag bd = blockstateparser.getNbtData();
if (bd == null)
bd = new CompoundTag();
bv = new BlockValue(blockstateparser.getBlockState(), null, null, bd );
if (bvCache.size()>10000)
bvCache.clear();
bvCache.put(str, bv);
@@ -182,11 +188,44 @@ public class BlockValue extends Value
throw new InternalExpressionException("Attemted to fetch blockstate without world or stored blockstate");
}
public CompoundTag getData()
{
if (data != null)
{
if (data.isEmpty())
return null;
return data;
}
if (world != null && pos != null)
{
BlockEntity be = world.getBlockEntity(pos);
CompoundTag tag = new CompoundTag();
if (be == null)
{
data = tag;
return null;
}
data = be.toTag(tag);
return data;
}
throw new InternalExpressionException("Attemted to fetch block data without world or stored block data");
}
public BlockValue(BlockState state, World world, BlockPos position)
{
this.world = world;
blockState = state;
pos = position;
data = null;
}
public BlockValue(BlockState state, World world, BlockPos position, CompoundTag nbt)
{
this.world = world;
blockState = state;
pos = position;
data = nbt;
}
@@ -100,4 +100,10 @@ public abstract class LazyListValue extends ListValue implements Iterator<Value>
throw new InternalExpressionException("Cannot add to iterators");
}
@Override
public boolean equals(final Value o)
{
return false;
}
}
@@ -203,25 +203,33 @@ public class ListValue extends Value
ListValue ol = (ListValue)o;
int this_size = this.getItems().size();
int o_size = ol.getItems().size();
if (this_size != o_size)
{
return this_size - o_size;
}
if (this_size == 0)
{
return 0;
}
if (this_size != o_size) return this_size - o_size;
if (this_size == 0) return 0;
for (int i = 0; i < this_size; i++)
{
int res = this.items.get(i).compareTo(ol.items.get(i));
if (res != 0)
{
return res;
}
if (res != 0) return res;
}
return 0;
}
return super.compareTo(o);
return getString().compareTo(o.getString());
}
@Override
public boolean equals(final Value o)
{
if (o instanceof ListValue)
{
ListValue ol = (ListValue)o;
int this_size = this.getItems().size();
int o_size = ol.getItems().size();
if (this_size != o_size) return false;
if (this_size == 0) return true;
for (int i = 0; i < this_size; i++)
if (!this.items.get(i).equals(ol.items.get(i))) return false;
return true;
}
return false;
}
public List<Value> getItems()
@@ -7,17 +7,15 @@ import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.block.entity.HopperBlockEntity;
import net.minecraft.command.arguments.ItemStackArgument;
import net.minecraft.command.arguments.ItemStackArgumentType;
import net.minecraft.command.arguments.ItemStringReader;
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.nbt.StringNbtReader;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import java.util.HashMap;
import java.util.List;
@@ -26,15 +24,33 @@ 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;
private Supplier<CompoundTag> nbtSupplier = null;
public NBTSerializableValue(ItemStack stack)
{
what = stack;
nbtSupplier = () -> ((ItemStack) what).toTag(new CompoundTag());
nbtSupplier = () -> stack.hasTag() ? stack.getTag(): new CompoundTag();
}
public NBTSerializableValue(String nbtString)
{
nbtSupplier = () ->
{
try
{
return (new StringNbtReader(new StringReader(nbtString))).parseCompoundTag();
}
catch (CommandSyntaxException e)
{
throw new InternalExpressionException("Incorrect nbt data: "+nbtString);
}
};
}
public NBTSerializableValue(CompoundTag tag)
{
nbtTag = tag;
}
public static InventoryLocator locateInventory(CarpetContext c, List<LazyValue> params, int offset)
@@ -93,19 +109,32 @@ public class NBTSerializableValue extends Value
}
private static Map<String,ItemStackArgument> itemCache = new HashMap<>();
public static ItemStackArgument parseItem(String itemString)
{
return parseItem(itemString, null);
}
public static ItemStackArgument parseItem(String itemString, CompoundTag customTag)
{
try
{
ItemStackArgument res = itemCache.get(itemString);
if (res != null)
return res;
if (customTag == null)
return res;
else
return new ItemStackArgument(res.getItem(), customTag);
ItemStringReader parser = (new ItemStringReader(new StringReader(itemString), false)).consume();
res = new ItemStackArgument(parser.getItem(), parser.getTag());
itemCache.put(itemString, res);
if (itemCache.size()>64000)
itemCache.clear();
return res;
if (customTag == null)
return res;
else
return new ItemStackArgument(res.getItem(), customTag);
}
catch (CommandSyntaxException e)
{
@@ -123,13 +152,23 @@ public class NBTSerializableValue extends Value
return slot;
}
private CompoundTag getTag()
public CompoundTag getTag()
{
if (nbtTag == null)
nbtTag = nbtSupplier.get();
return nbtTag;
}
@Override
public boolean equals(final Value o)
{
if (o instanceof NBTSerializableValue)
{
return getTag().equals(((NBTSerializableValue) o).getTag());
}
return super.equals(o);
}
@Override
public String getString()
@@ -116,7 +116,7 @@ public class NumericValue extends Value
{
return value.compareTo(((NumericValue) o).getDouble());
}
return super.compareTo(o);
return getString().compareTo(o.getString());
}
@Override
public boolean equals(Value o)