Fix /clear <player> <item> not clearing offhand (#3715)

Co-authored-by: Kenichycmd <kenichycmd@gmail.com>
Co-authored-by: MD <1917406+mdcfe@users.noreply.github.com>
This commit is contained in:
Kenichycmd 2020-11-09 08:43:55 -08:00 committed by GitHub
parent 5b0c2a4131
commit 80fb91b6d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 1 deletions

View File

@ -137,7 +137,7 @@ public class Commandclearinventory extends EssentialsCommand {
if (amount == -1) {
stack.setAmount(BASE_AMOUNT);
final ItemStack removedStack = player.getInventory().removeItem(stack).get(0);
final int removedAmount = BASE_AMOUNT - removedStack.getAmount();
final int removedAmount = BASE_AMOUNT - removedStack.getAmount() + InventoryWorkaround.clearItemInOffHand(player, stack);
if (removedAmount > 0 || showExtended) {
sender.sendMessage(tl("inventoryClearingStack", removedAmount, stack.getType().toString().toLowerCase(Locale.ENGLISH), player.getDisplayName()));
}

View File

@ -267,4 +267,22 @@ public final class InventoryWorkaround {
}
}
}
public static int clearItemInOffHand(final Player p, final ItemStack item) {
// This should be added because if `/clear` itself is not initilized it will return an Error: null.
if (hasMainHandSupport == null || hasMainHandSupport) {
try {
int removedAmount = 0;
if (p.getInventory().getItemInOffHand().getType().equals(item.getType())) {
removedAmount = p.getInventory().getItemInOffHand().getAmount();
p.getInventory().setItemInOffHand(null);
}
hasMainHandSupport = true;
return removedAmount;
} catch (final Throwable e) {
hasMainHandSupport = false;
}
}
return 0;
}
}