Jump to content

[1.7.2]Custom furnace like item not working(probably a noob mistake sorry)


Recommended Posts

Posted

Hi

 

Somehow my Custom furnace like block isn't working i probably made a noob error or something :3 sorry

 

container

package molecularscience.electrolyzer;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ICrafting;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class ContainerElectrolyzer extends Container {

        protected TileEntityElectrolyzer tileEntity;
        private int progress;

        public ContainerElectrolyzer (InventoryPlayer inventoryPlayer, TileEntityElectrolyzer te){
                tileEntity = te;
                addSlotToContainer(new Slot(tileEntity, 0, 22, );
                addSlotToContainer(new Slot(tileEntity, 1, 76, );
                addSlotToContainer(new Slot(tileEntity, 2, 49, 50));
                addSlotToContainer(new Slot(tileEntity, 3, 128, 29));
                bindPlayerInventory(inventoryPlayer);
        }

        @Override
        public boolean canInteractWith(EntityPlayer player) {
                return tileEntity.isUseableByPlayer(player);
        }


        protected void bindPlayerInventory(InventoryPlayer inventoryPlayer) {
                for (int i = 0; i < 3; i++) {
                        for (int j = 0; j < 9; j++) {
                                addSlotToContainer(new Slot(inventoryPlayer, j + i * 9 + 9,
                                                8 + j * 18, 99 + i * 18));
                        }
                }

                for (int i = 0; i < 9; i++) {
                        addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 157));
                }
        }

        @Override
        public ItemStack transferStackInSlot(EntityPlayer player, int slot) {
                ItemStack stack = null;
                Slot slotObject = (Slot) inventorySlots.get(slot);
                if (slotObject != null && slotObject.getHasStack()) {
                        ItemStack stackInSlot = slotObject.getStack();
                        stack = stackInSlot.copy();
                        if (slot < 9) {
                                if (!this.mergeItemStack(stackInSlot, 0, 35, true)) {
                                        return null;
                                }
                        }
                        else if (!this.mergeItemStack(stackInSlot, 0, 9, false)) {
                                return null;
                        }

                        if (stackInSlot.stackSize == 0) {
                                slotObject.putStack(null);
                        } else {
                                slotObject.onSlotChanged();
                        }

                        if (stackInSlot.stackSize == stack.stackSize) {
                                return null;
                        }
                        slotObject.onPickupFromSlot(player, stackInSlot);
                }
                return stack;
        }
        
    	public void addCraftingToCrafters(ICrafting crafting){
    		super.addCraftingToCrafters(crafting);
    	    crafting.sendProgressBarUpdate(this, 0, this.tileEntity.progress);
    	}
    	
    	public void detectAndSendChanges(){
    		super.detectAndSendChanges();
    		for (int i = 0; i < this.crafters.size(); ++i){
    			ICrafting icrafting = (ICrafting)this.crafters.get(i);
    			if (this.progress != this.tileEntity.progress){
    				icrafting.sendProgressBarUpdate(this, 0, this.tileEntity.progress);
    	        }
    	    }
    	    this.progress = this.tileEntity.progress;
    	}
    	
    	@SideOnly(Side.CLIENT)
    	public void updateProgressBar(int par1, int par2){
    		if (par1 == 0){
    			this.tileEntity.progress = par2;
    	    }
    	}
    	
}

 

TileEntity

package molecularscience.electrolyzer;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;

public class TileEntityElectrolyzer extends TileEntity implements IInventory {

        private ItemStack[] inv;
    	private int time;
        public int progress;
        private int resultItem = 3;
        
        public TileEntityElectrolyzer(){
                inv = new ItemStack[4];
        }
        
        @Override
        public int getSizeInventory() {
                return inv.length;
        }

        @Override
        public ItemStack getStackInSlot(int slot) {
                return inv[slot];
        }
        
        @Override
        public void setInventorySlotContents(int slot, ItemStack stack) {
                inv[slot] = stack;
                if (stack != null && stack.stackSize > getInventoryStackLimit()) {
                        stack.stackSize = getInventoryStackLimit();
                }               
        }

        @Override
        public ItemStack decrStackSize(int slot, int amt) {
                ItemStack stack = getStackInSlot(slot);
                if (stack != null) {
                        if (stack.stackSize <= amt) {
                                setInventorySlotContents(slot, null);
                        } else {
                                stack = stack.splitStack(amt);
                                if (stack.stackSize == 0) {
                                        setInventorySlotContents(slot, null);
                                }
                        }
                }
                return stack;
        }
        
        @Override
    	public void updateEntity(){
    	   if(!worldObj.isRemote){
    		   int item = getAvailableSlot();
    		   if(inv[item] != null){
    			   Item type = inv[item].getItem();
    			   ItemStack result = ElectrolyzerResults.getResult(item, type);
    			   time = ElectrolyzerResults.getMaxTime(item, type);

    			   if(result != null){
    				   if(progress > time){
    						if (inv[resultItem] == null){
    							inv[resultItem] = result.copy();
    			            }
    						else if (inv[resultItem].isItemEqual(result)){
    							if(inv[resultItem].stackSize + result.stackSize <= getInventoryStackLimit()){
    								inv[resultItem].stackSize += result.stackSize;
    							}
    						}
    						if(inv[item] != null){
    							if(!((inv[item].stackSize - ElectrolyzerResults.getReduceItem(item, type)) < 0)){
    								inv[item].stackSize -= ElectrolyzerResults.getReduceItem(item, type);
    							}
    							if(inv[item].stackSize <= 0){
    								inv[item] = null;
    							}
    						}
    						progress = 0;
    				   }
    				   if(inv[resultItem] != null){
    						   if(inv[resultItem].isItemEqual(result)){
    							   if(inv[resultItem].stackSize < getInventoryStackLimit() || inv[resultItem].stackSize < result.getItem().getItemStackLimit()){
    								   if(inv[item] != null){
    									   if(!((inv[item].stackSize - ElectrolyzerResults.getReduceItem(item, type)) < 0)){
    										   if(inv[resultItem].stackSize + result.stackSize <= getInventoryStackLimit()){
    											   progress++;  
    										   }
    										   else{
    											   progress = 0;
    										   }
    									   }
    									   else{
    										   progress = 0;
    									   }
    								   }
    								   else{
    									   progress = 0;
    								   }  
    							   }
    							   else{
    								   progress = 0;
    							   }
    						   }
    						   else{
    							   progress = 0;
    						   }
    					   }
    					   else{
    						   progress = 0;
    					   }
    				   }
    				   else{
    					   if(inv[item] != null){
    						   if(!((inv[item].stackSize - ElectrolyzerResults.getReduceItem(item, type)) < 0)){
    								   progress++;  
    						   }
    						   else{
    							   progress = 0;
    						   }
    					   }
    					   else{
    						   progress = 0;
    					   }
    				   }
    			   }
    			   else{
    				   progress = 0;
    			   }
    		   }
    	   }

        @Override
        public ItemStack getStackInSlotOnClosing(int slot) {
                ItemStack stack = getStackInSlot(slot);
                if (stack != null) {
                        setInventorySlotContents(slot, null);
                }
                return stack;
        }
        
        @Override
        public int getInventoryStackLimit() {
                return 64;
        }

        @Override
        public boolean isUseableByPlayer(EntityPlayer player) {
                return worldObj.getTileEntity(xCoord, yCoord, zCoord) == this &&
                player.getDistanceSq(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5) < 64;
        }

        public void openChest() {}

        public void closeChest() {}
        
        @Override
        public void readFromNBT(NBTTagCompound tagCompound) {
                super.readFromNBT(tagCompound);
                progress = tagCompound.getShort("progressTime");
                
        }

        @Override
        public void writeToNBT(NBTTagCompound tagCompound) {
                super.writeToNBT(tagCompound);
                tagCompound.setShort("progressTime", (short)progress);
                NBTTagList itemList = new NBTTagList();
                for (int i = 0; i < inv.length; i++) {
                        ItemStack stack = inv[i];
                        if (stack != null) {
                                NBTTagCompound tag = new NBTTagCompound();
                                tag.setByte("Slot", (byte) i);
                                stack.writeToNBT(tag);
                                itemList.appendTag(tag);
                        }
                }
                tagCompound.setTag("Inventory", itemList);
        }

			@Override
			public String getInventoryName() {
				return "molecularscience.tileentityelectrolyzer";
			}

		    @SideOnly(Side.CLIENT)
		    public int getCraftingProgressScaled(int par1){
		        //return this.progress * par1 / ElectrolyzerResults.getMaxTime(0, 0);
		    	return 0;
		    }
		    
			public int getAvailableSlot(){
				   for(int i = 0; i < inv.length - 1; i++){
					   if(inv[i] != null){
						   return i;
					   }
				   }
				   return 0;
				}
			public boolean isBusy(){
				return progress >= 0;
			}

