mrkirby153

Untitled

Feb 21st, 2015
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. package me.mrkirby153.KCNerfer.network;
  2.  
  3. import cpw.mods.fml.common.network.ByteBufUtils;
  4. import cpw.mods.fml.common.network.simpleimpl.IMessage;
  5. import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
  6. import cpw.mods.fml.common.network.simpleimpl.MessageContext;
  7. import io.netty.buffer.ByteBuf;
  8. import me.mrkirby153.KCNerfer.recipie.DisabledItem;
  9. import me.mrkirby153.KCNerfer.recipie.RecipeHandler;
  10.  
  11. import java.util.ArrayList;
  12. import java.util.HashMap;
  13.  
  14. public class NerfItemsPacket implements IMessage {
  15.  
  16. public NerfItemsPacket() {
  17. }
  18.  
  19. public HashMap<String, ArrayList<DisabledItem>> items;
  20.  
  21. public NerfItemsPacket(HashMap<String, ArrayList<DisabledItem>> items) {
  22. this.items = items;
  23. }
  24.  
  25. @Override
  26. public void fromBytes(ByteBuf buf) {
  27. // Now to read them back
  28. ArrayList<DisabledItem> dItems;
  29. // Read the total Hash size
  30. int totalHashSize = buf.readInt();
  31. for(int i = 0; i < totalHashSize; i++){
  32. // Read the player's name
  33. String playerName = ByteBufUtils.readUTF8String(buf);
  34. // Read the size of the disabled items array
  35. int disabledItemsSize = buf.readInt();
  36. dItems = new ArrayList<DisabledItem>();
  37. for(int j = 0; j < disabledItemsSize; j++){
  38. // Read the item's data value
  39. int dataValue = buf.readInt();
  40. // Read the modid and name combined
  41. String modIdAndName = ByteBufUtils.readUTF8String(buf);
  42. // Split apart
  43. String modId = modIdAndName.split(":")[0];
  44. String itemName = modIdAndName.split(":")[1];
  45. // Construct the item
  46. if(dataValue == -1){
  47. DisabledItem disabledItem = new DisabledItem(modId, itemName);
  48. dItems.add(disabledItem);
  49. } else {
  50. DisabledItem disabledItem = new DisabledItem(modId, itemName, dataValue);
  51. dItems.add(disabledItem);
  52. }
  53. }
  54. this.items.put(playerName, dItems);
  55. }
  56. }
  57.  
  58. @Override
  59. public void toBytes(ByteBuf buf) {
  60. // Write the total size of the hashmap
  61. buf.writeInt(items.size());
  62. // Begin writing the players
  63. for (String playerName : items.keySet()) {
  64. // Send the player
  65. ByteBufUtils.writeUTF8String(buf, playerName);
  66. // Send the total amount of banned items
  67. ArrayList<DisabledItem> items = this.items.get(playerName);
  68. buf.writeInt(items.size());
  69. // Begin wrting the DisabledItems
  70. for (DisabledItem i : items) {
  71. // Write the data value or -1 for all the data values
  72. if(i.getAllDataValues())
  73. buf.writeInt(-1);
  74. else
  75. buf.writeInt(i.getDataValue());
  76. // Write the modName and the itemName
  77. ByteBufUtils.writeUTF8String(buf, i.getModName()+":"+i.getItemName());
  78. }
  79. }
  80. }
  81.  
  82. public static class Handler implements IMessageHandler<NerfItemsPacket, IMessage> {
  83.  
  84. @Override
  85. public IMessage onMessage(NerfItemsPacket message, MessageContext ctx) {
  86. System.out.println("Received packet from server");
  87. RecipeHandler.disabledItems = message.items;
  88. return null;
  89. }
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment