fixed everything but codecs?

This commit is contained in:
Ell 2024-03-07 16:00:49 +01:00
parent 75cd73470f
commit 1ba1257569
21 changed files with 233 additions and 286 deletions

View file

@ -1,18 +1,18 @@
package de.ellpeck.prettypipes; package de.ellpeck.prettypipes;
import net.neoforged.api.distmarker.Dist; import net.neoforged.api.distmarker.Dist;
import net.neoforged.fml.DistExecutor; import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.common.Mod; import net.neoforged.fml.common.Mod;
import net.neoforged.fml.javafmlmod.FMLJavaModLoadingContext; import net.neoforged.fml.loading.FMLEnvironment;
@Mod(PrettyPipes.ID) @Mod(PrettyPipes.ID)
public final class PrettyPipes { public final class PrettyPipes {
public static final String ID = "prettypipes"; public static final String ID = "prettypipes";
public PrettyPipes() { public PrettyPipes(IEventBus eventBus) {
var bus = FMLJavaModLoadingContext.get().getModEventBus(); if (FMLEnvironment.dist == Dist.CLIENT)
bus.addListener(Registry::setup); eventBus.addListener(Registry.Client::setup);
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> bus.addListener(Registry.Client::setup));
} }
} }

View file

@ -5,8 +5,6 @@ import de.ellpeck.prettypipes.entities.PipeFrameRenderer;
import de.ellpeck.prettypipes.items.*; import de.ellpeck.prettypipes.items.*;
import de.ellpeck.prettypipes.misc.ItemEquality; import de.ellpeck.prettypipes.misc.ItemEquality;
import de.ellpeck.prettypipes.misc.ModuleClearingRecipe; import de.ellpeck.prettypipes.misc.ModuleClearingRecipe;
import de.ellpeck.prettypipes.network.PipeNetwork;
import de.ellpeck.prettypipes.packets.PacketHandler;
import de.ellpeck.prettypipes.pipe.IPipeConnectable; import de.ellpeck.prettypipes.pipe.IPipeConnectable;
import de.ellpeck.prettypipes.pipe.PipeBlock; import de.ellpeck.prettypipes.pipe.PipeBlock;
import de.ellpeck.prettypipes.pipe.PipeBlockEntity; import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
@ -65,12 +63,11 @@ import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.entity.BlockEntityType;
import net.neoforged.neoforge.capabilities.BlockCapability;
import net.neoforged.neoforge.common.extensions.IMenuTypeExtension;
import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.Mod; import net.neoforged.fml.common.Mod;
import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent; import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent;
import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent; import net.neoforged.neoforge.capabilities.BlockCapability;
import net.neoforged.neoforge.common.extensions.IMenuTypeExtension;
import net.neoforged.neoforge.registries.RegisterEvent; import net.neoforged.neoforge.registries.RegisterEvent;
import java.util.Comparator; import java.util.Comparator;
@ -210,10 +207,6 @@ public final class Registry {
helper.register(new ResourceLocation(PrettyPipes.ID, tier.name().toLowerCase(Locale.ROOT) + "_" + name), item.apply(name, tier)); helper.register(new ResourceLocation(PrettyPipes.ID, tier.name().toLowerCase(Locale.ROOT) + "_" + name), item.apply(name, tier));
} }
public static void setup(FMLCommonSetupEvent event) {
PacketHandler.setup();
}
public static final class Client { public static final class Client {
public static void setup(FMLClientSetupEvent event) { public static void setup(FMLClientSetupEvent event) {

View file

@ -3,7 +3,6 @@ package de.ellpeck.prettypipes.compat.jei;
import de.ellpeck.prettypipes.Registry; import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.misc.ItemEquality; import de.ellpeck.prettypipes.misc.ItemEquality;
import de.ellpeck.prettypipes.packets.PacketCraftingModuleTransfer; import de.ellpeck.prettypipes.packets.PacketCraftingModuleTransfer;
import de.ellpeck.prettypipes.packets.PacketHandler;
import de.ellpeck.prettypipes.pipe.modules.craft.CraftingModuleContainer; import de.ellpeck.prettypipes.pipe.modules.craft.CraftingModuleContainer;
import mezz.jei.api.constants.RecipeTypes; import mezz.jei.api.constants.RecipeTypes;
import mezz.jei.api.constants.VanillaTypes; import mezz.jei.api.constants.VanillaTypes;
@ -17,6 +16,7 @@ import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.CraftingRecipe; import net.minecraft.world.item.crafting.CraftingRecipe;
import net.minecraft.world.item.crafting.RecipeHolder; import net.minecraft.world.item.crafting.RecipeHolder;
import net.neoforged.neoforge.network.PacketDistributor;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
@ -63,7 +63,7 @@ public class CraftingModuleTransferHandler implements IRecipeTransferHandler<Cra
if (!remain.isEmpty()) if (!remain.isEmpty())
toAdd.add(remain); toAdd.add(remain);
} }
PacketHandler.sendToServer(new PacketCraftingModuleTransfer(inputs, outputs)); PacketDistributor.SERVER.noArg().send(new PacketCraftingModuleTransfer(inputs, outputs));
return null; return null;
} }

View file

@ -2,7 +2,6 @@ package de.ellpeck.prettypipes.compat.jei;
import de.ellpeck.prettypipes.Registry; import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.packets.PacketGhostSlot; import de.ellpeck.prettypipes.packets.PacketGhostSlot;
import de.ellpeck.prettypipes.packets.PacketHandler;
import de.ellpeck.prettypipes.terminal.containers.CraftingTerminalContainer; import de.ellpeck.prettypipes.terminal.containers.CraftingTerminalContainer;
import mezz.jei.api.constants.RecipeTypes; import mezz.jei.api.constants.RecipeTypes;
import mezz.jei.api.constants.VanillaTypes; import mezz.jei.api.constants.VanillaTypes;
@ -15,6 +14,7 @@ import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.MenuType; import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.item.crafting.CraftingRecipe; import net.minecraft.world.item.crafting.CraftingRecipe;
import net.minecraft.world.item.crafting.RecipeHolder; import net.minecraft.world.item.crafting.RecipeHolder;
import net.neoforged.neoforge.network.PacketDistributor;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
@ -47,7 +47,7 @@ public class CraftingTerminalTransferHandler implements IRecipeTransferHandler<C
var ingredients = slots.getSlotViews(RecipeIngredientRole.INPUT); var ingredients = slots.getSlotViews(RecipeIngredientRole.INPUT);
for (var entry : ingredients) for (var entry : ingredients)
stacks.add(new PacketGhostSlot.Entry(player.level(), entry.getIngredients(VanillaTypes.ITEM_STACK).collect(Collectors.toList()))); stacks.add(new PacketGhostSlot.Entry(player.level(), entry.getIngredients(VanillaTypes.ITEM_STACK).collect(Collectors.toList())));
PacketHandler.sendToServer(new PacketGhostSlot(container.getTile().getBlockPos(), stacks)); PacketDistributor.SERVER.noArg().send(new PacketGhostSlot(container.getTile().getBlockPos(), stacks));
return null; return null;
} }

View file

@ -3,15 +3,15 @@ package de.ellpeck.prettypipes.misc;
import de.ellpeck.prettypipes.PrettyPipes; import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.Registry; import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.network.PipeNetwork; import de.ellpeck.prettypipes.network.PipeNetwork;
import de.ellpeck.prettypipes.packets.*;
import net.minecraft.commands.Commands; import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation; import net.neoforged.bus.api.SubscribeEvent;
import net.minecraft.world.level.Level; import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.capabilities.Capabilities; import net.neoforged.neoforge.capabilities.Capabilities;
import net.neoforged.neoforge.capabilities.RegisterCapabilitiesEvent; import net.neoforged.neoforge.capabilities.RegisterCapabilitiesEvent;
import net.neoforged.neoforge.event.server.ServerStartingEvent; import net.neoforged.neoforge.event.server.ServerStartingEvent;
import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.neoforge.network.event.RegisterPayloadHandlerEvent;
import net.neoforged.fml.common.Mod;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -61,4 +61,15 @@ public final class Events {
}))); })));
} }
@SubscribeEvent
public static void onPayloadRegister(final RegisterPayloadHandlerEvent event) {
var registrar = event.registrar(PrettyPipes.ID);
registrar.play(PacketItemEnterPipe.ID, PacketItemEnterPipe::new, PacketItemEnterPipe::onMessage);
registrar.play(PacketButton.ID, PacketButton::new, PacketButton::onMessage);
registrar.play(PacketCraftingModuleTransfer.ID, PacketCraftingModuleTransfer::new, PacketCraftingModuleTransfer::onMessage);
registrar.play(PacketGhostSlot.ID, PacketGhostSlot::new, PacketGhostSlot::onMessage);
registrar.play(PacketNetworkItems.ID, PacketNetworkItems::new, PacketNetworkItems::onMessage);
registrar.play(PacketRequest.ID, PacketRequest::new, PacketRequest::onMessage);
}
} }

View file

@ -6,7 +6,6 @@ import com.google.common.collect.Streams;
import de.ellpeck.prettypipes.PrettyPipes; import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.misc.ItemEquality; import de.ellpeck.prettypipes.misc.ItemEquality;
import de.ellpeck.prettypipes.packets.PacketHandler;
import de.ellpeck.prettypipes.packets.PacketItemEnterPipe; import de.ellpeck.prettypipes.packets.PacketItemEnterPipe;
import de.ellpeck.prettypipes.pipe.IPipeItem; import de.ellpeck.prettypipes.pipe.IPipeItem;
import de.ellpeck.prettypipes.pipe.PipeBlock; import de.ellpeck.prettypipes.pipe.PipeBlock;
@ -22,6 +21,7 @@ import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.saveddata.SavedData; import net.minecraft.world.level.saveddata.SavedData;
import net.neoforged.neoforge.network.PacketDistributor;
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;
import org.jgrapht.ListenableGraph; import org.jgrapht.ListenableGraph;
import org.jgrapht.alg.shortestpath.DijkstraShortestPath; import org.jgrapht.alg.shortestpath.DijkstraShortestPath;
@ -206,7 +206,7 @@ public class PipeNetwork extends SavedData implements GraphListener<BlockPos, Ne
var item = itemSupplier.apply(startPipe.getItemSpeed(stack)); var item = itemSupplier.apply(startPipe.getItemSpeed(stack));
item.setDestination(startInventory, destInventory, path); item.setDestination(startInventory, destInventory, path);
startPipe.addNewItem(item); startPipe.addNewItem(item);
PacketHandler.sendToAllLoaded(this.level, startPipePos, new PacketItemEnterPipe(startPipePos, item)); PacketDistributor.TRACKING_CHUNK.with(this.level.getChunkAt(startPipePos)).send(new PacketItemEnterPipe(startPipePos, item));
return true; return true;
} }

View file

@ -1,5 +1,6 @@
package de.ellpeck.prettypipes.packets; package de.ellpeck.prettypipes.packets;
import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.items.IModule; import de.ellpeck.prettypipes.items.IModule;
import de.ellpeck.prettypipes.misc.ItemFilter.IFilteredContainer; import de.ellpeck.prettypipes.misc.ItemFilter.IFilteredContainer;
@ -14,23 +15,27 @@ import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer; import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.MenuProvider; import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.inventory.AbstractContainerMenu;
import net.neoforged.neoforge.network.PacketDistributor;
import net.neoforged.neoforge.network.handling.PlayPayloadContext;
import org.apache.logging.log4j.util.TriConsumer; import org.apache.logging.log4j.util.TriConsumer;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.function.Supplier;
import static de.ellpeck.prettypipes.misc.DirectionSelector.IDirectionContainer; import static de.ellpeck.prettypipes.misc.DirectionSelector.IDirectionContainer;
public class PacketButton { public class PacketButton implements CustomPacketPayload {
private BlockPos pos; public static final ResourceLocation ID = new ResourceLocation(PrettyPipes.ID, "button");
private ButtonResult result;
private int[] data; private final BlockPos pos;
private final ButtonResult result;
private final int[] data;
public PacketButton(BlockPos pos, ButtonResult result, int... data) { public PacketButton(BlockPos pos, ButtonResult result, int... data) {
this.pos = pos; this.pos = pos;
@ -38,38 +43,33 @@ public class PacketButton {
this.data = data; this.data = data;
} }
private PacketButton() { public PacketButton(FriendlyByteBuf buf) {
this.pos = buf.readBlockPos();
this.result = ButtonResult.values()[buf.readByte()];
this.data = buf.readVarIntArray();
} }
public static PacketButton fromBytes(FriendlyByteBuf buf) { @Override
var packet = new PacketButton(); public void write(FriendlyByteBuf buf) {
packet.pos = buf.readBlockPos(); buf.writeBlockPos(this.pos);
packet.result = ButtonResult.values()[buf.readByte()]; buf.writeByte(this.result.ordinal());
packet.data = buf.readVarIntArray(); buf.writeVarIntArray(this.data);
return packet;
} }
public static void toBytes(PacketButton packet, FriendlyByteBuf buf) { @Override
buf.writeBlockPos(packet.pos); public ResourceLocation id() {
buf.writeByte(packet.result.ordinal()); return PacketButton.ID;
buf.writeVarIntArray(packet.data);
} }
@SuppressWarnings("Convert2Lambda") public static void onMessage(PacketButton message, PlayPayloadContext ctx) {
public static void onMessage(PacketButton message, Supplier<NetworkEvent.Context> ctx) { ctx.workHandler().execute(() -> {
ctx.get().enqueueWork(new Runnable() { var player = ctx.player().orElseThrow();
@Override message.result.action.accept(message.pos, message.data, player);
public void run() {
Player player = ctx.get().getSender();
message.result.action.accept(message.pos, message.data, player);
}
}); });
ctx.get().setPacketHandled(true);
} }
public static void sendAndExecute(BlockPos pos, ButtonResult result, int... data) { public static void sendAndExecute(BlockPos pos, ButtonResult result, int... data) {
PacketHandler.sendToServer(new PacketButton(pos, result, data)); PacketDistributor.SERVER.noArg().send(new PacketButton(pos, result, data));
result.action.accept(pos, data, Minecraft.getInstance().player); result.action.accept(pos, data, Minecraft.getInstance().player);
} }
@ -77,10 +77,10 @@ public class PacketButton {
PIPE_TAB((pos, data, player) -> { PIPE_TAB((pos, data, player) -> {
var tile = Utility.getBlockEntity(PipeBlockEntity.class, player.level(), pos); var tile = Utility.getBlockEntity(PipeBlockEntity.class, player.level(), pos);
if (data[0] < 0) { if (data[0] < 0) {
NetworkHooks.openScreen((ServerPlayer) player, tile, pos); player.openMenu(tile, pos);
} else { } else {
var stack = tile.modules.getStackInSlot(data[0]); var stack = tile.modules.getStackInSlot(data[0]);
NetworkHooks.openScreen((ServerPlayer) player, new MenuProvider() { player.openMenu(new MenuProvider() {
@Override @Override
public Component getDisplayName() { public Component getDisplayName() {
return stack.getHoverName(); return stack.getHoverName();
@ -133,4 +133,5 @@ public class PacketButton {
this.action = action; this.action = action;
} }
} }
} }

View file

@ -1,66 +1,64 @@
package de.ellpeck.prettypipes.packets; package de.ellpeck.prettypipes.packets;
import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.pipe.modules.craft.CraftingModuleContainer; import de.ellpeck.prettypipes.pipe.modules.craft.CraftingModuleContainer;
import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.player.Player; import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.items.ItemHandlerHelper; import net.neoforged.neoforge.items.ItemHandlerHelper;
import net.neoforged.neoforge.items.ItemStackHandler; import net.neoforged.neoforge.items.ItemStackHandler;
import net.neoforged.neoforge.network.NetworkEvent; import net.neoforged.neoforge.network.handling.PlayPayloadContext;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.function.Supplier;
public class PacketCraftingModuleTransfer { public class PacketCraftingModuleTransfer implements CustomPacketPayload {
private List<ItemStack> inputs; public static final ResourceLocation ID = new ResourceLocation(PrettyPipes.ID, "crafting_module_transfer");
private List<ItemStack> outputs;
private final List<ItemStack> inputs;
private final List<ItemStack> outputs;
public PacketCraftingModuleTransfer(List<ItemStack> inputs, List<ItemStack> outputs) { public PacketCraftingModuleTransfer(List<ItemStack> inputs, List<ItemStack> outputs) {
this.inputs = inputs; this.inputs = inputs;
this.outputs = outputs; this.outputs = outputs;
} }
private PacketCraftingModuleTransfer() { public PacketCraftingModuleTransfer(FriendlyByteBuf buf) {
this.inputs = new ArrayList<>();
for (var i = buf.readInt(); i > 0; i--)
this.inputs.add(buf.readItem());
this.outputs = new ArrayList<>();
for (var i = buf.readInt(); i > 0; i--)
this.outputs.add(buf.readItem());
} }
public static PacketCraftingModuleTransfer fromBytes(FriendlyByteBuf buf) { @Override
var packet = new PacketCraftingModuleTransfer(); public void write(FriendlyByteBuf buf) {
packet.inputs = new ArrayList<>(); buf.writeInt(this.inputs.size());
for (var i = buf.readInt(); i > 0; i--) for (var stack : this.inputs)
packet.inputs.add(buf.readItem());
packet.outputs = new ArrayList<>();
for (var i = buf.readInt(); i > 0; i--)
packet.outputs.add(buf.readItem());
return packet;
}
public static void toBytes(PacketCraftingModuleTransfer packet, FriendlyByteBuf buf) {
buf.writeInt(packet.inputs.size());
for (var stack : packet.inputs)
buf.writeItem(stack); buf.writeItem(stack);
buf.writeInt(packet.outputs.size()); buf.writeInt(this.outputs.size());
for (var stack : packet.outputs) for (var stack : this.outputs)
buf.writeItem(stack); buf.writeItem(stack);
} }
@SuppressWarnings("Convert2Lambda") @Override
public static void onMessage(PacketCraftingModuleTransfer message, Supplier<NetworkEvent.Context> ctx) { public ResourceLocation id() {
ctx.get().enqueueWork(new Runnable() { return PacketCraftingModuleTransfer.ID;
@Override }
public void run() {
Player player = ctx.get().getSender(); public static void onMessage(PacketCraftingModuleTransfer message, PlayPayloadContext ctx) {
if (player.containerMenu instanceof CraftingModuleContainer container) { ctx.workHandler().execute(() -> {
PacketCraftingModuleTransfer.copy(container.input, message.inputs); var player = ctx.player().orElseThrow();
PacketCraftingModuleTransfer.copy(container.output, message.outputs); if (player.containerMenu instanceof CraftingModuleContainer container) {
container.modified = true; PacketCraftingModuleTransfer.copy(container.input, message.inputs);
container.broadcastChanges(); PacketCraftingModuleTransfer.copy(container.output, message.outputs);
} container.modified = true;
container.broadcastChanges();
} }
}); });
ctx.get().setPacketHandled(true);
} }
private static void copy(ItemStackHandler container, List<ItemStack> contents) { private static void copy(ItemStackHandler container, List<ItemStack> contents) {
@ -69,4 +67,5 @@ public class PacketCraftingModuleTransfer {
for (var stack : contents) for (var stack : contents)
ItemHandlerHelper.insertItem(container, stack, false); ItemHandlerHelper.insertItem(container, stack, false);
} }
} }

View file

@ -2,86 +2,62 @@ package de.ellpeck.prettypipes.packets;
import com.google.common.collect.Streams; import com.google.common.collect.Streams;
import com.mojang.datafixers.util.Pair; import com.mojang.datafixers.util.Pair;
import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.terminal.CraftingTerminalBlockEntity; import de.ellpeck.prettypipes.terminal.CraftingTerminalBlockEntity;
import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.registries.Registries; import net.minecraft.core.registries.Registries;
import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey; import net.minecraft.tags.TagKey;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item; import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items; import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.neoforged.neoforge.network.NetworkEvent; import net.neoforged.neoforge.network.handling.PlayPayloadContext;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class PacketGhostSlot { public class PacketGhostSlot implements CustomPacketPayload {
private BlockPos pos; public static final ResourceLocation ID = new ResourceLocation(PrettyPipes.ID, "ghost_slot");
private List<Entry> stacks;
private final BlockPos pos;
private final List<Entry> stacks;
public PacketGhostSlot(BlockPos pos, List<Entry> stacks) { public PacketGhostSlot(BlockPos pos, List<Entry> stacks) {
this.pos = pos; this.pos = pos;
this.stacks = stacks; this.stacks = stacks;
} }
private PacketGhostSlot() { public PacketGhostSlot(FriendlyByteBuf buf) {
this.pos = buf.readBlockPos();
} this.stacks = new ArrayList<>();
public static PacketGhostSlot fromBytes(FriendlyByteBuf buf) {
var packet = new PacketGhostSlot();
packet.pos = buf.readBlockPos();
packet.stacks = new ArrayList<>();
for (var i = buf.readInt(); i > 0; i--) for (var i = buf.readInt(); i > 0; i--)
packet.stacks.add(new Entry(buf)); this.stacks.add(new Entry(buf));
return packet;
} }
public static void toBytes(PacketGhostSlot packet, FriendlyByteBuf buf) { @Override
buf.writeBlockPos(packet.pos); public void write(FriendlyByteBuf buf) {
buf.writeInt(packet.stacks.size()); buf.writeBlockPos(this.pos);
for (var entry : packet.stacks) buf.writeInt(this.stacks.size());
for (var entry : this.stacks)
entry.write(buf); entry.write(buf);
} }
@SuppressWarnings("Convert2Lambda") @Override
public static void onMessage(PacketGhostSlot message, Supplier<NetworkEvent.Context> ctx) { public ResourceLocation id() {
var doIt = (Consumer<Player>) p -> { return PacketGhostSlot.ID;
var tile = Utility.getBlockEntity(CraftingTerminalBlockEntity.class, p.level(), message.pos); }
if (tile != null)
tile.setGhostItems(message.stacks);
};
// this whole thing is a dirty hack for allowing the same packet to be used public static void onMessage(PacketGhostSlot message, PlayPayloadContext ctx) {
// both client -> server and server -> client without any classloading issues var player = ctx.player().orElseThrow();
Player player = ctx.get().getSender(); var tile = Utility.getBlockEntity(CraftingTerminalBlockEntity.class, player.level(), message.pos);
// are we on the client? if (tile != null)
if (player == null) { tile.setGhostItems(message.stacks);
ctx.get().enqueueWork(new Runnable() {
@Override
public void run() {
doIt.accept(Minecraft.getInstance().player);
}
});
} else {
ctx.get().enqueueWork(new Runnable() {
@Override
public void run() {
doIt.accept(player);
}
});
}
ctx.get().setPacketHandled(true);
} }
public static class Entry { public static class Entry {
@ -115,12 +91,12 @@ public class PacketGhostSlot {
public List<ItemStack> getStacks(Level level) { public List<ItemStack> getStacks(Level level) {
if (this.stacks != null) if (this.stacks != null)
return this.stacks; return this.stacks;
return Streams.stream(level.registryAccess().registry(Registries.ITEM).get().getTagOrEmpty(this.tag).iterator()) return Streams.stream(level.registryAccess().registry(Registries.ITEM).orElseThrow().getTagOrEmpty(this.tag).iterator())
.filter(h -> h.value() != null & h.value() != Items.AIR) .filter(h -> h.value() != null & h.value() != Items.AIR)
.map(h -> new ItemStack(h.value())).collect(Collectors.toList()); .map(h -> new ItemStack(h.value())).collect(Collectors.toList());
} }
public FriendlyByteBuf write(FriendlyByteBuf buf) { public void write(FriendlyByteBuf buf) {
if (this.stacks != null) { if (this.stacks != null) {
buf.writeBoolean(true); buf.writeBoolean(true);
buf.writeInt(this.stacks.size()); buf.writeInt(this.stacks.size());
@ -130,11 +106,10 @@ public class PacketGhostSlot {
buf.writeBoolean(false); buf.writeBoolean(false);
buf.writeUtf(this.tag.location().toString()); buf.writeUtf(this.tag.location().toString());
} }
return buf;
} }
private static TagKey<Item> getTagForStacks(Level level, List<ItemStack> stacks) { private static TagKey<Item> getTagForStacks(Level level, List<ItemStack> stacks) {
return level.registryAccess().registry(Registries.ITEM).get().getTags().filter(e -> { return level.registryAccess().registry(Registries.ITEM).orElseThrow().getTags().filter(e -> {
var tag = e.getSecond(); var tag = e.getSecond();
if (tag.size() != stacks.size()) if (tag.size() != stacks.size())
return false; return false;
@ -145,5 +120,7 @@ public class PacketGhostSlot {
return true; return true;
}).map(Pair::getFirst).findFirst().orElse(null); }).map(Pair::getFirst).findFirst().orElse(null);
} }
} }
} }

View file

@ -1,39 +0,0 @@
package de.ellpeck.prettypipes.packets;
import de.ellpeck.prettypipes.PrettyPipes;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.neoforged.neoforge.network.PacketDistributor;
import net.neoforged.neoforge.network.simple.SimpleChannel;
import net.neoforged.neoforge.network.NetworkRegistry;
public final class PacketHandler {
private static final String VERSION = "1";
private static SimpleChannel network;
public static void setup() {
PacketHandler.network = NetworkRegistry.newSimpleChannel(new ResourceLocation(PrettyPipes.ID, "network"), () -> PacketHandler.VERSION, PacketHandler.VERSION::equals, PacketHandler.VERSION::equals);
PacketHandler.network.registerMessage(0, PacketItemEnterPipe.class, PacketItemEnterPipe::toBytes, PacketItemEnterPipe::fromBytes, PacketItemEnterPipe::onMessage);
PacketHandler.network.registerMessage(1, PacketButton.class, PacketButton::toBytes, PacketButton::fromBytes, PacketButton::onMessage);
PacketHandler.network.registerMessage(2, PacketNetworkItems.class, PacketNetworkItems::toBytes, PacketNetworkItems::fromBytes, PacketNetworkItems::onMessage);
PacketHandler.network.registerMessage(3, PacketRequest.class, PacketRequest::toBytes, PacketRequest::fromBytes, PacketRequest::onMessage);
PacketHandler.network.registerMessage(4, PacketGhostSlot.class, PacketGhostSlot::toBytes, PacketGhostSlot::fromBytes, PacketGhostSlot::onMessage);
PacketHandler.network.registerMessage(5, PacketCraftingModuleTransfer.class, PacketCraftingModuleTransfer::toBytes, PacketCraftingModuleTransfer::fromBytes, PacketCraftingModuleTransfer::onMessage);
}
public static void sendToAllLoaded(Level world, BlockPos pos, Object message) {
PacketHandler.network.send(PacketDistributor.TRACKING_CHUNK.with(() -> world.getChunkAt(pos)), message);
}
public static void sendTo(Player player, Object message) {
PacketHandler.network.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), message);
}
public static void sendToServer(Object message) {
PacketHandler.network.send(PacketDistributor.SERVER.noArg(), message);
}
}

View file

@ -1,5 +1,6 @@
package de.ellpeck.prettypipes.packets; package de.ellpeck.prettypipes.packets;
import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.pipe.IPipeItem; import de.ellpeck.prettypipes.pipe.IPipeItem;
import de.ellpeck.prettypipes.pipe.PipeBlockEntity; import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
@ -7,50 +8,48 @@ import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.FriendlyByteBuf;
import net.neoforged.neoforge.network.NetworkEvent; import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
import net.neoforged.neoforge.network.handling.PlayPayloadContext;
import java.util.function.Supplier; public class PacketItemEnterPipe implements CustomPacketPayload {
public class PacketItemEnterPipe { public static final ResourceLocation ID = new ResourceLocation(PrettyPipes.ID, "item_enter_pipe");
private BlockPos tilePos; private final BlockPos tilePos;
private CompoundTag item; private final CompoundTag item;
public PacketItemEnterPipe(BlockPos tilePos, IPipeItem item) { public PacketItemEnterPipe(BlockPos tilePos, IPipeItem item) {
this.tilePos = tilePos; this.tilePos = tilePos;
this.item = item.serializeNBT(); this.item = item.serializeNBT();
} }
private PacketItemEnterPipe() { public PacketItemEnterPipe(FriendlyByteBuf buf) {
this.tilePos = buf.readBlockPos();
this.item = buf.readNbt();
} }
public static PacketItemEnterPipe fromBytes(FriendlyByteBuf buf) { @Override
var client = new PacketItemEnterPipe(); public void write(FriendlyByteBuf buf) {
client.tilePos = buf.readBlockPos(); buf.writeBlockPos(this.tilePos);
client.item = buf.readNbt(); buf.writeNbt(this.item);
return client;
} }
public static void toBytes(PacketItemEnterPipe packet, FriendlyByteBuf buf) { @Override
buf.writeBlockPos(packet.tilePos); public ResourceLocation id() {
buf.writeNbt(packet.item); return PacketItemEnterPipe.ID;
} }
@SuppressWarnings("Convert2Lambda") public static void onMessage(PacketItemEnterPipe message, PlayPayloadContext ctx) {
public static void onMessage(PacketItemEnterPipe message, Supplier<NetworkEvent.Context> ctx) { ctx.workHandler().execute(() -> {
ctx.get().enqueueWork(new Runnable() { var mc = Minecraft.getInstance();
@Override if (mc.level == null)
public void run() { return;
var mc = Minecraft.getInstance(); var item = IPipeItem.load(message.item);
if (mc.level == null) var pipe = Utility.getBlockEntity(PipeBlockEntity.class, mc.level, message.tilePos);
return; if (pipe != null)
var item = IPipeItem.load(message.item); pipe.getItems().add(item);
var pipe = Utility.getBlockEntity(PipeBlockEntity.class, mc.level, message.tilePos);
if (pipe != null)
pipe.getItems().add(item);
}
}); });
ctx.get().setPacketHandled(true);
} }
} }

View file

@ -1,20 +1,24 @@
package de.ellpeck.prettypipes.packets; package de.ellpeck.prettypipes.packets;
import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.terminal.containers.ItemTerminalGui; import de.ellpeck.prettypipes.terminal.containers.ItemTerminalGui;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.network.NetworkEvent; import net.neoforged.neoforge.network.handling.PlayPayloadContext;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.function.Supplier;
public class PacketNetworkItems { public class PacketNetworkItems implements CustomPacketPayload {
private List<ItemStack> items; public static final ResourceLocation ID = new ResourceLocation(PrettyPipes.ID, "network_items");
private List<ItemStack> craftables;
private List<ItemStack> currentlyCrafting; private final List<ItemStack> items;
private final List<ItemStack> craftables;
private final List<ItemStack> currentlyCrafting;
public PacketNetworkItems(List<ItemStack> items, List<ItemStack> craftables, List<ItemStack> currentlyCrafting) { public PacketNetworkItems(List<ItemStack> items, List<ItemStack> craftables, List<ItemStack> currentlyCrafting) {
this.items = items; this.items = items;
@ -22,46 +26,46 @@ public class PacketNetworkItems {
this.currentlyCrafting = currentlyCrafting; this.currentlyCrafting = currentlyCrafting;
} }
private PacketNetworkItems() { public PacketNetworkItems(FriendlyByteBuf buf) {
this.items = new ArrayList<>();
}
public static PacketNetworkItems fromBytes(FriendlyByteBuf buf) {
var client = new PacketNetworkItems();
client.items = new ArrayList<>();
for (var i = buf.readVarInt(); i > 0; i--) { for (var i = buf.readVarInt(); i > 0; i--) {
var stack = buf.readItem(); var stack = buf.readItem();
stack.setCount(buf.readVarInt()); stack.setCount(buf.readVarInt());
client.items.add(stack); this.items.add(stack);
} }
client.craftables = new ArrayList<>(); this.craftables = new ArrayList<>();
for (var i = buf.readVarInt(); i > 0; i--) for (var i = buf.readVarInt(); i > 0; i--)
client.craftables.add(buf.readItem()); this.craftables.add(buf.readItem());
client.currentlyCrafting = new ArrayList<>(); this.currentlyCrafting = new ArrayList<>();
for (var i = buf.readVarInt(); i > 0; i--) for (var i = buf.readVarInt(); i > 0; i--)
client.currentlyCrafting.add(buf.readItem()); this.currentlyCrafting.add(buf.readItem());
return client;
} }
public static void toBytes(PacketNetworkItems packet, FriendlyByteBuf buf) { @Override
buf.writeVarInt(packet.items.size()); public void write(FriendlyByteBuf buf) {
for (var stack : packet.items) { buf.writeVarInt(this.items.size());
for (var stack : this.items) {
var copy = stack.copy(); var copy = stack.copy();
copy.setCount(1); copy.setCount(1);
buf.writeItem(copy); buf.writeItem(copy);
buf.writeVarInt(stack.getCount()); buf.writeVarInt(stack.getCount());
} }
buf.writeVarInt(packet.craftables.size()); buf.writeVarInt(this.craftables.size());
for (var stack : packet.craftables) for (var stack : this.craftables)
buf.writeItem(stack); buf.writeItem(stack);
buf.writeVarInt(packet.currentlyCrafting.size()); buf.writeVarInt(this.currentlyCrafting.size());
for (var stack : packet.currentlyCrafting) for (var stack : this.currentlyCrafting)
buf.writeItem(stack); buf.writeItem(stack);
} }
@Override
public ResourceLocation id() {
return PacketNetworkItems.ID;
}
@SuppressWarnings("Convert2Lambda") @SuppressWarnings("Convert2Lambda")
public static void onMessage(PacketNetworkItems message, Supplier<NetworkEvent.Context> ctx) { public static void onMessage(PacketNetworkItems message, PlayPayloadContext ctx) {
ctx.get().enqueueWork(new Runnable() { ctx.workHandler().execute(new Runnable() {
@Override @Override
public void run() { public void run() {
var mc = Minecraft.getInstance(); var mc = Minecraft.getInstance();
@ -69,6 +73,6 @@ public class PacketNetworkItems {
terminal.updateItemList(message.items, message.craftables, message.currentlyCrafting); terminal.updateItemList(message.items, message.craftables, message.currentlyCrafting);
} }
}); });
ctx.get().setPacketHandled(true);
} }
} }

View file

@ -1,61 +1,62 @@
package de.ellpeck.prettypipes.packets; package de.ellpeck.prettypipes.packets;
import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.terminal.ItemTerminalBlockEntity; import de.ellpeck.prettypipes.terminal.ItemTerminalBlockEntity;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.player.Player; import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.network.NetworkEvent; import net.neoforged.neoforge.network.handling.PlayPayloadContext;
import java.util.function.Supplier; public class PacketRequest implements CustomPacketPayload {
public class PacketRequest { public static final ResourceLocation ID = new ResourceLocation(PrettyPipes.ID, "request");
private BlockPos pos; private final BlockPos pos;
private ItemStack stack; private final ItemStack stack;
private int nbtHash; private final int nbtHash;
private int amount; private final int amount;
public PacketRequest(BlockPos pos, ItemStack stack, int amount) { public PacketRequest(BlockPos pos, ItemStack stack, int amount) {
this.pos = pos; this.pos = pos;
this.stack = stack; this.stack = stack;
if (stack.hasTag()) this.nbtHash = stack.hasTag() ? stack.getTag().hashCode() : 0;
this.nbtHash = stack.getTag().hashCode();
this.amount = amount; this.amount = amount;
} }
private PacketRequest() { public PacketRequest(FriendlyByteBuf buf) {
this.pos = buf.readBlockPos();
this.stack = buf.readItem();
this.nbtHash = buf.readVarInt();
this.amount = buf.readVarInt();
} }
public static PacketRequest fromBytes(FriendlyByteBuf buf) { @Override
var packet = new PacketRequest(); public void write(FriendlyByteBuf buf) {
packet.pos = buf.readBlockPos(); buf.writeBlockPos(this.pos);
packet.stack = buf.readItem(); buf.writeItem(this.stack);
packet.nbtHash = buf.readVarInt(); buf.writeVarInt(this.nbtHash);
packet.amount = buf.readVarInt(); buf.writeVarInt(this.amount);
return packet;
} }
public static void toBytes(PacketRequest packet, FriendlyByteBuf buf) { @Override
buf.writeBlockPos(packet.pos); public ResourceLocation id() {
buf.writeItem(packet.stack); return PacketRequest.ID;
buf.writeVarInt(packet.nbtHash);
buf.writeVarInt(packet.amount);
} }
@SuppressWarnings("Convert2Lambda") @SuppressWarnings("Convert2Lambda")
public static void onMessage(PacketRequest message, Supplier<NetworkEvent.Context> ctx) { public static void onMessage(PacketRequest message, PlayPayloadContext ctx) {
ctx.get().enqueueWork(new Runnable() { ctx.workHandler().execute(new Runnable() {
@Override @Override
public void run() { public void run() {
Player player = ctx.get().getSender(); var player = ctx.player().orElseThrow();
var tile = Utility.getBlockEntity(ItemTerminalBlockEntity.class, player.level(), message.pos); var tile = Utility.getBlockEntity(ItemTerminalBlockEntity.class, player.level(), message.pos);
message.stack.setCount(message.amount); message.stack.setCount(message.amount);
tile.requestItem(player, message.stack, message.nbtHash); tile.requestItem(player, message.stack, message.nbtHash);
} }
}); });
ctx.get().setPacketHandled(true);
} }
} }

View file

@ -91,7 +91,7 @@ public class PipeBlock extends BaseEntityBlock {
} }
} else if (handIn == InteractionHand.MAIN_HAND && stack.isEmpty()) { } else if (handIn == InteractionHand.MAIN_HAND && stack.isEmpty()) {
if (!worldIn.isClientSide) if (!worldIn.isClientSide)
NetworkHooks.openScreen((ServerPlayer) player, tile, pos); player.openMenu(tile, pos);
return InteractionResult.SUCCESS; return InteractionResult.SUCCESS;
} }
return InteractionResult.PASS; return InteractionResult.PASS;

View file

@ -4,7 +4,6 @@ import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.Registry; import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.items.IModule; import de.ellpeck.prettypipes.items.IModule;
import de.ellpeck.prettypipes.packets.PacketButton; import de.ellpeck.prettypipes.packets.PacketButton;
import de.ellpeck.prettypipes.packets.PacketHandler;
import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen; import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.resources.sounds.SimpleSoundInstance; import net.minecraft.client.resources.sounds.SimpleSoundInstance;
@ -14,6 +13,7 @@ import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.items.SlotItemHandler; import net.neoforged.neoforge.items.SlotItemHandler;
import net.neoforged.neoforge.network.PacketDistributor;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -152,7 +152,7 @@ public abstract class AbstractPipeGui<T extends AbstractPipeContainer<?>> extend
return false; return false;
if (mouseX < this.x || mouseY < this.y || mouseX >= this.x + 28 || mouseY >= this.y + 32) if (mouseX < this.x || mouseY < this.y || mouseX >= this.x + 28 || mouseY >= this.y + 32)
return false; return false;
PacketHandler.sendToServer(new PacketButton(AbstractPipeGui.this.menu.tile.getBlockPos(), PacketButton.ButtonResult.PIPE_TAB, this.index)); PacketDistributor.SERVER.noArg().send(new PacketButton(AbstractPipeGui.this.menu.tile.getBlockPos(), PacketButton.ButtonResult.PIPE_TAB, this.index));
AbstractPipeGui.this.getMinecraft().getSoundManager().play(SimpleSoundInstance.forUI(SoundEvents.UI_BUTTON_CLICK, 1)); AbstractPipeGui.this.getMinecraft().getSoundManager().play(SimpleSoundInstance.forUI(SoundEvents.UI_BUTTON_CLICK, 1));
return true; return true;
} }

View file

@ -38,7 +38,7 @@ public class PressurizerBlock extends BaseEntityBlock {
if (tile == null) if (tile == null)
return InteractionResult.PASS; return InteractionResult.PASS;
if (!worldIn.isClientSide) if (!worldIn.isClientSide)
NetworkHooks.openScreen((ServerPlayer) player, tile, pos); player.openMenu(tile, pos);
return InteractionResult.SUCCESS; return InteractionResult.SUCCESS;
} }

View file

@ -8,7 +8,6 @@ import de.ellpeck.prettypipes.misc.ItemEquality;
import de.ellpeck.prettypipes.network.NetworkLocation; import de.ellpeck.prettypipes.network.NetworkLocation;
import de.ellpeck.prettypipes.network.PipeNetwork; import de.ellpeck.prettypipes.network.PipeNetwork;
import de.ellpeck.prettypipes.packets.PacketGhostSlot; import de.ellpeck.prettypipes.packets.PacketGhostSlot;
import de.ellpeck.prettypipes.packets.PacketHandler;
import de.ellpeck.prettypipes.pipe.PipeBlockEntity; import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
import de.ellpeck.prettypipes.terminal.containers.CraftingTerminalContainer; import de.ellpeck.prettypipes.terminal.containers.CraftingTerminalContainer;
import net.minecraft.ChatFormatting; import net.minecraft.ChatFormatting;
@ -24,6 +23,7 @@ import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.neoforge.items.ItemHandlerHelper; import net.neoforged.neoforge.items.ItemHandlerHelper;
import net.neoforged.neoforge.items.ItemStackHandler; import net.neoforged.neoforge.items.ItemStackHandler;
import net.neoforged.neoforge.network.PacketDistributor;
import org.apache.commons.lang3.mutable.MutableInt; import org.apache.commons.lang3.mutable.MutableInt;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -98,7 +98,7 @@ public class CraftingTerminalBlockEntity extends ItemTerminalBlockEntity {
List<PacketGhostSlot.Entry> clients = new ArrayList<>(); List<PacketGhostSlot.Entry> clients = new ArrayList<>();
for (var i = 0; i < this.ghostItems.getSlots(); i++) for (var i = 0; i < this.ghostItems.getSlots(); i++)
clients.add(new PacketGhostSlot.Entry(this.level, Collections.singletonList(this.ghostItems.getStackInSlot(i)))); clients.add(new PacketGhostSlot.Entry(this.level, Collections.singletonList(this.ghostItems.getStackInSlot(i))));
PacketHandler.sendToAllLoaded(this.level, this.getBlockPos(), new PacketGhostSlot(this.getBlockPos(), clients)); PacketDistributor.TRACKING_CHUNK.with(this.level.getChunkAt(this.getBlockPos())).send(new PacketGhostSlot(this.getBlockPos(), clients));
} }
} }
@ -244,4 +244,5 @@ public class CraftingTerminalBlockEntity extends ItemTerminalBlockEntity {
} }
return lowestAvailable; return lowestAvailable;
} }
} }

View file

@ -44,7 +44,7 @@ public class ItemTerminalBlock extends BaseEntityBlock {
return InteractionResult.SUCCESS; return InteractionResult.SUCCESS;
} }
if (!worldIn.isClientSide) { if (!worldIn.isClientSide) {
NetworkHooks.openScreen((ServerPlayer) player, tile, pos); player.openMenu(tile, pos);
tile.updateItems(player); tile.updateItems(player);
} }
return InteractionResult.SUCCESS; return InteractionResult.SUCCESS;

View file

@ -9,7 +9,6 @@ import de.ellpeck.prettypipes.network.NetworkItem;
import de.ellpeck.prettypipes.network.NetworkLocation; import de.ellpeck.prettypipes.network.NetworkLocation;
import de.ellpeck.prettypipes.network.NetworkLock; import de.ellpeck.prettypipes.network.NetworkLock;
import de.ellpeck.prettypipes.network.PipeNetwork; import de.ellpeck.prettypipes.network.PipeNetwork;
import de.ellpeck.prettypipes.packets.PacketHandler;
import de.ellpeck.prettypipes.packets.PacketNetworkItems; import de.ellpeck.prettypipes.packets.PacketNetworkItems;
import de.ellpeck.prettypipes.pipe.ConnectionType; import de.ellpeck.prettypipes.pipe.ConnectionType;
import de.ellpeck.prettypipes.pipe.IPipeConnectable; import de.ellpeck.prettypipes.pipe.IPipeConnectable;
@ -22,6 +21,7 @@ import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag; import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.Style; import net.minecraft.network.chat.Style;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.MenuProvider; import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
@ -141,7 +141,7 @@ public class ItemTerminalBlockEntity extends BlockEntity implements IPipeConnect
continue; continue;
if (container.tile != this) if (container.tile != this)
continue; continue;
PacketHandler.sendTo(player, new PacketNetworkItems(clientItems, clientCraftables, currentlyCrafting)); ((ServerPlayer) player).connection.send(new PacketNetworkItems(clientItems, clientCraftables, currentlyCrafting));
} }
} }
} }

