Search in sources :

Example 11 with ResourceLocation

use of net.minecraft.resources.ResourceLocation in project MinecraftForge by MinecraftForge.

the class ForgeItemTagsProvider method addColored.

private void addColored(Consumer<Tag.Named<Item>> consumer, Tag.Named<Item> group, String pattern) {
    String prefix = group.getName().getPath().toUpperCase(Locale.ENGLISH) + '_';
    for (DyeColor color : DyeColor.values()) {
        ResourceLocation key = new ResourceLocation("minecraft", pattern.replace("{color}", color.getName()));
        Tag.Named<Item> tag = getForgeItemTag(prefix + color.getName());
        Item item = ForgeRegistries.ITEMS.getValue(key);
        if (item == null || item == Items.AIR)
            throw new IllegalStateException("Unknown vanilla item: " + key.toString());
        tag(tag).add(item);
        consumer.accept(tag);
    }
}
Also used : Item(net.minecraft.world.item.Item) ResourceLocation(net.minecraft.resources.ResourceLocation) Tag(net.minecraft.tags.Tag) DyeColor(net.minecraft.world.item.DyeColor)

Example 12 with ResourceLocation

use of net.minecraft.resources.ResourceLocation in project MinecraftForge by MinecraftForge.

the class CraftingHelper method getCondition.

public static ICondition getCondition(JsonObject json) {
    ResourceLocation type = new ResourceLocation(GsonHelper.getAsString(json, "type"));
    IConditionSerializer<?> serializer = conditions.get(type);
    if (serializer == null)
        throw new JsonSyntaxException("Unknown condition type: " + type.toString());
    return serializer.read(json);
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) ResourceLocation(net.minecraft.resources.ResourceLocation)

Example 13 with ResourceLocation

use of net.minecraft.resources.ResourceLocation in project MinecraftForge by MinecraftForge.

the class CraftingHelper method register.

public static IConditionSerializer<?> register(IConditionSerializer<?> serializer) {
    ResourceLocation key = serializer.getID();
    if (conditions.containsKey(key))
        throw new IllegalStateException("Duplicate recipe condition serializer: " + key);
    conditions.put(key, serializer);
    return serializer;
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation)

Example 14 with ResourceLocation

use of net.minecraft.resources.ResourceLocation in project MinecraftForge by MinecraftForge.

the class CraftingHelper method write.

public static <T extends Ingredient> void write(FriendlyByteBuf buffer, T ingredient) {
    // I wonder if there is a better way generic wise...
    @SuppressWarnings("unchecked") IIngredientSerializer<T> serializer = (IIngredientSerializer<T>) ingredient.getSerializer();
    ResourceLocation key = ingredients.inverse().get(serializer);
    if (key == null)
        throw new IllegalArgumentException("Tried to serialize unregistered Ingredient: " + ingredient + " " + serializer);
    if (serializer != VanillaIngredientSerializer.INSTANCE) {
        // Marker to know there is a custom ingredient
        buffer.writeVarInt(-1);
        buffer.writeResourceLocation(key);
    }
    serializer.write(buffer, ingredient);
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation)

Example 15 with ResourceLocation

use of net.minecraft.resources.ResourceLocation in project MinecraftForge by MinecraftForge.

the class CraftingHelper method getItemStack.

public static ItemStack getItemStack(JsonObject json, boolean readNBT, boolean disallowsAirInRecipe) {
    String itemName = GsonHelper.getAsString(json, "item");
    ResourceLocation itemKey = new ResourceLocation(itemName);
    if (!ForgeRegistries.ITEMS.containsKey(itemKey))
        throw new JsonSyntaxException("Unknown item '" + itemName + "'");
    Item item = ForgeRegistries.ITEMS.getValue(itemKey);
    if (disallowsAirInRecipe && item == Items.AIR)
        throw new JsonSyntaxException("Invalid item: " + itemName);
    if (readNBT && json.has("nbt")) {
        // Lets hope this works? Needs test
        try {
            JsonElement element = json.get("nbt");
            CompoundTag nbt;
            if (element.isJsonObject())
                nbt = TagParser.parseTag(GSON.toJson(element));
            else
                nbt = TagParser.parseTag(GsonHelper.convertToString(element, "nbt"));
            CompoundTag tmp = new CompoundTag();
            if (nbt.contains("ForgeCaps")) {
                tmp.put("ForgeCaps", nbt.get("ForgeCaps"));
                nbt.remove("ForgeCaps");
            }
            tmp.put("tag", nbt);
            tmp.putString("id", itemName);
            tmp.putInt("Count", GsonHelper.getAsInt(json, "count", 1));
            return ItemStack.of(tmp);
        } catch (CommandSyntaxException e) {
            throw new JsonSyntaxException("Invalid NBT Entry: " + e.toString());
        }
    }
    return new ItemStack(item, GsonHelper.getAsInt(json, "count", 1));
}
Also used : Item(net.minecraft.world.item.Item) JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonElement(com.google.gson.JsonElement) ResourceLocation(net.minecraft.resources.ResourceLocation) ItemStack(net.minecraft.world.item.ItemStack) CompoundTag(net.minecraft.nbt.CompoundTag) CommandSyntaxException(com.mojang.brigadier.exceptions.CommandSyntaxException)

Aggregations

ResourceLocation (net.minecraft.resources.ResourceLocation)86 ArrayList (java.util.ArrayList)7 Map (java.util.Map)7 LogManager (org.apache.logging.log4j.LogManager)7 Logger (org.apache.logging.log4j.Logger)7 List (java.util.List)6 FriendlyByteBuf (net.minecraft.network.FriendlyByteBuf)6 JsonObject (com.google.gson.JsonObject)5 CommandSyntaxException (com.mojang.brigadier.exceptions.CommandSyntaxException)5 IOException (java.io.IOException)5 Collectors (java.util.stream.Collectors)5 StringReader (com.mojang.brigadier.StringReader)4 CompoundTag (net.minecraft.nbt.CompoundTag)4 CraftServer (org.bukkit.craftbukkit.v1_17_R1.CraftServer)4 ItemStack (org.bukkit.inventory.ItemStack)4 InputStream (java.io.InputStream)3 Collections (java.util.Collections)3 Set (java.util.Set)3 Function (java.util.function.Function)3 Nullable (javax.annotation.Nullable)3