Skip to content

Commit

Permalink
Update users API & support SkinRestorer v15
Browse files Browse the repository at this point in the history
Closes #32
  • Loading branch information
MrMicky-FR committed Dec 1, 2023
1 parent 78f717f commit 9f24ddb
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 24 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
allprojects {
group 'com.azuriom'
version '1.3.2'
version '1.3.3'
}

subprojects {
Expand Down
2 changes: 1 addition & 1 deletion bukkit/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ 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:14.2.3'
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
Expand Up @@ -87,7 +87,13 @@ && getServer().getPluginManager().getPlugin("AuthMe") != null) {

if (getConfig().getBoolean("skinrestorer-integration")
&& getServer().getPluginManager().getPlugin("SkinsRestorer") != null) {
getServer().getPluginManager().registerEvents(new SkinRestorerIntegration(this), this);
try {
Class.forName("net.skinsrestorer.api.SkinsRestorer");

getServer().getPluginManager().registerEvents(new SkinRestorerIntegration(this), this);
} catch (ClassNotFoundException e) {
getLogger().severe("SkinsRestorer integration requires SkinsRestorer v15.0.0 or higher");
}
}

if (getServer().getPluginManager().getPlugin("PlaceholderAPI") != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,11 @@ private void loadLibrary(String groupId, String artifactId, String version) thro
}

URL[] urls = {jar.toUri().toURL()};
ClassLoader pluginClassLoader = plugin.getClass().getClassLoader();
Field classLoaderField = pluginClassLoader.getClass().getDeclaredField("libraryLoader");
ClassLoader libraryLoader = new URLClassLoader(urls, pluginClassLoader.getParent());
ClassLoader classLoader = plugin.getClass().getClassLoader();
Field classLoaderField = classLoader.getClass().getDeclaredField("libraryLoader");

classLoaderField.setAccessible(true);
classLoaderField.set(pluginClassLoader, libraryLoader);
classLoaderField.set(classLoader, new URLClassLoader(urls, classLoader.getParent()));
}

private String identifyNettyVersion() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
package com.azuriom.azlink.bukkit.integrations;

import com.azuriom.azlink.bukkit.AzLinkBukkitPlugin;
import net.skinsrestorer.api.PlayerWrapper;
import net.skinsrestorer.api.SkinsRestorerAPI;
import net.skinsrestorer.api.exception.SkinRequestException;
import net.skinsrestorer.api.property.IProperty;
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 org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;

