Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

Ok so i have a Tile Entity and block to go with it. I want one of my entities to set that block, then modify something in it's tile Entity. It keeps coming up with a NullPointerException.

 

Code:

 

Block:

 

package com.apmods.magicraft.block;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

public class BlockPortkey extends BlockBase{

public BlockPortkey(String name) {
	super(name);
	// TODO Auto-generated constructor stub
}

public boolean hasTileEntity(int metadata)
{
    return true;
}

public TileEntity createNewTileEntity(World par1World)
{
    try
    {
        return new TileEntityPortkey();
    }
    catch (Exception var3)
    {
        throw new RuntimeException(var3);
    }
}
public void setPortkeyCoords(EntityPlayer player, World world, int x, int y, int z, int x1, int y1, int z1){
	if(!world.isRemote){
		TileEntityPortkey p = (TileEntityPortkey)world.getTileEntity(x, y, z);
		p.setPortkeyCoords(world, x1, y1, z1);
	}
}
public boolean onBlockActivated(World world, int par2, int par3, int par4, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9)
    {
                if(!world.isRemote)
                {
                        TileEntityPortkey t = (TileEntityPortkey) world.getTileEntity(par2, par3, par4);
                        t.teleport(world, par5EntityPlayer);
                }
                return true;
    }

}

 

 

Relevant Entity code:

 

Block block = this.worldObj.getBlock(mop.blockX, mop.blockY, mop.blockZ);
   Material mat = this.worldObj.getBlock(mop.blockX, mop.blockY, mop.blockZ).getMaterial();
   BlockPortkey portkey = (BlockPortkey) BlockManager.portkey;
   portkey.createNewTileEntity(worldObj);
   this.worldObj.setBlock(mop.blockX, mop.blockY, mop.blockZ, portkey);
   System.out.println("setBlock");
   TileEntityPortkey p = (TileEntityPortkey) portkey.createNewTileEntity(worldObj);
   p.setPortkeyCoords(worldObj, this.getCoord(1), this.getCoord(2), this.getCoord(3));
   this.setDead();

 

 

TileEntity:

 

package com.apmods.magicraft.block;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

public class TileEntityPortkey extends TileEntity{
int prevx = 0;
int prevy = 0;
int prevz = 0;
 int x = 0;
 int y = 0;
 int z = 0;
@Override
public void readFromNBT(NBTTagCompound nbt)
{
    super.readFromNBT(nbt);
    this.x = nbt.getInteger("xcoord");
    this.y = nbt.getInteger("ycoord");
    this.z = nbt.getInteger("zcoord");
    this.prevx = nbt.getInteger("prevxcoord");
    this.prevy = nbt.getInteger("prevycoord");
    this.prevz = nbt.getInteger("prevzcoord");
}
@Override
public void writeToNBT(NBTTagCompound nbt)
{
    super.writeToNBT(nbt);
    nbt.setInteger("xcoord", x);
    nbt.setInteger("ycoord", y);
    nbt.setInteger("xcoord", z);
    nbt.setInteger("prevxcoord", prevx);
    nbt.setInteger("prevycoord", prevy);
    nbt.setInteger("prevzcoord", prevz);
}
public void setPortkeyCoords(World world, int x1, int y1, int z1) {
	x = x1;
	y = y1;
	z = z1;
	world.notifyBlockChange(x, y, z, BlockManager.portkey);

}
public void teleport(World world, EntityPlayer par5EntityPlayer) {
	if(world.isAirBlock(x, y, z)){
		prevx = (int) par5EntityPlayer.posX;
		prevy = (int) par5EntityPlayer.posY;
		prevz = (int) par5EntityPlayer.posZ;
		par5EntityPlayer.setPositionAndUpdate(x, y, z);
	}

}


}

 

 

Crash Report:

 

Caused by: java.lang.NullPointerException
at com.apmods.magicraft.block.BlockPortkey.onBlockActivated(BlockPortkey.java:41) ~[blockPortkey.class:?]
at net.minecraft.server.management.ItemInWorldManager.activateBlockOrUseItem(ItemInWorldManager.java:409) ~[itemInWorldManager.class:?]
at net.minecraft.network.NetHandlerPlayServer.processPlayerBlockPlacement(NetHandlerPlayServer.java:593) ~[NetHandlerPlayServer.class:?]
at net.minecraft.network.play.client.C08PacketPlayerBlockPlacement.processPacket(C08PacketPlayerBlockPlacement.java:74) ~[C08PacketPlayerBlockPlacement.class:?]
at net.minecraft.network.play.client.C08PacketPlayerBlockPlacement.processPacket(C08PacketPlayerBlockPlacement.java:122) ~[C08PacketPlayerBlockPlacement.class:?]
at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:241) ~[NetworkManager.class:?]
at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:182) ~[NetworkSystem.class:?]
... 5 more

 

 

Creator of the MyFit, MagiCraft, Tesseract gun, and Papa's Wingeria mod.

  • Author

Ahh thanks everyone! It works now!

Creator of the MyFit, MagiCraft, Tesseract gun, and Papa's Wingeria mod.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.