Skip to content

Commit

Permalink
Finish implementing JSONObjects.
Browse files Browse the repository at this point in the history
  • Loading branch information
creatorfromhell committed Dec 31, 2023
1 parent c9503c9 commit 4b9ed57
Show file tree
Hide file tree
Showing 6 changed files with 315 additions and 11 deletions.
6 changes: 4 additions & 2 deletions .changelog/0.1.2.6.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ Internals
- Denomination
- Note
- TaxEntry
- Holdings
- Account
- Member
- HoldingsEntry
- Updated to the latest version of TNML.
- The latest version of TNML gives stability and will allow for menus to be added into TNE now.

Expand All @@ -45,4 +46,5 @@ Fixes
- Fixed issue where empty entries in the enchantments/flags fields for currency notes would cause issues.
- Fixed issue where Essentials would send an OfflinePlayer with a null name
- Removed module download functionality from Sponge as requested by Sponge staff
- Fixed issue where /money wasn't displaying for multi-currency correctly.
- Fixed issue where /money wasn't displaying for multi-currency correctly.
- Fixed issue where using /tne reset wouldn't clear the cache.
159 changes: 159 additions & 0 deletions Core/src/net/tnemc/core/io/serialization/impl/SerialAccount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package net.tnemc.core.io.serialization.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 net.tnemc.core.EconomyManager;
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.account.holdings.HoldingsEntry;
import net.tnemc.core.account.holdings.Wallet;
import net.tnemc.core.account.shared.Member;
import net.tnemc.core.api.response.AccountAPIResponse;
import net.tnemc.core.io.serialization.JSONAble;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.util.Optional;
import java.util.UUID;

