Skip to content

Commit 2111328

Browse files
committed
Allow spell circles to access player inventory
1 parent f3edb1c commit 2111328

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;
@@ -165,19 +166,116 @@ public InteractionHand getCastingHand() {
165166
return InteractionHand.MAIN_HAND;
166167
}
167168

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

173227
@Override
174228
protected List<HeldItemInfo> getPrimaryStacks() {
175-
return List.of(); // TODO: Adjacent inv!
229+
var caster = this.execState.getCaster(this.world);
230+
if (caster == null) {
231+
return List.of();
232+
}
233+
234+
var primaryItem = caster.getItemInHand(InteractionHand.MAIN_HAND);
235+
236+
if (primaryItem.isEmpty())
237+
primaryItem = ItemStack.EMPTY.copy();
238+
239+
var alternateItem = caster.getItemInHand(InteractionHand.OFF_HAND);
240+
241+
if (alternateItem.isEmpty())
242+
alternateItem = ItemStack.EMPTY.copy();
243+
244+
return List.of(new HeldItemInfo(primaryItem, InteractionHand.MAIN_HAND),
245+
new HeldItemInfo(alternateItem, InteractionHand.OFF_HAND));
176246
}
177247

178248
@Override
179249
public boolean replaceItem(Predicate<ItemStack> stackOk, ItemStack replaceWith, @Nullable InteractionHand hand) {
180-
return false; // TODO: Adjacent inv!
250+
var caster = this.execState.getCaster(this.world);
251+
if (caster == null)
252+
return false;
253+
254+
if (hand != null && stackOk.test(caster.getItemInHand(hand))) {
255+
caster.setItemInHand(hand, replaceWith);
256+
return true;
257+
}
258+
259+
Inventory inv = caster.getInventory();
260+
for (int i = inv.items.size() - 1; i >= 0; i--) {
261+
if (i != inv.selected) {
262+
if (stackOk.test(inv.items.get(i))) {
263+
inv.setItem(i, replaceWith);
264+
return true;
265+
}
266+
}
267+
}
268+
269+
if (stackOk.test(caster.getItemInHand(InteractionHand.MAIN_HAND))) {
270+
caster.setItemInHand(InteractionHand.MAIN_HAND, replaceWith);
271+
return true;
272+
}
273+
if (stackOk.test(caster.getItemInHand(InteractionHand.OFF_HAND))) {
274+
caster.setItemInHand(InteractionHand.OFF_HAND, replaceWith);
275+
return true;
276+
}
277+
278+
return false;
181279
}
182280

183281
@Override

0 commit comments

Comments
 (0)