Jump to content

[1.12.2] Refreshing an old code - Multi Output Furnace for a single input [Solved]


sunsigne

Recommended Posts

Hello :)

 

i'm working on a multi-output furnace and i finally found a topic that could help me, but it is very old and i'm refreshing the code. Can anyone help me on a little part please ?

 

here are the lines i adapted (from recipes)

Spoiler

    public void addCentrifugeRecipe(ItemStack input, ItemStack item1, ItemStack item2) 
    {
        this.smeltingList.put(input, twoItem(item1, item2));
    }
      
    
    public void addSolidifying2(ItemStack input, ItemStack item1, ItemStack item2)
    {
            solidifyingList2.put(input, twoItem(item1, item2)/*add the ItemStack array into the recipe list*/);
    }

        
    private ItemStack [] twoItem(ItemStack i1 , ItemStack i2)
    {
            return new ItemStack[] {i1, i2};
    }
        

 

//HERE IS THE IMPORTANT PART, my new line and the old one as "commentary"

 

    public ItemStack [] getSolidifyingResult2Out(ItemStack index)//returns an ItemStack array so it can hold 2 items
    {
            return (ItemStack [])solidifyingList2.get(index);
    }

//    public ItemStack [] getSolidifyingResult2Out(int i)//returns an ItemStack array so it can hold 2 items
//    {
//            return (ItemStack [])solidifyingList2.get(Integer.valueOf(i));
//    }
    

 

And here is the part i can't figure out how i am supposed to adapt (from the tileentity)

Spoiler

ItemStack [] itemstack = RFurnaceRecipes.smelting().getSolidifyingResult2Out(furnaceItemStacks[0].getItem().itemID);

The solution is probably simple, but it's been two hours i can't figure out how i am supposed to do ^^

I stast like "ItemStack [] itemstack = CentrifugeRecipes.getInstance().getSolidifyingResult2Out((ItemStack)this.inventory.get(0)......." but then i don't know what to do.

 

 

if it can help you thinking, here are some lines after that part where "itemstack []" is used :

Spoiler

 if(!furnaceItemStacks[2].isItemEqual(itemstack[0]) || !furnaceItemStacks[3].isItemEqual(itemstack[1]))  //checks if the items in the output slots are equal to the ones that will be added
            {
                    return false;

 

 

 

Any advice is welcome :)

Edited by sunsigne
title change
Link to comment
Share on other sites

Not enough code here for us to solve your problem. Post the entire class at a minimum.

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

Ok, here is it :

 

reciepes code :

Spoiler

package sunsigne.MyMod.container;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Maps;
import com.google.common.collect.Table;

import net.minecraft.block.Block;
import net.minecraft.block.BlockStoneBrick;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.EnumDyeColor;
import net.minecraft.item.Item;
import net.minecraft.item.ItemFishFood;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.FurnaceRecipes;
import sunsigne.MyMod.init.ModBlocks;
import sunsigne.MyMod.init.ModItems;

public class CentrifugeRecipes {


    
    private static final CentrifugeRecipes INSTANCE = new CentrifugeRecipes();
    private Map smeltingList = new HashMap();
    private HashMap<List<Integer>, ItemStack> metaSmeltingList = new HashMap<List<Integer>, ItemStack>();
    private Map solidifyingList2 = new HashMap();

    
    public static CentrifugeRecipes getInstance()
    {
        return INSTANCE;
    }
    
    private boolean compareItemStacks(ItemStack stack1, ItemStack stack2)
    {
        return stack2.getItem() == stack1.getItem() && (stack2.getMetadata() == 32767 || stack2.getMetadata() == stack1.getMetadata());
    }
    
    public Map<List<Integer>, ItemStack> getMetaSmeltingList()
    {
        return metaSmeltingList;
    }
    
    
    public Map getSmeltingList()
    {
        return this.smeltingList;
    }
    
    
    
    public void addCentrifugeRecipe(ItemStack input, ItemStack item1, ItemStack item2) 
    {
        this.smeltingList.put(input, twoItem(item1, item2));
    }
    
       
    
    public void addSolidifying2(ItemStack input, ItemStack item1, ItemStack item2)
    {
            solidifyingList2.put(input, twoItem(item1, item2)/*add the ItemStack array into the recipe list*/);
    }

        
    private ItemStack [] twoItem(ItemStack i1 , ItemStack i2)
    {
            return new ItemStack[] {i1, i2};
    }
        

    public ItemStack [] getSolidifyingResult2Out(ItemStack index)//returns an ItemStack array so it can hold 2 items
    {
            return (ItemStack [])solidifyingList2.get(index);
    }

//    public ItemStack [] getSolidifyingResult2Out(int i)//returns an ItemStack array so it can hold 2 items
//    {
//            return (ItemStack [])solidifyingList2.get(Integer.valueOf(i));
//    }
    
    

    public void addSmelting(int item, int metadata, ItemStack itemstack, float experience)
    {
        metaSmeltingList.put(Arrays.asList(item, metadata), itemstack);

    }


    public ItemStack getCentrifugeResult(ItemStack item) 
    {
        if (item == null)
        {
            return null;
        }
        ItemStack ret = (ItemStack)metaSmeltingList.get(Arrays.asList(item, item.getItemDamage()));
        if (ret != null) 
        {
            return ret;
        }
        return (ItemStack)smeltingList.get(item);
    }

    
        
    
    
    private CentrifugeRecipes() 
    {
        

        
        
                
        
        addCentrifugeRecipe(new ItemStack(Blocks.OBSIDIAN), new ItemStack(Blocks.GOLD_BLOCK), new ItemStack(Blocks.DIAMOND_BLOCK));
//        addCentrifugeRecipe(new ItemStack(Blocks.DIAMOND_BLOCK), new ItemStack(Blocks.GOLD_BLOCK), new ItemStack(Blocks.IRON_BLOCK));
//        addCentrifugeRecipe(new ItemStack(Blocks.IRON_BLOCK), new ItemStack(Blocks.DIAMOND_BLOCK), new ItemStack(Blocks.GOLD_BLOCK));
        
    }

 

and tilte entitiy code :

Spoiler

package sunsigne.MyMod.tileentity;

import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ItemStackHelper;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ITickable;
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import sunsigne.MyMod.Reference;
import sunsigne.MyMod.blocks.CentrifugeBlock;
import sunsigne.MyMod.container.CentrifugeRecipes;


public class CentrifugeTileEntity extends TileEntity implements ITickable, IInventory {

    private NonNullList<ItemStack> inventory = NonNullList.<ItemStack>withSize(10, ItemStack.EMPTY);
    private String customName;

    private int cookTime;
    private int totalCookTime;
    
    @Override
    public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newSate)
    {
        return true;
    }
    
    public int getCookTime(ItemStack input) 
    {
        return 40;

    }
    
        
    @Override
    public String getName() 
    {
        return this.hasCustomName() ? this.customName : "container.centrifuge_block";
    }

    @Override
    public boolean hasCustomName() 
    {
        return this.customName != null && !this.customName.isEmpty();
    }
    
    public void setCustomName(String customName) 
    {
        this.customName = customName;
    }
    
    @Override
    public ITextComponent getDisplayName() 
    {
        return this.hasCustomName() ? new TextComponentString(this.getName()) : new TextComponentTranslation(this.getName());
    }

    @Override
    public int getSizeInventory() 
    {
        return this.inventory.size();
    }

    @Override
    public boolean isEmpty() 
    {
        for(ItemStack stack : this.inventory)
        {
            if(!stack.isEmpty()) return false;
        }
        return true;
    }

    @Override
    public ItemStack getStackInSlot(int index)
    {
        return (ItemStack)this.inventory.get(index);
    }

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

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

    @Override
    public void setInventorySlotContents(int index, ItemStack stack) 
    {
        ItemStack itemstack = (ItemStack)this.inventory.get(index);
        boolean flag = !stack.isEmpty() && stack.isItemEqual(itemstack) && ItemStack.areItemStackTagsEqual(stack, itemstack);
        this.inventory.set(index, stack);
        
        if(stack.getCount() > this.getInventoryStackLimit()) stack.setCount(this.getInventoryStackLimit());
        if(index == 0 && !flag)
        {
            this.totalCookTime = this.getCookTime(stack);
            this.cookTime = 0;
            this.markDirty();
        }
    }
    
    @Override
    public void readFromNBT(NBTTagCompound compound)
    {
        super.readFromNBT(compound);
        this.inventory = NonNullList.<ItemStack>withSize(this.getSizeInventory(), ItemStack.EMPTY);
        ItemStackHelper.loadAllItems(compound, this.inventory);
        this.cookTime = compound.getInteger("CookTime");
        this.totalCookTime = compound.getInteger("CookTimeTotal");
        
        if(compound.hasKey("CustomName", 8)) this.setCustomName(compound.getString("CustomName"));
    }
    
    @Override
    public NBTTagCompound writeToNBT(NBTTagCompound compound) 
    {
        compound.setInteger("CookTime", (short)this.cookTime);
        compound.setInteger("CookTimeTotal", (short)this.totalCookTime);
        ItemStackHelper.saveAllItems(compound, this.inventory);
        
        if(this.hasCustomName()) compound.setString("CustomName", this.customName);
        return super.writeToNBT(compound);
    }

    @Override
    public int getInventoryStackLimit() 
    {
        return 64;
    }
    
    public boolean isCooking() 
    {
        return this.cookTime > 0;
    }
    
    @SideOnly(Side.CLIENT)
    public static boolean isCooking(IInventory inventory) 
    {
        return inventory.getField(0) > 0;
    }
    
    public void update() 
    {
        
        boolean flag1 = false;
                
        if(!this.world.isRemote) 
        {
                        
            if(!((ItemStack)this.inventory.get(0)).isEmpty()) 
            {
                if(this.canSmelt()) 
                
                {
                    ++this.cookTime;
                    
                    if(this.cookTime == this.totalCookTime) 
                    {
                        this.cookTime = 0;
                        this.totalCookTime = this.getCookTime((ItemStack)this.inventory.get(0));
                        this.smeltItem();
                        flag1 = true;
                    }
                } 
                else this.cookTime = 0;
            } 
            else if(this.cookTime > 0) 
            {
                this.cookTime = MathHelper.clamp(this.cookTime - 2, 0, this.totalCookTime);
            }
            
        } 
        this.markDirty();
    }
    
    
    
    @Override
    public boolean isUsableByPlayer(EntityPlayer player) 
    {
        return this.world.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) 
    {
        if(index != 0) return false;
        else return true;
            
    }
    
    public String getGuiID() 
    {
        return Reference.MODID + ":centrifuge_block";
    }

    @Override
    public int getField(int id) 
    {
        switch(id) 
        {
        case 1:
            return this.cookTime;
        case 2:
            return this.totalCookTime;
        default:
            return 0;
        }
    }

    @Override
    public void setField(int id, int value) 
    {
        switch(id) 
        {
        case 1:
            this.cookTime = value;
            break;
        case 2:
            this.totalCookTime = value;
        }
    }

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

    @Override
    public void clear() 
    {
        this.inventory.clear();
    }

    
    
    
    
    // note sure about that
    
    private boolean canSmelt() 
    {
        if(((ItemStack)this.inventory.get(0)).isEmpty()) return false;
        else 
        {
            ItemStack result = CentrifugeRecipes.getInstance().getCentrifugeResult((ItemStack)this.inventory.get(0));    
            if(result.isEmpty()) return false;
            else
            

            {
                
                ItemStack output1 = (ItemStack)this.inventory.get(1);
                ItemStack output2 = (ItemStack)this.inventory.get(2);
                
                            
                
                if(output1.isEmpty() && output2.isEmpty()) return true;
                if(!output1.isItemEqual(result)) return false;
                int res = output1.getCount() + result.getCount();
                return res <= getInventoryStackLimit() && res <= output1.getMaxStackSize();
            }
        }
    }
    
    // note sure about that
    
    public void smeltItem() 
    {
        if(this.canSmelt()) 
        {
            ItemStack input = (ItemStack)this.inventory.get(0);
            ItemStack result = CentrifugeRecipes.getInstance().getCentrifugeResult(input);
            ItemStack output1 = (ItemStack)this.inventory.get(1);
            
            
            if(output1.isEmpty()) this.inventory.set(1, result.copy());
            else if(output1.getItem() == result.getItem()) output1.grow(result.getCount());
            
            
            
            input.shrink(1);
        }
    }
        
    
    
    //to adapt !!
    
    
    private boolean canSolidify2()
    {
        
            if(this.furnaceItemStacks[0] == null)
    //correct translation of this line is : 
            if(((ItemStack)this.inventory.get(0)).isEmpty())
            {
                    return false;
            }    
            
            ItemStack [] itemstack = RFurnaceRecipes.smelting().getSolidifyingResult2Out(furnaceItemStacks[0].getItem().itemID);
     //start of a correct translation       
            ItemStack [] itemstack = CentrifugeRecipes.getInstance().getSolidifyingResult2Out(???);
            if(itemstack == null)
           {
                    return false;
            }
           if(furnaceItemStacks[2] == null && furnaceItemStacks[3] == null)
            {
                    return true;
            }
            if(!furnaceItemStacks[2].isItemEqual(itemstack[0]) || !furnaceItemStacks[3].isItemEqual(itemstack[1]))//checks if the items in the output slots are equal to the ones that will be added
            {
                    return false;
            }
            if(furnaceItemStacks[2].stackSize < getInventoryStackLimit() && furnaceItemStacks[2].stackSize < furnaceItemStacks[2].getMaxStackSize() && furnaceItemStacks[3].stackSize < getInventoryStackLimit() && furnaceItemStacks[3].stackSize < furnaceItemStacks[3].getMaxStackSize())//checks the stack limit of both output slots
            {
                  return true;
            }
            return furnaceItemStacks[2].stackSize < itemstack[0].getMaxStackSize() && furnaceItemStacks[3].stackSize < itemstack[1].getMaxStackSize();
    }

    public void solidifyItem2()
   {
            if(!canSolidify2())
            {
                    return;
            }
            ItemStack [] itemstack = RFurnaceRecipes.smelting().getSolidifyingResult5Out(furnaceItemStacks[0].getItem().itemID);
           if(furnaceItemStacks[2] == null)
            {
                    furnaceItemStacks[2] = itemstack[0].copy();
            } else
           if(furnaceItemStacks[2].itemID == itemstack[0].itemID)
            {
                    furnaceItemStacks[2].stackSize += itemstack[0].stackSize;
            }
            if(furnaceItemStacks[3] == null)//this is so that the game knows to add the solidified item in the second output slot
            {
                    furnaceItemStacks[3] = itemstack[1].copy();
            } else
            if(furnaceItemStacks[3].itemID == itemstack[1].itemID)//so is this
            {
                    furnaceItemStacks[3].stackSize += itemstack[1].stackSize;
            }
            if(furnaceItemStacks[0].getItem().hasContainerItem())
            {
                    furnaceItemStacks[0] = new ItemStack(furnaceItemStacks[0].getItem().getContainerItem());
            } else
            {
                    furnaceItemStacks[0].stackSize--;
            }
            if(furnaceItemStacks[0].stackSize <= 0)
            {
                    furnaceItemStacks[0] = null;
            }
    }
  
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
   }

 

 

Except all the part after "canSmelt()" where i'm not sure, the code is working. I tried on another block.

(I know i should used IItemHandler, but as this system work for my first furnace while my other "iitemhadler" furnace refuse to cook anything (and i don't know why), i continue this method a little bit before fully adatping my entire code once i solved this problem.).

 

 

 

Ho, and thank again for helping us:) each time I read a topic I saw your name with good advices ;) it's pleasant to see your passion 

 

Link to comment
Share on other sites

12 hours ago, sunsigne said:

sunsigne.MyMod.container;

Required Java conventions

What: Lowercase package naming

Why: Some file systems (windows) considering “iTeMs” and “items” to be the same, but every other one considering them to be different.

Consequences: Not doing this _may_ never cause any issues (and very likely won’t in your dev environment), but on other operating systems with different case-sensitivities from yours this can cause massive problems.

How: Name your packages all in lowercase

 

What: Packaging that reflects your web presence

Why: Avoid name collisions - your web presence is unique so it is commonly used.

Consequences: Errors loading classes with other mods installed, usually happens when 2 mods never changed their packaging from the default in the MDK.

How: If you have an illegal character in your web presence, the convention is to replace it with “_”. If you don’t have a website, you can use your version-control repository as your web presence, e.g. “com.github.username”. If you don’t use version control, start using it! In the meantime you can use the packaging “mod.username.modid”.

12 hours ago, sunsigne said:

implements IInventory

Do not use IInventory. Use the capability API with IItemHandler. Why? its legacy vanilla code, and bad at that. it also prevents you from having hoppers hopper-like machines, pipes etc. interface with your TileEntity, and breaks compatibility with other mods

Edited by Cadiboo

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

4 hours ago, Cadiboo said:

 

Do not use IInventory. Use the capability API with IItemHandler. Why? its legacy vanilla code, and bad at that. it also prevents you from having hoppers etc. interface with your TileEntity, and breaks compatibility with other mods

Hoppers work just fine. It's mod added machines that won't interface properly.

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

@Cadiboo

You got some really good advices overall ! I'll take note of all that. But actually i'm just creating a mod for me and my girlfriend so i don't really need the second one, but i'll use the lowercase one ;)

 

And about IItemhandler, i'm okay with using it but i can't as long as this topic will remain unsolved. I'm working on the adaptation and post the answer when i'll find it out, but it's not my priority yet.

 

But still thank for your good advices

Link to comment
Share on other sites

Here is it ! It's solved ! Now i have my multi-output furnace :)

 

for curious people, here is the codes (be carefull, it is fuel-free)

 

the container :

Spoiler

package sunsigne.MyMod.container;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IContainerListener;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
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;
import sunsigne.MyMod.container.recipes.CentrifugeRecipes;
import sunsigne.MyMod.container.slots.CentrifugeOutputSlot;
import sunsigne.MyMod.tileentity.CentrifugeTileEntity;
import sunsigne.MyMod.tileentity.EnchantedCraftingTableTileEntity;

public class CentrifugeContainer extends Container {

    private final CentrifugeTileEntity tileentity;
    private int cookTime, burnTime, currentBurnTime;
    
    public CentrifugeContainer(InventoryPlayer player, CentrifugeTileEntity tileentity) 
    {
        this.tileentity = tileentity;
        IItemHandler itemHandler = tileentity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
        
        this.addSlotToContainer(new SlotItemHandler(itemHandler, 0, 66, 36));
        this.addSlotToContainer(new CentrifugeOutputSlot(player.player, itemHandler, 1, 138, 18));
        this.addSlotToContainer(new CentrifugeOutputSlot(player.player, itemHandler, 2, 156, 18));
        this.addSlotToContainer(new CentrifugeOutputSlot(player.player, itemHandler, 3, 174, 18));
        this.addSlotToContainer(new CentrifugeOutputSlot(player.player, itemHandler, 4, 138, 36));
        this.addSlotToContainer(new CentrifugeOutputSlot(player.player, itemHandler, 5, 156, 36));
        this.addSlotToContainer(new CentrifugeOutputSlot(player.player, itemHandler, 6, 174, 36));
        this.addSlotToContainer(new CentrifugeOutputSlot(player.player, itemHandler, 7, 138, 54));
        this.addSlotToContainer(new CentrifugeOutputSlot(player.player, itemHandler, 8, 156, 54));
        this.addSlotToContainer(new CentrifugeOutputSlot(player.player, itemHandler, 9, 174, 54));
        
        
        for(int y = 0; y < 3; y++)
        {
            for(int x = 0; x < 9; x++)
            {
                this.addSlotToContainer(new Slot(player, x + y*9 + 9, 48 + x*18, 84 + y*18));
            }
        }
        
        for(int x = 0; x < 9; x++)
        {
            this.addSlotToContainer(new Slot(player, x, 48 + x * 18, 142));
        }
    }
    
    
    
