Implemented a 'small' packet system for those mods that need to send rapid, small data packets and who are concered about the overhead of the 250 payload packet. It uses packet 131.

This commit is contained in:
LexManos committed 2012-04-18 19:40:48 -07:00
1 parent f4fc50b3c5
commit 86fd9e0b9c
8 files changed
+95 -53

No files matched your search

@@ -24,16 +24,4 @@ public class mod_MinecraftForge extends NetworkMod
MinecraftForge.getDungeonLootTries(); //Random thing to make things Initialize
MinecraftForgeClient.init();
}
@Override
public boolean clientSideRequired()
{
return false;
}
@Override
public boolean serverSideRequired()
{
return false;
}
}
@@ -21,6 +21,7 @@ import net.minecraft.src.ModLoader;
import net.minecraft.src.NBTTagCompound;
import net.minecraft.src.NetworkManager;
import net.minecraft.src.Packet;
import net.minecraft.src.Packet131MapData;
import net.minecraft.src.Packet1Login;
import net.minecraft.src.Packet250CustomPayload;
import net.minecraft.src.World;
@@ -32,6 +33,7 @@ import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.*;
import java.util.logging.Level;
public class ForgeHooks
{
@@ -659,5 +661,17 @@ public class ForgeHooks
{
return forgePacketHandler;
}
public static boolean onItemDataPacket(NetworkManager net, Packet131MapData pkt)
{
NetworkMod mod = MinecraftForge.getModByID(pkt.itemID);
if (mod == null)
{
ModLoader.getLogger().log(Level.WARNING, String.format("Received Unknown MapData packet %d:%d", pkt.itemID, pkt.uniqueID));
return false;
}
mod.onPacketData(net, pkt.uniqueID, pkt.itemData);
return true;
}
}
@@ -15,6 +15,7 @@ import net.minecraft.src.ItemStack;
import net.minecraft.src.ModLoader;
import net.minecraft.src.NetworkManager;
import net.minecraft.src.Packet;
import net.minecraft.src.Packet131MapData;
import net.minecraft.src.World;
import java.util.*;
@@ -1134,6 +1135,35 @@ public class MinecraftForge
ForgeHooks.getPacketHandler().sendPacket(net, packet);
}
/**
* Sends a 'small' payload packet to the specified manager.
* It uses the Packet131MapData packet for it's communication
* so things are limited.
*
* @param net The manager to send the packet to
* @param mod The mod associated with this packet
* @param id The ID number used to identify this packet
* @param data The data to be sent, must be no larger then 255 bytes.
*/
public static void sendPacket(NetworkManager net, NetworkMod mod, short id, byte[] data)
{
if (data == null)
{
data = new byte[0];
}
if (data.length > 255)
{
throw new IllegalArgumentException(String.format("Data argument was to long, must not be longer then 255 bytes was %d", data.length));
}
Packet131MapData pkt = new Packet131MapData();
pkt.itemID = (short)getModID(mod);
pkt.uniqueID = id;
pkt.itemData = data;
sendPacket(net, pkt);
}
private static int isClient = -1;
public static boolean isClient()
{
@@ -1,6 +1,7 @@
package net.minecraft.src.forge;
import net.minecraft.src.BaseMod;
import net.minecraft.src.NetworkManager;
/**
* This is for mods that are designed to be used on the server side alone,
@@ -14,12 +15,32 @@ public abstract class NetworkMod extends BaseMod
* mod installed when it is installed on a server.
* @return True if client is required, false if not.
*/
public abstract boolean clientSideRequired();
public boolean clientSideRequired()
{
return false;
}
/**
* Returns true if the server is required to have this
* mod when it is installed on the client.
* @return True if server is required, false if not.
*/
public abstract boolean serverSideRequired();
public boolean serverSideRequired()
{
return false;
}
/**
* Called when the 'small' data packet is received for this NetworkMod,
* Internally, this utilizes the Packet131MapData packet. This is useful
* data that is sent rapidly and would like to save the overhead of the
* 250 custom payload packet.
*
* Data is limited to 256 bytes.
*
* @param net
* @param id
* @param data
*/
public void onPacketData(NetworkManager net, short id, byte[] data){}
}
@@ -1,25 +0,0 @@
package net.minecraft.src.forge;
import net.minecraft.src.BaseMod;
/**
* This is for mods that are designed to be used on the server side alone,
* or both the client and server side. This class is used when registering
* various things relating to network traffic. Entity ID's, GUI Id's, etc..
*/
public abstract class NetworkMod extends BaseMod
{
/**
* Returns true if every client is required to have this
* mod installed when it is installed on a server.
* @return True if client is required, false if not.
*/
public abstract boolean clientSideRequired();
/**
* Returns true if the server is required to have this
* mod when it is installed on the client.
* @return True if server is required, false if not.
*/
public abstract boolean serverSideRequired();
}
@@ -1,5 +1,7 @@
package net.minecraft.src;
import java.util.Set;
import net.minecraft.src.forge.ForgeHooks;
import net.minecraft.src.forge.MinecraftForge;
import net.minecraft.src.forge.NetworkMod;
@@ -26,20 +28,15 @@ public class mod_MinecraftForge extends NetworkMod
{
if (mod instanceof NetworkMod)
{
if (x == Item.map.shiftedIndex)
{
x++;
}
ForgeHooks.networkMods.put(x++, (NetworkMod)mod);
}
}
//Add 131 to C->S list
((Set)ModLoader.getPrivateValue(Packet.class, null, 3)).add(131);
}
@Override
public boolean clientSideRequired()
{
return false;
}
@Override
public boolean serverSideRequired()
{
return false;
}
}
@@ -108,7 +108,18 @@
}
else
{
@@ -1114,5 +1172,40 @@
@@ -1012,6 +1070,10 @@
{
ItemMap.getMPMapData(par1Packet131MapData.uniqueID, this.mc.theWorld).func_28171_a(par1Packet131MapData.itemData);
}
+ else if (ForgeHooks.onItemDataPacket(netManager, par1Packet131MapData))
+ {
+ ;
+ }
else
{
System.out.println("Unknown itemid: " + par1Packet131MapData.uniqueID);
@@ -1114,5 +1176,40 @@
public void handleCustomPayload(Packet250CustomPayload var1)
{
ModLoader.receivePacket(var1);
@@ -102,7 +102,7 @@
}
}
@@ -969,9 +985,50 @@
@@ -969,9 +985,56 @@
{
this.playerEntity.capabilities.isFlying = par1Packet202PlayerAbilities.isFlying && this.playerEntity.capabilities.allowFlying;
}
@@ -154,5 +154,11 @@
+ {
+ inst.dispatchIncomingMessage(netManager, pkt.channel, pkt.data);
+ }
+ }
+
+ @Override
+ public void handleMapData(Packet131MapData par1Packet131MapData)
+ {
+ ForgeHooks.onItemDataPacket(netManager, par1Packet131MapData);
}
}