1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-20 03:35:51 +00:00

simplify cargo withdraw methods.

trying to reduce unnecessary item clone as much as possible.
This commit is contained in:
DNx 2020-04-10 08:05:17 +07:00
parent 88a5129b1a
commit c00fb1dc1b
2 changed files with 31 additions and 29 deletions

View File

@ -29,30 +29,31 @@ final class CargoUtils {
public static ItemStack withdraw(Block node, Block target, ItemStack template) { public static ItemStack withdraw(Block node, Block target, ItemStack template) {
DirtyChestMenu menu = getChestMenu(target); DirtyChestMenu menu = getChestMenu(target);
if (menu == null) {
if (BlockUtils.hasInventory(target)) {
BlockState state = target.getState();
if (state instanceof InventoryHolder) {
return withdrawFromVanillaInventory(node, template, ((InventoryHolder) state).getInventory());
}
}
return null;
}
if (menu != null) {
for (int slot : menu.getPreset().getSlotsAccessedByItemTransport(menu, ItemTransportFlow.WITHDRAW, null)) { for (int slot : menu.getPreset().getSlotsAccessedByItemTransport(menu, ItemTransportFlow.WITHDRAW, null)) {
ItemStack is = menu.getItemInSlot(slot); ItemStack is = menu.getItemInSlot(slot);
if (SlimefunUtils.isItemSimilar(is, template, true) && matchesFilter(node, is, -1)) { if (SlimefunUtils.isItemSimilar(is, template, true) && matchesFilter(node, is, -1)) {
if (is.getAmount() > template.getAmount()) { if (is.getAmount() > template.getAmount()) {
menu.replaceExistingItem(slot, new CustomItem(is, is.getAmount() - template.getAmount())); is.setAmount(is.getAmount() - template.getAmount());
menu.replaceExistingItem(slot, is);
return template; return template;
} }
else { else {
menu.replaceExistingItem(slot, null); menu.replaceExistingItem(slot, null);
return is.clone(); return is;
} }
} }
} }
}
else if (BlockUtils.hasInventory(target)) {
BlockState state = target.getState();
if (state instanceof InventoryHolder) {
return withdrawFromVanillaInventory(node, template, ((InventoryHolder) state).getInventory());
}
}
return null; return null;
} }
@ -75,12 +76,14 @@ final class CargoUtils {
if (SlimefunUtils.isItemSimilar(is, template, true) && matchesFilter(node, is, -1)) { if (SlimefunUtils.isItemSimilar(is, template, true) && matchesFilter(node, is, -1)) {
if (is.getAmount() > template.getAmount()) { if (is.getAmount() > template.getAmount()) {
inv.setItem(slot, new CustomItem(is, is.getAmount() - template.getAmount())); is.setAmount(is.getAmount() - template.getAmount());
inv.setItem(slot, is);
return template; return template;
} }
else { else {
inv.setItem(slot, new CustomItem(is, is.getAmount() - template.getAmount())); is.setAmount(is.getAmount() - template.getAmount());
return is.clone(); inv.setItem(slot, is);
return is;
} }
} }
} }
@ -97,7 +100,7 @@ final class CargoUtils {
if (matchesFilter(node, is, index)) { if (matchesFilter(node, is, index)) {
menu.replaceExistingItem(slot, null); menu.replaceExistingItem(slot, null);
return new ItemStackAndInteger(is.clone(), slot); return new ItemStackAndInteger(is, slot);
} }
} }
} }
@ -124,7 +127,7 @@ final class CargoUtils {
if (matchesFilter(node, is, index)) { if (matchesFilter(node, is, index)) {
inv.setItem(slot, null); inv.setItem(slot, null);
return new ItemStackAndInteger(is.clone(), slot); return new ItemStackAndInteger(is, slot);
} }
} }
} }

View File

@ -112,9 +112,8 @@ public class DirtyChestMenu extends ChestMenu {
} }
public void replaceExistingItem(int slot, ItemStack item, boolean event) { public void replaceExistingItem(int slot, ItemStack item, boolean event) {
ItemStack previous = getItemInSlot(slot);
if (event && this.event != null) { if (event && this.event != null) {
ItemStack previous = getItemInSlot(slot);
item = this.event.onEvent(slot, previous, item); item = this.event.onEvent(slot, previous, item);
} }