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

Try to inject and use the ItemStackWrapper into frequently used CargoUtils methods

This commit is contained in:
Andrew Wong 2021-05-18 14:12:28 +02:00
parent 9e6aa5b39a
commit 3dfc83ac6a
No known key found for this signature in database
GPG Key ID: F59F2C3DA0936DE4
3 changed files with 18 additions and 17 deletions

View File

@ -17,6 +17,7 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault; import javax.annotation.ParametersAreNonnullByDefault;
import io.github.thebusybiscuit.slimefun4.utils.itemstack.ItemStackWrapper;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
@ -135,7 +136,7 @@ abstract class AbstractItemNetwork extends Network {
Optional<Block> target = getAttachedBlock(l); Optional<Block> target = getAttachedBlock(l);
if (target.isPresent()) { if (target.isPresent()) {
item = CargoUtils.insert(this, inventories, l.getBlock(), target.get(), false, item); item = CargoUtils.insert(this, inventories, l.getBlock(), target.get(), false, item, ItemStackWrapper.ofItem(item));
if (item == null) { if (item == null) {
terminal.replaceExistingItem(request.getSlot(), null); terminal.replaceExistingItem(request.getSlot(), null);
@ -232,10 +233,10 @@ abstract class AbstractItemNetwork extends Network {
long timestamp = SlimefunPlugin.getProfiler().newEntry(); long timestamp = SlimefunPlugin.getProfiler().newEntry();
BlockMenu menu = BlockStorage.getInventory(bus); BlockMenu menu = BlockStorage.getInventory(bus);
if (menu.getItemInSlot(17) != null) { ItemStack itemSlot17 = menu.getItemInSlot(17);
if (itemSlot17 != null) {
Optional<Block> target = getAttachedBlock(bus); Optional<Block> target = getAttachedBlock(bus);
target.ifPresent(block -> menu.replaceExistingItem(17, CargoUtils.insert(this, inventories, bus.getBlock(), block, false, itemSlot17, ItemStackWrapper.ofItem(itemSlot17))));
target.ifPresent(block -> menu.replaceExistingItem(17, CargoUtils.insert(this, inventories, bus.getBlock(), block, false, menu.getItemInSlot(17))));
} }
if (menu.getItemInSlot(17) == null) { if (menu.getItemInSlot(17) == null) {

View File

@ -15,6 +15,7 @@ import java.util.logging.Level;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault; import javax.annotation.ParametersAreNonnullByDefault;
import io.github.thebusybiscuit.slimefun4.utils.itemstack.ItemStackWrapper;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
@ -112,7 +113,7 @@ class CargoNetworkTask implements Runnable {
return; return;
} }
ItemStack stack = slot.getItemStackWrapper(); ItemStack stack = slot.getItem();
int previousSlot = slot.getInt(); int previousSlot = slot.getInt();
List<Location> destinations = outputNodes.get(frequency); List<Location> destinations = outputNodes.get(frequency);
@ -184,7 +185,8 @@ class CargoNetworkTask implements Runnable {
Optional<Block> target = network.getAttachedBlock(output); Optional<Block> target = network.getAttachedBlock(output);
if (target.isPresent()) { if (target.isPresent()) {
item = CargoUtils.insert(network, inventories, output.getBlock(), target.get(), smartFill, item); ItemStackWrapper wrapper = ItemStackWrapper.ofItem(item);
item = CargoUtils.insert(network, inventories, output.getBlock(), target.get(), smartFill, item, wrapper);
if (item == null) { if (item == null) {
break; break;

View File

@ -152,12 +152,13 @@ final class CargoUtils {
return null; return null;
} }
ItemStackWrapper wrapper = ItemStackWrapper.ofItem(template); ItemStackWrapper wrapperTemplate = ItemStackWrapper.ofItem(template);
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);
ItemStackWrapper wrapperItemInSlot = ItemStackWrapper.ofItem(is);
if (SlimefunUtils.isItemSimilar(is, wrapper, true) && matchesFilter(network, node, is)) { if (SlimefunUtils.isItemSimilar(wrapperItemInSlot, wrapperTemplate, true) && matchesFilter(network, node, wrapperItemInSlot)) {
if (is.getAmount() > template.getAmount()) { if (is.getAmount() > template.getAmount()) {
is.setAmount(is.getAmount() - template.getAmount()); is.setAmount(is.getAmount() - template.getAmount());
menu.replaceExistingItem(slot, is); menu.replaceExistingItem(slot, is);
@ -184,8 +185,9 @@ final class CargoUtils {
for (int slot = minSlot; slot < maxSlot; slot++) { for (int slot = minSlot; slot < maxSlot; slot++) {
// Changes to these ItemStacks are synchronized with the Item in the Inventory // Changes to these ItemStacks are synchronized with the Item in the Inventory
ItemStack itemInSlot = contents[slot]; ItemStack itemInSlot = contents[slot];
ItemStackWrapper wrapperInSlot = ItemStackWrapper.ofItem(itemInSlot);
if (SlimefunUtils.isItemSimilar(itemInSlot, wrapper, true, false) && matchesFilter(network, node, itemInSlot)) { if (SlimefunUtils.isItemSimilar(wrapperInSlot, wrapper, true, false) && matchesFilter(network, node, wrapperInSlot)) {
if (itemInSlot.getAmount() > template.getAmount()) { if (itemInSlot.getAmount() > template.getAmount()) {
itemInSlot.setAmount(itemInSlot.getAmount() - template.getAmount()); itemInSlot.setAmount(itemInSlot.getAmount() - template.getAmount());
return template; return template;
@ -252,7 +254,7 @@ final class CargoUtils {
} }
@Nullable @Nullable
static ItemStack insert(AbstractItemNetwork network, Map<Location, Inventory> inventories, Block node, Block target, boolean smartFill, ItemStack stack) { static ItemStack insert(AbstractItemNetwork network, Map<Location, Inventory> inventories, Block node, Block target, boolean smartFill, ItemStack stack, ItemStackWrapper wrapper) {
if (!matchesFilter(network, node, stack)) { if (!matchesFilter(network, node, stack)) {
return stack; return stack;
} }
@ -264,7 +266,7 @@ final class CargoUtils {
Inventory inventory = inventories.get(target.getLocation()); Inventory inventory = inventories.get(target.getLocation());
if (inventory != null) { if (inventory != null) {
return insertIntoVanillaInventory(stack, smartFill, inventory); return insertIntoVanillaInventory(stack, wrapper, smartFill, inventory);
} }
BlockState state = PaperLib.getBlockState(target, false).getState(); BlockState state = PaperLib.getBlockState(target, false).getState();
@ -272,15 +274,13 @@ final class CargoUtils {
if (state instanceof InventoryHolder) { if (state instanceof InventoryHolder) {
inventory = ((InventoryHolder) state).getInventory(); inventory = ((InventoryHolder) state).getInventory();
inventories.put(target.getLocation(), inventory); inventories.put(target.getLocation(), inventory);
return insertIntoVanillaInventory(stack, smartFill, inventory); return insertIntoVanillaInventory(stack, wrapper, smartFill, inventory);
} }
} }
return stack; return stack;
} }
ItemStackWrapper wrapper = ItemStackWrapper.ofItem(stack);
for (int slot : menu.getPreset().getSlotsAccessedByItemTransport(menu, ItemTransportFlow.INSERT, wrapper)) { for (int slot : menu.getPreset().getSlotsAccessedByItemTransport(menu, ItemTransportFlow.INSERT, wrapper)) {
ItemStack itemInSlot = menu.getItemInSlot(slot); ItemStack itemInSlot = menu.getItemInSlot(slot);
@ -320,7 +320,7 @@ final class CargoUtils {
} }
@Nullable @Nullable
private static ItemStack insertIntoVanillaInventory(@Nonnull ItemStack stack, boolean smartFill, @Nonnull Inventory inv) { private static ItemStack insertIntoVanillaInventory(@Nonnull ItemStack stack, @Nonnull ItemStackWrapper wrapper, boolean smartFill, @Nonnull Inventory inv) {
/* /*
* If the Inventory does not accept this Item Type, bounce the item back. * If the Inventory does not accept this Item Type, bounce the item back.
* Example: Shulker boxes within shulker boxes (fixes #2662) * Example: Shulker boxes within shulker boxes (fixes #2662)
@ -334,8 +334,6 @@ final class CargoUtils {
int minSlot = range[0]; int minSlot = range[0];
int maxSlot = range[1]; int maxSlot = range[1];
ItemStackWrapper wrapper = ItemStackWrapper.ofItem(stack);
for (int slot = minSlot; slot < maxSlot; slot++) { for (int slot = minSlot; slot < maxSlot; slot++) {
// Changes to this ItemStack are synchronized with the Item in the Inventory // Changes to this ItemStack are synchronized with the Item in the Inventory
ItemStack itemInSlot = contents[slot]; ItemStack itemInSlot = contents[slot];