Skip to content

Commit 2943978

Browse files
committed
chore: Get first curios item
1 parent e3b105e commit 2943978

File tree

3 files changed

+44
-13
lines changed

3 files changed

+44
-13
lines changed

src/main/java/dev/tonimatas/mekanismcurios/MekanismCurios.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import com.mojang.logging.LogUtils;
44
import dev.tonimatas.mekanismcurios.bridge.PlayerBridge;
55
import dev.tonimatas.mekanismcurios.networking.OpenPortableQIOPacket;
6+
import dev.tonimatas.mekanismcurios.util.CuriosSlots;
67
import mekanism.common.registries.MekanismItems;
78
import net.minecraft.server.level.ServerPlayer;
89
import net.minecraft.world.InteractionHand;
910
import net.minecraft.world.entity.player.Player;
11+
import net.minecraft.world.item.Item;
1012
import net.minecraft.world.item.ItemStack;
1113
import net.neoforged.bus.api.IEventBus;
1214
import net.neoforged.bus.api.SubscribeEvent;
@@ -18,6 +20,11 @@
1820
import org.slf4j.Logger;
1921
import top.theillusivec4.curios.api.CuriosApi;
2022
import top.theillusivec4.curios.api.CuriosCapability;
23+
import top.theillusivec4.curios.api.SlotContext;
24+
import top.theillusivec4.curios.api.SlotResult;
25+
import top.theillusivec4.curios.api.type.capability.ICuriosItemHandler;
26+
27+
import java.util.Optional;
2128

2229
@Mod(MekanismCurios.MODID)
2330
public class MekanismCurios {
@@ -41,18 +48,18 @@ public void registerNetworking(final RegisterPayloadHandlersEvent event) {
4148
}
4249

4350
public static ItemStack getSlot(Player player) {
44-
String slot = ((PlayerBridge) player).mci$getSlot().id();
45-
46-
return CuriosApi.getCuriosInventory(player).map(iCuriosItemHandler ->
47-
iCuriosItemHandler.getCurios().get(slot).getStacks().getStackInSlot(0)).orElse(ItemStack.EMPTY);
51+
return ((PlayerBridge) player).mci$getSlot().getItemStack(player);
4852
}
4953

5054
public static void setSlot(Player player, ItemStack stack) {
5155
if (player instanceof ServerPlayer) {
52-
String slot = ((PlayerBridge) player).mci$getSlot().id();
56+
CuriosSlots slot = ((PlayerBridge) player).mci$getSlot();
57+
SlotContext slotContext = getFirstCurios(player, slot.getItem());
58+
59+
if (slotContext == null) return;
5360

5461
CuriosApi.getCuriosInventory(player).ifPresent(curiosInventory ->
55-
curiosInventory.setEquippedCurio(slot, 0, stack));
62+
curiosInventory.setEquippedCurio(slotContext.identifier(), slotContext.index(), stack));
5663
}
5764
}
5865

@@ -63,4 +70,15 @@ public static ItemStack getHandOrCuriosItem(Player player, InteractionHand hand)
6370
return player.getItemInHand(hand);
6471
}
6572
}
73+
74+
public static SlotResult getCuriosSlot(Player player, SlotContext slotContext) {
75+
Optional<ICuriosItemHandler> curiosInventory = CuriosApi.getCuriosInventory(player);
76+
return curiosInventory.flatMap(iCuriosItemHandler -> iCuriosItemHandler.findCurio(slotContext.identifier(), slotContext.index())).orElse(null);
77+
}
78+
79+
public static SlotContext getFirstCurios(Player player, Item item) {
80+
Optional<ICuriosItemHandler> curiosInventory = CuriosApi.getCuriosInventory(player);
81+
Optional<SlotResult> firstCurios = curiosInventory.flatMap(iCuriosItemHandler -> iCuriosItemHandler.findFirstCurio(item));
82+
return firstCurios.map(SlotResult::slotContext).orElse(null);
83+
}
6684
}

src/main/java/dev/tonimatas/mekanismcurios/networking/OpenPortableQIOPacket.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
import org.jetbrains.annotations.NotNull;
1717

1818
public record OpenPortableQIOPacket(CuriosSlots slot) implements CustomPacketPayload {
19-
public static final CustomPacketPayload.Type<OpenPortableQIOPacket> TYPE =
20-
new CustomPacketPayload.Type<>(ResourceLocation.fromNamespaceAndPath(MekanismCurios.MODID, "portable_qio_packet"));
19+
public static final Type<OpenPortableQIOPacket> TYPE =
20+
new Type<>(ResourceLocation.fromNamespaceAndPath(MekanismCurios.MODID, "portable_qio_packet"));
2121

2222
public static final StreamCodec<FriendlyByteBuf, OpenPortableQIOPacket> STREAM_CODEC = StreamCodec.composite(
2323
CuriosSlots.CURIOS_SLOT_STREAM_CODEC, OpenPortableQIOPacket::slot,
@@ -37,7 +37,7 @@ public static void handle(final OpenPortableQIOPacket packet, final IPayloadCont
3737

3838
((PlayerBridge) player).mci$setSlot(packet.slot);
3939

40-
ItemStack stack = MekanismCurios.getHandOrCuriosItem(player, null);
40+
ItemStack stack = packet.slot.getItemStack(player);
4141

4242
if (!stack.isEmpty()) {
4343
if (stack.getItem() instanceof IGuiItem item) {

src/main/java/dev/tonimatas/mekanismcurios/util/CuriosSlots.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
package dev.tonimatas.mekanismcurios.util;
22

3+
import dev.tonimatas.mekanismcurios.MekanismCurios;
34
import io.netty.buffer.ByteBuf;
45
import mekanism.common.network.PacketUtils;
6+
import mekanism.common.registries.MekanismItems;
57
import net.minecraft.network.codec.StreamCodec;
6-
7-
import java.util.Locale;
8+
import net.minecraft.world.entity.player.Player;
9+
import net.minecraft.world.item.Item;
10+
import net.minecraft.world.item.ItemStack;
11+
import top.theillusivec4.curios.api.SlotContext;
812

913
public enum CuriosSlots {
1014
QIO,
1115
TELEPORTER;
1216

13-
public String id() {
14-
return this.toString().toLowerCase(Locale.ENGLISH);
17+
public Item getItem() {
18+
return switch (this) {
19+
case QIO -> MekanismItems.PORTABLE_QIO_DASHBOARD.get();
20+
case TELEPORTER -> MekanismItems.PORTABLE_TELEPORTER.get();
21+
};
22+
}
23+
24+
public ItemStack getItemStack(Player player) {
25+
SlotContext slotContext = MekanismCurios.getFirstCurios(player, getItem());
26+
if (slotContext == null) return ItemStack.EMPTY;
27+
return MekanismCurios.getCuriosSlot(player, slotContext).stack();
1528
}
1629

1730
public static final StreamCodec<ByteBuf, CuriosSlots> CURIOS_SLOT_STREAM_CODEC = PacketUtils.enumCodec(CuriosSlots.class);

0 commit comments

Comments
 (0)