Last active
May 27, 2024 20:58
-
-
Save Arzio/8d95dd93588d182c22912a321d9e429c to your computer and use it in GitHub Desktop.
Salvando ItemStacks completos como se fossem Strings: MC 1.6.4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.arzio.exemplo; | |
import java.io.ByteArrayInputStream; | |
import java.io.ByteArrayOutputStream; | |
import java.lang.reflect.Method; | |
import org.apache.commons.lang.Validate; | |
import org.bukkit.craftbukkit.v1_6_R3.inventory.CraftItemStack; | |
import net.minecraft.server.v1_6_R3.NBTCompressedStreamTools; | |
import net.minecraft.server.v1_6_R3.NBTTagCompound; | |
/** | |
* EN: This example is ready for copy-and-paste. | |
* PT-BR: Este exemplo esta pronto para ser copiado e colado. | |
*/ | |
public class ItemStackIOUtil { | |
private static Class<?> baseEncoder; | |
private static Method encodeMethod; | |
private static Method decodeMethod; | |
private static Object base64; | |
static { | |
try { | |
// EN: We need to get the BaseEncoding class through reflection since Cauldron | |
// has a bug when using directly the class. | |
// PT-BR: Precisamos obter a classe BaseEncoding atraves de reflection porque | |
// o Cauldron tem um bug quando usa diretamente a classe. | |
baseEncoder = Class.forName("com.google.common.io.BaseEncoding"); | |
base64 = baseEncoder.getDeclaredMethod("base64").invoke(null); | |
encodeMethod = baseEncoder.getDeclaredMethod("encode", new Class[] { byte[].class }); | |
decodeMethod = baseEncoder.getDeclaredMethod("decode", new Class[] { CharSequence.class }); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
/** | |
* EN: Transforms an ItemStack into a String that can be saved anywhere. PT-BR: | |
* Transforma um ItemStack em uma String que pode ser salva em qualquer lugar. | |
* | |
* @return Base64 {@link String}. | |
*/ | |
public static String getStackAsString(org.bukkit.inventory.ItemStack itemStack) { | |
// EN: Throws NullPointerException with a error message if the stack is null | |
// PT-BR: Arremessa um NullPointerException com uma mensagem de erro se o stack for | |
// null | |
Validate.notNull(itemStack, "The ItemStack must not be null!"); | |
// EN: Creates an empty NBT Compound to be filled with our stack | |
// PT-BR: Cria um NBT Compound vazio para ser preenchido com nosso stack | |
NBTTagCompound nbtTagCompound = new NBTTagCompound(); | |
// EN: Creates a NMS ItemStack copy of a Bukkit ItemStack | |
// PT-BR: Cria uma copia de ItemStack NMS a partir do ItemStack do Bukkit | |
net.minecraft.server.v1_6_R3.ItemStack nmsItemStack = CraftItemStack.asNMSCopy(itemStack); | |
// EN: Saves the NMS ItemStack into the empty Compound | |
// PT-BR: Salva o ItemStack NMS no Compound vazio | |
nmsItemStack.save(nbtTagCompound); | |
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { | |
// EN: Saves the NBT Compound into our OutputStream | |
// PT-BR: Salva o NBT Compound em nosso OutputStream | |
NBTCompressedStreamTools.a(nbtTagCompound, outputStream); | |
// EN: Returns a Base64 String of the resulting byte array | |
// PT-BR: Retorna uma String Base64 to array de bytes resultante | |
return encode(outputStream.toByteArray()); | |
} catch (Exception e) { | |
throw new RuntimeException("Failed to save the ItemStack in a String!", e); | |
} | |
} | |
/** | |
* EN: Creates a ItemStack from a Base64 String. PT-BR: Cria um ItemStack a | |
* partir de uma String Base64. | |
* | |
* @return Bukkit {@link org.bukkit.inventory.ItemStack}. | |
*/ | |
public static org.bukkit.inventory.ItemStack createStackFromString(String itemStackString) { | |
// EN: Throws NullPointerException with a error message if the String is null | |
// PT-BR: Faz throw em NullPointerException com uma mensagem de erro se a String for | |
// null | |
Validate.notNull(itemStackString, "The String must not be null!"); | |
// EN: Decodes the String Base64, transforming it into a byte array | |
// PT-BR: Decodifica a String Base64, transformando-a em um array de bytes | |
byte[] decodedStack = decode(itemStackString); | |
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(decodedStack)) { | |
// EN: Gets a NBT Compound from the byte array | |
// PT-BR: Obtem um NBT Compound a partir do array de bytes | |
NBTTagCompound nbtTagCompound = NBTCompressedStreamTools.a(inputStream); | |
// EN: Creates a new NMS ItemStack from the Compound | |
// PT-BR: Cria um novo ItemStack NMS a partir do Compound | |
net.minecraft.server.v1_6_R3.ItemStack craftItemStack = net.minecraft.server.v1_6_R3.ItemStack | |
.createStack(nbtTagCompound); | |
// EN: Returns a Bukkit-copy of the NMS ItemStack. | |
// PT-BR: Retorna um ItemStack Bukkit copiado do ItemStack NMS. | |
return CraftItemStack.asBukkitCopy(craftItemStack); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
private static String encode(byte[] dataArray) { | |
try { | |
return (String) encodeMethod.invoke(base64, new Object[] { dataArray }); | |
} catch (Exception e) { | |
throw new RuntimeException("Something went wrong while encoding the String", e); | |
} | |
} | |
private static byte[] decode(String base64String) { | |
try { | |
return (byte[]) decodeMethod.invoke(base64, new Object[] { base64String }); | |
} catch (Exception e) { | |
throw new RuntimeException("Something went wrong while decoding the String", e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment