Search in sources :

Example 1 with StringNbtReader

use of net.minecraft.nbt.StringNbtReader in project nbt-crafting by Siphalor.

the class MixinIngredient method loadIngredientEntryCondition.

@Unique
private static IngredientEntryCondition loadIngredientEntryCondition(JsonObject jsonObject) {
    if (jsonObject.has("data")) {
        if (JsonHelper.hasString(jsonObject, "data")) {
            try {
                CompoundTag compoundTag = new StringNbtReader(new StringReader(jsonObject.get("data").getAsString())).parseCompoundTag();
                IngredientEntryCondition condition = new IngredientEntryCondition();
                if (compoundTag.contains("require") || compoundTag.contains("deny")) {
                    if (compoundTag.contains("require"))
                        condition.requiredElements = compoundTag.getCompound("require");
                    if (compoundTag.contains("deny"))
                        condition.deniedElements = compoundTag.getCompound("deny");
                } else {
                    condition.requiredElements = compoundTag;
                }
                return condition;
            } catch (CommandSyntaxException e) {
                e.printStackTrace();
            }
        } else if (jsonObject.get("data").isJsonObject()) {
            return IngredientEntryCondition.fromJson((JsonObject) JsonPreprocessor.process(jsonObject.get("data").getAsJsonObject()));
        }
    }
    return new IngredientEntryCondition();
}
Also used : StringNbtReader(net.minecraft.nbt.StringNbtReader) StringReader(com.mojang.brigadier.StringReader) CompoundTag(net.minecraft.nbt.CompoundTag) CommandSyntaxException(com.mojang.brigadier.exceptions.CommandSyntaxException) Unique(org.spongepowered.asm.mixin.Unique)

Example 2 with StringNbtReader

use of net.minecraft.nbt.StringNbtReader in project numismatic-overhaul by wisp-forest.

the class VillagerJsonHelper method getItemStackFromJson.

public static ItemStack getItemStackFromJson(JsonObject json) {
    if (!json.has("item"))
        throw new DeserializationException("ItemStack missing item ID " + json);
    Item item = getItemFromID(json.get("item").getAsString());
    int count = json.has("count") ? json.get("count").getAsInt() : 1;
    ItemStack stack = new ItemStack(item, count);
    if (json.has("tag")) {
        String toParse = json.get("tag").getAsJsonObject().toString();
        CompoundTag stackTag = null;
        try {
            stackTag = new StringNbtReader(new StringReader(toParse)).parseCompoundTag();
        } catch (CommandSyntaxException e) {
            VillagerTradesHandler.addLoadingException(new DeserializationException("Tag parsing error: " + e.getMessage()));
        }
        if (stackTag != null)
            stack.setTag(stackTag);
    }
    return stack;
}
Also used : Item(net.minecraft.item.Item) StringNbtReader(net.minecraft.nbt.StringNbtReader) StringReader(com.mojang.brigadier.StringReader) ItemStack(net.minecraft.item.ItemStack) DeserializationException(com.glisco.numismaticoverhaul.villagers.exceptions.DeserializationException) CompoundTag(net.minecraft.nbt.CompoundTag) CommandSyntaxException(com.mojang.brigadier.exceptions.CommandSyntaxException)

Example 3 with StringNbtReader

use of net.minecraft.nbt.StringNbtReader in project Pehkui by Virtuoel.

the class PehkuiEntitySelectorOptions method register.

public static void register() {
    EntitySelectorOptionsInvoker.callPutOption(Pehkui.id("scale").toString().replace(':', '.'), r -> cast(r).pehkui_setScaleRange(FloatRange.parse(r.getReader())), r -> cast(r).pehkui_getScaleRange().isDummy(), SCALE_RANGE_DESCRIPTION);
    EntitySelectorOptionsInvoker.callPutOption(Pehkui.id("scale_type").toString().replace(':', '.'), r -> cast(r).pehkui_setScaleType(parseScaleType(r)), r -> cast(r).pehkui_getScaleType() == ScaleTypes.INVALID, SCALE_TYPE_DESCRIPTION);
    EntitySelectorOptionsInvoker.callPutOption(Pehkui.id("computed_scale").toString().replace(':', '.'), r -> cast(r).pehkui_setComputedScaleRange(FloatRange.parse(r.getReader())), r -> cast(r).pehkui_getComputedScaleRange().isDummy(), SCALE_RANGE_DESCRIPTION);
    EntitySelectorOptionsInvoker.callPutOption(Pehkui.id("computed_scale_type").toString().replace(':', '.'), r -> cast(r).pehkui_setComputedScaleType(parseScaleType(r)), r -> cast(r).pehkui_getComputedScaleType() == ScaleTypes.INVALID, SCALE_TYPE_DESCRIPTION);
    EntitySelectorOptionsInvoker.callPutOption(Pehkui.id("scale_nbt").toString().replace(':', '.'), r -> {
        final boolean negated = r.readNegationCharacter();
        final NbtCompound parsed = (new StringNbtReader(r.getReader())).parseCompound();
        r.setPredicate(entity -> {
            final NbtCompound nbt = ((PehkuiEntityExtensions) entity).pehkui_writeScaleNbt(new NbtCompound());
            return NbtHelper.matches(parsed, nbt, true) != negated;
        });
    }, reader -> true, SCALE_NBT_DESCRIPTION);
}
Also used : StringNbtReader(net.minecraft.nbt.StringNbtReader) NbtCompound(net.minecraft.nbt.NbtCompound) PehkuiEntityExtensions(virtuoel.pehkui.util.PehkuiEntityExtensions)

Aggregations

StringNbtReader (net.minecraft.nbt.StringNbtReader)3 StringReader (com.mojang.brigadier.StringReader)2 CommandSyntaxException (com.mojang.brigadier.exceptions.CommandSyntaxException)2 CompoundTag (net.minecraft.nbt.CompoundTag)2 DeserializationException (com.glisco.numismaticoverhaul.villagers.exceptions.DeserializationException)1 Item (net.minecraft.item.Item)1 ItemStack (net.minecraft.item.ItemStack)1 NbtCompound (net.minecraft.nbt.NbtCompound)1 Unique (org.spongepowered.asm.mixin.Unique)1 PehkuiEntityExtensions (virtuoel.pehkui.util.PehkuiEntityExtensions)1