new Serializer class uses only NMS

This commit is contained in:
BuildTools 2016-10-25 23:33:45 -04:00
parent 4d945b9193
commit 8bc9cbb7c2
7 changed files with 854 additions and 472 deletions

View file

@ -2,17 +2,20 @@ package iie;
import java.io.File; import java.io.File;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
public class PerWorldInventoryPlugin extends JavaPlugin { public class PerWorldInventoryPlugin extends JavaPlugin {
public void onEnable(){ public void onEnable(){
getServer().getPluginManager().registerEvents(new WorldChangeListener(this), this); PluginManager manager = getServer().getPluginManager();
createConfig(); manager.registerEvents(new WorldChangeListener(this), this);
PerWorldInventoryPlugin.getPlugin(this.getClass()).getServer().getListeningPluginChannels();
createConfig();
} }
private void createConfig(){ private void createConfig(){
try { try {
@ -20,14 +23,15 @@ public class PerWorldInventoryPlugin extends JavaPlugin {
if (!getDataFolder().exists()) if (!getDataFolder().exists())
getDataFolder().mkdirs(); getDataFolder().mkdirs();
//CONFIG.YML
File file = new File(getDataFolder(), "config.yml"); File file = new File(getDataFolder(), "config.yml");
if (!file.exists()){ if (!file.exists()){
getLogger().info("Config.yml not found, creating!"); getLogger().info("Config.yml not found, creating!");
saveDefaultConfig(); saveDefaultConfig();
}else{ }else
getLogger().info("Config.yml found, loading!"); getLogger().info("Config.yml found, loading!");
}
}catch (Exception e) { }catch (Exception e) {

24
src/iie/PlayerData.java Normal file
View file

@ -0,0 +1,24 @@
package iie;
import org.bukkit.Location;
import org.bukkit.entity.Player;
public class PlayerData {
public static class Position {
public static String StringFromLocation(Player player){
Location location = player.getLocation();
return "x:" + location.getBlockX() + ",z:" + location.getBlockZ() + ",y:" + location.getBlockY();
}
public Location LocationFromString(String locationString){
return null;
}
}
}

View file

@ -1,431 +1,73 @@
package iie; package iie;
import java.util.ArrayList; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.Objects;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.apache.commons.codec.binary.Base64;
import net.minecraft.server.v1_10_R1.IInventory;
import net.minecraft.server.v1_10_R1.ItemStack;
import net.minecraft.server.v1_10_R1.NBTCompressedStreamTools;
import net.minecraft.server.v1_10_R1.NBTTagCompound;
import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.DyeColor;
import org.bukkit.FireworkEffect;
import org.bukkit.Material;
import org.bukkit.block.banner.Pattern;
import org.bukkit.block.banner.PatternType;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BannerMeta;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.inventory.meta.FireworkEffectMeta;
import org.bukkit.inventory.meta.FireworkMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.inventory.meta.MapMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.inventory.meta.BookMeta.Generation;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.potion.PotionType;
public class Serializer { public class Serializer {
public static String StringFromInventory (Inventory invInventory){
String serialization = invInventory.getSize() + ";";
for (int i = 0; i < invInventory.getSize(); i++){
ItemStack is = invInventory.getItem(i); public static String serializeItemStack(ItemStack itemStack){
ItemMeta im = null;
if (is != null){ if (itemStack == null) return "null";
if (is.hasItemMeta()) im = is.getItemMeta();
String serializedItemStack = new String(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
String isType = String.valueOf(is.getType()); try { NBTCompressedStreamTools.a(itemStack.getTag(), outputStream); }
serializedItemStack += "t@" + isType; catch (IOException e) { e.printStackTrace(); }
if (is.getDurability() != 0) return Base64.encodeBase64String(outputStream.toByteArray());
serializedItemStack += ":d@" + String.valueOf(is.getDurability());
if (is.getAmount() != 1)
serializedItemStack += ":a@" + String.valueOf(is.getAmount());
Map<Enchantment,Integer> isEnch = is.getEnchantments();
if (isEnch.size() > 0)
for (Entry<Enchantment,Integer> ench : isEnch.entrySet())
serializedItemStack += ":e@" + ench.getKey().getName() + "@" + ench.getValue();
if (im == null){
serialization += i + "#" + serializedItemStack + ";";
continue;
}
//ITEM META
if (im.getDisplayName() != null){
serializedItemStack += ":n@" + is.getItemMeta().getDisplayName()
.replace("#", "{hash}")
.replace("@", "{ampersand}")
.replace(";", "{semicolon}")
.replace("'", "{single quote}")
.replace("\"", "{double quote}");
}
if (im.getLore() != null){
serializedItemStack += ":l@" + is.getItemMeta().getLore().stream()
.map(s -> s
.replace("[", "{bracket start}")
.replace("]", "{bracket end}")
.replace(",", "{comma}")
.replace("#", "{hash}")
.replace("@", "{ampersand}")
.replace(";", "{semicolon}")
.replace("'", "{single quote}")
.replace("\"", "{double quote}"))
.collect(Collectors.joining(","));
}
if (im.getItemFlags().size() > 0){
ItemFlag[] flags = ItemFlag.values();
if (im.hasItemFlag(flags[0]))
serializedItemStack += ":f@HIDE_ATTRIBUTES";
if (im.hasItemFlag(flags[1]))
serializedItemStack += ":f@HIDE_DESTROYS";
if (im.hasItemFlag(flags[2]))
serializedItemStack += ":f@HIDE_ENCHANTS";
if (im.hasItemFlag(flags[3]))
serializedItemStack += ":f@HIDE_PLACED_ON";
if (im.hasItemFlag(flags[4]))
serializedItemStack += ":f@HIDE_POTION_EFFECTS";
if (im.hasItemFlag(flags[5]))
serializedItemStack += ":f@HIDE_UNBREAKABLE";
}
//BANNER META
if (isType == "BANNER"){
BannerMeta meta = (BannerMeta) im;
serializedItemStack += ":basecolor@" + meta.getBaseColor().name();
if (meta.getPatterns().size() > 0){
serializedItemStack += ":patterns@";
String list = meta.getPatterns().stream()
.map(p -> p.getPattern().name() + "~" + p.getColor().name())
.collect(Collectors.joining(","));
serializedItemStack += list;
}
}
//BOOK META
if (isType == "WRITTEN_BOOK"){
BookMeta meta = (BookMeta) im;
if (meta.hasTitle())
serializedItemStack += ":title@" + meta.getTitle();
if (meta.hasAuthor())
serializedItemStack += ":author@" + meta.getAuthor();
//if (meta.hasGeneration())
// serializedItemStack += ":generation@" + meta.getGeneration().name();
if (meta.hasPages()){
serializedItemStack += ":pages@" + meta.getPages().stream()
.map(s -> s
.replace("º", "°")
.replace("[", "{bracket start}")
.replace("]", "{bracket end}")
.replace(",", "{comma}")
.replace("#", "{hash}")
.replace("@", "{ampersand}")
.replace(";", "{semicolon}")
.replace("'", "{single quote}")
.replace("\"", "{double quote}"))
.collect(Collectors.joining("º"));
}
}
//ENCHANTED BOOK META
if (isType == "ENCHANTED_BOOK"){
EnchantmentStorageMeta meta = (EnchantmentStorageMeta) im;
if (meta.hasStoredEnchants()){
//serializedItemStack += ":storedenchants@" + meta.getStoredEnchants().entrySet().stream()
//.map(e -> e.getKey() + "~" + String.valueOf(e.getValue()))
//.collect(Collectors.joining(","));
ArrayList<String> list = new ArrayList<String>();
meta.getStoredEnchants().forEach((k,v) -> list.add(k.getName() + "~" + v));
serializedItemStack += ":storedenchants@" + list.stream().collect(Collectors.joining(","));
}
}
//FIREWORK CHARGE META
if (isType == "FIREWORK_CHARGE"){
FireworkEffectMeta meta = (FireworkEffectMeta) im;
if (meta.hasEffect()){
FireworkEffect e = meta.getEffect();
serializedItemStack += ":charge-effect@"
+ e.getType().name() + "~"
+ e.getColors()
.stream().map(c -> String.valueOf(c.asRGB()))
.collect(Collectors.joining("/")) + "~"
+ e.getFadeColors()
.stream().map(c -> String.valueOf(c.asRGB()))
.collect(Collectors.joining("/")) + "~"
+ e.hasFlicker() + "~"
+ e.hasTrail();
}
}
//FIREWORK META
if (isType == "FIREWORK"){
FireworkMeta meta = (FireworkMeta) im;
if (meta.hasEffects())
serializedItemStack += ":firework-effects@" + meta.getEffects().stream()
.map(s -> s.getType().name() + "~"
+ s.getColors()
.stream().map(c -> String.valueOf(c.asRGB()))
.collect(Collectors.joining("/")) + "~"
+ s.getFadeColors()
.stream().map(c -> String.valueOf(c.asRGB()))
.collect(Collectors.joining("/")) + "~"
+ s.hasFlicker() + "~"
+ s.hasTrail())
.collect(Collectors.joining(","));
serializedItemStack += ":power@" + meta.getPower();
}
//LEATHER ARMOR META
if (isType == "LEATHER_HELMET" || isType == "LEATHER_CHESTPLATE" || isType == "LEATHER_LEGGINGS" || isType == "LEATHER_BOOTS")
serializedItemStack += ":color@" + ((LeatherArmorMeta) im).getColor().asRGB();
//MAP META
if (isType == "MAP")
serializedItemStack += ":scaling@" + ((MapMeta) im).isScaling();
//POTION META
if (isType == "POTION" || isType == "LINGERING_POTION" || isType == "SPLASH_POTION"){
PotionMeta meta = (PotionMeta) im;
PotionData data = (PotionData) meta.getBasePotionData();
serializedItemStack += ":effect@" + data.getType().name() + "~" + data.isExtended() + "~" + data.isUpgraded();
if (meta.hasCustomEffects())
serializedItemStack += ":customeffects@" + meta.getCustomEffects().stream()
.map(s -> s.getType().getName() + "~"
+ s.getAmplifier() + "~"
+ s.getDuration() + "~"
+ s.hasParticles() + "~"
+ s.isAmbient() + "~"
+ s.getColor().asRGB())
.collect(Collectors.joining(","));
}
//SKULL META
if (isType == "SKULL")
serializedItemStack += ":owner@" + ((SkullMeta) im).getOwner();
serialization += i + "#" + serializedItemStack + ";";
}
}
return serialization;
} }
public static ItemStack deserializeItemStack(String itemStackString){
if (itemStackString.equals("null")) return null;
ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64.decodeBase64(itemStackString));
NBTTagCompound nbtTagCompound = null;
try {nbtTagCompound = NBTCompressedStreamTools.a(inputStream);}
catch (IOException e) {e.printStackTrace();}
return ItemStack.createStack(nbtTagCompound);
public static Inventory InventoryFromString(String invString, InventoryType type){
String[] serializedBlocks = invString.split(";");
int invSize = Integer.valueOf(serializedBlocks[0]);
Inventory deserializedInventory = Bukkit.getServer().createInventory(null, type);
for (int i = 1; i < serializedBlocks.length; i++){
String[] serializedBlock = serializedBlocks[i].split("#");
int stackPosition = Integer.valueOf(serializedBlock[0]);
if (stackPosition >= invSize)
continue;
ItemStack is = null;
ItemMeta im = null;
Boolean createdItemStack = false;
String[] serializedItemStack = serializedBlock[1].split(":");
for (String itemInfo : serializedItemStack){
String[] itemAttribute = itemInfo.split("@");
if (itemAttribute[0].equals("t")){
is = new ItemStack(Material.getMaterial((itemAttribute[1])));
createdItemStack = true;
if (is.getItemMeta() != null)
im = is.getItemMeta();
}else if(itemAttribute[0].equals("d") && createdItemStack){
is.setDurability(Short.valueOf(itemAttribute[1]));
}else if(itemAttribute[0].equals("a") && createdItemStack){
is.setAmount(Integer.valueOf(itemAttribute[1]));
}else if(itemAttribute[0].equals("e") && createdItemStack){
is.addUnsafeEnchantment(Enchantment.getByName(itemAttribute[1]), Integer.valueOf(itemAttribute[2]));
}else if(itemAttribute[0].equals("n") && createdItemStack){
im.setDisplayName(itemAttribute[1]
.replace("#", "{hash}")
.replace("@", "{ampersand}")
.replace(";", "{semicolon}")
.replace("'", "{single quote}")
.replace("\"", "{double quote}"));
}else if(itemAttribute[0].equals("l") && createdItemStack){
im.setLore(Arrays.asList(itemAttribute[1].split("(,)")).stream().map(s -> s
.replace("[", "{bracket start}")
.replace("]", "{bracket end}")
.replace(",", "{comma}")
.replace("#", "{hash}")
.replace("@", "{ampersand}")
.replace(";", "{semicolon}")
.replace("'", "{single quote}")
.replace("\"", "{double quote}"))
.collect(Collectors.toList()));
}else if(itemAttribute[0].equals("f") && createdItemStack){
im.addItemFlags(ItemFlag.valueOf(itemAttribute[1]));
//BANNER ATTRIBUTES
}else if(itemAttribute[0].equals("basecolor") && createdItemStack){
((BannerMeta) im).setBaseColor(DyeColor.valueOf(itemAttribute[1]));
}else if(itemAttribute[0].equals("patterns") && createdItemStack){
((BannerMeta) im).setPatterns((List<Pattern>) Arrays.asList(itemAttribute[1].split("(,)")).stream()
.map(s -> new Pattern(
DyeColor.valueOf(Arrays.asList(s.split("(~)")).get(1)),
PatternType.valueOf(Arrays.asList(s.split("(~)")).get(0))))
.collect(Collectors.toList()));
//BOOK ATTRIBUES
}else if(itemAttribute[0].equals("title") && createdItemStack){
((BookMeta) im).setTitle(itemAttribute[1]);
}else if(itemAttribute[0].equals("author") && createdItemStack){
((BookMeta) im).setAuthor(itemAttribute[1]);
}else if(itemAttribute[0].equals("generation") && createdItemStack){
((BookMeta) im).setGeneration(Generation.valueOf(itemAttribute[1]));
}else if(itemAttribute[0].equals("pages") && createdItemStack){
((BookMeta) im).setPages(Arrays.asList(itemAttribute[1]
.replace("{bracket start}","[")
.replace("{bracket end}","]")
.replace("{comma}",",")
.replace("{hash}","#")
.replace("{ampersand}","@")
.replace("{semicolon}",";")
.replace("{single quote}","'")
.replace("{double quote}","\"")
.split("º")));
//ENCHANTED BOOK ATTRIBUTES
}else if(itemAttribute[0].equals("storedenchants") && createdItemStack){
final EnchantmentStorageMeta storedEnchants = (EnchantmentStorageMeta) im;
Arrays.asList(itemAttribute[1].split("(,)")).stream().map(e -> e.split("(~)"))
.forEach(e -> storedEnchants.addStoredEnchant(Enchantment.getByName(e[0]),Integer.valueOf(e[1]),true));
im = storedEnchants;
//FIREWORK CHARGE ATTRIBUTES
}else if(itemAttribute[0].equals("charge-effect") && createdItemStack){
String[] e = itemAttribute[1].split("(~)");
((FireworkEffectMeta) im).setEffect(
FireworkEffect.builder()
.with(FireworkEffect.Type.valueOf(e[0]))
.withColor(Arrays.asList(e[1].split("/")).stream()
.map(s -> Color.fromRGB(Integer.valueOf(s)))
.collect(Collectors.toList()))
.withFade(Arrays.asList(e[2].split("/")).stream()
.map(s -> Color.fromRGB(Integer.valueOf(s)))
.collect(Collectors.toList()))
.flicker(Boolean.valueOf(e[3]))
.trail(Boolean.valueOf(e[4]))
.build());
//FIREWORK ATTRIBUTES
}else if(itemAttribute[0].equals("firework-effects") && createdItemStack){
final FireworkMeta effects = (FireworkMeta) im;
Arrays.asList(itemAttribute[1].split("(,)")).stream()
.map(s -> s.split("(~)"))
.forEach(s -> {
effects.addEffect(
FireworkEffect.builder()
.with(FireworkEffect.Type.valueOf(s[0]))
.withColor(Arrays.asList(s[1].split("/")).stream()
.map(c -> Color.fromRGB(Integer.valueOf(c)))
.collect(Collectors.toList()))
.withFade(Arrays.asList(s[2].split("/")).stream()
.map(c -> Color.fromRGB(Integer.valueOf(c)))
.collect(Collectors.toList()))
.flicker(Boolean.valueOf(s[3]))
.trail(Boolean.valueOf(s[4]))
.build());
});
im = effects;
//LEATHER ARMOR ATTRIBUTES
}else if(itemAttribute[0].equals("color") && createdItemStack){
((LeatherArmorMeta) im).setColor(Color.fromRGB(Integer.valueOf(itemAttribute[1])));
//MAP ATTRIBUTES
}else if(itemAttribute[0].equals("scaling") && createdItemStack){
((MapMeta) im).setScaling(Boolean.valueOf(itemAttribute[1]));
//POTION ATTRIBUTES
}else if(itemAttribute[0].equals("effect") && createdItemStack){
String[] data = itemAttribute[1].split("(~)");
((PotionMeta) im).setBasePotionData(new PotionData(PotionType.valueOf(data[0]), Boolean.valueOf(data[1]), Boolean.valueOf(data[2])));
}else if(itemAttribute[0].equals("customeffects") && createdItemStack){
final PotionMeta potionMeta = (PotionMeta) im;
Arrays.asList(itemAttribute[1].split("(,)")).stream()
.forEach(s -> {
String[] e = s.split("(~)");
((PotionMeta) potionMeta).addCustomEffect(
new PotionEffect(
PotionEffectType.getByName(e[0]),
Integer.valueOf(e[1]),
Integer.valueOf(e[2]),
Boolean.valueOf(e[3]),
Boolean.valueOf(e[4]),
Color.fromRGB(Integer.valueOf(e[5]))),
false);
});
im = potionMeta;
//SKULL ATTRIBUTES
}else if(itemAttribute[0].equals("owner") && createdItemStack)
((SkullMeta) im).setOwner(itemAttribute[1]);
}
is.setItemMeta(im);
deserializedInventory.setItem(stackPosition, is);
}
return deserializedInventory;
} }
}
public static String serializeInventory (IInventory invInventory){
return IntStream.range(0, invInventory.getSize())
.mapToObj(s -> {
ItemStack i = invInventory.getItem(s);
return Objects.isNull(i) ? null : s + "#" + serializeItemStack(i);
})
.filter(s -> s != null)
.collect(Collectors.joining(";"));
}
public static void setInventoryFromSerialized (IInventory invInventory, String invString){
invInventory.l();
if (invString != null && invString.contains(";"))
Arrays.asList(invString.split("(;)"))
.stream()
.forEach(s -> {
String[] e = s.split("#");
invInventory.setItem(Integer.parseInt(e[0]), deserializeItemStack(e[1]));
});
}
}

494
src/iie/SerializerOld.java Normal file
View file

@ -0,0 +1,494 @@
package iie;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.DyeColor;
import org.bukkit.FireworkEffect;
import org.bukkit.Material;
import org.bukkit.block.banner.Pattern;
import org.bukkit.block.banner.PatternType;
import org.bukkit.craftbukkit.v1_10_R1.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_10_R1.inventory.CraftInventory;
import org.bukkit.craftbukkit.v1_10_R1.inventory.CraftInventoryPlayer;
import org.bukkit.craftbukkit.v1_10_R1.inventory.CraftItemStack;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BannerMeta;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.inventory.meta.FireworkEffectMeta;
import org.bukkit.inventory.meta.FireworkMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.inventory.meta.MapMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.inventory.meta.BookMeta.Generation;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.potion.PotionType;
import com.mojang.authlib.GameProfile;
import net.minecraft.server.v1_10_R1.MinecraftServer;
import net.minecraft.server.v1_10_R1.ChunkProviderServer;
import net.minecraft.server.v1_10_R1.DataConverterManager;
import net.minecraft.server.v1_10_R1.EntityPlayer;
import net.minecraft.server.v1_10_R1.PlayerList;
import net.minecraft.server.v1_10_R1.WorldServer;
public class SerializerOld {
//public void test(){
// GameProfile profile = new com.mojang.authlib.GameProfile(player.getUniqueId(), "");
// profile.getProperties();
//}
public static String StringFromInventory (Inventory invInventory){
String serialization = invInventory.getSize() + ";";
for (int i = 0; i < invInventory.getSize(); i++){
ItemStack is = invInventory.getItem(i);
ItemMeta im = null;
if (is != null){
if (is.hasItemMeta()) im = is.getItemMeta();
String serializedItemStack = new String();
String isType = String.valueOf(is.getType());
serializedItemStack += "t@" + isType;
if (is.getDurability() != 0)
serializedItemStack += ":d@" + String.valueOf(is.getDurability());
if (is.getAmount() != 1)
serializedItemStack += ":a@" + String.valueOf(is.getAmount());
Map<Enchantment,Integer> isEnch = is.getEnchantments();
if (isEnch.size() > 0)
for (Entry<Enchantment,Integer> ench : isEnch.entrySet())
serializedItemStack += ":e@" + ench.getKey().getName() + "@" + ench.getValue();
if (im == null){
serialization += i + "#" + serializedItemStack + ";";
continue;
}
//ITEM META
if (im.getDisplayName() != null){
serializedItemStack += ":n@" + is.getItemMeta().getDisplayName()
.replace("#", "{hash}")
.replace("@", "{ampersand}")
.replace(";", "{semicolon}")
.replace("'", "{single quote}")
.replace("\"", "{double quote}");
}
if (im.getLore() != null){
serializedItemStack += ":l@" + is.getItemMeta().getLore().stream()
.map(s -> s
.replace("[", "{bracket start}")
.replace("]", "{bracket end}")
.replace(",", "{comma}")
.replace("#", "{hash}")
.replace("@", "{ampersand}")
.replace(";", "{semicolon}")
.replace("'", "{single quote}")
.replace("\"", "{double quote}"))
.collect(Collectors.joining(","));
}
if (im.getItemFlags().size() > 0){
ItemFlag[] flags = ItemFlag.values();
if (im.hasItemFlag(flags[0]))
serializedItemStack += ":f@HIDE_ATTRIBUTES";
if (im.hasItemFlag(flags[1]))
serializedItemStack += ":f@HIDE_DESTROYS";
if (im.hasItemFlag(flags[2]))
serializedItemStack += ":f@HIDE_ENCHANTS";
if (im.hasItemFlag(flags[3]))
serializedItemStack += ":f@HIDE_PLACED_ON";
if (im.hasItemFlag(flags[4]))
serializedItemStack += ":f@HIDE_POTION_EFFECTS";
if (im.hasItemFlag(flags[5]))
serializedItemStack += ":f@HIDE_UNBREAKABLE";
}
//BANNER META
if (isType == "BANNER"){
BannerMeta meta = (BannerMeta) im;
serializedItemStack += ":basecolor@" + meta.getBaseColor().name();
if (meta.getPatterns().size() > 0){
serializedItemStack += ":patterns@";
String list = meta.getPatterns().stream()
.map(p -> p.getPattern().name() + "~" + p.getColor().name())
.collect(Collectors.joining(","));
serializedItemStack += list;
}
}
//BOOK META
if (isType == "WRITTEN_BOOK"){
BookMeta meta = (BookMeta) im;
if (meta.hasTitle())
serializedItemStack += ":title@" + meta.getTitle();
if (meta.hasAuthor())
serializedItemStack += ":author@" + meta.getAuthor();
//if (meta.hasGeneration())
// serializedItemStack += ":generation@" + meta.getGeneration().name();
if (meta.hasPages()){
serializedItemStack += ":pages@" + meta.getPages().stream()
.map(s -> s
.replace("º", "°")
.replace("[", "{bracket start}")
.replace("]", "{bracket end}")
.replace(",", "{comma}")
.replace("#", "{hash}")
.replace("@", "{ampersand}")
.replace(";", "{semicolon}")
.replace("'", "{single quote}")
.replace("\"", "{double quote}"))
.collect(Collectors.joining("º"));
}
}
//ENCHANTED BOOK META
if (isType == "ENCHANTED_BOOK"){
EnchantmentStorageMeta meta = (EnchantmentStorageMeta) im;
if (meta.hasStoredEnchants()){
//serializedItemStack += ":storedenchants@" + meta.getStoredEnchants().entrySet().stream()
//.map(e -> e.getKey() + "~" + String.valueOf(e.getValue()))
//.collect(Collectors.joining(","));
ArrayList<String> list = new ArrayList<String>();
meta.getStoredEnchants().forEach((k,v) -> list.add(k.getName() + "~" + v));
serializedItemStack += ":storedenchants@" + list.stream().collect(Collectors.joining(","));
}
}
//FIREWORK CHARGE META
if (isType == "FIREWORK_CHARGE"){
FireworkEffectMeta meta = (FireworkEffectMeta) im;
if (meta.hasEffect()){
FireworkEffect e = meta.getEffect();
serializedItemStack += ":charge-effect@"
+ e.getType().name() + "~"
+ e.getColors()
.stream().map(c -> String.valueOf(c.asRGB()))
.collect(Collectors.joining("/")) + "~"
+ e.getFadeColors()
.stream().map(c -> String.valueOf(c.asRGB()))
.collect(Collectors.joining("/")) + "~"
+ e.hasFlicker() + "~"
+ e.hasTrail();
}
}
//FIREWORK META
if (isType == "FIREWORK"){
FireworkMeta meta = (FireworkMeta) im;
if (meta.hasEffects())
serializedItemStack += ":firework-effects@" + meta.getEffects().stream()
.map(s -> s.getType().name() + "~"
+ s.getColors()
.stream().map(c -> String.valueOf(c.asRGB()))
.collect(Collectors.joining("/")) + "~"
+ s.getFadeColors()
.stream().map(c -> String.valueOf(c.asRGB()))
.collect(Collectors.joining("/")) + "~"
+ s.hasFlicker() + "~"
+ s.hasTrail())
.collect(Collectors.joining(","));
serializedItemStack += ":power@" + meta.getPower();
}
//LEATHER ARMOR META
if (isType == "LEATHER_HELMET" || isType == "LEATHER_CHESTPLATE" || isType == "LEATHER_LEGGINGS" || isType == "LEATHER_BOOTS")
serializedItemStack += ":color@" + ((LeatherArmorMeta) im).getColor().asRGB();
//MAP META
if (isType == "MAP")
serializedItemStack += ":scaling@" + ((MapMeta) im).isScaling();
//POTION META
if (isType == "POTION" || isType == "LINGERING_POTION" || isType == "SPLASH_POTION"){
PotionMeta meta = (PotionMeta) im;
PotionData data = (PotionData) meta.getBasePotionData();
serializedItemStack += ":effect@" + data.getType().name() + "~" + data.isExtended() + "~" + data.isUpgraded();
if (meta.hasCustomEffects())
serializedItemStack += ":customeffects@" + meta.getCustomEffects().stream()
.map(s -> {
String p = s.getType().getName() + "~"
+ s.getAmplifier() + "~"
+ s.getDuration() + "~"
+ s.hasParticles() + "~"
+ s.isAmbient() + "~";
if (s.getColor() != null)
return p + s.getColor().asRGB();
return p;
})
.collect(Collectors.joining(","));
}
//SKULL META
if (isType == "SKULL_ITEM"){
String texture = null;
//net.minecraft.server.v1_10_R1.ItemStack nmsItem =
texture = ((CraftInventory) invInventory)
.getInventory()
.getContents()[i]
.getTag()
.getCompound("SkullOwner")
.toString();
//texture = Base64.getUrlEncoder().encodeToString
// (
// nmsItem.getTag().getCompound("SkullOwner").toString().getBytes()
// );
//.getCompound("SkullOwner")
//.getCompound("Properties")
//.getCompound("textures")
//.getString("Value");
if (((SkullMeta) im).hasOwner())
serializedItemStack += ":owner@" + ((SkullMeta) im).getOwner();
if (texture != null)
serializedItemStack += ":texture@" + texture;
((Player) invInventory.getHolder()).sendMessage(
Serializer.serializeItemStack(
((CraftInventory) invInventory).getInventory().getContents()[i]
)
);
}
serialization += i + "#" + serializedItemStack + ";";
}
}
return serialization;
}
public static Inventory InventoryFromString(String invString, InventoryType type){
String[] serializedBlocks = invString.split(";");
int invSize = Integer.valueOf(serializedBlocks[0]);
Inventory deserializedInventory = Bukkit.getServer().createInventory(null, type);
for (int i = 1; i < serializedBlocks.length; i++){
String[] serializedBlock = serializedBlocks[i].split("#");
int stackPosition = Integer.valueOf(serializedBlock[0]);
if (stackPosition >= invSize)
continue;
ItemStack is = null;
ItemMeta im = null;
Boolean createdItemStack = false;
String[] serializedItemStack = serializedBlock[1].split(":");
for (String itemInfo : serializedItemStack){
String[] itemAttribute = itemInfo.split("@");
if (itemAttribute[0].equals("t")){
is = new ItemStack(Material.getMaterial((itemAttribute[1])));
createdItemStack = true;
if (is.getItemMeta() != null)
im = is.getItemMeta();
}else if(itemAttribute[0].equals("d") && createdItemStack){
is.setDurability(Short.valueOf(itemAttribute[1]));
}else if(itemAttribute[0].equals("a") && createdItemStack){
is.setAmount(Integer.valueOf(itemAttribute[1]));
}else if(itemAttribute[0].equals("e") && createdItemStack){
is.addUnsafeEnchantment(Enchantment.getByName(itemAttribute[1]), Integer.valueOf(itemAttribute[2]));
}else if(itemAttribute[0].equals("n") && createdItemStack){
im.setDisplayName(itemAttribute[1]
.replace("#", "{hash}")
.replace("@", "{ampersand}")
.replace(";", "{semicolon}")
.replace("'", "{single quote}")
.replace("\"", "{double quote}"));
}else if(itemAttribute[0].equals("l") && createdItemStack){
im.setLore(Arrays.asList(itemAttribute[1].split("(,)")).stream().map(s -> s
.replace("[", "{bracket start}")
.replace("]", "{bracket end}")
.replace(",", "{comma}")
.replace("#", "{hash}")
.replace("@", "{ampersand}")
.replace(";", "{semicolon}")
.replace("'", "{single quote}")
.replace("\"", "{double quote}"))
.collect(Collectors.toList()));
}else if(itemAttribute[0].equals("f") && createdItemStack){
im.addItemFlags(ItemFlag.valueOf(itemAttribute[1]));
//BANNER ATTRIBUTES
}else if(itemAttribute[0].equals("basecolor") && createdItemStack){
((BannerMeta) im).setBaseColor(DyeColor.valueOf(itemAttribute[1]));
}else if(itemAttribute[0].equals("patterns") && createdItemStack){
((BannerMeta) im).setPatterns((List<Pattern>) Arrays.asList(itemAttribute[1].split("(,)")).stream()
.map(s -> new Pattern(
DyeColor.valueOf(Arrays.asList(s.split("(~)")).get(1)),
PatternType.valueOf(Arrays.asList(s.split("(~)")).get(0))))
.collect(Collectors.toList()));
//BOOK ATTRIBUES
}else if(itemAttribute[0].equals("title") && createdItemStack){
((BookMeta) im).setTitle(itemAttribute[1]);
}else if(itemAttribute[0].equals("author") && createdItemStack){
((BookMeta) im).setAuthor(itemAttribute[1]);
}else if(itemAttribute[0].equals("generation") && createdItemStack){
((BookMeta) im).setGeneration(Generation.valueOf(itemAttribute[1]));
}else if(itemAttribute[0].equals("pages") && createdItemStack){
((BookMeta) im).setPages(Arrays.asList(itemAttribute[1]
.replace("{bracket start}","[")
.replace("{bracket end}","]")
.replace("{comma}",",")
.replace("{hash}","#")
.replace("{ampersand}","@")
.replace("{semicolon}",";")
.replace("{single quote}","'")
.replace("{double quote}","\"")
.split("º")));
//ENCHANTED BOOK ATTRIBUTES
}else if(itemAttribute[0].equals("storedenchants") && createdItemStack){
final EnchantmentStorageMeta storedEnchants = (EnchantmentStorageMeta) im;
Arrays.asList(itemAttribute[1].split("(,)")).stream().map(e -> e.split("(~)"))
.forEach(e -> storedEnchants.addStoredEnchant(Enchantment.getByName(e[0]),Integer.valueOf(e[1]),true));
im = storedEnchants;
//FIREWORK CHARGE ATTRIBUTES
}else if(itemAttribute[0].equals("charge-effect") && createdItemStack){
String[] e = itemAttribute[1].split("(~)");
((FireworkEffectMeta) im).setEffect(
FireworkEffect.builder()
.with(FireworkEffect.Type.valueOf(e[0]))
.withColor(Arrays.asList(e[1].split("/")).stream()
.map(s -> Color.fromRGB(Integer.valueOf(s)))
.collect(Collectors.toList()))
.withFade(Arrays.asList(e[2].split("/")).stream()
.map(s -> Color.fromRGB(Integer.valueOf(s)))
.collect(Collectors.toList()))
.flicker(Boolean.valueOf(e[3]))
.trail(Boolean.valueOf(e[4]))
.build());
//FIREWORK ATTRIBUTES
}else if(itemAttribute[0].equals("firework-effects") && createdItemStack){
final FireworkMeta effects = (FireworkMeta) im;
Arrays.asList(itemAttribute[1].split("(,)")).stream()
.map(s -> s.split("(~)"))
.forEach(s -> {
effects.addEffect(
FireworkEffect.builder()
.with(FireworkEffect.Type.valueOf(s[0]))
.withColor(Arrays.asList(s[1].split("/")).stream()
.map(c -> Color.fromRGB(Integer.valueOf(c)))
.collect(Collectors.toList()))
.withFade(Arrays.asList(s[2].split("/")).stream()
.map(c -> Color.fromRGB(Integer.valueOf(c)))
.collect(Collectors.toList()))
.flicker(Boolean.valueOf(s[3]))
.trail(Boolean.valueOf(s[4]))
.build());
});
im = effects;
//LEATHER ARMOR ATTRIBUTES
}else if(itemAttribute[0].equals("color") && createdItemStack){
((LeatherArmorMeta) im).setColor(Color.fromRGB(Integer.valueOf(itemAttribute[1])));
//MAP ATTRIBUTES
}else if(itemAttribute[0].equals("scaling") && createdItemStack){
((MapMeta) im).setScaling(Boolean.valueOf(itemAttribute[1]));
//POTION ATTRIBUTES
}else if(itemAttribute[0].equals("effect") && createdItemStack){
String[] data = itemAttribute[1].split("(~)");
((PotionMeta) im).setBasePotionData(new PotionData(PotionType.valueOf(data[0]), Boolean.valueOf(data[1]), Boolean.valueOf(data[2])));
}else if(itemAttribute[0].equals("customeffects") && createdItemStack){
final PotionMeta potionMeta = (PotionMeta) im;
Arrays.asList(itemAttribute[1].split("(,)")).stream()
.forEach(s -> {
String[] e = s.split("(~)");
if (e.length == 6)
((PotionMeta) potionMeta).addCustomEffect(
new PotionEffect(
PotionEffectType.getByName(e[0]),
Integer.valueOf(e[1]),
Integer.valueOf(e[2]),
Boolean.valueOf(e[3]),
Boolean.valueOf(e[4]),
Color.fromRGB(Integer.valueOf(e[5]))),
false
);
else
((PotionMeta) potionMeta).addCustomEffect(
new PotionEffect(
PotionEffectType.getByName(e[0]),
Integer.valueOf(e[1]),
Integer.valueOf(e[2]),
Boolean.valueOf(e[3]),
Boolean.valueOf(e[4])),
false
);
});
im = potionMeta;
//SKULL ATTRIBUTES
}else if(itemAttribute[0].equals("owner") && createdItemStack)
((SkullMeta) im).setOwner(itemAttribute[1]);
}
is.setItemMeta(im);
deserializedInventory.setItem(stackPosition, is);
}
return deserializedInventory;
}
}

170
src/iie/SkullMaker.java Normal file
View file

@ -0,0 +1,170 @@
package iie;
import com.google.common.collect.ForwardingMultimap;
import org.apache.commons.codec.binary.Base64;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
public class SkullMaker {
private String owner;
private String url;
private int amount = 1;
private String name;
private List<String> lore = new ArrayList<>();
public SkullMaker withAmount(int amount) {
this.amount = amount;
return this;
}
public SkullMaker withName(String name) {
this.name = name;
return this;
}
public SkullMaker withLore(String line) {
lore.add(line);
return this;
}
public SkullMaker withLore(String... lines) {
lore.addAll(Arrays.asList(lines));
return this;
}
public SkullMaker withLore(List<String> lines) {
lore.addAll(lines);
return this;
}
public SkullMaker withOwner(String ownerName) {
this.owner = ownerName;
return this;
}
public SkullMaker withSkinUrl(String url) {
this.url = url;
return this;
}
public ItemStack build() {
ItemStack item = new ItemStack(Material.SKULL_ITEM, amount, (short) 3);
SkullMeta meta = (SkullMeta) item.getItemMeta();
if (owner != null) {
meta.setOwner(owner);
} else if (url != null) {
loadProfile(meta, url);
}
if (name != null) {
meta.setDisplayName(name);
}
if (!lore.isEmpty()) {
meta.setLore(lore);
}
item.setItemMeta(meta);
return item;
}
private void loadProfile(ItemMeta meta, String url) {
Class<?> profileClass = Reflection.getClass("com.mojang.authlib.GameProfile");
Constructor<?> profileConstructor = Reflection.getDeclaredConstructor(profileClass, UUID.class, String.class);
Object profile = Reflection.newInstance(profileConstructor, UUID.randomUUID(), null);
byte[] encodedData = Base64.encodeBase64(String.format("{textures:{SKIN:{url:\"%s\"}}}", url).getBytes());
Method getPropertiesMethod = Reflection.getDeclaredMethod(profileClass, "getProperties");
Object propertyMap = Reflection.invoke(getPropertiesMethod, profile);
Class<?> propertyClass = Reflection.getClass("com.mojang.authlib.properties.Property");
Reflection.invoke(
Reflection.getDeclaredMethod(
ForwardingMultimap.class, "put", Object.class, Object.class
),
propertyMap,
"textures",
Reflection.newInstance(Reflection.getDeclaredConstructor(propertyClass, String.class, String.class), "textures", new String(encodedData))
);
Reflection.setField("profile", meta, profile);
}
private static final class Reflection {
private static Class<?> getClass(String forName) {
try {
return Class.forName(forName);
} catch (ClassNotFoundException e) {
return null;
}
}
private static <T> Constructor<T> getDeclaredConstructor(Class<T> clazz, Class<?>... params) {
try {
return clazz.getDeclaredConstructor(params);
} catch (NoSuchMethodException e) {
return null;
}
}
private static <T> T newInstance(Constructor<T> constructor, Object... params) {
try {
return constructor.newInstance(params);
} catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
return null;
}
}
private static Method getDeclaredMethod(Class<?> clazz, String name, Class<?>... params) {
try {
return clazz.getDeclaredMethod(name, params);
} catch (NoSuchMethodException e) {
return null;
}
}
private static Object invoke(Method method, Object object, Object... params) {
method.setAccessible(true);
try {
return method.invoke(object, params);
} catch (InvocationTargetException | IllegalAccessException e) {
return null;
}
}
private static void setField(String name, Object instance, Object value) {
Field field = getDeclaredField(instance.getClass(), name);
field.setAccessible(true);
try {
field.set(instance, value);
} catch (IllegalAccessException ignored) {}
}
private static Field getDeclaredField(Class<?> clazz, String name) {
try {
return clazz.getDeclaredField(name);
} catch (NoSuchFieldException e) {
return null;
}
}
}
}

View file

@ -1,12 +1,17 @@
package iie; package iie;
import java.util.UUID;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.craftbukkit.v1_10_R1.inventory.CraftInventory;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerChangedWorldEvent; import org.bukkit.event.player.PlayerChangedWorldEvent;
import org.bukkit.inventory.Inventory;
import net.minecraft.server.v1_10_R1.IInventory;
public class WorldChangeListener implements Listener { public class WorldChangeListener implements Listener {
@ -16,53 +21,71 @@ public class WorldChangeListener implements Listener {
this.plugin = plugin; this.plugin = plugin;
} }
@EventHandler(priority = EventPriority.MONITOR) @EventHandler(priority = EventPriority.MONITOR)
public void onWorldChangeListener(PlayerChangedWorldEvent event){ public void onWorldChangeListener(PlayerChangedWorldEvent event){
Player player = event.getPlayer();
String playername = player.getName(); Player player = event.getPlayer();
UUID uuid = player.getUniqueId();
World worldTo = player.getWorld(); String playerPath = ".players.(" + player.getName() + ")" + uuid.toString() + ".";
World worldFrom = event.getFrom();
String worldToName = worldTo.getName(); World worldTo = player.getWorld();
String worldFromName = worldFrom.getName(); World worldFrom = event.getFrom();
String worldToName = worldTo.getName();
String pathTo = worldToName + "." + playername + "."; String worldFromName = worldFrom.getName();
String pathFrom = worldFromName + "." + playername + ".";
String pathTo = worldToName + playerPath;
Inventory invInventory; String pathFrom = worldFromName + playerPath;
String invString;
FileConfiguration config = plugin.getConfig();
//----------------------------------------------------------------------INVENTORY
invInventory = player.getInventory(); //----------------------------------------------------------------------instantiations
invString = Serializer.StringFromInventory(invInventory);
plugin.getConfig().set(pathFrom + "inventory",invString); //Serializer serializer = new Serializer(player);
plugin.saveConfig();
invString = (String) plugin.getConfig().get(pathTo + "inventory"); //----------------------------------------------------------------------INVENTORY
player.getInventory().clear();
player.getInventory().setContents( final IInventory inventory = ((CraftInventory)player.getInventory()).getInventory();
Serializer.InventoryFromString(invString, invInventory.getType()).getContents());
config.set(pathFrom + "inventory", Serializer.serializeInventory(inventory));
plugin.saveConfig();
//----------------------------------------------------------------------ENDERCHEST
Serializer.setInventoryFromSerialized(inventory, (String) config.get(pathTo + "inventory"));
invInventory = player.getEnderChest(); //player.getInventory().clear();
invString = Serializer.StringFromInventory(invInventory); //player.getInventory().setContents(
plugin.getConfig().set(pathFrom + "enderchest",invString); // Serializer.InventoryFromString(invString, invInventory.getType())
plugin.saveConfig(); // .getContents()
// );
invString = (String) plugin.getConfig().get(pathTo + "enderchest");
//----------------------------------------------------------------------ENDERCHEST
player.getEnderChest().clear();
player.getEnderChest().setContents(
Serializer.InventoryFromString(invString, invInventory.getType()).getContents()); final IInventory enderchest = ((CraftInventory)player.getEnderChest()).getInventory();
config.set(pathFrom + "enderchest", Serializer.serializeInventory(enderchest));
plugin.saveConfig();
Serializer.setInventoryFromSerialized(enderchest, (String) config.get(pathTo + "enderchest"));
//player.getEnderChest().clear();
//player.getEnderChest().setContents(
// Serializer.InventoryFromString(invString, invInventory.getType())
// .getContents()
// );
//----------------------------------------------------------------------PLAYER DATA
config.set(pathFrom + "status", PlayerData.Position.StringFromLocation(player));
plugin.saveConfig();
} }
} }

25
src/iie/test.java Normal file
View file

@ -0,0 +1,25 @@
package iie;
import net.minecraft.server.v1_10_R1.IInventory;
public class test {
public static IInventory invInventory (){
IInventory invInventory = null;
try{
invInventory = IInventory.class.newInstance();
}catch (IllegalAccessException | InstantiationException e) {
e.printStackTrace();
}
return invInventory;
}
public void method(){
final IInventory invInventory = invInventory();
Serializer.setInventoryFromSerialized(invInventory, "string");
}
public void method2(){
method();
}
}