Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EffEquip enhancements #6842

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions src/main/java/ch/njol/skript/effects/EffEquip.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@
import org.bukkit.Material;
import org.bukkit.entity.AbstractHorse;
import org.bukkit.entity.ChestedHorse;
import org.bukkit.entity.Horse;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Llama;
import org.bukkit.entity.Pig;
import org.bukkit.entity.Player;
import org.bukkit.entity.Steerable;
import org.bukkit.entity.Wolf;
import org.bukkit.event.Event;
import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.LlamaInventory;
import org.bukkit.inventory.*;
import org.eclipse.jdt.annotation.Nullable;

import ch.njol.skript.Skript;
Expand Down Expand Up @@ -92,6 +91,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 boolean SUPPORTS_WOLF_ARMOR = Skript.fieldExists(Material.class, "WOLF_ARMOR");

private static final ItemType CHESTPLATE = Aliases.javaItemType("chestplate");
private static final ItemType LEGGINGS = Aliases.javaItemType("leggings");
Expand All @@ -100,8 +100,14 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
private static final ItemType SADDLE = Aliases.javaItemType("saddle");
private static final ItemType CHEST = Aliases.javaItemType("chest");
private static final ItemType CARPET = Aliases.javaItemType("carpet");
private static ItemType WOLF_ARMOR = new ItemType();

private static final ItemType[] ALL_EQUIPMENT = new ItemType[] {CHESTPLATE, LEGGINGS, BOOTS, HORSE_ARMOR, SADDLE, CHEST, CARPET};
static {
if (SUPPORTS_WOLF_ARMOR)
WOLF_ARMOR = Aliases.javaItemType("wolf armor");
}

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

@Override
protected void execute(Event event) {
Expand Down Expand Up @@ -139,19 +145,27 @@ 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();
AbstractHorseInventory inv = ((AbstractHorse) entity).getInventory();
for (ItemType itemType : itemTypes) {
for (ItemStack item : itemType.getAll()) {
if (SADDLE.isOfType(item)) {
inv.setItem(0, equip ? item : null); // Slot 0=saddle
} else if (HORSE_ARMOR.isOfType(item)) {
inv.setItem(1, equip ? item : null); // Slot 1=armor
} else if (CHEST.isOfType(item) && entity instanceof ChestedHorse) {
inv.setSaddle(equip ? item : null);
} else if (HORSE_ARMOR.isOfType(item) && entity instanceof Horse) {
((HorseInventory) inv).setArmor(equip ? item : null);
} else if (CHEST.isOfType(item) && entity instanceof ChestedHorse) { // a Donkey, Mule, Llama or TraderLlama. NOT a Horse
((ChestedHorse) entity).setCarryingChest(equip);
}
}
}
} else if (entity instanceof Wolf) {
EntityEquipment equipment = ((Wolf) entity).getEquipment();
for (ItemType itemType : itemTypes) {
for (ItemStack item : itemType.getAll()) {
if (WOLF_ARMOR.isOfType(item)) {
equipment.setItem(EquipmentSlot.BODY, equip ? item : null);
}
}
}
} else {
EntityEquipment equipment = entity.getEquipment();
if (equipment == null)
Expand Down