Skip to content

Commit

Permalink
v1.15.1 - add custom potion effect serialization issue#59
Browse files Browse the repository at this point in the history
  • Loading branch information
Eredrim committed Dec 13, 2020
1 parent f1b6f2e commit 5f40d8d
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/net/slipcor/pvparena/core/ItemStackUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionEffect;

import java.util.*;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.bukkit.configuration.serialization.ConfigurationSerialization.deserializeObject;

Expand Down Expand Up @@ -82,9 +88,22 @@ public static Map<String, Object> getItemStackMap(ItemStack itemStack) {
if(isAnHandledMetaType(metaMap.get("meta-type").toString())) {
metaMap.remove("meta-type");

// Simplify leather armor colors
if(meta instanceof LeatherArmorMeta) {
metaMap.put("color", ((LeatherArmorMeta) meta).getColor().serialize());
}

// Simplify custom potion effects
if(meta instanceof PotionMeta) {
PotionMeta potionMeta = (PotionMeta) meta;

if(potionMeta.hasCustomEffects()) {
List<Map<String, Object>> customEffectMeta = potionMeta.getCustomEffects().stream()
.map(PotionEffect::serialize)
.collect(Collectors.toList());
metaMap.put("custom-effects", customEffectMeta);
}
}
}
result.put("meta", metaMap);
}
Expand Down Expand Up @@ -116,11 +135,20 @@ private static ItemStack deserialize(Map<String, Object> args) {
if(!metaMap.containsKey("meta-type")) {
metaMap.put("meta-type", getRightMetaType(metaMap.keySet()).name());

//Simplify Leather Armour colors
//Simplify Leather armor colors
if(metaMap.containsKey("color")) {
Color color = Color.deserialize((Map<String, Object>) metaMap.get("color"));
metaMap.put("color", color);
}

// Simplify custom potion effects
if(metaMap.containsKey("custom-effects")) {
List<PotionEffect> effectList = ((List<?>) metaMap.get("custom-effects")).stream()
.map(serializedEffect -> new PotionEffect((Map<String, Object>) serializedEffect))
.collect(Collectors.toList());

metaMap.put("custom-effects", effectList);
}
}


Expand Down

0 comments on commit 5f40d8d

Please sign in to comment.