			@Override
			public boolean hasCustomInventoryName() {
				// TODO Auto-generated method stub
				return false;
			}

			@Override
			public void openInventory() {
				// TODO Auto-generated method stub

			}

			@Override
			public void closeInventory() {
				// TODO Auto-generated method stub

			}

			@Override
			public boolean isItemValidForSlot(int var1, ItemStack var2) {
				// TODO Auto-generated method stub
				return false;
			}
}

 

Gui

package molecularscience.electrolyzer;

import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.StatCollector;

import org.lwjgl.opengl.GL11;

public class GuiElectrolyzer extends GuiContainer {

private static final ResourceLocation Guitexture = new ResourceLocation("molecularscience","textures/gui/container/guielectrolyzer.png");

    public GuiElectrolyzer (InventoryPlayer inventoryPlayer,
                    TileEntityElectrolyzer tileEntity) {
            super(new ContainerElectrolyzer(inventoryPlayer, tileEntity));
    }

    @Override
    protected void drawGuiContainerForegroundLayer(int param1, int param2) {

    }

    @Override
    protected void drawGuiContainerBackgroundLayer(float par1, int par2,
                    int par3) {
    		this.mc.getTextureManager().bindTexture(Guitexture);
            int x = (width - xSize) / 2;
            int y = (height - (ySize+27)) / 2;
            this.drawTexturedModalRect(x, y, 0, 0, xSize, ySize+27);
    }

}

 

results

package molecularscience.electrolyzer;

import java.util.HashMap;
import java.util.Map;

import molecularscience.moditems.BlocksItems;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;

public class ElectrolyzerResults {

private static Map<Integer, Object[]> results = new HashMap<Integer, Object[]>();

public static void initResult(){
	addResult(0, 0, BlocksItems.Emptyjar, 1, BlocksItems.Sample, 1);
}
public static ItemStack getResult(int slot, Item type){
	for(int i = 0; i < results.size(); i++){
		if((int)results.get(i)[0] == slot){
			if(results.get(i)[1] == type){
				return new ItemStack((Item) results.get(i)[4], 1);
			}
		}
	}
	return null;
}

public static int getMaxTime(int slot, Item type){
	return 50;
}

public static int getReduceItem(int slot, Item type){
	for(int i = 0; i < results.size(); i++){
		if((int)results.get(i)[0] == slot){
			if(results.get(i)[1] == type){
				return (int)results.get(i)[2];
			}
		}
	}
	return 1;
}

private static void addResult(int id, int slot, Item type, int ammount, Item result, int size){
	results.put(id, new Object[]{slot, type, ammount, result, size});
}
}

 

slot

package molecularscience.electrolyzer;

import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;

public class SlotElectrolyzer extends Slot {

private TileEntityElectrolyzer teSmithingAnvil;

public SlotElectrolyzer(IInventory inventory, int id, int x, int y) {
	super(inventory, id, x, y);
	this.teSmithingAnvil = (TileEntityElectrolyzer) inventory;
}

@Override
public boolean isItemValid(ItemStack itemstack) {
	return teSmithingAnvil.isItemValidForSlot(slotNumber, itemstack);
}
}

 

Thanks for your help :D

I'm always happy to help others!

 

Java is not my main programming language but I have alot experience in PHP/HTML/JS/CSS3/C#/SQL

Posted

nothing it's supposed to return a item when i put it in slot 0 but nope..

 

I'm always happy to help others!

 

Java is not my main programming language but I have alot experience in PHP/HTML/JS/CSS3/C#/SQL

Posted

I was just playing with TransferStackInSlot() and I noticed that when it is successful, it must return an empty stack, not the original stack. The return stack is used to indicate that not all of the items could be transferred. If it has items left, there are put back into the original slot. This is for those people who shift-click and do not have enough room to hole all the crafts.

I noticed that your return either null or the original stack, I don't think it will work that way.

Posted

Would be cool, if u post what you exactly fixed, maybe there are people who have the same problem. so this could be a bit helpful after all. ;)

*********************************

**  Always remember you are unique,  **

**  just like everyone else                   **     

**********************************

Was my post helpful? Say thanks and

press the thank you button, on my post!!

God, damn it!

Posted

It where some broken if fuctions, basic java people can see my github.

I'm always happy to help others!

 

Java is not my main programming language but I have alot experience in PHP/HTML/JS/CSS3/C#/SQL

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.