Skip to content

Commit

Permalink
Implement changes to nonPlayerAccount creation to support Towny imple…
Browse files Browse the repository at this point in the history
…mentation of VaultUnlocked.
  • Loading branch information
creatorfromhell committed Oct 27, 2024
1 parent 09550db commit 33c6ba4
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .changelog/0.1.3.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

## Minor Changes
- Added Messages.Money.HoldingsMultiOther to messages.yml for /balo usage.
- Updated VaultUnlocked hook to work for latest release of 2.5.0
- Updated VaultUnlocked hook to work for latest release of 2.7.0
- Updated VaultUnlocked to support Towny usage of VU

## Internals

Expand Down
4 changes: 3 additions & 1 deletion Bukkit/src/net/tnemc/bukkit/depend/towny/NationAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class NationAccount extends NonPlayerAccount {
public NationAccount(final UUID identifier, final String name) {

super(identifier, name);

this.identifier = identifier;
//this.owner = Objects.requireNonNull(TownyAPI.getInstance().getNation(name)).getKing().getUUID();
}

Expand All @@ -54,7 +56,7 @@ public UUID generateIdentifier(final String name) {

try {
return Objects.requireNonNull(TownyAPI.getInstance().getNation(name)).getUUID();
} catch(Exception ignore) {
} catch(final Exception ignore) {
return super.generateIdentifier(name);
}
}
Expand Down
4 changes: 3 additions & 1 deletion Bukkit/src/net/tnemc/bukkit/depend/towny/TownAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class TownAccount extends NonPlayerAccount {
public TownAccount(final UUID identifier, final String name) {

super(identifier, name);

this.identifier = identifier;
//this.owner = Objects.requireNonNull(TownyAPI.getInstance().getTown(name)).getMayor().getUUID();
}

Expand All @@ -54,7 +56,7 @@ public UUID generateIdentifier(final String name) {

try {
return Objects.requireNonNull(TownyAPI.getInstance().getTown(name)).getUUID();
} catch(Exception ignore) {
} catch(final Exception ignore) {
return super.generateIdentifier(name);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ public Collection<String> currencies() {
@Override
public boolean createAccount(final UUID uuid, final String name) {

PluginCore.log().debug("Account create called: " + uuid.toString() + " Name: " + name, DebugLevel.STANDARD);

return TNECore.eco().account().createAccount(uuid.toString(), name).getResponse().success();
}

Expand Down Expand Up @@ -375,11 +377,11 @@ public BigDecimal getBalance(final String pluginName, final UUID uuid, final Str
final Optional<Currency> currencyOpt = TNECore.eco().currency().findCurrency(currency);

if(account.isPresent() && currencyOpt.isPresent()) {
PluginCore.log().debug("Vault Balance call. Account exists. Name:" + account.get().getName(), DebugLevel.STANDARD);
PluginCore.log().debug("Vault Unlocked Balance call. Account exists. Name:" + account.get().getName(), DebugLevel.STANDARD);
return account.get().getHoldingsTotal(world, currencyOpt.get().getUid());
}

PluginCore.log().debug("Vault Balance call. Account doesn't exist. Name:" + uuid, DebugLevel.STANDARD);
PluginCore.log().debug("Vault Unlocked Balance call. Account doesn't exist. Name:" + uuid, DebugLevel.STANDARD);
return BigDecimal.ZERO;
}

Expand Down
8 changes: 4 additions & 4 deletions Core/src/net/tnemc/core/api/TNEAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@ public AccountAPIResponse getOrCreatePlayerAccount(@NotNull final UUID identifie
*
* @since 0.1.2.0
*/
public Optional<SharedAccount> createAccount(@NotNull final String identifier) {
public Optional<SharedAccount> createNonPlayerAccount(@NotNull final String identifier, final String name) {

return TNECore.eco().account().createNonPlayerAccount(identifier);
return TNECore.eco().account().createNonPlayerAccount(identifier, name);
}

/**
Expand Down Expand Up @@ -441,7 +441,7 @@ public TransactionResult removeHoldings(final String identifier, final String wo

return transaction.process();

} catch(InvalidTransactionException e) {
} catch(final InvalidTransactionException e) {
return new TransactionResult(false, e.getMessage());
}
}
Expand Down Expand Up @@ -477,7 +477,7 @@ public TransactionResult addHoldings(final String identifier, final String world

return transaction.process();

} catch(InvalidTransactionException e) {
} catch(final InvalidTransactionException e) {
return new TransactionResult(false, e.getMessage());
}
}
Expand Down
26 changes: 19 additions & 7 deletions Core/src/net/tnemc/core/manager/AccountManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public AccountAPIResponse createAccount(final String identifier, final String na
account = new PlayerAccount(id, name);

uuidProvider.store(new UUIDPair(id, name));
} catch(Exception ignore) {
} catch(final Exception ignore) {

//Our identifier is an invalid UUID, let's search for it.
final Optional<UUID> id = PluginCore.server().fromName(name);
Expand Down Expand Up @@ -190,11 +190,11 @@ public AccountAPIResponse createAccount(final String identifier, final String na
account = new GeyserAccount(id, name);

uuidProvider.store(new UUIDPair(id, name));
} catch(Exception ignore) {
} catch(final Exception ignore) {
return new AccountAPIResponse(null, AccountResponse.CREATION_FAILED);
}
} else {
final Optional<SharedAccount> nonPlayerAccount = createNonPlayerAccount(name);
final Optional<SharedAccount> nonPlayerAccount = createNonPlayerAccount(identifier, name);

if(nonPlayerAccount.isEmpty()) {
return new AccountAPIResponse(null, AccountResponse.CREATION_FAILED);
Expand All @@ -213,7 +213,7 @@ public AccountAPIResponse createAccount(final String identifier, final String na

try {
uuidProvider.store(new UUIDPair(account.getIdentifier(), account.getName()));
} catch(Exception ignore) {
} catch(final Exception ignore) {
//identifier isn't an uuid, so it'll be a string, most likely a non-player.
}
return new AccountAPIResponse(account, AccountResponse.CREATED);
Expand All @@ -228,15 +228,23 @@ public AccountAPIResponse createAccount(final String identifier, final String na
* @return An Optional containing the new account class if it was able to be created, otherwise an
* empty Optional.
*/
public Optional<SharedAccount> createNonPlayerAccount(final String name) {
public Optional<SharedAccount> createNonPlayerAccount(final String identifier, final String name) {

UUID uuid = UUID.randomUUID();

try {
uuid = UUID.fromString(identifier);
} catch(final Exception ignore) {
//stay with the random UUID if the identifier passed isn't a UUID.
}

for(final Map.Entry<Class<? extends SharedAccount>, Function<String, Boolean>> entry : types.entrySet()) {

if(entry.getValue().apply(name)) {
try {
return Optional.of(entry.getKey().getDeclaredConstructor(UUID.class, String.class)
.newInstance(UUID.randomUUID(), name));
} catch(Exception e) {
.newInstance(uuid, name));
} catch(final Exception e) {
PluginCore.log().error("An error occured while trying to create a new NonPlayer Account" +
"for : " + name, e, DebugLevel.STANDARD);
}
Expand Down Expand Up @@ -373,6 +381,10 @@ public void addDefaultTypes() {

PluginCore.callbacks().call(new AccountTypesCallback());

for(final Class<? extends SharedAccount> account : types.keySet()) {
PluginCore.log().debug("Adding default account types: " + account.getClass().getSimpleName());
}

addAccountType(NonPlayerAccount.class, (value)->true);
}

Expand Down

0 comments on commit 33c6ba4

Please sign in to comment.