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 Voiding Input Resources Bug & fix some Repeating Code #6

Closed
wants to merge 5 commits into from
Closed
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.mooy1</groupId>
<artifactId>InfinityLib</artifactId>
<version>1.3.9</version>
<version>1.4.0</version>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import lombok.Setter;

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.inventory.ItemStack;

Expand All @@ -19,6 +20,7 @@
import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack;
import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType;
import io.github.thebusybiscuit.slimefun4.libraries.dough.items.ItemUtils;
import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenuPreset;
Expand Down Expand Up @@ -99,26 +101,26 @@ protected boolean process(Block b, BlockMenu menu) {
}

MachineBlockRecipe recipe = getOutput(input);

if (recipe != null) {
if (!(getFreeSpace(layout.outputSlots(), recipe.output.clone(), menu) >= recipe.output.getAmount())) {
displayIfViewer(menu, NO_ROOM_ITEM);
return false;
}

ItemStack rem = menu.pushItem(recipe.output.clone(), layout.outputSlots());
if (rem == null || rem.getAmount() < recipe.output.getAmount()) {
if (rem == null) {
recipe.consume();
if (menu.hasViewer()) {
menu.replaceExistingItem(getStatusSlot(), PROCESSING_ITEM);
}
displayIfViewer(menu, PROCESSING_ITEM);
return true;
}
else {
if (menu.hasViewer()) {
menu.replaceExistingItem(getStatusSlot(), NO_ROOM_ITEM);
}
displayIfViewer(menu, NO_ROOM_ITEM);
return false;
}
}

if (menu.hasViewer()) {
menu.replaceExistingItem(getStatusSlot(), IDLE_ITEM);
}
displayIfViewer(menu, IDLE_ITEM);
return false;
}

Expand All @@ -143,6 +145,34 @@ MachineBlockRecipe getOutput(ItemStack[] items) {
return null;
}

int getFreeSpace(int[] slots, ItemStack toWrap, BlockMenu menu) {
int freeSpace = 0;
for (int slotNumber : slots) {
//Get the Item in the Slot and Check if it's an Actual Item
ItemStack itemStack = menu.getItemInSlot(slotNumber);
if (itemStack == null || itemStack.getType() == Material.AIR) {
freeSpace += toWrap.getMaxStackSize();
continue;
}

//If the Items can't stack then continue to the next Slot
if (!ItemUtils.canStack(toWrap, itemStack)) {
continue;
}

//Add the Amount of FreeSpace that is Left in the Stack
freeSpace += itemStack.getMaxStackSize() - itemStack.getAmount();
}

return freeSpace;
}

void displayIfViewer(@Nonnull BlockMenu menu, @Nonnull ItemStack toDisplay) {
if (menu.hasViewer()) {
menu.replaceExistingItem(getStatusSlot(), toDisplay);
}
}

@Override
protected int getStatusSlot() {
return layout.statusSlot();
Expand Down