Skip to content

Commit

Permalink
Refactor tests & universal SkinsRestorer integration
Browse files Browse the repository at this point in the history
Closes #34 and closes #35
  • Loading branch information
MrMicky-FR committed Dec 25, 2023
1 parent b1fcbdb commit cca5cc3
Show file tree
Hide file tree
Showing 21 changed files with 291 additions and 90 deletions.
1 change: 0 additions & 1 deletion bukkit/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Player> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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() {
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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<ProxiedPlayer> 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);
}
}
3 changes: 3 additions & 0 deletions bungee/src/main/resources/bungee-config.yml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions bungee/src/main/resources/bungee.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
12 changes: 9 additions & 3 deletions common/build.gradle
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
17 changes: 17 additions & 0 deletions common/src/main/java/com/azuriom/azlink/common/AzLinkPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
}
}
Original file line number Diff line number Diff line change
@@ -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<P> {

private final Class<P> playerClass;
protected final AzLinkPlugin plugin;

public BaseSkinsRestorer(AzLinkPlugin plugin, Class<P> 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());
}
}
}
78 changes: 40 additions & 38 deletions common/src/test/java/com/azuriom/azlink/common/HashTest.java
Original file line number Diff line number Diff line change
@@ -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<Arguments> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}
Loading

0 comments on commit cca5cc3

Please sign in to comment.