Jump to content

Changing block texture based on Tile entity variable


brady131313

Recommended Posts

I want to change my machines face to an active texture when It is active but how would I access that variable in the tile entity, This is what I've tried

Block

package com.brady131313.steamreborn.block.machines;

import com.brady131313.steamreborn.block.BlockSR;
import com.brady131313.steamreborn.init.ModBlocks;
import com.brady131313.steamreborn.init.ModItems;
import com.brady131313.steamreborn.reference.GuiId;
import com.brady131313.steamreborn.steamreborn;
import com.brady131313.steamreborn.tileentity.TileEntityAlloyCompressor;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
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.IIcon;
import net.minecraft.util.MathHelper;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;

import java.util.Random;

public class MachineAlloyCompressor extends BlockSR implements ITileEntityProvider {

    @SideOnly(Side.CLIENT)
    private static IIcon frontIcon;
    @SideOnly(Side.CLIENT)
    private static IIcon sideIcon;
    @SideOnly(Side.CLIENT)
    private static IIcon topbotIcon;

    TileEntityAlloyCompressor alloyCompressor;

    public MachineAlloyCompressor() {

        super(Material.iron);
        this.setBlockName("machineAlloyCompressor");
        this.setHardness(3.0f);
    }

    @Override
    @SideOnly(Side.CLIENT)
    public void registerBlockIcons(IIconRegister iconRegister) {

        this.frontIcon = iconRegister.registerIcon(TileEntityAlloyCompressor.isActive ? "steamreborn:AlloyCompressorActive" : "steamreborn:alloyCompressorFront1");
        this.sideIcon = iconRegister.registerIcon("steamreborn:steamMachineSide");
        this.topbotIcon = iconRegister.registerIcon("steamreborn:steamMachineTop");
    }

    @Override
    public IIcon getIcon(int side, int meta) {

        if (side == 3 && meta == 0) {
            return frontIcon;
        } else {
            if (side == 0 || side == 1) {
                return topbotIcon;
            } else if (side != meta) {
                return sideIcon;
            } else {
                return frontIcon;
            }
        }
    }

    @Override
    public void onBlockAdded(World world, int x, int y, int z) {

        super.onBlockAdded(world, x, y, z);
        setDefaultDirection(world, x, y, z);
    }

    @Override
    public TileEntity createNewTileEntity(World world, int metaData) {

        return new TileEntityAlloyCompressor();
    }

    @Override
    public void onNeighborBlockChange(World world, int x, int y, int z, Block block) {

        TileEntityAlloyCompressor tileEntityAlloyCompressor = (TileEntityAlloyCompressor) world.getTileEntity(x, y, z);
        super.onNeighborBlockChange(world, x, y, z, block);
    }

    @Override
    public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int facehit, float par7, float par8, float par9) {

        TileEntityAlloyCompressor tileEntity = (TileEntityAlloyCompressor) world.getTileEntity(x, y, z);
        ItemStack playerItem = player.getCurrentEquippedItem();

        if (tileEntity == null || player.isSneaking()) {
            return false;
        } else {
            if (!world.isRemote) {
                if (world.getTileEntity(x, y, z) instanceof TileEntityAlloyCompressor) {
                    player.openGui(steamreborn.instance, GuiId.ALLOY_COMPRESSOR.ordinal(), world, x, y, z);
                }
            }
        }
        return true;
    }

    @Override
    public boolean onBlockEventReceived(World world, int x, int y, int z, int eventId, int eventData) {

        super.onBlockEventReceived(world, x, y, z, eventId, eventData);
        TileEntity tileEntity = world.getTileEntity(x, y, z);
        return tileEntity != null && tileEntity.receiveClientEvent(eventId, eventData);
    }

    @Override
    public void breakBlock(World world, int x, int y, int z, Block block, int meta) {

        super.breakBlock(world, x, y, z, block, meta);
    }

    public static void setDefaultDirection(World world, int x, int y, int z) {

        if (!world.isRemote) {

            Block zNeg = world.getBlock(x, y, z - 1);
            Block zPos = world.getBlock(x, y, z + 1);
            Block xNeg = world.getBlock(x - 1, y, z);
            Block xPos = world.getBlock(x + 1, y, z);

            byte meta = 3;

            if (xNeg.func_149730_j() && !xPos.func_149730_j()) {
                meta = 5;
            }

            if (xPos.func_149730_j() && !xNeg.func_149730_j()) {
                meta = 4;
            }

            if (zNeg.func_149730_j() && !zPos.func_149730_j()) {
                meta = 3;
            }

            if (zPos.func_149730_j() && !zNeg.func_149730_j()) {
                meta = 2;
            }

            world.setBlockMetadataWithNotify(x, y, z, meta, 2);
        }
    }

    @Override
    public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase entity, ItemStack itemStack) {

        int rotation = MathHelper.floor_double((double) (entity.rotationYaw * 4F / 360F) + 0.5D) & 3;

        if (rotation == 0) {
            world.setBlockMetadataWithNotify(x, y, z, 2, 2);
        }

        if (rotation == 1) {
            world.setBlockMetadataWithNotify(x, y, z, 5, 2);
        }

        if (rotation == 2) {
            world.setBlockMetadataWithNotify(x, y, z, 3, 2);
        }

        if (rotation == 3) {
            world.setBlockMetadataWithNotify(x, y, z, 4, 2);
        }
    }

}

 

