From cca5cc3450440b8623c656d22f95110055c2def6 Mon Sep 17 00:00:00 2001 From: MrMicky Date: Mon, 25 Dec 2023 11:37:14 +0100 Subject: [PATCH] Refactor tests & universal SkinsRestorer integration Closes #34 and closes #35 --- bukkit/build.gradle | 1 - .../SkinsRestorerIntegration.java | 31 ++------ .../azlink/bungee/AzLinkBungeePlugin.java | 26 +++++++ .../SkinsRestorerIntegration.java | 23 ++++++ bungee/src/main/resources/bungee-config.yml | 3 + bungee/src/main/resources/bungee.yml | 1 + common/build.gradle | 12 ++- .../azuriom/azlink/common/AzLinkPlatform.java | 17 ++++ .../integrations/BaseSkinsRestorer.java | 39 ++++++++++ .../com/azuriom/azlink/common/HashTest.java | 78 ++++++++++--------- .../common/ThreadFactoryBuilderTest.java | 4 +- .../azlink/common/UpdateCheckerTest.java | 30 +++---- fabric/build.gradle | 8 +- .../azlink/fabric/command/TextAdapter.java | 2 +- .../azlink/sponge/AzLinkSpongePlugin.java | 27 +++++++ .../SkinsRestorerIntegration.java | 20 +++++ sponge/src/main/resources/azlink.conf | 3 + velocity/build.gradle | 4 +- .../azlink/velocity/AzLinkVelocityPlugin.java | 28 +++++++ .../SkinsRestorerIntegration.java | 21 +++++ .../src/main/resources/velocity-config.yml | 3 + 21 files changed, 291 insertions(+), 90 deletions(-) create mode 100644 bungee/src/main/java/com/azuriom/azlink/bungee/integrations/SkinsRestorerIntegration.java create mode 100644 bungee/src/main/resources/bungee-config.yml create mode 100644 common/src/main/java/com/azuriom/azlink/common/integrations/BaseSkinsRestorer.java create mode 100644 sponge/src/main/java/com/azuriom/azlink/sponge/integrations/SkinsRestorerIntegration.java create mode 100644 sponge/src/main/resources/azlink.conf create mode 100644 velocity/src/main/java/com/azuriom/azlink/velocity/integrations/SkinsRestorerIntegration.java create mode 100644 velocity/src/main/resources/velocity-config.yml diff --git a/bukkit/build.gradle b/bukkit/build.gradle index 0e0d5ce..493fb0a 100644 --- a/bukkit/build.gradle +++ b/bukkit/build.gradle @@ -11,7 +11,6 @@ dependencies { compileOnly 'io.netty:netty-all:4.1.25.Final' compileOnly 'fr.xephi:authme:5.6.0-beta2' compileOnly 'me.clip:placeholderapi:2.11.1' - compileOnly 'net.skinsrestorer:skinsrestorer-api:15.0.2' } // Folia is compiled with Java 17 diff --git a/bukkit/src/main/java/com/azuriom/azlink/bukkit/integrations/SkinsRestorerIntegration.java b/bukkit/src/main/java/com/azuriom/azlink/bukkit/integrations/SkinsRestorerIntegration.java index 58fb668..b321040 100644 --- a/bukkit/src/main/java/com/azuriom/azlink/bukkit/integrations/SkinsRestorerIntegration.java +++ b/bukkit/src/main/java/com/azuriom/azlink/bukkit/integrations/SkinsRestorerIntegration.java @@ -1,44 +1,23 @@ package com.azuriom.azlink.bukkit.integrations; import com.azuriom.azlink.bukkit.AzLinkBukkitPlugin; -import net.skinsrestorer.api.SkinsRestorer; -import net.skinsrestorer.api.SkinsRestorerProvider; -import net.skinsrestorer.api.connections.model.MineSkinResponse; -import net.skinsrestorer.api.exception.DataRequestException; -import net.skinsrestorer.api.exception.MineSkinException; +import com.azuriom.azlink.common.integrations.BaseSkinsRestorer; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; -public class SkinsRestorerIntegration implements Listener { - - private final AzLinkBukkitPlugin plugin; +public class SkinsRestorerIntegration + extends BaseSkinsRestorer implements Listener { public SkinsRestorerIntegration(AzLinkBukkitPlugin plugin) { - this.plugin = plugin; - - this.plugin.getLoggerAdapter().info("SkinsRestorer integration enabled."); + super(plugin.getPlugin(), Player.class); } @EventHandler public void onPlayerJoin(PlayerJoinEvent e) { Player player = e.getPlayer(); - String baseUrl = this.plugin.getPlugin().getConfig().getSiteUrl(); - - - if (baseUrl == null) { - return; - } - - try { - String url = baseUrl + "/api/skin-api/skins/" + player.getName(); - SkinsRestorer skins = SkinsRestorerProvider.get(); - MineSkinResponse res = skins.getMineSkinAPI().genSkin(url, null); - skins.getSkinApplier(Player.class).applySkin(player, res.getProperty()); - } catch (DataRequestException | MineSkinException ex) { - this.plugin.getLoggerAdapter().warn("Unable to apply skin for " + player.getName() + ": " + ex.getMessage()); - } + handleJoin(player.getName(), player); } } diff --git a/bungee/src/main/java/com/azuriom/azlink/bungee/AzLinkBungeePlugin.java b/bungee/src/main/java/com/azuriom/azlink/bungee/AzLinkBungeePlugin.java index 534e556..e02b6d2 100644 --- a/bungee/src/main/java/com/azuriom/azlink/bungee/AzLinkBungeePlugin.java +++ b/bungee/src/main/java/com/azuriom/azlink/bungee/AzLinkBungeePlugin.java @@ -2,6 +2,7 @@ import com.azuriom.azlink.bungee.command.BungeeCommandExecutor; import com.azuriom.azlink.bungee.command.BungeeCommandSender; +import com.azuriom.azlink.bungee.integrations.SkinsRestorerIntegration; import com.azuriom.azlink.common.AzLinkPlatform; import com.azuriom.azlink.common.AzLinkPlugin; import com.azuriom.azlink.common.command.CommandSender; @@ -11,7 +12,12 @@ import com.azuriom.azlink.common.platform.PlatformType; import com.azuriom.azlink.common.scheduler.SchedulerAdapter; import net.md_5.bungee.api.plugin.Plugin; +import net.md_5.bungee.config.Configuration; +import net.md_5.bungee.config.ConfigurationProvider; +import net.md_5.bungee.config.YamlConfiguration; +import java.io.File; +import java.io.IOException; import java.nio.file.Path; import java.util.stream.Stream; @@ -21,6 +27,7 @@ public final class AzLinkBungeePlugin extends Plugin implements AzLinkPlatform { private AzLinkPlugin plugin; private LoggerAdapter loggerAdapter; + private Configuration config; @Override public void onLoad() { @@ -33,6 +40,13 @@ public void onEnable() { this.plugin.init(); getProxy().getPluginManager().registerCommand(this, new BungeeCommandExecutor(this.plugin)); + + loadConfig(); + + if (this.config.getBoolean("skinsrestorer-integration") + && getProxy().getPluginManager().getPlugin("SkinsRestorer") != null) { + getProxy().getPluginManager().registerListener(this, new SkinsRestorerIntegration(this)); + } } @Override @@ -91,4 +105,16 @@ public void dispatchConsoleCommand(String command) { public int getMaxPlayers() { return getProxy().getConfig().getPlayerLimit(); } + + private void loadConfig() { + File configFile = new File(getDataFolder(), "config.yml"); + + try { + saveResource(configFile.toPath(), "bungee-config.yml"); + + this.config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(configFile); + } catch (IOException e) { + throw new RuntimeException("Unable to load configuration", e); + } + } } diff --git a/bungee/src/main/java/com/azuriom/azlink/bungee/integrations/SkinsRestorerIntegration.java b/bungee/src/main/java/com/azuriom/azlink/bungee/integrations/SkinsRestorerIntegration.java new file mode 100644 index 0000000..034d137 --- /dev/null +++ b/bungee/src/main/java/com/azuriom/azlink/bungee/integrations/SkinsRestorerIntegration.java @@ -0,0 +1,23 @@ +package com.azuriom.azlink.bungee.integrations; + +import com.azuriom.azlink.bungee.AzLinkBungeePlugin; +import com.azuriom.azlink.common.integrations.BaseSkinsRestorer; +import net.md_5.bungee.api.connection.ProxiedPlayer; +import net.md_5.bungee.api.event.PostLoginEvent; +import net.md_5.bungee.api.plugin.Listener; +import net.md_5.bungee.event.EventHandler; + +public class SkinsRestorerIntegration + extends BaseSkinsRestorer implements Listener { + + public SkinsRestorerIntegration(AzLinkBungeePlugin plugin) { + super(plugin.getPlugin(), ProxiedPlayer.class); + } + + @EventHandler + public void onPlayerJoin(PostLoginEvent event) { + ProxiedPlayer player = event.getPlayer(); + + handleJoin(player.getName(), player); + } +} diff --git a/bungee/src/main/resources/bungee-config.yml b/bungee/src/main/resources/bungee-config.yml new file mode 100644 index 0000000..6ad455c --- /dev/null +++ b/bungee/src/main/resources/bungee-config.yml @@ -0,0 +1,3 @@ +# When enabled, if SkinsRestorer is installed, and the SkinAPI plugin is present on the website, +# the player's skin will be updated to the website's skin +skinsrestorer-integration: false diff --git a/bungee/src/main/resources/bungee.yml b/bungee/src/main/resources/bungee.yml index b856c90..680924a 100644 --- a/bungee/src/main/resources/bungee.yml +++ b/bungee/src/main/resources/bungee.yml @@ -3,3 +3,4 @@ version: ${pluginVersion} author: Azuriom Team description: The plugin to link your Azuriom website with your server. main: com.azuriom.azlink.bungee.AzLinkBungeePlugin +softdepend: [SkinsRestorer] diff --git a/common/build.gradle b/common/build.gradle index d0e5edf..8b7455f 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -1,10 +1,16 @@ +repositories { + maven { url 'https://repo.codemc.io/repository/maven-public/' } +} + dependencies { compileOnly 'org.slf4j:slf4j-api:1.7.36' - compileOnly 'com.google.code.gson:gson:2.9.1' + compileOnly 'com.google.code.gson:gson:2.10.1' compileOnly 'io.netty:netty-all:4.1.42.Final' + compileOnly 'net.skinsrestorer:skinsrestorer-api:15.0.2' - testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0' - testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.9.0' + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.1' + testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.10.1' + testImplementation 'org.junit.jupiter:junit-jupiter-params:5.10.1' } test { diff --git a/common/src/main/java/com/azuriom/azlink/common/AzLinkPlatform.java b/common/src/main/java/com/azuriom/azlink/common/AzLinkPlatform.java index 6207604..ef6e498 100644 --- a/common/src/main/java/com/azuriom/azlink/common/AzLinkPlatform.java +++ b/common/src/main/java/com/azuriom/azlink/common/AzLinkPlatform.java @@ -8,6 +8,9 @@ import com.azuriom.azlink.common.platform.PlatformType; import com.azuriom.azlink.common.scheduler.SchedulerAdapter; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; import java.nio.file.Path; import java.util.Optional; import java.util.stream.Stream; @@ -42,6 +45,20 @@ default PlatformData getPlatformData() { return new PlatformData(getPlatformType(), getPlatformInfo()); } + default void saveResource(Path target, String name) throws IOException { + if (Files.exists(target)) { + return; + } + + if (!Files.isDirectory(target.getParent())) { + Files.createDirectory(target.getParent()); + } + + try (InputStream in = getClass().getClassLoader().getResourceAsStream(name)) { + Files.copy(in, target); + } + } + default void prepareDataAsync() { } } diff --git a/common/src/main/java/com/azuriom/azlink/common/integrations/BaseSkinsRestorer.java b/common/src/main/java/com/azuriom/azlink/common/integrations/BaseSkinsRestorer.java new file mode 100644 index 0000000..b3ca3c0 --- /dev/null +++ b/common/src/main/java/com/azuriom/azlink/common/integrations/BaseSkinsRestorer.java @@ -0,0 +1,39 @@ +package com.azuriom.azlink.common.integrations; + +import com.azuriom.azlink.common.AzLinkPlugin; +import net.skinsrestorer.api.SkinsRestorer; +import net.skinsrestorer.api.SkinsRestorerProvider; +import net.skinsrestorer.api.connections.model.MineSkinResponse; +import net.skinsrestorer.api.exception.DataRequestException; +import net.skinsrestorer.api.exception.MineSkinException; + +public class BaseSkinsRestorer

