Rework packet reading a little in more preparation for Forge.

This commit is contained in:
md_5 2013-02-12 12:05:06 +11:00
parent d17c457040
commit d2a919fc06
2 changed files with 101 additions and 9 deletions

View File

@ -1,10 +1,11 @@
package net.md_5.bungee.packet;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import java.io.DataInput;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import lombok.Delegate;
@ -15,7 +16,7 @@ import net.md_5.bungee.Util;
* subclasses can read and write to the backing byte array which can be
* retrieved via the {@link #getPacket()} method.
*/
public abstract class DefinedPacket implements DataInput, DataOutput
public abstract class DefinedPacket implements DataOutput
{
private interface Overriden
@ -25,8 +26,8 @@ public abstract class DefinedPacket implements DataInput, DataOutput
void writeUTF(String s);
}
@Delegate(excludes = Overriden.class)
private ByteArrayDataInput in;
private ByteArrayInputStream bin;
private DataInputStream input;
@Delegate(excludes = Overriden.class)
private ByteArrayDataOutput out;
/**
@ -40,7 +41,8 @@ public abstract class DefinedPacket implements DataInput, DataOutput
public DefinedPacket(int id, byte[] buf)
{
in = ByteStreams.newDataInput( buf );
bin = new ByteArrayInputStream( buf );
input = new DataInputStream( bin );
if ( readUnsignedByte() != id )
{
throw new IllegalArgumentException( "Wasn't expecting packet id " + Util.hex( id ) );
@ -74,7 +76,6 @@ public abstract class DefinedPacket implements DataInput, DataOutput
writeChars( s );
}
@Override
public String readUTF()
{
short len = readShort();
@ -100,6 +101,88 @@ public abstract class DefinedPacket implements DataInput, DataOutput
return ret;
}
public final int available()
{
return bin.available();
}
public final void readFully(byte b[])
{
try
{
input.readFully( b );
} catch ( IOException e )
{
throw new IllegalStateException( e );
}
}
public final boolean readBoolean()
{
try
{
return input.readBoolean();
} catch ( IOException e )
{
throw new IllegalStateException( e );
}
}
public final byte readByte()
{
try
{
return input.readByte();
} catch ( IOException e )
{
throw new IllegalStateException( e );
}
}
public final int readUnsignedByte()
{
try
{
return input.readUnsignedByte();
} catch ( IOException e )
{
throw new IllegalStateException( e );
}
}
public final short readShort()
{
try
{
return input.readShort();
} catch ( IOException e )
{
throw new IllegalStateException( e );
}
}
public final char readChar()
{
try
{
return input.readChar();
} catch ( IOException e )
{
throw new IllegalStateException( e );
}
}
public final int readInt()
{
try
{
return input.readInt();
} catch ( IOException e )
{
throw new IllegalStateException( e );
}
}
@Override
public abstract boolean equals(Object obj);

View File

@ -11,7 +11,7 @@ public class Packet1Login extends DefinedPacket
public int entityId;
public String levelType;
public byte gameMode;
public byte dimension;
public int dimension;
public byte difficulty;
public byte unused;
public byte maxPlayers;
@ -34,7 +34,16 @@ public class Packet1Login extends DefinedPacket
this.entityId = readInt();
this.levelType = readUTF();
this.gameMode = readByte();
this.dimension = readByte();
if ( available() == 4 )
{
this.dimension = readByte();
} else if ( available() == 7 )
{
this.dimension = readInt();
} else
{
throw new IllegalStateException();
}
this.difficulty = readByte();
this.unused = readByte();
this.maxPlayers = readByte();