From 2a097603cb14564036770b137f1c8879beb9296e Mon Sep 17 00:00:00 2001 From: "yrsegal@gmail.com" Date: Wed, 17 Aug 2022 13:47:44 -0400 Subject: [PATCH] operative slots are now a discovery as well --- .../api/misc/DiscoveryHandlers.java | 13 ++++++ .../api/spell/casting/CastingContext.kt | 41 ++++++++----------- .../casting/operators/spells/OpPlaceBlock.kt | 5 +-- 3 files changed, 33 insertions(+), 26 deletions(-) diff --git a/Common/src/main/java/at/petrak/hexcasting/api/misc/DiscoveryHandlers.java b/Common/src/main/java/at/petrak/hexcasting/api/misc/DiscoveryHandlers.java index e9f3a98d..05f0cdcd 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/misc/DiscoveryHandlers.java +++ b/Common/src/main/java/at/petrak/hexcasting/api/misc/DiscoveryHandlers.java @@ -18,6 +18,7 @@ public class DiscoveryHandlers { private static final List>> MANA_HOLDER_DISCOVERY = new ArrayList<>(); private static final List> GRID_SCALE_MODIFIERS = new ArrayList<>(); private static final List>> ITEM_SLOT_DISCOVERER = new ArrayList<>(); + private static final List>> OPERATIVE_SLOT_DISCOVERER = new ArrayList<>(); public static boolean hasLens(Player player) { for (var predicate : HAS_LENS_PREDICATE) { @@ -52,6 +53,14 @@ public class DiscoveryHandlers { return stacks; } + public static List collectOperableSlots(CastingContext ctx) { + List stacks = Lists.newArrayList(); + for (var discoverer : OPERATIVE_SLOT_DISCOVERER) { + stacks.addAll(discoverer.apply(ctx)); + } + return stacks; + } + public static void addLensPredicate(Predicate predicate) { HAS_LENS_PREDICATE.add(predicate); } @@ -67,4 +76,8 @@ public class DiscoveryHandlers { public static void addItemSlotDiscoverer(Function> discoverer) { ITEM_SLOT_DISCOVERER.add(discoverer); } + + public static void addOperativeSlotDiscoverer(Function> discoverer) { + OPERATIVE_SLOT_DISCOVERER.add(discoverer); + } } diff --git a/Common/src/main/java/at/petrak/hexcasting/api/spell/casting/CastingContext.kt b/Common/src/main/java/at/petrak/hexcasting/api/spell/casting/CastingContext.kt index dbf8c267..0e0866bd 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/spell/casting/CastingContext.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/spell/casting/CastingContext.kt @@ -134,26 +134,12 @@ data class CastingContext( // for what purpose i cannot imagine // http://redditpublic.com/images/b/b2/Items_slot_number.png looks right // and offhand is 150 Inventory.java:464 - // todo discovery? - fun getOperativeSlot(stackOK: Predicate): Int? { - val otherHandStack = this.caster.getItemInHand(this.otherHand) - if (stackOK.test(otherHandStack)) { - return when (this.otherHand) { - InteractionHand.MAIN_HAND -> this.caster.inventory.selected - InteractionHand.OFF_HAND -> 150 - } - } - val anchorSlot = when (this.castingHand) { - // slot to the right of the wand - InteractionHand.MAIN_HAND -> (this.caster.inventory.selected + 1) % 9 - // first hotbar slot - InteractionHand.OFF_HAND -> 0 - } - for (delta in 0 until 9) { - val slot = (anchorSlot + delta) % 9 - val stack = this.caster.inventory.getItem(slot) + fun getOperativeSlot(stackOK: Predicate): ItemStack? { + val operable = DiscoveryHandlers.collectOperableSlots(this) + + for (stack in operable) { if (stackOK.test(stack)) { - return slot + return stack } } return null @@ -167,11 +153,8 @@ data class CastingContext( fun withdrawItem(item: Item, count: Int, actuallyRemove: Boolean): Boolean { if (this.caster.isCreative) return true - val inv = this.caster.inventory // TODO: withdraw from ender chest given a specific ender charm? - val stacksToExamine = inv.items.toMutableList().apply { removeAt(inv.selected) }.asReversed().toMutableList() - stacksToExamine.addAll(inv.offhand) - stacksToExamine.add(inv.getSelected()) + val stacksToExamine = DiscoveryHandlers.collectItemSlots(this) fun matches(stack: ItemStack): Boolean = !stack.isEmpty && stack.`is`(item) @@ -226,6 +209,18 @@ data class CastingContext( add(inv.getSelected()) } } + + DiscoveryHandlers.addOperativeSlotDiscoverer { + val slots = mutableListOf() + val anchorSlot = if (it.castingHand == InteractionHand.MAIN_HAND) (it.caster.inventory.selected + 1) % 9 else 0 + + slots.add(it.caster.getItemInHand(it.otherHand)) + for (delta in 0 until 9) { + val slot = (anchorSlot + delta) % 9 + slots.add(it.caster.inventory.getItem(slot)) + } + slots + } } } } diff --git a/Common/src/main/java/at/petrak/hexcasting/common/casting/operators/spells/OpPlaceBlock.kt b/Common/src/main/java/at/petrak/hexcasting/common/casting/operators/spells/OpPlaceBlock.kt index e67e3e68..bd3758b0 100644 --- a/Common/src/main/java/at/petrak/hexcasting/common/casting/operators/spells/OpPlaceBlock.kt +++ b/Common/src/main/java/at/petrak/hexcasting/common/casting/operators/spells/OpPlaceBlock.kt @@ -63,9 +63,8 @@ object OpPlaceBlock : SpellOperator { ) val bstate = ctx.world.getBlockState(pos) - val placeeSlot = ctx.getOperativeSlot { it.item is BlockItem } - if (placeeSlot != null) { - val placeeStack = ctx.caster.inventory.getItem(placeeSlot).copy() + val placeeStack = ctx.getOperativeSlot { it.item is BlockItem }?.copy() + if (placeeStack != null) { if (!IXplatAbstractions.INSTANCE.isPlacingAllowed(ctx.world, pos, placeeStack, ctx.caster)) return