Skip to content

Commit 544c570

Browse files
committed
Abstract out inventory searches from PlayerBasedCastEnv to be in CastingEnvironment for use in CircleCastEnv
1 parent eb0ab95 commit 544c570

File tree

3 files changed

+125
-95
lines changed

3 files changed

+125
-95
lines changed

Common/src/main/java/at/petrak/hexcasting/api/casting/eval/CastingEnvironment.java

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import net.minecraft.world.InteractionHand;
2020
import net.minecraft.world.entity.Entity;
2121
import net.minecraft.world.entity.LivingEntity;
22+
import net.minecraft.world.entity.player.Inventory;
2223
import net.minecraft.world.entity.player.Player;
2324
import net.minecraft.world.item.ItemStack;
2425
import net.minecraft.world.phys.Vec3;
@@ -360,11 +361,88 @@ public InteractionHand getOtherHand() {
360361
*/
361362
protected abstract List<ItemStack> getUsableStacks(StackDiscoveryMode mode);
362363

364+
protected List<ItemStack> getUsableStacksForPlayer(StackDiscoveryMode mode, @Nullable InteractionHand castingHand, ServerPlayer caster) {
365+
return switch (mode) {
366+
case QUERY -> {
367+
var out = new ArrayList<ItemStack>();
368+
369+
if (castingHand == null) {
370+
var mainhand = caster.getItemInHand(InteractionHand.MAIN_HAND);
371+
if (!mainhand.isEmpty()) {
372+
out.add(mainhand);
373+
}
374+
375+
var offhand = caster.getItemInHand(InteractionHand.OFF_HAND);
376+
if (!offhand.isEmpty()) {
377+
out.add(offhand);
378+
}
379+
} else {
380+
var offhand = caster.getItemInHand(HexUtils.otherHand(castingHand));
381+
if (!offhand.isEmpty()) {
382+
out.add(offhand);
383+
}
384+
}
385+
386+
// If we're casting from the main hand, try to pick from the slot one to the right of the selected slot
387+
// Otherwise, scan the hotbar left to right
388+
var anchorSlot = castingHand != InteractionHand.OFF_HAND
389+
? (caster.getInventory().selected + 1) % 9
390+
: 0;
391+
392+
393+
for (int delta = 0; delta < 9; delta++) {
394+
var slot = (anchorSlot + delta) % 9;
395+
out.add(caster.getInventory().getItem(slot));
396+
}
397+
398+
yield out;
399+
}
400+
case EXTRACTION -> {
401+
// https://wiki.vg/Inventory is WRONG
402+
// slots 0-8 are the hotbar
403+
// for what purpose i cannot imagine
404+
// http://redditpublic.com/images/b/b2/Items_slot_number.png looks right
405+
// and offhand is 150 Inventory.java:464
406+
var out = new ArrayList<ItemStack>();
407+
408+
// First, the inventory backwards
409+
// We use inv.items here to get the main inventory, but not offhand or armor
410+
Inventory inv = caster.getInventory();
411+
for (int i = inv.items.size() - 1; i >= 0; i--) {
412+
if (i != inv.selected) {
413+
out.add(inv.items.get(i));
414+
}
415+
}
416+
417+
// then the offhand, then the selected hand
418+
out.addAll(inv.offhand);
419+
out.add(inv.getSelected());
420+
421+
yield out;
422+
}
423+
};
424+
}
425+
363426
/**
364427
* Get the primary/secondary item stacks this env can use (i.e. main hand and offhand for the player).
365428
*/
366429
protected abstract List<HeldItemInfo> getPrimaryStacks();
367430

431+
protected List<HeldItemInfo> getPrimaryStacksForPlayer(InteractionHand castingHand, ServerPlayer caster) {
432+
var primaryItem = caster.getItemInHand(castingHand);
433+
434+
if (primaryItem.isEmpty())
435+
primaryItem = ItemStack.EMPTY.copy();
436+
437+
var secondaryItem = caster.getItemInHand(HexUtils.otherHand(castingHand));
438+
439+
if (secondaryItem.isEmpty())
440+
secondaryItem = ItemStack.EMPTY.copy();
441+
442+
return List.of(new HeldItemInfo(secondaryItem, HexUtils.otherHand(castingHand)), new HeldItemInfo(primaryItem,
443+
castingHand));
444+
}
445+
368446
/**
369447
* Return the slot from which to take blocks and items.
370448
*/
@@ -463,6 +541,38 @@ public boolean withdrawItem(Predicate<ItemStack> stackOk, int count, boolean act
463541
*/
464542
public abstract boolean replaceItem(Predicate<ItemStack> stackOk, ItemStack replaceWith, @Nullable InteractionHand hand);
465543

544+
545+
public boolean replaceItemForPlayer(Predicate<ItemStack> stackOk, ItemStack replaceWith, @Nullable InteractionHand hand, ServerPlayer caster) {
546+
if (caster == null)
547+
return false;
548+
549+
if (hand != null && stackOk.test(caster.getItemInHand(hand))) {
550+
caster.setItemInHand(hand, replaceWith);
551+
return true;
552+
}
553+
554+
Inventory inv = caster.getInventory();
555+
for (int i = inv.items.size() - 1; i >= 0; i--) {
556+
if (i != inv.selected) {
557+
if (stackOk.test(inv.items.get(i))) {
558+
inv.setItem(i, replaceWith);
559+
return true;
560+
}
561+
}
562+
}
563+
564+
if (stackOk.test(caster.getItemInHand(getOtherHand()))) {
565+
caster.setItemInHand(getOtherHand(), replaceWith);
566+
return true;
567+
}
568+
if (stackOk.test(caster.getItemInHand(getCastingHand()))) {
569+
caster.setItemInHand(getCastingHand(), replaceWith);
570+
return true;
571+
}
572+
573+
return false;
574+
}
575+
466576
/**
467577
* The order/mode stacks should be discovered in
468578
*/

Common/src/main/java/at/petrak/hexcasting/api/casting/eval/env/CircleCastEnv.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,18 +166,27 @@ public InteractionHand getCastingHand() {
166166
}
167167

168168
@Override
169+
// TODO: Could do something like get items in inventories adjacent to the circle?
169170
protected List<ItemStack> getUsableStacks(StackDiscoveryMode mode) {
170-
return new ArrayList<>(); // TODO: Could do something like get items in inventories adjacent to the circle?
171+
if (this.getCaster() != null)
172+
return getUsableStacksForPlayer(mode, null, this.getCaster());
173+
return new ArrayList<>();
171174
}
172175

173176
@Override
177+
// TODO: Adjacent inv!
174178
protected List<HeldItemInfo> getPrimaryStacks() {
175-
return List.of(); // TODO: Adjacent inv!
179+
if (this.getCaster() != null)
180+
return getPrimaryStacksForPlayer(InteractionHand.OFF_HAND, this.getCaster());
181+
return List.of();
176182
}
177183

178184
@Override
185+
// TODO: Adjacent inv!
179186
public boolean replaceItem(Predicate<ItemStack> stackOk, ItemStack replaceWith, @Nullable InteractionHand hand) {
180-
return false; // TODO: Adjacent inv!
187+
if (this.getCaster() != null)
188+
return replaceItemForPlayer(stackOk, replaceWith, hand, this.getCaster());
189+
return false;
181190
}
182191

183192
@Override

Common/src/main/java/at/petrak/hexcasting/api/casting/eval/env/PlayerBasedCastEnv.java

Lines changed: 3 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -70,106 +70,17 @@ public void postExecution(CastResult result) {
7070

7171
@Override
7272
protected List<ItemStack> getUsableStacks(StackDiscoveryMode mode) {
73-
return switch (mode) {
74-
case QUERY -> {
75-
var out = new ArrayList<ItemStack>();
76-
77-
var offhand = this.caster.getItemInHand(HexUtils.otherHand(this.castingHand));
78-
if (!offhand.isEmpty()) {
79-
out.add(offhand);
80-
}
81-
82-
// If we're casting from the main hand, try to pick from the slot one to the right of the selected slot
83-
// Otherwise, scan the hotbar left to right
84-
var anchorSlot = this.castingHand == InteractionHand.MAIN_HAND
85-
? (this.caster.getInventory().selected + 1) % 9
86-
: 0;
87-
88-
89-
for (int delta = 0; delta < 9; delta++) {
90-
var slot = (anchorSlot + delta) % 9;
91-
out.add(this.caster.getInventory().getItem(slot));
92-
}
93-
94-
yield out;
95-
}
96-
case EXTRACTION -> {
97-
// https://wiki.vg/Inventory is WRONG
98-
// slots 0-8 are the hotbar
99-
// for what purpose i cannot imagine
100-
// http://redditpublic.com/images/b/b2/Items_slot_number.png looks right
101-
// and offhand is 150 Inventory.java:464
102-
var out = new ArrayList<ItemStack>();
103-
104-
// First, the inventory backwards
105-
// We use inv.items here to get the main inventory, but not offhand or armor
106-
Inventory inv = this.caster.getInventory();
107-
for (int i = inv.items.size() - 1; i >= 0; i--) {
108-
if (i != inv.selected) {
109-
out.add(inv.items.get(i));
110-
}
111-
}
112-
113-
// then the offhand, then the selected hand
114-
out.addAll(inv.offhand);
115-
out.add(inv.getSelected());
116-
117-
yield out;
118-
}
119-
};
73+
return getUsableStacksForPlayer(mode, castingHand, caster);
12074
}
12175

12276
@Override
12377
protected List<HeldItemInfo> getPrimaryStacks() {
124-
var primaryItem = this.caster.getItemInHand(this.castingHand);
125-
126-
if (primaryItem.isEmpty())
127-
primaryItem = ItemStack.EMPTY.copy();
128-
129-
return List.of(new HeldItemInfo(getAlternateItem(), this.getOtherHand()), new HeldItemInfo(primaryItem,
130-
this.castingHand));
131-
}
132-
133-
ItemStack getAlternateItem() {
134-
var otherHand = HexUtils.otherHand(this.castingHand);
135-
var stack = this.caster.getItemInHand(otherHand);
136-
if (stack.isEmpty()) {
137-
return ItemStack.EMPTY.copy();
138-
} else {
139-
return stack;
140-
}
78+
return getPrimaryStacksForPlayer(this.castingHand, this.caster);
14179
}
14280

14381
@Override
14482
public boolean replaceItem(Predicate<ItemStack> stackOk, ItemStack replaceWith, @Nullable InteractionHand hand) {
145-
if (caster == null)
146-
return false;
147-
148-
if (hand != null && stackOk.test(caster.getItemInHand(hand))) {
149-
caster.setItemInHand(hand, replaceWith);
150-
return true;
151-
}
152-
153-
Inventory inv = this.caster.getInventory();
154-
for (int i = inv.items.size() - 1; i >= 0; i--) {
155-
if (i != inv.selected) {
156-
if (stackOk.test(inv.items.get(i))) {
157-
inv.setItem(i, replaceWith);
158-
return true;
159-
}
160-
}
161-
}
162-
163-
if (stackOk.test(caster.getItemInHand(getOtherHand()))) {
164-
caster.setItemInHand(getOtherHand(), replaceWith);
165-
return true;
166-
}
167-
if (stackOk.test(caster.getItemInHand(getCastingHand()))) {
168-
caster.setItemInHand(getCastingHand(), replaceWith);
169-
return true;
170-
}
171-
172-
return false;
83+
return replaceItemForPlayer(stackOk, replaceWith, hand, this.caster);
17384
}
17485

17586
@Override

0 commit comments

Comments
 (0)