Guest User

Untitled

a guest
Apr 15th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | None | 0 0
  1. package com.demkom58.core;
  2.  
  3. import com.comphenix.protocol.PacketType;
  4. import com.comphenix.protocol.ProtocolLibrary;
  5. import com.comphenix.protocol.ProtocolManager;
  6. import com.comphenix.protocol.events.PacketAdapter;
  7. import com.comphenix.protocol.events.PacketContainer;
  8. import com.comphenix.protocol.events.PacketEvent;
  9. import com.comphenix.protocol.reflect.StructureModifier;
  10. import com.comphenix.protocol.wrappers.WrappedAttribute;
  11. import com.comphenix.protocol.wrappers.WrappedAttribute.Builder;
  12. import org.bukkit.entity.Player;
  13. import org.bukkit.plugin.Plugin;
  14.  
  15. import java.util.ArrayList;
  16. import java.util.HashSet;
  17. import java.util.List;
  18. import java.util.Set;
  19.  
  20. public class ActionHealth {
  21.     Main plugin;
  22.  
  23.     private static ProtocolManager protocolManager;
  24.  
  25.     public void onEnable(Plugin cplugin) {
  26.         plugin = (Main) cplugin;
  27.  
  28.         Set packets = new HashSet();
  29.         packets.add(PacketType.Play.Server.UPDATE_HEALTH);
  30.         packets.add(PacketType.Play.Server.UPDATE_ATTRIBUTES);
  31.  
  32.         ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(plugin, packets) {
  33.             @Override
  34.             public void onPacketSending(PacketEvent event) {
  35.                 if (event.getPlayer() instanceof Player ) {
  36.                     PacketContainer packet = event.getPacket();
  37.                     PacketType type = packet.getType();
  38.                     Player p = event.getPlayer();
  39.                     if (type == PacketType.Play.Server.UPDATE_HEALTH) {
  40.  
  41.                         float hp = (float) (p.getHealth() * 20 / p.getMaxHealth());
  42.  
  43.                         p.sendMessage(Methods.toColor("&7Твой % здоровья: &f"+hp+"&c❤"));
  44.                         p.sendMessage(Methods.toColor("&7Твое здоровье: &f"+Math.round(p.getHealth()) +"&7/&f" +p.getMaxHealth() +"&c❤"));
  45.  
  46.                        packet.getFloat().write(0, hp);
  47.                     }
  48.                     if (type == PacketType.Play.Server.UPDATE_ATTRIBUTES) {
  49.                         Builder builder;
  50.  
  51.                         List<WrappedAttribute> attributes = new ArrayList<>();
  52.                         builder = WrappedAttribute.newBuilder();
  53.                         builder.attributeKey("generic.maxHealth");
  54.                         builder.baseValue(20);
  55.                         builder.packet(packet);
  56.                         attributes.add(builder.build());
  57.  
  58.                         packet.getAttributeCollectionModifier().write(0, attributes);
  59.                     }
  60.                 }
  61.             }
  62.         });
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment