Make drop-items-if-full work on Essentials kits

This commit is contained in:
rye761 2019-10-13 17:00:11 -04:00
parent bae909ba10
commit 785b1fe937
4 changed files with 48 additions and 9 deletions

View File

@ -141,17 +141,18 @@ public class Kit {
}
}
public void expandItems(final User user) throws Exception {
expandItems(user, getItems(user));
public boolean expandItems(final User user) throws Exception {
return expandItems(user, getItems(user));
}
public void expandItems(final User user, final List<String> items) throws Exception {
public boolean expandItems(final User user, final List<String> items) throws Exception {
try {
IText input = new SimpleTextInput(items);
IText output = new KeywordReplacer(input, user.getSource(), ess, true, true);
boolean spew = false;
final boolean allowUnsafe = ess.getSettings().allowUnsafeEnchantments();
List<ItemStack> itemList = new ArrayList<>();
for (String kitItem : output.getLines()) {
if (kitItem.startsWith(ess.getSettings().getCurrencySymbol())) {
BigDecimal value = new BigDecimal(kitItem.substring(ess.getSettings().getCurrencySymbol().length()).trim());
@ -181,13 +182,19 @@ public class Kit {
// We pass a null sender here because kits should not do perm checks
metaStack.parseStringMeta(null, allowUnsafe, parts, 2, ess);
}
final Map<Integer, ItemStack> overfilled;
final boolean allowOversizedStacks = user.isAuthorized("essentials.oversizedstacks");
itemList.add(metaStack.getItemStack());
}
final Map<Integer, ItemStack> overfilled;
final boolean allowOversizedStacks = user.isAuthorized("essentials.oversizedstacks");
final boolean isDropItemsIfFull = ess.getSettings().isDropItemsIfFull();
if (isDropItemsIfFull) {
if (allowOversizedStacks) {
overfilled = InventoryWorkaround.addOversizedItems(user.getBase().getInventory(), ess.getSettings().getOversizedStackSize(), metaStack.getItemStack());
overfilled = InventoryWorkaround.addOversizedItems(user.getBase().getInventory(), ess.getSettings().getOversizedStackSize(), itemList.toArray(new ItemStack[itemList.size()]));
} else {
overfilled = InventoryWorkaround.addItems(user.getBase().getInventory(), metaStack.getItemStack());
overfilled = InventoryWorkaround.addItems(user.getBase().getInventory(), itemList.toArray(new ItemStack[itemList.size()]));
}
for (ItemStack itemStack : overfilled.values()) {
int spillAmount = itemStack.getAmount();
@ -200,6 +207,16 @@ public class Kit {
}
spew = true;
}
} else {
if (allowOversizedStacks) {
overfilled = InventoryWorkaround.addAllOversizedItems(user.getBase().getInventory(), ess.getSettings().getOversizedStackSize(), itemList.toArray(new ItemStack[itemList.size()]));
} else {
overfilled = InventoryWorkaround.addAllItems(user.getBase().getInventory(), itemList.toArray(new ItemStack[itemList.size()]));
}
if (overfilled != null) {
user.sendMessage(tl("kitInvFullNoDrop"));
return false;
}
}
user.getBase().updateInventory();
if (spew) {
@ -210,5 +227,6 @@ public class Kit {
ess.getLogger().log(Level.WARNING, e.getMessage());
throw new Exception(tl("kitError2"), e);
}
return true;
}
}

View File

@ -81,8 +81,9 @@ public class Commandkit extends EssentialsCommand {
kit.checkDelay(userFrom);
kit.checkAffordable(userFrom);
if (!kit.expandItems(userTo))
continue;
kit.setTime(userFrom);
kit.expandItems(userTo);
kit.chargeUser(userTo);
if (!userFrom.equals(userTo)) {

View File

@ -80,6 +80,25 @@ public final class InventoryWorkaround {
}
return addItems(fakeInventory, items);
}
public static Map<Integer, ItemStack> addAllOversizedItems(final Inventory inventory, final int oversizedStacks, final ItemStack... items) {
ItemStack[] contents = inventory.getContents();
final Inventory fakeInventory;
if (isCombinedInventory(inventory)) {
fakeInventory = makeTruncatedPlayerInventory((PlayerInventory) inventory);
} else {
fakeInventory = Bukkit.getServer().createInventory(null, inventory.getType());
fakeInventory.setContents(contents);
}
Map<Integer, ItemStack> overflow = addOversizedItems(fakeInventory, oversizedStacks, items);
if (overflow.isEmpty()) {
addOversizedItems(inventory, oversizedStacks, items);
return null;
}
return overflow;
}
// Returns what it couldn't store
public static Map<Integer, ItemStack> addItems(final Inventory inventory, final ItemStack... items) {

View File

@ -252,6 +252,7 @@ kitError=\u00a74There are no valid kits.
kitError2=\u00a74That kit is improperly defined. Contact an administrator.
kitGiveTo=\u00a76Giving kit\u00a7c {0}\u00a76 to \u00a7c{1}\u00a76.
kitInvFull=\u00a74Your inventory was full, placing kit on the floor.
kitInvFullNoDrop=\u00a74There is not enough room in your inventory for that kit.
kitItem=\u00a76- \u00a7f{0}
kitNotFound=\u00a74That kit does not exist.
kitOnce=\u00a74You can''t use that kit again.