diff --git a/docs/scarpet/resources/editors/npp/scarpet.xml b/docs/scarpet/resources/editors/npp/scarpet.xml index 8b47d8d7..8730b818 100644 --- a/docs/scarpet/resources/editors/npp/scarpet.xml +++ b/docs/scarpet/resources/editors/npp/scarpet.xml @@ -53,7 +53,7 @@ block pos pos_offset solid air liquid flammable - transparent opacity blocks_daylight emitted_light light block_light sky_light see_sky brightness + transparent opacity blocks_daylight emitted_light light block_light sky_light effective_light see_sky brightness hardness blast_resistance top loaded suffocates power ticks_randomly update block_tick random_tick in_slime_chunk set without_updates blocks_movement block_sound material map_colour block_state block_list block_tags block_data poi set_poi @@ -77,7 +77,7 @@ player spawn entity_id entity_list entity_area entity_selector query modify entity_event entity_load_handler entity_types - item_list item_tags stack_limit item_category recipe_data crafting_remaining_item + item_list item_tags stack_limit item_category recipe_data crafting_remaining_item item_display_name inventory_size inventory_has_items inventory_get inventory_set inventory_find inventory_remove drop_item diff --git a/src/main/java/carpet/script/api/Auxiliary.java b/src/main/java/carpet/script/api/Auxiliary.java index 8dcb70c1..39a789ee 100644 --- a/src/main/java/carpet/script/api/Auxiliary.java +++ b/src/main/java/carpet/script/api/Auxiliary.java @@ -258,10 +258,7 @@ public class Auxiliary { )); }); - expression.addContextFunction("item_display_name", -1, (c, t, lv)->{ - return new FormattedTextValue(ValueConversions.getItemStackFromValue(lv.get(0)).getHoverName()); - - }); + expression.addUnaryFunction("item_display_name", v -> new FormattedTextValue(ValueConversions.getItemStackFromValue(v, false).getHoverName())); expression.addContextFunction("particle_box", -1, (c, t, lv) -> { diff --git a/src/main/java/carpet/script/value/EntityValue.java b/src/main/java/carpet/script/value/EntityValue.java index 77dfd27c..a082c7c5 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=ValueConversions.getItemStackFromValue(v); + ItemStack item=ValueConversions.getItemStackFromValue(v, true); if(e instanceof ItemEntity itementity) itementity.setItem(item); if(e instanceof ItemFrame itemframe) diff --git a/src/main/java/carpet/script/value/ValueConversions.java b/src/main/java/carpet/script/value/ValueConversions.java index 3d8cecb8..e870fb8c 100644 --- a/src/main/java/carpet/script/value/ValueConversions.java +++ b/src/main/java/carpet/script/value/ValueConversions.java @@ -8,6 +8,7 @@ import carpet.utils.BlockInfo; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import net.minecraft.advancements.critereon.MinMaxBounds; +import net.minecraft.commands.arguments.item.ItemInput; import net.minecraft.core.BlockPos; import net.minecraft.core.GlobalPos; import net.minecraft.core.Registry; @@ -414,30 +415,52 @@ 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()){ + public static ItemStack getItemStackFromValue(Value value, boolean withCount) + { + 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(); + final String name; + int count = 1; + CompoundTag nbtTag = null; + if (value instanceof ListValue list) + { + if (list.length() != 3) + { + throw new ThrowStatement("item definition from list of size "+list.length(), Throwables.UNKNOWN_ITEM); } - else{ - nbt=new NBTSerializableValue(items.get(2).getString()).getCompoundTag(); + final List items = list.getItems(); + name = items.get(0).getString(); + if (withCount) + { + count = NumericValue.asNumber(items.get(1)).getInt(); + } + Value nbtValue = items.get(2); + if (!nbtValue.isNull()) + { + nbtTag = ((NBTSerializableValue) NBTSerializableValue.fromValue(nbtValue)).getCompoundTag(); } } - try{ - return NBTSerializableValue.parseItem(id,nbt).createItemStack(count,false); - } catch (CommandSyntaxException e1) { - throw new IllegalStateException("Unexpected exception while creating item stack", e1); + else + { + name = value.getString(); + } + ItemInput itemInput = NBTSerializableValue.parseItem(name, nbtTag); + try + { + return itemInput.createItemStack(count,false); + } + catch (CommandSyntaxException cse) + { + if (!withCount) + { + throw new IllegalStateException("Unexpected exception while creating item stack of " + name + ". All items should be able to stack to one", cse); + } + else + { + throw new ThrowStatement(count + " stack size of " + name, Throwables.UNKNOWN_ITEM); + } } }