public class SkinRestorerIntegration implements Listener {

private final SkinsRestorer skinsRestorer;
private final AzLinkBukkitPlugin plugin;

public SkinRestorerIntegration(AzLinkBukkitPlugin plugin) {
this.plugin = plugin;
this.skinsRestorer = SkinsRestorerProvider.get();

this.plugin.getLoggerAdapter().info("SkinRestorer integration enabled.");
}
Expand All @@ -32,10 +35,10 @@ public void onPlayerJoin(PlayerJoinEvent e) {

try {
String url = baseUrl + "/api/skin-api/skins/" + player.getName();
IProperty skin = SkinsRestorerAPI.getApi().genSkinUrl(url, null);
MineSkinResponse skin = this.skinsRestorer.getMineSkinAPI().genSkin(url, null);

SkinsRestorerAPI.getApi().applySkin(new PlayerWrapper(player), skin);
} catch (SkinRequestException ex) {
this.skinsRestorer.getSkinApplier(Player.class).applySkin(player, skin.getProperty());
} catch (DataRequestException | MineSkinException ex) {
this.plugin.getLoggerAdapter().warn("Unable to apply skin for " + player.getName() + ": " + ex.getMessage());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.azuriom.azlink.common.AzLinkPlugin;
import com.azuriom.azlink.common.data.UserInfo;
import com.azuriom.azlink.common.users.MoneyAction;

import java.io.IOException;
import java.util.Arrays;
Expand All @@ -14,7 +15,6 @@
public class AzLinkCommand {

private static final List<String> COMPLETIONS = Arrays.asList("status", "setup", "fetch", "money", "port");
private static final List<String> MONEY_ACTIONS = Arrays.asList("add", "remove", "set");

protected final AzLinkPlugin plugin;

Expand Down Expand Up @@ -123,7 +123,8 @@ public List<String> tabComplete(CommandSender sender, String[] args) {
}

if (args.length == 2 && args[0].equalsIgnoreCase("money")) {
return MONEY_ACTIONS.stream()
return Arrays.stream(MoneyAction.values())
.map(Enum::toString)
.filter(s -> startsWithIgnoreCase(s, args[1]))
.collect(Collectors.toList());
}
Expand All @@ -139,12 +140,13 @@ public List<String> tabComplete(CommandSender sender, String[] args) {
}

public void editMoney(CommandSender sender, String[] args) throws NumberFormatException {
if (args.length < 4 || !MONEY_ACTIONS.contains(args[1].toLowerCase())) {
MoneyAction action;

if (args.length < 4 || (action = MoneyAction.fromString(args[1])) == null) {
sender.sendMessage("&cUsage: /azlink money <add|remove|set> <player> <amount>");
return;
}

String action = args[1].toLowerCase();
double amount = Double.parseDouble(args[3]);
Optional<UserInfo> user = this.plugin.getUserManager().getUserByName(args[2]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
public class HttpClient {

private static final int CONNECT_TIMEOUT = 5000; // 5 seconds
private static final int REQUEST_TIMEOUT = 1000; // 1 second
private static final int READ_TIMEOUT = 5000; // 5 seconds

private final AzLinkPlugin plugin;

Expand Down Expand Up @@ -133,7 +133,7 @@ private HttpURLConnection prepareConnection(RequestMethod method, String endpoin
conn.setUseCaches(false);
conn.setInstanceFollowRedirects(true);
conn.setConnectTimeout(CONNECT_TIMEOUT);
conn.setReadTimeout(REQUEST_TIMEOUT);
conn.setReadTimeout(READ_TIMEOUT);
conn.setRequestMethod(method.name());
conn.addRequestProperty("Azuriom-Link-Token", token);
conn.addRequestProperty("Content-Type", "application/json; charset=utf-8");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
Expand Down Expand Up @@ -83,7 +84,7 @@ private void dispatchCommands(Map<String, List<String>> commands) {

Map<String, CommandSender> players = this.plugin.getPlatform()
.getOnlinePlayers()
.collect(Collectors.toMap(cs -> cs.getName().toLowerCase(), p -> p, (p1, p2) -> {
.collect(Collectors.toMap(cs -> cs.getName().toLowerCase(Locale.ROOT), p -> p, (p1, p2) -> {
String player1 = p1.getName() + " (" + p1.getUuid() + ')';
String player2 = p2.getName() + " (" + p2.getUuid() + ')';
this.plugin.getLogger().warn("Duplicate players names: " + player1 + " / " + player2);
Expand All @@ -92,7 +93,7 @@ private void dispatchCommands(Map<String, List<String>> commands) {

for (Map.Entry<String, List<String>> entry : commands.entrySet()) {
String playerName = entry.getKey();
CommandSender player = players.get(playerName.toLowerCase());
CommandSender player = players.get(playerName.toLowerCase(Locale.ROOT));

if (player != null) {
playerName = player.getName();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.azuriom.azlink.common.users;

import java.util.Locale;

public enum MoneyAction {
ADD, REMOVE, SET;

@Override
public String toString() {
return name().toLowerCase(Locale.ROOT);
}

public static MoneyAction fromString(String action) {
try {
return MoneyAction.valueOf(action.toUpperCase(Locale.ROOT));
} catch (IllegalArgumentException e) {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,19 @@ public void addUser(UserInfo user) {
this.usersByName.put(user.getName(), user);
}

public CompletableFuture<UserInfo> editMoney(UserInfo user, String action, double amount) {
return this.plugin.getHttpClient().editMoney(user, action, amount)
public CompletableFuture<UserInfo> editMoney(UserInfo user, MoneyAction action, double amount) {
return this.plugin.getHttpClient().editMoney(user, action.toString(), amount)
.thenApply(result -> {
user.setMoney(result.getNewBalance());
return user;
});
}

/**
* @deprecated Use {@link #editMoney(UserInfo, MoneyAction, double)} instead.
*/
@Deprecated
public CompletableFuture<UserInfo> editMoney(UserInfo user, String action, double amount) {
return editMoney(user, MoneyAction.valueOf(action.toUpperCase()), amount);
}
}

0 comments on commit 9f24ddb

Please sign in to comment.