diff --git a/README.md b/README.md index 120f96a..f690dfc 100644 --- a/README.md +++ b/README.md @@ -103,9 +103,9 @@ or registry synchronization (fabric-registry-sync mod). - You can disable it in the menu or by setting global protocol version to -1 (this will keep per-server translations still enabled) -### How to use protocol auto detector?: +### How to use protocol detection?: - For using globally, set the protocol to AUTO or -2. For using in a specific server: ``ddns.example.com._v-2.viafabric`` -- The protocol auto detector will try to ping with the client native protocol version. If you have ViaVersion or +- The protocol detector will try to ping with the client native protocol version. If you have ViaVersion or similar in the server it may use the translated version, differently than multiconnect which uses -1 version, which may get the native server version. - It may hold your handshake for up to 10 seconds. diff --git a/src/main/java/com/viaversion/fabric/common/AddressParser.java b/src/main/java/com/viaversion/fabric/common/AddressParser.java index 93a1ab1..bd7ca30 100644 --- a/src/main/java/com/viaversion/fabric/common/AddressParser.java +++ b/src/main/java/com/viaversion/fabric/common/AddressParser.java @@ -63,11 +63,8 @@ public class AddressParser { } String arg = part.substring(2); - switch (option) { - case "v": { - parseProtocol(arg); - break; - } + if ("v".equals(option)) { + parseProtocol(arg); } return true; diff --git a/src/main/java/com/viaversion/fabric/common/commands/UserCommandSender.java b/src/main/java/com/viaversion/fabric/common/commands/UserCommandSender.java index bfb2e5a..f1e7ff3 100644 --- a/src/main/java/com/viaversion/fabric/common/commands/UserCommandSender.java +++ b/src/main/java/com/viaversion/fabric/common/commands/UserCommandSender.java @@ -7,7 +7,7 @@ import com.viaversion.viaversion.api.connection.UserConnection; import java.util.UUID; public class UserCommandSender implements ViaCommandSender { - private UserConnection con; + private final UserConnection con; public UserCommandSender(UserConnection con) { this.con = con; diff --git a/src/main/java/com/viaversion/fabric/common/handler/FabricDecodeHandler.java b/src/main/java/com/viaversion/fabric/common/handler/FabricDecodeHandler.java index 6f3686e..f8b7d66 100644 --- a/src/main/java/com/viaversion/fabric/common/handler/FabricDecodeHandler.java +++ b/src/main/java/com/viaversion/fabric/common/handler/FabricDecodeHandler.java @@ -9,7 +9,6 @@ import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToMessageDecoder; -import java.lang.reflect.InvocationTargetException; import java.util.List; @ChannelHandler.Sharable diff --git a/src/main/java/com/viaversion/fabric/common/platform/FabricInjector.java b/src/main/java/com/viaversion/fabric/common/platform/FabricInjector.java index 716cea8..f3ed00f 100644 --- a/src/main/java/com/viaversion/fabric/common/platform/FabricInjector.java +++ b/src/main/java/com/viaversion/fabric/common/platform/FabricInjector.java @@ -46,7 +46,7 @@ public class FabricInjector implements ViaInjector { } @Override - public int getServerProtocolVersion() throws Exception { + public int getServerProtocolVersion() { return getServerProtocolVersions().firstInt(); } } diff --git a/src/main/java/com/viaversion/fabric/common/platform/FabricViaConfig.java b/src/main/java/com/viaversion/fabric/common/platform/FabricViaConfig.java index 82194d5..1ca4e1a 100644 --- a/src/main/java/com/viaversion/fabric/common/platform/FabricViaConfig.java +++ b/src/main/java/com/viaversion/fabric/common/platform/FabricViaConfig.java @@ -10,7 +10,7 @@ import java.util.Map; public class FabricViaConfig extends AbstractViaConfig { // Based on Sponge ViaVersion - private static List UNSUPPORTED = Arrays.asList("anti-xray-patch", "bungee-ping-interval", + private static final List UNSUPPORTED = Arrays.asList("anti-xray-patch", "bungee-ping-interval", "bungee-ping-save", "bungee-servers", "quick-move-action-fix", "nms-player-ticking", "velocity-ping-interval", "velocity-ping-save", "velocity-servers", "blockconnection-method", "change-1_9-hitbox", "change-1_14-hitbox"); diff --git a/src/main/java/com/viaversion/fabric/common/provider/AbstractFabricPlatform.java b/src/main/java/com/viaversion/fabric/common/provider/AbstractFabricPlatform.java index 94c5098..815b25a 100644 --- a/src/main/java/com/viaversion/fabric/common/provider/AbstractFabricPlatform.java +++ b/src/main/java/com/viaversion/fabric/common/provider/AbstractFabricPlatform.java @@ -3,6 +3,7 @@ package com.viaversion.fabric.common.provider; import com.viaversion.fabric.common.platform.FabricViaAPI; import com.viaversion.fabric.common.platform.FabricViaConfig; import com.viaversion.fabric.common.platform.NativeVersionProvider; +import com.viaversion.fabric.common.util.FutureTaskId; import com.viaversion.fabric.common.util.JLoggerToLog4j; import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.ViaAPI; @@ -11,6 +12,9 @@ import com.viaversion.viaversion.api.configuration.ViaVersionConfig; import com.viaversion.viaversion.api.platform.ViaPlatform; import com.viaversion.viaversion.libs.gson.JsonArray; import com.viaversion.viaversion.libs.gson.JsonObject; +import io.netty.channel.EventLoop; +import io.netty.util.concurrent.Future; +import io.netty.util.concurrent.GenericFutureListener; import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.loader.api.ModContainer; import net.fabricmc.loader.api.Version; @@ -20,6 +24,10 @@ import org.apache.logging.log4j.LogManager; import java.io.File; import java.nio.file.Path; import java.util.UUID; +import java.util.concurrent.CancellationException; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.TimeUnit; import java.util.logging.Logger; public abstract class AbstractFabricPlatform implements ViaPlatform { @@ -35,6 +43,51 @@ public abstract class AbstractFabricPlatform implements ViaPlatform { api = new FabricViaAPI(); } + protected abstract ExecutorService asyncService(); + + protected abstract EventLoop eventLoop(); + + protected FutureTaskId runEventLoop(Runnable runnable) { + return new FutureTaskId(eventLoop().submit(runnable).addListener(errorLogger())); + } + + @Override + public FutureTaskId runAsync(Runnable runnable) { + return new FutureTaskId(CompletableFuture.runAsync(runnable, asyncService()) + .exceptionally(throwable -> { + if (!(throwable instanceof CancellationException)) { + throwable.printStackTrace(); + } + return null; + })); + } + + @Override + public FutureTaskId runSync(Runnable runnable, long ticks) { + // ViaVersion seems to not need to run delayed tasks on main thread + return new FutureTaskId(eventLoop() + .schedule(() -> runSync(runnable), ticks * 50, TimeUnit.MILLISECONDS) + .addListener(errorLogger()) + ); + } + + @Override + public FutureTaskId runRepeatingSync(Runnable runnable, long ticks) { + // ViaVersion seems to not need to run repeating tasks on main thread + return new FutureTaskId(eventLoop() + .scheduleAtFixedRate(() -> runSync(runnable), 0, ticks * 50, TimeUnit.MILLISECONDS) + .addListener(errorLogger()) + ); + } + + protected > GenericFutureListener errorLogger() { + return future -> { + if (!future.isCancelled() && future.cause() != null) { + future.cause().printStackTrace(); + } + }; + } + @Override public boolean isProxy() { // We kinda of have all server versions @@ -110,7 +163,7 @@ public abstract class AbstractFabricPlatform implements ViaPlatform { mod.getMetadata().getAuthors().stream().map(it -> { JsonObject info = new JsonObject(); JsonObject contact = new JsonObject(); - it.getContact().asMap().entrySet().stream() + it.getContact().asMap().entrySet() .forEach(c -> contact.addProperty(c.getKey(), c.getValue())); if (contact.size() != 0) { info.add("contact", contact); diff --git a/src/main/java/com/viaversion/fabric/common/util/RemappingUtil.java b/src/main/java/com/viaversion/fabric/common/util/RemappingUtil.java new file mode 100644 index 0000000..8218ea5 --- /dev/null +++ b/src/main/java/com/viaversion/fabric/common/util/RemappingUtil.java @@ -0,0 +1,27 @@ +package com.viaversion.fabric.common.util; + +import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.gson.GsonComponentSerializer; +import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; + +public class RemappingUtil { + public static int swordId(String id) { + // https://github.com/ViaVersion/ViaVersion/blob/8de26a0ad33f5b739f5394ed80f69d14197fddc7/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/Protocol1_9To1_8.java#L86 + switch (id) { + case "minecraft:iron_sword": + return 267; + case "minecraft:wooden_sword": + return 268; + case "minecraft:golden_sword": + return 272; + case "minecraft:diamond_sword": + return 276; + case "minecraft:stone_sword": + return 283; + } + return 0; + } + + public static String legacyToJson(String legacy) { + return GsonComponentSerializer.gson().serialize(LegacyComponentSerializer.legacySection().deserialize(legacy)); + } +} diff --git a/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/ViaFabric.java b/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/ViaFabric.java index 5ce58c4..5aa3c65 100644 --- a/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/ViaFabric.java +++ b/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/ViaFabric.java @@ -1,29 +1,28 @@ package com.viaversion.fabric.mc114; -import com.viaversion.fabric.common.config.VFConfig; -import com.viaversion.fabric.common.platform.FabricInjector; -import com.viaversion.fabric.mc114.commands.VRCommandHandler; -import com.viaversion.fabric.mc114.platform.VFLoader; -import com.viaversion.fabric.mc114.platform.FabricPlatform; -import com.viaversion.fabric.common.protocol.HostnameParserProtocol; -import com.viaversion.fabric.common.util.JLoggerToLog4j; import com.google.common.collect.Range; import com.google.common.util.concurrent.ThreadFactoryBuilder; import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.RequiredArgumentBuilder; -import io.netty.channel.DefaultEventLoop; -import io.netty.channel.EventLoop; -import net.fabricmc.api.ModInitializer; -import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback; -import net.fabricmc.fabric.api.registry.CommandRegistry; -import net.fabricmc.loader.api.FabricLoader; -import net.minecraft.server.command.CommandSource; -import org.apache.logging.log4j.LogManager; +import com.viaversion.fabric.common.config.VFConfig; +import com.viaversion.fabric.common.platform.FabricInjector; +import com.viaversion.fabric.common.protocol.HostnameParserProtocol; +import com.viaversion.fabric.common.util.JLoggerToLog4j; +import com.viaversion.fabric.mc114.commands.VRCommandHandler; +import com.viaversion.fabric.mc114.platform.FabricPlatform; +import com.viaversion.fabric.mc114.platform.VFLoader; import com.viaversion.viaversion.ViaManagerImpl; import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.data.MappingDataLoader; import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; +import io.netty.channel.DefaultEventLoop; +import io.netty.channel.EventLoop; +import net.fabricmc.api.ModInitializer; +import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback; +import net.fabricmc.loader.api.FabricLoader; +import net.minecraft.server.command.CommandSource; +import org.apache.logging.log4j.LogManager; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; @@ -35,7 +34,7 @@ public class ViaFabric implements ModInitializer { public static final Logger JLOGGER = new JLoggerToLog4j(LogManager.getLogger("ViaFabric")); public static final ExecutorService ASYNC_EXECUTOR; public static final EventLoop EVENT_LOOP; - public static CompletableFuture INIT_FUTURE = new CompletableFuture<>(); + public static final CompletableFuture INIT_FUTURE = new CompletableFuture<>(); public static VFConfig config; static { @@ -73,16 +72,7 @@ public class ViaFabric implements ModInitializer { FabricLoader.getInstance().getEntrypoints("viafabric:via_api_initialized", Runnable.class).forEach(Runnable::run); - try { - registerCommandsV1(); - } catch (NoClassDefFoundError ignored) { - try { - registerCommandsV0(); - JLOGGER.info("Using Fabric Commands V0"); - } catch (NoClassDefFoundError ignored2) { - JLOGGER.info("Couldn't register command as Fabric Commands isn't installed"); - } - } + registerCommandsV1(); config = new VFConfig(FabricLoader.getInstance().getConfigDir().resolve("ViaFabric") .resolve("viafabric.yml").toFile()); @@ -91,15 +81,12 @@ public class ViaFabric implements ModInitializer { } private void registerCommandsV1() { - CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> dispatcher.register(command("viaversion"))); - CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> dispatcher.register(command("viaver"))); - CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> dispatcher.register(command("vvfabric"))); - } - - @SuppressWarnings("deprecation") - private void registerCommandsV0() { - CommandRegistry.INSTANCE.register(false, dispatcher -> dispatcher.register(command("viaversion"))); - CommandRegistry.INSTANCE.register(false, dispatcher -> dispatcher.register(command("viaver"))); - CommandRegistry.INSTANCE.register(false, dispatcher -> dispatcher.register(command("vvfabric"))); + try { + CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> dispatcher.register(command("viaversion"))); + CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> dispatcher.register(command("viaver"))); + CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> dispatcher.register(command("vvfabric"))); + } catch (NoClassDefFoundError ignored) { + JLOGGER.info("Couldn't register command as Fabric Commands V1 isn't installed"); + } } } diff --git a/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/commands/NMSCommandSender.java b/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/commands/NMSCommandSender.java index 1214e3a..16872fc 100644 --- a/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/commands/NMSCommandSender.java +++ b/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/commands/NMSCommandSender.java @@ -1,8 +1,7 @@ package com.viaversion.fabric.mc114.commands; +import com.viaversion.fabric.common.util.RemappingUtil; import com.viaversion.viaversion.api.command.ViaCommandSender; -import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.gson.GsonComponentSerializer; -import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.fabricmc.api.EnvType; import net.fabricmc.loader.api.FabricLoader; import net.minecraft.client.MinecraftClient; @@ -30,17 +29,13 @@ public class NMSCommandSender implements ViaCommandSender { @Override public void sendMessage(String s) { if (source instanceof ServerCommandSource) { - ((ServerCommandSource) source).sendFeedback(Text.Serializer.fromJson(legacyToJson(s)), false); + ((ServerCommandSource) source).sendFeedback(Text.Serializer.fromJson(RemappingUtil.legacyToJson(s)), false); } else if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT && source instanceof ClientCommandSource) { - MinecraftClient.getInstance().player.addChatMessage(Text.Serializer.fromJson(legacyToJson(s)), false); + MinecraftClient.getInstance().player.addChatMessage(Text.Serializer.fromJson(RemappingUtil.legacyToJson(s)), false); } } - private String legacyToJson(String legacy) { - return GsonComponentSerializer.gson().serialize(LegacyComponentSerializer.legacySection().deserialize(legacy)); - } - @Override public UUID getUUID() { if (source instanceof ServerCommandSource) { diff --git a/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/gui/ViaConfigScreen.java b/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/gui/ViaConfigScreen.java index 66a3f87..2e14050 100644 --- a/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/gui/ViaConfigScreen.java +++ b/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/gui/ViaConfigScreen.java @@ -2,7 +2,6 @@ package com.viaversion.fabric.mc114.gui; import com.viaversion.fabric.mc114.ViaFabric; import com.viaversion.fabric.common.util.ProtocolUtils; -import com.viaversion.viaversion.api.Via; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.MinecraftClient; diff --git a/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/platform/FabricPlatform.java b/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/platform/FabricPlatform.java index a2cd751..951438e 100644 --- a/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/platform/FabricPlatform.java +++ b/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/platform/FabricPlatform.java @@ -3,14 +3,12 @@ package com.viaversion.fabric.mc114.platform; import com.viaversion.fabric.common.commands.UserCommandSender; import com.viaversion.fabric.common.provider.AbstractFabricPlatform; import com.viaversion.fabric.common.util.FutureTaskId; +import com.viaversion.fabric.common.util.RemappingUtil; import com.viaversion.fabric.mc114.ViaFabric; import com.viaversion.fabric.mc114.commands.NMSCommandSender; import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.command.ViaCommandSender; -import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.gson.GsonComponentSerializer; -import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; -import io.netty.util.concurrent.Future; -import io.netty.util.concurrent.GenericFutureListener; +import io.netty.channel.EventLoop; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.fabricmc.loader.api.FabricLoader; @@ -22,9 +20,8 @@ import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; import java.util.UUID; -import java.util.concurrent.CancellationException; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.ExecutorService; import java.util.function.Supplier; import java.util.logging.Level; @@ -41,23 +38,6 @@ public class FabricPlatform extends AbstractFabricPlatform { return MinecraftClient.getInstance().getServer(); } - public static String legacyToJson(String legacy) { - return GsonComponentSerializer.gson().serialize(LegacyComponentSerializer.legacySection().deserialize(legacy)); - } - - @Override - public FutureTaskId runAsync(Runnable runnable) { - return new FutureTaskId(CompletableFuture - .runAsync(runnable, ViaFabric.ASYNC_EXECUTOR) - .exceptionally(throwable -> { - if (!(throwable instanceof CancellationException)) { - throwable.printStackTrace(); - } - return null; - }) - ); - } - @Override public FutureTaskId runSync(Runnable runnable) { if (getServer() != null) { @@ -72,42 +52,6 @@ public class FabricPlatform extends AbstractFabricPlatform { return new FutureTaskId(CompletableFuture.runAsync(runnable, getServer())); } - private FutureTaskId runEventLoop(Runnable runnable) { - return new FutureTaskId( - ViaFabric.EVENT_LOOP - .submit(runnable) - .addListener(errorLogger()) - ); - } - - @Override - public FutureTaskId runSync(Runnable runnable, long ticks) { - // ViaVersion seems to not need to run delayed tasks on main thread - return new FutureTaskId( - ViaFabric.EVENT_LOOP - .schedule(() -> runSync(runnable), ticks * 50, TimeUnit.MILLISECONDS) - .addListener(errorLogger()) - ); - } - - @Override - public FutureTaskId runRepeatingSync(Runnable runnable, long ticks) { - // ViaVersion seems to not need to run repeating tasks on main thread - return new FutureTaskId( - ViaFabric.EVENT_LOOP - .scheduleAtFixedRate(() -> runSync(runnable), 0, ticks * 50, TimeUnit.MILLISECONDS) - .addListener(errorLogger()) - ); - } - - private > GenericFutureListener errorLogger() { - return future -> { - if (!future.isCancelled() && future.cause() != null) { - future.cause().printStackTrace(); - } - }; - } - @Override public ViaCommandSender[] getOnlinePlayers() { MinecraftServer server = getServer(); @@ -137,7 +81,7 @@ public class FabricPlatform extends AbstractFabricPlatform { runServerSync(() -> { ServerPlayerEntity player = server.getPlayerManager().getPlayer(uuid); if (player == null) return; - player.sendChatMessage(Text.Serializer.fromJson(legacyToJson(s)), MessageType.SYSTEM); + player.sendChatMessage(Text.Serializer.fromJson(RemappingUtil.legacyToJson(s)), MessageType.SYSTEM); }); } @@ -152,7 +96,7 @@ public class FabricPlatform extends AbstractFabricPlatform { Supplier kickTask = () -> { ServerPlayerEntity player = server.getPlayerManager().getPlayer(uuid); if (player == null) return false; - player.networkHandler.disconnect(Text.Serializer.fromJson(legacyToJson(s))); + player.networkHandler.disconnect(Text.Serializer.fromJson(RemappingUtil.legacyToJson(s))); return true; }; if (server.isOnThread()) { @@ -163,4 +107,14 @@ public class FabricPlatform extends AbstractFabricPlatform { } return false; // Can't know if it worked } + + @Override + protected ExecutorService asyncService() { + return ViaFabric.ASYNC_EXECUTOR; + } + + @Override + protected EventLoop eventLoop() { + return ViaFabric.EVENT_LOOP; + } } diff --git a/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/providers/VRHandItemProvider.java b/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/providers/VRHandItemProvider.java index 0863d89..7cdfdc5 100644 --- a/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/providers/VRHandItemProvider.java +++ b/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/providers/VRHandItemProvider.java @@ -1,5 +1,6 @@ package com.viaversion.fabric.mc114.providers; +import com.viaversion.fabric.common.util.RemappingUtil; import com.viaversion.fabric.mc114.ViaFabric; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; @@ -21,7 +22,7 @@ import java.util.concurrent.ConcurrentHashMap; public class VRHandItemProvider extends HandItemProvider { public Item clientItem = null; - public Map serverPlayers = new ConcurrentHashMap<>(); + public final Map serverPlayers = new ConcurrentHashMap<>(); @Override public Item getHandItem(UserConnection info) { @@ -50,7 +51,7 @@ public class VRHandItemProvider extends HandItemProvider { } }); } catch (NoClassDefFoundError ignored2) { - ViaFabric.JLOGGER.info("Fabric Lifecycle V0/V1 isn't installed"); + ViaFabric.JLOGGER.info("Fabric Lifecycle V0 isn't installed"); } } @@ -77,25 +78,7 @@ public class VRHandItemProvider extends HandItemProvider { private Item fromNative(ItemStack original) { Identifier iid = Registry.ITEM.getId(original.getItem()); - if (iid == null) return new DataItem(0, (byte) 0, (short) 0, null); - int id = swordId(iid.toString()); + int id = RemappingUtil.swordId(iid.toString()); return new DataItem(id, (byte) original.getCount(), (short) original.getDamage(), null); } - - private int swordId(String id) { - // https://github.com/ViaVersion/ViaVersion/blob/8de26a0ad33f5b739f5394ed80f69d14197fddc7/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/Protocol1_9To1_8.java#L86 - switch (id) { - case "minecraft:iron_sword": - return 267; - case "minecraft:wooden_sword": - return 268; - case "minecraft:golden_sword": - return 272; - case "minecraft:diamond_sword": - return 276; - case "minecraft:stone_sword": - return 283; - } - return 0; - } } diff --git a/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/service/ProtocolAutoDetector.java b/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/service/ProtocolAutoDetector.java index 011bf64..ab7b09c 100644 --- a/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/service/ProtocolAutoDetector.java +++ b/viafabric-mc114/src/main/java/com/viaversion/fabric/mc114/service/ProtocolAutoDetector.java @@ -32,7 +32,7 @@ import java.util.logging.Level; @Environment(EnvType.CLIENT) public class ProtocolAutoDetector { - private static LoadingCache> SERVER_VER = CacheBuilder.newBuilder() + private static final LoadingCache> SERVER_VER = CacheBuilder.newBuilder() .expireAfterWrite(30, TimeUnit.SECONDS) .build(CacheLoader.from((address) -> { CompletableFuture future = new CompletableFuture<>(); diff --git a/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/ViaFabric.java b/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/ViaFabric.java index 6084500..8a6977a 100644 --- a/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/ViaFabric.java +++ b/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/ViaFabric.java @@ -10,8 +10,8 @@ import com.viaversion.fabric.common.platform.FabricInjector; import com.viaversion.fabric.common.protocol.HostnameParserProtocol; import com.viaversion.fabric.common.util.JLoggerToLog4j; import com.viaversion.fabric.mc115.commands.VRCommandHandler; -import com.viaversion.fabric.mc115.platform.VFLoader; import com.viaversion.fabric.mc115.platform.FabricPlatform; +import com.viaversion.fabric.mc115.platform.VFLoader; import com.viaversion.viaversion.ViaManagerImpl; import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.data.MappingDataLoader; @@ -20,7 +20,6 @@ import io.netty.channel.DefaultEventLoop; import io.netty.channel.EventLoop; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback; -import net.fabricmc.fabric.api.registry.CommandRegistry; import net.fabricmc.loader.api.FabricLoader; import net.minecraft.server.command.CommandSource; import org.apache.logging.log4j.LogManager; @@ -35,7 +34,7 @@ public class ViaFabric implements ModInitializer { public static final Logger JLOGGER = new JLoggerToLog4j(LogManager.getLogger("ViaFabric")); public static final ExecutorService ASYNC_EXECUTOR; public static final EventLoop EVENT_LOOP; - public static CompletableFuture INIT_FUTURE = new CompletableFuture<>(); + public static final CompletableFuture INIT_FUTURE = new CompletableFuture<>(); public static VFConfig config; static { @@ -73,16 +72,7 @@ public class ViaFabric implements ModInitializer { FabricLoader.getInstance().getEntrypoints("viafabric:via_api_initialized", Runnable.class).forEach(Runnable::run); - try { - registerCommandsV1(); - } catch (NoClassDefFoundError ignored) { - try { - registerCommandsV0(); - JLOGGER.info("Using Fabric Commands V0"); - } catch (NoClassDefFoundError ignored2) { - JLOGGER.info("Couldn't register command as Fabric Commands isn't installed"); - } - } + registerCommandsV1(); config = new VFConfig(FabricLoader.getInstance().getConfigDir().resolve("ViaFabric") .resolve("viafabric.yml").toFile()); @@ -91,15 +81,12 @@ public class ViaFabric implements ModInitializer { } private void registerCommandsV1() { - CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> dispatcher.register(command("viaversion"))); - CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> dispatcher.register(command("viaver"))); - CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> dispatcher.register(command("vvfabric"))); - } - - @SuppressWarnings("deprecation") - private void registerCommandsV0() { - CommandRegistry.INSTANCE.register(false, dispatcher -> dispatcher.register(command("viaversion"))); - CommandRegistry.INSTANCE.register(false, dispatcher -> dispatcher.register(command("viaver"))); - CommandRegistry.INSTANCE.register(false, dispatcher -> dispatcher.register(command("vvfabric"))); + try { + CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> dispatcher.register(command("viaversion"))); + CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> dispatcher.register(command("viaver"))); + CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> dispatcher.register(command("vvfabric"))); + } catch (NoClassDefFoundError ignored) { + JLOGGER.info("Couldn't register command as Fabric Commands V1 isn't installed"); + } } } diff --git a/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/commands/NMSCommandSender.java b/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/commands/NMSCommandSender.java index 005f6e4..a177351 100644 --- a/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/commands/NMSCommandSender.java +++ b/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/commands/NMSCommandSender.java @@ -1,8 +1,7 @@ package com.viaversion.fabric.mc115.commands; +import com.viaversion.fabric.common.util.RemappingUtil; import com.viaversion.viaversion.api.command.ViaCommandSender; -import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.gson.GsonComponentSerializer; -import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.fabricmc.api.EnvType; import net.fabricmc.loader.api.FabricLoader; import net.minecraft.client.MinecraftClient; @@ -30,16 +29,12 @@ public class NMSCommandSender implements ViaCommandSender { @Override public void sendMessage(String s) { if (source instanceof ServerCommandSource) { - ((ServerCommandSource) source).sendFeedback(Text.Serializer.fromJson(legacyToJson(s)), false); + ((ServerCommandSource) source).sendFeedback(Text.Serializer.fromJson(RemappingUtil.legacyToJson(s)), false); } else if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT && source instanceof ClientCommandSource) { - MinecraftClient.getInstance().player.addChatMessage(Text.Serializer.fromJson(legacyToJson(s)), false); + MinecraftClient.getInstance().player.addChatMessage(Text.Serializer.fromJson(RemappingUtil.legacyToJson(s)), false); } } - private String legacyToJson(String legacy) { - return GsonComponentSerializer.gson().serialize(LegacyComponentSerializer.legacySection().deserialize(legacy)); - } - @Override public UUID getUUID() { if (source instanceof ServerCommandSource) { diff --git a/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/gui/ViaConfigScreen.java b/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/gui/ViaConfigScreen.java index 58acb01..8c82a01 100644 --- a/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/gui/ViaConfigScreen.java +++ b/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/gui/ViaConfigScreen.java @@ -2,7 +2,6 @@ package com.viaversion.fabric.mc115.gui; import com.viaversion.fabric.mc115.ViaFabric; import com.viaversion.fabric.common.util.ProtocolUtils; -import com.viaversion.viaversion.api.Via; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.MinecraftClient; diff --git a/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/platform/FabricPlatform.java b/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/platform/FabricPlatform.java index 5895b1a..0590e0e 100644 --- a/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/platform/FabricPlatform.java +++ b/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/platform/FabricPlatform.java @@ -1,52 +1,29 @@ package com.viaversion.fabric.mc115.platform; import com.viaversion.fabric.common.commands.UserCommandSender; -import com.viaversion.fabric.common.platform.FabricViaAPI; -import com.viaversion.fabric.common.platform.FabricViaConfig; import com.viaversion.fabric.common.provider.AbstractFabricPlatform; import com.viaversion.fabric.common.util.FutureTaskId; -import com.viaversion.fabric.common.util.JLoggerToLog4j; +import com.viaversion.fabric.common.util.RemappingUtil; import com.viaversion.fabric.mc115.ViaFabric; import com.viaversion.fabric.mc115.commands.NMSCommandSender; import com.viaversion.viaversion.api.Via; -import com.viaversion.viaversion.api.ViaAPI; import com.viaversion.viaversion.api.command.ViaCommandSender; -import com.viaversion.viaversion.api.configuration.ConfigurationProvider; -import com.viaversion.viaversion.api.configuration.ViaVersionConfig; -import com.viaversion.viaversion.api.platform.ViaPlatform; -import com.viaversion.viaversion.dump.PluginInfo; -import com.viaversion.viaversion.libs.gson.JsonObject; -import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.gson.GsonComponentSerializer; -import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; -import com.viaversion.viaversion.util.GsonUtil; -import io.netty.util.concurrent.Future; -import io.netty.util.concurrent.GenericFutureListener; +import io.netty.channel.EventLoop; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.fabricmc.loader.api.FabricLoader; -import net.fabricmc.loader.api.ModContainer; -import net.fabricmc.loader.api.Version; -import net.fabricmc.loader.api.metadata.ModMetadata; import net.minecraft.client.MinecraftClient; import net.minecraft.entity.Entity; import net.minecraft.network.MessageType; import net.minecraft.server.MinecraftServer; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; -import org.apache.logging.log4j.LogManager; -import java.io.File; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.List; import java.util.UUID; -import java.util.concurrent.CancellationException; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.ExecutorService; import java.util.function.Supplier; import java.util.logging.Level; -import java.util.logging.Logger; -import java.util.stream.Collectors; public class FabricPlatform extends AbstractFabricPlatform { public static MinecraftServer getServer() { @@ -61,21 +38,14 @@ public class FabricPlatform extends AbstractFabricPlatform { return MinecraftClient.getInstance().getServer(); } - public static String legacyToJson(String legacy) { - return GsonComponentSerializer.gson().serialize(LegacyComponentSerializer.legacySection().deserialize(legacy)); + @Override + protected ExecutorService asyncService() { + return ViaFabric.ASYNC_EXECUTOR; } @Override - public FutureTaskId runAsync(Runnable runnable) { - return new FutureTaskId(CompletableFuture - .runAsync(runnable, ViaFabric.ASYNC_EXECUTOR) - .exceptionally(throwable -> { - if (!(throwable instanceof CancellationException)) { - throwable.printStackTrace(); - } - return null; - }) - ); + protected EventLoop eventLoop() { + return ViaFabric.EVENT_LOOP; } @Override @@ -92,42 +62,6 @@ public class FabricPlatform extends AbstractFabricPlatform { return new FutureTaskId(CompletableFuture.runAsync(runnable, getServer())); } - private FutureTaskId runEventLoop(Runnable runnable) { - return new FutureTaskId( - ViaFabric.EVENT_LOOP - .submit(runnable) - .addListener(errorLogger()) - ); - } - - @Override - public FutureTaskId runSync(Runnable runnable, long ticks) { - // ViaVersion seems to not need to run delayed tasks on main thread - return new FutureTaskId( - ViaFabric.EVENT_LOOP - .schedule(() -> runSync(runnable), ticks * 50, TimeUnit.MILLISECONDS) - .addListener(errorLogger()) - ); - } - - @Override - public FutureTaskId runRepeatingSync(Runnable runnable, long ticks) { - // ViaVersion seems to not need to run repeating tasks on main thread - return new FutureTaskId( - ViaFabric.EVENT_LOOP - .scheduleAtFixedRate(() -> runSync(runnable), 0, ticks * 50, TimeUnit.MILLISECONDS) - .addListener(errorLogger()) - ); - } - - private > GenericFutureListener errorLogger() { - return future -> { - if (!future.isCancelled() && future.cause() != null) { - future.cause().printStackTrace(); - } - }; - } - @Override public ViaCommandSender[] getOnlinePlayers() { MinecraftServer server = getServer(); @@ -157,7 +91,7 @@ public class FabricPlatform extends AbstractFabricPlatform { runServerSync(() -> { ServerPlayerEntity player = server.getPlayerManager().getPlayer(uuid); if (player == null) return; - player.sendChatMessage(Text.Serializer.fromJson(legacyToJson(s)), MessageType.SYSTEM); + player.sendChatMessage(Text.Serializer.fromJson(RemappingUtil.legacyToJson(s)), MessageType.SYSTEM); }); } @@ -172,7 +106,7 @@ public class FabricPlatform extends AbstractFabricPlatform { Supplier kickTask = () -> { ServerPlayerEntity player = server.getPlayerManager().getPlayer(uuid); if (player == null) return false; - player.networkHandler.disconnect(Text.Serializer.fromJson(legacyToJson(s))); + player.networkHandler.disconnect(Text.Serializer.fromJson(RemappingUtil.legacyToJson(s))); return true; }; if (server.isOnThread()) { diff --git a/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/providers/VRHandItemProvider.java b/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/providers/VRHandItemProvider.java index 8a79567..b7dde63 100644 --- a/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/providers/VRHandItemProvider.java +++ b/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/providers/VRHandItemProvider.java @@ -1,21 +1,21 @@ package com.viaversion.fabric.mc115.providers; +import com.viaversion.fabric.common.util.RemappingUtil; import com.viaversion.fabric.mc115.ViaFabric; -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; -import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; -import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; -import net.fabricmc.fabric.api.event.world.WorldTickCallback; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.network.ClientPlayerEntity; -import net.minecraft.item.ItemStack; -import net.minecraft.util.registry.Registry; -import net.minecraft.util.Identifier; -import net.minecraft.world.World; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.item.DataItem; import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.protocols.protocol1_9to1_8.providers.HandItemProvider; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.network.ClientPlayerEntity; +import net.minecraft.item.ItemStack; +import net.minecraft.util.Identifier; +import net.minecraft.util.registry.Registry; +import net.minecraft.world.World; import java.util.Map; import java.util.UUID; @@ -23,7 +23,7 @@ import java.util.concurrent.ConcurrentHashMap; public class VRHandItemProvider extends HandItemProvider { public Item clientItem = null; - public Map serverPlayers = new ConcurrentHashMap<>(); + public final Map serverPlayers = new ConcurrentHashMap<>(); @Override public Item getHandItem(UserConnection info) { @@ -48,15 +48,7 @@ public class VRHandItemProvider extends HandItemProvider { try { ClientTickEvents.END_WORLD_TICK.register(clientWorld -> tickClient()); } catch (NoClassDefFoundError ignored) { - try { - WorldTickCallback.EVENT.register(world -> { - if (world.isClient) { - tickClient(); - } - }); - } catch (NoClassDefFoundError ignored2) { - ViaFabric.JLOGGER.info("Fabric Lifecycle V0/V1 isn't installed"); - } + ViaFabric.JLOGGER.info("Fabric Lifecycle V1 isn't installed"); } } @@ -64,11 +56,6 @@ public class VRHandItemProvider extends HandItemProvider { try { ServerTickEvents.END_WORLD_TICK.register(this::tickServer); } catch (NoClassDefFoundError ignored) { - WorldTickCallback.EVENT.register(world -> { - if (!world.isClient) { - tickServer(world); - } - }); } } @@ -87,25 +74,7 @@ public class VRHandItemProvider extends HandItemProvider { private Item fromNative(ItemStack original) { Identifier iid = Registry.ITEM.getId(original.getItem()); - if (iid == null) return new DataItem(0, (byte) 0, (short) 0, null); - int id = swordId(iid.toString()); + int id = RemappingUtil.swordId(iid.toString()); return new DataItem(id, (byte) original.getCount(), (short) original.getDamage(), null); } - - private int swordId(String id) { - // https://github.com/ViaVersion/ViaVersion/blob/8de26a0ad33f5b739f5394ed80f69d14197fddc7/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/Protocol1_9To1_8.java#L86 - switch (id) { - case "minecraft:iron_sword": - return 267; - case "minecraft:wooden_sword": - return 268; - case "minecraft:golden_sword": - return 272; - case "minecraft:diamond_sword": - return 276; - case "minecraft:stone_sword": - return 283; - } - return 0; - } } diff --git a/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/service/ProtocolAutoDetector.java b/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/service/ProtocolAutoDetector.java index 7f78444..87c795a 100644 --- a/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/service/ProtocolAutoDetector.java +++ b/viafabric-mc115/src/main/java/com/viaversion/fabric/mc115/service/ProtocolAutoDetector.java @@ -32,7 +32,7 @@ import java.util.logging.Level; @Environment(EnvType.CLIENT) public class ProtocolAutoDetector { - private static LoadingCache> SERVER_VER = CacheBuilder.newBuilder() + private static final LoadingCache> SERVER_VER = CacheBuilder.newBuilder() .expireAfterWrite(30, TimeUnit.SECONDS) .build(CacheLoader.from((address) -> { CompletableFuture future = new CompletableFuture<>(); diff --git a/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/ViaFabric.java b/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/ViaFabric.java index 04f7bef..280d856 100644 --- a/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/ViaFabric.java +++ b/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/ViaFabric.java @@ -1,17 +1,21 @@ package com.viaversion.fabric.mc116; -import com.viaversion.fabric.common.config.VFConfig; -import com.viaversion.fabric.common.platform.FabricInjector; -import com.viaversion.fabric.mc116.commands.VRCommandHandler; -import com.viaversion.fabric.mc116.platform.VFLoader; -import com.viaversion.fabric.mc116.platform.FabricPlatform; -import com.viaversion.fabric.common.protocol.HostnameParserProtocol; -import com.viaversion.fabric.common.util.JLoggerToLog4j; import com.google.common.collect.Range; import com.google.common.util.concurrent.ThreadFactoryBuilder; import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.RequiredArgumentBuilder; +import com.viaversion.fabric.common.config.VFConfig; +import com.viaversion.fabric.common.platform.FabricInjector; +import com.viaversion.fabric.common.protocol.HostnameParserProtocol; +import com.viaversion.fabric.common.util.JLoggerToLog4j; +import com.viaversion.fabric.mc116.commands.VRCommandHandler; +import com.viaversion.fabric.mc116.platform.FabricPlatform; +import com.viaversion.fabric.mc116.platform.VFLoader; +import com.viaversion.viaversion.ViaManagerImpl; +import com.viaversion.viaversion.api.Via; +import com.viaversion.viaversion.api.data.MappingDataLoader; +import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; import io.netty.channel.DefaultEventLoop; import io.netty.channel.EventLoop; import net.fabricmc.api.EnvType; @@ -21,10 +25,6 @@ import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback; import net.fabricmc.loader.api.FabricLoader; import net.minecraft.command.CommandSource; import org.apache.logging.log4j.LogManager; -import com.viaversion.viaversion.ViaManagerImpl; -import com.viaversion.viaversion.api.Via; -import com.viaversion.viaversion.api.data.MappingDataLoader; -import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; @@ -36,7 +36,7 @@ public class ViaFabric implements ModInitializer { public static final Logger JLOGGER = new JLoggerToLog4j(LogManager.getLogger("ViaFabric")); public static final ExecutorService ASYNC_EXECUTOR; public static final EventLoop EVENT_LOOP; - public static CompletableFuture INIT_FUTURE = new CompletableFuture<>(); + public static final CompletableFuture INIT_FUTURE = new CompletableFuture<>(); public static VFConfig config; static { @@ -74,11 +74,7 @@ public class ViaFabric implements ModInitializer { FabricLoader.getInstance().getEntrypoints("viafabric:via_api_initialized", Runnable.class).forEach(Runnable::run); - try { - registerCommandsV1(); - } catch (NoClassDefFoundError ignored) { - JLOGGER.info("Couldn't register command as Fabric Commands isn't installed"); - } + registerCommandsV1(); config = new VFConfig(FabricLoader.getInstance().getConfigDir().resolve("ViaFabric") .resolve("viafabric.yml").toFile()); @@ -87,11 +83,15 @@ public class ViaFabric implements ModInitializer { } private void registerCommandsV1() { - CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> dispatcher.register(command("viaversion"))); - CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> dispatcher.register(command("viaver"))); - CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> dispatcher.register(command("vvfabric"))); - if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) { - ClientCommandManager.DISPATCHER.register(command("viafabricclient")); + try { + CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> dispatcher.register(command("viaversion"))); + CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> dispatcher.register(command("viaver"))); + CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> dispatcher.register(command("vvfabric"))); + if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) { + ClientCommandManager.DISPATCHER.register(command("viafabricclient")); + } + } catch (NoClassDefFoundError ignored) { + JLOGGER.info("Couldn't register command as Fabric Commands V1 isn't installed"); } } } diff --git a/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/commands/NMSCommandSender.java b/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/commands/NMSCommandSender.java index 6af54cf..2a01d5c 100644 --- a/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/commands/NMSCommandSender.java +++ b/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/commands/NMSCommandSender.java @@ -1,14 +1,12 @@ package com.viaversion.fabric.mc116.commands; -import com.viaversion.fabric.mc116.platform.FabricPlatform; +import com.viaversion.fabric.common.util.RemappingUtil; +import com.viaversion.viaversion.api.command.ViaCommandSender; import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource; import net.minecraft.command.CommandSource; import net.minecraft.entity.Entity; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.text.Text; -import com.viaversion.viaversion.api.command.ViaCommandSender; -import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.gson.GsonComponentSerializer; -import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import java.util.UUID; @@ -28,16 +26,12 @@ public class NMSCommandSender implements ViaCommandSender { @Override public void sendMessage(String s) { if (source instanceof ServerCommandSource) { - ((ServerCommandSource) source).sendFeedback(Text.Serializer.fromJson(FabricPlatform.legacyToJson(s)), false); + ((ServerCommandSource) source).sendFeedback(Text.Serializer.fromJson(RemappingUtil.legacyToJson(s)), false); } else if (source instanceof FabricClientCommandSource) { - ((FabricClientCommandSource) source).sendFeedback(Text.Serializer.fromJson(FabricPlatform.legacyToJson(s))); + ((FabricClientCommandSource) source).sendFeedback(Text.Serializer.fromJson(RemappingUtil.legacyToJson(s))); } } - private String legacyToJson(String legacy) { - return GsonComponentSerializer.gson().serialize(LegacyComponentSerializer.legacySection().deserialize(legacy)); - } - @Override public UUID getUUID() { if (source instanceof ServerCommandSource) { diff --git a/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/gui/ViaConfigScreen.java b/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/gui/ViaConfigScreen.java index 02794aa..244421a 100644 --- a/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/gui/ViaConfigScreen.java +++ b/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/gui/ViaConfigScreen.java @@ -2,7 +2,6 @@ package com.viaversion.fabric.mc116.gui; import com.viaversion.fabric.mc116.ViaFabric; import com.viaversion.fabric.common.util.ProtocolUtils; -import com.viaversion.viaversion.api.Via; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.MinecraftClient; diff --git a/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/platform/FabricPlatform.java b/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/platform/FabricPlatform.java index d971e9f..6983d64 100644 --- a/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/platform/FabricPlatform.java +++ b/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/platform/FabricPlatform.java @@ -3,14 +3,12 @@ package com.viaversion.fabric.mc116.platform; import com.viaversion.fabric.common.commands.UserCommandSender; import com.viaversion.fabric.common.provider.AbstractFabricPlatform; import com.viaversion.fabric.common.util.FutureTaskId; +import com.viaversion.fabric.common.util.RemappingUtil; import com.viaversion.fabric.mc116.ViaFabric; import com.viaversion.fabric.mc116.commands.NMSCommandSender; import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.command.ViaCommandSender; -import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.gson.GsonComponentSerializer; -import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; -import io.netty.util.concurrent.Future; -import io.netty.util.concurrent.GenericFutureListener; +import io.netty.channel.EventLoop; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.fabricmc.loader.api.FabricLoader; @@ -21,9 +19,8 @@ import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; import java.util.UUID; -import java.util.concurrent.CancellationException; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.ExecutorService; import java.util.function.Supplier; import java.util.logging.Level; @@ -40,21 +37,14 @@ public class FabricPlatform extends AbstractFabricPlatform { return MinecraftClient.getInstance().getServer(); } - public static String legacyToJson(String legacy) { - return GsonComponentSerializer.gson().serialize(LegacyComponentSerializer.legacySection().deserialize(legacy)); + @Override + protected ExecutorService asyncService() { + return ViaFabric.ASYNC_EXECUTOR; } @Override - public FutureTaskId runAsync(Runnable runnable) { - return new FutureTaskId(CompletableFuture - .runAsync(runnable, ViaFabric.ASYNC_EXECUTOR) - .exceptionally(throwable -> { - if (!(throwable instanceof CancellationException)) { - throwable.printStackTrace(); - } - return null; - }) - ); + protected EventLoop eventLoop() { + return ViaFabric.EVENT_LOOP; } @Override @@ -71,42 +61,6 @@ public class FabricPlatform extends AbstractFabricPlatform { return new FutureTaskId(CompletableFuture.runAsync(runnable, getServer())); } - private FutureTaskId runEventLoop(Runnable runnable) { - return new FutureTaskId( - ViaFabric.EVENT_LOOP - .submit(runnable) - .addListener(errorLogger()) - ); - } - - @Override - public FutureTaskId runSync(Runnable runnable, long ticks) { - // ViaVersion seems to not need to run delayed tasks on main thread - return new FutureTaskId( - ViaFabric.EVENT_LOOP - .schedule(() -> runSync(runnable), ticks * 50, TimeUnit.MILLISECONDS) - .addListener(errorLogger()) - ); - } - - @Override - public FutureTaskId runRepeatingSync(Runnable runnable, long ticks) { - // ViaVersion seems to not need to run repeating tasks on main thread - return new FutureTaskId( - ViaFabric.EVENT_LOOP - .scheduleAtFixedRate(() -> runSync(runnable), 0, ticks * 50, TimeUnit.MILLISECONDS) - .addListener(errorLogger()) - ); - } - - private > GenericFutureListener errorLogger() { - return future -> { - if (!future.isCancelled() && future.cause() != null) { - future.cause().printStackTrace(); - } - }; - } - @Override public ViaCommandSender[] getOnlinePlayers() { MinecraftServer server = getServer(); @@ -137,7 +91,7 @@ public class FabricPlatform extends AbstractFabricPlatform { ServerPlayerEntity player = server.getPlayerManager().getPlayer(uuid); if (player == null) return; player.sendMessage(Text.Serializer.fromJson( - legacyToJson(s) + RemappingUtil.legacyToJson(s) ), false); }); } @@ -153,7 +107,7 @@ public class FabricPlatform extends AbstractFabricPlatform { Supplier kickTask = () -> { ServerPlayerEntity player = server.getPlayerManager().getPlayer(uuid); if (player == null) return false; - player.networkHandler.disconnect(Text.Serializer.fromJson(legacyToJson(s))); + player.networkHandler.disconnect(Text.Serializer.fromJson(RemappingUtil.legacyToJson(s))); return true; }; if (server.isOnThread()) { diff --git a/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/providers/VRHandItemProvider.java b/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/providers/VRHandItemProvider.java index 25995a9..c1c231b 100644 --- a/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/providers/VRHandItemProvider.java +++ b/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/providers/VRHandItemProvider.java @@ -1,21 +1,21 @@ package com.viaversion.fabric.mc116.providers; +import com.viaversion.fabric.common.util.RemappingUtil; import com.viaversion.fabric.mc116.ViaFabric; -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; -import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; -import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; -import net.fabricmc.fabric.api.event.world.WorldTickCallback; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.network.ClientPlayerEntity; -import net.minecraft.item.ItemStack; -import net.minecraft.util.registry.Registry; -import net.minecraft.util.Identifier; -import net.minecraft.world.World; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.item.DataItem; import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.protocols.protocol1_9to1_8.providers.HandItemProvider; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.network.ClientPlayerEntity; +import net.minecraft.item.ItemStack; +import net.minecraft.util.Identifier; +import net.minecraft.util.registry.Registry; +import net.minecraft.world.World; import java.util.Map; import java.util.UUID; @@ -23,7 +23,7 @@ import java.util.concurrent.ConcurrentHashMap; public class VRHandItemProvider extends HandItemProvider { public Item clientItem = null; - public Map serverPlayers = new ConcurrentHashMap<>(); + public final Map serverPlayers = new ConcurrentHashMap<>(); @Override public Item getHandItem(UserConnection info) { @@ -48,15 +48,7 @@ public class VRHandItemProvider extends HandItemProvider { try { ClientTickEvents.END_WORLD_TICK.register(clientWorld -> tickClient()); } catch (NoClassDefFoundError ignored) { - try { - WorldTickCallback.EVENT.register(world -> { - if (world.isClient) { - tickClient(); - } - }); - } catch (NoClassDefFoundError ignored2) { - ViaFabric.JLOGGER.info("Fabric Lifecycle V0/V1 isn't installed"); - } + ViaFabric.JLOGGER.info("Fabric Lifecycle V1 isn't installed"); } } @@ -64,11 +56,6 @@ public class VRHandItemProvider extends HandItemProvider { try { ServerTickEvents.END_WORLD_TICK.register(this::tickServer); } catch (NoClassDefFoundError ignored) { - WorldTickCallback.EVENT.register(world -> { - if (!world.isClient) { - tickServer(world); - } - }); } } @@ -87,25 +74,7 @@ public class VRHandItemProvider extends HandItemProvider { private Item fromNative(ItemStack original) { Identifier iid = Registry.ITEM.getId(original.getItem()); - if (iid == null) return new DataItem(0, (byte) 0, (short) 0, null); - int id = swordId(iid.toString()); + int id = RemappingUtil.swordId(iid.toString()); return new DataItem(id, (byte) original.getCount(), (short) original.getDamage(), null); } - - private int swordId(String id) { - // https://github.com/ViaVersion/ViaVersion/blob/8de26a0ad33f5b739f5394ed80f69d14197fddc7/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/Protocol1_9To1_8.java#L86 - switch (id) { - case "minecraft:iron_sword": - return 267; - case "minecraft:wooden_sword": - return 268; - case "minecraft:golden_sword": - return 272; - case "minecraft:diamond_sword": - return 276; - case "minecraft:stone_sword": - return 283; - } - return 0; - } } diff --git a/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/service/ProtocolAutoDetector.java b/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/service/ProtocolAutoDetector.java index 9f1e527..a4b3084 100644 --- a/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/service/ProtocolAutoDetector.java +++ b/viafabric-mc116/src/main/java/com/viaversion/fabric/mc116/service/ProtocolAutoDetector.java @@ -32,7 +32,7 @@ import java.util.logging.Level; @Environment(EnvType.CLIENT) public class ProtocolAutoDetector { - private static LoadingCache> SERVER_VER = CacheBuilder.newBuilder() + private static final LoadingCache> SERVER_VER = CacheBuilder.newBuilder() .expireAfterWrite(30, TimeUnit.SECONDS) .build(CacheLoader.from((address) -> { CompletableFuture future = new CompletableFuture<>(); diff --git a/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/ViaFabric.java b/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/ViaFabric.java index 9974448..be79953 100644 --- a/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/ViaFabric.java +++ b/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/ViaFabric.java @@ -10,8 +10,6 @@ import com.viaversion.fabric.common.platform.FabricInjector; import com.viaversion.fabric.common.protocol.HostnameParserProtocol; import com.viaversion.fabric.common.util.JLoggerToLog4j; import com.viaversion.fabric.mc117.commands.VRCommandHandler; -import com.viaversion.fabric.mc117.gui.ViaConfigScreen; -import com.viaversion.fabric.mc117.mixin.gui.client.ScreenAccessor; import com.viaversion.fabric.mc117.platform.FabricPlatform; import com.viaversion.fabric.mc117.platform.VFLoader; import com.viaversion.viaversion.ViaManagerImpl; @@ -23,16 +21,9 @@ import io.netty.channel.EventLoop; import net.fabricmc.api.EnvType; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.client.command.v1.ClientCommandManager; -import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents; import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback; import net.fabricmc.loader.api.FabricLoader; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gui.screen.multiplayer.MultiplayerScreen; -import net.minecraft.client.gui.widget.ButtonWidget; -import net.minecraft.client.gui.widget.TexturedButtonWidget; import net.minecraft.command.CommandSource; -import net.minecraft.text.TranslatableText; -import net.minecraft.util.Identifier; import org.apache.logging.log4j.LogManager; import java.util.concurrent.CompletableFuture; @@ -45,7 +36,7 @@ public class ViaFabric implements ModInitializer { public static final Logger JLOGGER = new JLoggerToLog4j(LogManager.getLogger("ViaFabric")); public static final ExecutorService ASYNC_EXECUTOR; public static final EventLoop EVENT_LOOP; - public static CompletableFuture INIT_FUTURE = new CompletableFuture<>(); + public static final CompletableFuture INIT_FUTURE = new CompletableFuture<>(); public static VFConfig config; static { @@ -84,7 +75,6 @@ public class ViaFabric implements ModInitializer { FabricLoader.getInstance().getEntrypoints("viafabric:via_api_initialized", Runnable.class).forEach(Runnable::run); registerCommandsV1(); - registerGui(); config = new VFConfig(FabricLoader.getInstance().getConfigDir().resolve("ViaFabric") .resolve("viafabric.yml").toFile()); @@ -92,27 +82,6 @@ public class ViaFabric implements ModInitializer { INIT_FUTURE.complete(null); } - private void registerGui() { - if (FabricLoader.getInstance().getEnvironmentType() != EnvType.CLIENT) return; - try { - ScreenEvents.AFTER_INIT.register((client, screen, scaledWidth, scaledHeight) -> { - if (!(screen instanceof MultiplayerScreen)) return; - ButtonWidget enableClientSideViaVersion = new TexturedButtonWidget(scaledWidth / 2 + 113, 10, - 40, 20, // Size - 0, 0, // Start pos of texture - 20, // v Hover offset - new Identifier("viafabric:textures/gui/widgets.png"), - 256, 256, // Texture size - it -> MinecraftClient.getInstance().setScreen(new ViaConfigScreen(screen)), - new TranslatableText("gui.via_button")); - if (ViaFabric.config.isHideButton()) enableClientSideViaVersion.visible = false; - ((ScreenAccessor) screen).callAddDrawableChild(enableClientSideViaVersion); - }); - } catch (NoClassDefFoundError ignored) { - JLOGGER.info("Couldn't register screen handler as Fabric Screen isn't installed"); - } - } - private void registerCommandsV1() { try { CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> dispatcher.register(command("viaversion"))); @@ -122,7 +91,7 @@ public class ViaFabric implements ModInitializer { ClientCommandManager.DISPATCHER.register(command("viafabricclient")); } } catch (NoClassDefFoundError ignored) { - JLOGGER.info("Couldn't register command as Fabric Commands isn't installed"); + JLOGGER.info("Couldn't register command as Fabric Commands V1 isn't installed"); } } } diff --git a/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/ViaFabricClient.java b/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/ViaFabricClient.java new file mode 100644 index 0000000..41fcdce --- /dev/null +++ b/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/ViaFabricClient.java @@ -0,0 +1,39 @@ +package com.viaversion.fabric.mc117; + +import com.viaversion.fabric.mc117.gui.ViaConfigScreen; +import com.viaversion.fabric.mc117.mixin.gui.client.ScreenAccessor; +import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.screen.multiplayer.MultiplayerScreen; +import net.minecraft.client.gui.widget.ButtonWidget; +import net.minecraft.client.gui.widget.TexturedButtonWidget; +import net.minecraft.text.TranslatableText; +import net.minecraft.util.Identifier; + +public class ViaFabricClient implements ClientModInitializer { + @Override + public void onInitializeClient() { + registerGui(); + } + + private void registerGui() { + try { + ScreenEvents.AFTER_INIT.register((client, screen, scaledWidth, scaledHeight) -> { + if (!(screen instanceof MultiplayerScreen)) return; + ButtonWidget enableClientSideViaVersion = new TexturedButtonWidget(scaledWidth / 2 + 113, 10, + 40, 20, // Size + 0, 0, // Start pos of texture + 20, // v Hover offset + new Identifier("viafabric:textures/gui/widgets.png"), + 256, 256, // Texture size + it -> MinecraftClient.getInstance().setScreen(new ViaConfigScreen(screen)), + new TranslatableText("gui.via_button")); + if (ViaFabric.config.isHideButton()) enableClientSideViaVersion.visible = false; + ((ScreenAccessor) screen).callAddDrawableChild(enableClientSideViaVersion); + }); + } catch (NoClassDefFoundError ignored) { + ViaFabric.JLOGGER.info("Couldn't register screen handler as Fabric Screen isn't installed"); + } + } +} diff --git a/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/commands/NMSCommandSender.java b/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/commands/NMSCommandSender.java index 99188a9..4145109 100644 --- a/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/commands/NMSCommandSender.java +++ b/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/commands/NMSCommandSender.java @@ -1,5 +1,6 @@ package com.viaversion.fabric.mc117.commands; +import com.viaversion.fabric.common.util.RemappingUtil; import com.viaversion.fabric.mc117.platform.FabricPlatform; import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource; import net.minecraft.command.CommandSource; @@ -26,9 +27,9 @@ public class NMSCommandSender implements ViaCommandSender { @Override public void sendMessage(String s) { if (source instanceof ServerCommandSource) { - ((ServerCommandSource) source).sendFeedback(Text.Serializer.fromJson(FabricPlatform.legacyToJson(s)), false); + ((ServerCommandSource) source).sendFeedback(Text.Serializer.fromJson(RemappingUtil.legacyToJson(s)), false); } else if (source instanceof FabricClientCommandSource) { - ((FabricClientCommandSource) source).sendFeedback(Text.Serializer.fromJson(FabricPlatform.legacyToJson(s))); + ((FabricClientCommandSource) source).sendFeedback(Text.Serializer.fromJson(RemappingUtil.legacyToJson(s))); } } diff --git a/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/gui/ViaConfigScreen.java b/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/gui/ViaConfigScreen.java index b6922e8..78f73d0 100644 --- a/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/gui/ViaConfigScreen.java +++ b/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/gui/ViaConfigScreen.java @@ -2,7 +2,6 @@ package com.viaversion.fabric.mc117.gui; import com.viaversion.fabric.mc117.ViaFabric; import com.viaversion.fabric.common.util.ProtocolUtils; -import com.viaversion.viaversion.api.Via; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.MinecraftClient; diff --git a/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/platform/FabricPlatform.java b/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/platform/FabricPlatform.java index 62e3e06..d024295 100644 --- a/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/platform/FabricPlatform.java +++ b/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/platform/FabricPlatform.java @@ -3,14 +3,12 @@ package com.viaversion.fabric.mc117.platform; import com.viaversion.fabric.common.commands.UserCommandSender; import com.viaversion.fabric.common.provider.AbstractFabricPlatform; import com.viaversion.fabric.common.util.FutureTaskId; +import com.viaversion.fabric.common.util.RemappingUtil; import com.viaversion.fabric.mc117.ViaFabric; import com.viaversion.fabric.mc117.commands.NMSCommandSender; import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.command.ViaCommandSender; -import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.gson.GsonComponentSerializer; -import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; -import io.netty.util.concurrent.Future; -import io.netty.util.concurrent.GenericFutureListener; +import io.netty.channel.EventLoop; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.fabricmc.loader.api.FabricLoader; @@ -21,9 +19,8 @@ import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; import java.util.UUID; -import java.util.concurrent.CancellationException; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.ExecutorService; import java.util.function.Supplier; import java.util.logging.Level; @@ -40,23 +37,6 @@ public class FabricPlatform extends AbstractFabricPlatform { return MinecraftClient.getInstance().getServer(); } - public static String legacyToJson(String legacy) { - return GsonComponentSerializer.gson().serialize(LegacyComponentSerializer.legacySection().deserialize(legacy)); - } - - @Override - public FutureTaskId runAsync(Runnable runnable) { - return new FutureTaskId(CompletableFuture - .runAsync(runnable, ViaFabric.ASYNC_EXECUTOR) - .exceptionally(throwable -> { - if (!(throwable instanceof CancellationException)) { - throwable.printStackTrace(); - } - return null; - }) - ); - } - @Override public FutureTaskId runSync(Runnable runnable) { if (getServer() != null) { @@ -71,42 +51,6 @@ public class FabricPlatform extends AbstractFabricPlatform { return new FutureTaskId(CompletableFuture.runAsync(runnable, getServer())); } - private FutureTaskId runEventLoop(Runnable runnable) { - return new FutureTaskId( - ViaFabric.EVENT_LOOP - .submit(runnable) - .addListener(errorLogger()) - ); - } - - @Override - public FutureTaskId runSync(Runnable runnable, long ticks) { - // ViaVersion seems to not need to run delayed tasks on main thread - return new FutureTaskId( - ViaFabric.EVENT_LOOP - .schedule(() -> runSync(runnable), ticks * 50, TimeUnit.MILLISECONDS) - .addListener(errorLogger()) - ); - } - - @Override - public FutureTaskId runRepeatingSync(Runnable runnable, long ticks) { - // ViaVersion seems to not need to run repeating tasks on main thread - return new FutureTaskId( - ViaFabric.EVENT_LOOP - .scheduleAtFixedRate(() -> runSync(runnable), 0, ticks * 50, TimeUnit.MILLISECONDS) - .addListener(errorLogger()) - ); - } - - private > GenericFutureListener errorLogger() { - return future -> { - if (!future.isCancelled() && future.cause() != null) { - future.cause().printStackTrace(); - } - }; - } - @Override public ViaCommandSender[] getOnlinePlayers() { MinecraftServer server = getServer(); @@ -137,7 +81,7 @@ public class FabricPlatform extends AbstractFabricPlatform { ServerPlayerEntity player = server.getPlayerManager().getPlayer(uuid); if (player == null) return; player.sendMessage(Text.Serializer.fromJson( - legacyToJson(s) + RemappingUtil.legacyToJson(s) ), false); }); } @@ -153,7 +97,7 @@ public class FabricPlatform extends AbstractFabricPlatform { Supplier kickTask = () -> { ServerPlayerEntity player = server.getPlayerManager().getPlayer(uuid); if (player == null) return false; - player.networkHandler.disconnect(Text.Serializer.fromJson(legacyToJson(s))); + player.networkHandler.disconnect(Text.Serializer.fromJson(RemappingUtil.legacyToJson(s))); return true; }; if (server.isOnThread()) { @@ -164,4 +108,14 @@ public class FabricPlatform extends AbstractFabricPlatform { } return false; // Can't know if it worked } + + @Override + protected ExecutorService asyncService() { + return ViaFabric.ASYNC_EXECUTOR; + } + + @Override + protected EventLoop eventLoop() { + return ViaFabric.EVENT_LOOP; + } } diff --git a/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/providers/VRHandItemProvider.java b/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/providers/VRHandItemProvider.java index 24b21bb..b576827 100644 --- a/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/providers/VRHandItemProvider.java +++ b/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/providers/VRHandItemProvider.java @@ -1,29 +1,29 @@ package com.viaversion.fabric.mc117.providers; +import com.viaversion.fabric.common.util.RemappingUtil; import com.viaversion.fabric.mc117.ViaFabric; -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; -import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; -import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; -import net.fabricmc.fabric.api.event.world.WorldTickCallback; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.network.ClientPlayerEntity; -import net.minecraft.item.ItemStack; -import net.minecraft.util.registry.Registry; -import net.minecraft.util.Identifier; -import net.minecraft.world.World; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.item.DataItem; import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.protocols.protocol1_9to1_8.providers.HandItemProvider; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.network.ClientPlayerEntity; +import net.minecraft.item.ItemStack; +import net.minecraft.util.Identifier; +import net.minecraft.util.registry.Registry; +import net.minecraft.world.World; import java.util.Map; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; public class VRHandItemProvider extends HandItemProvider { + public final Map serverPlayers = new ConcurrentHashMap<>(); public Item clientItem = null; - public Map serverPlayers = new ConcurrentHashMap<>(); @Override public Item getHandItem(UserConnection info) { @@ -48,15 +48,7 @@ public class VRHandItemProvider extends HandItemProvider { try { ClientTickEvents.END_WORLD_TICK.register(clientWorld -> tickClient()); } catch (NoClassDefFoundError ignored) { - try { - WorldTickCallback.EVENT.register(world -> { - if (world.isClient) { - tickClient(); - } - }); - } catch (NoClassDefFoundError ignored2) { - ViaFabric.JLOGGER.info("Fabric Lifecycle V0/V1 isn't installed"); - } + ViaFabric.JLOGGER.info("Fabric Lifecycle V1 isn't installed"); } } @@ -64,11 +56,6 @@ public class VRHandItemProvider extends HandItemProvider { try { ServerTickEvents.END_WORLD_TICK.register(this::tickServer); } catch (NoClassDefFoundError ignored) { - WorldTickCallback.EVENT.register(world -> { - if (!world.isClient) { - tickServer(world); - } - }); } } @@ -87,25 +74,7 @@ public class VRHandItemProvider extends HandItemProvider { private Item fromNative(ItemStack original) { Identifier iid = Registry.ITEM.getId(original.getItem()); - if (iid == null) return new DataItem(0, (byte) 0, (short) 0, null); - int id = swordId(iid.toString()); + int id = RemappingUtil.swordId(iid.toString()); return new DataItem(id, (byte) original.getCount(), (short) original.getDamage(), null); } - - private int swordId(String id) { - // https://github.com/ViaVersion/ViaVersion/blob/8de26a0ad33f5b739f5394ed80f69d14197fddc7/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/Protocol1_9To1_8.java#L86 - switch (id) { - case "minecraft:iron_sword": - return 267; - case "minecraft:wooden_sword": - return 268; - case "minecraft:golden_sword": - return 272; - case "minecraft:diamond_sword": - return 276; - case "minecraft:stone_sword": - return 283; - } - return 0; - } } diff --git a/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/service/ProtocolAutoDetector.java b/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/service/ProtocolAutoDetector.java index 16fe0ae..b390bbe 100644 --- a/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/service/ProtocolAutoDetector.java +++ b/viafabric-mc117/src/main/java/com/viaversion/fabric/mc117/service/ProtocolAutoDetector.java @@ -32,7 +32,7 @@ import java.util.logging.Level; @Environment(EnvType.CLIENT) public class ProtocolAutoDetector { - private static LoadingCache> SERVER_VER = CacheBuilder.newBuilder() + private static final LoadingCache> SERVER_VER = CacheBuilder.newBuilder() .expireAfterWrite(30, TimeUnit.SECONDS) .build(CacheLoader.from((address) -> { CompletableFuture future = new CompletableFuture<>(); diff --git a/viafabric-mc117/src/main/resources/fabric.mod.json b/viafabric-mc117/src/main/resources/fabric.mod.json index 51e725f..c1086dc 100644 --- a/viafabric-mc117/src/main/resources/fabric.mod.json +++ b/viafabric-mc117/src/main/resources/fabric.mod.json @@ -31,6 +31,9 @@ "main": [ "com.viaversion.fabric.mc117.ViaFabric" ], + "client": [ + "com.viaversion.fabric.mc117.ViaFabricClient" + ], "modmenu": [ "com.viaversion.fabric.mc117.gui.ModMenuConfig" ] diff --git a/viafabric-mc118/src/main/java/com/viaversion/fabric/mc118/ViaFabric.java b/viafabric-mc118/src/main/java/com/viaversion/fabric/mc118/ViaFabric.java index 33c418f..5bb6891 100644 --- a/viafabric-mc118/src/main/java/com/viaversion/fabric/mc118/ViaFabric.java +++ b/viafabric-mc118/src/main/java/com/viaversion/fabric/mc118/ViaFabric.java @@ -10,8 +10,6 @@ import com.viaversion.fabric.common.platform.FabricInjector; import com.viaversion.fabric.common.protocol.HostnameParserProtocol; import com.viaversion.fabric.common.util.JLoggerToLog4j; import com.viaversion.fabric.mc118.commands.VRCommandHandler; -import com.viaversion.fabric.mc118.gui.ViaConfigScreen; -import com.viaversion.fabric.mc118.mixin.gui.client.ScreenAccessor; import com.viaversion.fabric.mc118.platform.FabricPlatform; import com.viaversion.fabric.mc118.platform.VFLoader; import com.viaversion.viaversion.ViaManagerImpl; @@ -23,16 +21,9 @@ import io.netty.channel.EventLoop; import net.fabricmc.api.EnvType; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.client.command.v1.ClientCommandManager; -import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents; import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback; import net.fabricmc.loader.api.FabricLoader; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gui.screen.multiplayer.MultiplayerScreen; -import net.minecraft.client.gui.widget.ButtonWidget; -import net.minecraft.client.gui.widget.TexturedButtonWidget; import net.minecraft.command.CommandSource; -import net.minecraft.text.TranslatableText; -import net.minecraft.util.Identifier; import org.apache.logging.log4j.LogManager; import java.util.concurrent.CompletableFuture; @@ -45,7 +36,7 @@ public class ViaFabric implements ModInitializer { public static final Logger JLOGGER = new JLoggerToLog4j(LogManager.getLogger("ViaFabric")); public static final ExecutorService ASYNC_EXECUTOR; public static final EventLoop EVENT_LOOP; - public static CompletableFuture INIT_FUTURE = new CompletableFuture<>(); + public static final CompletableFuture INIT_FUTURE = new CompletableFuture<>(); public static VFConfig config; static { @@ -84,7 +75,6 @@ public class ViaFabric implements ModInitializer { FabricLoader.getInstance().getEntrypoints("viafabric:via_api_initialized", Runnable.class).forEach(Runnable::run); registerCommandsV1(); - registerGui(); config = new VFConfig(FabricLoader.getInstance().getConfigDir().resolve("ViaFabric") .resolve("viafabric.yml").toFile()); @@ -92,26 +82,6 @@ public class ViaFabric implements ModInitializer { INIT_FUTURE.complete(null); } - private void registerGui() { - if (FabricLoader.getInstance().getEnvironmentType() != EnvType.CLIENT) return; - try { - ScreenEvents.AFTER_INIT.register((client, screen, scaledWidth, scaledHeight) -> { - if (!(screen instanceof MultiplayerScreen)) return; - ButtonWidget enableClientSideViaVersion = new TexturedButtonWidget(scaledWidth / 2 + 113, 10, - 40, 20, // Size - 0, 0, // Start pos of texture - 20, // v Hover offset - new Identifier("viafabric:textures/gui/widgets.png"), - 256, 256, // Texture size - it -> MinecraftClient.getInstance().setScreen(new ViaConfigScreen(screen)), - new TranslatableText("gui.via_button")); - if (ViaFabric.config.isHideButton()) enableClientSideViaVersion.visible = false; - ((ScreenAccessor) screen).callAddDrawableChild(enableClientSideViaVersion); - }); - } catch (NoClassDefFoundError ignored) { - JLOGGER.info("Couldn't register screen handler as Fabric Screen isn't installed"); - } - } private void registerCommandsV1() { try { @@ -122,7 +92,7 @@ public class ViaFabric implements ModInitializer { ClientCommandManager.DISPATCHER.register(command("viafabricclient")); } } catch (NoClassDefFoundError ignored) { - JLOGGER.info("Couldn't register command as Fabric Commands isn't installed"); + JLOGGER.info("Couldn't register command as Fabric Commands V1 isn't installed"); } } } diff --git a/viafabric-mc118/src/main/java/com/viaversion/fabric/mc118/ViaFabricClient.java b/viafabric-mc118/src/main/java/com/viaversion/fabric/mc118/ViaFabricClient.java new file mode 100644 index 0000000..4e56e2b --- /dev/null +++ b/viafabric-mc118/src/main/java/com/viaversion/fabric/mc118/ViaFabricClient.java @@ -0,0 +1,39 @@ +package com.viaversion.fabric.mc118; + +import com.viaversion.fabric.mc118.gui.ViaConfigScreen; +import com.viaversion.fabric.mc118.mixin.gui.client.ScreenAccessor; +import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.screen.multiplayer.MultiplayerScreen; +import net.minecraft.client.gui.widget.ButtonWidget; +import net.minecraft.client.gui.widget.TexturedButtonWidget; +import net.minecraft.text.TranslatableText; +import net.minecraft.util.Identifier; + +public class ViaFabricClient implements ClientModInitializer { + @Override + public void onInitializeClient() { + registerGui(); + } + + private void registerGui() { + try { + ScreenEvents.AFTER_INIT.register((client, screen, scaledWidth, scaledHeight) -> { + if (!(screen instanceof MultiplayerScreen)) return; + ButtonWidget enableClientSideViaVersion = new TexturedButtonWidget(scaledWidth / 2 + 113, 10, + 40, 20, // Size + 0, 0, // Start pos of texture + 20, // v Hover offset + new Identifier("viafabric:textures/gui/widgets.png"), + 256, 256, // Texture size + it -> MinecraftClient.getInstance().setScreen(new ViaConfigScreen(screen)), + new TranslatableText("gui.via_button")); + if (ViaFabric.config.isHideButton()) enableClientSideViaVersion.visible = false; + ((ScreenAccessor) screen).callAddDrawableChild(enableClientSideViaVersion); + }); + } catch (NoClassDefFoundError ignored) { + ViaFabric.JLOGGER.info("Couldn't register screen handler as Fabric Screen isn't installed"); + } + } +} diff --git a/viafabric-mc118/src/main/java/com/viaversion/fabric/mc118/commands/NMSCommandSender.java b/viafabric-mc118/src/main/java/com/viaversion/fabric/mc118/commands/NMSCommandSender.java index e786f2b..915ca5b 100644 --- a/viafabric-mc118/src/main/java/com/viaversion/fabric/mc118/commands/NMSCommandSender.java +++ b/viafabric-mc118/src/main/java/com/viaversion/fabric/mc118/commands/NMSCommandSender.java @@ -1,5 +1,6 @@ package com.viaversion.fabric.mc118.commands; +import com.viaversion.fabric.common.util.RemappingUtil; import com.viaversion.fabric.mc118.platform.FabricPlatform; import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource; import net.minecraft.command.CommandSource; @@ -26,9 +27,9 @@ public class NMSCommandSender implements ViaCommandSender { @Override public void sendMessage(String s) { if (source instanceof ServerCommandSource) { - ((ServerCommandSource) source).sendFeedback(Text.Serializer.fromJson(FabricPlatform.legacyToJson(s)), false); + ((ServerCommandSource) source).sendFeedback(Text.Serializer.fromJson(RemappingUtil.legacyToJson(s)), false); } else if (source instanceof FabricClientCommandSource) { - ((FabricClientCommandSource) source).sendFeedback(Text.Serializer.fromJson(FabricPlatform.legacyToJson(s))); + ((FabricClientCommandSource) source).sendFeedback(Text.Serializer.fromJson(RemappingUtil.legacyToJson(s))); } } diff --git a/viafabric-mc118/src/main/java/com/viaversion/fabric/mc118/gui/ViaConfigScreen.java b/viafabric-mc118/src/main/java/com/viaversion/fabric/mc118/gui/ViaConfigScreen.java index 6c1625e..2ba0a12 100644 --- a/viafabric-mc118/src/main/java/com/viaversion/fabric/mc118/gui/ViaConfigScreen.java +++ b/viafabric-mc118/src/main/java/com/viaversion/fabric/mc118/gui/ViaConfigScreen.java @@ -2,7 +2,6 @@ package com.viaversion.fabric.mc118.gui; import com.viaversion.fabric.mc118.ViaFabric; import com.viaversion.fabric.common.util.ProtocolUtils; -import com.viaversion.viaversion.api.Via; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.MinecraftClient; diff --git a/viafabric-mc118/src/main/java/com/viaversion/fabric/mc118/platform/FabricPlatform.java b/viafabric-mc118/src/main/java/com/viaversion/fabric/mc118/platform/FabricPlatform.java index 65cdc49..334dacb 100644 --- a/viafabric-mc118/src/main/java/com/viaversion/fabric/mc118/platform/FabricPlatform.java +++ b/viafabric-mc118/src/main/java/com/viaversion/fabric/mc118/platform/FabricPlatform.java @@ -3,14 +3,12 @@ package com.viaversion.fabric.mc118.platform; import com.viaversion.fabric.common.commands.UserCommandSender; import com.viaversion.fabric.common.provider.AbstractFabricPlatform; import com.viaversion.fabric.common.util.FutureTaskId; +import com.viaversion.fabric.common.util.RemappingUtil; import com.viaversion.fabric.mc118.ViaFabric; import com.viaversion.fabric.mc118.commands.NMSCommandSender; import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.command.ViaCommandSender; -import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.gson.GsonComponentSerializer; -import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; -import io.netty.util.concurrent.Future; -import io.netty.util.concurrent.GenericFutureListener; +import io.netty.channel.EventLoop; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.fabricmc.loader.api.FabricLoader; @@ -21,9 +19,8 @@ import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; import java.util.UUID; -import java.util.concurrent.CancellationException; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.ExecutorService; import java.util.function.Supplier; import java.util.logging.Level; @@ -40,23 +37,6 @@ public class FabricPlatform extends AbstractFabricPlatform { return MinecraftClient.getInstance().getServer(); } - public static String legacyToJson(String legacy) { - return GsonComponentSerializer.gson().serialize(LegacyComponentSerializer.legacySection().deserialize(legacy)); - } - - @Override - public FutureTaskId runAsync(Runnable runnable) { - return new FutureTaskId(CompletableFuture - .runAsync(runnable, ViaFabric.ASYNC_EXECUTOR) - .exceptionally(throwable -> { - if (!(throwable instanceof CancellationException)) { - throwable.printStackTrace(); - } - return null; - }) - ); - } - @Override public FutureTaskId runSync(Runnable runnable) { if (getServer() != null) { @@ -71,42 +51,6 @@ public class FabricPlatform extends AbstractFabricPlatform { return new FutureTaskId(CompletableFuture.runAsync(runnable, getServer())); } - private FutureTaskId runEventLoop(Runnable runnable) { - return new FutureTaskId( - ViaFabric.EVENT_LOOP - .submit(runnable) - .addListener(errorLogger()) - ); - } - - @Override - public FutureTaskId runSync(Runnable runnable, long ticks) { - // ViaVersion seems to not need to run delayed tasks on main thread - return new FutureTaskId( - ViaFabric.EVENT_LOOP - .schedule(() -> runSync(runnable), ticks * 50, TimeUnit.MILLISECONDS) - .addListener(errorLogger()) - ); - } - - @Override - public FutureTaskId runRepeatingSync(Runnable runnable, long ticks) { - // ViaVersion seems to not need to run repeating tasks on main thread - return new FutureTaskId( - ViaFabric.EVENT_LOOP - .scheduleAtFixedRate(() -> runSync(runnable), 0, ticks * 50, TimeUnit.MILLISECONDS) - .addListener(errorLogger()) - ); - } - - private > GenericFutureListener errorLogger() { - return future -> { - if (!future.isCancelled() && future.cause() != null) { - future.cause().printStackTrace(); - } - }; - } - @Override public ViaCommandSender[] getOnlinePlayers() { MinecraftServer server = getServer(); @@ -137,7 +81,7 @@ public class FabricPlatform extends AbstractFabricPlatform { ServerPlayerEntity player = server.getPlayerManager().getPlayer(uuid); if (player == null) return; player.sendMessage(Text.Serializer.fromJson( - legacyToJson(s) + RemappingUtil.legacyToJson(s) ), false); }); } @@ -153,7 +97,7 @@ public class FabricPlatform extends AbstractFabricPlatform { Supplier kickTask = () -> { ServerPlayerEntity player = server.getPlayerManager().getPlayer(uuid); if (player == null) return false; - player.networkHandler.disconnect(Text.Serializer.fromJson(legacyToJson(s))); + player.networkHandler.disconnect(Text.Serializer.fromJson(RemappingUtil.legacyToJson(s))); return true; }; if (server.isOnThread()) { @@ -164,4 +108,14 @@ public class FabricPlatform extends AbstractFabricPlatform { } return false; // Can't know if it worked } + + @Override + protected ExecutorService asyncService() { + return ViaFabric.ASYNC_EXECUTOR; + } + + @Override + protected EventLoop eventLoop() { + return ViaFabric.EVENT_LOOP; + } } diff --git a/viafabric-mc118/src/main/java/com/viaversion/fabric/mc118/providers/VRHandItemProvider.java b/viafabric-mc118/src/main/java/com/viaversion/fabric/mc118/providers/VRHandItemProvider.java index 59a52cb..f99f917 100644 --- a/viafabric-mc118/src/main/java/com/viaversion/fabric/mc118/providers/VRHandItemProvider.java +++ b/viafabric-mc118/src/main/java/com/viaversion/fabric/mc118/providers/VRHandItemProvider.java @@ -1,21 +1,21 @@ package com.viaversion.fabric.mc118.providers; +import com.viaversion.fabric.common.util.RemappingUtil; import com.viaversion.fabric.mc118.ViaFabric; -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; -import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; -import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; -import net.fabricmc.fabric.api.event.world.WorldTickCallback; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.network.ClientPlayerEntity; -import net.minecraft.item.ItemStack; -import net.minecraft.util.registry.Registry; -import net.minecraft.util.Identifier; -import net.minecraft.world.World; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.item.DataItem; import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.protocols.protocol1_9to1_8.providers.HandItemProvider; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.network.ClientPlayerEntity; +import net.minecraft.item.ItemStack; +import net.minecraft.util.Identifier; +import net.minecraft.util.registry.Registry; +import net.minecraft.world.World; import java.util.Map; import java.util.UUID; @@ -23,7 +23,7 @@ import java.util.concurrent.ConcurrentHashMap; public class VRHandItemProvider extends HandItemProvider { public Item clientItem = null; - public Map serverPlayers = new ConcurrentHashMap<>(); + public final Map serverPlayers = new ConcurrentHashMap<>(); @Override public Item getHandItem(UserConnection info) { @@ -48,15 +48,7 @@ public class VRHandItemProvider extends HandItemProvider { try { ClientTickEvents.END_WORLD_TICK.register(clientWorld -> tickClient()); } catch (NoClassDefFoundError ignored) { - try { - WorldTickCallback.EVENT.register(world -> { - if (world.isClient) { - tickClient(); - } - }); - } catch (NoClassDefFoundError ignored2) { - ViaFabric.JLOGGER.info("Fabric Lifecycle V0/V1 isn't installed"); - } + ViaFabric.JLOGGER.info("Fabric Lifecycle V1 isn't installed"); } } @@ -64,11 +56,6 @@ public class VRHandItemProvider extends HandItemProvider { try { ServerTickEvents.END_WORLD_TICK.register(this::tickServer); } catch (NoClassDefFoundError ignored) { - WorldTickCallback.EVENT.register(world -> { - if (!world.isClient) { - tickServer(world); - } - }); } } @@ -87,25 +74,7 @@ public class VRHandItemProvider extends HandItemProvider { private Item fromNative(ItemStack original) { Identifier iid = Registry.ITEM.getId(original.getItem()); - if (iid == null) return new DataItem(0, (byte) 0, (short) 0, null); - int id = swordId(iid.toString()); + int id = RemappingUtil.swordId(iid.toString()); return new DataItem(id, (byte) original.getCount(), (short) original.getDamage(), null); } - - private int swordId(String id) { - // https://github.com/ViaVersion/ViaVersion/blob/8de26a0ad33f5b739f5394ed80f69d14197fddc7/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/Protocol1_9To1_8.java#L86 - switch (id) { - case "minecraft:iron_sword": - return 267; - case "minecraft:wooden_sword": - return 268; - case "minecraft:golden_sword": - return 272; - case "minecraft:diamond_sword": - return 276; - case "minecraft:stone_sword": - return 283; - } - return 0; - } } diff --git a/viafabric-mc118/src/main/java/com/viaversion/fabric/mc118/service/ProtocolAutoDetector.java b/viafabric-mc118/src/main/java/com/viaversion/fabric/mc118/service/ProtocolAutoDetector.java index a1296c7..0505897 100644 --- a/viafabric-mc118/src/main/java/com/viaversion/fabric/mc118/service/ProtocolAutoDetector.java +++ b/viafabric-mc118/src/main/java/com/viaversion/fabric/mc118/service/ProtocolAutoDetector.java @@ -32,7 +32,7 @@ import java.util.logging.Level; @Environment(EnvType.CLIENT) public class ProtocolAutoDetector { - private static LoadingCache> SERVER_VER = CacheBuilder.newBuilder() + private static final LoadingCache> SERVER_VER = CacheBuilder.newBuilder() .expireAfterWrite(30, TimeUnit.SECONDS) .build(CacheLoader.from((address) -> { CompletableFuture future = new CompletableFuture<>(); diff --git a/viafabric-mc118/src/main/resources/fabric.mod.json b/viafabric-mc118/src/main/resources/fabric.mod.json index e6b7149..ff1b5ba 100644 --- a/viafabric-mc118/src/main/resources/fabric.mod.json +++ b/viafabric-mc118/src/main/resources/fabric.mod.json @@ -31,6 +31,9 @@ "main": [ "com.viaversion.fabric.mc118.ViaFabric" ], + "client": [ + "com.viaversion.fabric.mc118.ViaFabricClient" + ], "modmenu": [ "com.viaversion.fabric.mc118.gui.ModMenuConfig" ] diff --git a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/ViaFabric.java b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/ViaFabric.java index 88fb643..3c029cd 100644 --- a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/ViaFabric.java +++ b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/ViaFabric.java @@ -1,24 +1,25 @@ package com.viaversion.fabric.mc18; -import com.viaversion.fabric.common.platform.FabricInjector; -import com.viaversion.fabric.mc18.commands.NMSCommandImpl; -import com.viaversion.fabric.mc18.commands.VRCommandHandler; -import com.viaversion.fabric.common.config.VFConfig; -import com.viaversion.fabric.mc18.platform.VFLoader; -import com.viaversion.fabric.mc18.platform.FabricPlatform; -import com.viaversion.fabric.common.protocol.HostnameParserProtocol; -import com.viaversion.fabric.common.util.JLoggerToLog4j; import com.google.common.collect.Range; import com.google.common.util.concurrent.ThreadFactoryBuilder; -import io.netty.channel.EventLoop; -import io.netty.channel.local.LocalEventLoopGroup; -import net.fabricmc.api.ModInitializer; -import net.fabricmc.loader.api.FabricLoader; -import org.apache.logging.log4j.LogManager; +import com.viaversion.fabric.common.config.VFConfig; +import com.viaversion.fabric.common.platform.FabricInjector; +import com.viaversion.fabric.common.protocol.HostnameParserProtocol; +import com.viaversion.fabric.common.util.JLoggerToLog4j; +import com.viaversion.fabric.mc18.commands.NMSCommandImpl; +import com.viaversion.fabric.mc18.commands.VRCommandHandler; +import com.viaversion.fabric.mc18.platform.FabricPlatform; +import com.viaversion.fabric.mc18.platform.VFLoader; import com.viaversion.viaversion.ViaManagerImpl; import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.data.MappingDataLoader; import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; +import io.netty.channel.EventLoop; +import io.netty.channel.local.LocalEventLoopGroup; +import net.fabricmc.api.ModInitializer; +import net.fabricmc.loader.api.FabricLoader; +import net.legacyfabric.fabric.api.registry.CommandRegistry; +import org.apache.logging.log4j.LogManager; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; @@ -26,13 +27,11 @@ import java.util.concurrent.Executors; import java.util.concurrent.ThreadFactory; import java.util.logging.Logger; -import net.legacyfabric.fabric.api.registry.CommandRegistry; - public class ViaFabric implements ModInitializer { public static final Logger JLOGGER = new JLoggerToLog4j(LogManager.getLogger("ViaFabric")); public static final ExecutorService ASYNC_EXECUTOR; public static final EventLoop EVENT_LOOP; - public static CompletableFuture INIT_FUTURE = new CompletableFuture<>(); + public static final CompletableFuture INIT_FUTURE = new CompletableFuture<>(); public static VFConfig config; static { @@ -59,11 +58,7 @@ public class ViaFabric implements ModInitializer { FabricLoader.getInstance().getEntrypoints("viafabric:via_api_initialized", Runnable.class).forEach(Runnable::run); - try { - registerCommandsV0(); - } catch (NoClassDefFoundError ignored2) { - JLOGGER.info("Couldn't register command as Fabric Commands isn't installed"); - } + registerCommandsV0(); config = new VFConfig(FabricLoader.getInstance().getConfigDir().resolve("ViaFabric") .resolve("viafabric.yml").toFile()); @@ -71,8 +66,11 @@ public class ViaFabric implements ModInitializer { INIT_FUTURE.complete(null); } - @SuppressWarnings("deprecation") private void registerCommandsV0() { - CommandRegistry.INSTANCE.register(new NMSCommandImpl(Via.getManager().getCommandHandler())); + try { + CommandRegistry.INSTANCE.register(new NMSCommandImpl(Via.getManager().getCommandHandler())); + } catch (NoClassDefFoundError ignored2) { + JLOGGER.info("Couldn't register command as Fabric Commands isn't installed"); + } } } diff --git a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/commands/NMSCommandImpl.java b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/commands/NMSCommandImpl.java index b6bd7cb..e28f247 100644 --- a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/commands/NMSCommandImpl.java +++ b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/commands/NMSCommandImpl.java @@ -9,7 +9,7 @@ import java.util.Arrays; import java.util.List; public class NMSCommandImpl extends AbstractCommand { - private ViaVersionCommand handler; + private final ViaVersionCommand handler; public NMSCommandImpl(ViaVersionCommand handler) { this.handler = handler; diff --git a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/commands/NMSCommandSender.java b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/commands/NMSCommandSender.java index 70bb278..56bd75b 100644 --- a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/commands/NMSCommandSender.java +++ b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/commands/NMSCommandSender.java @@ -1,11 +1,10 @@ package com.viaversion.fabric.mc18.commands; +import com.viaversion.fabric.common.util.RemappingUtil; +import com.viaversion.viaversion.api.command.ViaCommandSender; import net.minecraft.command.CommandSource; import net.minecraft.entity.Entity; import net.minecraft.text.Text; -import com.viaversion.viaversion.api.command.ViaCommandSender; -import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.gson.GsonComponentSerializer; -import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import java.util.UUID; @@ -24,11 +23,7 @@ public class NMSCommandSender implements ViaCommandSender { @Override public void sendMessage(String s) { - source.sendMessage(Text.Serializer.deserialize(legacyToJson(s))); - } - - private String legacyToJson(String legacy) { - return GsonComponentSerializer.gson().serialize(LegacyComponentSerializer.legacySection().deserialize(legacy)); + source.sendMessage(Text.Serializer.deserialize(RemappingUtil.legacyToJson(s))); } @Override diff --git a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/gui/ChangedListener.java b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/gui/ChangedListener.java index 07fe01c..0222db1 100644 --- a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/gui/ChangedListener.java +++ b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/gui/ChangedListener.java @@ -5,7 +5,7 @@ import net.minecraft.client.gui.widget.PagedEntryListWidget; import java.util.function.Consumer; public class ChangedListener implements PagedEntryListWidget.Listener { - private Consumer consumer; + private final Consumer consumer; public ChangedListener(Consumer consumer) { this.consumer = consumer; diff --git a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/gui/ListeneableButton.java b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/gui/ListeneableButton.java index 814e6b1..632e30d 100644 --- a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/gui/ListeneableButton.java +++ b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/gui/ListeneableButton.java @@ -5,12 +5,7 @@ import net.minecraft.client.gui.widget.ButtonWidget; import java.util.function.Consumer; public class ListeneableButton extends ButtonWidget { - private Consumer click; - - public ListeneableButton(int id, int x, int y, String message, Consumer click) { - super(id, x, y, message); - this.click = click; - } + private final Consumer click; public ListeneableButton(int id, int x, int y, int width, int height, String message, Consumer click) { super(id, x, y, width, height, message); diff --git a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/gui/ViaButton.java b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/gui/ViaButton.java index 141bffb..93d6a08 100644 --- a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/gui/ViaButton.java +++ b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/gui/ViaButton.java @@ -10,11 +10,11 @@ import java.util.function.Consumer; public class ViaButton extends ButtonWidget { // Meant to be similar to higher versions TexturedButtonWidget - private int startU; - private int startV; - private int offsetHoverV; - private Identifier texturePath; - private Consumer onClick; + private final int startU; + private final int startV; + private final int offsetHoverV; + private final Identifier texturePath; + private final Consumer onClick; public ViaButton(int id, int x, int y, int width, int height, int startU, int startV, int offsetHoverV, Identifier texturePath, int textureSizeX, int textureSizeY, Consumer onClick, String altTxt) { @@ -34,12 +34,11 @@ public class ViaButton extends ButtonWidget { client.getTextureManager().bindTexture(texturePath); GlStateManager.color4f(1.0F, 1.0F, 1.0F, 1.0F); boolean hover = mouseX >= this.x && mouseY >= this.y && mouseX < this.x + this.width && mouseY < this.y + this.height; - int u = startU; int v = startV; if (hover) { v += offsetHoverV; } - this.drawTexture(this.x, this.y, u, v, this.width, this.height); + this.drawTexture(this.x, this.y, startU, v, this.width, this.height); } } diff --git a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/gui/ViaConfigScreen.java b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/gui/ViaConfigScreen.java index 28ce3e4..de99d62 100644 --- a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/gui/ViaConfigScreen.java +++ b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/gui/ViaConfigScreen.java @@ -2,7 +2,6 @@ package com.viaversion.fabric.mc18.gui; import com.viaversion.fabric.mc18.ViaFabric; import com.viaversion.fabric.common.util.ProtocolUtils; -import com.viaversion.viaversion.api.Via; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.MinecraftClient; @@ -20,7 +19,7 @@ public class ViaConfigScreen extends Screen { private static CompletableFuture latestProtocolSave; private final Screen parent; private TextFieldWidget protocolVersion; - private Text title; + private final Text title; public ViaConfigScreen(Screen parent) { super(); @@ -91,10 +90,6 @@ public class ViaConfigScreen extends Screen { validProtocol = true; } else { validProtocol = false; - String[] suggestions = ProtocolUtils.getProtocolSuggestions(text); - if (suggestions.length == 1) { - //protocolVersion.setSuggestion(suggestions[0].substring(text.length())); - } } protocolVersion.setEditableColor( @@ -137,11 +132,6 @@ public class ViaConfigScreen extends Screen { ViaFabric.config.saveConfig(); } - //@Override - //public void onClose() { - // MinecraftClient.getInstance().openScreen(this.parent); - //} - private TranslatableText getClientSideText() { return ViaFabric.config.isClientSideEnabled() ? new TranslatableText("gui.client_side.disable") diff --git a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/mixin/debug/client/MixinDebugHud.java b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/mixin/debug/client/MixinDebugHud.java index 4af3a43..654dc0d 100644 --- a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/mixin/debug/client/MixinDebugHud.java +++ b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/mixin/debug/client/MixinDebugHud.java @@ -21,7 +21,7 @@ public class MixinDebugHud { protected void getLeftText(CallbackInfoReturnable> info) { String line = "[ViaFabric] I: " + Via.getManager().getConnectionManager().getConnections().size() + " (F: " + Via.getManager().getConnectionManager().getConnectedClients().size() + ")"; - @SuppressWarnings("ConstantConditions") ChannelHandler viaDecoder = ((MixinClientConnectionAccessor) MinecraftClient.getInstance().getNetworkHandler() + ChannelHandler viaDecoder = ((MixinClientConnectionAccessor) MinecraftClient.getInstance().getNetworkHandler() .getClientConnection()).getChannel().pipeline().get(CommonTransformer.HANDLER_DECODER_NAME); if (viaDecoder instanceof FabricDecodeHandler) { ProtocolInfo protocol = ((FabricDecodeHandler) viaDecoder).getInfo().getProtocolInfo(); diff --git a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/platform/FabricPlatform.java b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/platform/FabricPlatform.java index 11b7e60..0d8d67a 100644 --- a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/platform/FabricPlatform.java +++ b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/platform/FabricPlatform.java @@ -3,14 +3,12 @@ package com.viaversion.fabric.mc18.platform; import com.viaversion.fabric.common.commands.UserCommandSender; import com.viaversion.fabric.common.provider.AbstractFabricPlatform; import com.viaversion.fabric.common.util.FutureTaskId; +import com.viaversion.fabric.common.util.RemappingUtil; import com.viaversion.fabric.mc18.ViaFabric; import com.viaversion.fabric.mc18.commands.NMSCommandSender; import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.command.ViaCommandSender; -import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.gson.GsonComponentSerializer; -import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; -import io.netty.util.concurrent.Future; -import io.netty.util.concurrent.GenericFutureListener; +import io.netty.channel.EventLoop; import net.fabricmc.api.EnvType; import net.fabricmc.loader.api.FabricLoader; import net.minecraft.client.MinecraftClient; @@ -22,7 +20,7 @@ import java.util.UUID; import java.util.concurrent.Callable; import java.util.concurrent.CancellationException; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.ExecutorService; import java.util.function.Supplier; import java.util.logging.Level; @@ -34,8 +32,14 @@ public class FabricPlatform extends AbstractFabricPlatform { return MinecraftServer.getServer(); } - public static String legacyToJson(String legacy) { - return GsonComponentSerializer.gson().serialize(LegacyComponentSerializer.legacySection().deserialize(legacy)); + @Override + protected ExecutorService asyncService() { + return ViaFabric.ASYNC_EXECUTOR; + } + + @Override + protected EventLoop eventLoop() { + return ViaFabric.EVENT_LOOP; } @Override @@ -68,42 +72,6 @@ public class FabricPlatform extends AbstractFabricPlatform { }))); } - private FutureTaskId runEventLoop(Runnable runnable) { - return new FutureTaskId( - ViaFabric.EVENT_LOOP - .submit(runnable) - .addListener(errorLogger()) - ); - } - - @Override - public FutureTaskId runSync(Runnable runnable, long ticks) { - // ViaVersion seems to not need to run delayed tasks on main thread - return new FutureTaskId( - ViaFabric.EVENT_LOOP - .schedule(() -> runSync(runnable), ticks * 50, TimeUnit.MILLISECONDS) - .addListener(errorLogger()) - ); - } - - @Override - public FutureTaskId runRepeatingSync(Runnable runnable, long ticks) { - // ViaVersion seems to not need to run repeating tasks on main thread - return new FutureTaskId( - ViaFabric.EVENT_LOOP - .scheduleAtFixedRate(() -> runSync(runnable), 0, ticks * 50, TimeUnit.MILLISECONDS) - .addListener(errorLogger()) - ); - } - - private > GenericFutureListener errorLogger() { - return future -> { - if (!future.isCancelled() && future.cause() != null) { - future.cause().printStackTrace(); - } - }; - } - @Override public ViaCommandSender[] getOnlinePlayers() { MinecraftServer server = getServer(); @@ -132,7 +100,7 @@ public class FabricPlatform extends AbstractFabricPlatform { runServerSync(() -> { ServerPlayerEntity player = server.getPlayerManager().getPlayer(uuid); if (player == null) return; - player.sendMessage(Text.Serializer.deserialize(legacyToJson(s))); + player.sendMessage(Text.Serializer.deserialize(RemappingUtil.legacyToJson(s))); }); } diff --git a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/providers/VRHandItemProvider.java b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/providers/VRHandItemProvider.java index 0bf18d5..997da2c 100644 --- a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/providers/VRHandItemProvider.java +++ b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/providers/VRHandItemProvider.java @@ -20,7 +20,7 @@ import java.util.concurrent.ConcurrentHashMap; public class VRHandItemProvider extends HandItemProvider { public Item clientItem = null; - public Map serverPlayers = new ConcurrentHashMap<>(); + public final Map serverPlayers = new ConcurrentHashMap<>(); @Override public Item getHandItem(UserConnection info) { @@ -75,21 +75,4 @@ public class VRHandItemProvider extends HandItemProvider { int id = net.minecraft.item.Item.getRawId(original.getItem()); return new DataItem(id, (byte) original.count, (short) original.getDamage(), null); } - - private int swordId(String id) { - // https://github.com/ViaVersion/ViaVersion/blob/8de26a0ad33f5b739f5394ed80f69d14197fddc7/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/Protocol1_9To1_8.java#L86 - switch (id) { - case "minecraft:iron_sword": - return 267; - case "minecraft:wooden_sword": - return 268; - case "minecraft:golden_sword": - return 272; - case "minecraft:diamond_sword": - return 276; - case "minecraft:stone_sword": - return 283; - } - return 0; - } } diff --git a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/service/ProtocolAutoDetector.java b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/service/ProtocolAutoDetector.java index 39532fc..ed767eb 100644 --- a/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/service/ProtocolAutoDetector.java +++ b/viafabric-mc18/src/main/java/com/viaversion/fabric/mc18/service/ProtocolAutoDetector.java @@ -32,7 +32,7 @@ import java.util.logging.Level; @Environment(EnvType.CLIENT) public class ProtocolAutoDetector { - private static LoadingCache> SERVER_VER = CacheBuilder.newBuilder() + private static final LoadingCache> SERVER_VER = CacheBuilder.newBuilder() .expireAfterWrite(30, TimeUnit.SECONDS) .build(CacheLoader.from((address) -> { CompletableFuture future = new CompletableFuture<>();