Guest User

Untitled

a guest
Dec 24th, 2015
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. package Com.gun.Packet;
  2.  
  3.  
  4. import Com.gun.gun.ItemGun;
  5. import cpw.mods.fml.common.network.ByteBufUtils;
  6. import cpw.mods.fml.common.network.simpleimpl.IMessage;
  7. import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
  8. import cpw.mods.fml.common.network.simpleimpl.MessageContext;
  9. import io.netty.buffer.ByteBuf;
  10. import net.minecraft.entity.player.EntityPlayer;
  11. import net.minecraft.entity.player.InventoryPlayer;
  12. import net.minecraft.entity.player.PlayerCapabilities;
  13. import net.minecraft.item.Item;
  14. import net.minecraft.item.ItemStack;
  15. import net.minecraft.nbt.NBTTagCompound;
  16.  
  17. public class ReloadGunPacket
  18. implements IMessage
  19. {
  20. private String username;
  21. private int slot;
  22. private ItemGun gun;
  23.  
  24. public ReloadGunPacket() {}
  25.  
  26. public ReloadGunPacket(String username, int slot, ItemGun gun)
  27. {
  28. this.username = username;
  29. this.slot = slot;
  30. this.gun = gun;
  31. }
  32.  
  33. public String getUsername()
  34. {
  35. return this.username;
  36. }
  37.  
  38. public int getSlot()
  39. {
  40. return this.slot;
  41. }
  42.  
  43. public ItemGun getGun()
  44. {
  45. return this.gun;
  46. }
  47.  
  48. public void fromBytes(ByteBuf buf)
  49. {
  50. NBTTagCompound nbt = ByteBufUtils.readTag(buf);
  51.  
  52. this.username = nbt.getString("username");
  53. this.slot = nbt.getInteger("slot");
  54. this.gun = ((ItemGun)Item.getItemById(nbt.getInteger("gun")));
  55. }
  56.  
  57. public void toBytes(ByteBuf buf)
  58. {
  59. NBTTagCompound nbt = new NBTTagCompound();
  60.  
  61. nbt.setString("username", getUsername());
  62. nbt.setInteger("slot", getSlot());
  63. nbt.setInteger("gun", Item.getIdFromItem(getGun()));
  64.  
  65. ByteBufUtils.writeTag(buf, nbt);
  66. }
  67.  
  68. public static class Handler
  69. implements IMessageHandler<ReloadGunPacket, IMessage>
  70. {
  71. public IMessage onMessage(ReloadGunPacket message, MessageContext ctx)
  72. {
  73. EntityPlayer player = PacketUtil.getPlayer(message.getUsername());
  74. ItemStack stack = player.inventory.getStackInSlot(message.getSlot());
  75. if ((stack != null) && (stack.getItem() == message.getGun()))
  76. {
  77. ItemGun gun = (ItemGun)stack.getAttributeModifiers();
  78. Item magazine = gun.getMagazineItem();
  79. NBTTagCompound nbt = stack.stackTagCompound != null ? stack.stackTagCompound : new NBTTagCompound();
  80. if (player.inventory.consumeInventoryItem(gun.getMagazineItem()))
  81. {
  82. nbt.setInteger("remainingAmmo", magazine.getMaxDamage());
  83. if (!player.capabilities.isCreativeMode) {
  84. player.inventory.consumeInventoryItem(gun.getMagazineItem());
  85. }
  86. }
  87. stack.stackTagCompound = nbt;
  88. }
  89. return null;
  90. }
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment