Skip to content

Commit

Permalink
Treasury Support Added.
Browse files Browse the repository at this point in the history
Signed-off-by: creatorfromhell <[email protected]>
  • Loading branch information
creatorfromhell committed Aug 19, 2023
1 parent e5821e8 commit 8208ff4
Show file tree
Hide file tree
Showing 18 changed files with 836 additions and 0 deletions.
1 change: 1 addition & 0 deletions Bukkit/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ softdepend:
- Essentials
- EssentialsX
- Vault
- Treasury
- Reserve
- mcMMO
- GUIShop
Expand Down
30 changes: 30 additions & 0 deletions Bukkit/src/net/tnemc/bukkit/impl/BukkitServerProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,24 @@ public Optional<UUID> fromName(String name) {
return Optional.empty();
}

/**
* Used to locate a username for a specific name. This could be called from either a primary or
* secondary thread, and should not call back to the Mojang API ever.
*
* @param id The {@link UUID} to use for the search.
*
* @return An optional containing the name if exists, otherwise false.
*/
@Override
public Optional<String> fromID(UUID id) {
for(OfflinePlayer player : Bukkit.getServer().getOfflinePlayers()) {
if(player.getUniqueId().equals(id)) {
return Optional.ofNullable(player.getName());
}
}
return Optional.empty();
}

/**
* Returns the name of the default region.
*
Expand All @@ -188,6 +206,18 @@ public String defaultRegion(final RegionMode mode) {
return Bukkit.getServer().getWorlds().get(0).getName();
}

/**
* Determines if a plugin with the correlating name is currently installed.
*
* @param name The name to use for our check.
*
* @return True if a plugin with that name exists, otherwise false.
*/
@Override
public boolean pluginAvailable(String name) {
return Bukkit.getPluginManager().isPluginEnabled(name);
}

/**
* Used to replace colour codes in a string.
*
Expand Down
7 changes: 7 additions & 0 deletions Core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@
<version>4.11.0</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>me.lokka30</groupId>
<artifactId>treasury-api</artifactId>
<version>2.0.2-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions Core/src/net/tnemc/core/TNECore.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import net.tnemc.core.config.MainConfig;
import net.tnemc.core.config.MessageConfig;
import net.tnemc.core.currency.Currency;
import net.tnemc.core.hook.treasury.TreasuryHook;
import net.tnemc.core.io.message.MessageData;
import net.tnemc.core.io.message.MessageHandler;
import net.tnemc.core.io.message.TranslationProvider;
Expand Down Expand Up @@ -246,6 +247,10 @@ protected void onEnable() {
this.economyManager.currency().load(directory, false);
this.economyManager.currency().saveCurrenciesUUID(directory);

if(server.pluginAvailable("Treasury")) {
new TreasuryHook().register();
}

//set our debug options.
this.level = DebugLevel.fromID(MainConfig.yaml().getString("Core.Debugging.Mode"));

Expand Down
21 changes: 21 additions & 0 deletions Core/src/net/tnemc/core/compatibility/ServerConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ default UUID getCached(final String name) {
*/
Optional<UUID> fromName(final String name);

/**
* Used to locate a username for a specific name. This could be called from either a primary or secondary thread, and
* should not call back to the Mojang API ever.
* @param id The {@link UUID} to use for the search.
* @return An optional containing the name if exists, otherwise false.
*/
Optional<String> fromID(final UUID id);

/**
* Returns the {@link Pattern pattern} utilized to determine if a string is a valid
* player username.
Expand Down Expand Up @@ -158,6 +166,19 @@ default String defaultRegion() {
default boolean pluginAvailable(final String name) {
return false;
}
/**
* Determines if a plugin with the correlating class is currently installed.
*
* @param classPath The class to use for our check.
* @return True if a plugin with that class exists, otherwise false.
*/
default boolean pluginClassAvailable(final String classPath) {
try {
Class.forName("net.milkbowl.vault.economy.Economy");
return true;
} catch(Exception ignore) {}
return false;
}

/**
* Used to replace colour codes in a string.
Expand Down
143 changes: 143 additions & 0 deletions Core/src/net/tnemc/core/hook/treasury/TNETreasury.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package net.tnemc.core.hook.treasury;
/*
* The New Economy
* Copyright (C) 2022 - 2023 Daniel "creatorfromhell" Vidmar
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import me.lokka30.treasury.api.common.NamespacedKey;
import me.lokka30.treasury.api.common.misc.TriState;
import me.lokka30.treasury.api.economy.EconomyProvider;
import me.lokka30.treasury.api.economy.account.AccountData;
import me.lokka30.treasury.api.economy.account.accessor.AccountAccessor;
import me.lokka30.treasury.api.economy.account.accessor.NonPlayerAccountAccessor;
import me.lokka30.treasury.api.economy.account.accessor.PlayerAccountAccessor;
import me.lokka30.treasury.api.economy.currency.Currency;
import net.tnemc.core.TNECore;
import net.tnemc.core.account.Account;
import net.tnemc.core.account.PlayerAccount;
import net.tnemc.core.account.SharedAccount;
import net.tnemc.core.hook.treasury.impl.TreasuryNonPlayerAccessor;
import net.tnemc.core.hook.treasury.impl.TreasuryPlayerAccessor;
import net.tnemc.core.hook.treasury.wrapper.TreasuryCurrency;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

/**
* TNETreasury
*
* @author creatorfromhell
* @since 0.1.2.0
*/
public class TNETreasury implements EconomyProvider, AccountAccessor {


final Set<Currency> currencies = new HashSet<>();
final TreasuryPlayerAccessor playerAccessor = new TreasuryPlayerAccessor();
final TreasuryNonPlayerAccessor nonPlayerAccessor = new TreasuryNonPlayerAccessor();

@Override
public @NotNull AccountAccessor accountAccessor() {
return this;
}

@Override
public @NotNull CompletableFuture<Boolean> hasAccount(@NotNull AccountData accountData) {

return CompletableFuture.supplyAsync(()->{
final String id = (accountData.isPlayerAccount())? accountData.getPlayerIdentifier().get().toString() :
accountData.getNonPlayerIdentifier().get().toString().replace("tne:", "");
return TNECore.eco().account().findAccount(id).isPresent();
});
}

@Override
public @NotNull CompletableFuture<Collection<UUID>> retrievePlayerAccountIds() {

return CompletableFuture.supplyAsync(()->{
final Collection<UUID> ids = new ArrayList<>();
for(Account account : TNECore.eco().account().getAccounts().values()) {
if(account instanceof PlayerAccount) ids.add(((PlayerAccount)account).getUUID());
}
return ids;
});
}

@Override
public @NotNull CompletableFuture<Collection<NamespacedKey>> retrieveNonPlayerAccountIds() {

return CompletableFuture.supplyAsync(()->{
final Collection<NamespacedKey> ids = new ArrayList<>();
for(Account account : TNECore.eco().account().getAccounts().values()) {
if(account instanceof SharedAccount) ids.add(NamespacedKey.fromString("tne:" + account.getIdentifier()));
}
return ids;
});
}

@Override
public @NotNull Currency getPrimaryCurrency() {
return new TreasuryCurrency(TNECore.eco().currency().getDefaultCurrency());
}

@Override
public @NotNull Optional<Currency> findCurrency(@NotNull String identifier) {

for(Currency cur : currencies) {
if(cur.getIdentifier().equalsIgnoreCase(identifier)) return Optional.of(cur);
}
return Optional.empty();
}

@Override
public @NotNull Set<Currency> getCurrencies() {

if(currencies.size() == 0) {
for(net.tnemc.core.currency.Currency cur : TNECore.eco().currency().currencies()) {
currencies.add(new TreasuryCurrency(cur));
}
}
return currencies;
}

@Override
public @NotNull CompletableFuture<TriState> registerCurrency(@NotNull Currency currency) {
//TODO: complete this.
return null;
}

@Override
public @NotNull CompletableFuture<TriState> unregisterCurrency(@NotNull Currency currency) {
//TODO: complete this.
return null;
}

@Override
public @NotNull PlayerAccountAccessor player() {
return playerAccessor;
}

@Override
public @NotNull NonPlayerAccountAccessor nonPlayer() {
return nonPlayerAccessor;
}
}
54 changes: 54 additions & 0 deletions Core/src/net/tnemc/core/hook/treasury/TreasuryHook.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package net.tnemc.core.hook.treasury;
/*
* The New Economy
* Copyright (C) 2022 - 2023 Daniel "creatorfromhell" Vidmar
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import me.lokka30.treasury.api.common.service.ServicePriority;
import me.lokka30.treasury.api.common.service.ServiceRegistry;
import me.lokka30.treasury.api.economy.EconomyProvider;
import net.tnemc.core.hook.Hook;

/**
* TNEHook
*
* @author creatorfromhell
* @since 0.1.2.0
*/
public class TreasuryHook implements Hook {

final EconomyProvider provider = new TNETreasury();
/**
* @return True if this hook is enabled, otherwise false.
*/
@Override
public boolean enabled() {
return true;
}

/**
* Used to register this service.
*/
@Override
public void register() {
ServiceRegistry.INSTANCE.registerService(
EconomyProvider.class,
provider,
"TheNewEconomy",
ServicePriority.HIGH
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package net.tnemc.core.hook.treasury.impl;
/*
* The New Economy
* Copyright (C) 2022 - 2023 Daniel "creatorfromhell" Vidmar
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import me.lokka30.treasury.api.economy.account.NonPlayerAccount;
import me.lokka30.treasury.api.economy.account.accessor.NonPlayerAccountAccessor;
import net.tnemc.core.TNECore;
import net.tnemc.core.account.SharedAccount;
import net.tnemc.core.api.response.AccountAPIResponse;
import net.tnemc.core.hook.treasury.wrapper.TreasuryNonPlayerAccount;
import org.jetbrains.annotations.NotNull;

import java.util.concurrent.CompletableFuture;

/**
* TreasuryNonPlayerAccessor
*
* @author creatorfromhell
* @since 0.1.2.0
*/
public class TreasuryNonPlayerAccessor extends NonPlayerAccountAccessor {
@Override
protected @NotNull CompletableFuture<NonPlayerAccount> getOrCreate(@NotNull NonPlayerAccountCreateContext context) {

final AccountAPIResponse response = TNECore.eco().account().createAccount(context.getIdentifier().toString(), context.getName());
if(response.getAccount().isEmpty() || !(response.getAccount().get() instanceof SharedAccount)) {
return CompletableFuture.failedFuture(new IllegalArgumentException("Account creation failed: " + response.getResponse().response()));
}
return CompletableFuture.completedFuture(new TreasuryNonPlayerAccount((SharedAccount)response.getAccount().get()));
}
}
Loading

0 comments on commit 8208ff4

Please sign in to comment.