From 564fb98e8c57d432774bbf52ef164167776aa02e Mon Sep 17 00:00:00 2001 From: Siphalor Date: Sat, 8 Jun 2019 20:29:05 +0200 Subject: [PATCH] Version 1.2.3 * Updated Tweed * Updated Loom * Updated mappings + Added additional refill capturing + Added configuration for refill rules (#15) + Added ability to swap armor items via the hotbar --- build.gradle | 24 ++-- gradle.properties | 7 +- .../mousewheelie/client/ClientCore.java | 21 +++- .../siphalor/mousewheelie/client/Config.java | 5 +- .../mixin/MixinAbstractContainerScreen.java | 14 +-- .../mixin/MixinAbstractFurnaceScreen.java | 4 +- .../mixin/MixinClientPlayNetworkHandler.java | 4 +- .../client/mixin/MixinContainer.java | 49 ++++++++ .../mixin/MixinCraftingTableScreen.java | 8 +- .../mixin/MixinCreativeInventoryScreen.java | 4 +- .../client/mixin/MixinInventoryScreen.java | 8 +- .../client/mixin/MixinLivingEntity.java | 2 +- .../client/mixin/MixinRecipeBookScreen.java | 4 +- .../util/inventory/InventorySorter.java | 12 +- .../client/util/inventory/SlotRefiller.java | 115 +++++++++++++----- .../client/util/inventory/SortMode.java | 8 +- .../client/util/inventory/ToolPicker.java | 4 +- .../de.siphalor.mousewheelie.client.json | 1 + 18 files changed, 207 insertions(+), 87 deletions(-) create mode 100644 src/main/java/de/siphalor/mousewheelie/client/mixin/MixinContainer.java diff --git a/build.gradle b/build.gradle index 19a2ccde..fbf97522 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ plugins { - id 'fabric-loom' version '0.2.2-SNAPSHOT' + id 'fabric-loom' version '0.2.4-SNAPSHOT' id 'maven-publish' } @@ -29,27 +29,27 @@ dependencies { //to change the versions see the gradle.properties file minecraft "com.mojang:minecraft:${project.minecraft_version}" mappings "net.fabricmc:yarn:${project.minecraft_version}+build.${project.yarn_build}" - modCompile "net.fabricmc:fabric-loader:${project.loader_version}" + modImplementation "net.fabricmc:fabric-loader:${project.loader_version}" - modCompile "net.fabricmc.fabric-api:fabric-keybindings:+" + modImplementation "net.fabricmc.fabric-api:fabric-keybindings:+" include "net.fabricmc.fabric-api:fabric-keybindings:+" - modCompile "net.fabricmc.fabric-api:fabric-resource-loader:+" + modImplementation "net.fabricmc.fabric-api:fabric-resource-loader:+" include "net.fabricmc.fabric-api:fabric-resource-loader:+" - modCompile "net.fabricmc.fabric-api:fabric-events-interaction-v0:+" + modImplementation "net.fabricmc.fabric-api:fabric-events-interaction-v0:+" include "net.fabricmc.fabric-api:fabric-events-interaction-v0:+" - modCompile "net.fabricmc.fabric-api:fabric-tag-extensions-v0:+" + modImplementation "net.fabricmc.fabric-api:fabric-tag-extensions-v0:+" include "net.fabricmc.fabric-api:fabric-tag-extensions-v0:+" - modCompile "net.fabricmc.fabric-api:fabric-mining-levels-v0:+" + modImplementation "net.fabricmc.fabric-api:fabric-mining-levels-v0:+" include "net.fabricmc.fabric-api:fabric-mining-levels-v0:+" - modCompile "net.fabricmc.fabric-api:fabric-item-groups:+" - modCompile "io.github.prospector.modmenu:ModMenu:+" + modImplementation "net.fabricmc.fabric-api:fabric-item-groups:+" + modImplementation "io.github.prospector.modmenu:ModMenu:+" - modCompile "com.github.siphalor:tweed-api:${project.tweed_version}" + modImplementation "com.github.siphalor:tweed-api:${project.tweed_version}" include "com.github.siphalor:tweed-api:${project.tweed_version}" - + + modImplementation "cloth-config:ClothConfig:${project.clothconfig_version}" include "cloth-config:ClothConfig:${project.clothconfig_version}" - modCompile "cloth-config:ClothConfig:${project.clothconfig_version}" } processResources { diff --git a/gradle.properties b/gradle.properties index 0b12b4a7..17d7b101 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,18 +1,19 @@ +# suppress inspection "UnusedProperty" for whole file # Done to increase the memory available to gradle. org.gradle.jvmargs=-Xmx1G # Fabric Properties # check these on https://fabricmc.net/use - minecraft_version=1.14.2 + minecraft_version=1.14.3-pre2 yarn_build=2 loader_version=0.4.8+build.154 # Mod Properties mod_id = mousewheelie - mod_version = 1.2.2 + mod_version = 1.2.3 maven_group = de.siphalor archives_base_name = mousewheelie # Dependencies - tweed_version = 2.1.0-beta.7 + tweed_version = 2.1.0-beta.9 clothconfig_version = 0.2.4.17 diff --git a/src/main/java/de/siphalor/mousewheelie/client/ClientCore.java b/src/main/java/de/siphalor/mousewheelie/client/ClientCore.java index c3244ac4..58d34201 100644 --- a/src/main/java/de/siphalor/mousewheelie/client/ClientCore.java +++ b/src/main/java/de/siphalor/mousewheelie/client/ClientCore.java @@ -8,9 +8,13 @@ import net.fabricmc.fabric.api.client.keybinding.FabricKeyBinding; import net.fabricmc.fabric.api.client.keybinding.KeyBindingRegistry; import net.fabricmc.fabric.api.event.client.player.ClientPickBlockGatherCallback; +import net.fabricmc.fabric.api.event.player.UseItemCallback; import net.fabricmc.fabric.api.tools.FabricToolTags; import net.minecraft.client.util.InputUtil; +import net.minecraft.entity.EquipmentSlot; +import net.minecraft.entity.mob.MobEntity; import net.minecraft.item.*; +import net.minecraft.util.ActionResult; import net.minecraft.util.Identifier; import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.hit.HitResult; @@ -24,7 +28,7 @@ public class ClientCore implements ClientModInitializer { public static TweedClothBridge tweedClothBridge; - public static boolean awaitFoodSlotUpdate = false; + public static boolean awaitSlotUpdate = false; @Override public void onInitializeClient() { @@ -47,6 +51,19 @@ public void onInitializeClient() { return ItemStack.EMPTY; }); + UseItemCallback.EVENT.register((player, world, hand) -> { + ItemStack stack = player.getStackInHand(hand); + EquipmentSlot equipmentSlot = MobEntity.getPreferredEquipmentSlot(stack); + if(equipmentSlot != EquipmentSlot.MAINHAND && equipmentSlot != EquipmentSlot.OFFHAND) { + if(!player.getEquippedStack(equipmentSlot).isEmpty()) { + player.setStackInHand(hand, player.getEquippedStack(equipmentSlot)); + player.setEquippedStack(equipmentSlot, stack); + return ActionResult.SUCCESS; + } + } + return ActionResult.PASS; + }); + Config.initialize(); tweedClothBridge = new TweedClothBridge(Config.configFile); @@ -57,6 +74,6 @@ public static boolean isTool(Item item) { } public static boolean isWeapon(Item item) { - return item instanceof BaseBowItem || item instanceof TridentItem || item instanceof SwordItem || FabricToolTags.SWORDS.contains(item); + return item instanceof RangedWeaponItem || item instanceof TridentItem || item instanceof SwordItem || FabricToolTags.SWORDS.contains(item); } } diff --git a/src/main/java/de/siphalor/mousewheelie/client/Config.java b/src/main/java/de/siphalor/mousewheelie/client/Config.java index a9154037..612e6ce9 100644 --- a/src/main/java/de/siphalor/mousewheelie/client/Config.java +++ b/src/main/java/de/siphalor/mousewheelie/client/Config.java @@ -2,6 +2,7 @@ import de.siphalor.mousewheelie.Core; import de.siphalor.mousewheelie.client.util.ToolPickMode; +import de.siphalor.mousewheelie.client.util.inventory.SlotRefiller; import de.siphalor.mousewheelie.client.util.inventory.SortMode; import de.siphalor.tweed.config.ConfigCategory; import de.siphalor.tweed.config.ConfigEnvironment; @@ -45,8 +46,10 @@ public class Config { public static BooleanEntry otherRefill = refillCategory.register("other", new BooleanEntry(true)) .setEnvironment(ConfigEnvironment.CLIENT) .setComment("Refill on other occasions"); + public static ConfigCategory refillRules = refillCategory.register("rules", new ConfigCategory()) + .setComment("Enable/Disable specific rules for how to refill items"); public static void initialize() { - + SlotRefiller.initialize(); } } diff --git a/src/main/java/de/siphalor/mousewheelie/client/mixin/MixinAbstractContainerScreen.java b/src/main/java/de/siphalor/mousewheelie/client/mixin/MixinAbstractContainerScreen.java index 6675fffa..28730125 100644 --- a/src/main/java/de/siphalor/mousewheelie/client/mixin/MixinAbstractContainerScreen.java +++ b/src/main/java/de/siphalor/mousewheelie/client/mixin/MixinAbstractContainerScreen.java @@ -16,8 +16,8 @@ import net.minecraft.container.SlotActionType; import net.minecraft.entity.player.PlayerInventory; import net.minecraft.item.ItemStack; -import net.minecraft.network.chat.Component; import net.minecraft.server.network.packet.PlayerActionC2SPacket; +import net.minecraft.text.Text; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Direction; import org.spongepowered.asm.mixin.Final; @@ -31,7 +31,7 @@ @Mixin(AbstractContainerScreen.class) public abstract class MixinAbstractContainerScreen extends Screen implements IContainerScreen { - protected MixinAbstractContainerScreen(Component textComponent_1) { + protected MixinAbstractContainerScreen(Text textComponent_1) { super(textComponent_1); } @@ -142,7 +142,7 @@ public boolean mouseWheelie_onMouseScroll(double mouseX, double mouseY, double s ItemStack referenceStack = hoveredStack.copy(); for(Slot slot : container.slotList) { if(slotsInSameScope.apply(slot, hoveredSlot)) { - if(slot.getStack().isEqualIgnoreTags(referenceStack)) + if(slot.getStack().isItemEqualIgnoreDamage(referenceStack)) onMouseClick(slot, slot.id, 0, SlotActionType.QUICK_MOVE); } } @@ -155,7 +155,7 @@ public boolean mouseWheelie_onMouseScroll(double mouseX, double mouseY, double s if(hasShiftDown() || hasControlDown()) { for(Slot slot : container.slotList) { if(slotsInSameScope.apply(slot, hoveredSlot)) continue; - if(slot.getStack().isEqualIgnoreTags(hoveredStack)) { + if(slot.getStack().isItemEqualIgnoreDamage(hoveredStack)) { onMouseClick(slot, slot.id, 0, SlotActionType.QUICK_MOVE); if(!hasControlDown()) break; @@ -166,9 +166,9 @@ public boolean mouseWheelie_onMouseScroll(double mouseX, double mouseY, double s int stackSize = Integer.MAX_VALUE; for(Slot slot : container.slotList) { if(slotsInSameScope.apply(slot, hoveredSlot)) continue; - if(slot.getStack().isEqualIgnoreTags(hoveredStack)) { - if(slot.getStack().getAmount() < stackSize) { - stackSize = slot.getStack().getAmount(); + if(slot.getStack().isItemEqualIgnoreDamage(hoveredStack)) { + if(slot.getStack().getCount() < stackSize) { + stackSize = slot.getStack().getCount(); moveSlot = slot; if(stackSize == 1) break; } diff --git a/src/main/java/de/siphalor/mousewheelie/client/mixin/MixinAbstractFurnaceScreen.java b/src/main/java/de/siphalor/mousewheelie/client/mixin/MixinAbstractFurnaceScreen.java index 039a4624..e8dc5f37 100644 --- a/src/main/java/de/siphalor/mousewheelie/client/mixin/MixinAbstractFurnaceScreen.java +++ b/src/main/java/de/siphalor/mousewheelie/client/mixin/MixinAbstractFurnaceScreen.java @@ -7,7 +7,7 @@ import net.minecraft.client.gui.screen.recipebook.AbstractFurnaceRecipeBookScreen; import net.minecraft.container.Container; import net.minecraft.entity.player.PlayerInventory; -import net.minecraft.network.chat.Component; +import net.minecraft.text.Text; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -17,7 +17,7 @@ public abstract class MixinAbstractFurnaceScreen extends AbstractContainerScreen @Shadow @Final public AbstractFurnaceRecipeBookScreen recipeBook; - public MixinAbstractFurnaceScreen(Container container_1, PlayerInventory playerInventory_1, Component textComponent_1) { + public MixinAbstractFurnaceScreen(Container container_1, PlayerInventory playerInventory_1, Text textComponent_1) { super(container_1, playerInventory_1, textComponent_1); } diff --git a/src/main/java/de/siphalor/mousewheelie/client/mixin/MixinClientPlayNetworkHandler.java b/src/main/java/de/siphalor/mousewheelie/client/mixin/MixinClientPlayNetworkHandler.java index 1bf4eb44..53daf4de 100644 --- a/src/main/java/de/siphalor/mousewheelie/client/mixin/MixinClientPlayNetworkHandler.java +++ b/src/main/java/de/siphalor/mousewheelie/client/mixin/MixinClientPlayNetworkHandler.java @@ -30,8 +30,8 @@ public void onGuiActionConfirmed(ConfirmGuiActionS2CPacket packet, CallbackInfo @Inject(method = "onGuiSlotUpdate", at = @At(value = "INVOKE", target = "Lnet/minecraft/container/PlayerContainer;setStackInSlot(ILnet/minecraft/item/ItemStack;)V", shift = At.Shift.BEFORE)) public void onGuiSlotUpdate(GuiSlotUpdateS2CPacket packet, CallbackInfo callbackInfo) { - if(ClientCore.awaitFoodSlotUpdate) { - ClientCore.awaitFoodSlotUpdate = false; + if(ClientCore.awaitSlotUpdate) { + ClientCore.awaitSlotUpdate = false; SlotRefiller.refill(); } else { PlayerInventory inventory = client.player.inventory; diff --git a/src/main/java/de/siphalor/mousewheelie/client/mixin/MixinContainer.java b/src/main/java/de/siphalor/mousewheelie/client/mixin/MixinContainer.java new file mode 100644 index 00000000..96f5c64d --- /dev/null +++ b/src/main/java/de/siphalor/mousewheelie/client/mixin/MixinContainer.java @@ -0,0 +1,49 @@ +package de.siphalor.mousewheelie.client.mixin; + +import de.siphalor.mousewheelie.client.Config; +import de.siphalor.mousewheelie.client.util.ISlot; +import de.siphalor.mousewheelie.client.util.inventory.SlotRefiller; +import net.minecraft.client.MinecraftClient; +import net.minecraft.container.Container; +import net.minecraft.container.PlayerContainer; +import net.minecraft.container.Slot; +import net.minecraft.entity.player.PlayerInventory; +import net.minecraft.item.ItemStack; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.LocalCapture; + +import java.util.List; + +@Mixin(Container.class) +public abstract class MixinContainer { + @Shadow public abstract Slot getSlot(int int_1); + + private static boolean mouseWheelie_scheduleRefill = false; + + @Inject(method = "updateSlotStacks", at = @At(value = "INVOKE", target = "Lnet/minecraft/container/Container;getSlot(I)Lnet/minecraft/container/Slot;", shift = At.Shift.BEFORE), locals = LocalCapture.CAPTURE_FAILSOFT) + public void onSlotUpdate(List itemStacks, CallbackInfo callbackInfo, int index) { + if((Object) this instanceof PlayerContainer && Config.otherRefill.value) { + PlayerInventory inventory = MinecraftClient.getInstance().player.inventory; + if(inventory.selectedSlot == ((ISlot) getSlot(index)).mouseWheelie_getInvSlot()) { + ItemStack stack = inventory.getMainHandStack(); + if (!stack.isEmpty() && itemStacks.get(index).isEmpty()) { + mouseWheelie_scheduleRefill = true; + SlotRefiller.set(inventory, stack.copy()); + } + } + } + } + + @Inject(method = "updateSlotStacks", at = @At(value = "INVOKE", target = "Lnet/minecraft/container/Slot;setStack(Lnet/minecraft/item/ItemStack;)V", shift = At.Shift.AFTER), locals = LocalCapture.CAPTURE_FAILSOFT) + public void onSlotUpdated(List stacks, CallbackInfo callbackInfo, int index) { + if(mouseWheelie_scheduleRefill) { + mouseWheelie_scheduleRefill = false; + SlotRefiller.refill(); + } + } + +} diff --git a/src/main/java/de/siphalor/mousewheelie/client/mixin/MixinCraftingTableScreen.java b/src/main/java/de/siphalor/mousewheelie/client/mixin/MixinCraftingTableScreen.java index fcaa682e..2ea61c10 100644 --- a/src/main/java/de/siphalor/mousewheelie/client/mixin/MixinCraftingTableScreen.java +++ b/src/main/java/de/siphalor/mousewheelie/client/mixin/MixinCraftingTableScreen.java @@ -4,19 +4,19 @@ import de.siphalor.mousewheelie.client.util.IScrollableRecipeBook; import net.minecraft.client.gui.screen.ingame.AbstractContainerScreen; import net.minecraft.client.gui.screen.ingame.CraftingTableScreen; -import net.minecraft.client.gui.screen.recipebook.RecipeBookScreen; +import net.minecraft.client.gui.screen.recipebook.RecipeBookWidget; import net.minecraft.container.CraftingTableContainer; import net.minecraft.entity.player.PlayerInventory; -import net.minecraft.network.chat.Component; +import net.minecraft.text.Text; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @Mixin(CraftingTableScreen.class) public abstract class MixinCraftingTableScreen extends AbstractContainerScreen implements IScrollableRecipeBook { - @Shadow @Final private RecipeBookScreen recipeBookGui; + @Shadow @Final private RecipeBookWidget recipeBookGui; - public MixinCraftingTableScreen(CraftingTableContainer container_1, PlayerInventory playerInventory_1, Component textComponent_1) { + public MixinCraftingTableScreen(CraftingTableContainer container_1, PlayerInventory playerInventory_1, Text textComponent_1) { super(container_1, playerInventory_1, textComponent_1); } diff --git a/src/main/java/de/siphalor/mousewheelie/client/mixin/MixinCreativeInventoryScreen.java b/src/main/java/de/siphalor/mousewheelie/client/mixin/MixinCreativeInventoryScreen.java index b0bca5e4..b26fcf9f 100644 --- a/src/main/java/de/siphalor/mousewheelie/client/mixin/MixinCreativeInventoryScreen.java +++ b/src/main/java/de/siphalor/mousewheelie/client/mixin/MixinCreativeInventoryScreen.java @@ -9,7 +9,7 @@ import net.minecraft.client.gui.screen.ingame.CreativeInventoryScreen; import net.minecraft.entity.player.PlayerInventory; import net.minecraft.item.ItemGroup; -import net.minecraft.network.chat.Component; +import net.minecraft.text.Text; import net.minecraft.util.math.MathHelper; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -21,7 +21,7 @@ public abstract class MixinCreativeInventoryScreen extends AbstractInventoryScre @Shadow protected abstract void setSelectedTab(ItemGroup itemGroup_1); - public MixinCreativeInventoryScreen(CreativeInventoryScreen.CreativeContainer container_1, PlayerInventory playerInventory_1, Component textComponent_1) { + public MixinCreativeInventoryScreen(CreativeInventoryScreen.CreativeContainer container_1, PlayerInventory playerInventory_1, Text textComponent_1) { super(container_1, playerInventory_1, textComponent_1); } diff --git a/src/main/java/de/siphalor/mousewheelie/client/mixin/MixinInventoryScreen.java b/src/main/java/de/siphalor/mousewheelie/client/mixin/MixinInventoryScreen.java index e9435619..88917b6b 100644 --- a/src/main/java/de/siphalor/mousewheelie/client/mixin/MixinInventoryScreen.java +++ b/src/main/java/de/siphalor/mousewheelie/client/mixin/MixinInventoryScreen.java @@ -4,19 +4,19 @@ import de.siphalor.mousewheelie.client.util.IScrollableRecipeBook; import net.minecraft.client.gui.screen.ingame.AbstractInventoryScreen; import net.minecraft.client.gui.screen.ingame.InventoryScreen; -import net.minecraft.client.gui.screen.recipebook.RecipeBookScreen; +import net.minecraft.client.gui.screen.recipebook.RecipeBookWidget; import net.minecraft.container.Container; import net.minecraft.entity.player.PlayerInventory; -import net.minecraft.network.chat.Component; +import net.minecraft.text.Text; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @Mixin(InventoryScreen.class) public abstract class MixinInventoryScreen extends AbstractInventoryScreen implements IScrollableRecipeBook { - @Shadow @Final private RecipeBookScreen recipeBook; + @Shadow @Final private RecipeBookWidget recipeBook; - public MixinInventoryScreen(Container container_1, PlayerInventory playerInventory_1, Component textComponent_1) { + public MixinInventoryScreen(Container container_1, PlayerInventory playerInventory_1, Text textComponent_1) { super(container_1, playerInventory_1, textComponent_1); } diff --git a/src/main/java/de/siphalor/mousewheelie/client/mixin/MixinLivingEntity.java b/src/main/java/de/siphalor/mousewheelie/client/mixin/MixinLivingEntity.java index 89faf11b..185af387 100644 --- a/src/main/java/de/siphalor/mousewheelie/client/mixin/MixinLivingEntity.java +++ b/src/main/java/de/siphalor/mousewheelie/client/mixin/MixinLivingEntity.java @@ -18,7 +18,7 @@ protected void onItemUseFinish(CallbackInfo callbackInfo) { if((Object) this instanceof PlayerEntity && Config.eatRefill.value) { PlayerInventory playerInventory = ((PlayerEntity)(Object) this).inventory; SlotRefiller.set(playerInventory, playerInventory.getMainHandStack().copy()); - ClientCore.awaitFoodSlotUpdate = true; + ClientCore.awaitSlotUpdate = true; } } } diff --git a/src/main/java/de/siphalor/mousewheelie/client/mixin/MixinRecipeBookScreen.java b/src/main/java/de/siphalor/mousewheelie/client/mixin/MixinRecipeBookScreen.java index 08e9b9bf..f4c075e5 100644 --- a/src/main/java/de/siphalor/mousewheelie/client/mixin/MixinRecipeBookScreen.java +++ b/src/main/java/de/siphalor/mousewheelie/client/mixin/MixinRecipeBookScreen.java @@ -4,7 +4,7 @@ import de.siphalor.mousewheelie.client.util.IRecipeBookGui; import de.siphalor.mousewheelie.client.util.IRecipeBookResults; import net.minecraft.client.gui.screen.recipebook.RecipeBookResults; -import net.minecraft.client.gui.screen.recipebook.RecipeBookScreen; +import net.minecraft.client.gui.screen.recipebook.RecipeBookWidget; import net.minecraft.client.gui.screen.recipebook.RecipeGroupButtonWidget; import net.minecraft.util.math.MathHelper; import org.spongepowered.asm.mixin.Final; @@ -13,7 +13,7 @@ import java.util.List; -@Mixin(RecipeBookScreen.class) +@Mixin(RecipeBookWidget.class) public abstract class MixinRecipeBookScreen implements IRecipeBookGui { @Shadow @Final protected RecipeBookResults recipesArea; diff --git a/src/main/java/de/siphalor/mousewheelie/client/util/inventory/InventorySorter.java b/src/main/java/de/siphalor/mousewheelie/client/util/inventory/InventorySorter.java index 0dd4837d..032a5b4b 100644 --- a/src/main/java/de/siphalor/mousewheelie/client/util/inventory/InventorySorter.java +++ b/src/main/java/de/siphalor/mousewheelie/client/util/inventory/InventorySorter.java @@ -56,18 +56,18 @@ private void combineStacks() { for(int i = stacks.length - 1; i >= 0; i--) { stack = stacks[i]; if(stack.isEmpty()) continue; - int stackSize = stack.getAmount(); - if(stackSize >= stack.getItem().getMaxAmount()) continue; + int stackSize = stack.getCount(); + if(stackSize >= stack.getItem().getMaxCount()) continue; clickEvents.add(new InteractionManager.ClickEvent(container.syncId, inventorySlots.get(i).id, 0, SlotActionType.PICKUP)); for(int j = 0; j < i; j++) { ItemStack targetStack = stacks[j]; if(targetStack.isEmpty()) continue; - if(targetStack.getAmount() >= targetStack.getItem().getMaxAmount()) continue; + if(targetStack.getCount() >= targetStack.getItem().getMaxCount()) continue; if(stack.getItem() == targetStack.getItem() && ItemStack.areTagsEqual(stack, targetStack)) { - int delta = targetStack.getItem().getMaxAmount() - targetStack.getAmount(); + int delta = targetStack.getItem().getMaxCount() - targetStack.getCount(); delta = Math.min(delta, stackSize); stackSize -= delta; - targetStack.setAmount(targetStack.getAmount() + delta); + targetStack.setCount(targetStack.getCount() + delta); clickEvents.add(new InteractionManager.ClickEvent(container.syncId, inventorySlots.get(j).id, 0, SlotActionType.PICKUP)); if(stackSize <= 0) break; } @@ -81,7 +81,7 @@ private void combineStacks() { clickEvents.clear(); if(stackSize > 0) { InteractionManager.pushClickEvent(container.syncId, inventorySlots.get(i).id, 0, SlotActionType.PICKUP); - stack.setAmount(stackSize); + stack.setCount(stackSize); } else { stacks[i] = ItemStack.EMPTY; } diff --git a/src/main/java/de/siphalor/mousewheelie/client/util/inventory/SlotRefiller.java b/src/main/java/de/siphalor/mousewheelie/client/util/inventory/SlotRefiller.java index b5bdd9af..e7d94487 100644 --- a/src/main/java/de/siphalor/mousewheelie/client/util/inventory/SlotRefiller.java +++ b/src/main/java/de/siphalor/mousewheelie/client/util/inventory/SlotRefiller.java @@ -1,6 +1,8 @@ package de.siphalor.mousewheelie.client.util.inventory; +import de.siphalor.mousewheelie.client.Config; import de.siphalor.mousewheelie.client.InteractionManager; +import de.siphalor.tweed.config.entry.BooleanEntry; import net.minecraft.block.Block; import net.minecraft.entity.player.PlayerInventory; import net.minecraft.item.BlockItem; @@ -9,15 +11,24 @@ import net.minecraft.item.ItemStack; import net.minecraft.server.network.packet.PickFromInventoryC2SPacket; import net.minecraft.server.network.packet.UpdateSelectedSlotC2SPacket; +import net.minecraft.util.DefaultedList; import java.util.Iterator; import java.util.concurrent.ConcurrentLinkedDeque; import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.function.Function; public class SlotRefiller { private static PlayerInventory playerInventory; private static ItemStack stack; + private static final Rule BLOCK_RULE; + private static final Rule ITEMGROUP_RULE; + private static final Rule ITEM_HIERARCHY_RULE; + private static final Rule BLOCK_HIERARCHY_RULE; + private static final Rule FOOD_RULE; + private static final Rule EQUAL_STACK_RULE; + private static final ConcurrentLinkedDeque rules = new ConcurrentLinkedDeque<>(); public static void set(PlayerInventory playerInventory, ItemStack stack) { @@ -48,29 +59,67 @@ public static boolean refill() { public interface Rule { boolean matches(ItemStack oldStack); int findMatchingStack(PlayerInventory playerInventory, ItemStack oldStack); + + @SuppressWarnings("UnnecessaryInterfaceModifier") + public static int iterateInventory(PlayerInventory playerInventory, Function consumer) { + for(int i = 0; i < playerInventory.main.size(); i++) { + if(consumer.apply(playerInventory.main.get(i))) + return i; + } + return -1; + } + } + + abstract static class ConfigRule implements Rule { + BooleanEntry booleanEntry; + + ConfigRule(String name, boolean enabled, String comment) { + booleanEntry = Config.refillRules.register(name, new BooleanEntry(enabled)).setComment(comment); + rules.add(this); + } + + @Override + public final boolean matches(ItemStack oldStack) { + if(booleanEntry.value) + return matchesEnabled(oldStack); + return false; + } + + abstract boolean matchesEnabled(ItemStack oldStack); } + public static void initialize() {} + static { - rules.add(new Rule() { + BLOCK_RULE = new ConfigRule("any-block", true, "Tries to find any block items") { @Override - public boolean matches(ItemStack oldStack) { - return oldStack.getItem().getItemGroup() != null; + boolean matchesEnabled(ItemStack oldStack) { + return oldStack.getItem() instanceof BlockItem; } @Override public int findMatchingStack(PlayerInventory playerInventory, ItemStack oldStack) { - ItemGroup itemGroup = oldStack.getItem().getItemGroup(); - for(int i = 0; i < playerInventory.getInvSize(); i++) { - if(playerInventory.getInvStack(i).getItem().getItemGroup() == itemGroup) return i; - } - return -1; + return Rule.iterateInventory(playerInventory, itemStack -> itemStack.getItem() instanceof BlockItem); + } + }; + + ITEMGROUP_RULE = new ConfigRule("itemgroup", true, "Find items of the same item group") { + @Override + boolean matchesEnabled(ItemStack oldStack) { + return oldStack.getItem().getGroup() != null; + } + + @Override + public int findMatchingStack(PlayerInventory playerInventory, ItemStack oldStack) { + ItemGroup itemGroup = oldStack.getItem().getGroup(); + return Rule.iterateInventory(playerInventory, (itemStack) -> itemStack.getItem().getGroup() == itemGroup); } - }); + }; - rules.add(new Rule() { + ITEM_HIERARCHY_RULE = new ConfigRule("item-hierarchy", true, "Try to find similar items through the item type hierarchy") { @Override - public boolean matches(ItemStack oldStack) { - return oldStack.getItem().getClass() != Item.class; + boolean matchesEnabled(ItemStack oldStack) { + return oldStack.getItem().getClass() != Item.class && !(oldStack.getItem() instanceof BlockItem); } @Override @@ -87,9 +136,11 @@ public int findMatchingStack(PlayerInventory playerInventory, ItemStack oldStack return -1; int index = -1; + + DefaultedList mainInv = playerInventory.main; outer: - for(int i = 0; i < playerInventory.getInvSize(); i++) { - clazz = playerInventory.getInvStack(i).getItem().getClass(); + for(int i = 0; i < mainInv.size(); i++) { + clazz = mainInv.get(i).getItem().getClass(); while(clazz != Item.class) { int classRank = classesSize; for (Iterator iterator = classes.iterator(); iterator.hasNext(); classRank--) { @@ -107,11 +158,11 @@ public int findMatchingStack(PlayerInventory playerInventory, ItemStack oldStack } return index; } - }); + }; - rules.add(new Rule() { + BLOCK_HIERARCHY_RULE = new ConfigRule("block-hierarchy", true, "Try to find similar block items through the block type hierarchy") { @Override - public boolean matches(ItemStack oldStack) { + public boolean matchesEnabled(ItemStack oldStack) { return oldStack.getItem() instanceof BlockItem; } @@ -129,10 +180,12 @@ public int findMatchingStack(PlayerInventory playerInventory, ItemStack oldStack return -1; int index = -1; + DefaultedList mainInv = playerInventory.main; + outer: - for(int i = 0; i < playerInventory.getInvSize(); i++) { - if(!(playerInventory.getInvStack(i).getItem() instanceof BlockItem)) continue; - clazz = ((BlockItem) playerInventory.getInvStack(i).getItem()).getBlock().getClass(); + for(int i = 0; i < mainInv.size(); i++) { + if(!(mainInv.get(i).getItem() instanceof BlockItem)) continue; + clazz = ((BlockItem) mainInv.get(i).getItem()).getBlock().getClass(); while(clazz != Block.class) { int classRank = classesSize; for (Iterator iterator = classes.iterator(); iterator.hasNext(); classRank--) { @@ -150,34 +203,30 @@ public int findMatchingStack(PlayerInventory playerInventory, ItemStack oldStack } return index; } - }); + }; - rules.add(new Rule() { + FOOD_RULE = new ConfigRule("food", true, "Try to find other food items") { @Override - public boolean matches(ItemStack oldStack) { + boolean matchesEnabled(ItemStack oldStack) { return oldStack.isFood(); } @Override public int findMatchingStack(PlayerInventory playerInventory, ItemStack oldStack) { - for(int i = 0; i < playerInventory.getInvSize(); i++) { - if(playerInventory.getInvStack(i).isFood()) - return i; - } - return -1; + return Rule.iterateInventory(playerInventory, ItemStack::isFood); } - }); + }; - rules.add(new Rule() { + EQUAL_STACK_RULE = new ConfigRule("equal-stacks", true, "Try to find equal stacks") { @Override - public boolean matches(ItemStack oldStack) { + boolean matchesEnabled(ItemStack oldStack) { return true; } @Override public int findMatchingStack(PlayerInventory playerInventory, ItemStack oldStack) { - return playerInventory.method_7371(oldStack); + return playerInventory.method_7371(oldStack); } - }); + }; } } diff --git a/src/main/java/de/siphalor/mousewheelie/client/util/inventory/SortMode.java b/src/main/java/de/siphalor/mousewheelie/client/util/inventory/SortMode.java index bb7d2f55..2445d680 100644 --- a/src/main/java/de/siphalor/mousewheelie/client/util/inventory/SortMode.java +++ b/src/main/java/de/siphalor/mousewheelie/client/util/inventory/SortMode.java @@ -48,7 +48,7 @@ public int compare(Integer o1, Integer o2) { if (strings[o2].equals("")) return -1; int comp = strings[o1].compareToIgnoreCase(strings[o2]); if (comp == 0) { - return Integer.compare(stacks[o2].getAmount(), stacks[o1].getAmount()); + return Integer.compare(stacks[o2].getCount(), stacks[o1].getCount()); } return comp; } @@ -63,14 +63,14 @@ void init(Integer[] sortIds, ItemStack[] stacks) { if (stack.isEmpty()) continue; if (!itemToAmountMap.containsKey(stack.getItem())) { HashMap newMap = new HashMap<>(); - newMap.put(stack.getOrCreateTag(), stack.getAmount()); + newMap.put(stack.getOrCreateTag(), stack.getCount()); itemToAmountMap.put(stack.getItem(), newMap); } else { HashMap itemMap = itemToAmountMap.get(stack.getItem()); if (!itemMap.containsKey(stack.getOrCreateTag())) { - itemMap.put(stack.getTag(), stack.getAmount()); + itemMap.put(stack.getTag(), stack.getCount()); } else { - itemMap.replace(stack.getTag(), itemMap.get(stack.getTag()) + stack.getAmount()); + itemMap.replace(stack.getTag(), itemMap.get(stack.getTag()) + stack.getCount()); } } } diff --git a/src/main/java/de/siphalor/mousewheelie/client/util/inventory/ToolPicker.java b/src/main/java/de/siphalor/mousewheelie/client/util/inventory/ToolPicker.java index 79d02717..ee176211 100644 --- a/src/main/java/de/siphalor/mousewheelie/client/util/inventory/ToolPicker.java +++ b/src/main/java/de/siphalor/mousewheelie/client/util/inventory/ToolPicker.java @@ -27,7 +27,7 @@ public int findToolFor(BlockState blockState) { lastToolPickSlot = index; return index; } else { - float breakSpeed = stack.getBlockBreakingSpeed(blockState); + float breakSpeed = stack.getMiningSpeed(blockState); if(breakSpeed > bestBreakSpeed) { bestSpeedSlot = index; bestBreakSpeed = breakSpeed; @@ -36,7 +36,7 @@ public int findToolFor(BlockState blockState) { } if(bestBreakSpeed == -1) { ItemStack stack = inventory.getInvStack(inventory.selectedSlot); - if(stack.isEffectiveOn(blockState) || stack.getBlockBreakingSpeed(blockState) > 1.0F) return inventory.selectedSlot; + if(stack.isEffectiveOn(blockState) || stack.getMiningSpeed(blockState) > 1.0F) return inventory.selectedSlot; } return bestSpeedSlot; } diff --git a/src/main/resources/de.siphalor.mousewheelie.client.json b/src/main/resources/de.siphalor.mousewheelie.client.json index 071b17bd..1f859476 100644 --- a/src/main/resources/de.siphalor.mousewheelie.client.json +++ b/src/main/resources/de.siphalor.mousewheelie.client.json @@ -7,6 +7,7 @@ "MixinAbstractContainerScreen", "MixinClientPlayerEntity", "MixinClientPlayNetworkHandler", + "MixinContainer", "MixinCraftingTableScreen", "MixinCreativeInventoryScreen", "MixinLivingEntity",