Advertisement
Guest User

Untitled

a guest
Oct 11th, 2012
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.61 KB | None | 0 0
  1. package me.itreptau.protocolLibTest;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.logging.Level;
  6. import java.util.logging.Logger;
  7.  
  8. import org.bukkit.Material;
  9. import org.bukkit.command.Command;
  10. import org.bukkit.entity.Entity;
  11. import org.bukkit.entity.Item;
  12. import org.bukkit.entity.Player;
  13. import org.bukkit.event.inventory.InventoryType;
  14. import org.bukkit.event.inventory.InventoryType.SlotType;
  15. import org.bukkit.inventory.Inventory;
  16. import org.bukkit.inventory.ItemStack;
  17. import org.bukkit.inventory.PlayerInventory;
  18. import org.bukkit.plugin.java.JavaPlugin;
  19.  
  20. import com.comphenix.protocol.ProtocolLibrary;
  21. import com.comphenix.protocol.ProtocolManager;
  22. import com.comphenix.protocol.events.ConnectionSide;
  23. import com.comphenix.protocol.events.ListenerPriority;
  24. import com.comphenix.protocol.events.PacketAdapter;
  25. import com.comphenix.protocol.events.PacketContainer;
  26. import com.comphenix.protocol.events.PacketEvent;
  27. import com.comphenix.protocol.reflect.FieldAccessException;
  28.  
  29. public class ProtocolLibTest extends JavaPlugin {
  30.  
  31.     public Map<Player, Inventory> outfitMap = new HashMap<Player, Inventory>();
  32.     private ProtocolManager protocolManager;
  33.     public Logger logger = Logger.getLogger("ProtocolLibTest");
  34.     enum Armortype {HELMET, CHESTPLATE, LEGGINGS, BOOTS}
  35.    
  36.     @Override
  37.     public void onDisable() {
  38.         System.out.println("ProtocolLibTest disabled!");
  39.     }
  40.  
  41.     @Override
  42.     public void onEnable() {
  43.         protocolManager = ProtocolLibrary.getProtocolManager();
  44.         getCommand("outfit").setExecutor(new ProtocolLibTestCommandExecutor(this));
  45.         enableOutfit();
  46.         System.out.println("ProtocolLibTest enabled!");
  47.     }
  48.    
  49.     public Player getPlayerFromEntityId (int entityId) {
  50.         for(Player player: getServer().getOnlinePlayers()) {
  51.             if(player.getEntityId() == entityId) {
  52.                 return player;
  53.             }
  54.         }
  55.         return null;
  56.     }
  57.    
  58.     public Inventory getOutfitInventory(Player player) {
  59.         if(!outfitMap.containsKey(player)) {
  60.             outfitMap.put(player, getServer().createInventory(player, InventoryType.PLAYER));
  61.         }
  62.         return outfitMap.get(player);
  63.     }
  64.    
  65.     public ItemStack getFirstItemStack(ItemStack[] inventoryContent, Armortype armorType) {
  66.         for(ItemStack itemStack: inventoryContent) {
  67.             if(itemStack == null) continue;
  68.             switch(armorType) {
  69.                 case HELMET:
  70.                     if(itemStack.getType() == Material.LEATHER_HELMET || itemStack.getType() == Material.CHAINMAIL_HELMET || itemStack.getType() == Material.IRON_HELMET || itemStack.getType() == Material.DIAMOND_HELMET)
  71.                         return itemStack;
  72.                     break;
  73.                    
  74.                 case CHESTPLATE:
  75.                     if(itemStack.getType() == Material.LEATHER_CHESTPLATE || itemStack.getType() == Material.CHAINMAIL_CHESTPLATE || itemStack.getType() == Material.IRON_CHESTPLATE || itemStack.getType() == Material.DIAMOND_CHESTPLATE)
  76.                         return itemStack;
  77.                     break;
  78.  
  79.                 case LEGGINGS:
  80.                     if(itemStack.getType() == Material.LEATHER_LEGGINGS || itemStack.getType() == Material.CHAINMAIL_LEGGINGS || itemStack.getType() == Material.IRON_LEGGINGS || itemStack.getType() == Material.DIAMOND_LEGGINGS)
  81.                         return itemStack;
  82.                     break;
  83.  
  84.                 case BOOTS:
  85.                     if(itemStack.getType() == Material.LEATHER_BOOTS || itemStack.getType() == Material.CHAINMAIL_BOOTS || itemStack.getType() == Material.IRON_BOOTS || itemStack.getType() == Material.DIAMOND_BOOTS)
  86.                         return itemStack;
  87.                     break;
  88.  
  89.             }
  90.         }
  91.         return null;
  92.     }
  93.    
  94.     public void enableOutfit() {
  95.         protocolManager.addPacketListener(new PacketAdapter(this, ConnectionSide.SERVER_SIDE, 0x5) {
  96.             @Override
  97.             public void onPacketSending(PacketEvent event) {
  98.                 PacketContainer packet = event.getPacket();
  99.                
  100.                 try {
  101.                    
  102.                     // Player equipment
  103.                     if (event.getPacketID() == 0x05) {
  104.                         int EntityId = packet.getSpecificModifier(int.class).read(0);
  105.                         Player player = getPlayerFromEntityId(EntityId);
  106.                         Inventory outfitInventory = getOutfitInventory(player);
  107.                         //Helmet
  108.                         if(packet.getSpecificModifier(int.class).read(1) == 1) {
  109.                             packet.getItemModifier().write(0, getFirstItemStack(outfitInventory.getContents(), Armortype.HELMET));
  110.                             System.out.println("Modified" + player.getName() + "'s helmet");
  111.                         }
  112.                         //Chestplate
  113.                         if(packet.getSpecificModifier(int.class).read(1) == 2) {
  114.                             packet.getItemModifier().write(0, getFirstItemStack(outfitInventory.getContents(), Armortype.CHESTPLATE));
  115.                             System.out.println("Modified" + player.getName() + "'s chestplate");
  116.                         }
  117.                         //Leggings
  118.                         if(packet.getSpecificModifier(int.class).read(1) == 3) {
  119.                             packet.getItemModifier().write(0, getFirstItemStack(outfitInventory.getContents(), Armortype.LEGGINGS));
  120.                             System.out.println("Modified" + player.getName() + "'s leggings");
  121.                         }
  122.                         //Shoes
  123.                         if(packet.getSpecificModifier(int.class).read(1) == 4) {
  124.                             packet.getItemModifier().write(0, getFirstItemStack(outfitInventory.getContents(), Armortype.BOOTS));
  125.                             System.out.println("Modified" + player.getName() + "'s shoes");
  126.                         }
  127.                     }
  128.                
  129.                 } catch (FieldAccessException e) {
  130.                     logger.log(Level.SEVERE, "Couldn't access field.", e);
  131.                 }
  132.             }
  133.             });
  134.     }
  135.    
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement