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

Add ItemStackWrapper#ofItem, try not to use duplicate ItemStackWrappers in CargoNet

This commit is contained in:
Andrew Wong 2021-05-17 21:36:10 +02:00
parent f822bdd12d
commit 727c8d34b9
No known key found for this signature in database
GPG Key ID: F59F2C3DA0936DE4
4 changed files with 14 additions and 7 deletions

View File

@ -152,7 +152,7 @@ final class CargoUtils {
return null;
}
ItemStackWrapper wrapper = new ItemStackWrapper(template);
ItemStackWrapper wrapper = ItemStackWrapper.ofItem(template);
for (int slot : menu.getPreset().getSlotsAccessedByItemTransport(menu, ItemTransportFlow.WITHDRAW, null)) {
ItemStack is = menu.getItemInSlot(slot);
@ -179,7 +179,7 @@ final class CargoUtils {
int minSlot = range[0];
int maxSlot = range[1];
ItemStackWrapper wrapper = new ItemStackWrapper(template);
ItemStackWrapper wrapper = ItemStackWrapper.ofItem(template);
for (int slot = minSlot; slot < maxSlot; slot++) {
// Changes to these ItemStacks are synchronized with the Item in the Inventory
@ -279,7 +279,7 @@ final class CargoUtils {
return stack;
}
ItemStackWrapper wrapper = new ItemStackWrapper(stack);
ItemStackWrapper wrapper = ItemStackWrapper.ofItem(stack);
for (int slot : menu.getPreset().getSlotsAccessedByItemTransport(menu, ItemTransportFlow.INSERT, wrapper)) {
ItemStack itemInSlot = menu.getItemInSlot(slot);
@ -334,7 +334,7 @@ final class CargoUtils {
int minSlot = range[0];
int maxSlot = range[1];
ItemStackWrapper wrapper = new ItemStackWrapper(stack);
ItemStackWrapper wrapper = ItemStackWrapper.ofItem(stack);
for (int slot = minSlot; slot < maxSlot; slot++) {
// Changes to this ItemStack are synchronized with the Item in the Inventory
@ -358,7 +358,7 @@ final class CargoUtils {
if (amount > maxStackSize) {
stack.setAmount(amount - maxStackSize);
itemInSlot.setAmount(Math.min(amount, maxStackSize));
itemInSlot.setAmount(maxStackSize);
return stack;
} else {
itemInSlot.setAmount(Math.min(amount, maxStackSize));

View File

@ -189,7 +189,7 @@ class ItemFilter implements Predicate<ItemStack> {
* If there is more than one potential match, create a wrapper to save
* performance on the ItemMeta otherwise just use the item directly.
*/
ItemStack subject = potentialMatches == 1 ? item : new ItemStackWrapper(item);
ItemStack subject = potentialMatches == 1 ? item : ItemStackWrapper.ofItem(item);
/*
* If there is only one match, we won't need to create a Wrapper

View File

@ -25,7 +25,7 @@ class ItemStackAndInteger {
public ItemStackWrapper getItemStackWrapper() {
if (wrapper == null && item != null) {
wrapper = new ItemStackWrapper(item);
wrapper = ItemStackWrapper.ofItem(item);
}
return wrapper;

View File

@ -46,6 +46,13 @@ public final class ItemStackWrapper extends ItemStack {
this(new ItemStack(material));
}
public static @Nonnull ItemStackWrapper ofItem(@Nonnull ItemStack itemStack) {
if (itemStack instanceof ItemStackWrapper) {
return (ItemStackWrapper) itemStack;
}
return new ItemStackWrapper(itemStack);
}
@Override
public boolean hasItemMeta() {
return hasItemMeta;