Last active
June 20, 2017 07:12
-
-
Save effective-light/ed248c63f8be58cd1f7e to your computer and use it in GitHub Desktop.
FakeArmor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.bukkit.Bukkit; | |
| import org.bukkit.entity.Entity; | |
| import org.bukkit.entity.Player; | |
| import org.bukkit.event.EventHandler; | |
| import org.bukkit.event.Listener; | |
| import org.bukkit.event.entity.EntityDamageEvent; | |
| import org.bukkit.event.inventory.InventoryClickEvent; | |
| import org.bukkit.event.inventory.InventoryType; | |
| import org.bukkit.inventory.ItemStack; | |
| import org.bukkit.plugin.Plugin; | |
| import org.bukkit.scheduler.BukkitRunnable; | |
| import org.bukkit.scheduler.BukkitTask; | |
| import java.lang.reflect.Constructor; | |
| import java.lang.reflect.Field; | |
| import java.lang.reflect.InvocationTargetException; | |
| import java.lang.reflect.Method; | |
| import java.util.HashMap; | |
| import java.util.Map; | |
| public class EntityEquipmentManager implements Listener | |
| { | |
| private Map<Integer, Map<Slot, ItemStack>> items = new HashMap<>(); | |
| private Constructor<?> entityEquipmentConstructor; | |
| private Method getPlayerHandle; | |
| private Method sendPacket; | |
| private Method asNMSCopy; | |
| private Field playerConnection; | |
| private Plugin plugin; | |
| public EntityEquipmentManager(Plugin plugin) | |
| { | |
| this.plugin = plugin; | |
| String version = Bukkit.getServer().getClass().getPackage().getName().split( "\\." )[ 3 ]; | |
| try | |
| { | |
| Class<?> entityPlayerClass = Class.forName( "net.minecraft.server." + version + ".EntityPlayer" ); | |
| Class<?> craftPlayerClass = Class.forName( "org.bukkit.craftbukkit." + version + ".entity.CraftPlayer" ); | |
| Class<?> craftItemStack = Class.forName( "org.bukkit.craftbukkit." + version + ".inventory.CraftItemStack" ); | |
| Class<?> typePlayerConnection = Class.forName( "net.minecraft.server." + version + ".PlayerConnection" ); | |
| Class<?> entityEquipmentClass = Class.forName( "net.minecraft.server." + version + ".PacketPlayOutEntityEquipment" ); | |
| Class<?> nmsItemStack = Class.forName( "net.minecraft.server." + version + ".ItemStack" ); | |
| entityEquipmentConstructor = entityEquipmentClass.getConstructor( int.class, int.class, nmsItemStack ); | |
| asNMSCopy = craftItemStack.getMethod( "asNMSCopy", ItemStack.class ); | |
| getPlayerHandle = craftPlayerClass.getMethod( "getHandle" ); | |
| playerConnection = entityPlayerClass.getField( "playerConnection" ); | |
| sendPacket = typePlayerConnection.getMethod( "sendPacket", | |
| Class.forName( "net.minecraft.server." + version + ".Packet" ) ); | |
| } catch ( ClassNotFoundException | NoSuchMethodException | NoSuchFieldException e ) | |
| { | |
| e.printStackTrace(); | |
| } | |
| plugin.getServer().getPluginManager().registerEvents( this, plugin ); | |
| } | |
| public void setEquipment(Entity entity, Slot slot, ItemStack itemStack) | |
| { | |
| try | |
| { | |
| Object packet = entityEquipmentConstructor.newInstance( entity.getEntityId(), slot.ordinal(), asNMSCopy.invoke( null, itemStack ) ); | |
| Map<Slot, ItemStack> tempMap; | |
| if ( items.containsKey( entity.getEntityId() ) ) | |
| { | |
| tempMap = items.get( entity.getEntityId() ); | |
| } else | |
| { | |
| tempMap = new HashMap<>(); | |
| items.put( entity.getEntityId(), tempMap ); | |
| } | |
| tempMap.put( slot, itemStack ); | |
| Player targetedPlayer = null; | |
| if ( entity instanceof Player ) | |
| targetedPlayer = ( Player ) entity; | |
| for ( Player player : Bukkit.getOnlinePlayers() ) | |
| { | |
| if ( targetedPlayer != null && targetedPlayer.getName().equals( player.getName() ) ) continue; | |
| Object entityPlayer = getPlayerHandle.invoke( player ); | |
| Object connection = playerConnection.get( entityPlayer ); | |
| sendPacket.invoke( connection, packet ); | |
| } | |
| } catch ( IllegalAccessException | InvocationTargetException | InstantiationException e ) | |
| { | |
| e.printStackTrace(); | |
| } | |
| } | |
| private void setArmor(Entity entity) | |
| { | |
| if ( items.containsKey( entity.getEntityId() ) ) | |
| { | |
| items.get( entity.getEntityId() ).forEach( (slot, itemStack) -> | |
| setEquipment( entity, slot, itemStack ) ); | |
| } | |
| } | |
| private void setArmor(Entity entity, Slot slot) | |
| { | |
| new BukkitRunnable() | |
| { | |
| @Override | |
| public void run() | |
| { | |
| if ( items.containsKey( entity.getEntityId() ) ) | |
| { | |
| if ( items.get( entity.getEntityId() ).containsKey( slot ) ) | |
| { | |
| ItemStack itemStack = items.get( entity.getEntityId() ).get( slot ); | |
| setEquipment( entity, slot, itemStack ); | |
| } | |
| } | |
| } | |
| }.runTaskLater( plugin, 2L ); | |
| } | |
| @EventHandler | |
| public void onEntityDamage(EntityDamageEvent event) | |
| { | |
| new BukkitRunnable() | |
| { | |
| @Override | |
| public void run() | |
| { | |
| setArmor( event.getEntity() ); | |
| } | |
| }.runTaskLater( plugin, 2L ); | |
| } | |
| @EventHandler | |
| public void onInventoryClick(InventoryClickEvent event) | |
| { | |
| if ( event.getSlotType().equals( InventoryType.SlotType.ARMOR ) ) | |
| { | |
| event.setCancelled( true ); | |
| } | |
| } | |
| public enum Slot | |
| { | |
| HELD( 36, 44 ), BOOTS( 8, 8 ), LEGGINGS( 7, 7 ), CHEST_PLATE( 6, 6 ), HELMET( 5, 5 ); | |
| private int min; | |
| private int max; | |
| Slot(int min, int max) | |
| { | |
| this.min = min; | |
| this.max = max; | |
| } | |
| public int getMin() | |
| { | |
| return min; | |
| } | |
| public int getMax() | |
| { | |
| return max; | |
| } | |
| public static Slot getBySlot(int slot) | |
| { | |
| for ( Slot enumSlot : values() ) | |
| { | |
| if ( enumSlot.getMin() >= slot && enumSlot.getMax() <= slot ) | |
| { | |
| System.out.println( enumSlot ); | |
| return enumSlot; | |
| } | |
| } | |
| return null; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment