optimize import and code reformat
This commit is contained in:
parent
36f8e3f6f0
commit
64ef73f7e0
15 changed files with 379 additions and 371 deletions
|
@ -5,8 +5,6 @@ import static me.unurled.sacredrealms.sr.utils.Logger.error;
|
|||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import me.unurled.sacredrealms.sr.SR;
|
||||
import me.unurled.sacredrealms.sr.data.DataHandler;
|
||||
import me.unurled.sacredrealms.sr.data.Redis;
|
||||
import me.unurled.sacredrealms.sr.managers.Manager;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
package me.unurled.sacredrealms.sr.data.gson;
|
||||
|
||||
import static me.unurled.sacredrealms.sr.utils.Logger.log;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
|
@ -9,7 +7,6 @@ import com.google.gson.JsonDeserializer;
|
|||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParseException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Arrays;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.inventory.InventoryType;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
|
@ -17,26 +14,26 @@ import org.bukkit.inventory.ItemStack;
|
|||
|
||||
public class InventoryDeserializer implements JsonDeserializer<Inventory> {
|
||||
|
||||
@Override
|
||||
public Inventory deserialize(JsonElement jsonElement, Type type,
|
||||
JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
|
||||
Inventory inv = Bukkit.createInventory(null, InventoryType.PLAYER);
|
||||
// parse from a string, to a list of string
|
||||
String[] contents = jsonElement.getAsString().split(";");
|
||||
@Override
|
||||
public Inventory deserialize(
|
||||
JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext)
|
||||
throws JsonParseException {
|
||||
Inventory inv = Bukkit.createInventory(null, InventoryType.PLAYER);
|
||||
// parse from a string, to a list of string
|
||||
String[] contents = jsonElement.getAsString().split(";");
|
||||
// iterate through the list of strings and create ItemStacks with deserialize()
|
||||
Gson gson =
|
||||
new GsonBuilder()
|
||||
.registerTypeAdapter(ItemStack.class, new ItemStackDeserializer())
|
||||
.create();
|
||||
for (int i = 0; i < contents.length; i++) {
|
||||
if (contents[i].equals("null")) {
|
||||
inv.setItem(i, null);
|
||||
} else {
|
||||
ItemStack item = gson.fromJson(contents[i], ItemStack.class);
|
||||
if (item != null)
|
||||
inv.setItem(i, item);
|
||||
}
|
||||
}
|
||||
return inv;
|
||||
for (int i = 0; i < contents.length; i++) {
|
||||
if (contents[i].equals("null")) {
|
||||
inv.setItem(i, null);
|
||||
} else {
|
||||
ItemStack item = gson.fromJson(contents[i], ItemStack.class);
|
||||
if (item != null) inv.setItem(i, item);
|
||||
}
|
||||
}
|
||||
return inv;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,27 +1,26 @@
|
|||
package me.unurled.sacredrealms.sr.data.gson;
|
||||
|
||||
import static me.unurled.sacredrealms.sr.utils.Logger.error;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
|
||||
public class InventorySerializer implements JsonSerializer<Inventory> {
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(Inventory itemStacks, Type type,
|
||||
JsonSerializationContext jsonSerializationContext) {
|
||||
List<String> collect = Arrays.stream(itemStacks.getContents())
|
||||
.map(item -> item == null ? "null" : Arrays.toString(item.serializeAsBytes())).collect(Collectors.toList());
|
||||
@Override
|
||||
public JsonElement serialize(
|
||||
Inventory itemStacks, Type type, JsonSerializationContext jsonSerializationContext) {
|
||||
List<String> collect =
|
||||
Arrays.stream(itemStacks.getContents())
|
||||
.map(item -> item == null ? "null" : Arrays.toString(item.serializeAsBytes()))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
String s = String.join(";", collect);
|
||||
return new JsonPrimitive(s);
|
||||
}
|
||||
String s = String.join(";", collect);
|
||||
return new JsonPrimitive(s);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,26 +1,24 @@
|
|||
package me.unurled.sacredrealms.sr.data.gson;
|
||||
|
||||
import com.google.common.reflect.TypeToken;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParseException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Map;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
public class ItemStackDeserializer implements JsonDeserializer<ItemStack> {
|
||||
|
||||
@Override
|
||||
public ItemStack deserialize(JsonElement jsonElement, Type type,
|
||||
JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
|
||||
Gson gson = new Gson();
|
||||
byte[] mm = gson.fromJson(jsonElement, byte[].class);
|
||||
if (mm != null) {
|
||||
return ItemStack.deserializeBytes(mm);
|
||||
}
|
||||
return null;
|
||||
@Override
|
||||
public ItemStack deserialize(
|
||||
JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext)
|
||||
throws JsonParseException {
|
||||
Gson gson = new Gson();
|
||||
byte[] mm = gson.fromJson(jsonElement, byte[].class);
|
||||
if (mm != null) {
|
||||
return ItemStack.deserializeBytes(mm);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,24 +14,25 @@ import org.bukkit.potion.PotionEffectType;
|
|||
|
||||
public class PotionEffectDeserializer implements JsonDeserializer<PotionEffect> {
|
||||
|
||||
@Override
|
||||
public PotionEffect deserialize(JsonElement jsonElement, Type type,
|
||||
JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
|
||||
try {
|
||||
String pot = jsonElement.getAsString();
|
||||
pot = pot.replace("{", "").replace("}", "");
|
||||
String[] parts = pot.split(",");
|
||||
// find the potion effect type from the string part[0] is a key
|
||||
PotionEffectType pet = Registry.POTION_EFFECT_TYPE.get(new NamespacedKey("minecraft",
|
||||
parts[0]));
|
||||
int duration = Integer.parseInt(parts[1]);
|
||||
int amplifier = Integer.parseInt(parts[2]);
|
||||
boolean particles = Boolean.parseBoolean(parts[3]);
|
||||
@Override
|
||||
public PotionEffect deserialize(
|
||||
JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext)
|
||||
throws JsonParseException {
|
||||
try {
|
||||
String pot = jsonElement.getAsString();
|
||||
pot = pot.replace("{", "").replace("}", "");
|
||||
String[] parts = pot.split(",");
|
||||
// find the potion effect type from the string part[0] is a key
|
||||
PotionEffectType pet =
|
||||
Registry.POTION_EFFECT_TYPE.get(new NamespacedKey("minecraft", parts[0]));
|
||||
int duration = Integer.parseInt(parts[1]);
|
||||
int amplifier = Integer.parseInt(parts[2]);
|
||||
boolean particles = Boolean.parseBoolean(parts[3]);
|
||||
|
||||
return new PotionEffect(pet, duration, amplifier, false, particles, false);
|
||||
} catch(Exception e) {
|
||||
error("Error deserializing potion effect: " + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
return new PotionEffect(pet, duration, amplifier, false, particles, false);
|
||||
} catch (Exception e) {
|
||||
error("Error deserializing potion effect: " + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package me.unurled.sacredrealms.sr.data.gson;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
|
@ -10,18 +9,17 @@ import org.bukkit.potion.PotionEffect;
|
|||
|
||||
public class PotionEffectSerializer implements JsonSerializer<PotionEffect> {
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(PotionEffect potionEffect, Type type,
|
||||
JsonSerializationContext jsonSerializationContext) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("{");
|
||||
sb.append(potionEffect.getType().key());
|
||||
sb.append(",");
|
||||
sb.append(potionEffect.getDuration());
|
||||
sb.append(",");
|
||||
sb.append(potionEffect.getAmplifier());
|
||||
sb.append(",").append(potionEffect.hasParticles());
|
||||
sb.append("}");
|
||||
return new JsonPrimitive(sb.toString());
|
||||
}
|
||||
@Override
|
||||
public JsonElement serialize(
|
||||
PotionEffect potionEffect, Type type, JsonSerializationContext jsonSerializationContext) {
|
||||
String sb = "{"
|
||||
+ potionEffect.getType().key()
|
||||
+ ","
|
||||
+ potionEffect.getDuration()
|
||||
+ ","
|
||||
+ potionEffect.getAmplifier()
|
||||
+ "," + potionEffect.hasParticles()
|
||||
+ "}";
|
||||
return new JsonPrimitive(sb);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue