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;
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.javafmlmod.FMLJavaModLoadingContext;
import net.neoforged.fml.loading.FMLEnvironment;
@Mod(PrettyPipes.ID)
public final class PrettyPipes {
public static final String ID = "prettypipes";
public PrettyPipes() {
var bus = FMLJavaModLoadingContext.get().getModEventBus();
bus.addListener(Registry::setup);
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> bus.addListener(Registry.Client::setup));
public PrettyPipes(IEventBus eventBus) {
if (FMLEnvironment.dist == Dist.CLIENT)
eventBus.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.misc.ItemEquality;
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.PipeBlock;
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.level.block.Block;
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.fml.common.Mod;
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 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));
}
public static void setup(FMLCommonSetupEvent event) {
PacketHandler.setup();
}
public static final class Client {
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.misc.ItemEquality;
import de.ellpeck.prettypipes.packets.PacketCraftingModuleTransfer;
import de.ellpeck.prettypipes.packets.PacketHandler;
import de.ellpeck.prettypipes.pipe.modules.craft.CraftingModuleContainer;
import mezz.jei.api.constants.RecipeTypes;
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.crafting.CraftingRecipe;
import net.minecraft.world.item.crafting.RecipeHolder;
import net.neoforged.neoforge.network.PacketDistributor;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
@ -63,7 +63,7 @@ public class CraftingModuleTransferHandler implements IRecipeTransferHandler<Cra
if (!remain.isEmpty())
toAdd.add(remain);
}
PacketHandler.sendToServer(new PacketCraftingModuleTransfer(inputs, outputs));
PacketDistributor.SERVER.noArg().send(new PacketCraftingModuleTransfer(inputs, outputs));
return null;
}

View file

@ -2,7 +2,6 @@ package de.ellpeck.prettypipes.compat.jei;
import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.packets.PacketGhostSlot;
import de.ellpeck.prettypipes.packets.PacketHandler;
import de.ellpeck.prettypipes.terminal.containers.CraftingTerminalContainer;
import mezz.jei.api.constants.RecipeTypes;
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.item.crafting.CraftingRecipe;
import net.minecraft.world.item.crafting.RecipeHolder;
import net.neoforged.neoforge.network.PacketDistributor;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
@ -47,7 +47,7 @@ public class CraftingTerminalTransferHandler implements IRecipeTransferHandler<C
var ingredients = slots.getSlotViews(RecipeIngredientRole.INPUT);
for (var entry : ingredients)
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;
}

View file

@ -3,15 +3,15 @@ package de.ellpeck.prettypipes.misc;
import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.network.PipeNetwork;
import de.ellpeck.prettypipes.packets.*;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.Level;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.capabilities.Capabilities;
import net.neoforged.neoforge.capabilities.RegisterCapabilitiesEvent;
import net.neoforged.neoforge.event.server.ServerStartingEvent;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.network.event.RegisterPayloadHandlerEvent;
import java.io.IOException;
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.Utility;
import de.ellpeck.prettypipes.misc.ItemEquality;
import de.ellpeck.prettypipes.packets.PacketHandler;
import de.ellpeck.prettypipes.packets.PacketItemEnterPipe;
import de.ellpeck.prettypipes.pipe.IPipeItem;
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.block.state.BlockState;
import net.minecraft.world.level.saveddata.SavedData;
import net.neoforged.neoforge.network.PacketDistributor;
import org.apache.commons.lang3.tuple.Pair;
import org.jgrapht.ListenableGraph;
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));
item.setDestination(startInventory, destInventory, path);
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;
}

View file

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

View file

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

View file

@ -2,86 +2,62 @@ package de.ellpeck.prettypipes.packets;
import com.google.common.collect.Streams;
import com.mojang.datafixers.util.Pair;
import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.terminal.CraftingTerminalBlockEntity;
import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
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.List;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;
public class PacketGhostSlot {
public class PacketGhostSlot implements CustomPacketPayload {
private BlockPos pos;
private List<Entry> stacks;
public static final ResourceLocation ID = new ResourceLocation(PrettyPipes.ID, "ghost_slot");
private final BlockPos pos;
private final List<Entry> stacks;
public PacketGhostSlot(BlockPos pos, List<Entry> stacks) {
this.pos = pos;
this.stacks = stacks;
}
private PacketGhostSlot() {
}
public static PacketGhostSlot fromBytes(FriendlyByteBuf buf) {
var packet = new PacketGhostSlot();
packet.pos = buf.readBlockPos();
packet.stacks = new ArrayList<>();
public PacketGhostSlot(FriendlyByteBuf buf) {
this.pos = buf.readBlockPos();
this.stacks = new ArrayList<>();
for (var i = buf.readInt(); i > 0; i--)
packet.stacks.add(new Entry(buf));
return packet;
this.stacks.add(new Entry(buf));
}
public static void toBytes(PacketGhostSlot packet, FriendlyByteBuf buf) {
buf.writeBlockPos(packet.pos);
buf.writeInt(packet.stacks.size());
for (var entry : packet.stacks)
@Override
public void write(FriendlyByteBuf buf) {
buf.writeBlockPos(this.pos);
buf.writeInt(this.stacks.size());
for (var entry : this.stacks)
entry.write(buf);
}
@SuppressWarnings("Convert2Lambda")
public static void onMessage(PacketGhostSlot message, Supplier<NetworkEvent.Context> ctx) {
var doIt = (Consumer<Player>) p -> {
var tile = Utility.getBlockEntity(CraftingTerminalBlockEntity.class, p.level(), message.pos);
if (tile != null)
tile.setGhostItems(message.stacks);
};
@Override
public ResourceLocation id() {
return PacketGhostSlot.ID;
}
// this whole thing is a dirty hack for allowing the same packet to be used
// both client -> server and server -> client without any classloading issues
Player player = ctx.get().getSender();
// are we on the client?
if (player == null) {
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 void onMessage(PacketGhostSlot message, PlayPayloadContext ctx) {
var player = ctx.player().orElseThrow();
var tile = Utility.getBlockEntity(CraftingTerminalBlockEntity.class, player.level(), message.pos);
if (tile != null)
tile.setGhostItems(message.stacks);
}
public static class Entry {
@ -115,12 +91,12 @@ public class PacketGhostSlot {
public List<ItemStack> getStacks(Level level) {
if (this.stacks != null)
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)
.map(h -> new ItemStack(h.value())).collect(Collectors.toList());
}
public FriendlyByteBuf write(FriendlyByteBuf buf) {
public void write(FriendlyByteBuf buf) {
if (this.stacks != null) {
buf.writeBoolean(true);
buf.writeInt(this.stacks.size());
@ -130,11 +106,10 @@ public class PacketGhostSlot {
buf.writeBoolean(false);
buf.writeUtf(this.tag.location().toString());
}
return buf;
}
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();
if (tag.size() != stacks.size())
return false;
@ -145,5 +120,7 @@ public class PacketGhostSlot {
return true;
}).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;
import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.pipe.IPipeItem;
import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
@ -7,50 +8,48 @@ import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
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 CompoundTag item;
private final BlockPos tilePos;
private final CompoundTag item;
public PacketItemEnterPipe(BlockPos tilePos, IPipeItem item) {
this.tilePos = tilePos;
this.item = item.serializeNBT();
}
private PacketItemEnterPipe() {
public PacketItemEnterPipe(FriendlyByteBuf buf) {
this.tilePos = buf.readBlockPos();
this.item = buf.readNbt();
}
public static PacketItemEnterPipe fromBytes(FriendlyByteBuf buf) {
var client = new PacketItemEnterPipe();
client.tilePos = buf.readBlockPos();
client.item = buf.readNbt();
return client;
@Override
public void write(FriendlyByteBuf buf) {
buf.writeBlockPos(this.tilePos);
buf.writeNbt(this.item);
}
public static void toBytes(PacketItemEnterPipe packet, FriendlyByteBuf buf) {
buf.writeBlockPos(packet.tilePos);
buf.writeNbt(packet.item);
@Override
public ResourceLocation id() {
return PacketItemEnterPipe.ID;
}
@SuppressWarnings("Convert2Lambda")
public static void onMessage(PacketItemEnterPipe message, Supplier<NetworkEvent.Context> ctx) {
ctx.get().enqueueWork(new Runnable() {
@Override
public void run() {
var mc = Minecraft.getInstance();
if (mc.level == null)
return;
var item = IPipeItem.load(message.item);
var pipe = Utility.getBlockEntity(PipeBlockEntity.class, mc.level, message.tilePos);
if (pipe != null)
pipe.getItems().add(item);
}
public static void onMessage(PacketItemEnterPipe message, PlayPayloadContext ctx) {
ctx.workHandler().execute(() -> {
var mc = Minecraft.getInstance();
if (mc.level == null)
return;
var item = IPipeItem.load(message.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;
import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.terminal.containers.ItemTerminalGui;
import net.minecraft.client.Minecraft;
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.neoforged.neoforge.network.NetworkEvent;
import net.neoforged.neoforge.network.handling.PlayPayloadContext;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
public class PacketNetworkItems {
public class PacketNetworkItems implements CustomPacketPayload {
private List<ItemStack> items;
private List<ItemStack> craftables;
private List<ItemStack> currentlyCrafting;
public static final ResourceLocation ID = new ResourceLocation(PrettyPipes.ID, "network_items");
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) {
this.items = items;
@ -22,46 +26,46 @@ public class PacketNetworkItems {
this.currentlyCrafting = currentlyCrafting;
}
private PacketNetworkItems() {
}
public static PacketNetworkItems fromBytes(FriendlyByteBuf buf) {
var client = new PacketNetworkItems();
client.items = new ArrayList<>();
public PacketNetworkItems(FriendlyByteBuf buf) {
this.items = new ArrayList<>();
for (var i = buf.readVarInt(); i > 0; i--) {
var stack = buf.readItem();
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--)
client.craftables.add(buf.readItem());
client.currentlyCrafting = new ArrayList<>();
this.craftables.add(buf.readItem());
this.currentlyCrafting = new ArrayList<>();
for (var i = buf.readVarInt(); i > 0; i--)
client.currentlyCrafting.add(buf.readItem());
return client;
this.currentlyCrafting.add(buf.readItem());
}
public static void toBytes(PacketNetworkItems packet, FriendlyByteBuf buf) {
buf.writeVarInt(packet.items.size());
for (var stack : packet.items) {
@Override
public void write(FriendlyByteBuf buf) {
buf.writeVarInt(this.items.size());
for (var stack : this.items) {
var copy = stack.copy();
copy.setCount(1);
buf.writeItem(copy);
buf.writeVarInt(stack.getCount());
}
buf.writeVarInt(packet.craftables.size());
for (var stack : packet.craftables)
buf.writeVarInt(this.craftables.size());
for (var stack : this.craftables)
buf.writeItem(stack);
buf.writeVarInt(packet.currentlyCrafting.size());
for (var stack : packet.currentlyCrafting)
buf.writeVarInt(this.currentlyCrafting.size());
for (var stack : this.currentlyCrafting)
buf.writeItem(stack);
}
@Override
public ResourceLocation id() {
return PacketNetworkItems.ID;
}
@SuppressWarnings("Convert2Lambda")
public static void onMessage(PacketNetworkItems message, Supplier<NetworkEvent.Context> ctx) {
ctx.get().enqueueWork(new Runnable() {
public static void onMessage(PacketNetworkItems message, PlayPayloadContext ctx) {
ctx.workHandler().execute(new Runnable() {
@Override
public void run() {
var mc = Minecraft.getInstance();
@ -69,6 +73,6 @@ public class PacketNetworkItems {
terminal.updateItemList(message.items, message.craftables, message.currentlyCrafting);
}
});
ctx.get().setPacketHandled(true);
}
}

View file

@ -1,61 +1,62 @@
package de.ellpeck.prettypipes.packets;
import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.terminal.ItemTerminalBlockEntity;
import net.minecraft.core.BlockPos;
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.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 ItemStack stack;
private int nbtHash;
private int amount;
private final BlockPos pos;
private final ItemStack stack;
private final int nbtHash;
private final int amount;
public PacketRequest(BlockPos pos, ItemStack stack, int amount) {
this.pos = pos;
this.stack = stack;
if (stack.hasTag())
this.nbtHash = stack.getTag().hashCode();
this.nbtHash = stack.hasTag() ? stack.getTag().hashCode() : 0;
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) {
var packet = new PacketRequest();
packet.pos = buf.readBlockPos();
packet.stack = buf.readItem();
packet.nbtHash = buf.readVarInt();
packet.amount = buf.readVarInt();
return packet;
@Override
public void write(FriendlyByteBuf buf) {
buf.writeBlockPos(this.pos);
buf.writeItem(this.stack);
buf.writeVarInt(this.nbtHash);
buf.writeVarInt(this.amount);
}
public static void toBytes(PacketRequest packet, FriendlyByteBuf buf) {
buf.writeBlockPos(packet.pos);
buf.writeItem(packet.stack);
buf.writeVarInt(packet.nbtHash);
buf.writeVarInt(packet.amount);
@Override
public ResourceLocation id() {
return PacketRequest.ID;
}
@SuppressWarnings("Convert2Lambda")
public static void onMessage(PacketRequest message, Supplier<NetworkEvent.Context> ctx) {
ctx.get().enqueueWork(new Runnable() {
public static void onMessage(PacketRequest message, PlayPayloadContext ctx) {
ctx.workHandler().execute(new Runnable() {
@Override
public void run() {
Player player = ctx.get().getSender();
var player = ctx.player().orElseThrow();
var tile = Utility.getBlockEntity(ItemTerminalBlockEntity.class, player.level(), message.pos);
message.stack.setCount(message.amount);
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()) {
if (!worldIn.isClientSide)
NetworkHooks.openScreen((ServerPlayer) player, tile, pos);
player.openMenu(tile, pos);
return InteractionResult.SUCCESS;
}
return InteractionResult.PASS;

View file

@ -4,7 +4,6 @@ import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.items.IModule;
import de.ellpeck.prettypipes.packets.PacketButton;
import de.ellpeck.prettypipes.packets.PacketHandler;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
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.item.ItemStack;
import net.neoforged.neoforge.items.SlotItemHandler;
import net.neoforged.neoforge.network.PacketDistributor;
import java.util.ArrayList;
import java.util.List;
@ -152,7 +152,7 @@ public abstract class AbstractPipeGui<T extends AbstractPipeContainer<?>> extend
return false;
if (mouseX < this.x || mouseY < this.y || mouseX >= this.x + 28 || mouseY >= this.y + 32)
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));
return true;
}

View file

@ -38,7 +38,7 @@ public class PressurizerBlock extends BaseEntityBlock {
if (tile == null)
return InteractionResult.PASS;
if (!worldIn.isClientSide)
NetworkHooks.openScreen((ServerPlayer) player, tile, pos);
player.openMenu(tile, pos);
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.PipeNetwork;
import de.ellpeck.prettypipes.packets.PacketGhostSlot;
import de.ellpeck.prettypipes.packets.PacketHandler;
import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
import de.ellpeck.prettypipes.terminal.containers.CraftingTerminalContainer;
import net.minecraft.ChatFormatting;
@ -24,6 +23,7 @@ import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.neoforge.items.ItemHandlerHelper;
import net.neoforged.neoforge.items.ItemStackHandler;
import net.neoforged.neoforge.network.PacketDistributor;
import org.apache.commons.lang3.mutable.MutableInt;
import javax.annotation.Nullable;
@ -98,7 +98,7 @@ public class CraftingTerminalBlockEntity extends ItemTerminalBlockEntity {
List<PacketGhostSlot.Entry> clients = new ArrayList<>();
for (var i = 0; i < this.ghostItems.getSlots(); 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;
}
}

View file

@ -44,7 +44,7 @@ public class ItemTerminalBlock extends BaseEntityBlock {
return InteractionResult.SUCCESS;
}
if (!worldIn.isClientSide) {
NetworkHooks.openScreen((ServerPlayer) player, tile, pos);
player.openMenu(tile, pos);
tile.updateItems(player);
}
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.NetworkLock;
import de.ellpeck.prettypipes.network.PipeNetwork;
import de.ellpeck.prettypipes.packets.PacketHandler;
import de.ellpeck.prettypipes.packets.PacketNetworkItems;
import de.ellpeck.prettypipes.pipe.ConnectionType;
import de.ellpeck.prettypipes.pipe.IPipeConnectable;
@ -22,6 +21,7 @@ import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.Style;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
@ -141,7 +141,7 @@ public class ItemTerminalBlockEntity extends BlockEntity implements IPipeConnect
continue;
if (container.tile != this)
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 de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.packets.PacketButton;
import de.ellpeck.prettypipes.packets.PacketHandler;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
import net.neoforged.neoforge.network.PacketDistributor;
public class CraftingTerminalGui extends ItemTerminalGui {
@ -28,7 +28,7 @@ public class CraftingTerminalGui extends ItemTerminalGui {
var amount = ItemTerminalGui.requestModifier();
// 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;
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());
this.tick();
}
@ -47,7 +47,6 @@ public class CraftingTerminalGui extends ItemTerminalGui {
}
}
@Override
protected void renderLabels(GuiGraphics graphics, int mouseX, int mouseY) {
super.renderLabels(graphics, mouseX, mouseY);
@ -82,4 +81,5 @@ public class CraftingTerminalGui extends ItemTerminalGui {
protected CraftingTerminalContainer getCraftingContainer() {
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.PlayerPrefs;
import de.ellpeck.prettypipes.packets.PacketButton;
import de.ellpeck.prettypipes.packets.PacketHandler;
import de.ellpeck.prettypipes.packets.PacketRequest;
import joptsimple.internal.Strings;
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.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.neoforged.neoforge.network.PacketDistributor;
import org.apache.commons.lang3.tuple.Pair;
import java.util.*;
@ -91,7 +91,7 @@ public class ItemTerminalGui extends AbstractContainerScreen<ItemTerminalContain
return;
var stack = widget.get().stack.copy();
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;
}).bounds(this.leftPos + this.getXOffset() + 95 - 7 - 25, this.topPos + 115, 50, 20).build());
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
if (button == 0 && this.cancelCraftingButton.visible && this.cancelCraftingButton.isHovered()) {
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;
}
}