Implement command, ViaCommandSender

This commit is contained in:
creeper123123321 2018-08-26 18:18:06 -03:00
parent 5ed38fbd5b
commit 11033ad2ee
7 changed files with 139 additions and 19 deletions

View File

@ -24,9 +24,7 @@
package com.github.creeper123123321.viarift; package com.github.creeper123123321.viarift;
import com.github.creeper123123321.viarift.platform.VRInjector; import com.github.creeper123123321.viarift.platform.*;
import com.github.creeper123123321.viarift.platform.VRLoader;
import com.github.creeper123123321.viarift.platform.VRPlatform;
import com.github.creeper123123321.viarift.util.JLoggerToLog4j; import com.github.creeper123123321.viarift.util.JLoggerToLog4j;
import io.netty.channel.DefaultEventLoop; import io.netty.channel.DefaultEventLoop;
import io.netty.channel.EventLoop; 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.launch.MixinBootstrap;
import org.spongepowered.asm.mixin.Mixins; import org.spongepowered.asm.mixin.Mixins;
import us.myles.ViaVersion.ViaManager; import us.myles.ViaVersion.ViaManager;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.Via; 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; import java.util.concurrent.ThreadFactory;
public class ViaRift implements InitializationListener { public class ViaRift implements InitializationListener {
@ -53,8 +57,33 @@ public class ViaRift implements InitializationListener {
public void onInitialization() { public void onInitialization() {
MixinBootstrap.init(); MixinBootstrap.init();
Mixins.addConfiguration("mixins.viarift.main.json"); 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(); 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); //Via.getManager().setDebug(true);
} }
} }

View File

@ -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 {
}

View File

@ -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;
}
}

View File

@ -29,12 +29,14 @@ import com.github.creeper123123321.viarift.util.FutureTaskId;
import com.github.creeper123123321.viarift.util.ThreadTaskId; import com.github.creeper123123321.viarift.util.ThreadTaskId;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.util.text.TextComponentString; import net.minecraft.util.text.TextComponentString;
import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.ViaAPI; import us.myles.ViaVersion.api.ViaAPI;
import us.myles.ViaVersion.api.ViaVersionConfig; import us.myles.ViaVersion.api.ViaVersionConfig;
import us.myles.ViaVersion.api.command.ViaCommandSender; import us.myles.ViaVersion.api.command.ViaCommandSender;
import us.myles.ViaVersion.api.configuration.ConfigurationProvider; import us.myles.ViaVersion.api.configuration.ConfigurationProvider;
import us.myles.ViaVersion.api.platform.TaskId; import us.myles.ViaVersion.api.platform.TaskId;
import us.myles.ViaVersion.api.platform.ViaPlatform; import us.myles.ViaVersion.api.platform.ViaPlatform;
import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import us.myles.ViaVersion.sponge.VersionInfo; import us.myles.ViaVersion.sponge.VersionInfo;
import us.myles.viaversion.libs.gson.JsonObject; import us.myles.viaversion.libs.gson.JsonObject;
@ -111,13 +113,18 @@ public class VRPlatform implements ViaPlatform {
@Override @Override
public ViaCommandSender[] getOnlinePlayers() { 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 @Override
public void sendMessage(UUID uuid, String s) { public void sendMessage(UUID uuid, String s) {
if (uuid.equals(Minecraft.getMinecraft().player.getUniqueID())) { if (uuid.equals(Minecraft.getMinecraft().player.getUniqueID())) {
Minecraft.getMinecraft().player.sendMessage(new TextComponentString(s)); Minecraft.getMinecraft().addScheduledTask(() ->
Minecraft.getMinecraft().player.sendMessage(new TextComponentString(s))
);
} }
} }

View File

@ -57,6 +57,7 @@ public class VRViaAPI implements ViaAPI<Void> {
} }
@Override @Override
@Deprecated
public boolean isPorted(UUID uuid) { public boolean isPorted(UUID uuid) {
return Via.getManager().getPortedPlayers().containsKey(uuid); return Via.getManager().getPortedPlayers().containsKey(uuid);
} }

View File

@ -108,6 +108,7 @@ public class VRViaConfig extends Config implements ViaVersionConfig {
} }
@Override @Override
@Deprecated
public boolean isUnknownEntitiesSuppressed() { public boolean isUnknownEntitiesSuppressed() {
return false; return false;
} }
@ -118,6 +119,7 @@ public class VRViaConfig extends Config implements ViaVersionConfig {
} }
@Override @Override
@Deprecated
public boolean isBlockBreakPatch() { public boolean isBlockBreakPatch() {
return false; return false;
} }

View File

@ -24,6 +24,7 @@
package com.github.creeper123123321.viarift.util; package com.github.creeper123123321.viarift.util;
import java.text.MessageFormat;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.LogRecord; import java.util.logging.LogRecord;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -71,17 +72,7 @@ public class JLoggerToLog4j extends Logger {
} }
public void log(Level level, String msg, Object[] params) { public void log(Level level, String msg, Object[] params) {
if (level == Level.FINE) { log(level, MessageFormat.format(msg, params));
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);
}
} }
public void log(Level level, String msg, Throwable params) { public void log(Level level, String msg, Throwable params) {