Skip to content

Commit 2f56641

Browse files
committed
Allow spell circles to access player inventory
1 parent b4aa319 commit 2f56641

File tree

1 file changed

+101
-3
lines changed

1 file changed

+101
-3
lines changed

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

Lines changed: 101 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import net.minecraft.sounds.SoundSource;
2222
import net.minecraft.world.InteractionHand;
2323
import net.minecraft.world.entity.LivingEntity;
24+
import net.minecraft.world.entity.player.Inventory;
2425
import net.minecraft.world.item.ItemStack;
2526
import net.minecraft.world.phys.Vec3;
2627
import org.jetbrains.annotations.Nullable;
@@ -158,19 +159,116 @@ public InteractionHand getCastingHand() {
158159
return InteractionHand.MAIN_HAND;
159160
}
160161

162+
// TODO: if I'm understanding correctly, this should probably be replaced with a gloop-like solution
163+
// allow access to player inv for now though
161164
@Override
162165
protected List<ItemStack> getUsableStacks(StackDiscoveryMode mode) {
163-
return new ArrayList<>(); // TODO: Could do something like get items in inventories adjacent to the circle?
166+
var caster = this.execState.getCaster(this.world);
167+
if (caster == null) {
168+
return List.of();
169+
}
170+
return switch (mode) {
171+
case QUERY -> {
172+
var out = new ArrayList<ItemStack>();
173+
174+
var mainhand = caster.getItemInHand(InteractionHand.MAIN_HAND);
175+
if (!mainhand.isEmpty()) {
176+
out.add(mainhand);
177+
}
178+
179+
var offhand = caster.getItemInHand(InteractionHand.OFF_HAND);
180+
if (!offhand.isEmpty()) {
181+
out.add(offhand);
182+
}
183+
184+
var anchorSlot = (caster.getInventory().selected + 1) % 9;
185+
186+
187+
for (int delta = 0; delta < 9; delta++) {
188+
var slot = (anchorSlot + delta) % 9;
189+
out.add(caster.getInventory().getItem(slot));
190+
}
191+
192+
yield out;
193+
}
194+
case EXTRACTION -> {
195+
// https://wiki.vg/Inventory is WRONG
196+
// slots 0-8 are the hotbar
197+
// for what purpose i cannot imagine
198+
// http://redditpublic.com/images/b/b2/Items_slot_number.png looks right
199+
// and offhand is 150 Inventory.java:464
200+
var out = new ArrayList<ItemStack>();
201+
202+
// First, the inventory backwards
203+
// We use inv.items here to get the main inventory, but not offhand or armor
204+
Inventory inv = caster.getInventory();
205+
for (int i = inv.items.size() - 1; i >= 0; i--) {
206+
if (i != inv.selected) {
207+
out.add(inv.items.get(i));
208+
}
209+
}
210+
211+
// then the offhand, then the selected hand
212+
out.addAll(inv.offhand);
213+
out.add(inv.getSelected());
214+
215+
yield out;
216+
}
217+
};
164218
}
165219

166220
@Override
167221
protected List<HeldItemInfo> getPrimaryStacks() {
168-
return List.of(); // TODO: Adjacent inv!
222+
var caster = this.execState.getCaster(this.world);
223+
if (caster == null) {
224+
return List.of();
225+
}
226+
227+
var primaryItem = caster.getItemInHand(InteractionHand.MAIN_HAND);
228+
229+
if (primaryItem.isEmpty())
230+
primaryItem = ItemStack.EMPTY.copy();
231+
232+
var alternateItem = caster.getItemInHand(InteractionHand.OFF_HAND);
233+
234+
if (alternateItem.isEmpty())
235+
alternateItem = ItemStack.EMPTY.copy();
236+
237+
return List.of(new HeldItemInfo(primaryItem, InteractionHand.MAIN_HAND),
238+
new HeldItemInfo(alternateItem, InteractionHand.OFF_HAND));
169239
}
170240

171241
@Override
172242
public boolean replaceItem(Predicate<ItemStack> stackOk, ItemStack replaceWith, @Nullable InteractionHand hand) {
173-
return false; // TODO: Adjacent inv!
243+
var caster = this.execState.getCaster(this.world);
244+
if (caster == null)
245+
return false;
246+
247+
if (hand != null && stackOk.test(caster.getItemInHand(hand))) {
248+
caster.setItemInHand(hand, replaceWith);
249+
return true;
250+
}
251+
252+
Inventory inv = caster.getInventory();
253+
for (int i = inv.items.size() - 1; i >= 0; i--) {
254+
if (i != inv.selected) {
255+
if (stackOk.test(inv.items.get(i))) {
256+
inv.setItem(i, replaceWith);
257+
return true;
258+
}
259+
}
260+
}
261+
262+
if (stackOk.test(caster.getItemInHand(InteractionHand.MAIN_HAND))) {
263+
caster.setItemInHand(InteractionHand.MAIN_HAND, replaceWith);
264+
return true;
265+
}
266+
if (stackOk.test(caster.getItemInHand(InteractionHand.OFF_HAND))) {
267+
caster.setItemInHand(InteractionHand.OFF_HAND, replaceWith);
268+
return true;
269+
}
270+
271+
return false;
174272
}
175273

176274
@Override

0 commit comments

Comments
 (0)