Jump to content

[1.7.2]Simple 1-slot chest/Container returns ClassCastException


Recommended Posts

Posted

I've just started learning modding for Minecraft(have been for 3 days). I'd say I'm close to intermediate when it comes to Java(been programming w/ it for about 8 months constantly). I have been trying to create, basically, a chest, from an outdated tutorial(which also didn't really explain the code). I got all of the error part out of the code(as in there is no red squiggle under the code), and when I click on the block to open it, it quickly displays the GUI, then the game immediately crashes.

 

Sorry if this seems like a "noob" question or anything, I am pretty beginner at this

 

EDIT: Can a mod help me out with this format? It's messing up for me

 

 

Stack trace:

 

 

 

---- Minecraft Crash Report ----

// I feel sad now :(

 

 

Time: 2/27/14 6:56 PM

Description: Ticking memory connection

 

 

java.lang.ClassCastException: tutorial.basic.BasicGui cannot be cast to net.minecraft.inventory.Container

  at cpw.mods.fml.common.network.NetworkRegistry.getRemoteGuiContainer(NetworkRegistry.java:241)

  at cpw.mods.fml.common.network.internal.FMLNetworkHandler.openGui(FMLNetworkHandler.java:75)

  at net.minecraft.entity.player.EntityPlayer.openGui(EntityPlayer.java:2476)

  at tutorial.basic.BasicContainerBlock.onBlockActivated(BasicContainerBlock.java:40)

  at net.minecraft.server.management.ItemInWorldManager.activateBlockOrUseItem(ItemInWorldManager.java:411)

  at net.minecraft.network.NetHandlerPlayServer.processPlayerBlockPlacement(NetHandlerPlayServer.java:596)

  at net.minecraft.network.play.client.C08PacketPlayerBlockPlacement.processPacket(C08PacketPlayerBlockPlacement.java:74)

  at net.minecraft.network.play.client.C08PacketPlayerBlockPlacement.processPacket(C08PacketPlayerBlockPlacement.java:122)

  at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:242)

  at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:190)

  at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:762)

  at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:650)

  at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:120)

  at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:528)

  at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:787)

 

 

 

 

A detailed walkthrough of the error, its code path and all known details is as follows:

---------------------------------------------------------------------------------------

 

 

-- Head --

Stacktrace:

  at cpw.mods.fml.common.network.NetworkRegistry.getRemoteGuiContainer(NetworkRegistry.java:241)

  at cpw.mods.fml.common.network.internal.FMLNetworkHandler.openGui(FMLNetworkHandler.java:75)

  at net.minecraft.entity.player.EntityPlayer.openGui(EntityPlayer.java:2476)

  at tutorial.basic.BasicContainerBlock.onBlockActivated(BasicContainerBlock.java:40)

  at net.minecraft.server.management.ItemInWorldManager.activateBlockOrUseItem(ItemInWorldManager.java:411)

  at net.minecraft.network.NetHandlerPlayServer.processPlayerBlockPlacement(NetHandlerPlayServer.java:596)

  at net.minecraft.network.play.client.C08PacketPlayerBlockPlacement.processPacket(C08PacketPlayerBlockPlacement.java:74)

  at net.minecraft.network.play.client.C08PacketPlayerBlockPlacement.processPacket(C08PacketPlayerBlockPlacement.java:122)

  at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:242)

 

 

-- Ticking connection --

Details:

  Connection: net.minecraft.network.NetworkManager@3f54e121

Stacktrace:

  at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:190)

  at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:762)

  at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:650)

  at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:120)

  at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:528)

  at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:787)

 

 

-- System Details --

Details:

  Minecraft Version: 1.7.2

  Operating System: Windows 7 (amd64) version 6.1

  Java Version: 1.7.0_21, Oracle Corporation

  Java VM Version: Java HotSpot 64-Bit Server VM (mixed mode), Oracle Corporation

  Memory: 927474744 bytes (884 MB) / 1038876672 bytes (990 MB) up to 1038876672 bytes (990 MB)

  JVM Flags: 3 total; -Xincgc -Xmx1024M -Xms1024M

  AABB Pool Size: 3621 (202776 bytes; 0 MB) allocated, 3370 (188720 bytes; 0 MB) used

  IntCache: cache: 0, tcache: 0, allocated: 0, tallocated: 0

  FML: MCP v9.01-pre FML v7.2.116.1024 Minecraft Forge 10.12.0.1024 7 mods loaded, 7 mods active

  mcp{8.09} [Minecraft Coder Pack] (minecraft.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available

  FML{7.2.116.1024} [Forge Mod Loader] (forgeSrc-1.7.2-10.12.0.1024.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available

  Forge{10.12.0.1024} [Minecraft Forge] (forgeSrc-1.7.2-10.12.0.1024.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available

  examplemod{1.0} [Example Mod] (bin) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available

  Animilia{0.0.0} [Animilia] (bin) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available

  Basic{0.0.1} [basic] (bin) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available

  GenericModID{0.0.0} [Generic] (bin) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available

  Profiler Position: N/A (disabled)

  Vec3 Pool Size: 545 (30520 bytes; 0 MB) allocated, 422 (23632 bytes; 0 MB) used

  Player Count: 1 / 8; [EntityPlayerMP['Player584'/170, l='Test', x=-382.49, y=4.00, z=44.73]]

  Type: Integrated Server (map_client.txt)

  Is Modded: Definitely; Client brand changed to 'fml,forge'

 

 

 

My code:

 

Main Class

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.Mod.Instance;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.registry.GameRegistry;


@Mod(modid="Basic", name="Basic", version="0.0.1")
public class Basic {
   public static Block basicOre = new BasicBlock(Material.ground);
   public static Item basicDust = new BasicDust();
   public static Block basicContainer = new BasicContainerBlock(Material.rock);
   
   @Instance(value = "Basic")
   public static Basic instance;
   
   @SidedProxy(clientSide = "tutorial.basic.ClientProxy", serverSide = "tutorial.basic.CommonProxy")
   public static CommonProxy proxy;
   
   @EventHandler
   public void preInit(FMLPreInitializationEvent event) {
      proxy.registerRenderers();
      
      GameRegistry.registerBlock(basicOre, "basicOre");
      GameRegistry.registerItem(basicDust, "basicDust");
      
      GameRegistry.registerBlock(basicContainer, ItemBlock.class, "basicContainer");
      GameRegistry.registerTileEntity(BasicTileEntity.class, "basicContainer");
      NetworkRegistry.INSTANCE.registerGuiHandler(this, new BasicGuiHandler());
   }
   
   @EventHandler
   public void load(FMLInitializationEvent event) {
      
   }
   
   @EventHandler
   public void postInit(FMLPostInitializationEvent event) {
      
   }
}

 

BasicContainer

 



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;


public class BasicContainer extends Container {
   protected BasicTileEntity tileEntity;
   
   public BasicContainer(InventoryPlayer inventoryPlayer, BasicTileEntity te) {
      tileEntity = te;
      
      for(int i = 0; i < 3; i++) {
         for(int j = 0; j < 3; j++) {
            addSlotToContainer(new Slot(tileEntity, j + i * 3, 62 + j * 18, 17 + i * 18));
         }
      }
      
      bindPlayerInventory(inventoryPlayer);
   }
   
   @Override
   public boolean canInteractWith(EntityPlayer var1) {
      return false;
   }
   
   protected void bindPlayerInventory(InventoryPlayer inventoryPlayer) {
      for(int i = 0; i < 3; i++) {
         for(int j = 0; j < 9; j++) {
            addSlotToContainer(new Slot(inventoryPlayer, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
         }
      }
      
      for(int i = 0; i < 9; i++) {
         addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 142));
      }
   }
   
   //For shift-clicking, without this, it would cause massive errors
   @Override
   public ItemStack transferStackInSlot(EntityPlayer player, int slot) {
      ItemStack stack = null;
      Slot slotObject = (Slot) inventorySlots.get(slot);
      
      //null checks and checks if the item can be stacked (maxStackSize > 1)
      if(slotObject != null && slotObject.getHasStack()) {
         ItemStack stackInSlot = slotObject.getStack();
         stack = stackInSlot.copy();
         
          //Merges the item into player inventory since its in the tileEntity
         if(slot < 9) {
            if(!this.mergeItemStack(stackInSlot, 0, 35, true)) {
               return null;
               
            }
            
         //places it into the tileEntity is possible since its in the player inventory   
         } else if(!this.mergeItemStack(stackInSlot, 0, 9, false)) {
            return null;
            
         }
         
         if(stackInSlot.stackSize == 0) {
            slotObject.putStack(null);
            
         } else {
            slotObject.onSlotChanged();
            
         }
         
         if(stackInSlot.stackSize == stack.stackSize) {
            return null;
         }
         
         slotObject.onPickupFromSlot(player, stackInSlot);
      }
      
      return stack;
   }
}

BasicContainerBlock

 

import java.util.Random;


import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.creativetab.CreativeTabs;
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.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;


public class BasicContainerBlock extends BlockContainer {


   protected BasicContainerBlock(Material material) {
      super(material);
      setHardness(2.0f);
      setResistance(5.0f);
      setBlockName("basicContainer");
      setCreativeTab(CreativeTabs.tabDecorations);
      
   }


   @Override
   public TileEntity createNewTileEntity(World var1, int var2) {
      return new BasicTileEntity();
   }
   
   @Override
   public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int metadata, float what, float these, float are) {
      TileEntity tileEntity = world.getTileEntity(x, y, z);
      if(tileEntity == null || player.isSneaking()) {
         return false;
      }
      
      player.openGui(Basic.instance, 0, world, x, y, z);
      return true;
   }
   
   @Override
   public void breakBlock(World world, int x, int y, int z, Block block, int par6) {
      dropItems(world, x, y, z);
      super.breakBlock(world, x, y, z, block, par6);
   }
   
   private void dropItems(World world, int x, int y, int z) {
      Random rand = new Random();
      
      TileEntity tileEntity = world.getTileEntity(x, y, z);
      if(!(tileEntity instanceof IInventory)) {
         return;
      }
      
      IInventory inventory = (IInventory) tileEntity;
      
      for(int i = 0; i < inventory.getSizeInventory(); i++) {
         ItemStack item = inventory.getStackInSlot(i);
         
         if(item != null && item.stackSize > 0) {
            float rx = rand.nextFloat() * 0.8f + 0.1f;
            float ry = rand.nextFloat() * 0.8f + 0.1f;
            float rz = rand.nextFloat() * 0.8f + 0.1f;
            
            EntityItem entityItem = new EntityItem(world, x + rx, y + ry, z + rz, new ItemStack(item.getItem(), item.stackSize, item.getItemDamage()));
            
            if(item.hasTagCompound()) {
               entityItem.getEntityItem().setTagCompound((NBTTagCompound) item.getTagCompound().copy());
               
            }
            
            float factor = 0.05f;
            entityItem.motionX = rand.nextGaussian() * factor;
            entityItem.motionY = rand.nextGaussian() * factor + 0.2F;
                entityItem.motionZ = rand.nextGaussian() * factor;
                world.spawnEntityInWorld(entityItem);
                item.stackSize = 0;
                
          }
      }
   }
}

BasicGui

 

import org.lwjgl.opengl.GL11;


import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.texture.ITextureObject;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.StatCollector;


public class BasicGui extends GuiContainer {
   
   public BasicGui(InventoryPlayer inventoryPlayer, BasicTileEntity tileEntity) {
      super(new BasicContainer(inventoryPlayer, tileEntity));
   }
   
   @Override
   protected void drawGuiContainerForegroundLayer(int param1, int param2) { //Draw text and stuff here
      fontRendererObj.drawString("Basic", 8, 6, 4210752); //params are string, x, y, color
      fontRendererObj.drawString(StatCollector.translateToLocal("container.inventory"), 8, ySize - 96 + 2, 4210752); //draws "Inventory
      
   }


   @Override
   protected void drawGuiContainerBackgroundLayer(float par1, int par2, int par3) { 
      GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
      this.mc.renderEngine.bindTexture(new ResourceLocation("basic:textures/gui/basicGui.png"));
      int x = (width - xSize) /2;
      int y = (height - ySize) /2;
      this.drawTexturedModalRect(x, y, 0, 0, xSize, ySize);
   }
}

BasicGuiHandler

 

[/size]



import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import cpw.mods.fml.common.network.IGuiHandler;


public class BasicGuiHandler implements IGuiHandler {


   @Override
   public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
      TileEntity tileEntity = world.getTileEntity(x, y, z);
      if(tileEntity instanceof BasicTileEntity) {
         return new BasicGui(player.inventory, (BasicTileEntity) tileEntity);
      }
      
      return null;
   }


   @Override
   public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
      TileEntity tileEntity = world.getTileEntity(x, y, z);
      if(tileEntity instanceof BasicTileEntity) {
         return new BasicGui(player.inventory, (BasicTileEntity) tileEntity);
         
      }
      return null;
   }
}

[/size]

 

 

BasicTileEntity

 

 

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;


public class BasicTileEntity extends TileEntity implements IInventory{
   public int customField;
   private ItemStack[] inv;
   
   public BasicTileEntity() {
      inv = new ItemStack[9];
   }
   
   @Override
   public void writeToNBT(NBTTagCompound tagCompound) {
      super.writeToNBT(tagCompound);
      
      NBTTagList itemList = new NBTTagList();
      for(int i = 0; i < inv.length; i++) {
         ItemStack stack = inv[i];
         if(stack != null) {
            NBTTagCompound tag = new NBTTagCompound();
            tag.setByte("Slot", (byte) i);
            stack.writeToNBT(tag);
            itemList.appendTag(tag);
         }
      }
      tagCompound.setTag("Inventory", itemList);
   }
   
   @Override
   public void readFromNBT(NBTTagCompound tagCompound) {
      super.readFromNBT(tagCompound);
      
      NBTTagList tagList = tagCompound.getTagList("Inventory", 0);
      for(int i = 0; i < tagList.tagCount(); i++) {
         NBTTagCompound tag = tagList.getCompoundTagAt(i);
         byte slot = tag.getByte("Slot");
         if(slot >= 0 && slot < inv.length) {
            inv[slot] = ItemStack.loadItemStackFromNBT(tag);
         }
      }
   }


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


