#1265 Limbo: fallback to old "group" during deserialization, favor old limbo's groups over new limbo's

This commit is contained in:
ljacqu
2017-07-02 10:55:16 +02:00
parent c758e15cd7
commit ff99b63385
9 changed files with 55 additions and 26 deletions
@@ -5,7 +5,6 @@ import org.bukkit.Location;
import org.bukkit.scheduler.BukkitTask;
import java.util.Collection;
import java.util.List;
/**
* Represents a player which is not logged in and keeps track of certain states (like OP status, flying)
@@ -18,14 +17,15 @@ public class LimboPlayer {
private final boolean canFly;
private final boolean operator;
private Collection<String> groups;
private final Collection<String> groups;
private final Location loc;
private final float walkSpeed;
private final float flySpeed;
private BukkitTask timeoutTask = null;
private MessageTask messageTask = null;
public LimboPlayer(Location loc, boolean operator, Collection<String> groups, boolean fly, float walkSpeed, float flySpeed) {
public LimboPlayer(Location loc, boolean operator, Collection<String> groups, boolean fly, float walkSpeed,
float flySpeed) {
this.loc = loc;
this.operator = operator;
this.groups = groups;
@@ -12,6 +12,8 @@ import javax.inject.Inject;
import java.util.Collection;
import java.util.Collections;
import static fr.xephi.authme.util.Utils.isCollectionEmpty;
/**
* Helper class for the LimboService.
*/
@@ -70,7 +72,7 @@ class LimboServiceHelper {
* <ul>
* <li><code>isOperator, allowFlight</code>: true if either limbo has true</li>
* <li><code>flySpeed, walkSpeed</code>: maximum value of either limbo player</li>
* <li><code>group, location</code>: from old limbo if not empty/null, otherwise from new limbo</li>
* <li><code>groups, location</code>: from old limbo if not empty/null, otherwise from new limbo</li>
* </ul>
*
* @param newLimbo the new limbo player
@@ -88,7 +90,7 @@ class LimboServiceHelper {
boolean canFly = newLimbo.isCanFly() || oldLimbo.isCanFly();
float flySpeed = Math.max(newLimbo.getFlySpeed(), oldLimbo.getFlySpeed());
float walkSpeed = Math.max(newLimbo.getWalkSpeed(), oldLimbo.getWalkSpeed());
Collection<String> groups = newLimbo.getGroups();
Collection<String> groups = getLimboGroups(oldLimbo.getGroups(), newLimbo.getGroups());
Location location = firstNotNull(oldLimbo.getLocation(), newLimbo.getLocation());
return new LimboPlayer(location, isOperator, groups, canFly, walkSpeed, flySpeed);
@@ -97,4 +99,10 @@ class LimboServiceHelper {
private static Location firstNotNull(Location first, Location second) {
return first == null ? second : first;
}
private static Collection<String> getLimboGroups(Collection<String> oldLimboGroups,
Collection<String> newLimboGroups) {
ConsoleLogger.debug("Limbo merge: new and old groups are `{0}` and `{1}`", newLimboGroups, oldLimboGroups);
return isCollectionEmpty(oldLimboGroups) ? newLimboGroups : oldLimboGroups;
}
}
@@ -29,12 +29,15 @@ import static fr.xephi.authme.data.limbo.persistence.LimboPlayerSerializer.LOC_Y
import static fr.xephi.authme.data.limbo.persistence.LimboPlayerSerializer.LOC_YAW;
import static fr.xephi.authme.data.limbo.persistence.LimboPlayerSerializer.LOC_Z;
import static fr.xephi.authme.data.limbo.persistence.LimboPlayerSerializer.WALK_SPEED;
import static java.util.Optional.ofNullable;
/**
* Converts a JsonElement to a LimboPlayer.
*/
class LimboPlayerDeserializer implements JsonDeserializer<LimboPlayer> {
private static final String GROUP_LEGACY = "group";
private BukkitService bukkitService;
LimboPlayerDeserializer(BukkitService bukkitService) {
@@ -51,7 +54,7 @@ class LimboPlayerDeserializer implements JsonDeserializer<LimboPlayer> {
Location loc = deserializeLocation(jsonObject);
boolean operator = getBoolean(jsonObject, IS_OP);
Collection<String> groups = getStringList(jsonObject, GROUPS);
Collection<String> groups = getLimboGroups(jsonObject);
boolean canFly = getBoolean(jsonObject, CAN_FLY);
float walkSpeed = getFloat(jsonObject, WALK_SPEED, LimboPlayer.DEFAULT_WALK_SPEED);
float flySpeed = getFloat(jsonObject, FLY_SPEED, LimboPlayer.DEFAULT_FLY_SPEED);
@@ -81,10 +84,11 @@ class LimboPlayerDeserializer implements JsonDeserializer<LimboPlayer> {
return element != null ? element.getAsString() : "";
}
private static List<String> getStringList(JsonObject jsonObject, String memberName) {
JsonElement element = jsonObject.get(memberName);
private static List<String> getLimboGroups(JsonObject jsonObject) {
JsonElement element = jsonObject.get(GROUPS);
if (element == null) {
return Collections.emptyList();
String legacyGroup = ofNullable(jsonObject.get(GROUP_LEGACY)).map(JsonElement::getAsString).orElse(null);
return legacyGroup == null ? Collections.emptyList() : Collections.singletonList(legacyGroup);
}
List<String> result = new ArrayList<>();
JsonArray jsonArray = element.getAsJsonArray();
@@ -1,7 +1,6 @@
package fr.xephi.authme.data.limbo.persistence;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
@@ -10,7 +9,6 @@ import fr.xephi.authme.data.limbo.LimboPlayer;
import org.bukkit.Location;
import java.lang.reflect.Type;
import java.util.Collection;
/**
* Converts a LimboPlayer to a JsonElement.
@@ -31,6 +29,8 @@ class LimboPlayerSerializer implements JsonSerializer<LimboPlayer> {
static final String WALK_SPEED = "walk-speed";
static final String FLY_SPEED = "fly-speed";
private static final Gson GSON = new Gson();
@Override
public JsonElement serialize(LimboPlayer limboPlayer, Type type, JsonSerializationContext context) {
@@ -45,7 +45,7 @@ class LimboPlayerSerializer implements JsonSerializer<LimboPlayer> {
JsonObject obj = new JsonObject();
obj.add(LOCATION, locationObject);
obj.add(GROUPS, new Gson().toJsonTree(limboPlayer.getGroups()).getAsJsonArray()); //TODO: maybe we should store the GSON instance somewhere. -sg
obj.add(GROUPS, GSON.toJsonTree(limboPlayer.getGroups()).getAsJsonArray());
obj.addProperty(IS_OP, limboPlayer.isOperator());
obj.addProperty(CAN_FLY, limboPlayer.isCanFly());