Packets

Discussion in 'Plugin Development' started by CeramicTitan, Nov 24, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    CeramicTitan

    Code:JAVA
    1. package me.ceramictitan.spy;
    2.  
    3. import java.util.HashSet;
    4. import java.util.Set;
    5.  
    6. import net.minecraft.server.EntityLiving;
    7. import net.minecraft.server.Packet5EntityEquipment;
    8.  
    9. import org.bukkit.Bukkit;
    10. import org.bukkit.Effect;
    11. import org.bukkit.EntityEffect;
    12. import org.bukkit.Material;
    13. import org.bukkit.craftbukkit.entity.CraftLivingEntity;
    14. import org.bukkit.craftbukkit.entity.CraftPlayer;
    15. import org.bukkit.craftbukkit.inventory.CraftItemStack;
    16. import org.bukkit.entity.Entity;
    17. import org.bukkit.entity.LivingEntity;
    18. import org.bukkit.entity.Player;
    19. import org.bukkit.event.EventHandler;
    20. import org.bukkit.event.Listener;
    21. import org.bukkit.event.block.Action;
    22. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    23. import org.bukkit.event.player.PlayerInteractEvent;
    24. import org.bukkit.event.player.PlayerItemHeldEvent;
    25. import org.bukkit.event.player.PlayerQuitEvent;
    26.  
    27. import org.bukkit.inventory.ItemStack;
    28. import org.bukkit.potion.PotionEffect;
    29. import org.bukkit.potion.PotionEffectType;
    30.  
    31.  
    32. public class myPlayerInteractListener implements Listener{
    33.  
    34. public Set<String> vanished = new HashSet<String>();
    35. public spy plugin;
    36. private int id;
    37.  
    38. public myPlayerInteractListener(spy plugin){
    39. this.plugin = plugin;
    40. }
    41. @EventHandler
    42. public void onPlayerInteract(PlayerInteractEvent event){
    43. final Player p = event.getPlayer();
    44. if(event.getAction() == Action.RIGHT_CLICK_AIR){
    45. if(p.getItemInHand().getType() == Material.REDSTONE){
    46. if(vanished.contains(p.getName())){
    47. p.sendMessage("Already Vanished");
    48. event.setCancelled(true);
    49. }else if(!vanished.contains(p.getName())){
    50. this.vanished.add(p.getName());
    51. p.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, -1, 1));
    52. setinvisibleEquipment(p);
    53. p.sendMessage("You are now hidden!");
    54. p.getWorld().playEffect(p.getLocation(), Effect.STEP_SOUND, Material.SNOW_BLOCK.getId());
    55. if(vanished.contains(p.getName())){
    56. id = Bukkit.getScheduler().scheduleAsyncRepeatingTask(plugin, new Runnable(){
    57.  
    58. @Override
    59. public void run() {
    60. if(p.getItemInHand().getType() == Material.REDSTONE){
    61. p.getItemInHand().setAmount(p.getItemInHand().getAmount() -1);
    62. if(p.getItemInHand().getType() == Material.REDSTONE){
    63. if(p.getItemInHand().getAmount() == 1){
    64. p.setItemInHand(new ItemStack(Material.AIR, 1));
    65. }
    66. if(p.getItemInHand().getType() != Material.REDSTONE){
    67. p.removePotionEffect(PotionEffectType.INVISIBILITY);
    68. p.sendMessage("No longer hidden");
    69. if(vanished.contains(p.getName())){
    70. vanished.remove(p.getName());
    71. }
    72. System.out.println("removed!");
    73. stopTask();
    74. }
    75. }
    76. }
    77. }
    78. },20L, 20L);
    79. }
    80. }
    81.  
    82. }
    83. }
    84. }
    85. @EventHandler
    86. public void onItemChange(PlayerItemHeldEvent event){
    87. if(event.getPlayer().getItemInHand().getType() == Material.REDSTONE){
    88. if(!vanished.contains(event.getPlayer().getName())){
    89. return;
    90. }
    91. event.getPlayer().removePotionEffect(PotionEffectType.INVISIBILITY);
    92. event.getPlayer().sendMessage("No longer hidden");
    93. setvisibleEquipment(event.getPlayer());
    94. if(vanished.contains(event.getPlayer().getName())){
    95. vanished.remove(event.getPlayer().getName());
    96. System.out.println("removed!");
    97. }
    98. stopTask();
    99. }
    100. }
    101. @EventHandler
    102. public void onDamage(EntityDamageByEntityEvent event){
    103. if(event.getEntity() instanceof Player){
    104. Player p = (Player)event.getEntity();
    105. if(vanished.contains(p.getName())){
    106. p.removePotionEffect(PotionEffectType.INVISIBILITY);
    107. setvisibleEquipment(p);
    108. event.setCancelled(false);
    109. p.getWorld().playEffect(p.getLocation(), Effect.STEP_SOUND, Material.LAVA.getId());
    110. p.playEffect(EntityEffect.HURT);
    111. vanished.remove(p.getName());
    112. p.sendMessage("You have been Spotted!");
    113. stopTask();
    114.  
    115. }
    116. }
    117. }
    118. @EventHandler
    119. public void onPlayerQuit(PlayerQuitEvent event){
    120. if(vanished.contains(event.getPlayer().getName())){
    121. vanished.remove(event.getPlayer().getName());
    122. }
    123. }
    124. private void stopTask(){
    125. Bukkit.getServer().getScheduler().cancelTask(id);
    126. }
    127. public void setinvisibleEquipment(Entity e){
    128. if(e instanceof LivingEntity){
    129. EntityLiving el = ((CraftLivingEntity) e).getHandle();
    130. for(int i=0;i<5;i++){
    131. net.minecraft.server.ItemStack item = el.getEquipment(i);
    132. Packet5EntityEquipment packet = new Packet5EntityEquipment(e.getEntityId(),(short)i, new CraftItemStack(Material.AIR).getHandle());
    133. for (Player play : Bukkit.getOnlinePlayers())
    134. {
    135. ((CraftPlayer)play).getHandle().netServerHandler.sendPacket(packet);
    136. }
    137. }
    138. }
    139. }
    140. public void setvisibleEquipment(Entity e){
    141. if(e instanceof LivingEntity){
    142. EntityLiving el = ((CraftLivingEntity) e).getHandle();
    143. for(int i=0;i<5;i++){
    144. net.minecraft.server.ItemStack item = el.getEquipment(i);
    145. Packet5EntityEquipment packet = new Packet5EntityEquipment(e.getEntityId(),(short)i,item);
    146. for (Player play : Bukkit.getOnlinePlayers())
    147. {
    148. ((CraftPlayer)play).getHandle().netServerHandler.sendPacket(packet);
    149. }
    150. }
    151. }
    152. }
    153. }
    154.  

    The packet sending causes me to get 'End of Stream' Also tried 'Packet5EntityEquipment packet = new Packet5EntityEquipment(e.getEntityId(),(short)i, null);' and it crashes the client
     
  2. Offline

    fireblast709

    probably a NPE clientside because you send a null?
     
  3. CeramicTitan I'm not sure, but it may be that this packet is for mobs only.
     
  4. Offline

    CeramicTitan

    when i set it to material.air i get end of stream
     
  5. I'm guessing that packet is for mobs only.
     
  6. Offline

    CeramicTitan

  7. Easy: Send a empty inventory to all the players.
     
  8. Offline

    CeramicTitan

    but how?
     
  9. I'm sorry, I looked at the packet and it seems that it's the right packet.
    I don't really have the time to explain it right now, so you should take a look at this website: http://www.wiki.vg/Protocol
    It's really helpful.

    After a quick look, it seems like you don't need to send the material/itemstack. But the NBT Compound of the itemstack. (basically the *savefile* of the itemstack)
     
  10. Offline

    md_5

    Damn, beat me to linking it.
    You can also check out ProtocolLib, although its not very useful for packet sending.
     
  11. Offline

    CeramicTitan

    If you look at my code, it has the requirements stated on that website. But I crash whenever the packet gets sent to me.
     
Thread Status:
Not open for further replies.

Share This Page