Skip to content

Commit

Permalink
Fix for carpet's stacked shulkerboxes mass crafting (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad2305m authored Mar 24, 2023
1 parent b28a298 commit 2b02a63
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/main/java/fi/dy/masa/itemscroller/ItemScroller.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ public class ItemScroller implements ModInitializer
public void onInitialize()
{
InitializationHandler.getInstance().registerInitializationHandler(new InitHandler());
fi.dy.masa.itemscroller.compat.carpet.StackingShulkerBoxes.init();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package fi.dy.masa.itemscroller.compat.carpet;

import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.block.ShulkerBoxBlock;
import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemStack;

import java.lang.invoke.*;
import java.util.function.Function;
import java.util.function.IntSupplier;

public class StackingShulkerBoxes {
public static boolean enabled = false;

public static void init(){
if (FabricLoader.getInstance().isModLoaded("carpet")){
try {
enabled = true;
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodHandle shulkerBoxHasItemsTarget = lookup.findStatic(Class.forName("carpet.helpers.InventoryHelper"), "shulkerBoxHasItems", MethodType.methodType(boolean.class, ItemStack.class));
shulkerBoxHasItems = (ItemStack stack) -> {
try {
return (Boolean) shulkerBoxHasItemsTarget.invokeWithArguments(stack);
} catch (Throwable e) {
throw new RuntimeException(e);
}
};
MethodHandle shulkerBoxStackSizeHandle = lookup.findStaticVarHandle(Class.forName("carpet.CarpetSettings"), "shulkerBoxStackSize", int.class).toMethodHandle(VarHandle.AccessMode.GET);
shulkerBoxStackSizeGetter = ()-> {
try {
return (int) shulkerBoxStackSizeHandle.invokeExact();
} catch (Throwable e) {
throw new RuntimeException(e);
}
};

} catch (Throwable e) {
e.printStackTrace();
}
}
}
private static IntSupplier shulkerBoxStackSizeGetter = null;
private static Function<ItemStack, Boolean> shulkerBoxHasItems = null;

/**
* @param stack {@link ItemStack}
* @return Stack size considering empty boxes stacking rule from carpet mod
* @author <a href="https://github.com/gnembon">gnembon</a>, <a href="https://github.com/vlad2305m">vlad2305m</a>
* @see <a href="https://https://github.com/gnembon/fabric-carpet/blob/master/src/main/java/carpet/mixins/Slot_stackableSBoxesMixin.java">Original implementation</a>
*/
public static int getMaxCount(ItemStack stack){
if (!enabled) return stack.getMaxCount();
int shulkerBoxStackSize = shulkerBoxStackSizeGetter.getAsInt();
if (shulkerBoxStackSize > 1 &&
stack.getItem() instanceof BlockItem &&
((BlockItem)stack.getItem()).getBlock() instanceof ShulkerBoxBlock &&
!shulkerBoxHasItems.apply(stack)
) {
return shulkerBoxStackSize;
}
return stack.getMaxCount();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.util.HashSet;
import java.util.Arrays;
import javax.annotation.Nonnull;

import fi.dy.masa.itemscroller.compat.carpet.StackingShulkerBoxes;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
Expand Down Expand Up @@ -56,8 +58,9 @@ public void initializeRecipe() {
if (this.recipe[i].getItem().hasRecipeRemainder()) {
this.recipeRemainders.add(recipe[i].getItem().getRecipeRemainder());
}
if (this.recipe[i].getMaxCount() < maxCraftAmount) {
maxCraftAmount = this.recipe[i].getMaxCount();
int maxCount = StackingShulkerBoxes.getMaxCount(this.recipe[i]);
if (maxCount < maxCraftAmount) {
maxCraftAmount = maxCount;
}
}
}
Expand Down

0 comments on commit 2b02a63

Please sign in to comment.