Jump to content

[1.9] [SOLVED] GUI doesn't open on right-click


GlowingWater

Recommended Posts

Hey, I'm having an issue with my custom block. When I right-click it, my GUI doesn't show up. What's the reason?

 

Main Class

GameRegistry.registerBlock(crate, crate.getUnlocalizedName().substring(5));
GameRegistry.registerTileEntity(TileEntityCrate.class, "tile_crate");
NetworkRegistry.INSTANCE.registerGuiHandler(DecorationMod.instance, new GuiHandlerCrate());

 

Block Class

package com.gwater.decorationmod.block;

import net.minecraft.block.BlockContainer;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.EnumBlockRenderType;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

import com.gwater.decorationmod.DecorationMod;
import com.gwater.decorationmod.handler.GuiHandlerCrate;
import com.gwater.decorationmod.tileentity.TileEntityCrate;

public class Crate extends BlockContainer {

public Crate(Material wood) {
	super(wood);
	this.setUnlocalizedName("crate");
	this.setHardness(2.5F);
	this.setHarvestLevel("axe", 0);
	this.setStepSound(SoundType.WOOD);
	this.setCreativeTab(DecorationMod.tabDecorationMod);
}

@Override
public TileEntity createNewTileEntity(World worldIn, int meta) {
	return new TileEntityCrate();
}

@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) {
	if(!worldIn.isRemote) {
		playerIn.openGui(DecorationMod.instance, GuiHandlerCrate.getGuiID(), worldIn, pos.getX(), pos.getY(), pos.getZ());
	}
	return true;
}

@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state) {

	IInventory inventory = worldIn.getTileEntity(pos) instanceof IInventory ? (IInventory)worldIn.getTileEntity(pos) : null;
	if(inventory != null) {
		for(int i = 0; i < inventory.getSizeInventory(); i++) {
			if(inventory.getStackInSlot(i) != null) {
				EntityItem item = new EntityItem(worldIn, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, inventory.getStackInSlot(i));
				float multiplier = 0.1F;
				float motionX = worldIn.rand.nextFloat() - 0.5F;
				float motionY = worldIn.rand.nextFloat() - 0.5F;
				float motionZ = worldIn.rand.nextFloat() - 0.5F;
				item.motionX = motionX * multiplier;
				item.motionY = motionY * multiplier;
				item.motionZ = motionZ * multiplier;

				worldIn.spawnEntityInWorld(item);
			}
		}
		inventory.clear();
	}

	super.breakBlock(worldIn, pos, state);
}

@Override
public BlockRenderLayer getBlockLayer() {
	return BlockRenderLayer.SOLID;
}

@Override
public EnumBlockRenderType getRenderType(IBlockState state) {
	return EnumBlockRenderType.MODEL;
}

@Override
public boolean isOpaqueCube(IBlockState state) {
	return true;
}

@Override
public boolean isFullCube(IBlockState state) {
	return true;
}
}

 

Container Class

package com.gwater.decorationmod.container;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;

import com.gwater.decorationmod.tileentity.TileEntityCrate;

public class ContainerCrate extends Container {

private TileEntityCrate tileEntityCrate;

private final int HOTBAR_SLOT_COUNT = 9;
private final int PLAYER_INVENTORY_ROW_COUNT = 3;
private final int PLAYER_INVENTORY_COLUMN_COUNT = 9;
private final int PLAYER_INVENTORY_SLOT_COUNT = PLAYER_INVENTORY_COLUMN_COUNT * PLAYER_INVENTORY_ROW_COUNT;
private final int VANILLA_SLOT_COUNT = HOTBAR_SLOT_COUNT + PLAYER_INVENTORY_SLOT_COUNT;

private final int VANILLA_FIRST_SLOT_INDEX = 0;
private final int TE_INVENTORY_FIRST_SLOT_INDEX = VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT;
private final int TE_INVENTORY_SLOT_COUNT = 9;

public ContainerCrate(InventoryPlayer invPlayer, TileEntityCrate tileEntityCrate) {
	this.tileEntityCrate = tileEntityCrate;
	final int SLOT_X_SPACING = 18;
	final int SLOT_Y_SPACING = 18;
	final int HOTBAR_XPOS = 8;
	final int HOTBAR_YPOS = 109;

	for(int x = 0; x < HOTBAR_SLOT_COUNT; x++) {
		int slotNumber = x;
		addSlotToContainer(new Slot(invPlayer, slotNumber, HOTBAR_XPOS + SLOT_X_SPACING * x, HOTBAR_YPOS));
	}

	final int PLAYER_INVENTORY_XPOS = 8;
	final int PLAYER_INVENTORY_YPOS = 51;

	for(int y = 0; y < PLAYER_INVENTORY_ROW_COUNT; y++) {
		for(int x = 0; x < PLAYER_INVENTORY_COLUMN_COUNT; x++) {
			int slotNumber = HOTBAR_SLOT_COUNT + y * PLAYER_INVENTORY_COLUMN_COUNT + x;
			int xpos = PLAYER_INVENTORY_XPOS + x * SLOT_X_SPACING;
			int ypos = PLAYER_INVENTORY_YPOS + y * SLOT_Y_SPACING;
			addSlotToContainer(new Slot(invPlayer, slotNumber, xpos, ypos));
		}
	}

	if(TE_INVENTORY_SLOT_COUNT != tileEntityCrate.getSizeInventory()) {
		System.err.println("Mismatched slot count in ContainerCrate(" + TE_INVENTORY_SLOT_COUNT + ") and TileEntityCrate(" + tileEntityCrate.getSizeInventory() + ")");
	}

	final int TILE_INVENTORY_XPOS = 8;
	final int TILE_INVENTORY_YPOS = 20;

	for(int x = 0; x < TE_INVENTORY_SLOT_COUNT; x++) {
		int slotNumber = x;
		addSlotToContainer(new Slot(tileEntityCrate, slotNumber, TILE_INVENTORY_XPOS + SLOT_X_SPACING * x, TILE_INVENTORY_YPOS));
	}
}

@Override
public boolean canInteractWith(EntityPlayer playerIn) {
	return tileEntityCrate.isUseableByPlayer(playerIn);
}

@Override
public ItemStack transferStackInSlot(EntityPlayer playerIn, int sourceSlotIndex) {
	Slot sourceSlot = (Slot)inventorySlots.get(sourceSlotIndex);
	if (sourceSlot == null || !sourceSlot.getHasStack()) return null;
	ItemStack sourceStack = sourceSlot.getStack();
	ItemStack copyOfSourceStack = sourceStack.copy();

	// Check if the slot clicked is one of the vanilla container slots
	if (sourceSlotIndex >= VANILLA_FIRST_SLOT_INDEX && sourceSlotIndex < VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT) {
		// This is a vanilla container slot so merge the stack into the tile inventory
		if (!mergeItemStack(sourceStack, TE_INVENTORY_FIRST_SLOT_INDEX, TE_INVENTORY_FIRST_SLOT_INDEX + TE_INVENTORY_SLOT_COUNT, false)){
			return null;
		}
	} else if (sourceSlotIndex >= TE_INVENTORY_FIRST_SLOT_INDEX && sourceSlotIndex < TE_INVENTORY_FIRST_SLOT_INDEX + TE_INVENTORY_SLOT_COUNT) {
		// This is a TE slot so merge the stack into the players inventory
		if (!mergeItemStack(sourceStack, VANILLA_FIRST_SLOT_INDEX, VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT, false)) {
			return null;
		}
	} else {
		System.err.print("Invalid slotIndex:" + sourceSlotIndex);
		return null;
	}

	// If stack size == 0 (the entire stack was moved) set slot contents to null
	if (sourceStack.stackSize == 0) {
		sourceSlot.putStack(null);
	} else {
		sourceSlot.onSlotChanged();
	}

	sourceSlot.onPickupFromSlot(playerIn, sourceStack);
	return copyOfSourceStack;
}

@Override
public void onContainerClosed(EntityPlayer playerIn) {
	super.onContainerClosed(playerIn);
	this.tileEntityCrate.closeInventory(playerIn);
}
}

 

GUI Class

package com.gwater.decorationmod.gui;

import java.awt.Color;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.util.ResourceLocation;

import com.gwater.decorationmod.References;
import com.gwater.decorationmod.container.ContainerCrate;
import com.gwater.decorationmod.tileentity.TileEntityCrate;

public class GuiCrate extends GuiContainer {

private static final ResourceLocation texture = new ResourceLocation(References.MODID, "/textures/gui/crate_gui_bg.png");
private TileEntityCrate tileEntityCrate;

public GuiCrate(InventoryPlayer invPlayer, TileEntityCrate tile) {
	super(new ContainerCrate(invPlayer, tile));
	tileEntityCrate = tile;

	xSize = 176;
	ySize = 133;
}

@Override
protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
	Minecraft.getMinecraft().getTextureManager().bindTexture(texture);
	GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
	drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize);
}

@Override
protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) {
	final int LABEL_XPOS = 5;
	final int LABEL_YPOS = 5;
	fontRendererObj.drawString(tileEntityCrate.getDisplayName().getUnformattedText(), LABEL_XPOS, LABEL_YPOS, Color.DARK_GRAY.getRGB());
}
}

 

GUI Handler Class

package com.gwater.decorationmod.handler;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.network.IGuiHandler;

import com.gwater.decorationmod.container.ContainerCrate;
import com.gwater.decorationmod.tileentity.TileEntityCrate;

public class GuiHandlerCrate implements IGuiHandler {

private static final int GUI_ID = 30;
public static int getGuiID() {return GUI_ID;}

@Override
public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
	if(ID != getGuiID()) {
		System.err.println("Invalid ID: expected " + getGuiID() + ", received " + ID);
	}

	BlockPos xyz = new BlockPos(x, y, z);
	TileEntity tileEntity = world.getTileEntity(xyz);
	if(tileEntity instanceof TileEntityCrate) {
		TileEntityCrate tileEntityCrate = (TileEntityCrate)tileEntity;
		return new ContainerCrate(player.inventory, tileEntityCrate);
	}
	return null;
}

@Override
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
	if(ID != getGuiID()) {
		System.err.println("Invalid ID: expected " + getGuiID() + ", received " + ID);
	}

	BlockPos xyz = new BlockPos(x, y, z);
	TileEntity tileEntity = world.getTileEntity(xyz);
	if(tileEntity instanceof TileEntityCrate) {
		TileEntityCrate tileEntityCrate = (TileEntityCrate)tileEntity;
		return new ContainerCrate(player.inventory, tileEntityCrate);
	}
	return null;
}
}

 

Tile Entity Class

package com.gwater.decorationmod.tileentity;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextComponentTranslation;
import scala.actors.threadpool.Arrays;

public class TileEntityCrate extends TileEntity implements IInventory {

final int NUMBER_OF_SLOTS = 9;
private ItemStack[] itemStacks = new ItemStack[NUMBER_OF_SLOTS];

@Override
public int getSizeInventory() {
	return itemStacks.length;
}

@Override
public ItemStack getStackInSlot(int index) {
	return itemStacks[index];
}

@Override
public ItemStack decrStackSize(int slotIndex, int count) {
	ItemStack itemStackInSlot = getStackInSlot(slotIndex);
	if(itemStackInSlot == null) return null;

	ItemStack itemStackRemoved;
	if(itemStackInSlot.stackSize <= count) {
		itemStackRemoved = itemStackInSlot;
		setInventorySlotContents(slotIndex, null);
	}
	else
	{
		itemStackRemoved = itemStackInSlot.splitStack(count);
		if(itemStackInSlot.stackSize == 0) {
			setInventorySlotContents(slotIndex, null);
		}
	}
	markDirty();
	return itemStackRemoved;
}

@Override
public void setInventorySlotContents(int slotIndex, ItemStack stack) {
	itemStacks[slotIndex] = stack;
	if(stack != null && stack.stackSize > getInventoryStackLimit()) {
		stack.stackSize = getInventoryStackLimit();
	}
	markDirty();
}

@Override
public int getInventoryStackLimit() {
	return 64;
}

@Override
public boolean isUseableByPlayer(EntityPlayer player) {
	if(this.worldObj.getTileEntity(this.pos) != this) return false;
	final double X_CENTRE_OFFSET = 0.5;
	final double Y_CENTRE_OFFSET = 0.5;
	final double Z_CENTRE_OFFSET = 0.5;
	final double MAXIMUM_DISTANCE_SQ = 8.0 * 8.0;
	return player.getDistanceSq(pos.getX() + X_CENTRE_OFFSET, pos.getY() + Y_CENTRE_OFFSET, pos.getZ() + Z_CENTRE_OFFSET) < MAXIMUM_DISTANCE_SQ;
}

@Override
public boolean isItemValidForSlot(int index, ItemStack stack) {
	return true;
}

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

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

	compound.setTag("Items", dataForAllSlots);
}

@Override
public void readFromNBT(NBTTagCompound compound) {
	super.readFromNBT(compound);
	final byte NBT_TYPE_COMPOUND = 10;
	NBTTagList dataForAllSlots = compound.getTagList("Items", NBT_TYPE_COMPOUND);

	Arrays.fill(itemStacks, null);
	for(int i = 0; i < dataForAllSlots.tagCount(); i++) {
		NBTTagCompound dataForOneSlot = dataForAllSlots.getCompoundTagAt(i);
		int slotIndex = dataForOneSlot.getByte("Slot") & 255;

		if(slotIndex >= 0 && slotIndex < this.itemStacks.length) {
			this.itemStacks[slotIndex] = ItemStack.loadItemStackFromNBT(dataForOneSlot);
		}
	}
}

