Jump to content

Recommended Posts

Posted

Kind of following the tutorial on Containers and GUI, only really changing the names of certain things so that when I dig down into the code and start experimenting I can turn it into my final product.  I'm pretty sure I copied everything right, but I might not have.  Anyway I'm getting a null pointer exception, and not really sure where it is coming from. 

 

Main Mod

 

package summoner;

import summoner.blocks.FusionTable;
import summoner.items.PigEssence;
import summoner.tileentities.TileEntityFusionTable;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.MinecraftForge;
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.NetworkMod;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.registry.GameRegistry;

@Mod(modid="SummonerModID", name = "Summoner", version = "0.0.0")
@NetworkMod(clientSideRequired=true, serverSideRequired=false)
public class Summoner {

@Instance(value = "SummonerModID")
public static Summoner instance;

@SidedProxy(clientSide="summoner.client.ClientProxy", serverSide="summoner.CommonProxy")
public static CommonProxy proxy;

@EventHandler
public void preInit(FMLPreInitializationEvent event){
	//Stub Method
}

@EventHandler
public void load(FMLInitializationEvent event){
	proxy.registerRenderers();
	MinecraftForge.EVENT_BUS.register(new SummonerLivingDrops());
	GameRegistry.registerBlock(fusionTable, "fusiontable");
	GameRegistry.registerTileEntity(TileEntityFusionTable.class, "tileentityfusiontable");
	NetworkRegistry.instance().registerGuiHandler(this, guihandler);
	GameRegistry.addShapelessRecipe(new ItemStack(fusionTable), new ItemStack(Block.dirt));
}

@EventHandler
public void postInit(FMLPostInitializationEvent event){
	//Stub Method
}

public static final Block fusionTable = new FusionTable(500);
public static final SummonerGuiHandler guihandler = new SummonerGuiHandler();
}

 

 

Block

 

package summoner.blocks;

import summoner.Summoner;
import summoner.tileentities.TileEntityFusionTable;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Icon;
import net.minecraft.world.World;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class FusionTable extends BlockContainer {

public FusionTable(int id) {
	super(id, Material.rock);
	setUnlocalizedName("fusiontable");
}

@SideOnly(Side.CLIENT)
public static Icon topIcon;
@SideOnly(Side.CLIENT)
public static Icon bottomIcon;
@SideOnly(Side.CLIENT)
public static Icon sideIcon;

@Override
@SideOnly(Side.CLIENT)
public void registerIcons(IconRegister icon){
	topIcon = icon.registerIcon("summoner:fusiontabletop");
	bottomIcon = icon.registerIcon("summoner:fusiontabletop");
	sideIcon = icon.registerIcon("summoner:fusiontableside");
}

@Override
@SideOnly(Side.CLIENT)
public Icon getIcon(int side, int metadata){
	if (side == 0){
		return bottomIcon;
	} else if (side == 1){
		return topIcon;
	} else {
		return sideIcon;
	}
}

@Override
public void onBlockAdded(World world, int i, int j, int k){
	super.onBlockAdded(world, i, j, k);
	world.markBlockForUpdate(i, j, k);
}

@Override
public void onBlockClicked(World world, int x, int y, int z, EntityPlayer player){
	super.onBlockClicked(world, x, y, z, player);
	System.out.println("Clicked a Fusion Table");
}

@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player,
		 int metadata, float par7, float par8, float par9){
	//System.out.println("Activated a Fusion Table");

	TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
	if (tileEntity == null || player.isSneaking()){
		return false;
	}

	player.openGui(Summoner.instance, 0, world, x, y, z);
	return true;
}

@Override
public TileEntity createNewTileEntity(World world) {
	// TODO Auto-generated method stub
	return new TileEntityFusionTable();
}

}

 

 

Tile Entity

 

package summoner.tileentities;

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 TileEntityFusionTable extends TileEntity implements IInventory {

private ItemStack[] inv;

public TileEntityFusionTable(){
	inv = new ItemStack[9];
}

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

	NBTTagList tagList = par1.getTagList("Inventory");
	for (int i = 0; i < tagList.tagCount(); i ++){
		NBTTagCompound tag = (NBTTagCompound)tagList.tagAt(i);
		byte slot = tag.getByte("Slot");
		if (slot >= 0 && slot < inv.length){
			inv[slot] = ItemStack.loadItemStackFromNBT(tag);
		}
	}
}

@Override
public void readFromNBT(NBTTagCompound par1){
	super.readFromNBT(par1);

	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);
		}
		par1.setTag("Inventory", itemList);
	}
}

@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 getInvName() {
	return "summoner.tileentites.tileentityfusiontable";
}

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

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

@Override
public void openChest() {

}

@Override
public void closeChest() {

}

@Override
public boolean isInvNameLocalized() {
	// TODO Auto-generated method stub
	return false;
}

@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack) {
	// TODO Auto-generated method stub
	return false;
}


}

 

 

Container

 

package summoner.containers;

import summoner.tileentities.TileEntityFusionTable;
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 FusionTableContainer extends Container {

protected TileEntityFusionTable tileEntity;

public FusionTableContainer(InventoryPlayer inventoryPlayer, TileEntityFusionTable 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, 18 + i * 18));
		}
	}

	bindPlayerInventory(inventoryPlayer);
}

@Override
public boolean canInteractWith(EntityPlayer entityplayer) {
	// TODO Auto-generated method stub
	return tileEntity.isUseableByPlayer(entityplayer);
}

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

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

}

 

 

Gui

 

package summoner.gui;

import org.lwjgl.opengl.GL11;

import summoner.containers.FusionTableContainer;
import summoner.tileentities.TileEntityFusionTable;

import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.StatCollector;

public class FusionTableGUI extends GuiContainer {

public FusionTableGUI(InventoryPlayer inventoryPlayer, TileEntityFusionTable tileEntity) {
	super(new FusionTableContainer(inventoryPlayer, tileEntity));
}

@Override
protected void drawGuiContainerForegroundLayer(int param1, int param2) {
	fontRenderer.drawString("Fusion Table", 8, 6, 4210752);
	fontRenderer.drawString(StatCollector.translateToLocal("container.inventory"), 8, ySize - 96 + 2, 4210752);
}

@Override
protected void drawGuiContainerBackgroundLayer(float f, int i, int j) {
	// TODO Auto-generated method stub
	//GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
	this.mc.renderEngine.bindTexture(guiTest);
	int x = (width - xSize) / 2;
	int y = (height - ySize) / 2;
	this.drawTexturedModalRect(x, y, 0, 0, xSize, ySize);
}

private ResourceLocation guiTest = new ResourceLocation("gui/guiTest.png");



}

 

 

There were two methods in the Tile Entity that were implemented but not covered in the tutorial, but I'm pretty sure they aren't being called.  My texture path might not be right, but I also tried "textures/gui/guiTest.png" and "summoner/textures/gui/guiTest.png".  I'm not extremely Java savy, but I know my way around.  Mostly I just mess with code to figure out how it works (when it works).  Any help is greatly appreciated.  Thank you.

Posted

Where is the null pointer exception?  This is key to solving the problem.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

Don't know what was wrong, but I found a much better tutorial for creating custom containers:

 

http://www.minecraftforum.net/topic/1959857-162-advanced-minecraft-forge-modding-tutorial-2-inventories/

 

more of a step by step of what the different methods are doing rather than the "here is the code" of the other tutorial I found.  Works like a charm, and I'm ready to start tweaking it to do what I want it to do :)

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.