    @Override
    public void detectAndSendChanges() 
    {
        super.detectAndSendChanges();
        
        for(int i = 0; i < this.listeners.size(); ++i) 
        {
            IContainerListener listener = (IContainerListener)this.listeners.get(i);
            
            if(this.cookTime != this.tileentity.getField(2)) listener.sendWindowProperty(this, 2, this.tileentity.getField(2));
            if(this.burnTime != this.tileentity.getField(0)) listener.sendWindowProperty(this, 0, this.tileentity.getField(0));
            if(this.currentBurnTime != this.tileentity.getField(1)) listener.sendWindowProperty(this, 1, this.tileentity.getField(1));
            
        }
        
        this.cookTime = this.tileentity.getField(2);
        this.burnTime = this.tileentity.getField(0);
        this.currentBurnTime = this.tileentity.getField(1);
        
    }
    
    @Override
    @SideOnly(Side.CLIENT)
    public void updateProgressBar(int id, int data) 
    {
        this.tileentity.setField(id, data);
    }
    
    @Override
    public boolean canInteractWith(EntityPlayer playerIn) 
    {
        return this.tileentity.isUsablebyplayer(playerIn);
    }
    
    @Override
    public ItemStack transferStackInSlot(EntityPlayer playerIn, int index) 
    {
        ItemStack stack = ItemStack.EMPTY;
        Slot slot = (Slot)this.inventorySlots.get(index);
        
        if(slot != null && slot.getHasStack()) 
        {
            ItemStack stack1 = slot.getStack();
            stack = stack1.copy();
            
            if(index == 1 || index == 2 || index == 3 || index == 4 || index == 5 || index == 6 || index == 7 || index == 8 || index == 9) 
            {
                if(!this.mergeItemStack(stack1, 10, 40, true)) return ItemStack.EMPTY;
                slot.onSlotChange(stack1, stack);
            }
            else if(index != 0) 
            {        
                
                ItemStack [] result = CentrifugeRecipes.getInstance().getCentrifugeResult(stack1);    
                
                if(result[0].isEmpty() || result[1].isEmpty())
                {
                    if(!this.mergeItemStack(stack1, 1, 9, false)) 
                    {
                        return ItemStack.EMPTY;
                    }
                    else if(index >= 10 && index < 31)
                    {
                        if(!this.mergeItemStack(stack1, 31, 40, false)) return ItemStack.EMPTY;
                    }
                    else if(index >= 31 && index < 40 && !this.mergeItemStack(stack1, 7, 31, false))
                    {
                        return ItemStack.EMPTY;
                    }
                }
            } 
            else if(!this.mergeItemStack(stack1, 10, 40, false)) 
            {
                return ItemStack.EMPTY;
            }
            if(stack1.isEmpty())
            {
                slot.putStack(ItemStack.EMPTY);
            }
            else
            {
                slot.onSlotChanged();

            }
            if(stack1.getCount() == stack.getCount()) return ItemStack.EMPTY;
            slot.onTake(playerIn, stack1);
        }
        return stack;
    }
}

 

the tileentiity :

Spoiler

package sunsigne.MyMod.tileentity;

import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.inventory.ItemStackHelper;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.ItemStackHandler;
import sunsigne.MyMod.Reference;
import sunsigne.MyMod.blocks.CentrifugeBlock;
import sunsigne.MyMod.container.recipes.CentrifugeRecipes;

public class CentrifugeTileEntity extends TileEntity implements ITickable {

    public static final int SIZE = 10;
    

    private int cookTime;
        
    
    private ItemStackHandler itemStackHandler = new ItemStackHandler(SIZE)
    {
        @Override
        protected void onContentsChanged(int slot) {
            
            CentrifugeTileEntity.this.markDirty();
        }
    };
    

    
    
    @Override
    public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newSate)
    {
        return true;
    }


    
    @SideOnly(Side.CLIENT)
      public static boolean isCooking(CentrifugeTileEntity te) {

        return te.getField(0) > 0;
    }

    public boolean isCooking() 
    {
        return this.cookTime > 0;
    }
    
    
    
    public void update() 
    {
        boolean flag = this.isCooking();
        boolean flag1 = false;

        if(!this.world.isRemote) 
        {
            if(!(this.itemStackHandler.getStackInSlot(0)).isEmpty())
            {
                if(this.canSmelt()) 
                {
                    ++this.cookTime;
                    
//the 20 here is the total cook time i want. The cook lasts 20 ticks then the item is cooked
                    
                    if(this.cookTime == 20) 
                    {
                        this.cookTime = 0;
                        this.smeltItem();
                        flag1 = true;
                    }
                } 
                else this.cookTime = 0;
            } 
            else if(this.cookTime > 0) 
                
//same here : 20 is for 20 ticks of cooking                
            {
                this.cookTime = MathHelper.clamp(this.cookTime - 2, 0, 20);
            }
            if(flag != this.isCooking()) 
            {
                flag1 = true;
                CentrifugeBlock.setState(this.isCooking(), this.world, this.pos);
            }
        } 
        if(flag1) this.markDirty();
    }
    
    
    
    
    
    public int getField(int id)
    {
        switch(id)
        {
       case 0:
            return this.cookTime;
        default:
            return 0;
        }
    }
        public void setField(int id, int value)
    {
        switch(id)
        {
        case 0:
            this.cookTime = value;
            break;
        }
    }


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

    this.cookTime = compound.getInteger("CookTime");
    
     if (compound.hasKey("items")) {itemStackHandler.deserializeNBT((NBTTagCompound) compound.getTag("items"));}
    
    }

@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) 
{
    super.writeToNBT(compound);
    
    compound.setInteger("CookTime", this.cookTime);

    
    compound.setTag("items", itemStackHandler.serializeNBT());
     return compound;
}

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

    private int getInventoryStackLimit() {
        return 64;
    
    }    
    
    public boolean isUsablebyplayer(EntityPlayer playerIn) {
        return !isInvalid() && playerIn.getDistanceSq(pos.add(0.5D, 0.5D, 0.5D)) <= 64D;
    }

    
    
    
    
        
    
 // note sure about that
    
         public void smeltItem() 
         {
             if(this.canSmelt()) 
             {
                 ItemStack input = this.itemStackHandler.getStackInSlot(0);
                 ItemStack [] result = CentrifugeRecipes.getInstance().getCentrifugeResult(input);
                 ItemStack output1 = this.itemStackHandler.getStackInSlot(1);
                 ItemStack output2 = this.itemStackHandler.getStackInSlot(2);
                 
                 {
                     if(output1.isEmpty()) this.itemStackHandler.setStackInSlot(1, result[0].copy());
                     if(output2.isEmpty()) this.itemStackHandler.setStackInSlot(2, result[1].copy());
                     
                     else {
                     
                         if(output1.getItem() == result[0].getItem()) output1.grow(result[0].getCount());
                         if(output2.getItem() == result[1].getItem()) output2.grow(result[1].getCount());
                     
                          }
                 }
                 
                                  
                 input.shrink(1);
             }
         }
    
    
    
      
    
    
    
    private boolean canSmelt() 
    {
            
            
        
        
        if((this.itemStackHandler.getStackInSlot(0)).isEmpty()) return false;
        else 
        {                
            ItemStack [] result = CentrifugeRecipes.getInstance().getCentrifugeResult(this.itemStackHandler.getStackInSlot(0));    
            
            if(result[0].isEmpty() || result[1].isEmpty()) return false;
            else
            {
                ItemStack output1 = this.itemStackHandler.getStackInSlot(1);
                ItemStack output2 = this.itemStackHandler.getStackInSlot(2);
                
                if(output1.isEmpty() && output2.isEmpty()) return true;
                if(!output1.isItemEqual(result[0]) || !output2.isItemEqual(result[1])) return false;
                int res = Math.max(output1.getCount() + result[0].getCount(), output2.getCount() + result[1].getCount());
                return res <= getInventoryStackLimit() && res <= output1.getMaxStackSize() && res <= output2.getMaxStackSize();
            }
        }
    }
    
    
    
    
}
    

 

