test save
(first /gift and when /reload)
This commit is contained in:
parent
bea5ce8088
commit
5f7a4767b9
@ -5,6 +5,8 @@ import org.bukkit.configuration.file.FileConfiguration;
|
|||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
|
import tokarotik.giftapi.NBT.cache.CacheManager;
|
||||||
|
import tokarotik.giftapi.NBT.pages.PagesManager;
|
||||||
import tokarotik.giftapi.inventory.InventoryManager;
|
import tokarotik.giftapi.inventory.InventoryManager;
|
||||||
|
|
||||||
public class APIManager {
|
public class APIManager {
|
||||||
@ -29,6 +31,13 @@ public class APIManager {
|
|||||||
|
|
||||||
public synchronized boolean add(Player player, ItemStack item)
|
public synchronized boolean add(Player player, ItemStack item)
|
||||||
{
|
{
|
||||||
|
CacheManager cache = plugin.getCacheManager();
|
||||||
|
PagesManager pages = cache.load(player);
|
||||||
|
|
||||||
|
pages.add(item);
|
||||||
|
|
||||||
|
cache.save(player, pages);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,7 +37,8 @@ public class Main extends JavaPlugin implements Listener
|
|||||||
public void onDisable()
|
public void onDisable()
|
||||||
{
|
{
|
||||||
getLogger().info("Saving GiftApi...");
|
getLogger().info("Saving GiftApi...");
|
||||||
Bukkit.getScheduler().runTaskAsynchronously(this, () -> cacheManager.disable());
|
cacheManager.disable();
|
||||||
|
//Bukkit.getScheduler().runTaskAsynchronously(this, () -> cacheManager.disable());
|
||||||
|
|
||||||
getLogger().info("GiftAPI disabled!");
|
getLogger().info("GiftAPI disabled!");
|
||||||
}
|
}
|
||||||
|
@ -22,23 +22,29 @@ public class CacheManager
|
|||||||
|
|
||||||
// Return cached PageManager if already loaded
|
// Return cached PageManager if already loaded
|
||||||
if (worldMap.containsKey(player)) {
|
if (worldMap.containsKey(player)) {
|
||||||
|
System.out.print("Is already loaded");
|
||||||
return worldMap.get(player);
|
return worldMap.get(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attempt to load from current world
|
// Attempt to load from current world
|
||||||
if (WorldNBTSynchronizer.hasAll(entityPlayer, playerWorld)) {
|
if (WorldNBTSynchronizer.hasAll(entityPlayer, playerWorld)) {
|
||||||
|
System.out.print("Load from current world");
|
||||||
return this.cacheUtil.loadAndCachePageManager(player, playerWorld, entityPlayer, worldMap);
|
return this.cacheUtil.loadAndCachePageManager(player, playerWorld, entityPlayer, worldMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attempt to load from another world if present
|
// Attempt to load from another world if present
|
||||||
String alternateWorld = WorldNBTSynchronizer.getWorldWhatHasPlayer(entityPlayer);
|
String alternateWorld = WorldNBTSynchronizer.getWorldWhatHasPlayer(entityPlayer);
|
||||||
if (alternateWorld != null) {
|
if (alternateWorld != null) {
|
||||||
|
System.out.print("Load from another world");
|
||||||
return this.cacheUtil.loadAndCachePageManager(player, alternateWorld, entityPlayer, worldMap);
|
return this.cacheUtil.loadAndCachePageManager(player, alternateWorld, entityPlayer, worldMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fallback to new empty PageManager
|
// Fallback to new empty PageManager
|
||||||
PagesManager defaultPages = new PagesManager(this.cacheUtil.max_stack);
|
PagesManager defaultPages = new PagesManager();
|
||||||
worldMap.put(player, defaultPages);
|
worldMap.put(player, defaultPages);
|
||||||
|
|
||||||
|
System.out.print("New empty PageManager");
|
||||||
|
|
||||||
return defaultPages;
|
return defaultPages;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,6 +31,8 @@ public class CacheUtil
|
|||||||
String nameWorld = world.getName();
|
String nameWorld = world.getName();
|
||||||
Map<Player, PagesManager> map = this.getWorlMap(nameWorld);
|
Map<Player, PagesManager> map = this.getWorlMap(nameWorld);
|
||||||
|
|
||||||
|
System.out.println("Map " + map.toString() + ". World: " + nameWorld);
|
||||||
|
|
||||||
if (map == null) { continue; }
|
if (map == null) { continue; }
|
||||||
|
|
||||||
PagesManager pages = map.get(player);
|
PagesManager pages = map.get(player);
|
||||||
@ -72,7 +74,7 @@ public class CacheUtil
|
|||||||
NBTTagCompound compound = new BasicNBT(world).readPlayerNBT(entityPlayer);
|
NBTTagCompound compound = new BasicNBT(world).readPlayerNBT(entityPlayer);
|
||||||
NBTTagList giftList = compound.getList("giftapi");
|
NBTTagList giftList = compound.getList("giftapi");
|
||||||
|
|
||||||
PagesManager pagesManager = new PagesManager(giftList, this.max_stack);
|
PagesManager pagesManager = new PagesManager(giftList);
|
||||||
worldMap.put(player, pagesManager);
|
worldMap.put(player, pagesManager);
|
||||||
return pagesManager;
|
return pagesManager;
|
||||||
}
|
}
|
||||||
|
@ -2,37 +2,31 @@ package tokarotik.giftapi.NBT.pages;
|
|||||||
|
|
||||||
import net.minecraft.server.v1_6_R3.NBTTagList;
|
import net.minecraft.server.v1_6_R3.NBTTagList;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import tokarotik.giftapi.NBT.pages.page.Page;
|
|
||||||
|
|
||||||
public class PagesManager
|
public class PagesManager
|
||||||
{
|
{
|
||||||
|
|
||||||
private final int max_stack;
|
|
||||||
|
|
||||||
private NBTTagList tag;
|
private NBTTagList tag;
|
||||||
|
|
||||||
private int current_page = 0;
|
private int current_page = 0;
|
||||||
|
|
||||||
public PagesManager(NBTTagList tag, int max_stack)
|
public PagesManager(NBTTagList tag)
|
||||||
{
|
{
|
||||||
this.tag = tag;
|
this.tag = tag;
|
||||||
this.max_stack = max_stack;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public PagesManager(int max_stack)
|
public PagesManager()
|
||||||
{
|
{
|
||||||
this.max_stack = max_stack;
|
|
||||||
this.tag = new NBTTagList("giftapi");
|
this.tag = new NBTTagList("giftapi");
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean add(ItemStack item)
|
public void add(ItemStack item)
|
||||||
{
|
{
|
||||||
return PagesUtil.add(item, this.tag, this.max_stack);
|
PagesUtil.add(item, this.tag);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean remove(ItemStack item)
|
public void remove(ItemStack item)
|
||||||
{
|
{
|
||||||
return PagesUtil.remove(item, this.tag, this.max_stack);
|
PagesUtil.remove(item, this.tag);
|
||||||
}
|
}
|
||||||
|
|
||||||
public NBTTagList getTag() { return this.tag; }
|
public NBTTagList getTag() { return this.tag; }
|
||||||
@ -46,15 +40,4 @@ public class PagesManager
|
|||||||
|
|
||||||
public void setIntCurrentPage(int current_page) { this.current_page = current_page; }
|
public void setIntCurrentPage(int current_page) { this.current_page = current_page; }
|
||||||
|
|
||||||
public Page getPage(int number)
|
|
||||||
{
|
|
||||||
if (number > 0 && number < this.tag.size())
|
|
||||||
{
|
|
||||||
NBTTagList tag = (NBTTagList) this.tag.get(number);
|
|
||||||
|
|
||||||
return new Page(tag, this.max_stack, number);
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,50 +1,72 @@
|
|||||||
package tokarotik.giftapi.NBT.pages;
|
package tokarotik.giftapi.NBT.pages;
|
||||||
|
|
||||||
|
import net.minecraft.server.v1_6_R3.NBTTagCompound;
|
||||||
import net.minecraft.server.v1_6_R3.NBTTagList;
|
import net.minecraft.server.v1_6_R3.NBTTagList;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import tokarotik.giftapi.NBT.item.ItemLoad;
|
||||||
|
import tokarotik.giftapi.NBT.item.ItemNBT;
|
||||||
import tokarotik.giftapi.NBT.pages.page.Page;
|
import tokarotik.giftapi.NBT.pages.page.Page;
|
||||||
|
|
||||||
public class PagesUtil
|
public class PagesUtil
|
||||||
{
|
{
|
||||||
public static boolean add(ItemStack item, NBTTagList tag, int max_stack)
|
public static void add(ItemStack item, NBTTagList tag)
|
||||||
{
|
{
|
||||||
Page page = getPageWithEmptySlots(tag, max_stack);
|
NBTTagCompound nbt = ItemNBT.getTag(item);
|
||||||
|
|
||||||
boolean is_added = page.add(item);
|
tag.add(nbt);
|
||||||
if (!is_added) { return false; } // if adding is failed, when false. why need to update when function finished without result?
|
|
||||||
|
|
||||||
NBTTagList new_tag = updateTag(page, tag);
|
|
||||||
return isUpdatedTag( tag, new_tag );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean remove(ItemStack item, NBTTagList tag, int max_stack)
|
public static NBTTagList remove(ItemStack item, NBTTagList tag)
|
||||||
{
|
{
|
||||||
Page page = getPageWithEmptySlots(tag, max_stack);
|
ItemStack[] new_list = addAllExtraOne(tag, item);
|
||||||
|
NBTTagList list = itemStackListToNBTList(new_list);
|
||||||
|
|
||||||
boolean is_added = page.remove(item);
|
return list;
|
||||||
if (!is_added) { return false; } // if removing is failed, when false.
|
|
||||||
|
|
||||||
NBTTagList new_tag = updateTag(page, tag);
|
|
||||||
return isUpdatedTag( tag, new_tag );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Page getPageWithEmptySlots(NBTTagList tag, int max_stack)
|
public static ItemStack[] addAllExtraOne(NBTTagList tag, ItemStack item)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < tag.size(); i++)
|
ItemStack[] new_list = new ItemStack[tag.size()];
|
||||||
|
ItemStack[] old_list = getRawList(tag);
|
||||||
|
|
||||||
|
for (int i = 0;i < tag.size();i++)
|
||||||
{
|
{
|
||||||
|
if (!old_list[i].equals(item))
|
||||||
Page page = new Page( (NBTTagList) tag.get(i), max_stack, i);
|
|
||||||
|
|
||||||
if (!page.isOverStacked())
|
|
||||||
{
|
{
|
||||||
return page;
|
new_list[i] = old_list[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Page page = new Page(max_stack);
|
return new_list;
|
||||||
page.setNumber(tag.size());
|
}
|
||||||
|
|
||||||
return page;
|
public static NBTTagList itemStackListToNBTList(ItemStack[] items)
|
||||||
|
{
|
||||||
|
NBTTagList list = new NBTTagList();
|
||||||
|
|
||||||
|
for (ItemStack item : items) {
|
||||||
|
if (item != null) {
|
||||||
|
NBTTagCompound compound = ItemNBT.getTag(item);
|
||||||
|
|
||||||
|
list.add(compound);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ItemStack[] getRawList(NBTTagList tag)
|
||||||
|
{
|
||||||
|
ItemStack[] list = new ItemStack[tag.size()];
|
||||||
|
|
||||||
|
for (int i = 0; i < tag.size(); i++)
|
||||||
|
{
|
||||||
|
NBTTagCompound compound = (NBTTagCompound) tag.get(i);
|
||||||
|
|
||||||
|
list[i] = ItemLoad.getItem(compound);
|
||||||
|
}
|
||||||
|
|
||||||
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isUpdatedTag(NBTTagList tag, NBTTagList new_tag)
|
public static boolean isUpdatedTag(NBTTagList tag, NBTTagList new_tag)
|
||||||
|
@ -1,11 +1,13 @@
|
|||||||
package tokarotik.giftapi.dev;
|
package tokarotik.giftapi.dev;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.Material;
|
||||||
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;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
import tokarotik.giftapi.APIManager;
|
import tokarotik.giftapi.APIManager;
|
||||||
import tokarotik.giftapi.Main;
|
import tokarotik.giftapi.Main;
|
||||||
|
|
||||||
@ -29,7 +31,12 @@ public class GiftCommand implements CommandExecutor {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ItemStack item = new ItemStack(Material.BRICK, 1);
|
||||||
|
|
||||||
Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> {
|
Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> {
|
||||||
|
plugin.getCacheManager().load((Player) sender);
|
||||||
|
apiManager.add((Player) sender, item);
|
||||||
|
|
||||||
apiManager.openInventory((Player) sender);
|
apiManager.openInventory((Player) sender);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user