Hello,
It's my first time working with Gui's and containers within forge.
I created a basic mod that includes two containers: a chest and a sort of furnace,
but I can't seem to acces them no matter what I try.
onBlockActivated gets called but the GUI jus't doesn't want to open.
I hope someone can help me
BlockLogStripper:
package com.almightyelement.thirteen.objects.machines.stripper;
import java.util.Random;
import com.almightyelement.thirteen.Main;
import com.almightyelement.thirteen.init.ModBlocks;
import com.almightyelement.thirteen.items.BlockBase;
import com.almightyelement.thirteen.objects.tileentity.TileEntityLogStripper;
import com.almightyelement.thirteen.util.Reference;
import com.almightyelement.thirteen.Main;
import com.almightyelement.thirteen.objects.tileentity.TileEntityLogStripper;
import com.almightyelement.thirteen.util.Reference;
import net.minecraft.block.BlockHorizontal;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.properties.PropertyDirection;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumBlockRenderType;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.Mirror;
import net.minecraft.util.Rotation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
public class BlockLogStripper extends BlockBase
{
public static final PropertyDirection FACING = BlockHorizontal.FACING;
public static final PropertyBool BURNING = PropertyBool.create("burning");
public BlockLogStripper(String name)
{
super(name, Material.IRON);
setSoundType(SoundType.METAL);
this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH).withProperty(BURNING, false));
}
@Override
public Item getItemDropped(IBlockState state, Random rand, int fortune)
{
return Item.getItemFromBlock(ModBlocks.LOG_STRIPPER);
}
@Override
public ItemStack getItem(World worldIn, BlockPos pos, IBlockState state)
{
return new ItemStack(ModBlocks.LOG_STRIPPER);
}
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
{
if(!worldIn.isRemote)
{
playerIn.openGui(Main.instance, Reference.GUI_LOG_STRIPPER, worldIn, pos.getX(), pos.getY(), pos.getZ());
}
return true;
}
@Override
public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
{
if (!worldIn.isRemote)
{
IBlockState north = worldIn.getBlockState(pos.north());
IBlockState south = worldIn.getBlockState(pos.south());
IBlockState west = worldIn.getBlockState(pos.west());
IBlockState east = worldIn.getBlockState(pos.east());
EnumFacing face = (EnumFacing)state.getValue(FACING);
if (face == EnumFacing.NORTH && north.isFullBlock() && !south.isFullBlock()) face = EnumFacing.SOUTH;
else if (face == EnumFacing.SOUTH && south.isFullBlock() && !north.isFullBlock()) face = EnumFacing.NORTH;
else if (face == EnumFacing.WEST && west.isFullBlock() && !east.isFullBlock()) face = EnumFacing.EAST;
else if (face == EnumFacing.EAST && east.isFullBlock() && !west.isFullBlock()) face = EnumFacing.WEST;
worldIn.setBlockState(pos, state.withProperty(FACING, face), 2);
}
}
public static void setState(boolean active, World worldIn, BlockPos pos)
{
IBlockState state = worldIn.getBlockState(pos);
TileEntity tileentity = worldIn.getTileEntity(pos);
if(active) worldIn.setBlockState(pos, ModBlocks.LOG_STRIPPER.getDefaultState().withProperty(FACING, state.getValue(FACING)).withProperty(BURNING, true), 3);
else worldIn.setBlockState(pos, ModBlocks.LOG_STRIPPER.getDefaultState().withProperty(FACING, state.getValue(FACING)).withProperty(BURNING, false), 3);
if(tileentity != null)
{
tileentity.validate();
worldIn.setTileEntity(pos, tileentity);
}
}
@Override
public boolean hasTileEntity(IBlockState state)
{
return true;
}
@Override
public TileEntity createTileEntity(World world, IBlockState state)
{
return new TileEntityLogStripper();
}
@Override
public IBlockState getStateForPlacement(World world, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer, EnumHand hand)
{
return this.getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite());
}
@Override
public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
{
worldIn.setBlockState(pos, this.getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite()), 2);
}
@Override
public EnumBlockRenderType getRenderType(IBlockState state)
{
return EnumBlockRenderType.MODEL;
}
@Override
public IBlockState withRotation(IBlockState state, Rotation rot)
{
return state.withProperty(FACING, rot.rotate((EnumFacing)state.getValue(FACING)));
}
@Override
public IBlockState withMirror(IBlockState state, Mirror mirrorIn)
{
return state.withRotation(mirrorIn.toRotation((EnumFacing)state.getValue(FACING)));
}
@Override
protected BlockStateContainer createBlockState()
{
return new BlockStateContainer(this, new IProperty[] {BURNING,FACING});
}
@Override
public IBlockState getStateFromMeta(int meta)
{
EnumFacing facing = EnumFacing.getFront(meta);
if(facing.getAxis() == EnumFacing.Axis.Y) facing = EnumFacing.NORTH;
return this.getDefaultState().withProperty(FACING, facing);
}
@Override
public int getMetaFromState(IBlockState state)
{
return ((EnumFacing)state.getValue(FACING)).getIndex();
}
}
GuiLogStripper:
package com.almightyelement.thirteen.objects.machines.stripper;
import com.almightyelement.thirteen.objects.tileentity.TileEntityLogStripper;
import com.almightyelement.thirteen.util.Reference;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.util.ResourceLocation;
public class GuiLogStripper extends GuiContainer
{
private static final ResourceLocation TEXTURES = new ResourceLocation(Reference.MOD_ID + ":textures/gui/log_stripper.png");
private final InventoryPlayer player;
private final TileEntityLogStripper tileentity;
public GuiLogStripper(InventoryPlayer player, TileEntityLogStripper tileentity)
{
super(new ContainerLogStripper(player, tileentity));
this.player = player;
this.tileentity = tileentity;
}
@Override
protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY)
{
String tileName = this.tileentity.getDisplayName().getUnformattedText();
this.fontRenderer.drawString(tileName, (this.xSize / 2 - this.fontRenderer.getStringWidth(tileName) / 2) + 3, 8, 4210752);
this.fontRenderer.drawString(this.player.getDisplayName().getUnformattedText(), 122, this.ySize - 96 + 2, 4210752);
}
@Override
protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY)
{
GlStateManager.color(1.0f, 1.0f, 1.0f, 1.0f);
this.mc.getTextureManager().bindTexture(TEXTURES);
this.drawTexturedModalRect(this.guiLeft, this.guiTop, 0, 0, this.xSize, this.ySize);
if(TileEntityLogStripper.isBurning(tileentity))
{
int k = this.getBurnLeftScaled(13);
this.drawTexturedModalRect(this.guiLeft + 8, this.guiTop + 54 + 12 - k, 176, 12 - k, 14, k + 1);
}
int l = this.getCookProgressScaled(24);
this.drawTexturedModalRect(this.guiLeft + 44, this.guiTop + 36, 176, 14, l + 1, 16);
}
private int getBurnLeftScaled(int pixels)
{
int i = this.tileentity.getField(1);
if(i == 0) i = 200;
return this.tileentity.getField(0) * pixels / i;
}
private int getCookProgressScaled(int pixels)
{
int i = this.tileentity.getField(2);
int j = this.tileentity.getField(3);
return j != 0 && i != 0 ? i * pixels / j : 0;
}
}
GuiHandler:
package com.almightyelement.thirteen.util.handlers;
import com.almightyelement.thirteen.objects.container.ContainerCopperChest;
import com.almightyelement.thirteen.objects.container.ContainerLogStripper;
import com.almightyelement.thirteen.objects.gui.GuiCopperChest;
import com.almightyelement.thirteen.objects.gui.GuiLogStripper;
import com.almightyelement.thirteen.objects.tileentity.TileEntityCopperChest;
import com.almightyelement.thirteen.objects.tileentity.TileEntityLogStripper;
import com.almightyelement.thirteen.util.Reference;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.network.IGuiHandler;
public class GuiHandler implements IGuiHandler
{
@Override
public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z)
{
if(ID == Reference.GUI_LOG_STRIPPER) return new ContainerLogStripper(player.inventory, (TileEntityLogStripper)world.getTileEntity(new BlockPos(x,y,z)));
if(ID == Reference.GUI_COPPER_CHEST) return new ContainerCopperChest(player.inventory, (TileEntityCopperChest)world.getTileEntity(new BlockPos(x,y,z)), player);
return null;
}
@Override
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z){
if(ID == Reference.GUI_LOG_STRIPPER) return new GuiLogStripper(player.inventory, (TileEntityLogStripper)world.getTileEntity(new BlockPos(x,y,z)));
if(ID == Reference.GUI_COPPER_CHEST) return new GuiCopperChest(player.inventory, (TileEntityCopperChest)world.getTileEntity(new BlockPos(x,y,z)), player);
return null;
}
}
Thanks in advance,
AlmightyElement