From d418b7d090521ae6b66eac5877d453f47ec3c1f0 Mon Sep 17 00:00:00 2001 From: ch-yx Date: Sun, 28 Aug 2022 00:23:47 +0800 Subject: [PATCH] move getItemStackFromValue to ValueConversions --- .../java/carpet/script/api/Auxiliary.java | 2 +- .../java/carpet/script/value/EntityValue.java | 28 +---------------- .../carpet/script/value/ValueConversions.java | 30 +++++++++++++++++++ 3 files changed, 32 insertions(+), 28 deletions(-) diff --git a/src/main/java/carpet/script/api/Auxiliary.java b/src/main/java/carpet/script/api/Auxiliary.java index 6380b2b3..8dcb70c1 100644 --- a/src/main/java/carpet/script/api/Auxiliary.java +++ b/src/main/java/carpet/script/api/Auxiliary.java @@ -259,7 +259,7 @@ public class Auxiliary { }); expression.addContextFunction("item_display_name", -1, (c, t, lv)->{ - return new FormattedTextValue(EntityValue.getItemStackFromValue(lv.get(0)).getHoverName()); + return new FormattedTextValue(ValueConversions.getItemStackFromValue(lv.get(0)).getHoverName()); }); diff --git a/src/main/java/carpet/script/value/EntityValue.java b/src/main/java/carpet/script/value/EntityValue.java index 86a8d400..77dfd27c 100644 --- a/src/main/java/carpet/script/value/EntityValue.java +++ b/src/main/java/carpet/script/value/EntityValue.java @@ -1701,7 +1701,7 @@ public class EntityValue extends Value }); put("item", (e, v) -> { - ItemStack item=getItemStackFromValue(v); + ItemStack item=ValueConversions.getItemStackFromValue(v); if(e instanceof ItemEntity itementity) itementity.setItem(item); if(e instanceof ItemFrame itemframe) @@ -1712,32 +1712,6 @@ public class EntityValue extends Value // "effect_"name [] }}; - static public ItemStack getItemStackFromValue(Value value){ - List items; - if(value instanceof ListValue blv && blv.length()==3){ - items = blv.getItems(); - }else if(value.isNull()){ - return ItemStack.EMPTY; - }else{ - items= List.of(StringValue.of(value.getString()),Value.ONE,Value.NULL); - } - String id=items.get(0).getString(); - int count=((NumericValue)items.get(1)).getInt(); - CompoundTag nbt=null; - if (!items.get(2).isNull()){ - if(items.get(2) instanceof NBTSerializableValue nbtv){ - nbt=nbtv.getCompoundTag(); - } - else{ - nbt=new NBTSerializableValue(items.get(2).getString()).getCompoundTag(); - } - } - try{ - return NBTSerializableValue.parseItem(id,nbt).createItemStack(count,false); - } catch (CommandSyntaxException e1) { - throw new IllegalStateException("Unexpected exception while creating item stack", e1); - } - } public void setEvent(CarpetContext cc, String eventName, FunctionValue fun, List args) { EntityEventsGroup.Event event = EntityEventsGroup.Event.byName.get(eventName); diff --git a/src/main/java/carpet/script/value/ValueConversions.java b/src/main/java/carpet/script/value/ValueConversions.java index dc5e631f..3d8cecb8 100644 --- a/src/main/java/carpet/script/value/ValueConversions.java +++ b/src/main/java/carpet/script/value/ValueConversions.java @@ -14,6 +14,7 @@ import net.minecraft.core.Registry; import net.minecraft.core.RegistryAccess; import net.minecraft.core.Vec3i; import net.minecraft.core.particles.ParticleOptions; +import net.minecraft.nbt.CompoundTag; import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.MinecraftServer; @@ -52,6 +53,8 @@ import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.StreamSupport; +import com.mojang.brigadier.exceptions.CommandSyntaxException; + public class ValueConversions { public static Value of(BlockPos pos) @@ -411,6 +414,33 @@ public class ValueConversions ); } + static public ItemStack getItemStackFromValue(Value value){ + List items; + if(value instanceof ListValue blv && blv.length()==3){ + items = blv.getItems(); + }else if(value.isNull()){ + return ItemStack.EMPTY; + }else{ + items= List.of(StringValue.of(value.getString()),Value.ONE,Value.NULL); + } + String id=items.get(0).getString(); + int count=((NumericValue)items.get(1)).getInt(); + CompoundTag nbt=null; + if (!items.get(2).isNull()){ + if(items.get(2) instanceof NBTSerializableValue nbtv){ + nbt=nbtv.getCompoundTag(); + } + else{ + nbt=new NBTSerializableValue(items.get(2).getString()).getCompoundTag(); + } + } + try{ + return NBTSerializableValue.parseItem(id,nbt).createItemStack(count,false); + } catch (CommandSyntaxException e1) { + throw new IllegalStateException("Unexpected exception while creating item stack", e1); + } + } + public static Value guess(ServerLevel serverWorld, Object o) { if (o == null) return Value.NULL;