test save

(first /gift and when /reload)
This commit is contained in:
ScriptCat 2025-07-28 00:29:11 +03:00
parent bea5ce8088
commit 5f7a4767b9
7 changed files with 80 additions and 50 deletions

View File

@ -5,6 +5,8 @@ import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import tokarotik.giftapi.NBT.cache.CacheManager;
import tokarotik.giftapi.NBT.pages.PagesManager;
import tokarotik.giftapi.inventory.InventoryManager;
public class APIManager {
@ -29,6 +31,13 @@ public class APIManager {
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;
}

View File

@ -37,7 +37,8 @@ public class Main extends JavaPlugin implements Listener
public void onDisable()
{
getLogger().info("Saving GiftApi...");
Bukkit.getScheduler().runTaskAsynchronously(this, () -> cacheManager.disable());
cacheManager.disable();
//Bukkit.getScheduler().runTaskAsynchronously(this, () -> cacheManager.disable());
getLogger().info("GiftAPI disabled!");
}

View File

@ -22,23 +22,29 @@ public class CacheManager
// Return cached PageManager if already loaded
if (worldMap.containsKey(player)) {
System.out.print("Is already loaded");
return worldMap.get(player);
}
// Attempt to load from current world
if (WorldNBTSynchronizer.hasAll(entityPlayer, playerWorld)) {
System.out.print("Load from current world");
return this.cacheUtil.loadAndCachePageManager(player, playerWorld, entityPlayer, worldMap);
}
// Attempt to load from another world if present
String alternateWorld = WorldNBTSynchronizer.getWorldWhatHasPlayer(entityPlayer);
if (alternateWorld != null) {
System.out.print("Load from another world");
return this.cacheUtil.loadAndCachePageManager(player, alternateWorld, entityPlayer, worldMap);
}
// Fallback to new empty PageManager
PagesManager defaultPages = new PagesManager(this.cacheUtil.max_stack);
PagesManager defaultPages = new PagesManager();
worldMap.put(player, defaultPages);
System.out.print("New empty PageManager");
return defaultPages;
}

View File

@ -31,6 +31,8 @@ public class CacheUtil
String nameWorld = world.getName();
Map<Player, PagesManager> map = this.getWorlMap(nameWorld);
System.out.println("Map " + map.toString() + ". World: " + nameWorld);
if (map == null) { continue; }
PagesManager pages = map.get(player);
@ -72,7 +74,7 @@ public class CacheUtil
NBTTagCompound compound = new BasicNBT(world).readPlayerNBT(entityPlayer);
NBTTagList giftList = compound.getList("giftapi");
PagesManager pagesManager = new PagesManager(giftList, this.max_stack);
PagesManager pagesManager = new PagesManager(giftList);
worldMap.put(player, pagesManager);
return pagesManager;
}

View File

@ -2,37 +2,31 @@ package tokarotik.giftapi.NBT.pages;
import net.minecraft.server.v1_6_R3.NBTTagList;
import org.bukkit.inventory.ItemStack;
import tokarotik.giftapi.NBT.pages.page.Page;
public class PagesManager
{
private final int max_stack;
private NBTTagList tag;
private int current_page = 0;
public PagesManager(NBTTagList tag, int max_stack)
public PagesManager(NBTTagList 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");
}
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; }
@ -46,15 +40,4 @@ public class PagesManager
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;
}
}

View File

@ -1,50 +1,72 @@
package tokarotik.giftapi.NBT.pages;
import net.minecraft.server.v1_6_R3.NBTTagCompound;
import net.minecraft.server.v1_6_R3.NBTTagList;
import org.bukkit.inventory.ItemStack;
import tokarotik.giftapi.NBT.item.ItemLoad;
import tokarotik.giftapi.NBT.item.ItemNBT;
import tokarotik.giftapi.NBT.pages.page.Page;
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);
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 );
tag.add(nbt);
}
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);
if (!is_added) { return false; } // if removing is failed, when false.
NBTTagList new_tag = updateTag(page, tag);
return isUpdatedTag( tag, new_tag );
return list;
}
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++)
{
Page page = new Page( (NBTTagList) tag.get(i), max_stack, i);
if (!page.isOverStacked())
if (!old_list[i].equals(item))
{
return page;
new_list[i] = old_list[i];
}
}
Page page = new Page(max_stack);
page.setNumber(tag.size());
return new_list;
}
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)

View File

@ -1,11 +1,13 @@
package tokarotik.giftapi.dev;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import tokarotik.giftapi.APIManager;
import tokarotik.giftapi.Main;
@ -29,7 +31,12 @@ public class GiftCommand implements CommandExecutor {
return true;
}
ItemStack item = new ItemStack(Material.BRICK, 1);
Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> {
plugin.getCacheManager().load((Player) sender);
apiManager.add((Player) sender, item);
apiManager.openInventory((Player) sender);
});