From baa3a0a8b501750ce2812be91b5b7c1605676a24 Mon Sep 17 00:00:00 2001 From: Wyatt Childers Date: Wed, 18 May 2016 17:38:46 -0400 Subject: [PATCH] Added WorldEdit CUI support --- .../worldedit/sponge/CUIChannelHandler.java | 60 +++++++++++++++++++ .../sk89q/worldedit/sponge/SpongePlayer.java | 7 ++- .../worldedit/sponge/SpongeWorldEdit.java | 5 +- 3 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/CUIChannelHandler.java diff --git a/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/CUIChannelHandler.java b/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/CUIChannelHandler.java new file mode 100644 index 000000000..1be22b0b0 --- /dev/null +++ b/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/CUIChannelHandler.java @@ -0,0 +1,60 @@ +/* + * 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.sponge; + +import com.sk89q.worldedit.LocalSession; +import org.spongepowered.api.Platform; +import org.spongepowered.api.Sponge; +import org.spongepowered.api.entity.living.player.Player; +import org.spongepowered.api.network.*; + +import java.nio.charset.StandardCharsets; + +public class CUIChannelHandler implements RawDataListener { + public static final String CUI_PLUGIN_CHANNEL = "WECUI"; + + private static ChannelBinding.RawDataChannel channel; + + public static void init() { + channel = Sponge.getChannelRegistrar().createRawChannel(SpongeWorldEdit.inst(), CUI_PLUGIN_CHANNEL); + channel.addListener(Platform.Type.SERVER, new CUIChannelHandler()); + } + + + public static ChannelBinding.RawDataChannel getActiveChannel() { + return channel; + } + + @Override + public void handlePayload(ChannelBuf data, RemoteConnection connection, Platform.Type side) { + if (connection instanceof PlayerConnection) { + Player player = ((PlayerConnection) connection).getPlayer(); + + LocalSession session = SpongeWorldEdit.inst().getSession(player); + + if (session.hasCUISupport()) { + return; + } + + session.handleCUIInitializationMessage(new String(data.readBytes(data.available()), StandardCharsets.UTF_8)); + session.describeCUI(SpongeWorldEdit.inst().wrapPlayer(player)); + } + } +} diff --git a/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongePlayer.java b/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongePlayer.java index bed23f3cc..30b1ea0c1 100644 --- a/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongePlayer.java +++ b/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongePlayer.java @@ -30,11 +30,8 @@ import com.sk89q.worldedit.internal.LocalWorldAdapter; import com.sk89q.worldedit.internal.cui.CUIEvent; import com.sk89q.worldedit.session.SessionKey; import com.sk89q.worldedit.util.Location; -import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.util.ChatComponentText; import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.item.inventory.ItemStack; -import org.spongepowered.api.text.LiteralText; import org.spongepowered.api.text.Text; import org.spongepowered.api.text.format.TextColor; import org.spongepowered.api.text.format.TextColors; @@ -42,6 +39,7 @@ import org.spongepowered.api.text.serializer.TextSerializers; import org.spongepowered.api.world.World; import javax.annotation.Nullable; +import java.nio.charset.StandardCharsets; import java.util.Optional; import java.util.UUID; @@ -116,6 +114,9 @@ public class SpongePlayer extends AbstractPlayerActor { if (params.length > 0) { send = send + "|" + StringUtil.joinString(params, "|"); } + + String finalData = send; + CUIChannelHandler.getActiveChannel().sendTo(player, buffer -> buffer.writeBytes(finalData.getBytes(StandardCharsets.UTF_8))); } @Override diff --git a/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongeWorldEdit.java b/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongeWorldEdit.java index 7b241cbd4..0ff60109a 100644 --- a/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongeWorldEdit.java +++ b/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongeWorldEdit.java @@ -37,14 +37,11 @@ import org.spongepowered.api.config.ConfigManager; import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.event.Listener; import org.spongepowered.api.event.block.InteractBlockEvent; -import org.spongepowered.api.event.cause.NamedCause; import org.spongepowered.api.event.game.state.*; import org.spongepowered.api.item.inventory.ItemStack; import org.spongepowered.api.plugin.Plugin; import org.spongepowered.api.plugin.PluginContainer; import org.spongepowered.api.scheduler.Task; -import org.spongepowered.api.text.Text; -import org.spongepowered.api.text.channel.MessageChannel; import org.spongepowered.api.world.Location; import org.spongepowered.api.world.World; @@ -107,7 +104,7 @@ public class SpongeWorldEdit { @Listener public void init(GameInitializationEvent event) { - // WECUIPacketHandler.init(); + CUIChannelHandler.init(); } @Listener