Jump to content

Recommended Posts

Posted

Hey everyone,

so iv finally got down to making a recipe class and a few recipes for my pulverizer, but the problem im running into is binding it to my TE, i have looked at the vanilla furnace container and TE for doing this.

as you can see in the code below i have tried doing it just like the furnace did it, but no luck.

TE Code:

package abused_master.JATMA.TE;

import javax.annotation.Nullable;

import abused_master.JATMA.GUI.GuiPulverizer;
import abused_master.JATMA.Registry.ModBlocks;
import abused_master.JATMA.TE.CraftingHandlers.RecipePulverizer;
import cofh.api.energy.EnergyStorage;
import cofh.api.energy.IEnergyProvider;
import cofh.api.energy.IEnergyReceiver;
import cofh.api.energy.TileEnergyHandler;
import net.minecraft.block.BlockFurnace;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.inventory.InventoryHelper;
import net.minecraft.inventory.ItemStackHelper;
import net.minecraft.inventory.SlotFurnaceFuel;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.FurnaceRecipes;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.text.ITextComponent;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.ItemStackHandler;
import scala.xml.persistent.SetStorage;

public class TilePulverizer extends TileEntity implements ISidedInventory, IEnergyReceiver {

protected EnergyStorage storage = new EnergyStorage(50000);
    public static final int SIZE = 2;
    private ItemStack[] pulverizerItemStacks = new ItemStack[2];

    public TilePulverizer() {
    	super();
    }

@Override
public void readFromNBT(NBTTagCompound nbt) {
	super.readFromNBT(nbt);
	storage.readFromNBT(nbt);

	if (nbt.hasKey("items")) {
            itemStackHandler.deserializeNBT((NBTTagCompound) nbt.getTag("items"));
        }
}

@Override
public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
	super.writeToNBT(nbt);
        nbt.setTag("items", itemStackHandler.serializeNBT());
	return storage.writeToNBT(nbt);
}

@Override
public boolean canConnectEnergy(EnumFacing from) {

	return true;
}

@Override
public int receiveEnergy(EnumFacing from, int maxReceive, boolean simulate) {
	return storage.receiveEnergy(maxReceive, simulate);
}

@Override
public int getEnergyStored(EnumFacing from) {

	return storage.getEnergyStored();
}

@Override
public int getMaxEnergyStored(EnumFacing from) {

	return storage.getMaxEnergyStored();
}

    private ItemStackHandler itemStackHandler = new ItemStackHandler(SIZE) {
        @Override
        protected void onContentsChanged(int slot) {
            TilePulverizer.this.markDirty();
        }
    };
    
    
    public boolean canInteractWith(EntityPlayer playerIn) {
        return !isInvalid() && playerIn.getDistanceSq(pos.add(0.5D, 0.5D, 0.5D)) <= 64D;
    }

    @Override
    public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
        if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
            return true;
        }
        return super.hasCapability(capability, facing);
    }

    @Override
    public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
        if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
            return (T) itemStackHandler;
        }
        return super.getCapability(capability, facing);
    } 
    
@Override
public int getSizeInventory() {
	return this.pulverizerItemStacks.length;
}


@Override
public ItemStack getStackInSlot(int index) {
        return this.pulverizerItemStacks[index];
}


@Override
public ItemStack decrStackSize(int index, int count) {
        return ItemStackHelper.getAndSplit(this.pulverizerItemStacks, index, count);
}


@Override
public ItemStack removeStackFromSlot(int index) {
        return ItemStackHelper.getAndRemove(this.pulverizerItemStacks, index);
}


@Override
public void setInventorySlotContents(int index, ItemStack stack) {

        boolean flag = stack != null && stack.isItemEqual(this.pulverizerItemStacks[index]) && ItemStack.areItemStackTagsEqual(stack, this.pulverizerItemStacks[index]);
        this.pulverizerItemStacks[index] = stack;

        if (stack != null && stack.stackSize > this.getInventoryStackLimit())
        {
            stack.stackSize = this.getInventoryStackLimit();
        }

        if (index == 0 && !flag)
        {
            this.markDirty();
        }		
}


@Override
public int getInventoryStackLimit() {
	return 64;
}


@Override
public boolean isUseableByPlayer(EntityPlayer player) {
        return this.worldObj.getTileEntity(this.pos) != this ? false : player.getDistanceSq((double)this.pos.getX() + 0.5D, (double)this.pos.getY() + 0.5D, (double)this.pos.getZ() + 0.5D) <= 64.0D;
}


@Override
public void openInventory(EntityPlayer player) {

}
@Override
public void closeInventory(EntityPlayer player) {

}


@Override
public boolean isItemValidForSlot(int index, ItemStack stack) {
	return false;
}


@Override
public int getField(int id) {
	return 0;
}


@Override
public void setField(int id, int value) {

}


@Override
public int getFieldCount() {
	return 0;
}


@Override
public void clear() {
        for (int i = 0; i < this.pulverizerItemStacks.length; ++i)
        {
            this.pulverizerItemStacks[i] = null;
        }		
}


@Override
public String getName() {
	return ModBlocks.Pulverizer.getUnlocalizedName();
}


@Override
public boolean hasCustomName() {
	return false;
}


@Override
public int[] getSlotsForFace(EnumFacing side) {
	return null;
}


@Override
public boolean canInsertItem(int index, ItemStack itemStackIn, EnumFacing direction) {
        return this.isItemValidForSlot(index, itemStackIn);
}


@Override
public boolean canExtractItem(int index, ItemStack stack, EnumFacing direction) {
	return true;
}

@Override
public void markDirty() {

}

@Override
public ITextComponent getDisplayName() {
	return null;
}
    
    private boolean canPulverize()
    {
        if (this.pulverizerItemStacks[0] == null)
        {
            return false;
        }
        else
        {
            ItemStack itemstack = RecipePulverizer.instance().getPulverizingResult(this.pulverizerItemStacks[0]);
            if (itemstack == null) return false;
            if (this.pulverizerItemStacks[1] == null) return true;
            if (!this.pulverizerItemStacks[1].isItemEqual(itemstack)) return false;
            int result = pulverizerItemStacks[1].stackSize + itemstack.stackSize;
            return result <= getInventoryStackLimit() && result <= this.pulverizerItemStacks[2].getMaxStackSize();
        }
    }

    public void PulverizeItemItem()
    {
        if (this.canPulverize())
        {
            ItemStack itemstack = RecipePulverizer.instance().getPulverizingResult(this.pulverizerItemStacks[0]);

            if (this.pulverizerItemStacks[1] == null)
            {
                this.pulverizerItemStacks[1] = itemstack.copy();
            }
            else if (this.pulverizerItemStacks[1].getItem() == itemstack.getItem())
            {
                this.pulverizerItemStacks[1].stackSize += itemstack.stackSize;
            }

            --this.pulverizerItemStacks[0].stackSize;

            if (this.pulverizerItemStacks[0].stackSize <= 0)
            {
                this.pulverizerItemStacks[0] = null;
            }
        }
    }
}

 

Container Code:

package abused_master.JATMA.TE.Container;

import javax.annotation.Nullable;

import abused_master.JATMA.GUI.SlotPulverizerOutput;
import abused_master.JATMA.TE.TilePulverizer;
import abused_master.JATMA.TE.CraftingHandlers.PulverizerRecipes;
import abused_master.JATMA.TE.CraftingHandlers.RecipePulverizer;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.inventory.SlotFurnaceFuel;
import net.minecraft.inventory.SlotFurnaceOutput;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.FurnaceRecipes;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityFurnace;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.SlotItemHandler;

public class PulverizerContainer extends Container {

IInventory TilePulverizer;

public PulverizerContainer(InventoryPlayer playerInv, IInventory pulvInv, TileEntity tile) {
	super();
	TilePulverizer = pulvInv;
    
	addSlotToContainer(new Slot(TilePulverizer, 0, 56, 26));
	addSlotToContainer(new SlotFurnaceOutput(playerInv.player, TilePulverizer, 1, 116, 26));

	for (int y = 0; y < 3; ++y) {
        for (int x = 0; x < 9; ++x) {
            this.addSlotToContainer(new Slot(playerInv, x + y * 9 + 9, 8 + x * 18, 84 + y * 18));
        }
    }

    for (int x = 0; x < 9; ++x) {
        this.addSlotToContainer(new Slot(playerInv, x, 8 + x * 18, 142));
    }
}

@Nullable
    public ItemStack transferStackInSlot(EntityPlayer playerIn, int index)
    {
        ItemStack itemstack = null;
        Slot slot = (Slot)this.inventorySlots.get(index);

        if (slot != null && slot.getHasStack())
        {
            ItemStack itemstack1 = slot.getStack();
            itemstack = itemstack1.copy();

            if (index == 2)
            {
                if (!this.mergeItemStack(itemstack1, 3, 38, true))
                {
                    return null;
                }

                slot.onSlotChange(itemstack1, itemstack);
            }
            else if (index != 1 && index != 0)
            {
                if (RecipePulverizer.instance().getPulverizingResult(itemstack1) != null)
                {
                    if (!this.mergeItemStack(itemstack1, 0, 1, false))
                    {
                        return null;
                    }
                }
                else if (index >= 3 && index < 30)
                {
                    if (!this.mergeItemStack(itemstack1, 30, 38, false))
                    {
                        return null;
                    }
                }
                else if (index >= 30 && index < 38 && !this.mergeItemStack(itemstack1, 3, 30, false))
                {
                    return null;
                }
            }
            else if (!this.mergeItemStack(itemstack1, 3, 38, false))
            {
                return null;
            }
             
            if (itemstack1.stackSize == 0)
            {
                slot.putStack((ItemStack)null);
            }
            else
            {
                slot.onSlotChanged();
            }

            if (itemstack1.stackSize == itemstack.stackSize)
            {
                return null;
            }

            slot.onPickupFromSlot(playerIn, itemstack1);
        }

        return itemstack;
    }

@Override
public boolean canInteractWith(EntityPlayer playerIn) {
	return this.TilePulverizer.isUseableByPlayer(playerIn);
}
}

RecipeClass:

package abused_master.JATMA.TE.CraftingHandlers;

import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Nullable;

import com.google.common.collect.Maps;

import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;


public class RecipePulverizer {

private static final RecipePulverizer PULVERIZING_BASE = new RecipePulverizer();
    private final Map<ItemStack, ItemStack> pulvList = Maps.<ItemStack, ItemStack>newHashMap();
    private final Map<ItemStack, Float> experienceList = Maps.<ItemStack, Float>newHashMap();

    public static RecipePulverizer instance()
    {
        return PULVERIZING_BASE;
    }

    private RecipePulverizer()
    {
    	this.addPulverizingRecipeForBlock(Blocks.COBBLESTONE, new ItemStack(Blocks.GRAVEL), 1.0F);
    	this.addPulverizingRecipeForBlock(Blocks.GRAVEL, new ItemStack(Blocks.SAND), 1.0F);
    	this.addPulverizingRecipeForBlock(Blocks.STONE, new ItemStack(Blocks.GRAVEL), 1.0F);

    }

    public void addPulverizingRecipeForBlock(Block input, ItemStack stack, float experience)
    {
        this.addPulverizingRecipeForItem(Item.getItemFromBlock(input), stack, experience);
    }

    public void addPulverizingRecipeForItem(Item input, ItemStack stack, float experience)
    {
        this.addSmeltingRecipe(new ItemStack(input, 1, 32767), stack, experience);
    }
    
    public void addSmeltingRecipe(ItemStack input, ItemStack stack, float experience)
    {
        if (getPulverizingResult(input) != null) { net.minecraftforge.fml.common.FMLLog.info("Ignored pulverizing recipe with conflicting input: " + input + " = " + stack); return; }
        this.pulvList.put(input, stack);
        this.experienceList.put(stack, Float.valueOf(experience));
    }

    
    @Nullable
    public ItemStack getPulverizingResult(ItemStack stack)
    {
        for (Entry<ItemStack, ItemStack> entry : this.pulvList.entrySet())
        {
            if (this.compareItemStacks(stack, (ItemStack)entry.getKey()))
            {
                return (ItemStack)entry.getValue();
            }
        }

        return null;
    }

    private boolean compareItemStacks(ItemStack stack1, ItemStack stack2)
    {
        return stack2.getItem() == stack1.getItem() && (stack2.getMetadata() == 32767 || stack2.getMetadata() == stack1.getMetadata());
    }

    public Map<ItemStack, ItemStack> getSmeltingList()
    {
        return this.pulvList;
    }

    public float getPulverizingExperience(ItemStack stack)
    {
        float ret = stack.getItem().getSmeltingExperience(stack);
        if (ret != -1) return ret;

        for (Entry<ItemStack, Float> entry : this.experienceList.entrySet())
        {
            if (this.compareItemStacks(stack, (ItemStack)entry.getKey()))
            {
                return ((Float)entry.getValue()).floatValue();
            }
        }

        return 0.0F;
    }

}

Posted

I was asking for help not asking someone to go over every aspect of my code and criticize me on it.

 

The problem is, you've overlapped two different ways of handling containers (the old way and the new way) and the two don't talk to each other.  At all.  You just kind of float along hoping Magic will solve it, but it won't.

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.

Posted

I was asking for help not asking someone to go over every aspect of my code and criticize me on it.

 

The problem is, you've overlapped two different ways of handling containers (the old way and the new way) and the two don't talk to each other.  At all.  You just kind of float along hoping Magic will solve it, but it won't.

 

Ahh, did not know there was an old and a new way to do it.

I have fixed my problem, Ty diesieben07, had not realized what you said since i had not looked at it closely enough.

 

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.