|
| 1 | +/* |
| 2 | + * Minecraft Forge, Patchwork Project |
| 3 | + * Copyright (c) 2016-2020, 2019-2020 |
| 4 | + * |
| 5 | + * This library is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the GNU Lesser General Public |
| 7 | + * License as published by the Free Software Foundation version 2.1 |
| 8 | + * of the License. |
| 9 | + * |
| 10 | + * This library is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + * Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public |
| 16 | + * License along with this library; if not, write to the Free Software |
| 17 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | + */ |
| 19 | + |
| 20 | +package com.patchworkmc.impl.networking; |
| 21 | + |
| 22 | +import java.util.function.Consumer; |
| 23 | + |
| 24 | +import io.netty.buffer.Unpooled; |
| 25 | +import net.minecraftforge.fml.network.FMLPlayMessages; |
| 26 | +import org.apache.logging.log4j.LogManager; |
| 27 | +import org.apache.logging.log4j.Logger; |
| 28 | + |
| 29 | +import net.minecraft.container.Container; |
| 30 | +import net.minecraft.container.ContainerType; |
| 31 | +import net.minecraft.container.NameableContainerProvider; |
| 32 | +import net.minecraft.entity.Entity; |
| 33 | +import net.minecraft.network.Packet; |
| 34 | +import net.minecraft.server.network.ServerPlayerEntity; |
| 35 | +import net.minecraft.text.LiteralText; |
| 36 | +import net.minecraft.util.Identifier; |
| 37 | +import net.minecraft.util.PacketByteBuf; |
| 38 | + |
| 39 | +import net.fabricmc.api.ModInitializer; |
| 40 | +import net.fabricmc.fabric.api.network.ClientSidePacketRegistry; |
| 41 | +import net.fabricmc.fabric.api.network.ServerSidePacketRegistry; |
| 42 | + |
| 43 | +public class PatchworkPlayNetworkingMessages implements ModInitializer, MessageFactory { |
| 44 | + private static final Logger LOGGER = LogManager.getLogger("patchwork-networking"); |
| 45 | + private static final Identifier IDENTIFIER = new Identifier("fml", "play"); |
| 46 | + private static final NetworkChannelVersion VERSION = new NetworkChannelVersion("FML2", version -> true, version -> true); |
| 47 | + private static final short SPAWN_ENTITY = 0; |
| 48 | + private static final short OPEN_CONTAINER = 1; |
| 49 | + |
| 50 | + @Override |
| 51 | + public void onInitialize() { |
| 52 | + PatchworkNetworking.getVersionManager().createChannel(IDENTIFIER, VERSION); |
| 53 | + PatchworkNetworking.setFactory(this); |
| 54 | + |
| 55 | + // TODO: Move to client initializer |
| 56 | + ClientSidePacketRegistry.INSTANCE.register(IDENTIFIER, (context, buf) -> { |
| 57 | + int id = buf.readUnsignedByte(); |
| 58 | + |
| 59 | + if (id == SPAWN_ENTITY) { |
| 60 | + FMLPlayMessages.SpawnEntity spawn = FMLPlayMessages.SpawnEntity.decode(buf); |
| 61 | + FMLPlayMessages.SpawnEntity.handle(spawn, context); |
| 62 | + } else if (id == OPEN_CONTAINER) { |
| 63 | + FMLPlayMessages.OpenContainer open = FMLPlayMessages.OpenContainer.decode(buf); |
| 64 | + FMLPlayMessages.OpenContainer.handle(open, context); |
| 65 | + } else { |
| 66 | + LOGGER.warn("Received an unknown fml:play message with an id of {} and a payload of {} bytes", id, buf.readableBytes()); |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + ServerSidePacketRegistry.INSTANCE.register(IDENTIFIER, (context, buf) -> { |
| 71 | + LOGGER.warn("Received an fml:play on the server, this should not happen! Kicking the offending client."); |
| 72 | + |
| 73 | + ServerPlayerEntity entity = (ServerPlayerEntity) context.getPlayer(); |
| 74 | + |
| 75 | + entity.networkHandler.disconnect(new LiteralText("fml:play messages should only be sent to the client!")); |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public Packet<?> getEntitySpawningPacket(Entity entity) { |
| 81 | + FMLPlayMessages.SpawnEntity message = new FMLPlayMessages.SpawnEntity(entity); |
| 82 | + PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); |
| 83 | + |
| 84 | + buf.writeByte(SPAWN_ENTITY); |
| 85 | + FMLPlayMessages.SpawnEntity.encode(message, buf); |
| 86 | + |
| 87 | + return ServerSidePacketRegistry.INSTANCE.toPacket(IDENTIFIER, buf); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void sendContainerOpenPacket(ServerPlayerEntity player, NameableContainerProvider provider, Consumer<PacketByteBuf> extraDataWriter) { |
| 92 | + if (player.world.isClient) { |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + player.method_14247(); |
| 97 | + |
| 98 | + ContainerSyncAccess access = (ContainerSyncAccess) player; |
| 99 | + int openContainerId = access.patchwork$getNewContainerSyncId(); |
| 100 | + |
| 101 | + PacketByteBuf extraData = new PacketByteBuf(Unpooled.buffer()); |
| 102 | + extraDataWriter.accept(extraData); |
| 103 | + |
| 104 | + // reset to beginning in case modders read for whatever reason |
| 105 | + extraData.readerIndex(0); |
| 106 | + |
| 107 | + PacketByteBuf output = new PacketByteBuf(Unpooled.buffer()); |
| 108 | + output.writeVarInt(extraData.readableBytes()); |
| 109 | + output.writeBytes(extraData); |
| 110 | + |
| 111 | + if (output.readableBytes() > 32600 || output.readableBytes() < 1) { |
| 112 | + throw new IllegalArgumentException("Invalid PacketByteBuf for openGui, found " + output.readableBytes() + " bytes"); |
| 113 | + } |
| 114 | + |
| 115 | + Container c = provider.createMenu(openContainerId, player.inventory, player); |
| 116 | + ContainerType<?> type = c.getType(); |
| 117 | + |
| 118 | + FMLPlayMessages.OpenContainer msg = new FMLPlayMessages.OpenContainer(type, openContainerId, provider.getDisplayName(), output); |
| 119 | + Packet<?> packet = PatchworkPlayNetworkingMessages.getOpenContainerPacket(msg); |
| 120 | + |
| 121 | + player.networkHandler.sendPacket(packet); |
| 122 | + player.container = c; |
| 123 | + player.container.addListener(player); |
| 124 | + |
| 125 | + // TODO MinecraftForge.EVENT_BUS.post(new PlayerContainerEvent.Open(player, c)); |
| 126 | + } |
| 127 | + |
| 128 | + private static Packet<?> getOpenContainerPacket(FMLPlayMessages.OpenContainer message) { |
| 129 | + PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); |
| 130 | + |
| 131 | + buf.writeByte(OPEN_CONTAINER); |
| 132 | + FMLPlayMessages.OpenContainer.encode(message, buf); |
| 133 | + |
| 134 | + return ServerSidePacketRegistry.INSTANCE.toPacket(IDENTIFIER, buf); |
| 135 | + } |
| 136 | +} |
0 commit comments