/**
* SerialAccount
*
* @author creatorfromhell
* @since 0.1.2.0
*/
public class SerialAccount implements JSONAble<Account> {

/**
* Used to serialize this object to a JSON-valid string.
*
* @param account The object to serialize.
*
* @return The {@link JSONObject} associated with the JSON-valid String.
*/
@Override
public JSONObject toJSON(Account account) {
final JSONObject json = new JSONObject();

json.put("identifier", account.getIdentifier());
json.put("name", account.getName());
json.put("creationDate", account.getCreationDate());
json.put("pin", account.getPin());
json.put("status", account.getStatus().identifier());
json.put("type", account.type());

final JSONArray holdingsArray = new JSONArray();
for (HoldingsEntry entry : account.getWallet().entryList()) {
holdingsArray.add(new SerialHoldings().toJSON(entry));
}
json.put("holdings", holdingsArray);

if(account instanceof SharedAccount shared) {
json.put("owner", shared.getOwner());

final JSONArray membersArray = new JSONArray();
for (Member member : shared.getMembers().values()) {
membersArray.add(new SerialMember().toJSON(member));
}
json.put("members", membersArray);
}

if(account instanceof PlayerAccount player) {
json.put("lastOnline", player.getLastOnline());
json.put("language", player.getLanguage());
}

return json;
}

/**
* Used to generate information for this object from
*
* @param serialized The JSON-valid String that we are going to deserialize.
*
* @return The object that was deserialized from the JSON string.
*/
@Override
public Account fromJSON(String serialized) {
try {
final JSONParser parser = new JSONParser();
final JSONObject json = (JSONObject)parser.parse(serialized);

final String identifier = (String)json.get("identifier");
final String name = (String)json.get("name");
final String type = (String)json.get("type");

final AccountAPIResponse response = TNECore.eco().account().createAccount(identifier,
name,
!(type.equalsIgnoreCase("player") ||
type.equalsIgnoreCase("bedrock")));
if(response.getResponse().success()) {

final Optional<Account> account = response.getAccount();
if(account.isPresent()) {

//Basic account variables
account.get().setPin((String)json.get("pin"));
account.get().setCreationDate((Long)json.get("creationDate"));
account.get().setStatus(TNECore.eco().account().findStatus((String)json.get("status")));

final JSONArray holdingsArray = (JSONArray) json.get("holdings");
final Wallet wallet = new Wallet();
for(Object entryObj : holdingsArray) {
final JSONObject entryJson = (JSONObject)entryObj;
final HoldingsEntry entry = new SerialHoldings().fromJSON(entryJson.toJSONString());
wallet.setHoldings(entry);
}


//Shared accounts.
if(account.get() instanceof SharedAccount shared) {
shared.setOwner((UUID)json.get("owner"));

if(json.containsKey("members")) {

final JSONArray membersArray = (JSONArray) json.get("members");
for(Object memberObj : membersArray) {

final JSONObject memberJson = (JSONObject) memberObj;
final Member member = new SerialMember().fromJSON(memberJson.toJSONString());
shared.getMembers().put(member.getId(), member);
}
}
}

//Player-based accounts.
if(account.get() instanceof PlayerAccount player) {

player.setLastOnline((Long)json.get("lastOnline"));
player.setLanguage((String)json.get("language"));
}
return account.get();
}
}
} catch (ParseException | NumberFormatException | ClassCastException e) {
e.printStackTrace();
return null;
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,28 +141,29 @@ public Currency fromJSON(String serialized) {
currency.setDecimalPlaces(((Long)jsonObject.get("decimalPlaces")).intValue());
currency.setMinorWeight(((Long)jsonObject.get("minorWeight")).intValue());

// Item currency
//Item currency
if(currency instanceof ItemCurrency item) {
item.setEnderChest((Boolean)jsonObject.get("ender"));
}

// Note
//Note
if(jsonObject.containsKey("note")) {

final JSONObject noteJson = (JSONObject)jsonObject.get("note");
currency.setNote(new SerialNote().fromJSON(noteJson.toJSONString()));
}

// Denominations
//Denominations
final JSONArray denominationsArray = (JSONArray)jsonObject.get("denominations");
for(final Object denomObj : denominationsArray) {

final JSONObject denomJson = (JSONObject)denomObj;
currency.getDenominations().put(BigDecimal.valueOf(Double.parseDouble(denomJson.get("weight").toString())),
new SerialDenomination().fromJSON(denomJson.toJSONString()));
final Denomination denom = new SerialDenomination().fromJSON(denomJson.toJSONString());

currency.getDenominations().put(denom.weight(), denom);
}

// Conversion
//Conversion
final JSONArray conversionArray = (JSONArray)jsonObject.get("conversion");
for(final Object entryObj : conversionArray) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
import net.tnemc.core.currency.item.ItemDenomination;
import net.tnemc.core.io.serialization.JSONAble;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.math.BigDecimal;
import java.util.List;

/**
* SerialDenomination
Expand All @@ -45,6 +50,7 @@ public JSONObject toJSON(Denomination denom) {

if(denom instanceof ItemDenomination itemDenomination) {
json.put("enchantments", itemDenomination.getEnchantments());
json.put("item", true);
json.put("flags", itemDenomination.getFlags());
json.put("lore", itemDenomination.getLore());
json.put("material", itemDenomination.getMaterial());
Expand All @@ -63,6 +69,36 @@ public JSONObject toJSON(Denomination denom) {
*/
@Override
public Denomination fromJSON(String serialized) {
return null;
final JSONParser parser = new JSONParser();
JSONObject jsonObject = null;
try {
jsonObject = (JSONObject) parser.parse(serialized);

final BigDecimal weight = new BigDecimal(jsonObject.get("weight").toString());

final Denomination denomination = (jsonObject.containsKey("item"))?
new ItemDenomination(weight) :
new Denomination(weight);

denomination.setSingle((String)jsonObject.get("single"));
denomination.setPlural((String)jsonObject.get("plural"));

if(denomination instanceof ItemDenomination itemDenomination) {

itemDenomination.setEnchantments((List<String>) jsonObject.get("enchantments"));
itemDenomination.setFlags((List<String>) jsonObject.get("flags"));
itemDenomination.setLore((List<String>) jsonObject.get("lore"));
itemDenomination.setMaterial((String) jsonObject.get("material"));
itemDenomination.setDamage(((Long) jsonObject.get("damage")).shortValue());
itemDenomination.setName((String) jsonObject.get("name"));
itemDenomination.setCustomModel((Integer) jsonObject.get("customModel"));
itemDenomination.setTexture((String) jsonObject.get("texture"));

return itemDenomination;
}
return denomination;
} catch(ParseException e) {
return null;
}
}
}
27 changes: 25 additions & 2 deletions Core/src/net/tnemc/core/io/serialization/impl/SerialHoldings.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@

import net.tnemc.core.account.holdings.HoldingsEntry;
import net.tnemc.core.io.serialization.JSONAble;
import net.tnemc.core.utils.Identifier;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.math.BigDecimal;
import java.util.UUID;

/**
* SerialHoldings
Expand All @@ -38,7 +44,13 @@ public class SerialHoldings implements JSONAble<HoldingsEntry> {
*/
@Override
public JSONObject toJSON(HoldingsEntry holdings) {
return null;
final JSONObject json = new JSONObject();

json.put("region", holdings.getRegion());
json.put("currency", holdings.getCurrency().toString());
json.put("amount", holdings.getAmount().toString());
json.put("handler", holdings.getHandler().asID());
return json;
}

/**
Expand All @@ -50,6 +62,17 @@ public JSONObject toJSON(HoldingsEntry holdings) {
*/
@Override
public HoldingsEntry fromJSON(String serialized) {
return null;
try {
final JSONParser parser = new JSONParser();
final JSONObject json = (JSONObject)parser.parse(serialized);

return new HoldingsEntry((String)json.get("region"),
UUID.fromString((String)json.get("currency")),
new BigDecimal((String)json.get("amount")),
Identifier.fromID((String)json.get("handler")));
} catch (ParseException | NumberFormatException | ClassCastException e) {
e.printStackTrace();
return null;
}
}
}
Loading

0 comments on commit 4b9ed57

Please sign in to comment.