diff --git a/build.gradle.kts b/build.gradle.kts index e981100..1b0ce5e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -9,11 +9,11 @@ plugins { group = "com.github.creeper123123321.viafabric" val gitVersion: groovy.lang.Closure by extra -version = "0.2.1-SNAPSHOT+" + try { +version = "0.2.2-SNAPSHOT+" + try { gitVersion() } catch (e: Exception) { "unknown" -} + "+1.16" +} + "-mc-1.16" extra.set("archivesBaseName", "ViaFabric") description = "Client-side and server-side ViaVersion implementation for Fabric" diff --git a/src/main/java/com/github/creeper123123321/viafabric/commands/NMSCommandSender.java b/src/main/java/com/github/creeper123123321/viafabric/commands/NMSCommandSender.java index c7e04d3..231636e 100644 --- a/src/main/java/com/github/creeper123123321/viafabric/commands/NMSCommandSender.java +++ b/src/main/java/com/github/creeper123123321/viafabric/commands/NMSCommandSender.java @@ -24,10 +24,8 @@ package com.github.creeper123123321.viafabric.commands; -import net.fabricmc.api.EnvType; -import net.fabricmc.loader.api.FabricLoader; +import io.github.cottonmc.clientcommands.CottonClientCommandSource; import net.minecraft.client.MinecraftClient; -import net.minecraft.client.network.ClientCommandSource; import net.minecraft.entity.Entity; import net.minecraft.server.command.CommandSource; import net.minecraft.server.command.ServerCommandSource; @@ -38,7 +36,7 @@ import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter; import java.util.UUID; public class NMSCommandSender implements ViaCommandSender { - private CommandSource source; + private final CommandSource source; public NMSCommandSender(CommandSource source) { this.source = source; @@ -54,9 +52,8 @@ public class NMSCommandSender implements ViaCommandSender { public void sendMessage(String s) { if (source instanceof ServerCommandSource) { ((ServerCommandSource) source).sendFeedback(Text.Serializer.fromJson(ChatRewriter.legacyTextToJson(s)), false); - } else if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT && source instanceof ClientCommandSource) { - MinecraftClient.getInstance().player - .sendSystemMessage(Text.Serializer.fromJson(ChatRewriter.legacyTextToJson(s))); + } else if (source instanceof CottonClientCommandSource) { + ((CottonClientCommandSource) source).sendFeedback(Text.Serializer.fromJson(ChatRewriter.legacyTextToJson(s)), false); } } @@ -65,7 +62,7 @@ public class NMSCommandSender implements ViaCommandSender { if (source instanceof ServerCommandSource) { Entity entity = ((ServerCommandSource) source).getEntity(); if (entity != null) return entity.getUuid(); - } else if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT && source instanceof ClientCommandSource) { + } else if (source instanceof CottonClientCommandSource) { return MinecraftClient.getInstance().player.getUuid(); } return UUID.fromString(getName()); @@ -75,7 +72,7 @@ public class NMSCommandSender implements ViaCommandSender { public String getName() { if (source instanceof ServerCommandSource) { return ((ServerCommandSource) source).getName(); - } else if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT && source instanceof ClientCommandSource) { + } else if (source instanceof CottonClientCommandSource) { return MinecraftClient.getInstance().player.getEntityName(); } return "?"; diff --git a/src/main/java/com/github/creeper123123321/viafabric/handler/clientside/VRDecodeHandler.java b/src/main/java/com/github/creeper123123321/viafabric/handler/clientside/VRDecodeHandler.java index 54f9225..4c43b15 100644 --- a/src/main/java/com/github/creeper123123321/viafabric/handler/clientside/VRDecodeHandler.java +++ b/src/main/java/com/github/creeper123123321/viafabric/handler/clientside/VRDecodeHandler.java @@ -31,13 +31,12 @@ import io.netty.handler.codec.MessageToMessageDecoder; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.exception.CancelException; -import us.myles.ViaVersion.protocols.base.ProtocolInfo; import us.myles.ViaVersion.util.PipelineUtil; import java.util.List; public class VRDecodeHandler extends MessageToMessageDecoder { - private UserConnection user; + private final UserConnection user; public VRDecodeHandler(UserConnection user) { this.user = user; @@ -68,9 +67,6 @@ public class VRDecodeHandler extends MessageToMessageDecoder { @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { super.channelInactive(ctx); - ProtocolInfo info = user.get(ProtocolInfo.class); - if (info.getUuid() != null) { - Via.getManager().removePortedClient(info.getUuid()); - } + Via.getManager().handleDisconnect(user); } } diff --git a/src/main/java/com/github/creeper123123321/viafabric/handler/clientside/VREncodeHandler.java b/src/main/java/com/github/creeper123123321/viafabric/handler/clientside/VREncodeHandler.java index 16ef085..18e6e64 100644 --- a/src/main/java/com/github/creeper123123321/viafabric/handler/clientside/VREncodeHandler.java +++ b/src/main/java/com/github/creeper123123321/viafabric/handler/clientside/VREncodeHandler.java @@ -35,7 +35,7 @@ import us.myles.ViaVersion.util.PipelineUtil; import java.util.List; public class VREncodeHandler extends MessageToMessageEncoder { - private UserConnection user; + private final UserConnection user; public VREncodeHandler(UserConnection user) { this.user = user; diff --git a/src/main/java/com/github/creeper123123321/viafabric/handler/serverside/FabricDecodeHandler.java b/src/main/java/com/github/creeper123123321/viafabric/handler/serverside/FabricDecodeHandler.java index b65fdb2..f85c2fc 100644 --- a/src/main/java/com/github/creeper123123321/viafabric/handler/serverside/FabricDecodeHandler.java +++ b/src/main/java/com/github/creeper123123321/viafabric/handler/serverside/FabricDecodeHandler.java @@ -70,9 +70,6 @@ public class FabricDecodeHandler extends MessageToMessageDecoder { @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { super.channelInactive(ctx); // May call decode - ProtocolInfo pi = user.get(ProtocolInfo.class); - if (pi.getUuid() != null) { - Via.getManager().removePortedClient(pi.getUuid()); - } + Via.getManager().handleDisconnect(user); } } \ No newline at end of file diff --git a/src/main/java/com/github/creeper123123321/viafabric/mixin/client/MixinMultiplayerScreen.java b/src/main/java/com/github/creeper123123321/viafabric/mixin/client/MixinMultiplayerScreen.java index bedc933..03b46af 100644 --- a/src/main/java/com/github/creeper123123321/viafabric/mixin/client/MixinMultiplayerScreen.java +++ b/src/main/java/com/github/creeper123123321/viafabric/mixin/client/MixinMultiplayerScreen.java @@ -144,8 +144,7 @@ public abstract class MixinMultiplayerScreen extends Screen { } @Inject(method = "render", at = { - @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/Screen;render(IIF)V"), - @At(value = "INVOKE", target = "Lnet/minecraft/class_437;method_25394(IIF)V") // refmap bug + @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screen/Screen;render(IIF)V"), }) private void onRender(int int_1, int int_2, float float_1, CallbackInfo ci) { protocolVersion.render(int_1, int_2, float_1); diff --git a/src/main/java/com/github/creeper123123321/viafabric/platform/VRBossBar.java b/src/main/java/com/github/creeper123123321/viafabric/platform/VRBossBar.java index 3d9fb65..0066718 100644 --- a/src/main/java/com/github/creeper123123321/viafabric/platform/VRBossBar.java +++ b/src/main/java/com/github/creeper123123321/viafabric/platform/VRBossBar.java @@ -28,7 +28,7 @@ import us.myles.ViaVersion.api.boss.BossColor; import us.myles.ViaVersion.api.boss.BossStyle; import us.myles.ViaVersion.boss.CommonBoss; -public class VRBossBar extends CommonBoss { +public class VRBossBar extends CommonBoss { public VRBossBar(String title, float health, BossColor color, BossStyle style) { super(title, health, color, style); } diff --git a/src/main/java/com/github/creeper123123321/viafabric/platform/VRConnectionManager.java b/src/main/java/com/github/creeper123123321/viafabric/platform/VRConnectionManager.java new file mode 100644 index 0000000..0fffbba --- /dev/null +++ b/src/main/java/com/github/creeper123123321/viafabric/platform/VRConnectionManager.java @@ -0,0 +1,48 @@ +/* + * 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.viafabric.platform; + +import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.platform.ViaConnectionManager; + +public class VRConnectionManager extends ViaConnectionManager { + @Override + public void onLoginSuccess(UserConnection connection) { + if (connection instanceof VRClientSideUserConnection) { + this.connections.add(connection); + } else { + super.onLoginSuccess(connection); + } + } + + @Override + public void onDisconnect(UserConnection connection) { + if (connection instanceof VRClientSideUserConnection) { + this.connections.remove(connection); + } else { + super.onDisconnect(connection); + } + } +} diff --git a/src/main/java/com/github/creeper123123321/viafabric/platform/VRPlatform.java b/src/main/java/com/github/creeper123123321/viafabric/platform/VRPlatform.java index f3b331b..7607121 100644 --- a/src/main/java/com/github/creeper123123321/viafabric/platform/VRPlatform.java +++ b/src/main/java/com/github/creeper123123321/viafabric/platform/VRPlatform.java @@ -49,6 +49,7 @@ import us.myles.ViaVersion.api.command.ViaCommandSender; import us.myles.ViaVersion.api.configuration.ConfigurationProvider; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.platform.TaskId; +import us.myles.ViaVersion.api.platform.ViaConnectionManager; import us.myles.ViaVersion.api.platform.ViaPlatform; import us.myles.ViaVersion.dump.PluginInfo; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter; @@ -62,19 +63,24 @@ import java.util.ArrayList; import java.util.List; import java.util.UUID; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; +import java.util.logging.Level; import java.util.logging.Logger; import java.util.stream.Collectors; -public class VRPlatform implements ViaPlatform { - private VRViaConfig config; - private File dataFolder; +public class VRPlatform implements ViaPlatform { + private final VRViaConfig config; + private final File dataFolder; + private final ViaConnectionManager connectionManager; + private final ViaAPI api; public VRPlatform() { Path configDir = FabricLoader.getInstance().getConfigDirectory().toPath().resolve("ViaFabric"); config = new VRViaConfig(configDir.resolve("viaversion.yml").toFile()); dataFolder = configDir.toFile(); + connectionManager = new VRConnectionManager(); + api = new VRViaAPI(); } public static MinecraftServer getServer() { @@ -127,22 +133,23 @@ public class VRPlatform implements ViaPlatform { @Override public TaskId runSync(Runnable runnable) { - // Kick task needs to be on main thread - Executor executor = ViaFabric.EVENT_LOOP; - boolean alreadyLogged; - MinecraftServer server = getServer(); - if (server != null) { - alreadyLogged = true; - executor = server; + if (getServer() != null) { + return runServerSync(runnable); } else { - alreadyLogged = false; + return runEventLoop(runnable); } + } + + private TaskId runServerSync(Runnable runnable) { + // Kick task needs to be on main thread + return new FutureTaskId(CompletableFuture.runAsync(runnable, getServer())); + } + + private TaskId runEventLoop(Runnable runnable) { return new FutureTaskId( - CompletableFuture.runAsync(runnable, executor) + CompletableFuture.runAsync(runnable, ViaFabric.EVENT_LOOP) .exceptionally(throwable -> { - if (!alreadyLogged) { - throwable.printStackTrace(); - } + throwable.printStackTrace(); return null; }) ); @@ -187,33 +194,40 @@ public class VRPlatform implements ViaPlatform { public ViaCommandSender[] getOnlinePlayers() { MinecraftServer server = getServer(); if (server != null && server.isOnThread()) { - // Not thread safe - return server.getPlayerManager().getPlayerList().stream() - .map(Entity::getCommandSource) - .map(NMSCommandSender::new) - .toArray(ViaCommandSender[]::new); + return getServerPlayers(); } - return Via.getManager().getPortedPlayers().values().stream() + return Via.getManager().getConnectedClients().values().stream() .map(UserCommandSender::new) .toArray(ViaCommandSender[]::new); } + private ViaCommandSender[] getServerPlayers() { + return getServer().getPlayerManager().getPlayerList().stream() + .map(Entity::getCommandSource) + .map(NMSCommandSender::new) + .toArray(ViaCommandSender[]::new); + } + @Override public void sendMessage(UUID uuid, String s) { - UserConnection user = Via.getManager().getPortedPlayers().get(uuid); + UserConnection user = Via.getManager().getConnection(uuid); if (user instanceof VRClientSideUserConnection) { sendMessageClient(s); } else { - runSync(() -> { - MinecraftServer server = getServer(); - if (server == null) return; - ServerPlayerEntity player = server.getPlayerManager().getPlayer(uuid); - if (player == null) return; - player.sendMessage(Text.Serializer.fromJson(ChatRewriter.legacyTextToJson(s)), MessageType.SYSTEM); - }); + sendMessageServer(uuid, s); } } + private void sendMessageServer(UUID uuid, String s) { + MinecraftServer server = getServer(); + if (server == null) return; + runServerSync(() -> { + ServerPlayerEntity player = server.getPlayerManager().getPlayer(uuid); + if (player == null) return; + player.sendMessage(Text.Serializer.fromJson(ChatRewriter.legacyTextToJson(s)), MessageType.SYSTEM); + }); + } + @Environment(EnvType.CLIENT) private void sendMessageClient(String s) { ClientPlayNetworkHandler handler = MinecraftClient.getInstance().getNetworkHandler(); @@ -229,18 +243,12 @@ public class VRPlatform implements ViaPlatform { @Override public boolean kickPlayer(UUID uuid, String s) { - UserConnection user = Via.getManager().getPortedPlayers().get(uuid); + UserConnection user = Via.getManager().getConnection(uuid); if (user instanceof VRClientSideUserConnection) { return kickClient(s); } else { - MinecraftServer server = getServer(); - if (server != null && server.isOnThread()) { - ServerPlayerEntity player = server.getPlayerManager().getPlayer(uuid); - if (player == null) return false; - player.networkHandler.disconnect(Text.Serializer.fromJson(ChatRewriter.legacyTextToJson(s))); - } + return kickServer(uuid, s); } - return false; } @Environment(EnvType.CLIENT) @@ -258,14 +266,32 @@ public class VRPlatform implements ViaPlatform { return false; } + private boolean kickServer(UUID uuid, String s) { + MinecraftServer server = getServer(); + if (server == null) return false; + Supplier kickTask = () -> { + ServerPlayerEntity player = server.getPlayerManager().getPlayer(uuid); + if (player == null) return false; + player.networkHandler.disconnect(Text.Serializer.fromJson(ChatRewriter.legacyTextToJson(s))); + return true; + }; + if (server.isOnThread()) { + return kickTask.get(); + } else { + ViaFabric.JLOGGER.log(Level.WARNING, "Weird!? Player kicking was called off-thread", new Throwable()); + runServerSync(kickTask::get); + } + return false; // Can't know if it worked + } + @Override public boolean isPluginEnabled() { return true; } @Override - public ViaAPI getApi() { - return new VRViaAPI(); + public ViaAPI getApi() { + return api; } @Override @@ -294,11 +320,12 @@ public class VRPlatform implements ViaPlatform { List mods = new ArrayList<>(); for (ModContainer mod : FabricLoader.getInstance().getAllMods()) { mods.add(new PluginInfo(true, - mod.getMetadata().getName(), + mod.getMetadata().getId() + " (" + mod.getMetadata().getName() + ")", mod.getMetadata().getVersion().getFriendlyString(), null, mod.getMetadata().getAuthors().stream() - .map(info -> info.getName() + "(" + info.getContact().asMap() + ")") + .map(info -> info.getName() + + (info.getContact().asMap().isEmpty() ? "" : " " + info.getContact().asMap())) .collect(Collectors.toList()) )); } @@ -311,4 +338,9 @@ public class VRPlatform implements ViaPlatform { public boolean isOldClientsAllowed() { return true; } + + @Override + public ViaConnectionManager getConnectionManager() { + return connectionManager; + } } diff --git a/src/main/java/com/github/creeper123123321/viafabric/platform/VRViaAPI.java b/src/main/java/com/github/creeper123123321/viafabric/platform/VRViaAPI.java index 2b15e6d..34759f0 100644 --- a/src/main/java/com/github/creeper123123321/viafabric/platform/VRViaAPI.java +++ b/src/main/java/com/github/creeper123123321/viafabric/platform/VRViaAPI.java @@ -38,15 +38,10 @@ import java.util.SortedSet; import java.util.TreeSet; import java.util.UUID; -public class VRViaAPI implements ViaAPI { - @Override - public int getPlayerVersion(Void o) { - throw new UnsupportedOperationException(); - } - +public class VRViaAPI implements ViaAPI { @Override public int getPlayerVersion(UUID uuid) { - UserConnection con = Via.getManager().getPortedPlayers().get(uuid); + UserConnection con = Via.getManager().getConnection(uuid); if (con != null) { return con.get(ProtocolInfo.class).getProtocolVersion(); } @@ -58,9 +53,8 @@ public class VRViaAPI implements ViaAPI { } @Override - @Deprecated - public boolean isPorted(UUID uuid) { - return Via.getManager().getPortedPlayers().containsKey(uuid); + public boolean isInjected(UUID uuid) { + return Via.getManager().isClientConnected(uuid); } @Override @@ -68,24 +62,19 @@ public class VRViaAPI implements ViaAPI { return Via.getPlatform().getPluginVersion(); } - @Override - public void sendRawPacket(Void o, ByteBuf byteBuf) throws IllegalArgumentException { - throw new UnsupportedOperationException(); - } - @Override public void sendRawPacket(UUID uuid, ByteBuf byteBuf) throws IllegalArgumentException { - UserConnection ci = Via.getManager().getPortedPlayers().get(uuid); + UserConnection ci = Via.getManager().getConnection(uuid); ci.sendRawPacket(byteBuf); } @Override - public BossBar createBossBar(String s, BossColor bossColor, BossStyle bossStyle) { + public BossBar createBossBar(String s, BossColor bossColor, BossStyle bossStyle) { return new VRBossBar(s, 1f, bossColor, bossStyle); } @Override - public BossBar createBossBar(String s, float v, BossColor bossColor, BossStyle bossStyle) { + public BossBar createBossBar(String s, float v, BossColor bossColor, BossStyle bossStyle) { return new VRBossBar(s, v, bossColor, bossStyle); } diff --git a/src/main/java/com/github/creeper123123321/viafabric/util/FutureTaskId.java b/src/main/java/com/github/creeper123123321/viafabric/util/FutureTaskId.java index d6a5b94..0e4a683 100644 --- a/src/main/java/com/github/creeper123123321/viafabric/util/FutureTaskId.java +++ b/src/main/java/com/github/creeper123123321/viafabric/util/FutureTaskId.java @@ -29,7 +29,7 @@ import us.myles.ViaVersion.api.platform.TaskId; import java.util.concurrent.Future; public class FutureTaskId implements TaskId { - private Future object; + private final Future object; public FutureTaskId(Future object) { this.object = object;