All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ru.cwcode.tkach.config.paper.jackson.modules.ItemStackDeserializer Maven / Gradle / Ivy

There is a newer version: 2.1.6
Show newest version
package ru.cwcode.tkach.config.paper.jackson.modules;

import ru.cwcode.tkach.config.relocate.com.fasterxml.jackson.core.JsonParser;
import ru.cwcode.tkach.config.relocate.com.fasterxml.jackson.core.JsonToken;
import ru.cwcode.tkach.config.relocate.com.fasterxml.jackson.databind.DeserializationContext;
import ru.cwcode.tkach.config.relocate.com.fasterxml.jackson.databind.JsonDeserializer;
import org.bukkit.inventory.ItemStack;
import ru.cwcode.cwutils.items.ItemStackUtils;

import java.io.IOException;
import java.util.Base64;

public class ItemStackDeserializer extends JsonDeserializer {
  
  @Override
  public ItemStack deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
    if (p.hasToken(JsonToken.VALUE_STRING)) {
      String str = p.getValueAsString();
      
      if (!str.contains("{")) {
        return ItemStack.deserializeBytes(Base64.getDecoder().decode(str));
      }
      
      return ItemStackUtils.fromSNBT(str);
    }
    
    return ItemStack.deserializeBytes(p.getBinaryValue());
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy