Jump to content

Custom Grinder Power


MrHubbard

Recommended Posts

Ok first of hi.

 

I have a grinder that does as expected when you put redstone as fuel it grinds ores to dust etc... But i am trying to get it so it stores a Power level so you don't need to have redstone to Grind, It stores the said fuel internally as power. I am not sure if this makes sense but consider the thermal expansion pulverizer only instead of RF it takes redstone that when placed in the power slot converts it to power.

 

I have made what i thought would work perfectly but i guess not, Any help would be appreciated.

 

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
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 hubbard.randomthings.Blocks.Block_Grinder;
import hubbard.randomthings.containers.Container_Grinder;
import hubbard.randomthings.init.Block_List;
import hubbard.randomthings.init.Item_List;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.inventory.SlotFurnaceFuel;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemHoe;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemSword;
import net.minecraft.item.ItemTool;
import hubbard.randomthings.init.GrinderRecipes;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.server.gui.IUpdatePlayerListBox;
import net.minecraft.tileentity.TileEntityLockable;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.MathHelper;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class TileEntity_Grinder_Powered extends TileEntityLockable implements IUpdatePlayerListBox, ISidedInventory
{
    private static final int[] slotsTop = new int[] {0};
    private static final int[] slotsBottom = new int[] {2, 1};
    private static final int[] slotsSides = new int[] {1};
    private ItemStack[] GrinderItemStacks = new ItemStack[3];
    private int cookTime;
    private int totalCookTime;
    private String GrinderCustomName;
    private int Power;
    private int maxPower = 20000;

    /**
     * Returns the number of slots in the inventory.
     */
    public int getSizeInventory()
    {
        return this.GrinderItemStacks.length;
    }

    /**
     * Returns the stack in slot i
     */
    public ItemStack getStackInSlot(int index)
    {
        return this.GrinderItemStacks[index];
    }

    /**
     * Removes from an inventory slot (first arg) up to a specified number (second arg) of items and returns them in a
     * new stack.
     */
    public ItemStack decrStackSize(int index, int count)
    {
        if (this.GrinderItemStacks[index] != null)
        {
            ItemStack itemstack;

            if (this.GrinderItemStacks[index].stackSize <= count)
            {
                itemstack = this.GrinderItemStacks[index];
                this.GrinderItemStacks[index] = null;
                return itemstack;
            }
            else
            {
                itemstack = this.GrinderItemStacks[index].splitStack(count);

                if (this.GrinderItemStacks[index].stackSize == 0)
                {
                    this.GrinderItemStacks[index] = null;
                }

                return itemstack;
            }
        }
        else
        {
            return null;
        }
    }

    /**
     * When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem -
     * like when you close a workbench GUI.
     */
    public ItemStack getStackInSlotOnClosing(int index)
    {
        if (this.GrinderItemStacks[index] != null)
        {
            ItemStack itemstack = this.GrinderItemStacks[index];
            this.GrinderItemStacks[index] = null;
            return itemstack;
        }
        else
        {
            return null;
        }
    }

    /**
     * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
     */
    public void setInventorySlotContents(int index, ItemStack stack)
    {
        boolean flag = stack != null && stack.isItemEqual(this.GrinderItemStacks[index]) && ItemStack.areItemStackTagsEqual(stack, this.GrinderItemStacks[index]);
        this.GrinderItemStacks[index] = stack;

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

        if (index == 0 && !flag)
        {
            this.totalCookTime = this.PowerLevel(stack);
            this.cookTime = 0;
            this.markDirty();
        }
    }

    /**
     * Gets the name of this command sender (usually username, but possibly "Rcon")
     */
    public String getName()
    {
        return this.hasCustomName() ? this.GrinderCustomName : "container.grinder";
    }

    /**
     * Returns true if this thing is named
     */
    public boolean hasCustomName()
    {
        return this.GrinderCustomName != null && this.GrinderCustomName.length() > 0;
    }

    public void setCustomInventoryName(String Name)
    {
        this.GrinderCustomName = Name;
    }

    public void readFromNBT(NBTTagCompound compound)
    {
        super.readFromNBT(compound);
        NBTTagList nbttaglist = compound.getTagList("Items", 10);
        this.GrinderItemStacks = new ItemStack[this.getSizeInventory()];

        for (int i = 0; i < nbttaglist.tagCount(); ++i)
        {
            NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
            byte b0 = nbttagcompound1.getByte("Slot");

            if (b0 >= 0 && b0 < this.GrinderItemStacks.length)
            {
                this.GrinderItemStacks[b0] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
            }
        }

        this.cookTime = compound.getShort("CookTime");
        this.totalCookTime = compound.getShort("CookTimeTotal");
        this.Power = compound.getShort("Power");

        if (compound.hasKey("CustomName", )
        {
            this.GrinderCustomName = compound.getString("CustomName");
        }
    }

    public void writeToNBT(NBTTagCompound compound)
    {
        super.writeToNBT(compound);
        compound.setShort("CookTime", (short)this.cookTime);
        compound.setShort("CookTimeTotal", (short)this.totalCookTime);
        compound.setShort("Power", (short)this.Power);
        NBTTagList nbttaglist = new NBTTagList();

        for (int i = 0; i < this.GrinderItemStacks.length; ++i)
        {
            if (this.GrinderItemStacks[i] != null)
            {
                NBTTagCompound nbttagcompound1 = new NBTTagCompound();
                nbttagcompound1.setByte("Slot", (byte)i);
                this.GrinderItemStacks[i].writeToNBT(nbttagcompound1);
                nbttaglist.appendTag(nbttagcompound1);
            }
        }

        compound.setTag("Items", nbttaglist);

        if (this.hasCustomName())
        {
            compound.setString("CustomName", this.GrinderCustomName);
        }
    }

    /**
     * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended. *Isn't
     * this more of a set than a get?*
     */
    public int getInventoryStackLimit()
    {
        return 64;
    }

    /**
     * Grinder isPowered
     */
    public boolean isPowered()
    {
        return this.Power > 0;
    }

    @SideOnly(Side.CLIENT)
    public static boolean isPowered(IInventory player)
    {
        return player.getField(0) > 0;
    }

    /**
     * Updates the JList with a new model.
     */
    public void update()
    {
        boolean flag = this.isPowered();
        boolean flag1 = false;

        if(Power > maxPower)
        	Power = maxPower;
       
        if (!this.worldObj.isRemote)
        {
            if (!this.isPowered()&& (this.GrinderItemStacks[1] == null || this.GrinderItemStacks[0] == null))
            {
                if (!this.isPowered() && this.cookTime > 0)
                {
                    --Power;
                }
            }
            else
            {
                if (!this.isPowered() && this.canGrind())
                {
                    if (this.isPowered())
                    {
                        flag1 = true;

                        if (this.GrinderItemStacks[1] != null)
                        {
                            --this.GrinderItemStacks[1].stackSize;
                            this.Power = this.PowerLevel(this.GrinderItemStacks[0]);

                            if (this.GrinderItemStacks[1].stackSize == 0)
                            {
                                this.GrinderItemStacks[1] = GrinderItemStacks[1].getItem().getContainerItem(GrinderItemStacks[1]);
                            }
                        }
                    }
                }

                if (this.isPowered() && this.canGrind())
                {
                    ++this.cookTime;
                    --Power;

                    if (this.cookTime == this.totalCookTime)
                    {
                        this.cookTime = 0;
                        this.totalCookTime = this.PowerLevel(this.GrinderItemStacks[0]);
                        this.GrindItem();
                        flag1 = true;
                    }
                }
                else
                {
                    this.cookTime = 0;
                }
            }

            if (flag != this.isPowered())
            {
                flag1 = true;
                Block_Grinder.setState(this.isPowered(), this.worldObj, this.pos);
            }
        }

        if (flag1)
        {
            this.markDirty();
        }
    }

    public int PowerLevel(ItemStack Power)
    {
        return 10;
    }

    /**
     * Returns true if the Grinder can grind an item, i.e. has a source item, destination stack isn't full, etc.
     */
    private boolean canGrind()
    {
        if (this.GrinderItemStacks[0] == null)
        {
            return false;
        }
        else
        {
            ItemStack itemstack = GrinderRecipes.instance().getGrindingResult(this.GrinderItemStacks[0]);
            if (itemstack == null) return false;
            if (this.GrinderItemStacks[2] == null) return true;
            if (!this.GrinderItemStacks[2].isItemEqual(itemstack)) return false;
            int result = GrinderItemStacks[2].stackSize + itemstack.stackSize;
            return result <= getInventoryStackLimit() && result <= this.GrinderItemStacks[2].getMaxStackSize(); //Forge BugFix: Make it respect stack sizes properly.
        }
    }

    /**
     * Turn one item from the Grinder source stack into the appropriate grinded item in the Grinder result stack
     */
    public void GrindItem()
    {
        if (this.canGrind() && Power > 0)
        {
            ItemStack itemstack = GrinderRecipes.instance().getGrindingResult(this.GrinderItemStacks[0]);

            if (this.GrinderItemStacks[2] == null)
            {
                this.GrinderItemStacks[2] = itemstack.copy();
            }
            else if (this.GrinderItemStacks[2].getItem() == itemstack.getItem())
            {
                this.GrinderItemStacks[2].stackSize += itemstack.stackSize; // Forge BugFix: Results may have multiple items
            }

            if (this.GrinderItemStacks[0].getItem() == Item.getItemFromBlock(Blocks.sponge) && this.GrinderItemStacks[0].getMetadata() == 1 && this.GrinderItemStacks[1] != null && this.GrinderItemStacks[1].getItem() == Items.bucket)
            {
                this.GrinderItemStacks[1] = new ItemStack(Items.water_bucket);
            }

            --this.GrinderItemStacks[0].stackSize;

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

    public static int getItemPowerLevel(ItemStack Power)
    {
        if (Power == null)
        {
            return 0;
        }
        else
        {
            Item item = Power.getItem();

            if (item == Items.redstone) return 10;
            return 10;
        }
    }

    public static boolean isItemPower(ItemStack Power)
    {
        return getItemPowerLevel(Power) > 0;
    }

    /**
     * Do not make give this method the name canInteractWith because it clashes with Container
     */
    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;
    }

    public void openInventory(EntityPlayer player) {}

    public void closeInventory(EntityPlayer player) {}

    /**
     * Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot.
     */
    public boolean isItemValidForSlot(int index, ItemStack stack)
    {
        return index == 2 ? false : (index != 1 ? true : isItemPower(stack) || SlotFurnaceFuel.isBucket(stack));
    }

    public int[] getSlotsForFace(EnumFacing side)
    {
        return side == EnumFacing.DOWN ? slotsBottom : (side == EnumFacing.UP ? slotsTop : slotsSides);
    }

    /**
     * Returns true if automation can insert the given item in the given slot from the given side. Args: slot, item,
     * side
     */
    public boolean canInsertItem(int index, ItemStack itemStackIn, EnumFacing direction)
    {
        return this.isItemValidForSlot(index, itemStackIn);
    }

    /**
     * Returns true if automation can extract the given item in the given slot from the given side. Args: slot, item,
     * side
     */
    public boolean canExtractItem(int index, ItemStack stack, EnumFacing direction)
    {
        if (direction == EnumFacing.DOWN && index == 1)
        {
            Item item = stack.getItem();

            if (item != Items.water_bucket && item != Items.bucket)
            {
                return false;
            }
        }

        return true;
    }

    public String getGuiID()
    {
        return "rt:Grinder";
    }

    public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn)
    {
        return new Container_Grinder(playerInventory, this);
    }

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

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

    public int getFieldCount()
    {
        return 4;
    }

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

 

2co2dkk.jpg

Link to comment
Share on other sites

You need to make a redstone bar thing in your gui.png.

Then go to your GuiGrinder class and put that

to the right coordinates. You need to check if

there is power in it. If there is, you need to get

the value of the power thats in there. But first in the

tile entity, you check if in that slot there is redstone.

If there is redstone, then give it 10+ power and

decrement the itemstack of the redstone. So every redstone

gives you 10 power more. Make a method too where it is

updating the tile entity everytime..

Then you go to the gui class and take the

variable of the amount of the power in the

x or y variable. If its going up, I think you need

to do y + power.. If its going down, then

y - power. ( I think). I did this last year,

so I can't remember all I did.

But I hope this was helpful. mkay bye (Dary)

Maybe I meant the containergrinder class

and not the guigrinder class.. I just

remember that I synced it but yeah..

Bye..

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.