aboutsummaryrefslogtreecommitdiff
path: root/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsNetwork.java
diff options
context:
space:
mode:
authorshedaniel <daniel@shedaniel.me>2023-08-28 13:16:57 +0800
committershedaniel <daniel@shedaniel.me>2023-09-01 19:48:57 +0800
commit6a8bc6a8c34af1e3ff15fe8a802ef5ece3c417d2 (patch)
treea41c145c1273ab063c725bf44c1f39ca6e0bd5bd /runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsNetwork.java
parentf8bea2079764219f68070be9ae45ffd8d517de5d (diff)
downloadRoughlyEnoughItems-6a8bc6a8c34af1e3ff15fe8a802ef5ece3c417d2.tar.gz
RoughlyEnoughItems-6a8bc6a8c34af1e3ff15fe8a802ef5ece3c417d2.tar.bz2
RoughlyEnoughItems-6a8bc6a8c34af1e3ff15fe8a802ef5ece3c417d2.zip
Reworked the transfer api
See https://www.craft.me/s/TVL01jO3OZarPE for the documentation of the new experimental simple transfer handle
Diffstat (limited to 'runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsNetwork.java')
-rw-r--r--runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsNetwork.java63
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 64a433dbe..5e8e49475 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;
}
}