Skip to content

Commit cd6e75d

Browse files
committed
Support body/saddle slots in equipment functions
Also fixes keys in get_equipment_droprates() to match get_mob_equipment().
1 parent 2a2cf2d commit cd6e75d

File tree

8 files changed

+93
-65
lines changed

8 files changed

+93
-65
lines changed

src/main/java/com/laytonsmith/abstraction/MCEntityEquipment.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ public interface MCEntityEquipment {
77

88
void clearEquipment();
99

10-
int getSize();
11-
1210
MCEntity getHolder();
1311

1412
Map<MCEquipmentSlot, MCItemStack> getAllEquipment();

src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCEntityEquipment.java

Lines changed: 58 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
import com.laytonsmith.abstraction.MCEntityEquipment;
55
import com.laytonsmith.abstraction.MCItemStack;
66
import com.laytonsmith.abstraction.enums.MCEquipmentSlot;
7+
import com.laytonsmith.abstraction.enums.MCVersion;
8+
import com.laytonsmith.core.Static;
79
import org.bukkit.inventory.EntityEquipment;
10+
import org.bukkit.inventory.EquipmentSlot;
11+
import org.bukkit.inventory.ItemStack;
812

913
import java.util.EnumMap;
1014
import java.util.Map;
@@ -22,11 +26,6 @@ public void clearEquipment() {
2226
ee.clear();
2327
}
2428

25-
@Override
26-
public int getSize() {
27-
return MCEquipmentSlot.values().length;
28-
}
29-
3029
@Override
3130
public MCEntity getHolder() {
3231
return BukkitConvertor.BukkitGetCorrectEntity(ee.getHolder());
@@ -35,26 +34,23 @@ public MCEntity getHolder() {
3534
@Override
3635
public Map<MCEquipmentSlot, MCItemStack> getAllEquipment() {
3736
Map<MCEquipmentSlot, MCItemStack> slots = new EnumMap<>(MCEquipmentSlot.class);
38-
for(MCEquipmentSlot key : MCEquipmentSlot.values()) {
39-
switch(key) {
40-
case WEAPON:
41-
slots.put(key, getWeapon());
42-
break;
43-
case OFF_HAND:
44-
slots.put(key, getItemInOffHand());
45-
break;
46-
case HELMET:
47-
slots.put(key, getHelmet());
48-
break;
49-
case CHESTPLATE:
50-
slots.put(key, getChestplate());
51-
break;
52-
case LEGGINGS:
53-
slots.put(key, getLeggings());
54-
break;
55-
case BOOTS:
56-
slots.put(key, getBoots());
57-
break;
37+
slots.put(MCEquipmentSlot.WEAPON, getWeapon());
38+
slots.put(MCEquipmentSlot.OFF_HAND, getItemInOffHand());
39+
slots.put(MCEquipmentSlot.HELMET, getHelmet());
40+
slots.put(MCEquipmentSlot.CHESTPLATE, getChestplate());
41+
slots.put(MCEquipmentSlot.LEGGINGS, getLeggings());
42+
slots.put(MCEquipmentSlot.BOOTS, getBoots());
43+
BukkitMCServer server = (BukkitMCServer) Static.getServer();
44+
if(server.getMinecraftVersion().gte(MCVersion.MC1_20_6)) {
45+
try {
46+
slots.put(MCEquipmentSlot.BODY, new BukkitMCItemStack(ee.getItem(EquipmentSlot.BODY)));
47+
} catch(IllegalArgumentException ignored) {
48+
// API says it can throw an exception here, but it never seems to do so.
49+
}
50+
if(server.getMinecraftVersion().gte(MCVersion.MC1_21_5)) {
51+
try {
52+
slots.put(MCEquipmentSlot.SADDLE, new BukkitMCItemStack(ee.getItem(EquipmentSlot.SADDLE)));
53+
} catch(IllegalArgumentException ignored) {}
5854
}
5955
}
6056
return slots;
@@ -84,33 +80,40 @@ public void setAllEquipment(Map<MCEquipmentSlot, MCItemStack> slots) {
8480
case BOOTS:
8581
setBoots(stack);
8682
break;
83+
case BODY:
84+
if(Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_20_6)) {
85+
try {
86+
ee.setItem(EquipmentSlot.BODY, (ItemStack) stack.getHandle());
87+
} catch(IllegalArgumentException ignored) {
88+
// API says it can throw an exception here, but it never seems to do so.
89+
}
90+
}
91+
break;
92+
case SADDLE:
93+
if(Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_21_5)) {
94+
try {
95+
ee.setItem(EquipmentSlot.SADDLE, (ItemStack) stack.getHandle());
96+
} catch(IllegalArgumentException ignored) {}
97+
}
98+
break;
8799
}
88100
}
89101
}
90102

91103
@Override
92104
public Map<MCEquipmentSlot, Float> getAllDropChances() {
93105
Map<MCEquipmentSlot, Float> slots = new EnumMap<>(MCEquipmentSlot.class);
94-
for(MCEquipmentSlot key : MCEquipmentSlot.values()) {
95-
switch(key) {
96-
case WEAPON:
97-
slots.put(key, getWeaponDropChance());
98-
break;
99-
case OFF_HAND:
100-
slots.put(key, getOffHandDropChance());
101-
break;
102-
case HELMET:
103-
slots.put(key, getHelmetDropChance());
104-
break;
105-
case CHESTPLATE:
106-
slots.put(key, getChestplateDropChance());
107-
break;
108-
case LEGGINGS:
109-
slots.put(key, getLeggingsDropChance());
110-
break;
111-
case BOOTS:
112-
slots.put(key, getBootsDropChance());
113-
break;
106+
slots.put(MCEquipmentSlot.WEAPON, getWeaponDropChance());
107+
slots.put(MCEquipmentSlot.OFF_HAND, getOffHandDropChance());
108+
slots.put(MCEquipmentSlot.HELMET, getHelmetDropChance());
109+
slots.put(MCEquipmentSlot.CHESTPLATE, getChestplateDropChance());
110+
slots.put(MCEquipmentSlot.LEGGINGS, getLeggingsDropChance());
111+
slots.put(MCEquipmentSlot.BOOTS, getBootsDropChance());
112+
BukkitMCServer server = (BukkitMCServer) Static.getServer();
113+
if(server.getMinecraftVersion().gte(MCVersion.MC1_20_6)) {
114+
slots.put(MCEquipmentSlot.BODY, ee.getDropChance(EquipmentSlot.BODY));
115+
if(server.getMinecraftVersion().gte(MCVersion.MC1_21_5)) {
116+
slots.put(MCEquipmentSlot.SADDLE, ee.getDropChance(EquipmentSlot.SADDLE));
114117
}
115118
}
116119
return slots;
@@ -140,6 +143,16 @@ public void setAllDropChances(Map<MCEquipmentSlot, Float> slots) {
140143
case BOOTS:
141144
setBootsDropChance(chance);
142145
break;
146+
case BODY:
147+
if(Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_20_6)) {
148+
ee.setDropChance(EquipmentSlot.BODY, chance);
149+
}
150+
break;
151+
case SADDLE:
152+
if(Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_21_5)) {
153+
ee.setDropChance(EquipmentSlot.SADDLE, chance);
154+
}
155+
break;
143156
}
144157
}
145158
}

src/main/java/com/laytonsmith/abstraction/bukkit/entities/BukkitMCLivingEntity.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.bukkit.entity.Player;
3535
import org.bukkit.event.entity.EntityDamageEvent;
3636
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
37+
import org.bukkit.inventory.EntityEquipment;
3738
import org.bukkit.potion.PotionEffect;
3839
import org.bukkit.potion.PotionEffectType;
3940
import org.bukkit.util.BlockIterator;
@@ -313,10 +314,11 @@ public LivingEntity asLivingEntity() {
313314

314315
@Override
315316
public MCEntityEquipment getEquipment() {
316-
if(le.getEquipment() == null) {
317+
EntityEquipment eq = le.getEquipment();
318+
if(eq == null) {
317319
return null;
318320
}
319-
return new BukkitMCEntityEquipment(le.getEquipment());
321+
return new BukkitMCEntityEquipment(eq);
320322
}
321323

322324
@Override

src/main/java/com/laytonsmith/abstraction/bukkit/entities/BukkitMCPlayer.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,16 @@ public void sendEquipmentChange(MCLivingEntity entity, MCEquipmentSlot slot, MCI
917917
case LEGGINGS -> p.sendEquipmentChange(le, EquipmentSlot.LEGS, is);
918918
case CHESTPLATE -> p.sendEquipmentChange(le, EquipmentSlot.CHEST, is);
919919
case HELMET -> p.sendEquipmentChange(le, EquipmentSlot.HEAD, is);
920+
case BODY -> {
921+
if(Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_20_6)) {
922+
p.sendEquipmentChange(le, EquipmentSlot.BODY, is);
923+
}
924+
}
925+
case SADDLE -> {
926+
if(Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_21_5)) {
927+
p.sendEquipmentChange(le, EquipmentSlot.SADDLE, is);
928+
}
929+
}
920930
}
921931
} catch(NoSuchMethodError ex) {
922932
// probably before 1.18, which is unsupported

src/main/java/com/laytonsmith/abstraction/enums/MCEquipmentSlot.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ public enum MCEquipmentSlot {
99
BOOTS,
1010
LEGGINGS,
1111
CHESTPLATE,
12-
HELMET
12+
HELMET,
13+
BODY,
14+
SADDLE,
1315
}

src/main/java/com/laytonsmith/abstraction/enums/bukkit/BukkitMCEquipmentSlot.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ protected MCEquipmentSlot getAbstractedEnumCustom(EquipmentSlot concrete) {
2727
switch(concrete) {
2828
case HAND:
2929
return MCEquipmentSlot.WEAPON;
30-
case OFF_HAND:
31-
return MCEquipmentSlot.OFF_HAND;
3230
case FEET:
3331
return MCEquipmentSlot.BOOTS;
3432
case LEGS:
@@ -46,8 +44,6 @@ protected EquipmentSlot getConcreteEnumCustom(MCEquipmentSlot abstracted) {
4644
switch(abstracted) {
4745
case WEAPON:
4846
return EquipmentSlot.HAND;
49-
case OFF_HAND:
50-
return EquipmentSlot.OFF_HAND;
5147
case BOOTS:
5248
return EquipmentSlot.FEET;
5349
case LEGGINGS:

src/main/java/com/laytonsmith/core/functions/MobManagement.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.laytonsmith.abstraction.enums.MCPotionEffectType;
2424
import com.laytonsmith.abstraction.enums.MCVersion;
2525
import com.laytonsmith.annotations.api;
26+
import com.laytonsmith.annotations.seealso;
2627
import com.laytonsmith.core.ArgumentValidation;
2728
import com.laytonsmith.core.MSLog;
2829
import com.laytonsmith.core.MSVersion;
@@ -696,6 +697,7 @@ public MSVersion since() {
696697
}
697698

698699
@api(environments = {CommandHelperEnvironment.class})
700+
@seealso(set_mob_equipment.class)
699701
public static class get_mob_equipment extends EntityManagement.EntityGetterFunction {
700702

701703
@Override
@@ -721,7 +723,9 @@ public String getName() {
721723
@Override
722724
public String docs() {
723725
return "array {entityUUID} Returns an associative array showing the equipment this mob is wearing."
724-
+ " This only works on mobs and armor stands.";
726+
+ " The equipment slots are: weapon, off_hand, helmet, chestplate, leggings, boots,"
727+
+ " body (MC 1.20.6+), and saddle (MC 1.21.5+)."
728+
+ " This works on mobs, players, mannequins, and armor stands.";
725729
}
726730

727731
@Override
@@ -742,6 +746,7 @@ public MSVersion since() {
742746
}
743747

744748
@api(environments = {CommandHelperEnvironment.class})
749+
@seealso(get_mob_equipment.class)
745750
public static class set_mob_equipment extends EntityManagement.EntitySetterFunction {
746751

747752
@Override
@@ -751,11 +756,12 @@ public Mixed exec(Target t, Environment environment, Mixed... args) throws Confi
751756
if(ee == null) {
752757
throw new CREBadEntityTypeException("Entities of type \"" + le.getType() + "\" do not have equipment.", t);
753758
}
754-
Map<MCEquipmentSlot, MCItemStack> eq = ee.getAllEquipment();
755759
if(args[1] instanceof CNull) {
756760
ee.clearEquipment();
757761
return CVoid.VOID;
758-
} else if(args[1].isInstanceOf(CArray.TYPE)) {
762+
}
763+
Map<MCEquipmentSlot, MCItemStack> eq = ee.getAllEquipment();
764+
if(args[1].isInstanceOf(CArray.TYPE)) {
759765
CArray ea = (CArray) args[1];
760766
for(String key : ea.stringKeySet()) {
761767
try {
@@ -779,17 +785,18 @@ public String getName() {
779785
@Override
780786
public String docs() {
781787
return "void {entityUUID, array} Takes an associative array with keys representing equipment slots and"
782-
+ " values of itemArrays, the same used by set_pinv(). This only works on mobs and armor stands."
783-
+ " Unless a mod, plugin, or future update changes vanilla functionality,"
784-
+ " only humanoid mobs will render their equipment slots. The equipment slots are: "
785-
+ StringUtils.Join(MCEquipmentSlot.values(), ", ", ", or ", " or ");
788+
+ " values of item arrays. The equipment slots are: weapon, off_hand, helmet, chestplate, leggings,"
789+
+ " boots, body (MC 1.20.6+), and saddle (MC 1.21.5+)."
790+
+ " This works on mobs, players, mannequins, and armor stands."
791+
+ " While you may set any slot for any of these entities, some slots are not used by some entities."
792+
+ " Setting unused slots is not officially supported behavior, so your results may vary.";
786793
}
787794

788795
@Override
789796
public ExampleScript[] examples() throws ConfigCompileException {
790797
return new ExampleScript[]{
791798
new ExampleScript("Basic usage",
792-
"set_mob_equipment(spawn_entity('SKELETON')[0], array(WEAPON: array(name: 'BOW')))",
799+
"set_mob_equipment(spawn_entity('SKELETON')[0], array(weapon: array(name: 'BOW')))",
793800
"Gives a bow to a skeleton")
794801
};
795802
}
@@ -874,7 +881,7 @@ public Mixed exec(Target t, Environment environment, Mixed... args) throws Confi
874881
}
875882
CArray ret = CArray.GetAssociativeArray(t);
876883
for(Map.Entry<MCEquipmentSlot, Float> ent : eq.getAllDropChances().entrySet()) {
877-
ret.set(ent.getKey().name(), new CDouble(ent.getValue(), t), t);
884+
ret.set(ent.getKey().name().toLowerCase(), new CDouble(ent.getValue(), t), t);
878885
}
879886
return ret;
880887
}

src/main/java/com/laytonsmith/core/functions/PlayerManagement.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7146,8 +7146,8 @@ public String docs() {
71467146
return "void {[player], entityUUID, equipmentArray} Changes a living entity's equipment only for the"
71477147
+ " specified player. (MC 1.18+) Equipment array can be null to make all equipment not visible."
71487148
+ " Otherwise equipment array must be an associative array where the keys are equipment slots and"
7149-
+ " the values are item arrays or null. The equipment slots are: "
7150-
+ StringUtils.Join(MCEquipmentSlot.values(), ", ", ", or ", " or ");
7149+
+ " the values are item arrays or null. The equipment slots are: weapon, off_hand, helmet,"
7150+
+ " chestplate, leggings, boots, body (MC 1.20.6+), and saddle (MC 1.21.5+).";
71517151
}
71527152

71537153
@Override

0 commit comments

Comments
 (0)