Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. package net.logan31.poppiecraft.network;
  2.  
  3.  
  4. import io.netty.buffer.ByteBuf;
  5. import io.netty.buffer.ByteBufUtil;
  6. import net.logan31.poppiecraft.Capabilities.IWork;
  7. import net.logan31.poppiecraft.Utils.Utils;
  8. import net.logan31.poppiecraft.init.ModCapabilities;
  9. import net.minecraft.tileentity.TileEntity;
  10. import net.minecraft.util.EnumFacing;
  11. import net.minecraft.util.math.BlockPos;
  12. import net.minecraftforge.common.capabilities.Capability;
  13. import net.minecraftforge.fml.common.FMLCommonHandler;
  14. import net.minecraftforge.fml.common.network.ByteBufUtils;
  15. import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
  16. import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
  17. import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
  18. import net.minecraftforge.fml.relauncher.Side;
  19.  
  20. public class PacketGetWorker implements IMessage {
  21.  
  22. /**
  23. * States whether the message is valid or not
  24. */
  25. private boolean messageValid;
  26.  
  27. /**
  28. * The variables to send
  29. */
  30. private BlockPos pos;
  31. private EnumFacing side;
  32.  
  33. private String className;
  34. private String cooldownFieldName;
  35. private String maxCooldownFieldName;
  36.  
  37. /**
  38. * Default constructor used for registration
  39. */
  40. public PacketGetWorker() {
  41. this.messageValid = false;
  42. }
  43.  
  44. /**
  45. * Create a packet which will retrieve
  46. * {@link ModCapabilities#CAPABILITY_WORKER} details from the
  47. * {@link TileEntity} on the {@link Side#SERVER}
  48. *
  49. * @param pos
  50. * The position of the {@link TileEntity}
  51. * @param side
  52. * The side of the {@link TileEntity} for use with
  53. * {@link Capability}
  54. * @param className
  55. * The name of the class the data will be stored in
  56. * @param cooldownFieldName
  57. * The name of the cooldown field which will store the cooldown
  58. * data
  59. * @param maxCooldownFieldName
  60. * The name of the max cooldown field which will store the max
  61. * cooldown data
  62. */
  63. public PacketGetWorker(BlockPos pos, EnumFacing side, String className, String cooldownFieldName,
  64. String maxCooldownFieldName) {
  65. this.pos = pos;
  66. this.side = side;
  67. this.className = className;
  68. this.cooldownFieldName = cooldownFieldName;
  69. this.maxCooldownFieldName = maxCooldownFieldName;
  70. this.messageValid = true;
  71.  
  72. }
  73.  
  74. /**
  75. * Read data from the {@link ByteBuf} provided
  76. */
  77. @Override
  78. public void fromBytes(ByteBuf buf) {
  79. try {
  80. this.pos = new BlockPos(buf.readInt(), buf.readInt(), buf.readInt());
  81. this.side = EnumFacing.byName(ByteBufUtils.readUTF8String(buf));
  82. this.className = ByteBufUtils.readUTF8String(buf);
  83. this.cooldownFieldName = ByteBufUtils.readUTF8String(buf);
  84. this.maxCooldownFieldName = ByteBufUtils.readUTF8String(buf);
  85. } catch (IndexOutOfBoundsException ioe) {
  86. Utils.getLogger().catching(ioe);
  87. return;
  88. }
  89. this.messageValid = true;
  90. }
  91.  
  92. /**
  93. * Write data to the {@link ByteBuf} provided
  94. */
  95. @Override
  96. public void toBytes(ByteBuf buf) {
  97. if (!this.messageValid)
  98. return;
  99. buf.writeInt(pos.getX());
  100. buf.writeInt(pos.getY());
  101. buf.writeInt(pos.getZ());
  102. ByteBufUtils.writeUTF8String(buf, this.side.getName2());
  103. ByteBufUtils.writeUTF8String(buf, this.className);
  104. ByteBufUtils.writeUTF8String(buf, this.cooldownFieldName);
  105. ByteBufUtils.writeUTF8String(buf, this.maxCooldownFieldName);
  106. }
  107.  
  108. /**
  109. * Handles the {@link PacketGetWorker} message
  110. *
  111. * @author CJMinecraft
  112. *
  113. */
  114. public static class Handler implements IMessageHandler<PacketGetWorker, IMessage> {
  115.  
  116. /**
  117. * Adds a task to the server handler to process our message
  118. */
  119. @Override
  120. public IMessage onMessage(PacketGetWorker message, MessageContext ctx) {
  121. if (!message.messageValid && ctx.side != Side.SERVER)
  122. return null;
  123. FMLCommonHandler.instance().getWorldThread(ctx.netHandler)
  124. .addScheduledTask(() -> processMessage(message, ctx));
  125.  
  126. return null;
  127. }
  128.  
  129. /**
  130. * Gets all information required from the {@link TileEntity} at the
  131. * given pos
  132. *
  133. * @param message
  134. * Which holds the data regarding {@link BlockPos} and side
  135. * @param ctx
  136. * The message context
  137. */
  138. void processMessage(PacketGetWorker message, MessageContext ctx) {
  139. TileEntity te = ctx.getServerHandler().playerEntity.getServerWorld().getTileEntity(message.pos);
  140. if (te == null)
  141. return;
  142.  
  143. if (!te.hasCapability(ModCapabilities.CAPABILITY_WORKER, message.side)) {
  144.  
  145. return;
  146. }
  147. if(PacketHandler.INSTANCE != null) {
  148. IWork worker = te.getCapability(ModCapabilities.CAPABILITY_WORKER, message.side);
  149. PacketHandler.INSTANCE.sendTo(new PacketReturnWorker(worker.getWorkDone(), worker.getMaxWork(),
  150. message.className, message.cooldownFieldName, message.maxCooldownFieldName),
  151. ctx.getServerHandler().playerEntity);
  152. }
  153. System.out.println("Instance " + PacketHandler.INSTANCE);
  154.  
  155. }
  156. }
  157.  
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement