Skip to content

Commit

Permalink
Add some adjustments to YAML Datables.
Browse files Browse the repository at this point in the history
  • Loading branch information
creatorfromhell committed Oct 23, 2024
1 parent 1e1d545 commit 1a0a9ca
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 122 deletions.
117 changes: 61 additions & 56 deletions Core/src/net/tnemc/core/io/storage/datables/yaml/YAMLAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -92,7 +93,7 @@ public void store(final StorageConnector<?> connector, @NotNull final Account ac

try {
Thread.sleep(1000);
} catch(InterruptedException e) {
} catch(final InterruptedException e) {
e.printStackTrace();
}
}
Expand All @@ -103,7 +104,7 @@ public void store(final StorageConnector<?> connector, @NotNull final Account ac
if(!accFile.exists()) {
try {
accFile.createNewFile();
} catch(IOException ignore) {
} catch(final IOException ignore) {

PluginCore.log().error("Issue creating account file. Account: " + account.getName(), DebugLevel.OFF);
return;
Expand All @@ -114,7 +115,7 @@ public void store(final StorageConnector<?> connector, @NotNull final Account ac
YamlDocument yaml = null;
try {
yaml = YamlDocument.create(accFile);
} catch(IOException ignore) {
} catch(final IOException ignore) {

PluginCore.log().error("Issue loading account file. Account: " + account.getName(), DebugLevel.OFF);
return;
Expand All @@ -127,11 +128,11 @@ public void store(final StorageConnector<?> connector, @NotNull final Account ac
yaml.set("Info.CreationDate", account.getCreationDate());
yaml.set("Info.Pin", account.getPin());

if(account instanceof PlayerAccount playerAccount) {
if(account instanceof final PlayerAccount playerAccount) {
yaml.set("Info.LastOnline", playerAccount.getLastOnline());
}

if(account instanceof SharedAccount shared) {
if(account instanceof final SharedAccount shared) {
final String owner = (shared.getOwner() == null)? account.getIdentifier().toString() :
shared.getOwner().toString();

Expand All @@ -145,13 +146,14 @@ public void store(final StorageConnector<?> connector, @NotNull final Account ac
}
}
try {

yaml.save();

final AccountSaveCallback callback = new AccountSaveCallback(account);
PluginCore.callbacks().call(callback);

yaml = null;
} catch(IOException ignore) {
} catch(final IOException ignore) {
PluginCore.log().error("Issue saving account file. Account: " + account.getName());
return;
}
Expand Down Expand Up @@ -199,77 +201,80 @@ public Optional<Account> load(final File accFile, final String identifier) {
PluginCore.log().error("Null account file passed to YAMLAccount.load. Account: " + identifier, DebugLevel.OFF);
return Optional.empty();
}

YamlDocument yaml = null;
try {
yaml = YamlDocument.create(accFile);
} catch(Exception ignore) {

PluginCore.log().error("Issue loading account file. Account: " + identifier + ". You may need to remove this account file! This is due to a previous server crash or improper shutdown and not a bug.", DebugLevel.OFF);
return Optional.empty();
}
try(final FileInputStream fis = new FileInputStream(accFile)) {

if(yaml != null) {
final YamlDocument yaml = YamlDocument.create(fis);

Account account = null;
if(yaml != null) {

//Validate account file
if(!yaml.contains("Info.Name") || !yaml.contains("Info.Type")) {
Account account = null;

PluginCore.log().error("Invalid account file. Account: " + identifier + ". You may need to remove this account file! This is due to a previous server crash or improper shutdown and not a bug.", DebugLevel.OFF);
return Optional.empty();
}
//Validate account file
if(!yaml.contains("Info.Name") || !yaml.contains("Info.Type")) {

final String type = yaml.getString("Info.Type");
PluginCore.log().error("Invalid account file. Account: " + identifier + ". You may need to remove this account file! This is due to a previous server crash or improper shutdown and not a bug.", DebugLevel.OFF);
return Optional.empty();
}

//create our account from the type
final AccountAPIResponse response = TNECore.eco().account().createAccount(identifier,
yaml.getString("Info.Name"),
!(type.equalsIgnoreCase("player") ||
type.equalsIgnoreCase("bedrock")));
if(response.getResponse().success() && response.getAccount().isPresent()) {
final String type = yaml.getString("Info.Type");

//load our basic account information
account = response.getAccount().get();
//create our account from the type
final AccountAPIResponse response = TNECore.eco().account().createAccount(identifier,
yaml.getString("Info.Name"),
!(type.equalsIgnoreCase("player") ||
type.equalsIgnoreCase("bedrock")));
if(response.getResponse().success() && response.getAccount().isPresent()) {

account.setStatus(TNECore.eco().account().findStatus(yaml.getString("Info.Status")));
account.setCreationDate(yaml.getLong("Info.CreationDate"));
account.setPin(yaml.getString("Info.Pin"));
}
//load our basic account information
account = response.getAccount().get();

if(account != null) {
account.setStatus(TNECore.eco().account().findStatus(yaml.getString("Info.Status")));
account.setCreationDate(yaml.getLong("Info.CreationDate"));
account.setPin(yaml.getString("Info.Pin"));
}

if(account instanceof PlayerAccount playerAccount) {
playerAccount.setLastOnline(yaml.getLong("Info.LastOnline"));
}
if(account != null) {

if(account instanceof SharedAccount shared && yaml.contains("Members")) {
if(account instanceof final PlayerAccount playerAccount) {
playerAccount.setLastOnline(yaml.getLong("Info.LastOnline"));
}

if(account instanceof final SharedAccount shared && yaml.contains("Members")) {

final Section section = yaml.getSection("Members");
for(final Object memberObj : section.getKeys()) {

final Section section = yaml.getSection("Members");
for(final Object memberObj : section.getKeys()) {
final String member = (String)memberObj;
for(final Object permissionObj : section.getSection(member).getKeys()) {

final String member = (String)memberObj;
for(final Object permissionObj : section.getSection(member).getKeys()) {
final String permission = (String)permissionObj;
shared.addPermission(UUID.fromString(member), permission,
yaml.getBoolean("Members." + member +
"." + permission));
}
}
}

final String permission = (String)permissionObj;
shared.addPermission(UUID.fromString(member), permission,
yaml.getBoolean("Members." + member +
"." + permission));
final Collection<HoldingsEntry> holdings = TNECore.instance().storage().loadAll(HoldingsEntry.class, identifier);
for(final HoldingsEntry entry : holdings) {
account.getWallet().setHoldings(entry);
}

final AccountLoadCallback callback = new AccountLoadCallback(account);
PluginCore.callbacks().call(callback);
}
}

final Collection<HoldingsEntry> holdings = TNECore.instance().storage().loadAll(HoldingsEntry.class, identifier);
for(final HoldingsEntry entry : holdings) {
account.getWallet().setHoldings(entry);
return Optional.ofNullable(account);
}

final AccountLoadCallback callback = new AccountLoadCallback(account);
PluginCore.callbacks().call(callback);
}
yaml = null;
return Optional.ofNullable(account);
} catch(final Exception ignore) {

PluginCore.log().error("Issue loading account file. Account: " + identifier + ". You may need to remove this account file! This is due to a previous server crash or improper shutdown and not a bug.", DebugLevel.OFF);
return Optional.empty();
}

return Optional.empty();
}

Expand All @@ -293,7 +298,7 @@ public Collection<Account> loadAll(final StorageConnector<?> connector, @Nullabl
accounts.add(loaded.get());
TNECore.eco().account().uuidProvider().store(new UUIDPair(loaded.get().getIdentifier(), loaded.get().getName()));
}
} catch(Exception ignore) {
} catch(final Exception ignore) {
PluginCore.log().error("Issue loading account file. File: " + file.getName() + ". You may need to remove this account file! This is due to a previous server crash or improper shutdown and not a bug.", DebugLevel.OFF);
}
}
Expand Down
87 changes: 42 additions & 45 deletions Core/src/net/tnemc/core/io/storage/datables/yaml/YAMLHoldings.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -235,75 +237,70 @@ public Collection<HoldingsEntry> loadAll(final StorageConnector<?> connector, @N
return holdings;
}


YamlDocument yaml = null;
try {
yaml = YamlDocument.create(accFile);
} catch(final IOException ignore) {

PluginCore.log().error("Issue loading account file. Account: " + identifier, DebugLevel.OFF);
}

try(final FileInputStream fis = new FileInputStream(accFile)) {

try {
//region, currency, amount, type
if(yaml != null) {
final YamlDocument yaml = YamlDocument.create(fis);

//Holdings.Server.Region.Currency.Handler: Balance
if(yaml.contains("Holdings")) {
final Section main = yaml.getSection("Holdings");
for(final Object serverObj : main.getKeys()) {

final String server = (String)serverObj;
if(!main.contains(server) || !main.isSection(server)) {
continue;
}
//region, currency, amount, type
if(yaml != null) {

for(final Object regionObj : main.getSection(server).getKeys()) {
//Holdings.Server.Region.Currency.Handler: Balance
if(yaml.contains("Holdings")) {
final Section main = yaml.getSection("Holdings");
for(final Object serverObj : main.getKeys()) {

final String region = (String)regionObj;
if(!main.contains(server + "." + region) || !main.isSection(server + "." + region)) {
final String server = (String)serverObj;
if(!main.contains(server) || !main.isSection(server)) {
continue;
}

for(final Object currencyObj : main.getSection(server + "." + region).getKeys()) {
for(final Object regionObj : main.getSection(server).getKeys()) {


final String currency = (String)currencyObj;
if(TNECore.eco().currency().findCurrency(currency).isEmpty()) {
EconomyManager.invalidCurrencies().add(currency);
}

if(!main.contains(server + "." + region + "." + currency) || !main.isSection(server + "." + region + "." + currency)) {
final String region = (String)regionObj;
if(!main.contains(server + "." + region) || !main.isSection(server + "." + region)) {
continue;
}

for(final Object handlerObj : main.getSection(server + "." + region + "." + currency).getKeys()) {
for(final Object currencyObj : main.getSection(server + "." + region).getKeys()) {


final String currency = (String)currencyObj;
if(TNECore.eco().currency().findCurrency(currency).isEmpty()) {
EconomyManager.invalidCurrencies().add(currency);
}

final String handler = (String)handlerObj;
final String amount = yaml.getString("Holdings." + server + "." + region + "." + currency + "." + handler, "0.0");
if(!main.contains(server + "." + region + "." + currency) || !main.isSection(server + "." + region + "." + currency)) {
continue;
}

//region, currency, amount, type
final HoldingsEntry entry = new HoldingsEntry(region,
UUID.fromString(currency),
new BigDecimal(amount),
Identifier.fromID(handler)
);
for(final Object handlerObj : main.getSection(server + "." + region + "." + currency).getKeys()) {

PluginCore.log().debug("YAMLHoldings-loadAll-Entry ID:" + entry.getHandler(), DebugLevel.DEVELOPER);
PluginCore.log().debug("YAMLHoldings-loadAll-Entry AMT:" + entry.getAmount().toPlainString(), DebugLevel.DEVELOPER);
holdings.add(entry);
final String handler = (String)handlerObj;
final String amount = yaml.getString("Holdings." + server + "." + region + "." + currency + "." + handler, "0.0");

//region, currency, amount, type
final HoldingsEntry entry = new HoldingsEntry(region,
UUID.fromString(currency),
new BigDecimal(amount),
Identifier.fromID(handler)
);

PluginCore.log().debug("YAMLHoldings-loadAll-Entry ID:" + entry.getHandler(), DebugLevel.DEVELOPER);
PluginCore.log().debug("YAMLHoldings-loadAll-Entry AMT:" + entry.getAmount().toPlainString(), DebugLevel.DEVELOPER);
holdings.add(entry);
}
}
}
}
}
}
}
} catch(final Exception ignore) {
} catch(final IOException ignore) {

PluginCore.log().error("Issue loading account file. Skipping. Account: " + identifier, DebugLevel.OFF);
PluginCore.log().error("Issue loading account file. Account: " + identifier, DebugLevel.OFF);
}
yaml = null;
}
return holdings;
}
Expand Down
Loading

0 comments on commit 1a0a9ca

Please sign in to comment.