View file

@ -3,13 +3,13 @@ package de.ellpeck.prettypipes.terminal.containers;
import com.mojang.blaze3d.platform.InputConstants; import com.mojang.blaze3d.platform.InputConstants;
import de.ellpeck.prettypipes.PrettyPipes; import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.packets.PacketButton; import de.ellpeck.prettypipes.packets.PacketButton;
import de.ellpeck.prettypipes.packets.PacketHandler;
import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button; import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.Screen; import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Inventory;
import net.neoforged.neoforge.network.PacketDistributor;
public class CraftingTerminalGui extends ItemTerminalGui { public class CraftingTerminalGui extends ItemTerminalGui {
@ -28,7 +28,7 @@ public class CraftingTerminalGui extends ItemTerminalGui {
var amount = ItemTerminalGui.requestModifier(); var amount = ItemTerminalGui.requestModifier();
// also allow holding backspace instead of alt for people whose alt key is inaccessible (linux?) // also allow holding backspace instead of alt for people whose alt key is inaccessible (linux?)
var force = Screen.hasAltDown() || InputConstants.isKeyDown(this.minecraft.getWindow().getWindow(), 259) ? 1 : 0; var force = Screen.hasAltDown() || InputConstants.isKeyDown(this.minecraft.getWindow().getWindow(), 259) ? 1 : 0;
PacketHandler.sendToServer(new PacketButton(this.menu.tile.getBlockPos(), PacketButton.ButtonResult.CRAFT_TERMINAL_REQUEST, amount, force)); PacketDistributor.SERVER.noArg().send(new PacketButton(this.menu.tile.getBlockPos(), PacketButton.ButtonResult.CRAFT_TERMINAL_REQUEST, amount, force));
}).bounds(this.leftPos + 8, this.topPos + 100, 50, 20).build()); }).bounds(this.leftPos + 8, this.topPos + 100, 50, 20).build());
this.tick(); this.tick();
} }
@ -47,7 +47,6 @@ public class CraftingTerminalGui extends ItemTerminalGui {
} }
} }
@Override @Override
protected void renderLabels(GuiGraphics graphics, int mouseX, int mouseY) { protected void renderLabels(GuiGraphics graphics, int mouseX, int mouseY) {
super.renderLabels(graphics, mouseX, mouseY); super.renderLabels(graphics, mouseX, mouseY);
@ -82,4 +81,5 @@ public class CraftingTerminalGui extends ItemTerminalGui {
protected CraftingTerminalContainer getCraftingContainer() { protected CraftingTerminalContainer getCraftingContainer() {
return (CraftingTerminalContainer) this.menu; return (CraftingTerminalContainer) this.menu;
} }
} }

View file

@ -5,7 +5,6 @@ import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.misc.ItemTerminalWidget; import de.ellpeck.prettypipes.misc.ItemTerminalWidget;
import de.ellpeck.prettypipes.misc.PlayerPrefs; import de.ellpeck.prettypipes.misc.PlayerPrefs;
import de.ellpeck.prettypipes.packets.PacketButton; import de.ellpeck.prettypipes.packets.PacketButton;
import de.ellpeck.prettypipes.packets.PacketHandler;
import de.ellpeck.prettypipes.packets.PacketRequest; import de.ellpeck.prettypipes.packets.PacketRequest;
import joptsimple.internal.Strings; import joptsimple.internal.Strings;
import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.gui.GuiGraphics;
@ -23,6 +22,7 @@ import net.minecraft.util.Mth;
import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag; import net.minecraft.world.item.TooltipFlag;
import net.neoforged.neoforge.network.PacketDistributor;
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;
import java.util.*; import java.util.*;
@ -91,7 +91,7 @@ public class ItemTerminalGui extends AbstractContainerScreen<ItemTerminalContain
return; return;
var stack = widget.get().stack.copy(); var stack = widget.get().stack.copy();
stack.setCount(1); stack.setCount(1);
PacketHandler.sendToServer(new PacketRequest(this.menu.tile.getBlockPos(), stack, this.requestAmount)); PacketDistributor.SERVER.noArg().send(new PacketRequest(this.menu.tile.getBlockPos(), stack, this.requestAmount));
this.requestAmount = 1; this.requestAmount = 1;
}).bounds(this.leftPos + this.getXOffset() + 95 - 7 - 25, this.topPos + 115, 50, 20).build()); }).bounds(this.leftPos + this.getXOffset() + 95 - 7 - 25, this.topPos + 115, 50, 20).build());
this.requestButton.active = false; this.requestButton.active = false;
@ -159,7 +159,7 @@ public class ItemTerminalGui extends AbstractContainerScreen<ItemTerminalContain
// and vanilla buttons are activated when the click starts, so we'll always invoke jei accidentally by default // and vanilla buttons are activated when the click starts, so we'll always invoke jei accidentally by default
if (button == 0 && this.cancelCraftingButton.visible && this.cancelCraftingButton.isHovered()) { if (button == 0 && this.cancelCraftingButton.visible && this.cancelCraftingButton.isHovered()) {
if (this.currentlyCrafting != null && !this.currentlyCrafting.isEmpty()) { if (this.currentlyCrafting != null && !this.currentlyCrafting.isEmpty()) {
PacketHandler.sendToServer(new PacketButton(this.menu.tile.getBlockPos(), PacketButton.ButtonResult.CANCEL_CRAFTING)); PacketDistributor.SERVER.noArg().send(new PacketButton(this.menu.tile.getBlockPos(), PacketButton.ButtonResult.CANCEL_CRAFTING));
return true; return true;
} }
} }