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:
parent
88a5129b1a
commit
c00fb1dc1b
@ -29,31 +29,32 @@ 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 (menu != null) {
|
if (BlockUtils.hasInventory(target)) {
|
||||||
for (int slot : menu.getPreset().getSlotsAccessedByItemTransport(menu, ItemTransportFlow.WITHDRAW, null)) {
|
BlockState state = target.getState();
|
||||||
ItemStack is = menu.getItemInSlot(slot);
|
if (state instanceof InventoryHolder) {
|
||||||
|
return withdrawFromVanillaInventory(node, template, ((InventoryHolder) state).getInventory());
|
||||||
if (SlimefunUtils.isItemSimilar(is, template, true) && matchesFilter(node, is, -1)) {
|
|
||||||
if (is.getAmount() > template.getAmount()) {
|
|
||||||
menu.replaceExistingItem(slot, new CustomItem(is, is.getAmount() - template.getAmount()));
|
|
||||||
return template;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
menu.replaceExistingItem(slot, null);
|
|
||||||
return is.clone();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
return null;
|
||||||
else if (BlockUtils.hasInventory(target)) {
|
}
|
||||||
BlockState state = target.getState();
|
|
||||||
|
|
||||||
if (state instanceof InventoryHolder) {
|
for (int slot : menu.getPreset().getSlotsAccessedByItemTransport(menu, ItemTransportFlow.WITHDRAW, null)) {
|
||||||
return withdrawFromVanillaInventory(node, template, ((InventoryHolder) state).getInventory());
|
ItemStack is = menu.getItemInSlot(slot);
|
||||||
|
|
||||||
|
if (SlimefunUtils.isItemSimilar(is, template, true) && matchesFilter(node, is, -1)) {
|
||||||
|
if (is.getAmount() > template.getAmount()) {
|
||||||
|
is.setAmount(is.getAmount() - template.getAmount());
|
||||||
|
menu.replaceExistingItem(slot, is);
|
||||||
|
return template;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
menu.replaceExistingItem(slot, null);
|
||||||
|
return is;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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,10 +100,10 @@ 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (BlockUtils.hasInventory(target)) {
|
else if (BlockUtils.hasInventory(target)) {
|
||||||
BlockState state = target.getState();
|
BlockState state = target.getState();
|
||||||
|
|
||||||
@ -114,7 +117,7 @@ final class CargoUtils {
|
|||||||
if (inv instanceof FurnaceInventory) {
|
if (inv instanceof FurnaceInventory) {
|
||||||
minSlot = 2;
|
minSlot = 2;
|
||||||
maxSlot = 3;
|
maxSlot = 3;
|
||||||
}
|
}
|
||||||
else if (inv instanceof BrewerInventory) {
|
else if (inv instanceof BrewerInventory) {
|
||||||
maxSlot = 3;
|
maxSlot = 3;
|
||||||
}
|
}
|
||||||
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user