/src/main/java/pcl/common/network/TinyModPacket.java

https://gitlab.com/Michiyo/lanteacraft · Java · 105 lines · 77 code · 14 blank · 14 comment · 1 complexity · 7eb4852f8000cadc2847f5df32cc3997 MD5 · raw file

  1. package pcl.common.network;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.DataInputStream;
  4. import java.io.DataOutputStream;
  5. import java.io.IOException;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8. import net.minecraft.network.packet.Packet250CustomPayload;
  9. import pcl.common.util.WorldLocation;
  10. public class TinyModPacket extends ModPacket {
  11. private DataInputStream instream;
  12. private DataOutputStream outstream;
  13. private ByteArrayOutputStream outbuff;
  14. private WorldLocation origin;
  15. private boolean toServer = false;
  16. /**
  17. * Reads a new TinyModPacket
  18. *
  19. * @param data
  20. * The data stream
  21. * @return The TinyModPacket result
  22. * @throws IOException
  23. * Any network or read exception
  24. */
  25. public static TinyModPacket createPacket(DataInputStream data) throws IOException {
  26. boolean isServer = (data.readByte() == 1);
  27. IStreamPackable<?> unpacker = ModPacket.findPacker(WorldLocation.class);
  28. WorldLocation location = (WorldLocation) unpacker.unpack(data);
  29. TinyModPacket pkt = new TinyModPacket(data, location);
  30. pkt.setIsForServer(isServer);
  31. return pkt;
  32. }
  33. public TinyModPacket(WorldLocation creationOrigin) {
  34. outbuff = new ByteArrayOutputStream();
  35. outstream = new DataOutputStream(outbuff);
  36. origin = creationOrigin;
  37. }
  38. public TinyModPacket(DataInputStream data, WorldLocation sourceOrigin) {
  39. instream = data;
  40. origin = sourceOrigin;
  41. }
  42. @Override
  43. public WorldLocation getOriginLocation() {
  44. return origin;
  45. }
  46. public DataInputStream getIn() {
  47. return instream;
  48. }
  49. public DataOutputStream getOut() {
  50. return outstream;
  51. }
  52. public void setIsForServer(boolean b) {
  53. toServer = b;
  54. }
  55. @Override
  56. public boolean getPacketIsForServer() {
  57. return toServer;
  58. }
  59. /**
  60. * Converts this packet instance into a Forge payload packet
  61. *
  62. * @return A custom Packet250CustomPayload packet for Forge networking
  63. */
  64. @Override
  65. public Packet250CustomPayload toPacket() {
  66. ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  67. Packet250CustomPayload pkt = new Packet250CustomPayload();
  68. try {
  69. bytes.write(1);
  70. bytes.write((toServer) ? 1 : 0);
  71. IStreamPackable<WorldLocation> packer = (IStreamPackable<WorldLocation>) ModPacket
  72. .findPacker(WorldLocation.class);
  73. DataOutputStream wrapper = new DataOutputStream(bytes);
  74. packer.pack(origin, wrapper);
  75. wrapper.flush();
  76. wrapper.close();
  77. outstream.flush();
  78. outstream.close();
  79. bytes.write(outbuff.toByteArray());
  80. } catch (IOException e) {
  81. Logger.getLogger("pcl.common").log(Level.WARNING, "Exception when writing packet!", e);
  82. }
  83. pkt.data = bytes.toByteArray();
  84. pkt.length = pkt.data.length;
  85. return pkt;
  86. }
  87. @Override
  88. public String getType() {
  89. return "TinyPacket";
  90. }
  91. }