code refactoring
This commit is contained in:
parent
ad919e76d5
commit
f706a7e91a
4 changed files with 16 additions and 11 deletions
|
@ -11,12 +11,15 @@ import org.bukkit.Bukkit;
|
|||
import org.bukkit.event.inventory.InventoryType;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class InventoryDeserializer implements JsonDeserializer<Inventory> {
|
||||
|
||||
@Override
|
||||
public Inventory deserialize(
|
||||
JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext)
|
||||
@NotNull JsonElement jsonElement,
|
||||
Type type,
|
||||
JsonDeserializationContext jsonDeserializationContext)
|
||||
throws JsonParseException {
|
||||
Inventory inv = Bukkit.createInventory(null, InventoryType.PLAYER);
|
||||
// parse from a string, to a list of string
|
||||
|
|
|
@ -7,18 +7,18 @@ import com.google.gson.JsonSerializer;
|
|||
import java.lang.reflect.Type;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class InventorySerializer implements JsonSerializer<Inventory> {
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(
|
||||
Inventory itemStacks, Type type, JsonSerializationContext jsonSerializationContext) {
|
||||
@NotNull 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());
|
||||
.toList();
|
||||
|
||||
String s = String.join(";", collect);
|
||||
return new JsonPrimitive(s);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue