Jump to content

How do I get what's there in a slot in a tile entity


PadFoot2008

Recommended Posts

I don't get what do I have to do exactly.

I'm following Kaupenjoe's tutorial: https://github.com/Tutorials-By-Kaupenjoe/Minecraft-1.16.5

Here's the TileEntity class:

public class EnchantingPedestralTile extends TileEntity implements ITickableTileEntity {

    private final ItemStackHandler itemHandler = createHandler();
    private final LazyOptional<IItemHandler> handler = LazyOptional.of(() -> itemHandler);

    public EnchantingPedestralTile(TileEntityType<?> tileEntityTypeIn) {

        super(tileEntityTypeIn);
    }

    public EnchantingPedestralTile() {

        this(ModTileEntities.ENCHANTING_PEDESTRAL_TILE.get());
    }

    @Override
    public void read(BlockState state, CompoundNBT nbt) {
        itemHandler.deserializeNBT(nbt.getCompound("inv"));
        super.read(state, nbt);
    }

    @Override
    public CompoundNBT write(CompoundNBT compound) {
        compound.put("inv", itemHandler.serializeNBT());
        return super.write(compound);
    }

    private ItemStackHandler createHandler() {
        return new ItemStackHandler(2) {
            @Override
            protected void onContentsChanged(int slot) {
                markDirty();
            }

            @Override
            public boolean isItemValid(int slot, @Nonnull ItemStack stack) {
                return true;
            }

            @Override
            public int getSlotLimit(int slot) {
                return 1;
            }

            @Nonnull
            @Override
            public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate) {
                if(!isItemValid(slot, stack)) {
                    return stack;
                }

                return super.insertItem(slot, stack, simulate);
            }
        };
    }

    @Nonnull
    @Override
    public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
        if(cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
            return handler.cast();
        }


        return super.getCapability(cap, side);
    }

    private void strikeLightning() {
        if(!this.world.isRemote()) {
            EntityType.LIGHTNING_BOLT.spawn((ServerWorld)world, null, null,
                    pos, SpawnReason.TRIGGERED, true, true);
        }
    }

    public void craft() {
        Inventory inv = new Inventory(itemHandler.getSlots());
        for (int i = 0; i < itemHandler.getSlots(); i++) {
            inv.setInventorySlotContents(i, itemHandler.getStackInSlot(i));
        }

        Optional<EnchantingPedestralRecipe> recipe = world.getRecipeManager()
                .getRecipe(ModRecipeTypes.ENCHANTING_RECIPE, inv, world);

        recipe.ifPresent(iRecipe -> {
            ItemStack tool = iRecipe.getRecipeOutput();

            if(iRecipe.getWeather().equals(EnchantingPedestralRecipe.Weather.CLEAR) &&
                    !world.isRaining()) {
                craftTheItem(tool);
            }

            if(iRecipe.getWeather().equals(EnchantingPedestralRecipe.Weather.RAIN) &&
                    world.isRaining()) {
                craftTheItem(tool);
            }

            if(iRecipe.getWeather().equals(EnchantingPedestralRecipe.Weather.THUNDERING) &&
                    world.isThundering()) {
                strikeLightning();
                craftTheItem(tool);
            }

            markDirty();
        });
    }

    private void craftTheItem(ItemStack tool) {
        itemHandler.extractItem(0, 1, false);
        itemHandler.extractItem(1, 1, false);
        itemHandler.insertItem(0, tool, false);
    }

    @Override
    public void tick() {
        if(world.isRemote)
            return;

        craft();
    }
}

And the custom Recipe class:

public class EnchantingPedestralRecipe implements IEnchantingPedestralRecipe {
    public enum Weather {
        CLEAR,
        RAIN,
        THUNDERING;

        public static Weather getWeatherByString(String s) {
            return Objects.equals(s, "thundering") ? THUNDERING : Objects.equals(s, "rain") ? RAIN : CLEAR;
        }
    }

    private final ResourceLocation id;
    private final ItemStack output;
    private final NonNullList<Ingredient> recipeItems;
    private final Weather weather;

    public EnchantingPedestralRecipe(ResourceLocation id, ItemStack output,
                                    NonNullList<Ingredient> recipeItems, Weather weather) {
        this.id = id;
        this.output = output;
        this.recipeItems = recipeItems;
        this.weather = weather;
    }

    @Override
    public boolean matches(IInventory inv, World worldIn) {
        // Checks for correct focus (Glass Pane)
        if(recipeItems.get(0).test(inv.getStackInSlot(0))) {
            return recipeItems.get(1).test(inv.getStackInSlot(1));
        }

        return false;
    }

    @Override
    public NonNullList<Ingredient> getIngredients() {
        return recipeItems;
    }

    @Override
    public ItemStack getCraftingResult(IInventory inv) {
        return output;
    }

    @Override
    public ItemStack getRecipeOutput() {
        return output.copy();
    }

    public Weather getWeather() {
        return this.weather;
    }

    public ItemStack getIcon() {
        return new ItemStack(ModBlocks.ENCHANTING_PEDESTRAL.get());
    }

    @Override
    public ResourceLocation getId() {
        return id;
    }

    @Override
    public IRecipeSerializer<?> getSerializer() {
        return ModRecipeTypes.ENCHANTING_SERIALIZER.get();
    }

    public static class EnchantingRecipeType implements IRecipeType<EnchantingPedestralRecipe> {
        @Override
        public String toString() {
            return EnchantingPedestralRecipe.TYPE_ID.toString();
        }
    }

    public static class Serializer extends ForgeRegistryEntry<IRecipeSerializer<?>>
            implements IRecipeSerializer<EnchantingPedestralRecipe> {


        @Override
        public EnchantingPedestralRecipe read(ResourceLocation recipeId, JsonObject json) {
            String enchantraw = JSONUtils.getString(json, "enchantment");
            Enchantment enchant = ForgeRegistries.ENCHANTMENTS.getValue(new ResourceLocation(enchantraw));
      		
      		//Here's where I want to get what's in the first slot of my TileEntity

            ItemStack output = ShapedRecipe.deserializeItem(JSONUtils.getJsonObject(json, "output"));
            output.addEnchantment(enchant,1);

            String weather = JSONUtils.getString(json, "weather");

            JsonArray ingredients = JSONUtils.getJsonArray(json, "ingredients");
            NonNullList<Ingredient> inputs = NonNullList.withSize(2, Ingredient.EMPTY);

            for (int i = 0; i < inputs.size(); i++) {
                inputs.set(i, Ingredient.deserialize(ingredients.get(i)));
            }

            return new EnchantingPedestralRecipe(recipeId, output,
                    inputs, Weather.getWeatherByString(weather));
        }

        @Nullable
        @Override
        public EnchantingPedestralRecipe read(ResourceLocation recipeId, PacketBuffer buffer) {
            NonNullList<Ingredient> inputs = NonNullList.withSize(2, Ingredient.EMPTY);

            for (int i = 0; i < inputs.size(); i++) {
                inputs.set(i, Ingredient.read(buffer));
            }

            ItemStack output = buffer.readItemStack();
            return new EnchantingPedestralRecipe(recipeId, output,
                    inputs, null);
        }

        @Override
        public void write(PacketBuffer buffer, EnchantingPedestralRecipe recipe) {
            buffer.writeInt(recipe.getIngredients().size());
            for (Ingredient ing : recipe.getIngredients()) {
                ing.write(buffer);
            }
            buffer.writeItemStack(recipe.getRecipeOutput(), false);
        }
    }
}

 

What do I add to the Recipe class?

Edited by PadFoot2008
Link to comment
Share on other sites

I'm making a Tile Entity. It has 2 slots: one for a tool and the second for a crystal. The tool will be enchanted based on the crystal provided. Which enchantment will be applied in respect to the crystal is kept in a Json file. I've been able to get enchantment from the Json file and it also checks whether the correct crystal is present.

But I am unable to get the tool present in the first slot and then enchant it.

Link to comment
Share on other sites

Wait, let me explain –

Currently, in recipe Json, there's 5 objects –

1. Weather – Returns the weather required for the recipe to work

2. Enchantment – Returns the enchantment that will be applied to the output

3. Ingredients (slot 0) – Returns the tool required in slot 0.

4. Ingredients (slot 1) – Returns the crystal required in slot 1.

5. Output – Returns the output tool.

So, I get the Output and the Enchantment and then apply the enchantment on the output and then return it in

public EnchantingPedestralRecipe read(ResourceLocation recipeId, PacketBuffer buffer)

. And in

public boolean matches(IInventory inv, World worldIn)

I check if Ingredient (slot 0) and Ingredient (slot 1) are placed in their respective slots.

This will be really tedious as I have to add recipes for all tools and armour and their respective Enchantments.

 

Instead, I thought it would be better if

public boolean matches(IInventory inv, World worldIn)

would check for only the Ingredients (slot 1) in slot 1 and any tool that will be placed slot 0 shall be enchanted based it. For that I would need to get what is there in slot 0 in

public EnchantingPedestralRecipe read(ResourceLocation recipeId, PacketBuffer buffer)

get the enchantment and then enchant it; and return it as the output. 

 

So how do I get what's kept in slot 0?

Edited by PadFoot2008
Link to comment
Share on other sites

Thanks it worked!

But I just wanted to know how can I get the max enchantment level of an enchantment like for Power it's 5, for Fortune it's 3, etc. Is there a method I can use like:

int enchantmentlevel = SomeClass.getEnchantmentLevel(Enchantments.SILK_TOUCH);

 

Link to comment
Share on other sites

Hye I want a bit of help.

I modified the code to this :

public class EnchantingPedestralRecipe implements IEnchantingPedestralRecipe {
    public enum Weather {
        CLEAR,
        RAIN,
        THUNDERING;

        public static Weather getWeatherByString(String s) {
            return Objects.equals(s, "thundering") ? THUNDERING : Objects.equals(s, "rain") ? RAIN : CLEAR;
        }
    }

    private final ResourceLocation id;
    private final ItemStack output;
    private final NonNullList<Ingredient> recipeItems;
    private final Weather weather;

    public static ItemStack outputin;

    public EnchantingPedestralRecipe(ResourceLocation id, ItemStack output,
                                    NonNullList<Ingredient> recipeItems, Weather weather) {
        this.id = id;
        this.output = output;
        this.recipeItems = recipeItems;
        this.weather = weather;
    }

    @Override
    public boolean matches(IInventory inv, World worldIn) {
        ItemStack item = inv.getStackInSlot(0);
        if(item != null) {
            if (recipeItems.get(1).test(inv.getStackInSlot(1))) {
                outputin = item;
                return recipeItems.get(1).test(inv.getStackInSlot(1));
            }
        }
        return false;
    }

    @Override
    public NonNullList<Ingredient> getIngredients() {
        return recipeItems;
    }

    @Override
    public ItemStack getCraftingResult(IInventory inv) {
        return output;
    }

    @Override
    public ItemStack getRecipeOutput() {
        return output.copy();
    }

    public Weather getWeather() {
        return this.weather;
    }

    public ItemStack getIcon() {
        return new ItemStack(ModBlocks.ENCHANTING_PEDESTRAL.get());
    }

    @Override
    public ResourceLocation getId() {
        return id;
    }

    @Override
    public IRecipeSerializer<?> getSerializer() {
        return ModRecipeTypes.ENCHANTING_SERIALIZER.get();
    }

    public static class EnchantingRecipeType implements IRecipeType<EnchantingPedestralRecipe> {
        @Override
        public String toString() {
            return EnchantingPedestralRecipe.TYPE_ID.toString();
        }
    }

    public static class Serializer extends ForgeRegistryEntry<IRecipeSerializer<?>>
            implements IRecipeSerializer<EnchantingPedestralRecipe> {


        @Override
        public EnchantingPedestralRecipe read(ResourceLocation recipeId, JsonObject json) {
            String enchantraw = JSONUtils.getString(json, "enchantment");
            Enchantment enchant = ForgeRegistries.ENCHANTMENTS.getValue(new ResourceLocation(enchantraw));

            ItemStack output;

            if (outputin != null) {
                output = outputin;
                int i = EnchantmentHelper.getEnchantmentLevel(enchant, output);
                int j = enchant.getMaxLevel();
                int k = i++;
                if (output.isEnchantable() == true) {
                    if (i<j) {
                        output.addEnchantment(enchant, k);
                    }
                }
            } else {
                output = new ItemStack(Items.BELL);
            }

            String weather = JSONUtils.getString(json, "weather");

            JsonArray ingredients = JSONUtils.getJsonArray(json, "ingredients");
            NonNullList<Ingredient> inputs = NonNullList.withSize(2, Ingredient.EMPTY);

            for (int i = 0; i < inputs.size(); i++) {
                inputs.set(i, Ingredient.deserialize(ingredients.get(i)));
            }

            return new EnchantingPedestralRecipe(recipeId, output,
                    inputs, Weather.getWeatherByString(weather));
        }

        @Nullable
        @Override
        public EnchantingPedestralRecipe read(ResourceLocation recipeId, PacketBuffer buffer) {
            NonNullList<Ingredient> inputs = NonNullList.withSize(2, Ingredient.EMPTY);

            for (int i = 0; i < inputs.size(); i++) {
                inputs.set(i, Ingredient.read(buffer));
            }

            ItemStack output = buffer.readItemStack();
            return new EnchantingPedestralRecipe(recipeId, output,
                    inputs, null);
        }

        @Override
        public void write(PacketBuffer buffer, EnchantingPedestralRecipe recipe) {
            buffer.writeInt(recipe.getIngredients().size());
            for (Ingredient ing : recipe.getIngredients()) {
                ing.write(buffer);
            }
            buffer.writeItemStack(recipe.getRecipeOutput(), false);
        }
    }
}

And when I run Minecraft, it always outputs a bell, instead of enchanting the item? What's the mistake I've done? How do I solve it?

Edited by PadFoot2008
Link to comment
Share on other sites

34 minutes ago, PadFoot2008 said:
output.addEnchantment(enchant, k);

It does enchant.

8 minutes ago, diesieben07 said:

Why is this static? Do you know what static means? Why is this assigned in matches? The values inside a recipe instance should really be immutable, i.e. the recipe should be stateless.

So what do I do? You know what I want to do. I have told that earlier in this post itself. Please help.

Link to comment
Share on other sites

Okay, but what do I add here?

@Nullable
        @Override
        public EnchantingPedestralRecipe read(ResourceLocation recipeId, PacketBuffer buffer) {
            NonNullList<Ingredient> inputs = NonNullList.withSize(2, Ingredient.EMPTY);

            for (int i = 0; i < inputs.size(); i++) {
                inputs.set(i, Ingredient.read(buffer));
            }

            ItemStack output = buffer.readItemStack();
            //What do I add here
            return new EnchantingPedestralRecipe(recipeId, output,
                    inputs, null, enchant);
        }

The whole code (as of now):

package com.padfoot.immersivesurvival.data.recipes;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonObject;
import com.padfoot.immersivesurvival.block.ModBlocks;
import com.padfoot.immersivesurvival.container.EnchantingPedestralContainer;
import com.padfoot.immersivesurvival.screen.EnchantingPedestralScreen;
import com.padfoot.immersivesurvival.tileentity.EnchantingPedestralTile;
import net.minecraft.enchantment.*;
import net.minecraft.entity.item.minecart.MinecartEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.item.crafting.IRecipeSerializer;
import net.minecraft.item.crafting.IRecipeType;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.item.crafting.ShapedRecipe;
import net.minecraft.nbt.ListNBT;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.JSONUtils;
import net.minecraft.util.NonNullList;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraftforge.client.MinecraftForgeClient;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.ForgeRegistryEntry;

import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.Objects;

public class EnchantingPedestralRecipe implements IEnchantingPedestralRecipe {
    public enum Weather {
        CLEAR,
        RAIN,
        THUNDERING;

        public static Weather getWeatherByString(String s) {
            return Objects.equals(s, "thundering") ? THUNDERING : Objects.equals(s, "rain") ? RAIN : CLEAR;
        }
    }

    private final ResourceLocation id;
    private final ItemStack output;
    private final NonNullList<Ingredient> recipeItems;
    private final Weather weather;
    private final Enchantment enchant;

    public EnchantingPedestralRecipe(ResourceLocation id, ItemStack output,
                                    NonNullList<Ingredient> recipeItems, Weather weather, Enchantment enchant) {
        this.id = id;
        this.output = output;
        this.recipeItems = recipeItems;
        this.weather = weather;
        this.enchant = enchant;
    }

    @Override
    public boolean matches(IInventory inv, World worldIn) {
            if (recipeItems.get(1).test(inv.getStackInSlot(1))) {
                return recipeItems.get(1).test(inv.getStackInSlot(1));
        }
        return false;
    }

    @Override
    public NonNullList<Ingredient> getIngredients() {
        return recipeItems;
    }

    @Override
    public ItemStack getCraftingResult(IInventory inv) {
        ItemStack output = inv.getStackInSlot(0);
        int i = EnchantmentHelper.getEnchantmentLevel(enchant, output);
        int j = enchant.getMaxLevel();
        int k = i++;
        if (output.isEnchantable() == true) {
            if (i<j) {
                    output.addEnchantment(enchant, k);
            }
        }
        return output;
    }

    @Override
    public ItemStack getRecipeOutput() {
        return output.copy();
    }

    public Weather getWeather() {
        return this.weather;
    }

    public ItemStack getIcon() {
        return new ItemStack(ModBlocks.ENCHANTING_PEDESTRAL.get());
    }

    @Override
    public ResourceLocation getId() {
        return id;
    }

    @Override
    public IRecipeSerializer<?> getSerializer() {
        return ModRecipeTypes.ENCHANTING_SERIALIZER.get();
    }

    public static class EnchantingRecipeType implements IRecipeType<EnchantingPedestralRecipe> {
        @Override
        public String toString() {
            return EnchantingPedestralRecipe.TYPE_ID.toString();
        }
    }

    public static class Serializer extends ForgeRegistryEntry<IRecipeSerializer<?>>
            implements IRecipeSerializer<EnchantingPedestralRecipe> {


        @Override
        public EnchantingPedestralRecipe read(ResourceLocation recipeId, JsonObject json) {
            String enchantraw = JSONUtils.getString(json, "enchantment");
            Enchantment enchant = ForgeRegistries.ENCHANTMENTS.getValue(new ResourceLocation(enchantraw));

            ItemStack output = new ItemStack(Items.BELL);


            String weather = JSONUtils.getString(json, "weather");

            JsonArray ingredients = JSONUtils.getJsonArray(json, "ingredients");
            NonNullList<Ingredient> inputs = NonNullList.withSize(2, Ingredient.EMPTY);

            for (int i = 0; i < inputs.size(); i++) {
                inputs.set(i, Ingredient.deserialize(ingredients.get(i)));
            }

            return new EnchantingPedestralRecipe(recipeId, output,
                    inputs, Weather.getWeatherByString(weather), enchant);
        }

        @Nullable
        @Override
        public EnchantingPedestralRecipe read(ResourceLocation recipeId, PacketBuffer buffer) {
            NonNullList<Ingredient> inputs = NonNullList.withSize(2, Ingredient.EMPTY);

            for (int i = 0; i < inputs.size(); i++) {
                inputs.set(i, Ingredient.read(buffer));
            }

            ItemStack output = buffer.readItemStack();
			//What do I add here
            return new EnchantingPedestralRecipe(recipeId, output,
                    inputs, null, enchant);
        }

        @Override
        public void write(PacketBuffer buffer, EnchantingPedestralRecipe recipe) {
            buffer.writeInt(recipe.getIngredients().size());
            for (Ingredient ing : recipe.getIngredients()) {
                ing.write(buffer);
            }
            buffer.writeItemStack(recipe.getRecipeOutput(), false);
          	//Do I add anything here?
        }
    }
}

Also do I need to anything in write method?

Link to comment
Share on other sites

It isn't working (see code):

public class EnchantingPedestralRecipe implements IEnchantingPedestralRecipe {
    public enum Weather {
        CLEAR,
        RAIN,
        THUNDERING;

        public static Weather getWeatherByString(String s) {
            return Objects.equals(s, "thundering") ? THUNDERING : Objects.equals(s, "rain") ? RAIN : CLEAR;
        }
    }

    private final ResourceLocation id;
    private final ItemStack output;
    private final NonNullList<Ingredient> recipeItems;
    private final Weather weather;
    private final Enchantment enchant;

    public EnchantingPedestralRecipe(ResourceLocation id, ItemStack output,
                                    NonNullList<Ingredient> recipeItems, Weather weather, Enchantment enchant) {
        this.id = id;
        this.output = output;
        this.recipeItems = recipeItems;
        this.weather = weather;
        this.enchant = enchant;
    }

    @Override
    public boolean matches(IInventory inv, World worldIn) {
            if (recipeItems.get(1).test(inv.getStackInSlot(1))) {
                return recipeItems.get(1).test(inv.getStackInSlot(1));
        }
        return false;
    }

    @Override
    public NonNullList<Ingredient> getIngredients() {
        return recipeItems;
    }

    @Override
    public ItemStack getCraftingResult(IInventory inv) {
        ItemStack output = inv.getStackInSlot(0);
        int i = EnchantmentHelper.getEnchantmentLevel(enchant, output);
        int j = enchant.getMaxLevel();
        int k = i++;
        if (output.isEnchantable() == true) {
            if (i<j) {
                    output.addEnchantment(enchant, k);
            }
        }
        return output;
    }

    @Override
    public ItemStack getRecipeOutput() {
        return output.copy();
    }

    public Enchantment getEnchant() {
        return enchant;
    }

    public Weather getWeather() {
        return this.weather;
    }

    public ItemStack getIcon() {
        return new ItemStack(ModBlocks.ENCHANTING_PEDESTRAL.get());
    }

    @Override
    public ResourceLocation getId() {
        return id;
    }

    @Override
    public IRecipeSerializer<?> getSerializer() {
        return ModRecipeTypes.ENCHANTING_SERIALIZER.get();
    }

    public static class EnchantingRecipeType implements IRecipeType<EnchantingPedestralRecipe> {
        @Override
        public String toString() {
            return EnchantingPedestralRecipe.TYPE_ID.toString();
        }
    }

    public static class Serializer extends ForgeRegistryEntry<IRecipeSerializer<?>>
            implements IRecipeSerializer<EnchantingPedestralRecipe> {


        @Override
        public EnchantingPedestralRecipe read(ResourceLocation recipeId, JsonObject json) {
            String enchantraw = JSONUtils.getString(json, "enchantment");
            Enchantment enchant = ForgeRegistries.ENCHANTMENTS.getValue(new ResourceLocation(enchantraw));

            ItemStack output = null; //I know this is incorrect; what do I do?


            String weather = JSONUtils.getString(json, "weather");

            JsonArray ingredients = JSONUtils.getJsonArray(json, "ingredients");
            NonNullList<Ingredient> inputs = NonNullList.withSize(2, Ingredient.EMPTY);

            for (int i = 0; i < inputs.size(); i++) {
                inputs.set(i, Ingredient.deserialize(ingredients.get(i)));
            }

            return new EnchantingPedestralRecipe(recipeId, output,
                    inputs, Weather.getWeatherByString(weather), enchant);
        }

        @Nullable
        @Override
        public EnchantingPedestralRecipe read(ResourceLocation recipeId, PacketBuffer buffer) {
            NonNullList<Ingredient> inputs = NonNullList.withSize(2, Ingredient.EMPTY);

            for (int i = 0; i < inputs.size(); i++) {
                inputs.set(i, Ingredient.read(buffer));
            }

            ItemStack output = buffer.readItemStack();
            Enchantment enchant = buffer.readRegistryIdUnsafe(ForgeRegistries.ENCHANTMENTS);
            return new EnchantingPedestralRecipe(recipeId, output,
                    inputs, null, enchant);
        }

        @Override
        public void write(PacketBuffer buffer, EnchantingPedestralRecipe recipe) {
            buffer.writeInt(recipe.getIngredients().size());
            for (Ingredient ing : recipe.getIngredients()) {
                ing.write(buffer);
            }
            buffer.writeItemStack(recipe.getRecipeOutput(), false);
            buffer.writeRegistryIdUnsafe(ForgeRegistries.ENCHANTMENTS, recipe.getEnchant());
        }
    }
}

 

Edited by PadFoot2008
Link to comment
Share on other sites

But this is a syntax error? In read method output's coming as red.

public class EnchantingPedestralRecipe implements IEnchantingPedestralRecipe {
    public enum Weather {
        CLEAR,
        RAIN,
        THUNDERING;

        public static Weather getWeatherByString(String s) {
            return Objects.equals(s, "thundering") ? THUNDERING : Objects.equals(s, "rain") ? RAIN : CLEAR;
        }
    }

    private final ResourceLocation id;
    private final ItemStack output;
    private final NonNullList<Ingredient> recipeItems;
    private final Weather weather;
    private final Enchantment enchant;

    public EnchantingPedestralRecipe(ResourceLocation id, ItemStack output,
                                    NonNullList<Ingredient> recipeItems, Weather weather, Enchantment enchant) {
        this.id = id;
        this.output = output;
        this.recipeItems = recipeItems;
        this.weather = weather;
        this.enchant = enchant;
    }

    @Override
    public boolean matches(IInventory inv, World worldIn) {
            if (recipeItems.get(1).test(inv.getStackInSlot(1))) {
                return recipeItems.get(1).test(inv.getStackInSlot(1));
        }
        return false;
    }

    @Override
    public NonNullList<Ingredient> getIngredients() {
        return recipeItems;
    }

    @Override
    public ItemStack getCraftingResult(IInventory inv) {
        ItemStack output = inv.getStackInSlot(0);
        int i = EnchantmentHelper.getEnchantmentLevel(enchant, output);
        int j = enchant.getMaxLevel();
        int k = i++;
        if (output.isEnchantable() == true) {
            if (i<j) {
                    output.addEnchantment(enchant, k);
            }
        }
        return output;
    }

    @Override
    public ItemStack getRecipeOutput() {
        return output.copy();
    }

    public Enchantment getEnchant() {
        return enchant;
    }

    public Weather getWeather() {
        return this.weather;
    }

    public ItemStack getIcon() {
        return new ItemStack(ModBlocks.ENCHANTING_PEDESTRAL.get());
    }

    @Override
    public ResourceLocation getId() {
        return id;
    }

    @Override
    public IRecipeSerializer<?> getSerializer() {
        return ModRecipeTypes.ENCHANTING_SERIALIZER.get();
    }

    public static class EnchantingRecipeType implements IRecipeType<EnchantingPedestralRecipe> {
        @Override
        public String toString() {
            return EnchantingPedestralRecipe.TYPE_ID.toString();
        }
    }

    public static class Serializer extends ForgeRegistryEntry<IRecipeSerializer<?>>
            implements IRecipeSerializer<EnchantingPedestralRecipe> {


        @Override
        public EnchantingPedestralRecipe read(ResourceLocation recipeId, JsonObject json) {
            String enchantraw = JSONUtils.getString(json, "enchantment");
            Enchantment enchant = ForgeRegistries.ENCHANTMENTS.getValue(new ResourceLocation(enchantraw));



            String weather = JSONUtils.getString(json, "weather");

            JsonArray ingredients = JSONUtils.getJsonArray(json, "ingredients");
            NonNullList<Ingredient> inputs = NonNullList.withSize(2, Ingredient.EMPTY);

            for (int i = 0; i < inputs.size(); i++) {
                inputs.set(i, Ingredient.deserialize(ingredients.get(i)));
            }

            return new EnchantingPedestralRecipe(recipeId, output,
                    inputs, Weather.getWeatherByString(weather), enchant);
        }

        @Nullable
        @Override
        public EnchantingPedestralRecipe read(ResourceLocation recipeId, PacketBuffer buffer) {
            NonNullList<Ingredient> inputs = NonNullList.withSize(2, Ingredient.EMPTY);

            for (int i = 0; i < inputs.size(); i++) {
                inputs.set(i, Ingredient.read(buffer));
            }

            ItemStack output = buffer.readItemStack();
            Enchantment enchant = buffer.readRegistryIdUnsafe(ForgeRegistries.ENCHANTMENTS);
            return new EnchantingPedestralRecipe(recipeId, output,
                    inputs, null, enchant);
        }

        @Override
        public void write(PacketBuffer buffer, EnchantingPedestralRecipe recipe) {
            buffer.writeInt(recipe.getIngredients().size());
            for (Ingredient ing : recipe.getIngredients()) {
                ing.write(buffer);
            }
            buffer.writeItemStack(recipe.getRecipeOutput(), false);
            buffer.writeRegistryIdUnsafe(ForgeRegistries.ENCHANTMENTS, recipe.getEnchant());
        }
    }
}

 

Link to comment
Share on other sites

Eg, ItemStack.EMPTY ...

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Current code:

public class EnchantingPedestralRecipe implements IEnchantingPedestralRecipe {
    public enum Weather {
        CLEAR,
        RAIN,
        THUNDERING;

        public static Weather getWeatherByString(String s) {
            return Objects.equals(s, "thundering") ? THUNDERING : Objects.equals(s, "rain") ? RAIN : CLEAR;
        }
    }

    private final ResourceLocation id;
    private final NonNullList<Ingredient> recipeItems;
    private final Weather weather;
    private final Enchantment enchant;

    public EnchantingPedestralRecipe(ResourceLocation id,
                                    NonNullList<Ingredient> recipeItems, Weather weather, Enchantment enchant) {
        this.id = id;
        this.recipeItems = recipeItems;
        this.weather = weather;
        this.enchant = enchant;
    }

    @Override
    public boolean matches(IInventory inv, World worldIn) {
            if (recipeItems.get(1).test(inv.getStackInSlot(1))) {
                return recipeItems.get(1).test(inv.getStackInSlot(1));
        }
        return false;
    }

    @Override
    public NonNullList<Ingredient> getIngredients() {
        return recipeItems;
    }

    @Override
    public ItemStack getCraftingResult(IInventory inv) {
        ItemStack output = inv.getStackInSlot(0);
        int i = EnchantmentHelper.getEnchantmentLevel(enchant, output);
        int j = enchant.getMaxLevel();
        int k = i++;
        if (output.isEnchantable() == true) {
            if (i<j) {
                    output.addEnchantment(enchant, k);
            }
        }
        return output;
    }

    @Override
    public ItemStack getRecipeOutput() {
        ItemStack output = ItemStack.EMPTY;
        return output.copy();
    }

    public Enchantment getEnchant() {
        return enchant;
    }

    public Weather getWeather() {
        return this.weather;
    }

    public ItemStack getIcon() {
        return new ItemStack(ModBlocks.ENCHANTING_PEDESTRAL.get());
    }

    @Override
    public ResourceLocation getId() {
        return id;
    }

    @Override
    public IRecipeSerializer<?> getSerializer() {
        return ModRecipeTypes.ENCHANTING_SERIALIZER.get();
    }

    public static class EnchantingRecipeType implements IRecipeType<EnchantingPedestralRecipe> {
        @Override
        public String toString() {
            return EnchantingPedestralRecipe.TYPE_ID.toString();
        }
    }

    public static class Serializer extends ForgeRegistryEntry<IRecipeSerializer<?>>
            implements IRecipeSerializer<EnchantingPedestralRecipe> {


        @Override
        public EnchantingPedestralRecipe read(ResourceLocation recipeId, JsonObject json) {
            String enchantraw = JSONUtils.getString(json, "enchantment");
            Enchantment enchant = ForgeRegistries.ENCHANTMENTS.getValue(new ResourceLocation(enchantraw));



            String weather = JSONUtils.getString(json, "weather");

            JsonArray ingredients = JSONUtils.getJsonArray(json, "ingredients");
            NonNullList<Ingredient> inputs = NonNullList.withSize(2, Ingredient.EMPTY);

            for (int i = 0; i < inputs.size(); i++) {
                inputs.set(i, Ingredient.deserialize(ingredients.get(i)));
            }

            return new EnchantingPedestralRecipe(recipeId,
                    inputs, Weather.getWeatherByString(weather), enchant);
        }

        @Nullable
        @Override
        public EnchantingPedestralRecipe read(ResourceLocation recipeId, PacketBuffer buffer) {
            NonNullList<Ingredient> inputs = NonNullList.withSize(2, Ingredient.EMPTY);

            for (int i = 0; i < inputs.size(); i++) {
                inputs.set(i, Ingredient.read(buffer));
            }

            Enchantment enchant = buffer.readRegistryIdUnsafe(ForgeRegistries.ENCHANTMENTS);
            return new EnchantingPedestralRecipe(recipeId,
                    inputs, null, enchant);
        }

        @Override
        public void write(PacketBuffer buffer, EnchantingPedestralRecipe recipe) {
            buffer.writeInt(recipe.getIngredients().size());
            for (Ingredient ing : recipe.getIngredients()) {
                ing.write(buffer);
            }
            buffer.writeRegistryIdUnsafe(ForgeRegistries.ENCHANTMENTS, recipe.getEnchant());
        }
    }
}

Please help.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.