From c849f69ef418eedb32ca2a1d704a61c9839e1bd4 Mon Sep 17 00:00:00 2001 From: Matthew Miller Date: Sat, 29 Dec 2018 16:42:02 +1000 Subject: [PATCH] Convert across the network handlers --- .../sk89q/worldedit/forge/ForgePlatform.java | 3 +- .../sk89q/worldedit/forge/ForgePlayer.java | 3 +- .../sk89q/worldedit/forge/ForgeWorldEdit.java | 8 ++- .../worldedit/forge/WECUIPacketHandler.java | 72 ------------------- .../handler}/InternalPacketHandler.java | 23 +++--- .../forge/net/handler/WECUIPacketHandler.java | 72 +++++++++++++++++++ .../LeftClickAirEventMessage.java | 28 ++++---- 7 files changed, 104 insertions(+), 105 deletions(-) delete mode 100644 worldedit-forge/src/main/java/com/sk89q/worldedit/forge/WECUIPacketHandler.java rename worldedit-forge/src/main/java/com/sk89q/worldedit/forge/{ => net/handler}/InternalPacketHandler.java (56%) create mode 100644 worldedit-forge/src/main/java/com/sk89q/worldedit/forge/net/handler/WECUIPacketHandler.java rename worldedit-forge/src/main/java/com/sk89q/worldedit/forge/net/{ => packet}/LeftClickAirEventMessage.java (53%) diff --git a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgePlatform.java b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgePlatform.java index 543285447..5c2f1ab89 100644 --- a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgePlatform.java +++ b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgePlatform.java @@ -30,7 +30,6 @@ import com.sk89q.worldedit.util.command.Dispatcher; import com.sk89q.worldedit.world.World; import com.sk89q.worldedit.world.registry.Registries; import net.minecraft.command.ServerCommandManager; -import net.minecraft.entity.EntityList; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.server.MinecraftServer; import net.minecraft.server.management.PlayerList; @@ -69,7 +68,7 @@ class ForgePlatform extends AbstractPlatform implements MultiUserPlatform { @Override public boolean isValidMobType(String type) { - return EntityList.isRegistered(new ResourceLocation(type)); + return net.minecraftforge.registries.ForgeRegistries.ENTITIES.containsKey(new ResourceLocation(type)); } @Override diff --git a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgePlayer.java b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgePlayer.java index c36e06471..5b3b2ca84 100644 --- a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgePlayer.java +++ b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgePlayer.java @@ -24,6 +24,7 @@ import com.sk89q.worldedit.blocks.BaseItemStack; import com.sk89q.worldedit.entity.BaseEntity; import com.sk89q.worldedit.extension.platform.AbstractPlayerActor; import com.sk89q.worldedit.extent.inventory.BlockBag; +import com.sk89q.worldedit.forge.net.handler.WECUIPacketHandler; import com.sk89q.worldedit.internal.cui.CUIEvent; import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.math.Vector3; @@ -32,10 +33,8 @@ import com.sk89q.worldedit.util.HandSide; import com.sk89q.worldedit.util.Location; import com.sk89q.worldedit.world.block.BaseBlock; import com.sk89q.worldedit.world.block.BlockStateHolder; -import com.sk89q.worldedit.world.item.ItemTypes; import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.network.PacketBuffer; import net.minecraft.network.play.server.SPacketCustomPayload; diff --git a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeWorldEdit.java b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeWorldEdit.java index 3da93610e..cf3b99c37 100644 --- a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeWorldEdit.java +++ b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeWorldEdit.java @@ -26,7 +26,9 @@ import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.event.platform.PlatformReadyEvent; import com.sk89q.worldedit.extension.platform.Platform; -import com.sk89q.worldedit.forge.net.LeftClickAirEventMessage; +import com.sk89q.worldedit.forge.net.handler.WECUIPacketHandler; +import com.sk89q.worldedit.forge.net.packet.LeftClickAirEventMessage; +import com.sk89q.worldedit.forge.net.handler.InternalPacketHandler; import com.sk89q.worldedit.util.Location; import com.sk89q.worldedit.world.block.BlockType; import com.sk89q.worldedit.world.block.BlockTypes; @@ -65,7 +67,7 @@ public class ForgeWorldEdit { private static final Logger LOGGER = LogManager.getLogger(); public static final String MOD_ID = "worldedit"; - public static final String CUI_PLUGIN_CHANNEL = "worldedit:cui"; + public static final String CUI_PLUGIN_CHANNEL = "cui"; private ForgePermissionsProvider provider; @@ -176,7 +178,7 @@ public class ForgeWorldEdit { if (event.getWorld().isRemote && event instanceof LeftClickEmpty) { // catch LCE, pass it to server - InternalPacketHandler.CHANNEL.sendToServer(new LeftClickAirEventMessage()); + InternalPacketHandler.HANDLER.sendToServer(new LeftClickAirEventMessage()); return; } diff --git a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/WECUIPacketHandler.java b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/WECUIPacketHandler.java deleted file mode 100644 index 276eadcc6..000000000 --- a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/WECUIPacketHandler.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * WorldEdit, a Minecraft world manipulation toolkit - * Copyright (C) sk89q - * Copyright (C) WorldEdit team and contributors - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - * for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - */ - -package com.sk89q.worldedit.forge; - -import com.sk89q.worldedit.LocalSession; -import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.network.NetHandlerPlayServer; -import net.minecraft.network.PacketBuffer; -import net.minecraft.network.ThreadQuickExitException; -import net.minecraft.network.play.server.SPacketCustomPayload; -import net.minecraftforge.eventbus.api.SubscribeEvent; -import net.minecraftforge.fml.common.network.FMLEventChannel; -import net.minecraftforge.fml.common.network.FMLNetworkEvent.ClientCustomPacketEvent; -import net.minecraftforge.fml.common.network.FMLNetworkEvent.ServerCustomPacketEvent; -import net.minecraftforge.fml.common.network.NetworkRegistry; - -import java.nio.charset.Charset; - -public class WECUIPacketHandler { - public static final Charset UTF_8_CHARSET = Charset.forName("UTF-8"); - public static FMLEventChannel WECUI_CHANNEL; - - public static void init() { - WECUI_CHANNEL = NetworkRegistry.INSTANCE.newEventDrivenChannel(ForgeWorldEdit.CUI_PLUGIN_CHANNEL); - WECUI_CHANNEL.register(new WECUIPacketHandler()); - } - - @SubscribeEvent - public void onPacketData(ServerCustomPacketEvent event) { - if (event.getPacket().channel().equals(ForgeWorldEdit.CUI_PLUGIN_CHANNEL)) { - EntityPlayerMP player = getPlayerFromEvent(event); - LocalSession session = ForgeWorldEdit.inst.getSession(player); - - if (session.hasCUISupport()) { - return; - } - - String text = event.getPacket().payload().toString(UTF_8_CHARSET); - session.handleCUIInitializationMessage(text); - session.describeCUI(ForgeWorldEdit.inst.wrap(player)); - } - } - - @SubscribeEvent - public void callProcessPacket(ClientCustomPacketEvent event) { - try { - new SPacketCustomPayload(event.getPacket().channel(), new PacketBuffer(event.getPacket().payload())).processPacket(event.getHandler()); - } catch (ThreadQuickExitException suppress) { - } - } - - private static EntityPlayerMP getPlayerFromEvent(ServerCustomPacketEvent event) { - return ((NetHandlerPlayServer) event.getHandler()).player; - } -} \ No newline at end of file diff --git a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/InternalPacketHandler.java b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/net/handler/InternalPacketHandler.java similarity index 56% rename from worldedit-forge/src/main/java/com/sk89q/worldedit/forge/InternalPacketHandler.java rename to worldedit-forge/src/main/java/com/sk89q/worldedit/forge/net/handler/InternalPacketHandler.java index c81062121..5af96dd61 100644 --- a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/InternalPacketHandler.java +++ b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/net/handler/InternalPacketHandler.java @@ -17,25 +17,26 @@ * along with this program. If not, see . */ -package com.sk89q.worldedit.forge; +package com.sk89q.worldedit.forge.net.handler; -import com.sk89q.worldedit.forge.net.LeftClickAirEventMessage; -import javafx.geometry.Side; +import com.sk89q.worldedit.forge.ForgeWorldEdit; +import com.sk89q.worldedit.forge.net.packet.LeftClickAirEventMessage; import net.minecraft.util.ResourceLocation; import net.minecraftforge.fml.network.NetworkRegistry; import net.minecraftforge.fml.network.simple.SimpleChannel; -import java.nio.charset.Charset; - public class InternalPacketHandler { - public static final Charset UTF_8_CHARSET = Charset.forName("UTF-8"); - public static SimpleChannel CHANNEL; + private static final String PROTOCOL_VERSION = Integer.toString(1); + public static SimpleChannel HANDLER = NetworkRegistry.ChannelBuilder + .named(new ResourceLocation(ForgeWorldEdit.MOD_ID, "internal")) + .clientAcceptedVersions(PROTOCOL_VERSION::equals) + .serverAcceptedVersions(PROTOCOL_VERSION::equals) + .networkProtocolVersion(() -> PROTOCOL_VERSION) + .simpleChannel(); public static void init() { - CHANNEL = NetworkRegistry.newSimpleChannel(new ResourceLocation(ForgeWorldEdit.MOD_ID, "worldedit"), () -> "1", check -> true, check -> true); - CHANNEL.registerMessage(LeftClickAirEventMessage.Handler.class, LeftClickAirEventMessage.class, 0, Side.SERVER); - } + int disc = 0; - private InternalPacketHandler() { + HANDLER.registerMessage(disc++, LeftClickAirEventMessage.class, LeftClickAirEventMessage::encode, LeftClickAirEventMessage::decode, LeftClickAirEventMessage.Handler::handle); } } diff --git a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/net/handler/WECUIPacketHandler.java b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/net/handler/WECUIPacketHandler.java new file mode 100644 index 000000000..215e31526 --- /dev/null +++ b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/net/handler/WECUIPacketHandler.java @@ -0,0 +1,72 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldedit.forge.net.handler; + +import com.sk89q.worldedit.LocalSession; +import com.sk89q.worldedit.forge.ForgeWorldEdit; +import net.minecraft.client.Minecraft; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.network.ThreadQuickExitException; +import net.minecraft.network.play.server.SPacketCustomPayload; +import net.minecraft.util.ResourceLocation; +import net.minecraftforge.fml.network.NetworkEvent; +import net.minecraftforge.fml.network.NetworkRegistry; +import net.minecraftforge.fml.network.event.EventNetworkChannel; + +import java.nio.charset.Charset; + +public class WECUIPacketHandler { + public static final Charset UTF_8_CHARSET = Charset.forName("UTF-8"); + private static final String PROTOCOL_VERSION = Integer.toString(1); + public static EventNetworkChannel HANDLER = NetworkRegistry.ChannelBuilder + .named(new ResourceLocation(ForgeWorldEdit.MOD_ID, ForgeWorldEdit.CUI_PLUGIN_CHANNEL)) + .clientAcceptedVersions(PROTOCOL_VERSION::equals) + .serverAcceptedVersions(PROTOCOL_VERSION::equals) + .networkProtocolVersion(() -> PROTOCOL_VERSION) + .eventNetworkChannel(); + + public static void init() { + HANDLER.addListener(WECUIPacketHandler::onPacketData); + HANDLER.addListener(WECUIPacketHandler::callProcessPacket); + } + + public static void onPacketData(NetworkEvent.ServerCustomPayloadEvent event) { + EntityPlayerMP player = event.getSource().get().getSender(); + LocalSession session = ForgeWorldEdit.inst.getSession(player); + + if (session.hasCUISupport()) { + return; + } + + String text = event.getPayload().toString(UTF_8_CHARSET); + session.handleCUIInitializationMessage(text); + session.describeCUI(ForgeWorldEdit.inst.wrap(player)); + } + + public static void callProcessPacket(NetworkEvent.ClientCustomPayloadEvent event) { + try { + new SPacketCustomPayload( + new ResourceLocation(ForgeWorldEdit.MOD_ID, ForgeWorldEdit.CUI_PLUGIN_CHANNEL), + event.getPayload() + ).processPacket(Minecraft.getInstance().player.connection); + } catch (ThreadQuickExitException suppress) { + } + } +} \ No newline at end of file diff --git a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/net/LeftClickAirEventMessage.java b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/net/packet/LeftClickAirEventMessage.java similarity index 53% rename from worldedit-forge/src/main/java/com/sk89q/worldedit/forge/net/LeftClickAirEventMessage.java rename to worldedit-forge/src/main/java/com/sk89q/worldedit/forge/net/packet/LeftClickAirEventMessage.java index 1e3b2b20e..9d983848c 100644 --- a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/net/LeftClickAirEventMessage.java +++ b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/net/packet/LeftClickAirEventMessage.java @@ -17,34 +17,32 @@ * along with this program. If not, see . */ -package com.sk89q.worldedit.forge.net; +package com.sk89q.worldedit.forge.net.packet; import com.sk89q.worldedit.forge.ForgeWorldEdit; import io.netty.buffer.ByteBuf; +import net.minecraft.network.PacketBuffer; import net.minecraftforge.event.entity.player.PlayerInteractEvent; -import net.minecraftforge.fml.common.network.simpleimpl.IMessage; -import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler; -import net.minecraftforge.fml.common.network.simpleimpl.MessageContext; +import net.minecraftforge.fml.network.NetworkEvent; -public class LeftClickAirEventMessage implements IMessage { +import java.util.function.Supplier; - public static final class Handler implements IMessageHandler { +public class LeftClickAirEventMessage { - @Override - public IMessage onMessage(LeftClickAirEventMessage message, final MessageContext ctx) { - ctx.getServerHandler().player.mcServer.addScheduledTask( - () -> ForgeWorldEdit.inst.onPlayerInteract(new PlayerInteractEvent.LeftClickEmpty(ctx.getServerHandler().player))); - return null; + public static final class Handler { + + public static void handle(final LeftClickAirEventMessage message, Supplier ctx) { + NetworkEvent.Context context = ctx.get(); + context.enqueueWork(() -> ForgeWorldEdit.inst.onPlayerInteract(new PlayerInteractEvent.LeftClickEmpty(context.getSender()))); } } - @Override - public void fromBytes(ByteBuf buf) { + public static LeftClickAirEventMessage decode(ByteBuf buf) { + return new LeftClickAirEventMessage(); } - @Override - public void toBytes(ByteBuf buf) { + public static void encode(LeftClickAirEventMessage msg, PacketBuffer buf) { } }