Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Item Overflowing #220

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 46 additions & 78 deletions src/main/java/io/github/sefiraat/networks/network/NetworkRoot.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.github.sefiraat.networks.slimefun.network.NetworkQuantumStorage;
import io.github.sefiraat.networks.utils.StackUtils;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;
import io.github.thebusybiscuit.slimefun4.utils.NumberUtils;
import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config;
import me.mrCookieSlime.Slimefun.api.BlockStorage;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu;
Expand Down Expand Up @@ -226,40 +227,28 @@ public Map<ItemStack, Integer> getAllNetworkItems() {

// Barrels
for (BarrelIdentity barrelIdentity : getBarrels()) {
final Integer currentAmount = itemStacks.get(barrelIdentity.getItemStack());
final int newAmount;
if (currentAmount == null) {
newAmount = barrelIdentity.getAmount();
} else {
long newLong = (long) currentAmount + (long) barrelIdentity.getAmount();
if (newLong > Integer.MAX_VALUE) {
newAmount = Integer.MAX_VALUE;
} else {
newAmount = currentAmount + barrelIdentity.getAmount();
itemStacks.compute(barrelIdentity.getItemStack(), (item, amount) -> {
int barrelAmount = barrelIdentity.getAmount();
if (amount == null) {
return barrelAmount;
}
}
itemStacks.put(barrelIdentity.getItemStack(), newAmount);
return NumberUtils.flowSafeAddition(amount, barrelAmount);
});
}

for (BlockMenu blockMenu : getGreedyBlocks()) {
final ItemStack itemStack = blockMenu.getItemInSlot(NetworkGreedyBlock.INPUT_SLOT);
if (itemStack == null || itemStack.getType() == Material.AIR) {
continue;
}
final ItemStack clone = StackUtils.getAsQuantity(itemStack, 1);
final Integer currentAmount = itemStacks.get(clone);
final int newAmount;
if (currentAmount == null) {
newAmount = itemStack.getAmount();
} else {
long newLong = (long) currentAmount + (long) itemStack.getAmount();
if (newLong > Integer.MAX_VALUE) {
newAmount = Integer.MAX_VALUE;
} else {
newAmount = currentAmount + itemStack.getAmount();

itemStacks.compute(StackUtils.getAsOne(itemStack), (item, amount) -> {
int itemAmount = itemStack.getAmount();
if (amount == null) {
return itemAmount;
}
}
itemStacks.put(clone, newAmount);
return NumberUtils.flowSafeAddition(amount, itemAmount);
});
}

for (BlockMenu blockMenu : getCrafterOutputs()) {
Expand All @@ -269,46 +258,30 @@ public Map<ItemStack, Integer> getAllNetworkItems() {
if (itemStack == null || itemStack.getType() == Material.AIR) {
continue;
}
final ItemStack clone = StackUtils.getAsQuantity(itemStack, 1);
final Integer currentAmount = itemStacks.get(clone);
final int newAmount;
if (currentAmount == null) {
newAmount = itemStack.getAmount();
} else {
long newLong = (long) currentAmount + (long) itemStack.getAmount();
if (newLong > Integer.MAX_VALUE) {
newAmount = Integer.MAX_VALUE;
} else {
newAmount = currentAmount + itemStack.getAmount();

itemStacks.compute(StackUtils.getAsOne(itemStack), (item, amount) -> {
int itemAmount = itemStack.getAmount();
if (amount == null) {
return itemAmount;
}
}
itemStacks.put(clone, newAmount);
return NumberUtils.flowSafeAddition(amount, itemAmount);
});
}
}

for (BlockMenu blockMenu : getCellMenus()) {
for (ItemStack itemStack : blockMenu.getContents()) {
if (itemStack != null && itemStack.getType() != Material.AIR) {
final ItemStack clone = itemStack.clone();

clone.setAmount(1);

final Integer currentAmount = itemStacks.get(clone);
int newAmount;

if (currentAmount == null) {
newAmount = itemStack.getAmount();
} else {
long newLong = (long) currentAmount + (long) itemStack.getAmount();
if (newLong > Integer.MAX_VALUE) {
newAmount = Integer.MAX_VALUE;
} else {
newAmount = currentAmount + itemStack.getAmount();
}
}

itemStacks.put(clone, newAmount);
if (itemStack == null || itemStack.getType() == Material.AIR) {
continue;
}

itemStacks.compute(StackUtils.getAsOne(itemStack), (item, amount) -> {
int itemAmount = itemStack.getAmount();
if (amount == null) {
return itemAmount;
}
return NumberUtils.flowSafeAddition(amount, itemAmount);
});
}
}
return itemStacks;
Expand Down Expand Up @@ -368,32 +341,29 @@ public Set<BarrelIdentity> getBarrels() {
@Nullable
private InfinityBarrel getInfinityBarrel(@Nonnull BlockMenu blockMenu, @Nonnull StorageUnit storageUnit) {
final ItemStack itemStack = blockMenu.getItemInSlot(16);
final Config config = BlockStorage.getLocationInfo(blockMenu.getLocation());
final String storedString = config.getString("stored");

if (storedString == null) {
if (itemStack == null || itemStack.getType() == Material.AIR) {
return null;
}

final int storedInt = Integer.parseInt(storedString);
final Config config = BlockStorage.getLocationInfo(blockMenu.getLocation());
final String storedString = config.getString("stored");

if (itemStack == null || itemStack.getType() == Material.AIR) {
if (storedString == null) {
return null;
}

final int storedInt = Integer.parseInt(storedString);
final io.github.mooy1.infinityexpansion.items.storage.StorageCache cache = storageUnit.getCache(blockMenu.getLocation());

if (cache == null) {
return null;
}

final ItemStack clone = itemStack.clone();
clone.setAmount(1);

return new InfinityBarrel(
blockMenu.getLocation(),
clone,
storedInt + itemStack.getAmount(),
StackUtils.getAsOne(itemStack),
NumberUtils.flowSafeAddition(storedInt, itemStack.getAmount()),
cache
);
}
Expand All @@ -403,28 +373,26 @@ private NetworkStorage getNetworkStorage(@Nonnull BlockMenu blockMenu) {

final QuantumCache cache = NetworkQuantumStorage.getCaches().get(blockMenu.getLocation());

if (cache == null || cache.getItemStack() == null) {
if (cache == null) {
return null;
}

final ItemStack output = blockMenu.getItemInSlot(NetworkQuantumStorage.OUTPUT_SLOT);
final ItemStack itemStack = cache.getItemStack();
int storedInt = cache.getAmount();

if (output != null && output.getType() != Material.AIR && StackUtils.itemsMatch(cache, output, true)) {
storedInt = storedInt + output.getAmount();
}

if (itemStack == null || itemStack.getType() == Material.AIR) {
return null;
}

final ItemStack clone = itemStack.clone();
clone.setAmount(1);
final ItemStack output = blockMenu.getItemInSlot(NetworkQuantumStorage.OUTPUT_SLOT);
int storedInt = cache.getAmount();

if (output != null && output.getType() != Material.AIR && StackUtils.itemsMatch(cache, output, true)) {
storedInt = NumberUtils.flowSafeAddition(storedInt, output.getAmount());
}

return new NetworkStorage(
blockMenu.getLocation(),
clone,
StackUtils.getAsOne(itemStack),
storedInt
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public void tryEncode(@Nonnull Player player, @Nonnull BlockMenu blockMenu) {
return;
}

final ItemStack blueprintClone = StackUtils.getAsQuantity(blueprint, 1);
final ItemStack blueprintClone = StackUtils.getAsOne(blueprint);

blueprint.setAmount(blueprint.getAmount() - 1);
CraftingBlueprint.setBlueprint(blueprintClone, inputs, crafted);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private void setConfigurator(@Nonnull NetworkDirectional directional, @Nonnull I
for (int slot : directional.getItemSlots()) {
final ItemStack possibleStack = blockMenu.getItemInSlot(slot);
if (possibleStack != null) {
itemStacks[i] = StackUtils.getAsQuantity(blockMenu.getItemInSlot(slot), 1);
itemStacks[i] = StackUtils.getAsOne(blockMenu.getItemInSlot(slot));
}
i++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static void applyConfig(@Nonnull NetworkDirectional directional, @Nonnull
boolean worked = false;
for (ItemStack stack : player.getInventory()) {
if (StackUtils.itemsMatch(stack, templateStack)) {
final ItemStack stackClone = StackUtils.getAsQuantity(stack, 1);
final ItemStack stackClone = StackUtils.getAsOne(stack);
stack.setAmount(stack.getAmount() - 1);
blockMenu.replaceExistingItem(directional.getItemSlots()[i], stackClone);
player.sendMessage(Theme.SUCCESS + "Item [" + i + "]: " + Theme.PASSIVE + "Item added into filter");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
@UtilityClass
public class StackUtils {

@Nonnull
public static ItemStack getAsOne(@Nonnull ItemStack itemStack) {
return getAsQuantity(itemStack, 1);
}

@Nonnull
public static ItemStack getAsQuantity(@Nonnull ItemStack itemStack, int amount) {
ItemStack clone = itemStack.clone();
Expand Down