From 6b14bb0b56217c2954b2dfb4cc942d7d3a90b4a2 Mon Sep 17 00:00:00 2001 From: Magnus Ulf Date: Sat, 19 Jan 2019 15:11:18 +0100 Subject: [PATCH] Fix migrator bug --- .../migrator/MigratorMPlayer001Ranks.java | 50 +++++++++++++++---- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/src/com/massivecraft/factions/entity/migrator/MigratorMPlayer001Ranks.java b/src/com/massivecraft/factions/entity/migrator/MigratorMPlayer001Ranks.java index 431ab9a8..91d51677 100644 --- a/src/com/massivecraft/factions/entity/migrator/MigratorMPlayer001Ranks.java +++ b/src/com/massivecraft/factions/entity/migrator/MigratorMPlayer001Ranks.java @@ -1,8 +1,11 @@ package com.massivecraft.factions.entity.migrator; +import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; import com.massivecraft.factions.entity.Faction; +import com.massivecraft.factions.entity.FactionColl; +import com.massivecraft.factions.entity.MConf; import com.massivecraft.factions.entity.MPlayer; import com.massivecraft.factions.entity.Rank; import com.massivecraft.massivecore.store.migrator.MigratorRoot; @@ -29,18 +32,45 @@ public class MigratorMPlayer001Ranks extends MigratorRoot @Override public void migrateInner(JsonObject entity) { - String role = entity.remove("role").getAsString(); - String factionId = entity.get("factionId").getAsString(); - Faction faction = Faction.get(factionId); - - Collection ranks = faction.getRanks().getAll(); - for (Rank rank : ranks) + // Get role + JsonElement jsonRole = entity.remove("role"); + String role; + if (jsonRole == null) { - if (!rank.getName().equalsIgnoreCase(role)) continue; - - entity.add("rankId", new JsonPrimitive(rank.getId())); - break; + // The role can be null. + // Then they are probably recruit in the default faction (Wilderness). + role = null; } + else + { + role = jsonRole.getAsString(); + } + + // Get faction + JsonElement jsonFaction = entity.get("factionId"); + + String factionId; + if (jsonFaction == null) factionId = MConf.get().defaultPlayerFactionId; + else factionId = jsonFaction.getAsString(); + + Faction faction = FactionColl.get().get(factionId); + if (faction == null) faction = FactionColl.get().getNone(); + + // Get rank + Rank rank = null; + if (role != null) + { + Collection ranks = faction.getRanks().getAll(); + for (Rank r : ranks) + { + if (!r.getName().equalsIgnoreCase(role)) continue; + rank = r; + break; + } + } + if (rank == null) rank = faction.getLowestRank(); + + entity.add("rankId", new JsonPrimitive(rank.getId())); } }