Tile Entity

package com.brady131313.steamreborn.tileentity;

import com.brady131313.steamreborn.init.ModFluids;
import com.brady131313.steamreborn.inventory.ContainerSR;
import com.brady131313.steamreborn.item.crafting.RecipeAlloyCompressor;
import com.brady131313.steamreborn.network.PacketHandler;
import com.brady131313.steamreborn.network.message.MessageTileEntityAlloyCompressor;
import com.brady131313.steamreborn.recipe.RecipesAlloyCompressor;
import com.brady131313.steamreborn.reference.Names;
import com.brady131313.steamreborn.tileentity.base.TileMachineTank;
import com.brady131313.steamreborn.utility.Tank;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ICrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.Packet;
import net.minecraft.util.ChatComponentText;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.FluidStack;

/**
* Created by brady on 8/6/2014.
*/
public class TileEntityAlloyCompressor extends TileMachineTank {

    public static final int INVENTORY_SIZE = 3;
    public static final int ITEM1_INVENTORY_INDEX = 0;
    public static final int ITEM2_INVENTORY_INDEX = 1;
    public static final int OUTPUT_INVENTORY_INDEX = 2;
    private int tankSize;

    public int itemCookTime; //How long the current item has been cooking
    public int deviceCookTime; //How long the smelter will cook

    public static boolean isActive = false;

    public ItemStack outputItemStack;

    public TileEntityAlloyCompressor() {
        tank = new Tank(new FluidStack(ModFluids.steamFluid, 0), 10000);
        inventory = new ItemStack[iNVENTORY_SIZE];
    }

    public void alloyItem() {

        if (this.canAlloy()) {
            RecipeAlloyCompressor recipe = RecipesAlloyCompressor.getInstance().getRecipe(inventory[iTEM1_INVENTORY_INDEX], inventory[iTEM2_INVENTORY_INDEX]);

            if (this.inventory[OUTPUT_INVENTORY_INDEX] == null) {
                this.inventory[OUTPUT_INVENTORY_INDEX] = recipe.getRecipeOutput().copy();
            } else if (this.inventory[OUTPUT_INVENTORY_INDEX].isItemEqual(recipe.getRecipeOutput())) {
                inventory[OUTPUT_INVENTORY_INDEX].stackSize += recipe.getRecipeOutput().stackSize;
            }

            decrStackSize(ITEM1_INVENTORY_INDEX, recipe.getRecipeInputs()[0].getStackSize());
            decrStackSize(ITEM2_INVENTORY_INDEX, recipe.getRecipeInputs()[1].getStackSize());
            this.tank.drain(100, true);
        }
    }

    @Override
    public void updateEntity() {

        boolean isBurning = this.deviceCookTime > 0;
        boolean sendUpdate = false;
        isActive = isBurning;

        //If the smelter still has burn time, decrement it
        if (this.deviceCookTime > 0) {
            this.deviceCookTime--;
         }

        if (!this.worldObj.isRemote) {
            //Start cooking new item if we can
            if (this.deviceCookTime == 0 && this.canAlloy()) {
                if (this.tank.getFluidAmount() >= 100) {
                    this.deviceCookTime += 1;
                    //Implement fluid decrement
                }
            }

            //Continue cooking the same item if we can
            if (this.deviceCookTime > 0 && this.canAlloy()) {
                this.itemCookTime++;

                if (this.itemCookTime == 200) {
                    this.itemCookTime = 0;
                    this.alloyItem();
                    sendUpdate = true;
                }
            } else {
                this.itemCookTime = 0;
            }

            // If the state has changed catch something has changed
            if (isBurning != this.deviceCookTime > 0) {
                sendUpdate = true;
            }
        }

        if (sendUpdate) {
            this.markDirty();
            this.state = this.deviceCookTime > 0 ? (byte) 1 : (byte) 0;
            this.worldObj.addBlockEvent(this.xCoord, this.yCoord, this.zCoord, this.getBlockType(), 1, this.state);
            PacketHandler.INSTANCE.sendToAllAround(new MessageTileEntityAlloyCompressor(this, inventory[OUTPUT_INVENTORY_INDEX]), new NetworkRegistry.TargetPoint(this.worldObj.provider.dimensionId, (double) this.xCoord, (double) this.yCoord, (double) this.zCoord, 128d));
            this.worldObj.notifyBlockChange(this.xCoord, this.yCoord, this.zCoord, this.getBlockType());
        }
    }

    private boolean canAlloy() {

        if (inventory[iTEM1_INVENTORY_INDEX] == null || inventory[iTEM2_INVENTORY_INDEX] == null) {

            return false;
        } else {
            ItemStack alloyedItemStack = RecipesAlloyCompressor.getInstance().getResult(inventory[iTEM1_INVENTORY_INDEX], inventory[iTEM2_INVENTORY_INDEX]);

            if (alloyedItemStack == null) {
                return false;
            }

            if (inventory[OUTPUT_INVENTORY_INDEX] == null) {
                return true;
            } else {
                boolean outputEquals = this.inventory[OUTPUT_INVENTORY_INDEX].isItemEqual(alloyedItemStack);
                int mergedOutputStackSize = this.inventory[OUTPUT_INVENTORY_INDEX].stackSize + alloyedItemStack.stackSize;

                if (outputEquals) {
                    return mergedOutputStackSize <= getInventoryStackLimit() && mergedOutputStackSize <= alloyedItemStack.getMaxStackSize();
                }
            }
        }

        return false;
    }

    @Override
    public void markDirty() {

        PacketHandler.INSTANCE.sendToAllAround(new MessageTileEntityAlloyCompressor(this, inventory[OUTPUT_INVENTORY_INDEX]), new NetworkRegistry.TargetPoint(this.worldObj.provider.dimensionId, (double) this.xCoord, (double) this.yCoord, (double) this.zCoord, 128d));
        worldObj.func_147451_t(xCoord, yCoord, zCoord);
    }

    @Override
    public int[] getAccessibleSlotsFromSide(int side) {

        return side == ForgeDirection.DOWN.ordinal() ? new int[]{OUTPUT_INVENTORY_INDEX} : new int[]{ITEM1_INVENTORY_INDEX, ITEM2_INVENTORY_INDEX, OUTPUT_INVENTORY_INDEX};
    }

    @Override
    public boolean canInsertItem(int slotIndex, ItemStack itemStack, int side) {

        if (worldObj.getTileEntity(xCoord, yCoord, zCoord) instanceof TileEntityAlloyCompressor) {
            return isItemValidForSlot(slotIndex, itemStack);
        } else {
            return false;
        }
    }

    @Override
    public boolean canExtractItem(int slotIndex, ItemStack itemStack, int side) {

        return slotIndex == OUTPUT_INVENTORY_INDEX;
    }

    @Override
    public boolean isItemValidForSlot(int slotIndex, ItemStack itemStack) {

        switch (slotIndex) {

            case ITEM1_INVENTORY_INDEX: {
                return true;
            }
            case ITEM2_INVENTORY_INDEX: {
                return true;
            }
            default: {
                return false;
            }
        }
    }

    @Override
    public String getInventoryName() {

        return this.hasCustomName() ? this.getCustomName() : Names.Containers.ALLOY_COMPRESSOR;
    }

    @Override
    public boolean hasCustomInventoryName() {

        return this.hasCustomName();
    }

    @Override
    public boolean isUseableByPlayer(EntityPlayer player) {

        return true;
    }

    @SideOnly(Side.CLIENT)
    public int getCookProgressScaled(int scale) {

        return this.itemCookTime * scale / 200;
    }

