From 4b9ed577ecb8c0383a41ad2fde32db85e577ebcb Mon Sep 17 00:00:00 2001 From: creatorfromhell Date: Sun, 31 Dec 2023 09:23:20 -0500 Subject: [PATCH] Finish implementing JSONObjects. --- .changelog/0.1.2.6.md | 6 +- .../io/serialization/impl/SerialAccount.java | 159 ++++++++++++++++++ .../io/serialization/impl/SerialCurrency.java | 13 +- .../impl/SerialDenomination.java | 38 ++++- .../io/serialization/impl/SerialHoldings.java | 27 ++- .../io/serialization/impl/SerialMember.java | 83 +++++++++ 6 files changed, 315 insertions(+), 11 deletions(-) create mode 100644 Core/src/net/tnemc/core/io/serialization/impl/SerialAccount.java create mode 100644 Core/src/net/tnemc/core/io/serialization/impl/SerialMember.java diff --git a/.changelog/0.1.2.6.md b/.changelog/0.1.2.6.md index df9f4b49..31a5531e 100644 --- a/.changelog/0.1.2.6.md +++ b/.changelog/0.1.2.6.md @@ -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. @@ -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. \ No newline at end of file +- Fixed issue where /money wasn't displaying for multi-currency correctly. +- Fixed issue where using /tne reset wouldn't clear the cache. \ No newline at end of file diff --git a/Core/src/net/tnemc/core/io/serialization/impl/SerialAccount.java b/Core/src/net/tnemc/core/io/serialization/impl/SerialAccount.java new file mode 100644 index 00000000..dfc15de7 --- /dev/null +++ b/Core/src/net/tnemc/core/io/serialization/impl/SerialAccount.java @@ -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 . + */ + +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 { + + /** + * 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 = 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; + } +} diff --git a/Core/src/net/tnemc/core/io/serialization/impl/SerialCurrency.java b/Core/src/net/tnemc/core/io/serialization/impl/SerialCurrency.java index 264cb3c9..17424e8b 100644 --- a/Core/src/net/tnemc/core/io/serialization/impl/SerialCurrency.java +++ b/Core/src/net/tnemc/core/io/serialization/impl/SerialCurrency.java @@ -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) { diff --git a/Core/src/net/tnemc/core/io/serialization/impl/SerialDenomination.java b/Core/src/net/tnemc/core/io/serialization/impl/SerialDenomination.java index 26e6f427..3bf7b0f9 100644 --- a/Core/src/net/tnemc/core/io/serialization/impl/SerialDenomination.java +++ b/Core/src/net/tnemc/core/io/serialization/impl/SerialDenomination.java @@ -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 @@ -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()); @@ -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) jsonObject.get("enchantments")); + itemDenomination.setFlags((List) jsonObject.get("flags")); + itemDenomination.setLore((List) 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; + } } } \ No newline at end of file diff --git a/Core/src/net/tnemc/core/io/serialization/impl/SerialHoldings.java b/Core/src/net/tnemc/core/io/serialization/impl/SerialHoldings.java index 3df64978..3c746274 100644 --- a/Core/src/net/tnemc/core/io/serialization/impl/SerialHoldings.java +++ b/Core/src/net/tnemc/core/io/serialization/impl/SerialHoldings.java @@ -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 @@ -38,7 +44,13 @@ public class SerialHoldings implements JSONAble { */ @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; } /** @@ -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; + } } } \ No newline at end of file diff --git a/Core/src/net/tnemc/core/io/serialization/impl/SerialMember.java b/Core/src/net/tnemc/core/io/serialization/impl/SerialMember.java new file mode 100644 index 00000000..c7e65af0 --- /dev/null +++ b/Core/src/net/tnemc/core/io/serialization/impl/SerialMember.java @@ -0,0 +1,83 @@ +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 . + */ + +import net.tnemc.core.account.shared.Member; +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.util.UUID; + +/** + * SerialMember + * + * @author creatorfromhell + * @since 0.1.2.0 + */ +public class SerialMember implements JSONAble { + + /** + * Used to serialize this object to a JSON-valid string. + * + * @param member The object to serialize. + * + * @return The {@link JSONObject} associated with the JSON-valid String. + */ + @Override + public JSONObject toJSON(Member member) { + final JSONObject json = new JSONObject(); + json.put("id", member.getId().toString()); + + JSONObject permissionsJson = new JSONObject(); + member.getPermissions().forEach((permission, value) -> permissionsJson.put(permission, value)); + json.put("permissions", permissionsJson); + + 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 Member fromJSON(String serialized) { + try { + final JSONParser parser = new JSONParser(); + final JSONObject json = (JSONObject)parser.parse(serialized); + + final Member member = new Member(UUID.fromString((String)json.get("id"))); + + final JSONObject permissionsJson = (JSONObject) json.get("permissions"); + permissionsJson.forEach((permission, value) -> { + if(value instanceof Boolean) { + member.addPermission((String) permission, (Boolean) value); + } + }); + + return member; + } catch (ParseException | NumberFormatException | ClassCastException e) { + e.printStackTrace(); + return null; + } + } +} \ No newline at end of file