   @Override
   public ItemStack getStackInSlot(int slot) {
      return inv[slot];
   }


   @Override
   public ItemStack decrStackSize(int slot, int amt) {
      ItemStack stack = getStackInSlot(slot);
      if(stack != null) {
         if(stack.stackSize <= amt) {
            setInventorySlotContents(slot, null);
         } else {
            stack = stack.splitStack(amt);
            if(stack.stackSize == 0) {
               setInventorySlotContents(slot, null);
               
            }
         }
      }
      return stack;
   }


   @Override
   public ItemStack getStackInSlotOnClosing(int slot) {
      ItemStack stack = getStackInSlot(slot);
      if(stack != null) {
         setInventorySlotContents(slot, null);
         
      }
      
      return stack;
   }


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


   @Override
   public String getInventoryName() {
      return "basic.BasicTileEntity";
   }


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


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


   @Override
   public boolean isUseableByPlayer(EntityPlayer player) {
      return worldObj.getTileEntity(xCoord, yCoord, zCoord) == this && player.getDistanceSq(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5) < 64;
   }


   @Override
   public void openInventory() {
      
   }


   @Override
   public void closeInventory() {
      
   }


   @Override
   public boolean isItemValidForSlot(int var1, ItemStack var2) {
      return false;
   }
}

Posted

In your GuiHandler:

 @Override
   public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
      TileEntity tileEntity = world.getTileEntity(x, y, z);
      if(tileEntity instanceof BasicTileEntity) {
         return new BasicGui(player.inventory, (BasicTileEntity) tileEntity);
      }
      
      return null;
   }

You're returning a gui on the server side, but you need to return a container class.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Posted

In your GuiHandler:

 @Override
   public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
      TileEntity tileEntity = world.getTileEntity(x, y, z);
      if(tileEntity instanceof BasicTileEntity) {
         return new BasicGui(player.inventory, (BasicTileEntity) tileEntity);
      }
      
      return null;
   }

You're returning a gui on the server side, but you need to return a container class.

 

So should I do something like

 

return new BasicContainer(player.inventory, (BasicTileEntity) tileEntity);

 

in the getServerGuiElemet method?

Posted

Yeah, that should work.

 

Do I replace the

return new BasicGui(player.inventory, (BasicTileEntity) tileEntity) 

 

with the thing I suggested? because I can only return one or the other

 

Also, if I wanted to say create a backpack, how on right click with the backpack item, it would open a chest inventory and store the contents in the item, would I only need a IGuiHandler and a GuiContainer, or those 2 and a container?

Posted

Yeah, that should work.

 

Do I replace the

return new BasicGui(player.inventory, (BasicTileEntity) tileEntity) 

 

with the thing I suggested? because I can only return one or the other

 

Also, if I wanted to say create a backpack, how on right click with the backpack item, it would open a chest inventory and store the contents in the item, would I only need a IGuiHandler and a GuiContainer, or those 2 and a container?

That's the line you need to replace.

 

I haven't done anything with backpack type stuff, but i know what you need:

Something with IInventory

A GuiContainer class

A Container class

A GuiHandler (you already got one)

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Posted

Yeah, that should work.

 

Do I replace the

return new BasicGui(player.inventory, (BasicTileEntity) tileEntity) 

 

with the thing I suggested? because I can only return one or the other

 

Also, if I wanted to say create a backpack, how on right click with the backpack item, it would open a chest inventory and store the contents in the item, would I only need a IGuiHandler and a GuiContainer, or those 2 and a container?

That's the line you need to replace.

 

I haven't done anything with backpack type stuff, but i know what you need:

Something with IInventory

A GuiContainer class

A Container class

A GuiHandler (you already got one)

 

Now when I right-click the block, it opens the gui then immediately closes it. Any suggestions?

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.