|
| 1 | +package su.nightexpress.quantumrpg.data.api.serialize; |
| 2 | + |
| 3 | +import com.google.gson.*; |
| 4 | +import org.bukkit.inventory.ItemStack; |
| 5 | +import su.nexmedia.engine.utils.CollectionsUT; |
| 6 | +import su.nexmedia.engine.utils.ItemUT; |
| 7 | +import su.nightexpress.quantumrpg.QuantumRPG; |
| 8 | +import su.nightexpress.quantumrpg.api.QuantumAPI; |
| 9 | +import su.nightexpress.quantumrpg.data.api.UserEntityNamesMode; |
| 10 | +import su.nightexpress.quantumrpg.data.api.UserProfile; |
| 11 | +import su.nightexpress.quantumrpg.manager.effects.buffs.SavedBuff; |
| 12 | +import su.nightexpress.quantumrpg.modules.list.classes.api.RPGClass; |
| 13 | +import su.nightexpress.quantumrpg.modules.list.classes.api.UserClassData; |
| 14 | + |
| 15 | +import java.lang.reflect.Type; |
| 16 | +import java.util.HashSet; |
| 17 | +import java.util.Set; |
| 18 | + |
| 19 | +public class UserProfileDeserializer implements JsonDeserializer<UserProfile> { |
| 20 | + public UserProfile deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { |
| 21 | + JsonObject j = json.getAsJsonObject(); |
| 22 | + String id = j.get("name").getAsString(); |
| 23 | + boolean isDefault = j.get("isDefault").getAsBoolean(); |
| 24 | + Set<SavedBuff> buffDamage = new HashSet<>(); |
| 25 | + JsonElement jBuffsElem = j.get("buffDamage"); |
| 26 | + JsonArray jBuffs = null; |
| 27 | + if (jBuffsElem != null) { |
| 28 | + jBuffs = jBuffsElem.getAsJsonArray(); |
| 29 | + for (JsonElement e : jBuffs) |
| 30 | + buffDamage.add(context.deserialize(e, SavedBuff.class)); |
| 31 | + } |
| 32 | + Set<SavedBuff> buffDefense = new HashSet<>(); |
| 33 | + jBuffsElem = j.get("buffDefense"); |
| 34 | + jBuffs = null; |
| 35 | + if (jBuffsElem != null) { |
| 36 | + jBuffs = jBuffsElem.getAsJsonArray(); |
| 37 | + for (JsonElement e : jBuffs) |
| 38 | + buffDefense.add(context.deserialize(e, SavedBuff.class)); |
| 39 | + } |
| 40 | + Set<SavedBuff> buffStats = new HashSet<>(); |
| 41 | + jBuffsElem = j.get("buffStats"); |
| 42 | + jBuffs = null; |
| 43 | + if (jBuffsElem != null) { |
| 44 | + jBuffs = jBuffsElem.getAsJsonArray(); |
| 45 | + for (JsonElement e : jBuffs) |
| 46 | + buffStats.add(context.deserialize(e, SavedBuff.class)); |
| 47 | + } |
| 48 | + JsonElement eInventory = j.get("inventory"); |
| 49 | + ItemStack[] inventory = new ItemStack[41]; |
| 50 | + if (eInventory != null) { |
| 51 | + int count = 0; |
| 52 | + for (JsonElement item : eInventory.getAsJsonArray()) |
| 53 | + inventory[count++] = ItemUT.fromBase64(item.getAsString()); |
| 54 | + } |
| 55 | + JsonElement eNames = j.get("namesMode"); |
| 56 | + String namesModeRaw = (eNames != null) ? eNames.getAsString() : null; |
| 57 | + UserEntityNamesMode namesMode = (namesModeRaw != null) ? CollectionsUT.getEnum(namesModeRaw, UserEntityNamesMode.class) : UserEntityNamesMode.DEFAULT; |
| 58 | + JsonElement eHideHelmet = j.get("hideHelmet"); |
| 59 | + boolean hideHelmet = eHideHelmet != null && eHideHelmet.getAsBoolean(); |
| 60 | + UserClassData cData = null; |
| 61 | + JsonElement jData = j.get("cData"); |
| 62 | + if (jData != null && QuantumRPG.getInstance().cfg().isModuleEnabled("classes")) { |
| 63 | + JsonObject jClass = jData.getAsJsonObject(); |
| 64 | + cData = context.deserialize(jClass, UserClassData.class); |
| 65 | + String clazzId = cData.getClassId(); |
| 66 | + RPGClass clazz = QuantumAPI.getModuleManager().getClassManager().getClassById(clazzId); |
| 67 | + if (clazz == null) { |
| 68 | + System.out.println("[QuantumRPG] Player class '" + clazzId + "' no more exists."); |
| 69 | + cData = null; |
| 70 | + } else { |
| 71 | + cData.setPlayerClass(clazz); |
| 72 | + } |
| 73 | + } |
| 74 | + JsonElement jCooldown = j.get("cCooldown"); |
| 75 | + long cCooldown = 0L; |
| 76 | + if (jCooldown != null) |
| 77 | + cCooldown = jCooldown.getAsLong(); |
| 78 | + return new UserProfile( |
| 79 | + id, |
| 80 | + isDefault, |
| 81 | + |
| 82 | + buffDamage, |
| 83 | + buffDefense, |
| 84 | + buffStats, |
| 85 | + |
| 86 | + inventory, |
| 87 | + (namesMode == null) ? UserEntityNamesMode.DEFAULT : namesMode, |
| 88 | + hideHelmet, |
| 89 | + |
| 90 | + cData, |
| 91 | + cCooldown); |
| 92 | + } |
| 93 | +} |
0 commit comments