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

Hello, I'm trying to use a class that implements IExtendedEntityProperties to add a currency to the player. I initialize the class when the player entity is constructed like so:

@ForgeSubscribe(priority = EventPriority.NORMAL)
public void onEntityConstuct(EntityConstructing event) {
	if (event.entity instanceof EntityPlayerMP) {
		EntityPlayerMP ep = (EntityPlayerMP) event.entity;
		ep.registerExtendedProperties(PlayerProperties.IDENTIFIER, new PlayerProperties());
	}
}

 

The PlayerProperties class looks like so:

public class PlayerProperties implements IExtendedEntityProperties {

public static final String IDENTIFIER = "playerproperties";

public static PlayerProperties fromPlayer(EntityPlayerMP player) {
	return (PlayerProperties) player.getExtendedProperties(IDENTIFIER);
}

int credits = 0;

EntityPlayerMP player;

@Override
public void saveNBTData(NBTTagCompound compound) {
	compound.setInteger("credits", credits);
}

@Override
public void loadNBTData(NBTTagCompound compound) {
	if (compound.hasKey("credits")) {
		this.credits = compound.getInteger("credits");
	} else {
		this.credits = 30;
	}
	sendPacket();
}

@Override
public void init(Entity entity, World world) {
	player = (EntityPlayerMP) entity;
}

private void sendPacket() {
	PacketDispatcher.sendPacketToPlayer(new PropertiesPacket(credits).makePacket(), (Player) player);
}
}

 

Where my PropertiesPacket class looks like so:

public class PropertiesPacket extends BasePacket {

int credits;

public PropertiesPacket(int credits) {
	this.credits = credits;
}

public PropertiesPacket() {

}

@Override
public void write(ByteArrayDataOutput out) {
	out.writeInt(credits);
}

@Override
public void read(ByteArrayDataInput in) {
	credits = in.readInt();
}

@Override
public void execute(EntityPlayer player, Side side) {
	if (side.isClient()) {
		PlayerData.credits = credits; // PlayerData is just a static client side class which stores the data.
	}
}

}

 

Where the BasePacket class looks like this:

public abstract class BasePacket {

public static final String CHANNEL = "MyPacket";
private static final BiMap<Integer, Class<? extends BasePacket>> idMap;

static {
	ImmutableBiMap.Builder<Integer, Class<? extends BasePacket>> builder = ImmutableBiMap.builder();

	builder.put(Integer.valueOf(0), PropertiesPacket.class);

	idMap = builder.build();
}

public static BasePacket constructPacket(int packetId) throws ProtocolException, ReflectiveOperationException {
	Class<? extends BasePacket> clazz = idMap.get(Integer.valueOf(packetId));
	if (clazz == null) {
		throw new ProtocolException("Unknown Packet Id!");
	} else {
		return clazz.newInstance();
	}
}

public static class ProtocolException extends Exception {

	public ProtocolException() {
	}

	public ProtocolException(String message, Throwable cause) {
		super(message, cause);
	}

	public ProtocolException(String message) {
		super(message);
	}

	public ProtocolException(Throwable cause) {
		super(cause);
	}
}

public final int getPacketId() {
	if (idMap.inverse().containsKey(getClass())) {
		return idMap.inverse().get(getClass()).intValue();
	} else {
		throw new RuntimeException("Packet " + getClass().getSimpleName() + " is missing a mapping!");
	}
}

public final Packet makePacket() {
	ByteArrayDataOutput out = ByteStreams.newDataOutput();
	out.writeByte(getPacketId());
	write(out);
	return PacketDispatcher.getPacket(CHANNEL, out.toByteArray());
}

public abstract void write(ByteArrayDataOutput out);

public abstract void read(ByteArrayDataInput in);

public abstract void execute(EntityPlayer player, Side side);
}

 

and my packet handler is as so:

public class PacketHandler implements IPacketHandler {

@Override
public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player) {
	try {
		EntityPlayer entityPlayer = (EntityPlayer) player;
		ByteArrayDataInput in = ByteStreams.newDataInput(packet.data);
		int packetId = in.readUnsignedByte();
		BasePacket basePacket = BasePacket.constructPacket(packetId);
		basePacket.read(in);
		basePacket.execute(entityPlayer, entityPlayer.worldObj.isRemote ? Side.CLIENT : Side.SERVER);
	} catch (ProtocolException e) {
		if (player instanceof EntityPlayerMP) {
			((EntityPlayerMP) player).playerNetServerHandler.kickPlayerFromServer("Protocol Exception!");
			Logger.getLogger("DemoMod").warning("Player " + ((EntityPlayer) player).username + " caused a Protocol Exception!");
		}
	} catch (ReflectiveOperationException e) {
		throw new RuntimeException("Unexpected Reflection exception during Packet construction!", e);
	}
}
}

However, when this code is run, it throws a null pointer exception and the packet isn't sent. Every time I've used this method of sending packets previously it's worked a charm, but this time it doesn't.

 

The stack trace is:

2013-09-11 21:30:37 [iNFO] [sTDERR] java.lang.NullPointerException
2013-09-11 21:30:37 [iNFO] [sTDERR] 	at cpw.mods.fml.common.network.PacketDispatcher.sendPacketToPlayer(PacketDispatcher.java:45)
2013-09-11 21:30:37 [iNFO] [sTDERR] 	at mods.SaberMod.SaberValues.sendPacket(SaberValues.java:147)
2013-09-11 21:30:37 [iNFO] [sTDERR] 	at mods.SaberMod.SaberValues.loadNBTData(SaberValues.java:76)
2013-09-11 21:30:37 [iNFO] [sTDERR] 	at net.minecraft.entity.Entity.readFromNBT(Entity.java:1647)
2013-09-11 21:30:37 [iNFO] [sTDERR] 	at net.minecraft.server.management.ServerConfigurationManager.readPlayerDataFromFile(ServerConfigurationManager.java:239)
2013-09-11 21:30:37 [iNFO] [sTDERR] 	at net.minecraft.server.management.ServerConfigurationManager.initializeConnectionToPlayer(ServerConfigurationManager.java:113)
2013-09-11 21:30:37 [iNFO] [sTDERR] 	at net.minecraft.server.integrated.IntegratedServerListenThread.networkTick(IntegratedServerListenThread.java:97)
2013-09-11 21:30:37 [iNFO] [sTDERR] 	at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:689)
2013-09-11 21:30:37 [iNFO] [sTDERR] 	at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:585)
2013-09-11 21:30:37 [iNFO] [sTDERR] 	at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:129)
2013-09-11 21:30:37 [iNFO] [sTDERR] 	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:482)
2013-09-11 21:30:37 [iNFO] [sTDERR] 	at net.minecraft.server.ThreadMinecraftServer.run(ThreadMinecraftServer.java:16)

 

which points to this line in the packet dispatcher:

public static void sendPacketToPlayer(Packet packet, Player player)
    {
        if (player instanceof EntityPlayerMP)
        {
            ((EntityPlayerMP)player).playerNetServerHandler.sendPacketToPlayer(packet); //<-- this one
        }
    }

This suggests the player in its own variables is wrong, the player's missing the server handler or the packet is null. I'm assuming the packet is null, but I don't see why this one shouldn't work while the others do. What is the likely problem here?

  • Author

How do I use breakpoints, I've inserted one on the line, generated the exception, but cannot find the values of variables etc. (Using eclipse IDE)

  • Author

I've applied a conditional breakpoint for if the server handler is null and it is. How can I send the information to the player?

  • Author

Yeah, thanks for that it really cleared up a load of my code and helped me get much more organized :) I'll look into that now :)

 

EDIT: thanks this has now worked for me :)

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.