{ + + private final Class

playerClass; + protected final AzLinkPlugin plugin; + + public BaseSkinsRestorer(AzLinkPlugin plugin, Class

playerClass) { + this.plugin = plugin; + this.playerClass = playerClass; + + this.plugin.getLogger().info("SkinsRestorer integration enabled."); + } + + protected void handleJoin(String playerName, P player) { + String baseUrl = this.plugin.getConfig().getSiteUrl(); + + if (baseUrl == null) { + return; + } + + try { + String url = baseUrl + "/api/skin-api/skins/" + playerName; + SkinsRestorer skins = SkinsRestorerProvider.get(); + MineSkinResponse res = skins.getMineSkinAPI().genSkin(url, null); + + skins.getSkinApplier(this.playerClass).applySkin(player, res.getProperty()); + } catch (DataRequestException | MineSkinException ex) { + this.plugin.getLogger().warn("Unable to apply skin for " + playerName + ": " + ex.getMessage()); + } + } +} diff --git a/common/src/test/java/com/azuriom/azlink/common/HashTest.java b/common/src/test/java/com/azuriom/azlink/common/HashTest.java index 601eb36..96cf908 100644 --- a/common/src/test/java/com/azuriom/azlink/common/HashTest.java +++ b/common/src/test/java/com/azuriom/azlink/common/HashTest.java @@ -1,52 +1,54 @@ package com.azuriom.azlink.common; import com.azuriom.azlink.common.utils.Hash; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; -import static org.junit.jupiter.api.Assertions.assertEquals; - -public class HashTest { +import java.util.stream.Stream; - private static final String VALUE_1 = "H5vBfLcF3vqaCo8"; - private static final String VALUE_2 = "YCJLMo7uX5t7WxG"; - private static final String VALUE_3 = "g3kmfbpwfHdDFQL"; - - @Test - public void testSha256() { - Hash hash = Hash.SHA_256; +import static org.junit.jupiter.api.Assertions.assertEquals; - String expected1 = "e936363862ca96e0866afaa51ce622959a626938b1328a6a03921941989922fb"; - String expected2 = "35d729f6ab4c9abe9409bbdca2896eba1bc4608dab2e0f80fece72a67002866b"; - String expected3 = "cfa211834f280d238965a6d4afa15c8851b3286ee0cec146dd6a598e389568e3"; +class HashTest { - assertEquals(expected1, hash.hash(VALUE_1)); - assertEquals(expected2, hash.hash(VALUE_2)); - assertEquals(expected3, hash.hash(VALUE_3)); + @ParameterizedTest + @MethodSource("hashTestData") + void testSha256(String[] data) { + assertEquals(data[1], Hash.SHA_256.hash(data[0])); } - @Test - public void testSha384() { - Hash hash = Hash.SHA_384; - - String expected1 = "ad7e3d46584ebcfff0c420e0b7e40b1c612f0f46d94054af2dcbf0f65a148ec47e56461ec93073f6b35c24aafd8906dc"; - String expected2 = "1e49c84bc3b4d827fafef3fe7ef34966d5884ebe1bf814d6ec71d05cb530889fe196ed8a184c82377b566c8bfd95ebdd"; - String expected3 = "96b714d99c039829328275f3b90fa5abe304e4d78ed8114292ab0d16e9c1e636c57d763aae11f8490b2cd8216c5b64c2"; - - assertEquals(expected1, hash.hash(VALUE_1)); - assertEquals(expected2, hash.hash(VALUE_2)); - assertEquals(expected3, hash.hash(VALUE_3)); + @ParameterizedTest + @MethodSource("hashTestData") + void testSha384(String[] data) { + assertEquals(data[2], Hash.SHA_384.hash(data[0])); } - @Test - public void testSha512() { - Hash hash = Hash.SHA_512; - - String expected1 = "754d79ec450e8f1090aba4b0c25e9a0602d351561cb6f39c902ad3a9e779dbaf2d7ae194dde3d35492530f77566fc90d0137027f3fdf8fa560c8d19ed73767ec"; - String expected2 = "c4c940c69bf0cbd24057d409be393373e26593d5b0ac8117c49c929a17131defe321dea2589664e75c438e0bf0635074a91ee3f4fbb3d3e3b211f9771587fdee"; - String expected3 = "370ca5636ecbabe0f992dae196fe79cadeddea736a712a3cca5c9417bdffc252e26636e6e1d0a08c35ee10f28f5c88fa165935e0d5bc8ce480a14ee6b8950f61"; + @ParameterizedTest + @MethodSource("hashTestData") + void testSha512(String[] data) { + assertEquals(data[3], Hash.SHA_512.hash(data[0])); + } - assertEquals(expected1, hash.hash(VALUE_1)); - assertEquals(expected2, hash.hash(VALUE_2)); - assertEquals(expected3, hash.hash(VALUE_3)); + private static Stream hashTestData() { + String[] test1 = { + "H5vBfLcF3vqaCo8", + "e936363862ca96e0866afaa51ce622959a626938b1328a6a03921941989922fb", + "ad7e3d46584ebcfff0c420e0b7e40b1c612f0f46d94054af2dcbf0f65a148ec47e56461ec93073f6b35c24aafd8906dc", + "754d79ec450e8f1090aba4b0c25e9a0602d351561cb6f39c902ad3a9e779dbaf2d7ae194dde3d35492530f77566fc90d0137027f3fdf8fa560c8d19ed73767ec", + }; + String[] test2 = { + "YCJLMo7uX5t7WxG", + "35d729f6ab4c9abe9409bbdca2896eba1bc4608dab2e0f80fece72a67002866b", + "1e49c84bc3b4d827fafef3fe7ef34966d5884ebe1bf814d6ec71d05cb530889fe196ed8a184c82377b566c8bfd95ebdd", + "c4c940c69bf0cbd24057d409be393373e26593d5b0ac8117c49c929a17131defe321dea2589664e75c438e0bf0635074a91ee3f4fbb3d3e3b211f9771587fdee", + }; + String[] test3 = { + "g3kmfbpwfHdDFQL", + "cfa211834f280d238965a6d4afa15c8851b3286ee0cec146dd6a598e389568e3", + "96b714d99c039829328275f3b90fa5abe304e4d78ed8114292ab0d16e9c1e636c57d763aae11f8490b2cd8216c5b64c2", + "370ca5636ecbabe0f992dae196fe79cadeddea736a712a3cca5c9417bdffc252e26636e6e1d0a08c35ee10f28f5c88fa165935e0d5bc8ce480a14ee6b8950f61", + }; + + return Stream.of(test1, test2, test3).map(s -> Arguments.of((Object) s)); } } diff --git a/common/src/test/java/com/azuriom/azlink/common/ThreadFactoryBuilderTest.java b/common/src/test/java/com/azuriom/azlink/common/ThreadFactoryBuilderTest.java index 801fcf2..7088ad7 100644 --- a/common/src/test/java/com/azuriom/azlink/common/ThreadFactoryBuilderTest.java +++ b/common/src/test/java/com/azuriom/azlink/common/ThreadFactoryBuilderTest.java @@ -5,12 +5,12 @@ import static org.junit.jupiter.api.Assertions.*; -public class ThreadFactoryBuilderTest { +class ThreadFactoryBuilderTest { private final Runnable voidRunnable = () -> {}; @Test - public void testThreadFactoryBuilder() { + void testThreadFactoryBuilder() { ThreadFactoryBuilder factory = new ThreadFactoryBuilder().name("thread-factory-test-%t").priority(2); Thread thread0 = factory.newThread(this.voidRunnable); diff --git a/common/src/test/java/com/azuriom/azlink/common/UpdateCheckerTest.java b/common/src/test/java/com/azuriom/azlink/common/UpdateCheckerTest.java index 85e6b08..4280ebb 100644 --- a/common/src/test/java/com/azuriom/azlink/common/UpdateCheckerTest.java +++ b/common/src/test/java/com/azuriom/azlink/common/UpdateCheckerTest.java @@ -1,22 +1,26 @@ package com.azuriom.azlink.common; import com.azuriom.azlink.common.utils.UpdateChecker; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import static org.junit.jupiter.api.Assertions.assertEquals; -public class UpdateCheckerTest { +class UpdateCheckerTest { - @Test - public void testCompareVersions() { - assertEquals(0, UpdateChecker.compareVersions("1.2", "1.2.0")); - assertEquals(0, UpdateChecker.compareVersions("1.2.1", "1.2.1")); - assertEquals(1, UpdateChecker.compareVersions("1.2.1", "1.2.0")); - assertEquals(1, UpdateChecker.compareVersions("1.2.1", "1.2")); - assertEquals(1, UpdateChecker.compareVersions("1.2.1", "0.8.1")); - assertEquals(-1, UpdateChecker.compareVersions("0.9", "1.0.1")); - assertEquals(-1, UpdateChecker.compareVersions("1.0.1", "1.10")); - assertEquals(-1, UpdateChecker.compareVersions("1.0.1", "1.10.0")); - assertEquals(-1, UpdateChecker.compareVersions("1.1.2", "1.2.0")); + @ParameterizedTest + @CsvSource({ + "0, 1.2, 1.2.0", + "0, 1.2.1, 1.2.1", + "1, 1.2.1, 1.2.0", + "1, 1.2.1, 1.2", + "1, 1.2.1, 0.8.1", + "-1, 0.9, 1.0.1", + "-1, 1.0.1, 1.10.0", + "-1, 1.0.1, 1.10.0", + "-1, 1.1.2, 1.2.0", + }) + void testCompareVersions(int expected, String ver1, String ver2) { + assertEquals(expected, UpdateChecker.compareVersions(ver1, ver2)); } } diff --git a/fabric/build.gradle b/fabric/build.gradle index 425707d..1dd96c4 100644 --- a/fabric/build.gradle +++ b/fabric/build.gradle @@ -7,10 +7,10 @@ sourceCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17 ext { - minecraft_version = '1.20.2' - yarn_mappings = '1.20.2+build.4' - loader_version = '0.15.0' - fabric_version = '0.91.1+1.20.2' + minecraft_version = '1.20.4' + yarn_mappings = '1.20.4+build.1' + loader_version = '0.15.1' + fabric_version = '0.91.2+1.20.4' } dependencies { diff --git a/fabric/src/main/java/com/azuriom/azlink/fabric/command/TextAdapter.java b/fabric/src/main/java/com/azuriom/azlink/fabric/command/TextAdapter.java index 064472b..9a01b03 100644 --- a/fabric/src/main/java/com/azuriom/azlink/fabric/command/TextAdapter.java +++ b/fabric/src/main/java/com/azuriom/azlink/fabric/command/TextAdapter.java @@ -20,6 +20,6 @@ public static Text toText(String message) { TextComponent component = LEGACY_SERIALIZER.deserialize(message); String json = GsonComponentSerializer.gson().serialize(component); - return Text.Serializer.fromJson(json); + return Text.Serialization.fromJson(json); } } diff --git a/sponge/src/main/java/com/azuriom/azlink/sponge/AzLinkSpongePlugin.java b/sponge/src/main/java/com/azuriom/azlink/sponge/AzLinkSpongePlugin.java index 26e3564..8f5defb 100644 --- a/sponge/src/main/java/com/azuriom/azlink/sponge/AzLinkSpongePlugin.java +++ b/sponge/src/main/java/com/azuriom/azlink/sponge/AzLinkSpongePlugin.java @@ -12,6 +12,7 @@ import com.azuriom.azlink.common.tasks.TpsTask; import com.azuriom.azlink.sponge.command.SpongeCommandExecutor; import com.azuriom.azlink.sponge.command.SpongeCommandSender; +import com.azuriom.azlink.sponge.integrations.SkinsRestorerIntegration; import com.azuriom.azlink.sponge.logger.Log4jLoggerAdapter; import com.google.common.collect.Iterables; import com.google.inject.Inject; @@ -30,10 +31,13 @@ import org.spongepowered.api.scheduler.Task; import org.spongepowered.api.scheduler.TaskExecutorService; import org.spongepowered.api.util.Ticks; +import org.spongepowered.configurate.ConfigurationNode; +import org.spongepowered.configurate.hocon.HoconConfigurationLoader; import org.spongepowered.plugin.PluginContainer; import org.spongepowered.plugin.builtin.jvm.Plugin; import org.spongepowered.plugin.metadata.PluginMetadata; +import java.io.IOException; import java.nio.file.Path; import java.util.Optional; import java.util.stream.Stream; @@ -48,6 +52,7 @@ public final class AzLinkSpongePlugin implements AzLinkPlatform { private final LoggerAdapter logger; private final AzLinkPlugin plugin; private SchedulerAdapter scheduler; + private ConfigurationNode config; @Inject public AzLinkSpongePlugin(PluginContainer pluginContainer, Game game, @ConfigDir(sharedRoot = false) Path configDirectory, Logger logger) { @@ -70,6 +75,13 @@ public void onServerStarted(StartedEngineEvent event) { .build(); event.engine().scheduler().submit(task); + + loadConfig(); + + if (this.game.pluginManager().plugin("skinsrestorer").isPresent() + && this.config.node("skinsrestorer-integration").getBoolean()) { + this.game.eventManager().registerListeners(this.pluginContainer, new SkinsRestorerIntegration(this)); + } } @Listener @@ -164,4 +176,19 @@ private SchedulerAdapter initScheduler() { return new JavaSchedulerAdapter(asyncExecutor, syncExecutor, asyncExecutor); } + + private void loadConfig() { + Path configPath = this.configDirectory.resolve("azlink.conf"); + + try { + saveResource(configPath, "azlink.conf"); + + this.config = HoconConfigurationLoader.builder() + .path(configPath) + .build() + .load(); + } catch (IOException e) { + throw new RuntimeException("Unable to load configuration", e); + } + } } diff --git a/sponge/src/main/java/com/azuriom/azlink/sponge/integrations/SkinsRestorerIntegration.java b/sponge/src/main/java/com/azuriom/azlink/sponge/integrations/SkinsRestorerIntegration.java new file mode 100644 index 0000000..95c8371 --- /dev/null +++ b/sponge/src/main/java/com/azuriom/azlink/sponge/integrations/SkinsRestorerIntegration.java @@ -0,0 +1,20 @@ +package com.azuriom.azlink.sponge.integrations; + +import com.azuriom.azlink.common.integrations.BaseSkinsRestorer; +import com.azuriom.azlink.sponge.AzLinkSpongePlugin; +import org.spongepowered.api.entity.living.player.server.ServerPlayer; +import org.spongepowered.api.event.Listener; +import org.spongepowered.api.event.network.ServerSideConnectionEvent; + +public class SkinsRestorerIntegration + extends BaseSkinsRestorer { + + public SkinsRestorerIntegration(AzLinkSpongePlugin plugin) { + super(plugin.getPlugin(), ServerPlayer.class); + } + + @Listener + public void onPlayerJoin(ServerSideConnectionEvent.Join event) { + handleJoin(event.player().name(), event.player()); + } +} diff --git a/sponge/src/main/resources/azlink.conf b/sponge/src/main/resources/azlink.conf new file mode 100644 index 0000000..ee87486 --- /dev/null +++ b/sponge/src/main/resources/azlink.conf @@ -0,0 +1,3 @@ +# When enabled, if SkinsRestorer is installed, and the SkinAPI plugin is present on the website, +# the player's skin will be updated to the website's skin +skinsrestorer-integration = false diff --git a/velocity/build.gradle b/velocity/build.gradle index eeb149c..284d2e7 100644 --- a/velocity/build.gradle +++ b/velocity/build.gradle @@ -9,10 +9,10 @@ repositories { dependencies { implementation project(':azlink-common') - compileOnly 'com.velocitypowered:velocity-api:3.0.1' + compileOnly 'com.velocitypowered:velocity-api:3.1.1' compileOnly 'net.elytrium.limboapi:api:1.1.13' compileOnly 'net.elytrium:limboauth:1.1.1' - annotationProcessor 'com.velocitypowered:velocity-api:3.0.1' + annotationProcessor 'com.velocitypowered:velocity-api:3.1.1' } // LimboAuth support diff --git a/velocity/src/main/java/com/azuriom/azlink/velocity/AzLinkVelocityPlugin.java b/velocity/src/main/java/com/azuriom/azlink/velocity/AzLinkVelocityPlugin.java index aae0cee..39e0f92 100644 --- a/velocity/src/main/java/com/azuriom/azlink/velocity/AzLinkVelocityPlugin.java +++ b/velocity/src/main/java/com/azuriom/azlink/velocity/AzLinkVelocityPlugin.java @@ -11,6 +11,7 @@ import com.azuriom.azlink.velocity.command.VelocityCommandExecutor; import com.azuriom.azlink.velocity.command.VelocityCommandSender; import com.azuriom.azlink.velocity.integrations.LimboAuthIntegration; +import com.azuriom.azlink.velocity.integrations.SkinsRestorerIntegration; import com.google.inject.Inject; import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; @@ -20,8 +21,11 @@ import com.velocitypowered.api.plugin.annotation.DataDirectory; import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.util.ProxyVersion; +import ninja.leaping.configurate.ConfigurationNode; +import ninja.leaping.configurate.yaml.YAMLConfigurationLoader; import org.slf4j.Logger; +import java.io.IOException; import java.nio.file.Path; import java.util.stream.Stream; @@ -34,6 +38,7 @@ authors = "Azuriom Team", dependencies = { @Dependency(id = "limboauth", optional = true), + @Dependency(id = "skinsrestorer", optional = true), } ) public final class AzLinkVelocityPlugin implements AzLinkPlatform { @@ -45,6 +50,7 @@ public final class AzLinkVelocityPlugin implements AzLinkPlatform { private final LoggerAdapter logger; private AzLinkPlugin plugin; + private ConfigurationNode config; @Inject public AzLinkVelocityPlugin(ProxyServer proxy, @DataDirectory Path dataDirectory, Logger logger) { @@ -69,9 +75,16 @@ public void onProxyInitialization(ProxyInitializeEvent event) { this.proxy.getCommandManager() .register("azlink", new VelocityCommandExecutor(this.plugin), "azuriomlink"); + loadConfig(); + if (this.proxy.getPluginManager().getPlugin("limboauth").isPresent()) { this.proxy.getEventManager().register(this, new LimboAuthIntegration(this)); } + + if (this.proxy.getPluginManager().getPlugin("skinsrestorer").isPresent() + && this.config.getNode("skinsrestorer-integration").getBoolean()) { + this.proxy.getEventManager().register(this, new SkinsRestorerIntegration(this)); + } } @Subscribe @@ -136,4 +149,19 @@ public void dispatchConsoleCommand(String command) { public ProxyServer getProxy() { return this.proxy; } + + private void loadConfig() { + Path configPath = this.dataDirectory.resolve("config.yml"); + + try { + saveResource(configPath, "velocity-config.yml"); + + this.config = YAMLConfigurationLoader.builder() + .setPath(configPath) + .build() + .load(); + } catch (IOException e) { + throw new RuntimeException("Unable to load configuration", e); + } + } } diff --git a/velocity/src/main/java/com/azuriom/azlink/velocity/integrations/SkinsRestorerIntegration.java b/velocity/src/main/java/com/azuriom/azlink/velocity/integrations/SkinsRestorerIntegration.java new file mode 100644 index 0000000..9797582 --- /dev/null +++ b/velocity/src/main/java/com/azuriom/azlink/velocity/integrations/SkinsRestorerIntegration.java @@ -0,0 +1,21 @@ +package com.azuriom.azlink.velocity.integrations; + + +import com.azuriom.azlink.common.integrations.BaseSkinsRestorer; +import com.azuriom.azlink.velocity.AzLinkVelocityPlugin; +import com.velocitypowered.api.event.Subscribe; +import com.velocitypowered.api.event.connection.PostLoginEvent; +import com.velocitypowered.api.proxy.Player; + +public class SkinsRestorerIntegration + extends BaseSkinsRestorer { + + public SkinsRestorerIntegration(AzLinkVelocityPlugin plugin) { + super(plugin.getPlugin(), Player.class); + } + + @Subscribe + public void onPlayerJoin(PostLoginEvent event) { + handleJoin(event.getPlayer().getUsername(), event.getPlayer()); + } +} diff --git a/velocity/src/main/resources/velocity-config.yml b/velocity/src/main/resources/velocity-config.yml new file mode 100644 index 0000000..6ad455c --- /dev/null +++ b/velocity/src/main/resources/velocity-config.yml @@ -0,0 +1,3 @@ +# When enabled, if SkinsRestorer is installed, and the SkinAPI plugin is present on the website, +# the player's skin will be updated to the website's skin +skinsrestorer-integration: false