Skip to content

Commit

Permalink
Additions/Improvements
Browse files Browse the repository at this point in the history
- Improve unequip performance
- Add Elytra support
  • Loading branch information
AyhamAl-Ali committed Apr 5, 2024
1 parent fd52802 commit a39277d
Showing 1 changed file with 44 additions and 18 deletions.
62 changes: 44 additions & 18 deletions src/main/java/ch/njol/skript/effects/EffEquip.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"equip player with stained glass pane as a hat",
"equip player with diamond sword"
})
@Since("1.0, 2.7 (multiple entities, unequip), INSERT VERSION (items)")
@Since("1.0, 2.7 (multiple entities, unequip), INSERT VERSION (giving items/as hat)")
public class EffEquip extends Effect {

static {
Expand Down Expand Up @@ -100,6 +100,7 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
private static final boolean SUPPORTS_STEERABLE = Skript.classExists("org.bukkit.entity.Steerable");

private static final ItemType HELMET = Aliases.javaItemType("helmet");
private static final ItemType ELYTRA = Aliases.javaItemType("elytra");
private static final ItemType CHESTPLATE = Aliases.javaItemType("chestplate");
private static final ItemType LEGGINGS = Aliases.javaItemType("leggings");
private static final ItemType BOOTS = Aliases.javaItemType("boots");
Expand All @@ -108,26 +109,32 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
private static final ItemType CHEST = Aliases.javaItemType("chest");
private static final ItemType CARPET = Aliases.javaItemType("carpet");

private static final ItemType[] ALL_EQUIPMENT = new ItemType[] {CHESTPLATE, LEGGINGS, BOOTS, HORSE_ARMOR, SADDLE, CHEST, CARPET};

@Override
protected void execute(Event event) {
ItemType[] itemTypes;
boolean unequipHelmet = false;
if (this.itemTypes != null) {
itemTypes = this.itemTypes.getArray(event);
} else {
itemTypes = ALL_EQUIPMENT;
unequipHelmet = true;
itemTypes = new ItemType[0];
}
boolean isUnequipAll = !equip && itemTypes.length == 0;
for (LivingEntity entity : entities.getArray(event)) {
if (SUPPORTS_STEERABLE && entity instanceof Steerable) {
if (isUnequipAll) { // shortcut
((Steerable) entity).setSaddle(false);
continue;
}
for (ItemType itemType : itemTypes) {
if (SADDLE.isOfType(itemType.getMaterial())) {
((Steerable) entity).setSaddle(equip);
break;
}
}
} else if (entity instanceof Pig) {
if (isUnequipAll) { // shortcut
((Pig) entity).setSaddle(false);
continue;
}
for (ItemType itemType : itemTypes) {
if (itemType.isOfType(Material.SADDLE)) {
((Pig) entity).setSaddle(equip);
Expand All @@ -136,6 +143,11 @@ protected void execute(Event event) {
}
} else if (entity instanceof Llama) {
LlamaInventory inv = ((Llama) entity).getInventory();
if (isUnequipAll) { // shortcut
inv.setDecor(null);
((Llama) entity).setCarryingChest(false);
continue;
}
for (ItemType itemType : itemTypes) {
for (ItemStack item : itemType.getAll()) {
if (CARPET.isOfType(item)) {
Expand All @@ -148,6 +160,13 @@ protected void execute(Event event) {
} else if (entity instanceof AbstractHorse) {
// Spigot's API is bad, just bad... Abstract horse doesn't have horse inventory!
Inventory inv = ((AbstractHorse) entity).getInventory();
if (isUnequipAll) { // shortcut
inv.setItem(0, null);
inv.setItem(1, null);
if (entity instanceof ChestedHorse)
((ChestedHorse) entity).setCarryingChest(false);
continue;
}
for (ItemType itemType : itemTypes) {
for (ItemStack item : itemType.getAll()) {
if (SADDLE.isOfType(item)) {
Expand All @@ -159,31 +178,38 @@ protected void execute(Event event) {
}
}
}
} else {
} else { // players and other entities
EntityEquipment equipment = entity.getEquipment();
if (equipment == null)
continue;
boolean isPlayer = entity instanceof Player;
if (isUnequipAll) { // shortcut
// We shouldn't affect player's inventory by removing anything other than armor
equipment.setHelmet(null);
equipment.setChestplate(null);
equipment.setLeggings(null);
equipment.setBoots(null);
if (isPlayer)
PlayerUtils.updateInventory((Player) entity);
continue;
}
for (ItemType itemType : itemTypes) {
for (ItemStack item : itemType.getAll()) {
if (CHESTPLATE.isOfType(item)) {
if (isHat || HELMET.isOfType(item)) {
// Apply all other items to head (if isHat), as all items will appear on a player's head
equipment.setHelmet(equip ? item : null);
} else if (CHESTPLATE.isOfType(item) || ELYTRA.isOfType(item)) {
equipment.setChestplate(equip ? item : null);
} else if (LEGGINGS.isOfType(item)) {
equipment.setLeggings(equip ? item : null);
} else if (BOOTS.isOfType(item)) {
equipment.setBoots(equip ? item : null);
} else if (isHat || HELMET.isOfType(item)) {
// Apply all other items to head (if isHat), as all items will appear on a player's head
equipment.setHelmet(equip ? item : null);
} else {
if (entity instanceof Player)
((Player) entity).getInventory().addItem(item);
} else if (isEquipWith && isPlayer && item != null) { // only to players
((Player) entity).getInventory().addItem(item);
}
}
if (unequipHelmet) { // Since players can wear any helmet, itemTypes won't have the item in the array every time
equipment.setHelmet(null);
}
}
if (entity instanceof Player)
if (isPlayer)
PlayerUtils.updateInventory((Player) entity);
}
}
Expand Down

0 comments on commit a39277d

Please sign in to comment.