Skip to content

Commit daf8c53

Browse files
committed
Fix inconsistency getting an item from a player slot
Previously when getting the item in hand, it would return an empty item stack instead of null. This was inconsistent with all other slot values, resulting in unexpected behavior and core errors in some item meta functions. To fix this inconsistent function behavior, I decided to return empty values where that would have already been a handled output, avoiding breaking any scripts where possible. This also matches the previous behavior of the most common use cases.
1 parent ababae6 commit daf8c53

File tree

3 files changed

+30
-33
lines changed

3 files changed

+30
-33
lines changed

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

+10-9
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,15 @@ public MCPlayerInventory getInventory() {
127127

128128
@Override
129129
public MCItemStack getItemAt(Integer slot) {
130-
if(slot == null) {
131-
return new BukkitMCItemStack(p.getInventory().getItemInMainHand());
132-
}
133130
ItemStack is = null;
134-
//Special slots
135-
if(slot == 100) {
131+
if(slot == null) {
132+
is = p.getInventory().getItemInMainHand();
133+
// Empty slots should return null, but unlike other PlayerInventory methods,
134+
// getItemInMainHand() never returns null.
135+
if(is.getType() == Material.AIR) {
136+
return null;
137+
}
138+
} else if(slot == 100) {
136139
is = p.getInventory().getBoots();
137140
} else if(slot == 101) {
138141
is = p.getInventory().getLeggings();
@@ -142,15 +145,13 @@ public MCItemStack getItemAt(Integer slot) {
142145
is = p.getInventory().getHelmet();
143146
} else if(slot == -106) {
144147
is = p.getInventory().getItemInOffHand();
145-
}
146-
if(slot >= 0 && slot <= 35) {
148+
} else if(slot >= 0 && slot <= 35) {
147149
is = p.getInventory().getItem(slot);
148150
}
149151
if(is == null) {
150152
return null;
151-
} else {
152-
return new BukkitMCItemStack(is);
153153
}
154+
return new BukkitMCItemStack(is);
154155
}
155156

156157
@Override

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ public Mixed exec(Target t, Environment environment, Mixed... args) throws Confi
343343
}
344344
MCItemStack is = p.getItemAt(slot instanceof CNull ? null : ArgumentValidation.getInt32(slot, t));
345345
if(is == null) {
346-
throw new CRECastException("There is no item at slot " + slot, t);
346+
return new CArray(t);
347347
}
348348
return ObjectGenerator.GetGenerator().enchants(is.getEnchantments(), t);
349349
}

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

+19-23
Original file line numberDiff line numberDiff line change
@@ -77,24 +77,18 @@ public Boolean runAsync() {
7777

7878
@Override
7979
public Mixed exec(Target t, Environment environment, Mixed... args) throws ConfigRuntimeException {
80-
MCPlayer p = environment.getEnv(CommandHelperEnvironment.class).GetPlayer();
80+
MCPlayer p;
8181
MCItemStack is;
8282
Mixed slot;
8383
if(args.length == 2) {
8484
p = Static.GetPlayer(args[0], t);
8585
slot = args[1];
8686
} else {
87+
p = environment.getEnv(CommandHelperEnvironment.class).GetPlayer();
88+
Static.AssertPlayerNonNull(p, t);
8789
slot = args[0];
8890
}
89-
Static.AssertPlayerNonNull(p, t);
90-
if(slot instanceof CNull) {
91-
is = p.getItemAt(null);
92-
} else {
93-
is = p.getItemAt(ArgumentValidation.getInt32(slot, t));
94-
}
95-
if(is == null) {
96-
throw new CRECastException("There is no item at slot " + slot, t);
97-
}
91+
is = p.getItemAt(slot instanceof CNull ? null : ArgumentValidation.getInt32(slot, t));
9892
return ObjectGenerator.GetGenerator().itemMeta(is, t);
9993
}
10094

@@ -186,7 +180,7 @@ public Boolean runAsync() {
186180

187181
@Override
188182
public Mixed exec(Target t, Environment environment, Mixed... args) throws ConfigRuntimeException {
189-
MCPlayer p = environment.getEnv(CommandHelperEnvironment.class).GetPlayer();
183+
MCPlayer p;
190184
Mixed slot;
191185
Mixed meta;
192186
MCItemStack is;
@@ -195,15 +189,12 @@ public Mixed exec(Target t, Environment environment, Mixed... args) throws Confi
195189
slot = args[1];
196190
meta = args[2];
197191
} else {
192+
p = environment.getEnv(CommandHelperEnvironment.class).GetPlayer();
193+
Static.AssertPlayerNonNull(p, t);
198194
slot = args[0];
199195
meta = args[1];
200196
}
201-
Static.AssertPlayerNonNull(p, t);
202-
if(slot instanceof CNull) {
203-
is = p.getItemAt(null);
204-
} else {
205-
is = p.getItemAt(ArgumentValidation.getInt32(slot, t));
206-
}
197+
is = p.getItemAt(slot instanceof CNull ? null : ArgumentValidation.getInt32(slot, t));
207198
if(is == null) {
208199
throw new CRECastException("There is no item at slot " + slot, t);
209200
}
@@ -441,16 +432,21 @@ public Boolean runAsync() {
441432

442433
@Override
443434
public Mixed exec(Target t, Environment environment, Mixed... args) throws ConfigRuntimeException {
444-
MCPlayer p = environment.getEnv(CommandHelperEnvironment.class).GetPlayer();
445-
int slot;
435+
MCPlayer p;
436+
Mixed slot;
446437
if(args.length == 2) {
447438
p = Static.GetPlayer(args[0], t);
448-
slot = ArgumentValidation.getInt32(args[1], t);
439+
slot = args[1];
449440
} else {
450-
slot = ArgumentValidation.getInt32(args[0], t);
441+
p = environment.getEnv(CommandHelperEnvironment.class).GetPlayer();
442+
Static.AssertPlayerNonNull(p, t);
443+
slot = args[0];
451444
}
452-
Static.AssertPlayerNonNull(p, t);
453-
return CBoolean.get(p.getItemAt(slot).getItemMeta() instanceof MCLeatherArmorMeta);
445+
MCItemStack is = p.getItemAt(slot instanceof CNull ? null : ArgumentValidation.getInt32(slot, t));
446+
if(is == null) {
447+
return CBoolean.FALSE;
448+
}
449+
return CBoolean.get(is.getItemMeta() instanceof MCLeatherArmorMeta);
454450
}
455451

456452
@Override

0 commit comments

Comments
 (0)