Created new framework to use json command packets instead of standard binary packets.

This commit is contained in:
ProfMobius committed 2014-03-17 15:37:59 +01:00
1 parent 1df480c15d
commit 705d665d1e
4 files changed
+240

No files matched your search

@@ -0,0 +1,50 @@
package mcp.mobius.opis.network.json;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import cpw.mods.fml.common.network.PacketDispatcher;
import net.minecraft.network.packet.Packet250CustomPayload;
public class CommandPacket {
public static CommandPayload getCommand(Packet250CustomPayload packet) {
DataInputStream istream = new DataInputStream(new ByteArrayInputStream(packet.data));
CommandPayload payload = CommandPayload.readFromStream(istream);
return payload;
}
private static Packet250CustomPayload create(CommandPayload payload){
Packet250CustomPayload packet = new Packet250CustomPayload();
ByteArrayOutputStream bos = new ByteArrayOutputStream(1);
DataOutputStream ostream = new DataOutputStream(bos);
payload.writeToStream(ostream);
packet.channel = "Opis";
packet.data = bos.toByteArray();
packet.length = bos.size();
return packet;
}
public static void sendCommand(OpisCommand command){
CommandPacket.sendCommand(command, null);
}
public static void sendCommand(OpisCommand command, Object[] params){
Packet250CustomPayload packet = null;
if (params == null)
packet = CommandPacket.create(new CommandPayload(command));
else
packet = CommandPacket.create(new CommandPayload(command, params));
PacketDispatcher.sendPacketToServer(packet);
}
}
@@ -0,0 +1,67 @@
package mcp.mobius.opis.network.json;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import mcp.mobius.opis.modOpis;
import mcp.mobius.opis.tools.NBTUtil;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagString;
import com.google.gson.Gson;
public class CommandPayload {
private OpisCommand command;
private Object[] params;
public CommandPayload(OpisCommand command, Object ... params){
this.command = command;
this.params = params;
}
public CommandPayload(OpisCommand command){
this.command = command;
this.params = new Object[0];
}
public OpisCommand getCommand(){
return this.command;
}
public Object[] getParams(){
return this.params;
}
public NBTTagCompound toNBT(){
Gson gson = new Gson();
NBTTagCompound tag = new NBTTagCompound();
tag.setString("payload", gson.toJson(this));
return tag;
}
public void writeToStream(DataOutputStream ostream){
try {
NBTUtil.writeNBTTagCompound(this.toNBT(), ostream);
} catch (Exception e) {
modOpis.log.warning(String.format("Error while writing command packet to stream for sending. COMMAND : %s", this.command));
e.printStackTrace();
}
}
public static CommandPayload readFromStream(DataInputStream istream){
try {
Gson gson = new Gson();
NBTTagCompound tag = NBTUtil.readNBTTagCompound(istream);
String jsonStr = tag.getString("payload");
CommandPayload commandPacket = gson.fromJson(jsonStr, CommandPayload.class);
return commandPacket;
} catch (Exception e) {
modOpis.log.warning("Error while reading command packet from stream.");
e.printStackTrace();
}
return new CommandPayload(OpisCommand.INVALID);
}
}
@@ -0,0 +1,5 @@
package mcp.mobius.opis.network.json;
public enum OpisCommand {
INVALID;
}
+118
View File
@@ -0,0 +1,118 @@
package mcp.mobius.opis.tools;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.HashSet;
import java.util.logging.Level;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagByte;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagInt;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTTagLong;
import net.minecraft.nbt.NBTTagShort;
import net.minecraft.nbt.NBTTagString;
public class NBTUtil {
public static NBTBase getTag(String key, NBTTagCompound tag){
String[] path = key.split("\\.");
NBTTagCompound deepTag = tag;
for (String i : path){
if (deepTag.hasKey(i)){
if (deepTag.getTag(i) instanceof NBTTagCompound)
deepTag = deepTag.getCompoundTag(i);
else{
return deepTag.getTag(i);
}
} else {
return null;
}
}
return deepTag;
}
public static NBTTagCompound setTag(String key, NBTTagCompound targetTag, NBTBase addedTag){
String[] path = key.split("\\.");
NBTTagCompound deepTag = targetTag;
for (int i = 0; i < path.length - 1; i++){
if (!deepTag.hasKey(path[i]))
deepTag.setCompoundTag(path[i], new NBTTagCompound());
deepTag = deepTag.getCompoundTag(path[i]);
}
deepTag.setTag(path[path.length - 1], addedTag);
return targetTag;
}
public static NBTTagCompound createTag(NBTTagCompound inTag, HashSet<String> keys){
if (keys.contains("*")) return inTag;
NBTTagCompound outTag = new NBTTagCompound();
for (String key : keys){
NBTBase tagToAdd = getTag(key, inTag);
//System.out.printf("%s\n", tagToAdd);
if (tagToAdd != null)
outTag = setTag(key, outTag, tagToAdd);
}
return outTag;
}
public static void writeNBTTagCompound(NBTTagCompound par0NBTTagCompound, DataOutputStream ostream) throws IOException
{
if (par0NBTTagCompound == null)
{
ostream.writeShort(-1);
}
else
{
byte[] abyte = CompressedStreamTools.compress(par0NBTTagCompound);
if (abyte.length > 32000)
ostream.writeShort(-1);
else{
ostream.writeShort((short)abyte.length);
ostream.write(abyte);
}
}
}
public static NBTTagCompound readNBTTagCompound(DataInputStream istream) throws IOException
{
short short1 = istream.readShort();
if (short1 < 0)
{
return null;
}
else
{
byte[] abyte = new byte[short1];
istream.readFully(abyte);
return CompressedStreamTools.decompress(abyte);
}
}
public static int getNBTInteger(NBTTagCompound tag, String keyname){
NBTBase subtag = tag.getTag(keyname);
if (subtag instanceof NBTTagInt)
return tag.getInteger(keyname);
if (subtag instanceof NBTTagShort)
return tag.getShort(keyname);
if (subtag instanceof NBTTagByte)
return tag.getByte(keyname);
return 0;
}
}