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:
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;
}
}