Skip to content

Commit

Permalink
Add PlaceholderAPI support with the %azlink_money% placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMicky-FR committed Mar 21, 2022
1 parent 41a5336 commit 231fb68
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 1 deletion.
2 changes: 2 additions & 0 deletions bukkit/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ repositories {
maven { url 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/' }
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
maven { url 'https://repo.codemc.io/repository/maven-public/' }
maven { url 'https://repo.extendedclip.com/content/repositories/placeholderapi/' }
}

dependencies {
implementation project(':azlink-common')
compileOnly 'org.spigotmc:spigot-api:1.15.2-R0.1-SNAPSHOT'
compileOnly 'io.netty:netty-all:4.1.25.Final'
compileOnly 'fr.xephi:authme:5.6.0-beta2'
compileOnly 'me.clip:placeholderapi:2.11.1'
}

processResources {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.azuriom.azlink.bukkit.command.BukkitCommandSender;
import com.azuriom.azlink.bukkit.injector.InjectedHttpServer;
import com.azuriom.azlink.bukkit.integrations.AuthMeIntegration;
import com.azuriom.azlink.bukkit.integrations.MoneyPlaceholderExpansion;
import com.azuriom.azlink.common.AzLinkPlatform;
import com.azuriom.azlink.common.AzLinkPlugin;
import com.azuriom.azlink.common.command.CommandSender;
Expand Down Expand Up @@ -75,6 +76,10 @@ protected HttpServer createHttpServer() {
&& getServer().getPluginManager().getPlugin("AuthMe") != null) {
getServer().getPluginManager().registerEvents(new AuthMeIntegration(this), this);
}

if (getServer().getPluginManager().getPlugin("PlaceholderAPI") != null) {
MoneyPlaceholderExpansion.enable(this);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.azuriom.azlink.bukkit.integrations;

import com.azuriom.azlink.bukkit.AzLinkBukkitPlugin;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import org.bukkit.OfflinePlayer;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

public class MoneyPlaceholderExpansion extends PlaceholderExpansion {

private static final DecimalFormat FORMATTER = createDecimalFormat();

private final AzLinkBukkitPlugin plugin;

public MoneyPlaceholderExpansion(AzLinkBukkitPlugin plugin) {
this.plugin = plugin;
}

@Override
public String getName() {
return this.plugin.getName();
}

@Override
public String getAuthor() {
return String.join(", ", this.plugin.getDescription().getAuthors());
}

@Override
public String getIdentifier() {
return "azlink";
}

@Override
public String getVersion() {
return this.plugin.getPluginVersion();
}

@Override
public List<String> getPlaceholders() {
return Collections.singletonList("%azlink_money%");
}

@Override
public boolean persist() {
return true;
}

@Override
public String onRequest(OfflinePlayer player, String params) {
if (params.equalsIgnoreCase("money")) {
return this.plugin.getPlugin()
.getUser(player.getName())
.map(user -> FORMATTER.format(user.getMoney()))
.orElse("?");
}

return null;
}

public static void enable(AzLinkBukkitPlugin plugin) {
if (new MoneyPlaceholderExpansion(plugin).register()) {
plugin.getLogger().info("PlaceholderAPI expansion enabled");
}
}

private static DecimalFormat createDecimalFormat() {
DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(Locale.ROOT);
return new DecimalFormat("0.##", symbols);
}
}
1 change: 1 addition & 0 deletions bukkit/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ description: The plugin to link your Azuriom website with your server.
website: https://azuriom.com
main: com.azuriom.azlink.bukkit.AzLinkBukkitPlugin
api-version: 1.13
softdepend: [PlaceholderAPI]
loadbefore: [AuthMe]
commands:
azlink:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.azuriom.azlink.common.data.PlayerData;
import com.azuriom.azlink.common.data.ServerData;
import com.azuriom.azlink.common.data.SystemData;
import com.azuriom.azlink.common.data.UserInfo;
import com.azuriom.azlink.common.data.WorldData;
import com.azuriom.azlink.common.http.client.HttpClient;
import com.azuriom.azlink.common.http.server.HttpServer;
Expand All @@ -29,6 +30,7 @@
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -195,6 +197,10 @@ public static Gson getGsonPrettyPrint() {
return GSON_PRETTY_PRINT;
}

public Optional<UserInfo> getUser(String name) {
return this.fetcherTask.getUser(name);
}

protected HttpServer createHttpServer() {
return new NettyHttpServer(this);
}
Expand Down
20 changes: 20 additions & 0 deletions common/src/main/java/com/azuriom/azlink/common/data/UserInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.azuriom.azlink.common.data;

public class UserInfo {

private final String name;
private final double money;

public UserInfo(String name, double money) {
this.name = name;
this.money = money;
}

public String getName() {
return name;
}

public double getMoney() {
return money;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.azuriom.azlink.common.data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WebsiteResponse {

private final Map<String, List<String>> commands = new HashMap<>();
private final List<UserInfo> users = new ArrayList<>();

public WebsiteResponse(Map<String, List<String>> commands) {
this.commands.putAll(commands);
Expand All @@ -15,4 +17,8 @@ public WebsiteResponse(Map<String, List<String>> commands) {
public Map<String, List<String>> getCommands() {
return this.commands;
}

public List<UserInfo> getUsers() {
return users;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@
import com.azuriom.azlink.common.AzLinkPlugin;
import com.azuriom.azlink.common.command.CommandSender;
import com.azuriom.azlink.common.data.ServerData;
import com.azuriom.azlink.common.data.UserInfo;
import com.azuriom.azlink.common.data.WebsiteResponse;

import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

public class FetcherTask implements Runnable {

private final Map<String, UserInfo> usersByName = new ConcurrentHashMap<>();
private final AzLinkPlugin plugin;

private Instant lastFullDataSent = Instant.MIN;
Expand Down Expand Up @@ -44,11 +48,25 @@ public void run() {
});
}

public Optional<UserInfo> getUser(String name) {
return Optional.ofNullable(usersByName.get(name));
}

private void sendData(ServerData data, boolean sendFullData) {
try {
WebsiteResponse response = this.plugin.getHttpClient().postData(data);

if (response == null || response.getCommands().isEmpty()) {
if (response == null) {
return;
}

if (response.getUsers() != null) {
for (UserInfo user : response.getUsers()) {
this.usersByName.put(user.getName(), user);
}
}

if (response.getCommands().isEmpty()) {
return;
}

Expand Down

0 comments on commit 231fb68

Please sign in to comment.