@Override
public void clear() {
	Arrays.fill(itemStacks, null);
}

@Override
public String getName() {
	return "container.crate.name";
}

@Override
public boolean hasCustomName() {
	return false;
}

@Override
public ITextComponent getDisplayName() {
	return this.hasCustomName() ? new TextComponentString(this.getName()) : new TextComponentTranslation(this.getName());
}

@Override
public ItemStack removeStackFromSlot(int slotIndex) {
	ItemStack itemStack = getStackInSlot(slotIndex);
	if(itemStack != null) setInventorySlotContents(slotIndex, null);
	return itemStack;
}

@Override
public void openInventory(EntityPlayer player) {}

@Override
public void closeInventory(EntityPlayer player) {}

@Override
public int getField(int id) {
	return 0;
}

@Override
public void setField(int id, int value) {}

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

Link to comment
Share on other sites

Thanks for reply. Did what you said but it still doesn't open up.

 

Updated code:

@Mod(modid = References.MODID, version = References.VERSION, name = References.NAME, guiFactory="com.gwater.decorationmod.handler.GuiHandlerCrate")

 

Have a console report also:

[15:49:58] [Client thread/FATAL]: Error executing task
java.util.concurrent.ExecutionException: java.lang.ClassCastException: com.gwater.decorationmod.container.ContainerCrate cannot be cast to net.minecraft.client.gui.GuiScreen
at java.util.concurrent.FutureTask.report(FutureTask.java:122) ~[?:1.8.0_77]
at java.util.concurrent.FutureTask.get(FutureTask.java:192) ~[?:1.8.0_77]
at net.minecraft.util.Util.runTask(Util.java:24) [util.class:?]
at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1104) [Minecraft.class:?]
at net.minecraft.client.Minecraft.run(Minecraft.java:401) [Minecraft.class:?]
at net.minecraft.client.main.Main.main(Main.java:118) [Main.class:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_77]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_77]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_77]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_77]
at net.minecraft.launchwrapper.Launch.launch(Launch.java:135) [launchwrapper-1.12.jar:?]
at net.minecraft.launchwrapper.Launch.main(Launch.java:28) [launchwrapper-1.12.jar:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_77]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_77]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_77]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_77]
at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97) [start/:?]
at GradleStart.main(GradleStart.java:26) [start/:?]
Caused by: java.lang.ClassCastException: com.gwater.decorationmod.container.ContainerCrate cannot be cast to net.minecraft.client.gui.GuiScreen
at net.minecraftforge.fml.client.FMLClientHandler.showGuiScreen(FMLClientHandler.java:471) ~[FMLClientHandler.class:?]
at net.minecraftforge.fml.common.FMLCommonHandler.showGuiScreen(FMLCommonHandler.java:309) ~[FMLCommonHandler.class:?]
at net.minecraftforge.fml.common.network.internal.FMLNetworkHandler.openGui(FMLNetworkHandler.java:103) ~[FMLNetworkHandler.class:?]
at net.minecraft.entity.player.EntityPlayer.openGui(EntityPlayer.java:2694) ~[EntityPlayer.class:?]
at net.minecraftforge.fml.common.network.internal.OpenGuiHandler.process(OpenGuiHandler.java:39) ~[OpenGuiHandler.class:?]
at net.minecraftforge.fml.common.network.internal.OpenGuiHandler.access$000(OpenGuiHandler.java:15) ~[OpenGuiHandler.class:?]
at net.minecraftforge.fml.common.network.internal.OpenGuiHandler$1.run(OpenGuiHandler.java:30) ~[OpenGuiHandler$1.class:?]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) ~[?:1.8.0_77]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) ~[?:1.8.0_77]
at net.minecraft.util.Util.runTask(Util.java:23) ~[util.class:?]
... 15 more

Link to comment
Share on other sites

Thanks coolAliaz. I was in fact returning container for client-side.

 

Updated code:

@Override
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
	if(ID != getGuiID()) {
		System.err.println("Invalid ID: expected " + getGuiID() + ", received " + ID);
	}

	BlockPos xyz = new BlockPos(x, y, z);
	TileEntity tileEntity = world.getTileEntity(xyz);
	if(tileEntity instanceof TileEntityCrate) {
		TileEntityCrate tileEntityCrate = (TileEntityCrate)tileEntity;
		return new GuiCrate(player.inventory, tileEntityCrate);
	}
	return null;
}

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.