and the recipes :

Spoiler

package sunsigne.MyMod.container.recipes;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Maps;
import com.google.common.collect.Table;

import net.minecraft.block.Block;
import net.minecraft.block.BlockStoneBrick;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.EnumDyeColor;
import net.minecraft.item.Item;
import net.minecraft.item.ItemFishFood;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.FurnaceRecipes;
import sunsigne.MyMod.init.ModBlocks;
import sunsigne.MyMod.init.ModItems;

public class CentrifugeRecipes {


    
    private static final CentrifugeRecipes INSTANCE = new CentrifugeRecipes();
    private final Map<ItemStack, ItemStack[]> smeltingList = Maps.<ItemStack, ItemStack[]>newHashMap();
    
    public static CentrifugeRecipes getInstance()
    { 
        return INSTANCE;
    }
    
    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.smeltingList;
    }
    
    
    
    public void addCentrifugeRecipe(ItemStack input, ItemStack item1, ItemStack item2) 
    {
   
        this.smeltingList.put(input, twoItem(item1, item2));
    }
    
 
               
    private ItemStack [] twoItem(ItemStack i1 , ItemStack i2)
    {
            return new ItemStack[] {i1, i2};
    }
        
    
    

    public ItemStack [] getCentrifugeResult(ItemStack input) 
    {
        for (Entry<ItemStack, ItemStack[]> entry : this.smeltingList.entrySet())
        {
            if (this.compareItemStacks(input, entry.getKey()))
            {
                return entry.getValue();
            }
        }

        return null;
    }


    
        
    
    
    private CentrifugeRecipes() 
    {
        
        
        
        //test recipe
        
        addCentrifugeRecipe(new ItemStack(Blocks.OBSIDIAN), new ItemStack(Blocks.GOLD_BLOCK), new ItemStack(Blocks.IRON_BLOCK));
        addCentrifugeRecipe(new ItemStack(Blocks.COBBLESTONE), new ItemStack(Blocks.OBSIDIAN), new ItemStack(Blocks.IRON_BLOCK));
        
                             
        
        
    }

 

it just has a HUGE default, if you put something that can't be smelt, it crashed the game. I didn't find HOW to explain the game just doing nothing

(even if i know I just need to turn "public ItemStack [] getCentrifugeResult(ItemStack input) .........   return null;" into an equivalent to "return Itemstack.EMPTY" but for severeal output)

 

but as long as i trick it, just forbidding the player putting anything but true input in the input slot, it's no longer a problem

 

exemple

Spoiler

@Override
        public boolean isItemValid(ItemStack input) 
        {
        
            if(input.isItemEqual(new ItemStack(Blocks.OBSIDIAN))) return true;
            if(input.isItemEqual(new ItemStack(Blocks.COBBLESTONE))) return true;
            else return false;
            
            
        }    
            


 

Link to comment
Share on other sites

3 hours ago, sunsigne said:

Input.isItemEqual(new ItemStack(Blocks.OBSIDIAN))

This is equivalent to input.getItem() == Item.getFromBlock(Blocks.OBSIDIAN) and uses less memory by not creating an item stack that has to be cleaned up. 

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

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.