operative slots are now a discovery as well

This commit is contained in:
yrsegal@gmail.com 2022-08-17 13:47:44 -04:00
parent 36321c3cd0
commit 2a097603cb
3 changed files with 33 additions and 26 deletions

View file

@ -18,6 +18,7 @@ public class DiscoveryHandlers {
private static final List<Function<CastingHarness, List<ManaHolder>>> MANA_HOLDER_DISCOVERY = new ArrayList<>();
private static final List<ToFloatFunction<Player>> GRID_SCALE_MODIFIERS = new ArrayList<>();
private static final List<Function<CastingContext, List<ItemStack>>> ITEM_SLOT_DISCOVERER = new ArrayList<>();
private static final List<Function<CastingContext, List<ItemStack>>> 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<ItemStack> collectOperableSlots(CastingContext ctx) {
List<ItemStack> stacks = Lists.newArrayList();
for (var discoverer : OPERATIVE_SLOT_DISCOVERER) {
stacks.addAll(discoverer.apply(ctx));
}
return stacks;
}
public static void addLensPredicate(Predicate<Player> predicate) {
HAS_LENS_PREDICATE.add(predicate);
}
@ -67,4 +76,8 @@ public class DiscoveryHandlers {
public static void addItemSlotDiscoverer(Function<CastingContext, List<ItemStack>> discoverer) {
ITEM_SLOT_DISCOVERER.add(discoverer);
}
public static void addOperativeSlotDiscoverer(Function<CastingContext, List<ItemStack>> discoverer) {
OPERATIVE_SLOT_DISCOVERER.add(discoverer);
}
}

View file

@ -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<ItemStack>): 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>): 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<ItemStack>()
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
}
}
}
}

View file

@ -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