Skip to content

Commit

Permalink
feat(expansion): add %townyplus_town_NAME_networth%
Browse files Browse the repository at this point in the history
  • Loading branch information
BrycensRanch committed Mar 10, 2023
1 parent f1a899e commit b28cf62
Showing 1 changed file with 43 additions and 4 deletions.
47 changes: 43 additions & 4 deletions src/main/java/me/romvnly/TownyPlus/TownyPlusExpansion.java
@@ -1,12 +1,23 @@
package me.romvnly.TownyPlus;

import com.palmergames.bukkit.towny.TownyAPI;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.economy.Account;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import me.romvnly.TownyPlus.util.Debug;
import org.bukkit.OfflinePlayer;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class TownyPlusExpansion extends PlaceholderExpansion {
TownyPlusMain plugin;

// Although this class does technically need Towny, the plugin will already be disabled if Towny is not present or enabled.

public TownyPlusExpansion() {
super();
plugin = TownyPlusMain.plugin;
}
@Override
Expand All @@ -23,19 +34,47 @@ public TownyPlusExpansion() {
public @NotNull String getVersion() {
return plugin.getDescription().getVersion();
}
@Override
public boolean canRegister() {
return true;
}

@Override
public boolean persist() {
return true; // This is required or else PlaceholderAPI will unregister the Expansion on reload
}

@Override
public String onRequest(OfflinePlayer player, String placeholder) {
if(placeholder.equalsIgnoreCase("name")) {
if (placeholder.equalsIgnoreCase("name")) {
return player == null ? null : player.getName(); // "name" requires the player to be valid
}

if(placeholder.equalsIgnoreCase("stats")) {
if (placeholder.equalsIgnoreCase("stats")) {
return "Placeholder Text 1";
}

if(placeholder.equalsIgnoreCase("placeholder2")) {
if (placeholder.equalsIgnoreCase("placeholder2")) {
return "Placeholder Text 2";
}

if (placeholder.startsWith("town_") && placeholder.endsWith("_networth")) {
String placeholderTown = placeholder.substring(5, placeholder.length() - 9);
Town town = TownyAPI.getInstance().getTown(placeholderTown);
if (town == null) return null;
List < Resident > residents = town.getResidents();
double networth = 0;
for (Resident resident: residents) {
Account residentAccount = resident.getAccountOrNull();
if (residentAccount != null) {
networth += residentAccount.getHoldingBalance();
} else {
Debug.log("Resident " + resident.getName() + " has no account! (Town: " + town.getName() + ")");
}
}
return networth + "";
}

return null; // Placeholder is unknown by the Expansion
}
}
}

0 comments on commit b28cf62

Please sign in to comment.