Advertisement
PerryPlaysMC

PlayerArmorEditEvent

Feb 26th, 2021
1,259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.83 KB | None | 0 0
  1. import org.bukkit.Material;
  2. import org.bukkit.entity.Player;
  3. import org.bukkit.event.Cancellable;
  4. import org.bukkit.event.HandlerList;
  5. import org.bukkit.event.player.PlayerEvent;
  6. import org.bukkit.inventory.ItemStack;
  7.  
  8. import javax.annotation.Nullable;
  9.  
  10. /**
  11.  * Copy Right ©
  12.  * This code is private
  13.  * Owner: PerryPlaysMC
  14.  * From: 02/2021-Now
  15.  **/
  16.  
  17. public class PlayerArmorEditEvent extends PlayerEvent implements Cancellable {
  18.  
  19.     private static final HandlerList HANDLERS = new HandlerList();
  20.     private boolean cancelled;
  21.     private final int slot;
  22.     private boolean force;
  23.     private boolean attemptNonArmor;
  24.     private boolean attemptWrongSlot;
  25.     private ItemStack oldPiece, newPiece;
  26.     private final ArmorType type;
  27.     private final Cause cause;
  28.  
  29.  
  30.     public PlayerArmorEditEvent(Player who, int slot, ItemStack oldPiece, ItemStack newPiece, ArmorType type, Cause cause) {
  31.         super(who);
  32.         this.player = who;
  33.         this.slot = slot;
  34.         this.oldPiece = oldPiece;
  35.         this.newPiece = newPiece;
  36.         this.type = type;
  37.         this.cause = cause;
  38.         this.force = false;
  39.         this.attemptNonArmor = false;
  40.         this.attemptWrongSlot = false;
  41.         if(this.oldPiece == null) this.oldPiece = new ItemStack(Material.AIR);
  42.         if(this.newPiece == null) this.newPiece = new ItemStack(Material.AIR);
  43.     }
  44.  
  45.     public ItemStack getOldPiece() {
  46.         return oldPiece.clone();
  47.     }
  48.  
  49.     public ItemStack getNewPiece() {
  50.         return newPiece.clone();
  51.     }
  52.  
  53.     public void setNewPiece(ItemStack newPiece) {
  54.         this.newPiece = newPiece;
  55.         if(this.newPiece == null) this.newPiece = new ItemStack(Material.AIR);
  56.     }
  57.  
  58.     public void setOldPiece(ItemStack oldPiece) {
  59.         this.oldPiece = oldPiece;
  60.         if(this.oldPiece == null) this.oldPiece = new ItemStack(Material.AIR);
  61.     }
  62.  
  63.     /**
  64.      * Returns the Cause of the event
  65.      * @return Cause
  66.      */
  67.     public Cause getCause() {
  68.         return cause;
  69.     }
  70.  
  71.     /**
  72.      * Returns the ArmorType of the NewItem/Slot
  73.      * @return ArmorType
  74.      */
  75.     public ArmorType getArmorType() {
  76.         return type;
  77.     }
  78.  
  79.     /**
  80.      * Gets the slot where the new armor slot is
  81.      * @return int
  82.      */
  83.     public int getSlot() {
  84.         return slot;
  85.     }
  86.  
  87.     @Override
  88.     public boolean isCancelled() {
  89.         return cancelled;
  90.     }
  91.  
  92.     @Override
  93.     public void setCancelled(boolean cancelled) {
  94.         this.cancelled = cancelled;
  95.     }
  96.  
  97.     /**
  98.      * Is it going to try and set the armor piece to whatever the cursor/NewPiece is?
  99.      * @return void
  100.      */
  101.     public boolean isForced() {
  102.         return force;
  103.     }
  104.  
  105.     /**
  106.      * Set if it is going to try and set the armor piece to whatever the cursor/NewPiece
  107.      * @param force
  108.      * @return void
  109.      */
  110.     public void setForced(boolean force) {
  111.         this.force = force;
  112.     }
  113.  
  114.     /**
  115.      * Is the current cursor/NewItem an non armor type (or pumpkin/skull)
  116.      * @return void
  117.      */
  118.     public boolean isAttemptNonArmor() {
  119.         return attemptNonArmor;
  120.     }
  121.  
  122.     protected void setAttemptNonArmor(boolean attemptNonArmor) {
  123.         this.attemptNonArmor = attemptNonArmor;
  124.     }
  125.  
  126.     /**
  127.      * Is the current cursor/NewItem an armor type (or pumpkin/skull) but in the wrong slot
  128.      * @return void
  129.      */
  130.     public boolean isAttemptedWrongSlot() {
  131.         return attemptWrongSlot;
  132.     }
  133.  
  134.     protected void setAttemptedWrongSlot(boolean attemptWrongSlot) {
  135.         this.attemptWrongSlot = attemptWrongSlot;
  136.     }
  137.  
  138.     @Override
  139.     public HandlerList getHandlers() {
  140.         return HANDLERS;
  141.     }
  142.  
  143.     public static HandlerList getHandlerList() {
  144.         return HANDLERS;
  145.     }
  146.  
  147.     enum Cause {
  148.         DISPENSER, RIGHT_CLICK, TAKE, SET, SWAP, BREAK, CURSOR_COLLECT
  149.     }
  150.  
  151.     enum ArmorType{
  152.         HELMET(3) {
  153.             @Override
  154.             boolean isType(Material material) {
  155.                 return material.name().endsWith("_HELMET") || material==
  156.                         (findMaterial("CARVED_PUMPKIN")!=null?ArmorType.findMaterial("CARVED_PUMPKIN"):material == Material.PUMPKIN)
  157.                         || material == (findMaterial("SKULL")!=null?findMaterial("SKULL"):findMaterial("PLAYER_HEAD"));
  158.             }
  159.         },
  160.         CHESTPLATE(2) {
  161.             @Override
  162.             boolean isType(Material material) {
  163.                 return material.name().endsWith("_CHESTPLATE");
  164.             }
  165.         },
  166.         LEGGINGS(1) {
  167.             @Override
  168.             boolean isType(Material material) {
  169.                 return material.name().endsWith("_LEGGINGS");
  170.             }
  171.         },
  172.         BOOTS(0) {
  173.             @Override
  174.             boolean isType(Material material) {
  175.                 return material.name().endsWith("_BOOTS");
  176.             }
  177.         };
  178.  
  179.         private int id = 0;
  180.         ArmorType(int id) {
  181.             this.id = id;
  182.         }
  183.  
  184.         public int getId() {
  185.             return id;
  186.         }
  187.  
  188.         abstract boolean isType(Material material);
  189.  
  190.         public static ArmorType fromSlot(int slot) {
  191.             return slot == 36 ? BOOTS : slot == 37 ? LEGGINGS : slot == 38 ? CHESTPLATE : slot == 39 ? HELMET : null;
  192.         }
  193.         public static ArmorType fromMaterial(Material type) {
  194.             for(ArmorType armorType : values())
  195.                 if(armorType.isType(type))return armorType;
  196.             return null;
  197.         }
  198.         public static ArmorType fromItem(@Nullable ItemStack type) {
  199.             if(type == null) return null;
  200.             return fromMaterial(type.getType());
  201.         }
  202.  
  203.         private static Material findMaterial(String name) {
  204.             for(Material mat : Material.values())
  205.                 if(mat.name().equalsIgnoreCase(name))return mat;
  206.             return null;
  207.         }
  208.  
  209.     }
  210. }
  211.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement