From 09cecef68ecab8f9fed90eb189eedd441b676ec4 Mon Sep 17 00:00:00 2001 From: Sollace Date: Thu, 16 May 2024 21:42:58 +0100 Subject: [PATCH] Fix quick moving items in the spellbook. Fixes #348 --- .../client/gui/spellbook/IngredientTree.java | 4 +- .../gui/spellbook/SpellbookChapterList.java | 10 --- .../SpellbookCraftingPageContent.java | 5 -- .../SpellbookProfilePageContent.java | 5 -- .../client/gui/spellbook/SpellbookScreen.java | 14 +-- .../SpellbookTraitDexPageContent.java | 4 +- .../container/SpellbookScreenHandler.java | 86 +++++++++++++------ .../unicopia/container/SpellbookState.java | 5 ++ 8 files changed, 74 insertions(+), 59 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/IngredientTree.java b/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/IngredientTree.java index 831ea7ca..fb4f6337 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/IngredientTree.java +++ b/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/IngredientTree.java @@ -18,6 +18,8 @@ import net.minecraft.client.gui.DrawContext; import net.minecraft.client.item.TooltipContext; import net.minecraft.client.render.*; import com.minelittlepony.unicopia.client.render.RenderLayers; +import com.minelittlepony.unicopia.container.SpellbookState; + import net.minecraft.client.render.item.ItemRenderer; import net.minecraft.client.render.model.json.ModelTransformationMode; import net.minecraft.client.util.math.MatrixStack; @@ -324,7 +326,7 @@ public class IngredientTree implements SpellbookRecipe.CraftingTreeBuilder { @Override public void onClick() { if (MinecraftClient.getInstance().currentScreen instanceof SpellbookScreen spellbook) { - spellbook.getState().setCurrentPageId(SpellbookChapterList.TRAIT_DEX_ID); + spellbook.getState().setCurrentPageId(SpellbookState.TRAIT_DEX_ID); spellbook.getTraitDex().pageTo(spellbook, trait); } } diff --git a/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/SpellbookChapterList.java b/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/SpellbookChapterList.java index 3e91b7a1..6b206793 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/SpellbookChapterList.java +++ b/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/SpellbookChapterList.java @@ -6,16 +6,10 @@ import java.util.stream.Stream; import com.minelittlepony.common.client.gui.IViewRoot; import com.minelittlepony.unicopia.Debug; -import com.minelittlepony.unicopia.Unicopia; - import net.minecraft.client.gui.DrawContext; import net.minecraft.util.Identifier; public class SpellbookChapterList { - public static final Identifier CRAFTING_ID = Unicopia.id("crafting"); - public static final Identifier PROFILE_ID = Unicopia.id("profile"); - public static final Identifier TRAIT_DEX_ID = Unicopia.id("traits"); - private final SpellbookScreen screen; private final Chapter craftingChapter; @@ -73,10 +67,6 @@ public class SpellbookChapterList { default void copyStateFrom(Content old) {} - default boolean showInventory() { - return false; - } - default Identifier getIcon(Chapter chapter, Identifier icon) { return icon; } diff --git a/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/SpellbookCraftingPageContent.java b/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/SpellbookCraftingPageContent.java index a6fde947..513e4e8f 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/SpellbookCraftingPageContent.java +++ b/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/SpellbookCraftingPageContent.java @@ -58,11 +58,6 @@ public class SpellbookCraftingPageContent extends ScrollContainer implements Spe init(this::initPageContent); } - @Override - public boolean showInventory() { - return state.getOffset() == 0; - } - private void initPageContent() { getContentPadding().setVertical(10); getContentPadding().bottom = 30; diff --git a/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/SpellbookProfilePageContent.java b/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/SpellbookProfilePageContent.java index 65c6ebe2..f0829b23 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/SpellbookProfilePageContent.java +++ b/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/SpellbookProfilePageContent.java @@ -58,11 +58,6 @@ public class SpellbookProfilePageContent implements SpellbookChapterList.Content } } - @Override - public boolean showInventory() { - return true; - } - @Override public void draw(DrawContext context, int mouseX, int mouseY, IViewRoot container) { diff --git a/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/SpellbookScreen.java b/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/SpellbookScreen.java index 20a1dfb7..56ff0539 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/SpellbookScreen.java +++ b/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/SpellbookScreen.java @@ -52,12 +52,11 @@ public class SpellbookScreen extends HandledScreen imple private final RecipeBookWidget recipeBook = new RecipeBookWidget(); - private final Chapter craftingChapter; private final SpellbookTraitDexPageContent traitDex = new SpellbookTraitDexPageContent(this); private final SpellbookChapterList chapters = new SpellbookChapterList(this, - craftingChapter = new Chapter(SpellbookChapterList.CRAFTING_ID, TabSide.LEFT, 0, 0, Optional.of(new SpellbookCraftingPageContent(this))), - new Chapter(SpellbookChapterList.PROFILE_ID, TabSide.LEFT, 1, 0, Optional.of(new SpellbookProfilePageContent(this))), - new Chapter(SpellbookChapterList.TRAIT_DEX_ID, TabSide.LEFT, 3, 0, Optional.of(traitDex)) + new Chapter(SpellbookState.CRAFTING_ID, TabSide.LEFT, 0, 0, Optional.of(new SpellbookCraftingPageContent(this))), + new Chapter(SpellbookState.PROFILE_ID, TabSide.LEFT, 1, 0, Optional.of(new SpellbookProfilePageContent(this))), + new Chapter(SpellbookState.TRAIT_DEX_ID, TabSide.LEFT, 3, 0, Optional.of(traitDex)) ); private final SpellbookTabBar tabs = new SpellbookTabBar(this, chapters); @@ -68,13 +67,6 @@ public class SpellbookScreen extends HandledScreen imple backgroundWidth = 405; backgroundHeight = 219; contentBounds = new Bounds(CONTENT_PADDING, CONTENT_PADDING, backgroundWidth - CONTENT_PADDING * 2, backgroundHeight - CONTENT_PADDING * 3 - 2); - - handler.addSlotShowingCondition(slotType -> { - if (slotType == SlotType.INVENTORY) { - return chapters.getCurrentChapter().content().filter(Content::showInventory).isPresent(); - } - return chapters.getCurrentChapter() == craftingChapter; - }); handler.getSpellbookState().setSynchronizer(state -> { Channel.CLIENT_SPELLBOOK_UPDATE.sendToServer(MsgSpellbookStateChanged.create(handler, state)); }); diff --git a/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/SpellbookTraitDexPageContent.java b/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/SpellbookTraitDexPageContent.java index c5f7f347..6322bf1c 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/SpellbookTraitDexPageContent.java +++ b/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/SpellbookTraitDexPageContent.java @@ -73,7 +73,7 @@ public class SpellbookTraitDexPageContent implements SpellbookChapterList.Conten return; } page /= 2; - state = screen.getState().getState(SpellbookChapterList.TRAIT_DEX_ID); + state = screen.getState().getState(SpellbookState.TRAIT_DEX_ID); state.setOffset(page); leftPage.verticalScrollbar.scrollBy(leftPage.verticalScrollbar.getScrubber().getPosition()); rightPage.verticalScrollbar.scrollBy(rightPage.verticalScrollbar.getScrubber().getPosition()); @@ -84,7 +84,7 @@ public class SpellbookTraitDexPageContent implements SpellbookChapterList.Conten @Override public void onRecipesChanged() { - init(screen, SpellbookChapterList.TRAIT_DEX_ID); + init(screen, SpellbookState.TRAIT_DEX_ID); } private final class DexPage extends ScrollContainer { diff --git a/src/main/java/com/minelittlepony/unicopia/container/SpellbookScreenHandler.java b/src/main/java/com/minelittlepony/unicopia/container/SpellbookScreenHandler.java index 47a088f1..de69aacf 100644 --- a/src/main/java/com/minelittlepony/unicopia/container/SpellbookScreenHandler.java +++ b/src/main/java/com/minelittlepony/unicopia/container/SpellbookScreenHandler.java @@ -1,8 +1,6 @@ package com.minelittlepony.unicopia.container; import java.util.*; -import java.util.function.Predicate; - import org.jetbrains.annotations.Nullable; import com.minelittlepony.unicopia.ability.magic.spell.crafting.SpellbookRecipe; @@ -55,8 +53,6 @@ public class SpellbookScreenHandler extends ScreenHandler { private final ScreenHandlerContext context; - private Predicate canShowSlots; - private final SpellbookState state; @Nullable @@ -166,12 +162,13 @@ public class SpellbookScreenHandler extends ScreenHandler { return state; } - public void addSlotShowingCondition(Predicate canShowSlots) { - this.canShowSlots = canShowSlots; - } - public boolean canShowSlots(SlotType type) { - return canShowSlots == null || canShowSlots.test(type); + Identifier pageId = state.getCurrentPageId().orElse(null); + boolean isCraftingPage = SpellbookState.CRAFTING_ID.equals(pageId); + return switch (type) { + case INVENTORY -> isCraftingPage ? state.getState(pageId).getOffset() == 0 : SpellbookState.PROFILE_ID.equals(pageId); + case CRAFTING -> isCraftingPage; + }; } public int getOutputSlotId() { @@ -212,11 +209,16 @@ public class SpellbookScreenHandler extends ScreenHandler { return ItemStack.EMPTY; } + @Override + public boolean canInsertIntoSlot(ItemStack stack, Slot slot) { + return slot != null && slot.canInsert(stack) && slot.isEnabled(); + } + @Override public ItemStack quickMove(PlayerEntity player, int index) { Slot sourceSlot = slots.get(index); - if (sourceSlot == null || !sourceSlot.hasStack()) { + if (sourceSlot == null || !sourceSlot.hasStack() || (sourceSlot instanceof SpellSlot)) { return ItemStack.EMPTY; } @@ -228,35 +230,69 @@ public class SpellbookScreenHandler extends ScreenHandler { } if (index >= HOTBAR_START && !(sourceSlot instanceof OutputSlot || sourceSlot instanceof InputSlot)) { - if (!gemSlot.hasStack() && gemSlot.canInsert(stack)) { - if (insertItem(transferredStack, GEM_SLOT_INDEX, GEM_SLOT_INDEX + 1, false)) { + // hotbar or inventory -> crafting grid + if (canShowSlots(SlotType.CRAFTING)) { + if (!gemSlot.hasStack() && gemSlot.canInsert(stack)) { + if (insertItem(transferredStack, GEM_SLOT_INDEX, GEM_SLOT_INDEX + 1, false)) { + onContentChanged(input); + return ItemStack.EMPTY; + } + } + + if (insertItem(transferredStack, 0, GEM_SLOT_INDEX, false)) { + sourceSlot.onQuickTransfer(transferredStack, stack); onContentChanged(input); return ItemStack.EMPTY; } } - if (insertItem(transferredStack, 0, GEM_SLOT_INDEX, false)) { - sourceSlot.onQuickTransfer(transferredStack, stack); - onContentChanged(input); - return ItemStack.EMPTY; + if (index < HOTBAR_END) { + if (canShowSlots(SlotType.INVENTORY)) { + // hotbar -> inventory + // insert into inventory - armor + if (insertItem(transferredStack, HOTBAR_END + 27, HOTBAR_END + 27 + 4, false)) { + sourceSlot.onQuickTransfer(transferredStack, stack); + onContentChanged(input); + return ItemStack.EMPTY; + } + + // insert into inventory - inventory + if (insertItem(transferredStack, HOTBAR_END, HOTBAR_END + 27, false)) { + sourceSlot.onQuickTransfer(transferredStack, stack); + onContentChanged(input); + return ItemStack.EMPTY; + } + } + } else { + // inventory -> hotbar + if (insertItem(transferredStack, HOTBAR_START, HOTBAR_END, true)) { + sourceSlot.onQuickTransfer(transferredStack, stack); + onContentChanged(input); + return ItemStack.EMPTY; + } } } else { + // crafting grid -> hotbar if (insertItem(transferredStack, HOTBAR_START, HOTBAR_END, true)) { sourceSlot.onQuickTransfer(transferredStack, stack); onContentChanged(input); return ItemStack.EMPTY; } - if (insertItem(transferredStack, HOTBAR_END + 27, HOTBAR_END + 27 + 4, false)) { - sourceSlot.onQuickTransfer(transferredStack, stack); - onContentChanged(input); - return ItemStack.EMPTY; - } + if (canShowSlots(SlotType.INVENTORY)) { + // crafting grid -> armor + if (insertItem(transferredStack, HOTBAR_END + 27, HOTBAR_END + 27 + 4, false)) { + sourceSlot.onQuickTransfer(transferredStack, stack); + onContentChanged(input); + return ItemStack.EMPTY; + } - if (insertItem(transferredStack, HOTBAR_END, HOTBAR_END + 27, false)) { - sourceSlot.onQuickTransfer(transferredStack, stack); - onContentChanged(input); - return ItemStack.EMPTY; + // crafting grid -> inventory + if (insertItem(transferredStack, HOTBAR_END, HOTBAR_END + 27, false)) { + sourceSlot.onQuickTransfer(transferredStack, stack); + onContentChanged(input); + return ItemStack.EMPTY; + } } } diff --git a/src/main/java/com/minelittlepony/unicopia/container/SpellbookState.java b/src/main/java/com/minelittlepony/unicopia/container/SpellbookState.java index c616a8a1..9fedcffc 100644 --- a/src/main/java/com/minelittlepony/unicopia/container/SpellbookState.java +++ b/src/main/java/com/minelittlepony/unicopia/container/SpellbookState.java @@ -3,6 +3,7 @@ package com.minelittlepony.unicopia.container; import java.util.*; import java.util.function.Consumer; +import com.minelittlepony.unicopia.Unicopia; import com.minelittlepony.unicopia.network.datasync.Synchronizable; import com.minelittlepony.unicopia.util.NbtSerialisable; @@ -13,6 +14,10 @@ import net.minecraft.util.Identifier; import net.minecraft.util.math.MathHelper; public class SpellbookState extends Synchronizable implements NbtSerialisable { + public static final Identifier CRAFTING_ID = Unicopia.id("crafting"); + public static final Identifier PROFILE_ID = Unicopia.id("profile"); + public static final Identifier TRAIT_DEX_ID = Unicopia.id("traits"); + private Optional currentPageId = Optional.empty(); private boolean dirty;