Jump to content

[SOLVED] KeyHandler crashed on ServerPacketHandler load


lorizz

Recommended Posts

Hey, everytime I try to run my mod on the server, where the server need to use PacketHandler for send packets from the client to the server in a KeyHandler class, it crashes, the game doesn't close because it's a Server Crash not a Client Crash... D:

 

ServerPacketHandler:

 

package mods.exoworld.handler;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;

import mods.exoworld.event.ExoArmorPowerup;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet250CustomPayload;
import cpw.mods.fml.common.network.IPacketHandler;
import cpw.mods.fml.common.network.Player;

public class ExoServerPacketHandler implements IPacketHandler {

public static String[] Chanels;

public static final String ARMORPOWERUP = "ExoArmorPowerup";

// Before adding it remember to add it also in chanels, in network mod
// attribute

@Override
public void onPacketData(INetworkManager manager,
		Packet250CustomPayload packet, Player player) {

	if (packet.channel.equals(ARMORPOWERUP)) {
		ExoArmorPowerup.handlePacket(packet, (EntityPlayer) player);

	}

}

}

 

KeyHandler:

 

package mods.exoworld.handler;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.util.EnumSet;
import java.util.Random;

import mods.exoworld.event.ExoArmorPowerup;
import net.minecraft.client.entity.EntityClientPlayerMP;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.network.packet.Packet250CustomPayload;

import org.lwjgl.input.Keyboard;

import cpw.mods.fml.client.registry.KeyBindingRegistry.KeyHandler;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.TickType;
import cpw.mods.fml.relauncher.Side;

public class ExoServerKeyHandler extends KeyHandler {

public ExoArmorPowerup eventArmorPowerup;
public EntityPlayer player;

public static KeyBinding armorPowerup = new KeyBinding("Armor's Powerup",
		Keyboard.KEY_F);

public static KeyBinding[] arrayOfKeys = new KeyBinding[] { armorPowerup };
public static boolean[] areRepeating = new boolean[] { false };

public ExoServerKeyHandler() {
	super(arrayOfKeys, areRepeating);
}

@Override
public String getLabel() {
	return "ExotiumWorld's ServerKeyHandler";
}

@Override
public void keyDown(EnumSet<TickType> types, KeyBinding kb,
		boolean tickEnd, boolean isRepeat) {
	if(kb.keyCode == armorPowerup.keyCode) {
		if(tickEnd)
		 {
			 System.out.println("Key up");
			 eventArmorPowerup.sendPacket();
		 }
	}
}

@Override
public void keyUp(EnumSet<TickType> types, KeyBinding kb, boolean tickEnd) {
}

// FireModeHandler.switchFireMode();
@Override
public EnumSet<TickType> ticks() {
	return EnumSet.of(TickType.CLIENT);
}

}

 

EventHandler:

 

package mods.exoworld.event;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.util.Random;

import mods.exoworld.handler.ExoServerPacketHandler;
import mods.exoworld.handler.ExoServerTickHandler;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityClientPlayerMP;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.network.packet.Packet250CustomPayload;
import net.minecraft.world.World;
import cpw.mods.fml.client.FMLClientHandler;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.relauncher.Side;

public class ExoArmorPowerup {

public static ExoServerTickHandler event;
public static EntityPlayer player;
public static World world;

public static void sendPacket() {
	EntityPlayer player = Minecraft.getMinecraft().thePlayer;
	ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
	DataOutputStream dataOutputStream = new DataOutputStream(
			byteOutputStream);
	try {
		dataOutputStream.writeInt(player.entityId);
	} catch (Exception e) {
		e.printStackTrace();
	}
	Packet250CustomPayload packet = new Packet250CustomPayload(
			ExoServerPacketHandler.ARMORPOWERUP,
			byteOutputStream.toByteArray());

	FMLClientHandler.instance().sendPacket(packet);
}

public static void handlePacket(Packet250CustomPayload packet,
		EntityPlayer player2) {
	// TODO Auto-generated method stub
	if (event.powerupIsAvailable == true) {
		world.createExplosion(null, player.posX + 5, player.posY,
				player.posZ, 5.0F, false);
		world.createExplosion(null, player.posX - 5, player.posY,
				player.posZ, 5.0F, false);
		world.createExplosion(null, player.posX, player.posY,
				player.posZ + 5, 5.0F, false);
		world.createExplosion(null, player.posX, player.posY,
				player.posZ - 5, 5.0F, false);
		event.powerupIsAvailable = false;
	}
}
}

 

The ServerPacketHandler is registered in the MainMod class like this:

 

@NetworkMod(clientSideRequired = true, serverSideRequired = false, channels = { ExoServerPacketHandler.ARMORPOWERUP
}, packetHandler = ExoServerPacketHandler.class)

 

Any help? Thanks to all!

Link to comment
Share on other sites

ServerKeyHandler doesn't really make sense as a name.  :P

Anyway, most errors are here:

public class ExoArmorPowerup {

public static ExoServerTickHandler event;
public static EntityPlayer player;
public static World world;// i don't see the initialization of this field, but it is useless anyway

public static void sendPacket() {
	EntityPlayer player = Minecraft.getMinecraft().thePlayer;//This is client side only method, meaning player field is now client side only
//prefer using this method in your keyhandler, and passing the argument in the method
	ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
	DataOutputStream dataOutputStream = new DataOutputStream(
			byteOutputStream);
	try {
		dataOutputStream.writeInt(player.entityId);//this info is unused when handling the packet ??
	} catch (Exception e) {
		e.printStackTrace();
	}
	Packet250CustomPayload packet = new Packet250CustomPayload(
			ExoServerPacketHandler.ARMORPOWERUP,
			byteOutputStream.toByteArray());

	FMLClientHandler.instance().sendPacket(packet);
}

public static void handlePacket(Packet250CustomPayload packet,
		EntityPlayer player2) {
	// TODO Auto-generated method stub
	if (event.powerupIsAvailable == true) {//you are using client-only player field while being on server side -> crash
//instead use the player argument from the method
//prefer using player.worldObj instead of your world field
		world.createExplosion(null, player.posX + 5, player.posY,
				player.posZ, 5.0F, false);
		world.createExplosion(null, player.posX - 5, player.posY,
				player.posZ, 5.0F, false);
		world.createExplosion(null, player.posX, player.posY,
				player.posZ + 5, 5.0F, false);
		world.createExplosion(null, player.posX, player.posY,
				player.posZ - 5, 5.0F, false);
		event.powerupIsAvailable = false;
	}
}
}

Link to comment
Share on other sites

ServerKeyHandler doesn't really make sense as a name.  :P

Anyway, most errors are here:

public class ExoArmorPowerup {

public static ExoServerTickHandler event;
public static EntityPlayer player;
public static World world;// i don't see the initialization of this field, but it is useless anyway

public static void sendPacket() {
	EntityPlayer player = Minecraft.getMinecraft().thePlayer;//This is client side only method, meaning player field is now client side only
//prefer using this method in your keyhandler, and passing the argument in the method
	ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
	DataOutputStream dataOutputStream = new DataOutputStream(
			byteOutputStream);
	try {
		dataOutputStream.writeInt(player.entityId);//this info is unused when handling the packet ??
	} catch (Exception e) {
		e.printStackTrace();
	}
	Packet250CustomPayload packet = new Packet250CustomPayload(
			ExoServerPacketHandler.ARMORPOWERUP,
			byteOutputStream.toByteArray());

	FMLClientHandler.instance().sendPacket(packet);
}

public static void handlePacket(Packet250CustomPayload packet,
		EntityPlayer player2) {
	// TODO Auto-generated method stub
	if (event.powerupIsAvailable == true) {//you are using client-only player field while being on server side -> crash
//instead use the player argument from the method
//prefer using player.worldObj instead of your world field
		world.createExplosion(null, player.posX + 5, player.posY,
				player.posZ, 5.0F, false);
		world.createExplosion(null, player.posX - 5, player.posY,
				player.posZ, 5.0F, false);
		world.createExplosion(null, player.posX, player.posY,
				player.posZ + 5, 5.0F, false);
		world.createExplosion(null, player.posX, player.posY,
				player.posZ - 5, 5.0F, false);
		event.powerupIsAvailable = false;
	}
}
}

 

I tried doing this, but I have no idea on how to do that, I tried using EntityPlayerMP in each class, EntityPlayer and EntityPlayerMP both but nothing D: can you try to fix it at your own?

Link to comment
Share on other sites

public class ExoArmorPowerup {

public static ExoServerTickHandler event;

public static void sendPacket(EntityPlayer player) {
	ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
	DataOutputStream dataOutputStream = new DataOutputStream(
			byteOutputStream);
	try {
		dataOutputStream.writeInt(player.entityId);//this info is unused when handling the packet ??
	} catch (Exception e) {
		e.printStackTrace();
	}
	Packet250CustomPayload packet = new Packet250CustomPayload(
			ExoServerPacketHandler.ARMORPOWERUP,
			byteOutputStream.toByteArray());

	FMLClientHandler.instance().sendPacket(packet);
}

public static void handlePacket(Packet250CustomPayload packet,
		EntityPlayer player) {
	// TODO Auto-generated method stub
	if (event.powerupIsAvailable == true) {
		player.worldObj.createExplosion(null, player.posX + 5, player.posY,
				player.posZ, 5.0F, false);
		player.worldObj.createExplosion(null, player.posX - 5, player.posY,
				player.posZ, 5.0F, false);
		player.worldObj.createExplosion(null, player.posX, player.posY,
				player.posZ + 5, 5.0F, false);
		player.worldObj.createExplosion(null, player.posX, player.posY,
				player.posZ - 5, 5.0F, false);
		event.powerupIsAvailable = false;
	}
}
}

public class ExoServerKeyHandler extends KeyHandler {

public static KeyBinding armorPowerup = new KeyBinding("Armor's Powerup",
		Keyboard.KEY_F);

public static KeyBinding[] arrayOfKeys = new KeyBinding[] { armorPowerup };
public static boolean[] areRepeating = new boolean[] { false };

public ExoServerKeyHandler() {
	super(arrayOfKeys, areRepeating);
}

@Override
public String getLabel() {
	return "ExotiumWorld's ServerKeyHandler";
}

@Override
public void keyDown(EnumSet<TickType> types, KeyBinding kb,
		boolean tickEnd, boolean isRepeat) {
	if(kb.keyCode == armorPowerup.keyCode) {
		if(tickEnd)
		 {
			 System.out.println("Key up");
			 ExoArmorPowerup.sendPacket(Minecraft.getMinecraft().thePlayer);
		 }
	}
}

@Override
public void keyUp(EnumSet<TickType> types, KeyBinding kb, boolean tickEnd) {
}

// FireModeHandler.switchFireMode();
@Override
public EnumSet<TickType> ticks() {
	return EnumSet.of(TickType.CLIENT);
}

}

Link to comment
Share on other sites

public class ExoArmorPowerup {

public static ExoServerTickHandler event;

public static void sendPacket(EntityPlayer player) {
	ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
	DataOutputStream dataOutputStream = new DataOutputStream(
			byteOutputStream);
	try {
		dataOutputStream.writeInt(player.entityId);//this info is unused when handling the packet ??
	} catch (Exception e) {
		e.printStackTrace();
	}
	Packet250CustomPayload packet = new Packet250CustomPayload(
			ExoServerPacketHandler.ARMORPOWERUP,
			byteOutputStream.toByteArray());

	FMLClientHandler.instance().sendPacket(packet);
}

public static void handlePacket(Packet250CustomPayload packet,
		EntityPlayer player) {
	// TODO Auto-generated method stub
	if (event.powerupIsAvailable == true) {
		player.worldObj.createExplosion(null, player.posX + 5, player.posY,
				player.posZ, 5.0F, false);
		player.worldObj.createExplosion(null, player.posX - 5, player.posY,
				player.posZ, 5.0F, false);
		player.worldObj.createExplosion(null, player.posX, player.posY,
				player.posZ + 5, 5.0F, false);
		player.worldObj.createExplosion(null, player.posX, player.posY,
				player.posZ - 5, 5.0F, false);
		event.powerupIsAvailable = false;
	}
}
}

public class ExoServerKeyHandler extends KeyHandler {

public static KeyBinding armorPowerup = new KeyBinding("Armor's Powerup",
		Keyboard.KEY_F);

public static KeyBinding[] arrayOfKeys = new KeyBinding[] { armorPowerup };
public static boolean[] areRepeating = new boolean[] { false };

public ExoServerKeyHandler() {
	super(arrayOfKeys, areRepeating);
}

@Override
public String getLabel() {
	return "ExotiumWorld's ServerKeyHandler";
}

@Override
public void keyDown(EnumSet<TickType> types, KeyBinding kb,
		boolean tickEnd, boolean isRepeat) {
	if(kb.keyCode == armorPowerup.keyCode) {
		if(tickEnd)
		 {
			 System.out.println("Key up");
			 ExoArmorPowerup.sendPacket(Minecraft.getMinecraft().thePlayer);
		 }
	}
}

@Override
public void keyUp(EnumSet<TickType> types, KeyBinding kb, boolean tickEnd) {
}

// FireModeHandler.switchFireMode();
@Override
public EnumSet<TickType> ticks() {
	return EnumSet.of(TickType.CLIENT);
}

}

 

Error on this statement:

if (event.powerupIsAvailable == true) { //java.lang.NullPointerException
		player.worldObj.createExplosion(null, player.posX + 5, player.posY,
				player.posZ, 5.0F, false);
		player.worldObj.createExplosion(null, player.posX - 5, player.posY,
				player.posZ, 5.0F, false);
		player.worldObj.createExplosion(null, player.posX, player.posY,
				player.posZ + 5, 5.0F, false);
		player.worldObj.createExplosion(null, player.posX, player.posY,
				player.posZ - 5, 5.0F, false);
		event.powerupIsAvailable = false;
	}

And there:

ExoArmorPowerup.handlePacket(packet, (EntityPlayer) player); //java.lang.NullPointerException

Link to comment
Share on other sites

You never showed that ExoServerTickHandler class. I assumed it was working.

Anyway, this is another issue, which is probably easy to fix for you.

 

There is it:

package mods.exoworld.handler;

import java.util.EnumSet;

import mods.exoworld.Main;
import mods.exoworld.event.ExoArmorPowerup;
import mods.exoworld.item.ExoTNTHelmet;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import cpw.mods.fml.client.FMLClientHandler;
import cpw.mods.fml.common.ITickHandler;
import cpw.mods.fml.common.TickType;

public class ExoServerTickHandler implements ITickHandler {

public float counter = 49.0F;
public boolean powerupIsAvailable = false;
public ExoKeyHandler keyHandler;

public void onPlayerTick(EntityPlayer player) {
	ItemStack helmet = player.inventory.armorItemInSlot(3);
	ItemStack chestplate = player.inventory.armorItemInSlot(2);
	ItemStack leggings = player.inventory.armorItemInSlot(1);
	ItemStack boots = player.inventory.armorItemInSlot(0);
	if (player.inventory.armorItemInSlot(2) != null
			&& player.inventory.armorItemInSlot(1) != null
			&& player.inventory.armorItemInSlot(0) != null
			&& helmet.itemID == Main.ExoTNTHelmet.itemID
			&& chestplate.itemID == Main.ExoTNTChestplate.itemID
			&& leggings.itemID == Main.ExoTNTLeggings.itemID
			&& boots.itemID == Main.ExoTNTBoots.itemID) {
		player.fallDistance = 0.0F;
		System.out.println(counter);
		counter += 0.021F;
		if (counter > 50F && powerupIsAvailable == false) {
			player.sendChatToPlayer("ExoModChat: La tua armatura è possente! Premi F per scatenare la tua furia!");
			powerupIsAvailable = true;
		}
		if (powerupIsAvailable == true) {
			counter -= 5F;
			if (counter < 0F) {
				counter = 0F;
			}
		}
	}
}

@Override
public void tickStart(EnumSet<TickType> type, Object... tickData) {
	// TODO Auto-generated method stub
	if (type.equals(EnumSet.of(TickType.PLAYER))) {
		onPlayerTick((EntityPlayer) tickData[0]);
	}

}

@Override
public void tickEnd(EnumSet<TickType> type, Object... tickData) {
	// TODO Auto-generated method stub
}

@Override
public EnumSet<TickType> ticks() {
	// TODO Auto-generated method stub
	return EnumSet.of(TickType.PLAYER, TickType.SERVER);
}

@Override
public String getLabel() {
	// TODO Auto-generated method stub
	return "ExotiumWorld's ServerTickHandler";
}

}

 

I FIXED IT!

 

I just put "static" on the boolean variable and in the counter variable, I excepted that ahaha thanks! +1 for the rest help!

Link to comment
Share on other sites

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.