-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement functioning packet handler.
Tile entity updates are now handled by a custom packet.
- Loading branch information
Showing
8 changed files
with
351 additions
and
134 deletions.
There are no files selected for viewing
19 changes: 14 additions & 5 deletions
19
aestuscraft_common/dk/kiljacken/aestuscraft/core/proxy/ClientProxy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,14 @@ | ||
package dk.kiljacken.aestuscraft.core.proxy; | ||
|
||
public class ClientProxy extends CommonProxy { | ||
|
||
} | ||
package dk.kiljacken.aestuscraft.core.proxy; | ||
|
||
import cpw.mods.fml.client.FMLClientHandler; | ||
import net.minecraft.nbt.NBTTagCompound; | ||
import net.minecraft.tileentity.TileEntity; | ||
|
||
public class ClientProxy extends CommonProxy { | ||
@Override | ||
public void handleTileUpdate(int x, int y, int z, NBTTagCompound nbtTagCompound) { | ||
TileEntity tileEntity = FMLClientHandler.instance().getClient().theWorld.getBlockTileEntity(x, y, z); | ||
|
||
tileEntity.readFromNBT(nbtTagCompound); | ||
} | ||
} |
85 changes: 45 additions & 40 deletions
85
aestuscraft_common/dk/kiljacken/aestuscraft/core/proxy/CommonProxy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,45 @@ | ||
package dk.kiljacken.aestuscraft.core.proxy; | ||
|
||
import net.minecraft.entity.player.EntityPlayer; | ||
import net.minecraft.world.World; | ||
import cpw.mods.fml.common.network.IGuiHandler; | ||
import cpw.mods.fml.common.registry.GameRegistry; | ||
import dk.kiljacken.aestuscraft.gui.inventory.GuiInsulatedFurnace; | ||
import dk.kiljacken.aestuscraft.inventory.ContainerInsulatedFurnace; | ||
import dk.kiljacken.aestuscraft.lib.GuiIds; | ||
import dk.kiljacken.aestuscraft.lib.StringResources; | ||
import dk.kiljacken.aestuscraft.tileentity.TileInsulatedFurnace; | ||
import dk.kiljacken.aestuscraft.util.LogHelper; | ||
|
||
public class CommonProxy implements IGuiHandler { | ||
public void registerTileEntities() { | ||
LogHelper.info("Registering tile entities"); | ||
|
||
GameRegistry.registerTileEntity(TileInsulatedFurnace.class, StringResources.TE_INSULATED_FURNACE_NAME); | ||
} | ||
|
||
@Override | ||
public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { | ||
if (ID == GuiIds.INSULATED_FURNACE) { | ||
TileInsulatedFurnace tileEntityInsulatedFurnace = (TileInsulatedFurnace) world.getBlockTileEntity(x, y, z); | ||
return new ContainerInsulatedFurnace(player.inventory, tileEntityInsulatedFurnace); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
@Override | ||
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { | ||
if (ID == GuiIds.INSULATED_FURNACE) { | ||
TileInsulatedFurnace tileEntityInsulatedFurnace = (TileInsulatedFurnace) world.getBlockTileEntity(x, y, z); | ||
return new GuiInsulatedFurnace(player.inventory, tileEntityInsulatedFurnace); | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
package dk.kiljacken.aestuscraft.core.proxy; | ||
|
||
import net.minecraft.entity.player.EntityPlayer; | ||
import net.minecraft.nbt.NBTTagCompound; | ||
import net.minecraft.world.World; | ||
import cpw.mods.fml.common.network.IGuiHandler; | ||
import cpw.mods.fml.common.registry.GameRegistry; | ||
import dk.kiljacken.aestuscraft.gui.inventory.GuiInsulatedFurnace; | ||
import dk.kiljacken.aestuscraft.inventory.ContainerInsulatedFurnace; | ||
import dk.kiljacken.aestuscraft.lib.GuiIds; | ||
import dk.kiljacken.aestuscraft.lib.StringResources; | ||
import dk.kiljacken.aestuscraft.tileentity.TileInsulatedFurnace; | ||
import dk.kiljacken.aestuscraft.util.LogHelper; | ||
|
||
public class CommonProxy implements IGuiHandler { | ||
public void registerTileEntities() { | ||
LogHelper.info("Registering tile entities"); | ||
|
||
GameRegistry.registerTileEntity(TileInsulatedFurnace.class, StringResources.TE_INSULATED_FURNACE_NAME); | ||
} | ||
|
||
@Override | ||
public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { | ||
if (ID == GuiIds.INSULATED_FURNACE) { | ||
TileInsulatedFurnace tileEntityInsulatedFurnace = (TileInsulatedFurnace) world.getBlockTileEntity(x, y, z); | ||
return new ContainerInsulatedFurnace(player.inventory, tileEntityInsulatedFurnace); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
@Override | ||
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { | ||
if (ID == GuiIds.INSULATED_FURNACE) { | ||
TileInsulatedFurnace tileEntityInsulatedFurnace = (TileInsulatedFurnace) world.getBlockTileEntity(x, y, z); | ||
return new GuiInsulatedFurnace(player.inventory, tileEntityInsulatedFurnace); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public void handleTileUpdate(int x, int y, int z, NBTTagCompound nbtTagCompound) { | ||
|
||
} | ||
} |
34 changes: 18 additions & 16 deletions
34
aestuscraft_common/dk/kiljacken/aestuscraft/network/PacketHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
package dk.kiljacken.aestuscraft.network; | ||
|
||
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 PacketHandler implements IPacketHandler { | ||
|
||
@Override | ||
public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player) { | ||
// TODO Auto-generated method stub | ||
|
||
} | ||
|
||
} | ||
package dk.kiljacken.aestuscraft.network; | ||
|
||
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; | ||
import dk.kiljacken.aestuscraft.network.packet.PacketAEC; | ||
|
||
public class PacketHandler implements IPacketHandler { | ||
|
||
@Override | ||
public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player) { | ||
PacketAEC packetAEC = PacketType.buildPacketFrom(packet.data); | ||
|
||
packetAEC.process(manager, player); | ||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
aestuscraft_common/dk/kiljacken/aestuscraft/network/PacketType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package dk.kiljacken.aestuscraft.network; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.DataInputStream; | ||
import java.io.IOException; | ||
|
||
import net.minecraft.network.packet.Packet; | ||
import net.minecraft.network.packet.Packet250CustomPayload; | ||
import dk.kiljacken.aestuscraft.lib.Reference; | ||
import dk.kiljacken.aestuscraft.network.packet.PacketAEC; | ||
import dk.kiljacken.aestuscraft.network.packet.PacketTileUpdate; | ||
import dk.kiljacken.aestuscraft.util.LogHelper; | ||
|
||
public enum PacketType { | ||
TILE_UPDATE(PacketTileUpdate.class); | ||
|
||
private Class<? extends PacketAEC> clazz; | ||
|
||
PacketType(Class<? extends PacketAEC> clazz) { | ||
this.clazz = clazz; | ||
} | ||
|
||
public static PacketAEC buildPacketFrom(DataInputStream ios) { | ||
PacketAEC packet = null; | ||
|
||
try { | ||
int packetId = ios.read(); | ||
|
||
if (packetId == -1) { | ||
LogHelper.severe("Tried to build packet from ended inputstream"); | ||
|
||
return null; | ||
} | ||
|
||
PacketType[] packetTypes = values(); | ||
if (packetId >= packetTypes.length) { | ||
LogHelper.severe("Tried to build packet with invalid id: " + packetId); | ||
|
||
return null; | ||
} | ||
|
||
packet = packetTypes[packetId].clazz.newInstance(); | ||
packet.readPacketData(ios); | ||
} catch (IOException e) { | ||
LogHelper.severe("IOException while building packet"); | ||
|
||
return null; | ||
} catch (InstantiationException | IllegalAccessException e) { | ||
LogHelper.severe("Error instatiating new packet"); | ||
|
||
return null; | ||
} | ||
|
||
return packet; | ||
} | ||
|
||
public static PacketAEC buildPacketFrom(byte[] data) { | ||
return buildPacketFrom(new DataInputStream(new ByteArrayInputStream(data))); | ||
} | ||
|
||
public static Packet buildMCPacket(PacketAEC packet) { | ||
byte[] data = packet.getPacketData(); | ||
|
||
Packet250CustomPayload packet250 = new Packet250CustomPayload(); | ||
packet250.channel = Reference.CHANNEL; | ||
packet250.data = data; | ||
packet250.length = data.length; | ||
packet250.isChunkDataPacket = packet.isChunkData; | ||
|
||
return null; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
aestuscraft_common/dk/kiljacken/aestuscraft/network/packet/PacketAEC.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package dk.kiljacken.aestuscraft.network.packet; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.DataInputStream; | ||
import java.io.DataOutputStream; | ||
import java.io.IOException; | ||
|
||
import net.minecraft.network.INetworkManager; | ||
import cpw.mods.fml.common.network.Player; | ||
import dk.kiljacken.aestuscraft.network.PacketType; | ||
import dk.kiljacken.aestuscraft.util.LogHelper; | ||
|
||
public abstract class PacketAEC { | ||
public PacketType type; | ||
public boolean isChunkData; | ||
|
||
public PacketAEC(PacketType type, boolean isChunkData) { | ||
this.type = type; | ||
this.isChunkData = isChunkData; | ||
} | ||
|
||
public byte[] getPacketData() { | ||
ByteArrayOutputStream output = new ByteArrayOutputStream(); | ||
|
||
try { | ||
writePacketData(new DataOutputStream(output)); | ||
} catch (IOException e) { | ||
LogHelper.severe("IOException while writing packet data"); | ||
|
||
return null; | ||
} | ||
|
||
return output.toByteArray(); | ||
} | ||
|
||
public abstract void readPacketData(DataInputStream input) throws IOException; | ||
public abstract void writePacketData(DataOutputStream output) throws IOException; | ||
public abstract void process(INetworkManager manager, Player player); | ||
} |
52 changes: 52 additions & 0 deletions
52
aestuscraft_common/dk/kiljacken/aestuscraft/network/packet/PacketTileUpdate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package dk.kiljacken.aestuscraft.network.packet; | ||
|
||
import java.io.DataInputStream; | ||
import java.io.DataOutputStream; | ||
import java.io.IOException; | ||
|
||
import net.minecraft.nbt.NBTTagCompound; | ||
import net.minecraft.network.INetworkManager; | ||
import cpw.mods.fml.common.network.Player; | ||
import dk.kiljacken.aestuscraft.AestusCraft; | ||
import dk.kiljacken.aestuscraft.network.PacketType; | ||
import dk.kiljacken.aestuscraft.util.LogHelper; | ||
import dk.kiljacken.aestuscraft.util.StreamUtil; | ||
|
||
public class PacketTileUpdate extends PacketAEC { | ||
public int x, y, z; | ||
public NBTTagCompound nbtTagCompound; | ||
|
||
public PacketTileUpdate() { | ||
super(PacketType.TILE_UPDATE, false); | ||
} | ||
|
||
@Override | ||
public void readPacketData(DataInputStream input) throws IOException { | ||
x = input.readInt(); | ||
y = input.readInt(); | ||
z = input.readInt(); | ||
|
||
nbtTagCompound = StreamUtil.readNBTTagCompound(input); | ||
} | ||
|
||
@Override | ||
public void writePacketData(DataOutputStream output) throws IOException { | ||
output.writeInt(x); | ||
output.writeInt(y); | ||
output.writeInt(z); | ||
|
||
StreamUtil.writeNBTTagCompound(nbtTagCompound, output); | ||
} | ||
|
||
@Override | ||
public void process(INetworkManager manager, Player player) { | ||
if (nbtTagCompound == null) { | ||
LogHelper.severe("NBTTagCompound was null when processing tile update"); | ||
|
||
return; | ||
} | ||
|
||
AestusCraft.proxy.handleTileUpdate(x, y, z, nbtTagCompound); | ||
} | ||
|
||
} |
Oops, something went wrong.