mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-01-25 17:41:21 +01:00
Implemented support for sponge economy (#721)
* Implement basic economy support for Sponge * Implemented global server stats
This commit is contained in:
parent
2783841e4b
commit
67712f9b26
@ -22,6 +22,7 @@ import com.djrapitops.pluginbridge.plan.mcmmo.McmmoHook;
|
|||||||
import com.djrapitops.pluginbridge.plan.nucleus.NucleusHook;
|
import com.djrapitops.pluginbridge.plan.nucleus.NucleusHook;
|
||||||
import com.djrapitops.pluginbridge.plan.protocolsupport.ProtocolSupportHook;
|
import com.djrapitops.pluginbridge.plan.protocolsupport.ProtocolSupportHook;
|
||||||
import com.djrapitops.pluginbridge.plan.redprotect.RedProtectHook;
|
import com.djrapitops.pluginbridge.plan.redprotect.RedProtectHook;
|
||||||
|
import com.djrapitops.pluginbridge.plan.sponge.SpongeEconomyHook;
|
||||||
import com.djrapitops.pluginbridge.plan.superbvote.SuperbVoteHook;
|
import com.djrapitops.pluginbridge.plan.superbvote.SuperbVoteHook;
|
||||||
import com.djrapitops.pluginbridge.plan.towny.TownyHook;
|
import com.djrapitops.pluginbridge.plan.towny.TownyHook;
|
||||||
import com.djrapitops.pluginbridge.plan.vault.VaultHook;
|
import com.djrapitops.pluginbridge.plan.vault.VaultHook;
|
||||||
@ -74,6 +75,7 @@ public class Bridge {
|
|||||||
private static Hook[] getSpongeHooks(HookHandler h) {
|
private static Hook[] getSpongeHooks(HookHandler h) {
|
||||||
return new Hook[]{
|
return new Hook[]{
|
||||||
new BuyCraftHook(h),
|
new BuyCraftHook(h),
|
||||||
|
new SpongeEconomyHook(h),
|
||||||
new NucleusHook(h)
|
new NucleusHook(h)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,97 @@
|
|||||||
|
package com.djrapitops.pluginbridge.plan.sponge;
|
||||||
|
|
||||||
|
import com.djrapitops.plan.data.element.AnalysisContainer;
|
||||||
|
import com.djrapitops.plan.data.element.InspectContainer;
|
||||||
|
import com.djrapitops.plan.data.plugin.ContainerSize;
|
||||||
|
import com.djrapitops.plan.data.plugin.PluginData;
|
||||||
|
import com.djrapitops.plan.data.store.keys.AnalysisKeys;
|
||||||
|
import com.djrapitops.plan.data.store.keys.PlayerKeys;
|
||||||
|
import com.djrapitops.plan.data.store.mutators.PlayersMutator;
|
||||||
|
import com.djrapitops.plan.system.cache.DataCache;
|
||||||
|
import com.djrapitops.plan.utilities.html.icon.Color;
|
||||||
|
import com.djrapitops.plan.utilities.html.icon.Icon;
|
||||||
|
import org.spongepowered.api.service.economy.Currency;
|
||||||
|
import org.spongepowered.api.service.economy.EconomyService;
|
||||||
|
import org.spongepowered.api.service.economy.account.UniqueAccount;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PluginData for Sponge.
|
||||||
|
*
|
||||||
|
* @author BrainStone
|
||||||
|
*/
|
||||||
|
public class SpongeEconomyData extends PluginData {
|
||||||
|
private static final Color color = Color.AMBER;
|
||||||
|
private static final String nameMoneyIcon = "money-bill-wave";
|
||||||
|
private static final Icon moneyIcon = Icon.called(nameMoneyIcon).build();
|
||||||
|
private static final Icon moneyIconColored = Icon.called(nameMoneyIcon).of(color).build();
|
||||||
|
|
||||||
|
private final EconomyService economyService;
|
||||||
|
|
||||||
|
public SpongeEconomyData(EconomyService economyService) {
|
||||||
|
super(ContainerSize.THIRD, "Sponge Economy");
|
||||||
|
|
||||||
|
this.economyService = economyService;
|
||||||
|
|
||||||
|
setPluginIcon(moneyIconColored);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InspectContainer getPlayerData(UUID uuid, InspectContainer inspectContainer) {
|
||||||
|
String name = DataCache.getInstance().getName(uuid);
|
||||||
|
|
||||||
|
if (name == null) {
|
||||||
|
return inspectContainer;
|
||||||
|
}
|
||||||
|
|
||||||
|
Optional<UniqueAccount> uOpt = economyService.getOrCreateAccount(uuid);
|
||||||
|
|
||||||
|
if (!uOpt.isPresent()) {
|
||||||
|
return inspectContainer;
|
||||||
|
}
|
||||||
|
|
||||||
|
UniqueAccount acc = uOpt.get();
|
||||||
|
|
||||||
|
for(Currency currency : economyService.getCurrencies()) {
|
||||||
|
BigDecimal balance = acc.getBalance(currency);
|
||||||
|
inspectContainer.addValue(getWithIcon(currency.getName(), moneyIconColored), currency.format(balance).toPlain());
|
||||||
|
}
|
||||||
|
|
||||||
|
return inspectContainer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public AnalysisContainer getServerData(Collection<UUID> uuids, AnalysisContainer analysisContainer) {
|
||||||
|
List<UniqueAccount> players = uuids.stream().map(economyService::getOrCreateAccount)
|
||||||
|
.filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
|
||||||
|
|
||||||
|
for(Currency currency : economyService.getCurrencies()) {
|
||||||
|
addCurrencyToContainer(currency, players, analysisContainer);
|
||||||
|
}
|
||||||
|
|
||||||
|
return analysisContainer;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addCurrencyToContainer(Currency currency, List<UniqueAccount> players, AnalysisContainer analysisContainer) {
|
||||||
|
BigDecimal totalBalance = BigDecimal.ZERO;
|
||||||
|
Map<UUID, String> playerBalances = new HashMap<>();
|
||||||
|
|
||||||
|
for (UniqueAccount player : players) {
|
||||||
|
BigDecimal balance = player.getBalance(currency);
|
||||||
|
|
||||||
|
totalBalance = totalBalance.add(balance);
|
||||||
|
playerBalances.put(player.getUniqueId(), currency.format(balance).toPlain());
|
||||||
|
}
|
||||||
|
|
||||||
|
analysisContainer.addValue(getWithIcon("Total Server Balance " + currency.getName(), moneyIconColored), currency.format(totalBalance).toPlain());
|
||||||
|
analysisContainer.addPlayerTableValues(getWithIcon("Balance " + currency.getName(), moneyIcon), playerBalances);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
package com.djrapitops.pluginbridge.plan.sponge;
|
||||||
|
|
||||||
|
import com.djrapitops.plan.data.plugin.HookHandler;
|
||||||
|
import com.djrapitops.pluginbridge.plan.Hook;
|
||||||
|
import org.spongepowered.api.Sponge;
|
||||||
|
import org.spongepowered.api.service.economy.EconomyService;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Class responsible for hooking to Sponge and registering 1 data sources
|
||||||
|
*
|
||||||
|
* @author BrainStone
|
||||||
|
* @since 4.4.6
|
||||||
|
*/
|
||||||
|
public class SpongeEconomyHook extends Hook {
|
||||||
|
public SpongeEconomyHook(HookHandler hookHandler) {
|
||||||
|
super("org.spongepowered.api.Sponge", hookHandler);
|
||||||
|
|
||||||
|
try {
|
||||||
|
Optional<EconomyService> serviceOpt = Sponge.getServiceManager().provide(EconomyService.class);
|
||||||
|
enabled = serviceOpt.isPresent();
|
||||||
|
} catch(NoClassDefFoundError e) {
|
||||||
|
enabled = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void hook() {
|
||||||
|
if (enabled) {
|
||||||
|
addPluginDataSource(new SpongeEconomyData(Sponge.getServiceManager().provide(EconomyService.class).get()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user