    public int getScaledSteamLevel(int scale) {

        final double steamLevel = tank.getFluid() == null ? 0 : this.tank.getFluidAmount();

        return (int) (steamLevel * scale / this.tank.getCapacity());
    }

    public FluidStack getSteam() {

        return tank.getFluid();
    }

    @Override
    public void process() {


    }

    public void getGUINetworkData(int id, int value) {

        if (id == 6) tankSize = value;
        if (id == 7) itemCookTime = value;
        else super.getGuiNetworkData(id, value);
    }

    public void sendGUINetworkData(ContainerSR container, ICrafting crafting) {

        super.sendGuiNetworkData(container, crafting);
        crafting.sendProgressBarUpdate(container, 6, tankSize);
        crafting.sendProgressBarUpdate(container, 7, itemCookTime);
    }

    @Override
    public Packet getDescriptionPacket() {

        return PacketHandler.INSTANCE.getPacketFrom(new MessageTileEntityAlloyCompressor(this, inventory[OUTPUT_INVENTORY_INDEX]));
    }

    @Override
    public void readFromNBT(NBTTagCompound nbt) {

        super.readFromNBT(nbt);

        NBTTagList tagList = nbt.getTagList(Names.NBT.ITEMS, 10);
        inventory = new ItemStack[this.getSizeInventory()];

        for (int i = 0; i < tagList.tagCount(); ++i) {
            NBTTagCompound tagCompound = tagList.getCompoundTagAt(i);
            byte slotIndex = tagCompound.getByte("Slot");
            if (slotIndex >= 0 && slotIndex < inventory.length) {
                inventory[slotIndex] = ItemStack.loadItemStackFromNBT(tagCompound);
            }
        }

        tank.readFromNBT(nbt.getCompoundTag("steamTank"));
        deviceCookTime = nbt.getInteger("deviceCookTime");
        itemCookTime = nbt.getInteger("itemCookTime");
    }

    @Override
    public void writeToNBT(NBTTagCompound nbt) {

        super.writeToNBT(nbt);

        NBTTagList tagList = new NBTTagList();
        for (int currentIndex = 0; currentIndex < inventory.length; ++currentIndex) {
            if (inventory[currentIndex] != null) {
                NBTTagCompound tagCompound = new NBTTagCompound();
                tagCompound.setByte("Slot", (byte) currentIndex);
                inventory[currentIndex].writeToNBT(tagCompound);
                tagList.appendTag(tagCompound);
            }
        }

        nbt.setTag("steamTank", tank.writeToNBT(new NBTTagCompound()));
        nbt.setTag(Names.NBT.ITEMS, tagList);
        nbt.setInteger("deviceCookTime", deviceCookTime);
        nbt.setInteger("itemCookTime", itemCookTime);
    }

    public void sendChatInfoToPlayer(EntityPlayer player) {

        player.addChatMessage(new ChatComponentText("Tanks Fluid Amount: " + tank.getFluidAmount()));
        player.addChatMessage(new ChatComponentText("Tanks Capacity: " + tank.getCapacity()));
    }
}

Link to comment
Share on other sites

Look at how they have done this for the vanilla furnace.

 

The isActive variable goes in the block class, not the tileentity class

 

above your constructor in the block class, add this:

public final boolean isActive;

then make your constructor look something like this:

public MachineAlloyCompressor(Material mat, boolean isActive) {
        super(mat) 
        this.isActive = isActive;
}

then when you are registering the icons, change TileEntityAlloyCompressor.isActive to this.isActive

Also, where you are registering your blocks, you need to put true inside the brackets where you normally set the material on the active version of your block, and false on the idle version. This is because you have a boolean (isActive) in the constructor

I ask complicated questions, and apparently like to write really long detailed posts. But I also help others when I can.

Link to comment
Share on other sites

Okay, nevermind then, this is how i do it, and this is how the vanilla furnace does it though

blockRegistry.addObject(61, "furnace", (new BlockFurnace(false)).setHardness(3.5F).setStepSound(soundTypePiston).setBlockName("furnace").setCreativeTab(CreativeTabs.tabDecorations));
blockRegistry.addObject(62, "lit_furnace", (new BlockFurnace(true)).setHardness(3.5F).setStepSound(soundTypePiston).setLightLevel(0.875F).setBlockName("furnace"));

I ask complicated questions, and apparently like to write really long detailed posts. But I also help others when I can.

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.