diff options
Diffstat (limited to 'runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsNetwork.java')
| -rw-r--r-- | runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsNetwork.java | 63 |
1 files changed, 62 insertions, 1 deletions
diff --git a/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsNetwork.java b/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsNetwork.java index 1844edf1e..821dc8e82 100644 --- a/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsNetwork.java +++ b/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsNetwork.java @@ -28,20 +28,35 @@ import dev.architectury.networking.transformers.SplitPacketTransformer; import io.netty.buffer.Unpooled; import me.shedaniel.rei.api.common.category.CategoryIdentifier; import me.shedaniel.rei.api.common.display.Display; +import me.shedaniel.rei.api.common.entry.EntryIngredient; +import me.shedaniel.rei.api.common.entry.EntryStack; +import me.shedaniel.rei.api.common.entry.InputIngredient; +import me.shedaniel.rei.api.common.entry.type.VanillaEntryTypes; +import me.shedaniel.rei.api.common.transfer.info.stack.SlotAccessor; +import me.shedaniel.rei.api.common.transfer.info.stack.SlotAccessorRegistry; import me.shedaniel.rei.impl.common.transfer.InputSlotCrafter; +import me.shedaniel.rei.impl.common.transfer.LegacyInputSlotCrafter; +import me.shedaniel.rei.impl.common.transfer.NewInputSlotCrafter; import net.minecraft.ChatFormatting; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.ListTag; +import net.minecraft.nbt.Tag; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerPlayer; import net.minecraft.util.Mth; import net.minecraft.world.Container; +import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.inventory.InventoryMenu; import net.minecraft.world.inventory.RecipeBookMenu; import net.minecraft.world.item.ItemStack; +import java.util.ArrayList; import java.util.Collections; +import java.util.List; +import java.util.Map; public class RoughlyEnoughItemsNetwork { public static final ResourceLocation DELETE_ITEMS_PACKET = new ResourceLocation("roughlyenoughitems", "delete_item"); @@ -50,6 +65,7 @@ public class RoughlyEnoughItemsNetwork { public static final ResourceLocation CREATE_ITEMS_GRAB_PACKET = new ResourceLocation("roughlyenoughitems", "create_item_grab"); public static final ResourceLocation CREATE_ITEMS_MESSAGE_PACKET = new ResourceLocation("roughlyenoughitems", "ci_msg"); public static final ResourceLocation MOVE_ITEMS_PACKET = new ResourceLocation("roughlyenoughitems", "move_items"); + public static final ResourceLocation MOVE_ITEMS_NEW_PACKET = new ResourceLocation("roughlyenoughitems", "move_items_new"); public static final ResourceLocation NOT_ENOUGH_ITEMS_PACKET = new ResourceLocation("roughlyenoughitems", "og_not_enough"); public static void onInitialize() { @@ -122,7 +138,7 @@ public class RoughlyEnoughItemsNetwork { try { boolean shift = packetByteBuf.readBoolean(); try { - InputSlotCrafter<AbstractContainerMenu, Container, Display> crafter = InputSlotCrafter.start(category, container, player, packetByteBuf.readAnySizeNbt(), shift); + LegacyInputSlotCrafter<AbstractContainerMenu, Container, Display> crafter = LegacyInputSlotCrafter.start(category, container, player, packetByteBuf.readAnySizeNbt(), shift); } catch (InputSlotCrafter.NotEnoughMaterialsException e) { if (!(container instanceof RecipeBookMenu)) { return; @@ -147,5 +163,50 @@ public class RoughlyEnoughItemsNetwork { e.printStackTrace(); } }); + NetworkManager.registerReceiver(NetworkManager.c2s(), MOVE_ITEMS_NEW_PACKET, Collections.singletonList(new SplitPacketTransformer()), (packetByteBuf, context) -> { + ServerPlayer player = (ServerPlayer) context.getPlayer(); + CategoryIdentifier<Display> category = CategoryIdentifier.of(packetByteBuf.readResourceLocation()); + AbstractContainerMenu container = player.containerMenu; + InventoryMenu playerContainer = player.inventoryMenu; + try { + boolean shift = packetByteBuf.readBoolean(); + try { + CompoundTag nbt = packetByteBuf.readAnySizeNbt(); + List<InputIngredient<ItemStack>> inputs = readInputs(nbt.getCompound("Inputs")); + List<SlotAccessor> input = readSlots(container, player, nbt.getList("InputSlots", Tag.TAG_COMPOUND)); + List<SlotAccessor> inventory = readSlots(container, player, nbt.getList("InventorySlots", Tag.TAG_COMPOUND)); + NewInputSlotCrafter<AbstractContainerMenu, Container> crafter = new NewInputSlotCrafter<>(container, input, inventory, inputs); + crafter.fillInputSlots(player, shift); + } catch (InputSlotCrafter.NotEnoughMaterialsException e) { + if (!(container instanceof RecipeBookMenu)) { + return; + } + } catch (IllegalStateException e) { + player.sendSystemMessage(Component.translatable(e.getMessage()).withStyle(ChatFormatting.RED)); + } catch (Exception e) { + player.sendSystemMessage(Component.translatable("error.rei.internal.error", e.getMessage()).withStyle(ChatFormatting.RED)); + e.printStackTrace(); + } + } catch (Exception e) { + e.printStackTrace(); + } + }); + } + + private static List<SlotAccessor> readSlots(AbstractContainerMenu menu, Player player, ListTag tag) { + List<SlotAccessor> slots = new ArrayList<>(); + for (Tag t : tag) { + slots.add(SlotAccessorRegistry.getInstance().read(menu, player, (CompoundTag) t)); + } + return slots; + } + + private static List<InputIngredient<ItemStack>> readInputs(CompoundTag tag) { + List<InputIngredient<ItemStack>> inputs = new ArrayList<>(); + for (Map.Entry<String, Tag> entry : tag.tags.entrySet()) { + InputIngredient<EntryStack<?>> stacks = InputIngredient.of(Integer.parseInt(entry.getKey()), EntryIngredient.read((ListTag) entry.getValue())); + inputs.add(InputIngredient.withType(stacks, VanillaEntryTypes.ITEM)); + } + return inputs; } } |
