Created
April 14, 2023 20:12
-
-
Save fallenthereaper/1ba885159ab09bc70452730b8206a47a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fallenreaper.fable.core.networking.packets; | |
import fallenreaper.fable.core.base.blocks.BaseBlockEntity; | |
import fallenreaper.fable.core.networking.BaseSimplePacket; | |
import net.minecraft.client.Minecraft; | |
import net.minecraft.client.multiplayer.ClientLevel; | |
import net.minecraft.core.BlockPos; | |
import net.minecraft.network.FriendlyByteBuf; | |
import net.minecraft.world.level.block.entity.BlockEntity; | |
import net.minecraftforge.api.distmarker.Dist; | |
import net.minecraftforge.fml.DistExecutor; | |
import net.minecraftforge.network.NetworkEvent; | |
import java.util.function.Supplier; | |
/** | |
* A server to client version of {@link BlockEntitySyncPacket} | |
* | |
* @param <TE> | |
*/ | |
public abstract class BlockEntityClientSyncPacket<TE extends BaseBlockEntity> extends BaseSimplePacket { | |
protected BlockPos tilePos; | |
public BlockEntityClientSyncPacket(FriendlyByteBuf buffer) { | |
tilePos = buffer.readBlockPos(); | |
} | |
public BlockEntityClientSyncPacket(BlockPos pos) { | |
this.tilePos = pos; | |
} | |
@Override | |
public void write(FriendlyByteBuf buffer) { | |
buffer.writeBlockPos(tilePos); | |
writeData(buffer); | |
} | |
@Override | |
public void handle(Supplier<NetworkEvent.Context> context) { | |
NetworkEvent.Context ctx = context.get(); | |
ctx.enqueueWork(() -> { | |
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> { | |
ClientLevel world = Minecraft.getInstance().level; | |
if (world == null) | |
return; | |
BlockEntity tile = world.getBlockEntity(tilePos); | |
if (tile instanceof BaseBlockEntity) { | |
handlePacket((TE) tile); | |
} | |
}); | |
}); | |
ctx.setPacketHandled(true); | |
} | |
protected abstract void writeData(FriendlyByteBuf buffer); | |
protected abstract void handlePacket(TE tile); | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[21:06:20] [Netty Server IO #1/ERROR] [minecraft/Connection]: Received class net.minecraft.network.protocol.game.ClientboundDisconnectPacket that couldn't be processed | |
java.lang.ClassCastException: class net.minecraft.server.network.ServerGamePacketListenerImpl cannot be cast to class net.minecraft.network.protocol.game.ClientGamePacketListener (net.minecraft.server.network.ServerGamePacketListenerImpl and net.minecraft.network.protocol.game.ClientGamePacketListener are in module [email protected] of loader 'TRANSFORMER' @6e6d4780) | |
at net.minecraft.network.protocol.game.ClientboundDisconnectPacket.handle(ClientboundDisconnectPacket.java:7) ~[forge-1.19.2-43.1.52_mapped_parchment_2022.11.06-1.19.2-recomp.jar%23183!/:?] {re:classloading} | |
at net.minecraft.network.Connection.genericsFtw(Connection.java:175) ~[forge-1.19.2-43.1.52_mapped_parchment_2022.11.06-1.19.2-recomp.jar%23183!/:?] {re:classloading} | |
at net.minecraft.network.Connection.channelRead0(Connection.java:160) [forge-1.19.2-43.1.52_mapped_parchment_2022.11.06-1.19.2-recomp.jar%23183!/:?] {re:classloading} | |
at net.minecraft.network.Connection.channelRead0(Connection.java:48) [forge-1.19.2-43.1.52_mapped_parchment_2022.11.06-1.19.2-recomp.jar%23183!/:?] {re:classloading} | |
at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:99) [netty-transport-4.1.77.Final.jar%23137!/:4.1.77.Final] {} | |
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) [netty-transport-4.1.77.Final.jar%23137!/:4.1.77.Final] {} | |
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) [netty-transport-4.1.77.Final.jar%23137!/:4.1.77.Final] {} | |
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357) [netty-transport-4.1.77.Final.jar%23137!/:4.1.77.Final] {} | |
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410) [netty-transport-4.1.77.Final.jar%23137!/:4.1.77.Final] {} | |
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) [netty-transport-4.1.77.Final.jar%23137!/:4.1.77.Final] {} | |
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) [netty-transport-4.1.77.Final.jar%23137!/:4.1.77.Final] {} | |
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919) [netty-transport-4.1.77.Final.jar%23137!/:4.1.77.Final] {} | |
at io.netty.channel.local.LocalChannel.readInbound(LocalChannel.java:299) [netty-transport-4.1.77.Final.jar%23137!/:4.1.77.Final] {} | |
at io.netty.channel.local.LocalChannel.finishPeerRead0(LocalChannel.java:445) [netty-transport-4.1.77.Final.jar%23137!/:4.1.77.Final] {} | |
at io.netty.channel.local.LocalChannel.access$400(LocalChannel.java:50) [netty-transport-4.1.77.Final.jar%23137!/:4.1.77.Final] {} | |
at io.netty.channel.local.LocalChannel$5.run(LocalChannel.java:403) [netty-transport-4.1.77.Final.jar%23137!/:4.1.77.Final] {} | |
at io.netty.util.concurrent.AbstractEventExecutor.runTask$$$capture(AbstractEventExecutor.java:174) [netty-common-4.1.77.Final.jar%23140!/:4.1.77.Final] {} | |
at io.netty.util.concurrent.AbstractEventExecutor.runTask(AbstractEventExecutor.java) [netty-common-4.1.77.Final.jar%23140!/:4.1.77.Final] {} | |
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute$$$capture(AbstractEventExecutor.java:167) [netty-common-4.1.77.Final.jar%23140!/:4.1.77.Final] {} | |
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java) [netty-common-4.1.77.Final.jar%23140!/:4.1.77.Final] {} | |
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:470) [netty-common-4.1.77.Final.jar%23140!/:4.1.77.Final] {} | |
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:503) [netty-transport-4.1.77.Final.jar%23137!/:4.1.77.Final] {} | |
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:995) [netty-common-4.1.77.Final.jar%23140!/:4.1.77.Final] {} | |
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) [netty-common-4.1.77.Final.jar%23140!/:4.1.77.Final] {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fallenreaper.fable.core.networking.packets.mailing; | |
import fallenreaper.fable.content.blocks.mailbox.MailboxBlockEntity; | |
import fallenreaper.fable.content.entities.owl.mail.Mail; | |
import fallenreaper.fable.content.entities.owl.mail.mailbox.FormattedAddressText; | |
import fallenreaper.fable.content.entities.owl.mail.mailbox.MailAddress; | |
import fallenreaper.fable.content.entities.owl.mail.mailbox.MailboxLocation; | |
import fallenreaper.fable.core.networking.packets.BlockEntityClientSyncPacket; | |
import net.minecraft.core.BlockPos; | |
import net.minecraft.network.FriendlyByteBuf; | |
public class MailboxAddressSyncPacket extends BlockEntityClientSyncPacket<MailboxBlockEntity> { | |
String name; | |
BlockPos pos; | |
public MailboxAddressSyncPacket(FriendlyByteBuf buffer) { | |
super(buffer); | |
this.name = buffer.readUtf(); | |
this.pos = buffer.readBlockPos(); | |
} | |
public MailboxAddressSyncPacket(BlockPos pos, MailAddress address) { | |
super(pos); | |
this.name = address.getAddressText().text; | |
this.pos = address.mailboxLocation.getPosition(); | |
} | |
@Override | |
protected void writeData(FriendlyByteBuf buffer) { | |
buffer.writeBlockPos(pos); | |
buffer.writeUtf(name); | |
} | |
@Override | |
protected void handlePacket(MailboxBlockEntity tile) { | |
MailboxLocation mailboxLocation = new MailboxLocation().setPosition(pos); | |
FormattedAddressText address1 = FormattedAddressText.create(name); | |
MailAddress address = new MailAddress(address1, mailboxLocation); | |
tile.setMailAddress(address); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
protected void read(CompoundTag tag, boolean clientPacket) { | |
super.read(tag, clientPacket); | |
mailItemHandler.deserializeNBT(tag.getCompound("Inventory")); | |
this.openProgress = tag.getFloat("OpenProgress"); | |
if (tag.contains("MailAddressInfo")) { | |
String name = "null"; | |
BlockPos position = BlockPos.ZERO; | |
CompoundTag addressInfo = tag.getCompound("MailAddressInfo"); | |
CompoundTag addressNameInfo = addressInfo.getCompound("AddressNameInfo"); | |
name = addressNameInfo.getString("AddressName"); | |
if(addressInfo.contains("MailBoxLocation")) { | |
CompoundTag mailboxLocation = addressInfo.getCompound("MailBoxLocation"); | |
CompoundTag blockPos = mailboxLocation.getCompound("BlockPos"); | |
position = NbtUtils.readBlockPos(blockPos); | |
} | |
this.mailAddress = new MailAddress(FormattedAddressText.create(name), new MailboxLocation().setPosition(position)); | |
PacketsHandler.sendMSGToAll(new MailboxAddressSyncPacket(this.getBlockPos(),this.mailAddress)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fallenreaper.fable.core.networking; | |
import fallenreaper.fable.Fable; | |
import fallenreaper.fable.core.networking.packets.*; | |
import fallenreaper.fable.core.networking.packets.mailing.*; | |
import net.minecraft.core.BlockPos; | |
import net.minecraft.network.FriendlyByteBuf; | |
import net.minecraft.resources.ResourceLocation; | |
import net.minecraft.server.level.ServerPlayer; | |
import net.minecraft.world.level.Level; | |
import net.minecraftforge.network.NetworkDirection; | |
import net.minecraftforge.network.NetworkEvent; | |
import net.minecraftforge.network.NetworkRegistry; | |
import net.minecraftforge.network.PacketDistributor; | |
import net.minecraftforge.network.simple.SimpleChannel; | |
import net.minecraftforge.server.ServerLifecycleHooks; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.function.BiConsumer; | |
import java.util.function.Function; | |
import java.util.function.Supplier; | |
import static net.minecraftforge.network.NetworkDirection.PLAY_TO_CLIENT; | |
import static net.minecraftforge.network.NetworkDirection.PLAY_TO_SERVER; | |
public enum PacketsHandler { | |
//Server -> Client | |
MOUNT_RIDE(MountRidePacket.class, MountRidePacket::new, PLAY_TO_CLIENT), | |
PETRIFY_EFFECT(PetrifyPacket.class, PetrifyPacket::new, PLAY_TO_CLIENT), | |
SYNC_TRAVELER(MailTravelerEditPacket.class, MailTravelerEditPacket::new, PLAY_TO_CLIENT), | |
SYNC_ADDRESS(MailboxAddressSyncPacket.class, MailboxAddressSyncPacket::new, PLAY_TO_CLIENT), | |
SYNC_CAMERA_SHAKE(CreateCameraShakePacket.class, CreateCameraShakePacket::new, PLAY_TO_CLIENT), | |
//Client -> Server | |
ENVELOPE_DATA_SYNC(EnvelopeSyncPacket.class, EnvelopeSyncPacket::new, PLAY_TO_SERVER), | |
CONTAINER_SYNC(ContainerEditPacket.class, ContainerEditPacket::new, PLAY_TO_SERVER), | |
PLAYER_INVENTORY_SYNC(PlayerInventoryEditPacket.class, PlayerInventoryEditPacket::new, PLAY_TO_SERVER), | |
OPEN_CONTAINER(OpenMailboxContainerPacket.class, OpenMailboxContainerPacket::new, PLAY_TO_SERVER), | |
MAILBOX_ADDRESS_SYNC(MailboxSyncPacket.class, MailboxSyncPacket::new, PLAY_TO_SERVER), | |
EDIT_TRAVELLER(TravelerEditPacket.class, TravelerEditPacket::new, PLAY_TO_SERVER); | |
public static final ResourceLocation CHANNEL_NAME = Fable.asResource("main"); | |
public static final int NETWORK_VERSION = 1; | |
public static final String NETWORK_VERSION_STR = String.valueOf(NETWORK_VERSION); | |
private static final List<LoadedPacket<?>> PACKETS = new ArrayList<>(); | |
public static SimpleChannel CHANNEL; | |
private final LoadedPacket<?> packet; | |
<T extends BaseSimplePacket> PacketsHandler(Class<T> type, Function<FriendlyByteBuf, T> factory, NetworkDirection direction) { | |
packet = new PacketsHandler.LoadedPacket<>(type, factory, direction); | |
} | |
public static <MSG> void sendMSGToAll(MSG message) { | |
for (ServerPlayer player : ServerLifecycleHooks.getCurrentServer().getPlayerList().getPlayers()) { | |
CHANNEL.sendTo(message, player.connection.connection, NetworkDirection.PLAY_TO_CLIENT); | |
} | |
} | |
public static void sendToNear(Level world, BlockPos pos, int range, Object message) { | |
CHANNEL.send( | |
PacketDistributor.NEAR.with(PacketDistributor.TargetPoint.p(pos.getX(), pos.getY(), pos.getZ(), range, world.dimension())), | |
message); | |
} | |
public static void registerPackets() { | |
CHANNEL = NetworkRegistry.ChannelBuilder.named(CHANNEL_NAME) | |
.serverAcceptedVersions(NETWORK_VERSION_STR::equals) | |
.clientAcceptedVersions(NETWORK_VERSION_STR::equals) | |
.networkProtocolVersion(() -> NETWORK_VERSION_STR) | |
.simpleChannel(); | |
// for (LoadedPacket<?> packet : PACKETS) { | |
// packet.register(); | |
// } | |
for (PacketsHandler packet : values()) | |
packet.packet.register(); | |
} | |
private static class LoadedPacket<T extends BaseSimplePacket> { | |
private static int index = 0; | |
private final BiConsumer<T, FriendlyByteBuf> encoder; | |
private final Function<FriendlyByteBuf, T> decoder; | |
private final BiConsumer<T, Supplier<NetworkEvent.Context>> handler; | |
private final Class<T> type; | |
private final NetworkDirection direction; | |
private LoadedPacket(Class<T> type, Function<FriendlyByteBuf, T> factory, NetworkDirection direction) { | |
encoder = T::write; | |
decoder = factory; | |
handler = T::handle; | |
this.type = type; | |
this.direction = direction; | |
} | |
private void register() { | |
CHANNEL.messageBuilder(type, index++, direction).encoder(encoder).decoder(decoder).consumer(handler).add(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment