добавлено сохранение ItemStack в NBT
This commit is contained in:
parent
78f93de50f
commit
2164ba76ab
@ -47,5 +47,7 @@ public class Main extends JavaPlugin implements Listener
|
|||||||
}
|
}
|
||||||
|
|
||||||
// will open inventory GiftAPI
|
// will open inventory GiftAPI
|
||||||
public void openGUI(Player player) {apiManager.openInventory(player);}
|
public void openGUI(Player player) {
|
||||||
|
apiManager.openInventory(player);
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,6 +1,5 @@
|
|||||||
package tokarotik.giftapi.dev;
|
package tokarotik.giftapi.dev;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandExecutor;
|
import org.bukkit.command.CommandExecutor;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
@ -25,7 +24,7 @@ public class GiftCommand implements CommandExecutor {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
new NBTManager().write((Player) sender, Bukkit.getWorld("world").getWorldFolder().getAbsolutePath());
|
new NBTManager().write((Player) sender);
|
||||||
|
|
||||||
this.apiManager.openInventory((Player) sender);
|
this.apiManager.openInventory((Player) sender);
|
||||||
|
|
||||||
|
61
src/main/java/tokarotik/giftapi/savemanager/BasicNBT.java
Normal file
61
src/main/java/tokarotik/giftapi/savemanager/BasicNBT.java
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
package tokarotik.giftapi.savemanager;
|
||||||
|
|
||||||
|
import net.minecraft.server.v1_6_R3.EntityPlayer;
|
||||||
|
import net.minecraft.server.v1_6_R3.NBTCompressedStreamTools;
|
||||||
|
import net.minecraft.server.v1_6_R3.NBTTagCompound;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.craftbukkit.v1_6_R3.entity.CraftPlayer;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
|
||||||
|
public class BasicNBT {
|
||||||
|
|
||||||
|
private String path;
|
||||||
|
|
||||||
|
public boolean writePlayerNBT(NBTTagCompound compound, EntityPlayer entityPlayer)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
entityPlayer.f(compound);
|
||||||
|
|
||||||
|
FileOutputStream out = new FileOutputStream(this.path + '/' + entityPlayer.getName());
|
||||||
|
NBTCompressedStreamTools.a(compound, out);
|
||||||
|
out.close();
|
||||||
|
}
|
||||||
|
catch(Exception e)
|
||||||
|
{
|
||||||
|
Bukkit.getLogger().warning(e.toString());
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public NBTTagCompound readPlayerNBT(EntityPlayer entityPlayer)
|
||||||
|
{
|
||||||
|
NBTTagCompound compound = new NBTTagCompound();
|
||||||
|
|
||||||
|
entityPlayer.e(compound);
|
||||||
|
|
||||||
|
return compound;
|
||||||
|
}
|
||||||
|
|
||||||
|
public EntityPlayer getPlayer(Player player)
|
||||||
|
{
|
||||||
|
return ((CraftPlayer) player).getHandle();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPath() {
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return new File(".").getCanonicalFile().getAbsolutePath().toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,59 +1,26 @@
|
|||||||
package tokarotik.giftapi.savemanager;
|
package tokarotik.giftapi.savemanager;
|
||||||
|
|
||||||
import net.minecraft.server.v1_6_R3.*;
|
import net.minecraft.server.v1_6_R3.EntityPlayer;
|
||||||
import org.bukkit.Bukkit;
|
import net.minecraft.server.v1_6_R3.NBTTagCompound;
|
||||||
import org.bukkit.craftbukkit.v1_6_R3.entity.CraftPlayer;
|
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
|
|
||||||
public class NBTManager
|
public class NBTManager
|
||||||
{
|
{
|
||||||
public boolean write(Player player, String path)
|
private BasicNBT basicNBT;
|
||||||
|
|
||||||
|
public NBTManager()
|
||||||
{
|
{
|
||||||
EntityPlayer entityPlayer = getPlayer(player);
|
this.basicNBT = new BasicNBT();
|
||||||
|
|
||||||
NBTTagCompound tag = readNBT(entityPlayer);
|
|
||||||
|
|
||||||
tag.setInt("ASOME", 123);
|
|
||||||
|
|
||||||
return writeNBT(tag, entityPlayer, path + "/players/" + player.getName() + ".dat");
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private EntityPlayer getPlayer(Player player)
|
|
||||||
|
public boolean write(Player player)
|
||||||
{
|
{
|
||||||
return ((CraftPlayer) player).getHandle();
|
EntityPlayer entityPlayer = this.basicNBT.getPlayer(player);
|
||||||
}
|
|
||||||
|
|
||||||
|
NBTTagCompound tag = this.basicNBT.readPlayerNBT(entityPlayer);
|
||||||
|
|
||||||
private boolean writeNBT(NBTTagCompound compound, EntityPlayer entityPlayer, String path)
|
return this.basicNBT.writePlayerNBT(tag, entityPlayer);
|
||||||
{
|
|
||||||
entityPlayer.f(compound);
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
FileOutputStream out = new FileOutputStream(path);
|
|
||||||
NBTCompressedStreamTools.a(compound, out);
|
|
||||||
out.close();
|
|
||||||
}
|
|
||||||
catch(Exception e)
|
|
||||||
{
|
|
||||||
Bukkit.getLogger().warning(e.toString());
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private NBTTagCompound readNBT(EntityPlayer entityPlayer)
|
|
||||||
{
|
|
||||||
NBTTagCompound compound = new NBTTagCompound();
|
|
||||||
|
|
||||||
entityPlayer.e(compound);
|
|
||||||
|
|
||||||
return compound;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
29
src/main/java/tokarotik/giftapi/savemanager/item/Item.java
Normal file
29
src/main/java/tokarotik/giftapi/savemanager/item/Item.java
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package tokarotik.giftapi.savemanager.item;
|
||||||
|
|
||||||
|
import net.minecraft.server.v1_6_R3.NBTTagCompound;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
|
public class Item
|
||||||
|
{
|
||||||
|
private ItemStack item;
|
||||||
|
private NBTTagCompound tag;
|
||||||
|
|
||||||
|
public Item(ItemStack item)
|
||||||
|
{
|
||||||
|
this.item = item;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Item(NBTTagCompound tag)
|
||||||
|
{
|
||||||
|
this.tag = tag;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void getTag()
|
||||||
|
{
|
||||||
|
ItemNBTTag item = new ItemNBTTag(this.item);
|
||||||
|
|
||||||
|
this.tag = item.getTag();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package tokarotik.giftapi.savemanager.item;
|
||||||
|
|
||||||
|
public class ItemGame
|
||||||
|
{
|
||||||
|
public ItemGame()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
108
src/main/java/tokarotik/giftapi/savemanager/item/ItemNBTTag.java
Normal file
108
src/main/java/tokarotik/giftapi/savemanager/item/ItemNBTTag.java
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
package tokarotik.giftapi.savemanager.item;
|
||||||
|
|
||||||
|
import net.minecraft.server.v1_6_R3.NBTTagCompound;
|
||||||
|
import org.bukkit.enchantments.Enchantment;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class ItemNBTTag
|
||||||
|
{
|
||||||
|
ItemStack item;
|
||||||
|
|
||||||
|
public ItemNBTTag(ItemStack item)
|
||||||
|
{
|
||||||
|
this.item = item;
|
||||||
|
}
|
||||||
|
|
||||||
|
public NBTTagCompound getTag()
|
||||||
|
{
|
||||||
|
if (this.item == null) {return null;}
|
||||||
|
|
||||||
|
NBTTagCompound tag = new NBTTagCompound("giftapi");
|
||||||
|
|
||||||
|
setBasic(tag);
|
||||||
|
setMetaTag(tag);
|
||||||
|
|
||||||
|
return tag;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setBasic(NBTTagCompound tag)
|
||||||
|
{
|
||||||
|
tag.setShort("count", (short)this.item.getAmount());
|
||||||
|
tag.setInt("id", this.item.getTypeId());
|
||||||
|
tag.setShort("durability", this.item.getDurability());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setMetaTag(NBTTagCompound baseTag)
|
||||||
|
{
|
||||||
|
NBTTagCompound tag = new NBTTagCompound("meta");
|
||||||
|
|
||||||
|
if (this.item.hasItemMeta())
|
||||||
|
{
|
||||||
|
ItemMeta meta = this.item.getItemMeta();
|
||||||
|
|
||||||
|
if (meta.hasDisplayName())
|
||||||
|
{
|
||||||
|
tag.setString(
|
||||||
|
"name",
|
||||||
|
meta.getDisplayName()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (meta.hasLore())
|
||||||
|
{
|
||||||
|
tag.setCompound(
|
||||||
|
"lore",
|
||||||
|
stringListToNBTCompound(meta.getLore())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (meta.hasEnchants())
|
||||||
|
{
|
||||||
|
|
||||||
|
tag.setCompound(
|
||||||
|
"enchantments",
|
||||||
|
enchantsToNBTCompound(meta.getEnchants())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
baseTag.setCompound("meta", tag);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private NBTTagCompound stringListToNBTCompound(List<String> list)
|
||||||
|
{
|
||||||
|
NBTTagCompound NBTlist = new NBTTagCompound();
|
||||||
|
|
||||||
|
for (String s : list) {
|
||||||
|
NBTlist.setString(
|
||||||
|
null, //Integer.toString(i), - for fun
|
||||||
|
s
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return NBTlist;
|
||||||
|
}
|
||||||
|
|
||||||
|
private NBTTagCompound enchantsToNBTCompound(Map<Enchantment, Integer> enchantments)
|
||||||
|
{
|
||||||
|
NBTTagCompound NBTlist = new NBTTagCompound();
|
||||||
|
|
||||||
|
|
||||||
|
enchantments.forEach((enchantment, level) -> {
|
||||||
|
NBTTagCompound enchantmentNBT = new NBTTagCompound();
|
||||||
|
|
||||||
|
int levelNBT = 0;
|
||||||
|
if (level != null) // type Integer can be null. To not get a null error I wrote it :)
|
||||||
|
{
|
||||||
|
levelNBT = level;
|
||||||
|
}
|
||||||
|
|
||||||
|
enchantmentNBT.setInt("id", enchantment.getId());
|
||||||
|
enchantmentNBT.setInt("level", levelNBT);
|
||||||
|
});
|
||||||
|
return NBTlist;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user