Fixed server crash on startup. Fixes #176 #183 #186

This commit is contained in:
Adubbz 2014-02-11 06:17:07 +11:00
parent 8732497a24
commit 33b90334b4
3 changed files with 30 additions and 26 deletions

View File

@ -88,7 +88,7 @@ public class BiomesOPlenty
@EventHandler @EventHandler
public void load(FMLInitializationEvent event) public void load(FMLInitializationEvent event)
{ {
packetPipeline.initalize(); packetPipeline.initialize();
TreecapitatorIntegration.init(); TreecapitatorIntegration.init();

View File

@ -13,7 +13,6 @@ public class CreativeTabsBOP extends CreativeTabs
} }
@Override @Override
//TODO: public ItemStack getIconItemStack()
public ItemStack getIconItemStack() public ItemStack getIconItemStack()
{ {
return new ItemStack(BOPItemHelper.get("food"), 1, 7); return new ItemStack(BOPItemHelper.get("food"), 1, 7);

View File

@ -1,25 +1,29 @@
package biomesoplenty.common.network; package biomesoplenty.common.network;
import java.util.*;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageCodec; import io.netty.handler.codec.MessageToMessageCodec;
import java.util.Collections;
import java.util.Comparator;
import java.util.EnumMap;
import java.util.LinkedList;
import java.util.List;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.network.INetHandler; import net.minecraft.network.INetHandler;
import net.minecraft.network.NetHandlerPlayServer; import net.minecraft.network.NetHandlerPlayServer;
import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.network.FMLEmbeddedChannel; import cpw.mods.fml.common.network.FMLEmbeddedChannel;
import cpw.mods.fml.common.network.FMLOutboundHandler; import cpw.mods.fml.common.network.FMLOutboundHandler;
import cpw.mods.fml.common.network.NetworkRegistry; import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.network.internal.FMLProxyPacket; import cpw.mods.fml.common.network.internal.FMLProxyPacket;
import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
/** /**
* Packet pipeline class. Directs all registered packet data to be handled by the packets themselves. * Packet pipeline class. Directs all registered packet data to be handled by the packets themselves.
@ -30,8 +34,8 @@ import cpw.mods.fml.relauncher.Side;
public class PacketPipeline extends MessageToMessageCodec<FMLProxyPacket, AbstractPacket> public class PacketPipeline extends MessageToMessageCodec<FMLProxyPacket, AbstractPacket>
{ {
private EnumMap<Side, FMLEmbeddedChannel> channels; private EnumMap<Side, FMLEmbeddedChannel> channels;
private LinkedList<Class<? extends AbstractPacket>> packets = new LinkedList<Class<? extends AbstractPacket>>(); private LinkedList<Class<? extends AbstractPacket>> packets = new LinkedList<Class<? extends AbstractPacket>>();
private boolean isPostInitialised = false; private boolean isPostInitialized = false;
/** /**
* Register your packet with the pipeline. Discriminators are automatically set. * Register your packet with the pipeline. Discriminators are automatically set.
@ -42,8 +46,7 @@ public class PacketPipeline extends MessageToMessageCodec<FMLProxyPacket, Abstra
*/ */
public boolean registerPacket(Class<? extends AbstractPacket> clazz) public boolean registerPacket(Class<? extends AbstractPacket> clazz)
{ {
if (this.packets.size() > 256) if (this.packets.size() > 256) {
{
// You should log here!! // You should log here!!
return false; return false;
} }
@ -54,7 +57,7 @@ public class PacketPipeline extends MessageToMessageCodec<FMLProxyPacket, Abstra
return false; return false;
} }
if (this.isPostInitialised) if (this.isPostInitialized)
{ {
// You should log here!! // You should log here!!
return false; return false;
@ -70,8 +73,7 @@ public class PacketPipeline extends MessageToMessageCodec<FMLProxyPacket, Abstra
{ {
ByteBuf buffer = Unpooled.buffer(); ByteBuf buffer = Unpooled.buffer();
Class<? extends AbstractPacket> clazz = msg.getClass(); Class<? extends AbstractPacket> clazz = msg.getClass();
if (!this.packets.contains(msg.getClass())) if (!this.packets.contains(msg.getClass())) {
{
throw new NullPointerException("No Packet Registered for: " + msg.getClass().getCanonicalName()); throw new NullPointerException("No Packet Registered for: " + msg.getClass().getCanonicalName());
} }
@ -98,10 +100,9 @@ public class PacketPipeline extends MessageToMessageCodec<FMLProxyPacket, Abstra
pkt.decodeInto(ctx, payload.slice()); pkt.decodeInto(ctx, payload.slice());
EntityPlayer player; EntityPlayer player;
switch (FMLCommonHandler.instance().getEffectiveSide()) switch (FMLCommonHandler.instance().getEffectiveSide()) {
{
case CLIENT: case CLIENT:
player = Minecraft.getMinecraft().thePlayer; player = this.getClientPlayer();
pkt.handleClientSide(player); pkt.handleClientSide(player);
break; break;
@ -118,26 +119,25 @@ public class PacketPipeline extends MessageToMessageCodec<FMLProxyPacket, Abstra
} }
// Method to call from FMLInitializationEvent // Method to call from FMLInitializationEvent
public void initalize() public void initialize()
{ {
this.channels = NetworkRegistry.INSTANCE.newChannel("BiomeOPlenty", this); this.channels = NetworkRegistry.INSTANCE.newChannel("BiomesOPlenty", this);
} }
// Method to call from FMLPostInitializationEvent // Method to call from FMLPostInitializationEvent
// Ensures that packet discriminators are common between server and client by using logical sorting // Ensures that packet discriminators are common between server and client by using logical sorting
public void postInitialize() public void postInitialize()
{ {
if (this.isPostInitialised) if (this.isPostInitialized)
{ {
return; return;
} }
this.isPostInitialised = true; this.isPostInitialized = true;
Collections.sort(this.packets, new Comparator<Class<? extends AbstractPacket>>() Collections.sort(this.packets, new Comparator<Class<? extends AbstractPacket>>() {
{
@Override @Override
public int compare(Class<? extends AbstractPacket> clazz1, Class<? extends AbstractPacket> clazz2) public int compare(Class<? extends AbstractPacket> clazz1, Class<? extends AbstractPacket> clazz2) {
{
int com = String.CASE_INSENSITIVE_ORDER.compare(clazz1.getCanonicalName(), clazz2.getCanonicalName()); int com = String.CASE_INSENSITIVE_ORDER.compare(clazz1.getCanonicalName(), clazz2.getCanonicalName());
if (com == 0) { if (com == 0) {
com = clazz1.getCanonicalName().compareTo(clazz2.getCanonicalName()); com = clazz1.getCanonicalName().compareTo(clazz2.getCanonicalName());
@ -148,6 +148,12 @@ public class PacketPipeline extends MessageToMessageCodec<FMLProxyPacket, Abstra
}); });
} }
@SideOnly(Side.CLIENT)
private EntityPlayer getClientPlayer()
{
return Minecraft.getMinecraft().thePlayer;
}
/** /**
* Send this message to everyone. * Send this message to everyone.
* <p/> * <p/>
@ -155,8 +161,7 @@ public class PacketPipeline extends MessageToMessageCodec<FMLProxyPacket, Abstra
* *
* @param message The message to send * @param message The message to send
*/ */
public void sendToAll(AbstractPacket message) public void sendToAll(AbstractPacket message) {
{
this.channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.ALL); this.channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.ALL);
this.channels.get(Side.SERVER).writeAndFlush(message); this.channels.get(Side.SERVER).writeAndFlush(message);
} }
@ -218,4 +223,4 @@ public class PacketPipeline extends MessageToMessageCodec<FMLProxyPacket, Abstra
this.channels.get(Side.CLIENT).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.TOSERVER); this.channels.get(Side.CLIENT).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.TOSERVER);
this.channels.get(Side.CLIENT).writeAndFlush(message); this.channels.get(Side.CLIENT).writeAndFlush(message);
} }
} }