From 11033ad2ee0016bbed5d5659b1118c936121a957 Mon Sep 17 00:00:00 2001 From: creeper123123321 Date: Sun, 26 Aug 2018 18:18:06 -0300 Subject: [PATCH] Implement command, ViaCommandSender --- .../creeper123123321/viarift/ViaRift.java | 41 +++++++++++-- .../viarift/platform/VRCommandHandler.java | 30 ++++++++++ .../viarift/platform/VRCommandSender.java | 60 +++++++++++++++++++ .../viarift/platform/VRPlatform.java | 11 +++- .../viarift/platform/VRViaAPI.java | 1 + .../viarift/platform/VRViaConfig.java | 2 + .../viarift/util/JLoggerToLog4j.java | 13 +--- 7 files changed, 139 insertions(+), 19 deletions(-) create mode 100644 src/main/java/com/github/creeper123123321/viarift/platform/VRCommandHandler.java create mode 100644 src/main/java/com/github/creeper123123321/viarift/platform/VRCommandSender.java diff --git a/src/main/java/com/github/creeper123123321/viarift/ViaRift.java b/src/main/java/com/github/creeper123123321/viarift/ViaRift.java index b16cb7d..ae0fd15 100644 --- a/src/main/java/com/github/creeper123123321/viarift/ViaRift.java +++ b/src/main/java/com/github/creeper123123321/viarift/ViaRift.java @@ -24,9 +24,7 @@ package com.github.creeper123123321.viarift; -import com.github.creeper123123321.viarift.platform.VRInjector; -import com.github.creeper123123321.viarift.platform.VRLoader; -import com.github.creeper123123321.viarift.platform.VRPlatform; +import com.github.creeper123123321.viarift.platform.*; import com.github.creeper123123321.viarift.util.JLoggerToLog4j; import io.netty.channel.DefaultEventLoop; import io.netty.channel.EventLoop; @@ -37,10 +35,16 @@ import org.dimdev.riftloader.listener.InitializationListener; import org.spongepowered.asm.launch.MixinBootstrap; import org.spongepowered.asm.mixin.Mixins; import us.myles.ViaVersion.ViaManager; +import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.Via; +import us.myles.ViaVersion.api.protocol.ProtocolRegistry; +import us.myles.ViaVersion.api.protocol.ProtocolVersion; +import us.myles.ViaVersion.api.remapper.PacketHandler; +import us.myles.ViaVersion.api.remapper.PacketRemapper; +import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.packets.State; +import us.myles.ViaVersion.protocols.base.ProtocolInfo; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ThreadFactory; public class ViaRift implements InitializationListener { @@ -53,8 +57,33 @@ public class ViaRift implements InitializationListener { public void onInitialization() { MixinBootstrap.init(); Mixins.addConfiguration("mixins.viarift.main.json"); - Via.init(ViaManager.builder().injector(new VRInjector()).loader(new VRLoader()).platform(new VRPlatform()).build()); + Via.init(ViaManager.builder() + .injector(new VRInjector()) + .loader(new VRLoader()) + .commandHandler(new VRCommandHandler()) + .platform(new VRPlatform()).build()); Via.getManager().init(); + ProtocolRegistry.getProtocolPath(ProtocolVersion.v1_13.getId(), ProtocolVersion.v1_12_2.getId()) // XGH to intercept /viarift commands + .get(0).getValue().registerIncoming(State.PLAY, 0x02, 0x02, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.STRING); + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper packetWrapper) throws Exception { + String msg = packetWrapper.get(Type.STRING, 0); + ProtocolInfo info = packetWrapper.user().get(ProtocolInfo.class); + if (msg.startsWith("/viarift")) { + Via.getManager().getCommandHandler().onCommand( + new VRCommandSender(info.getUuid(), info.getUsername()), + (msg.length() == 8 ? "" : msg.substring(9)).split(" ", -1) + ); + packetWrapper.cancel(); + } + } + }); + } + }); //Via.getManager().setDebug(true); } } diff --git a/src/main/java/com/github/creeper123123321/viarift/platform/VRCommandHandler.java b/src/main/java/com/github/creeper123123321/viarift/platform/VRCommandHandler.java new file mode 100644 index 0000000..dcbaec8 --- /dev/null +++ b/src/main/java/com/github/creeper123123321/viarift/platform/VRCommandHandler.java @@ -0,0 +1,30 @@ +/* + * MIT License + * + * Copyright (c) 2018 creeper123123321 and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.github.creeper123123321.viarift.platform; + +import us.myles.ViaVersion.commands.ViaCommandHandler; + +public class VRCommandHandler extends ViaCommandHandler { +} diff --git a/src/main/java/com/github/creeper123123321/viarift/platform/VRCommandSender.java b/src/main/java/com/github/creeper123123321/viarift/platform/VRCommandSender.java new file mode 100644 index 0000000..0050baa --- /dev/null +++ b/src/main/java/com/github/creeper123123321/viarift/platform/VRCommandSender.java @@ -0,0 +1,60 @@ +/* + * MIT License + * + * Copyright (c) 2018 creeper123123321 and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.github.creeper123123321.viarift.platform; + +import us.myles.ViaVersion.api.Via; +import us.myles.ViaVersion.api.command.ViaCommandSender; + +import java.util.UUID; + +public class VRCommandSender implements ViaCommandSender { + private UUID uuid; + private String name; + + public VRCommandSender(UUID uuid, String name) { + this.uuid = uuid; + this.name = name; + } + + @Override + public boolean hasPermission(String s) { + return true; + } + + @Override + public void sendMessage(String s) { + Via.getPlatform().sendMessage(uuid, s); + } + + @Override + public UUID getUUID() { + return uuid; + } + + @Override + public String getName() { + return name; + } +} diff --git a/src/main/java/com/github/creeper123123321/viarift/platform/VRPlatform.java b/src/main/java/com/github/creeper123123321/viarift/platform/VRPlatform.java index 88451ca..d75fe07 100644 --- a/src/main/java/com/github/creeper123123321/viarift/platform/VRPlatform.java +++ b/src/main/java/com/github/creeper123123321/viarift/platform/VRPlatform.java @@ -29,12 +29,14 @@ import com.github.creeper123123321.viarift.util.FutureTaskId; import com.github.creeper123123321.viarift.util.ThreadTaskId; import net.minecraft.client.Minecraft; import net.minecraft.util.text.TextComponentString; +import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.ViaAPI; import us.myles.ViaVersion.api.ViaVersionConfig; import us.myles.ViaVersion.api.command.ViaCommandSender; import us.myles.ViaVersion.api.configuration.ConfigurationProvider; import us.myles.ViaVersion.api.platform.TaskId; import us.myles.ViaVersion.api.platform.ViaPlatform; +import us.myles.ViaVersion.protocols.base.ProtocolInfo; import us.myles.ViaVersion.sponge.VersionInfo; import us.myles.viaversion.libs.gson.JsonObject; @@ -111,13 +113,18 @@ public class VRPlatform implements ViaPlatform { @Override public ViaCommandSender[] getOnlinePlayers() { - throw new UnsupportedOperationException(); + return Via.getManager().getPortedPlayers().values().stream().map(it -> { + ProtocolInfo info = it.get(ProtocolInfo.class); + return new VRCommandSender(info.getUuid(), info.getUsername()); + }).toArray(ViaCommandSender[]::new); } @Override public void sendMessage(UUID uuid, String s) { if (uuid.equals(Minecraft.getMinecraft().player.getUniqueID())) { - Minecraft.getMinecraft().player.sendMessage(new TextComponentString(s)); + Minecraft.getMinecraft().addScheduledTask(() -> + Minecraft.getMinecraft().player.sendMessage(new TextComponentString(s)) + ); } } diff --git a/src/main/java/com/github/creeper123123321/viarift/platform/VRViaAPI.java b/src/main/java/com/github/creeper123123321/viarift/platform/VRViaAPI.java index 8f1c815..70fae2a 100644 --- a/src/main/java/com/github/creeper123123321/viarift/platform/VRViaAPI.java +++ b/src/main/java/com/github/creeper123123321/viarift/platform/VRViaAPI.java @@ -57,6 +57,7 @@ public class VRViaAPI implements ViaAPI { } @Override + @Deprecated public boolean isPorted(UUID uuid) { return Via.getManager().getPortedPlayers().containsKey(uuid); } diff --git a/src/main/java/com/github/creeper123123321/viarift/platform/VRViaConfig.java b/src/main/java/com/github/creeper123123321/viarift/platform/VRViaConfig.java index 600ec23..a026637 100644 --- a/src/main/java/com/github/creeper123123321/viarift/platform/VRViaConfig.java +++ b/src/main/java/com/github/creeper123123321/viarift/platform/VRViaConfig.java @@ -108,6 +108,7 @@ public class VRViaConfig extends Config implements ViaVersionConfig { } @Override + @Deprecated public boolean isUnknownEntitiesSuppressed() { return false; } @@ -118,6 +119,7 @@ public class VRViaConfig extends Config implements ViaVersionConfig { } @Override + @Deprecated public boolean isBlockBreakPatch() { return false; } diff --git a/src/main/java/com/github/creeper123123321/viarift/util/JLoggerToLog4j.java b/src/main/java/com/github/creeper123123321/viarift/util/JLoggerToLog4j.java index a4130b8..be93818 100644 --- a/src/main/java/com/github/creeper123123321/viarift/util/JLoggerToLog4j.java +++ b/src/main/java/com/github/creeper123123321/viarift/util/JLoggerToLog4j.java @@ -24,6 +24,7 @@ package com.github.creeper123123321.viarift.util; +import java.text.MessageFormat; import java.util.logging.Level; import java.util.logging.LogRecord; import java.util.logging.Logger; @@ -71,17 +72,7 @@ public class JLoggerToLog4j extends Logger { } public void log(Level level, String msg, Object[] params) { - if (level == Level.FINE) { - this.base.debug(msg, params); - } else if (level == Level.WARNING) { - this.base.warn(msg, params); - } else if (level == Level.SEVERE) { - this.base.error(msg, params); - } else if (level == Level.INFO) { - this.base.info(msg, params); - } else { - this.base.trace(msg, params); - } + log(level, MessageFormat.format(msg, params)); } public void log(Level level, String msg, Throwable params) {