Cleanup CustomItems after a while

This commit is contained in:
samczsun 2016-06-10 21:51:47 -04:00
parent 1e5d91e392
commit 67f4361959
No known key found for this signature in database
GPG Key ID: B0AFDBD87206805D
4 changed files with 59 additions and 53 deletions

View File

@ -1,8 +1,6 @@
package mineplex.game.clans.items;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Iterator;
@ -22,9 +20,6 @@ import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
@ -35,8 +30,6 @@ import com.google.gson.GsonBuilder;
import mineplex.core.MiniPlugin;
import mineplex.core.account.CoreClientManager;
import mineplex.core.common.util.C;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.common.util.UtilServer;
import mineplex.core.common.weight.Weight;
import mineplex.core.common.weight.WeightSet;
@ -224,7 +217,7 @@ public class GearManager extends MiniPlugin implements IPacketHandler, Runnable
GSON = new GsonBuilder().registerTypeAdapterFactory(attributeFactory).registerTypeAdapterFactory(customItemType).create();
}
private static Map<UUID, CustomItem> _customItemCache = new HashMap<>();
private static GearManager _instance; // Singleton instance
// Mapping of player names (key) to cached gear set (value).
@ -232,8 +225,6 @@ public class GearManager extends MiniPlugin implements IPacketHandler, Runnable
private GearShop _shop;
private static Map<UUID, CustomItem> customItemCache = new HashMap<>();
public GearManager(JavaPlugin plugin, PacketHandler packetHandler, CoreClientManager clientManager, DonationManager donationManager)
{
super("CustomGear", plugin);
@ -411,13 +402,15 @@ public class GearManager extends MiniPlugin implements IPacketHandler, Runnable
String strUUID = ((NBTTagString) data.get("gearmanager.uuid")).a_();
try
{
System.out.println("Is new item: " + strUUID);
UUID uuid = UUID.fromString(strUUID);
CustomItem customItem = customItemCache.get(uuid);
CustomItem customItem = _customItemCache.get(uuid);
if (customItem == null)
{
System.out.println("Reparsing");
String json = ((NBTTagString) data.get("gearmanager.json")).a_();
customItem = deserialize(json);
customItemCache.put(uuid, customItem);
_customItemCache.put(uuid, customItem);
}
return customItem;
}
@ -478,11 +471,13 @@ public class GearManager extends MiniPlugin implements IPacketHandler, Runnable
try
{
CustomItem customItem = deserialize(serialization);
System.out.println("Is old format: " + customItem._uuid);
System.out.println(serialization);
data.put("gearmanager.uuid", new NBTTagString(customItem._uuid));
data.put("gearmanager.json", new NBTTagString(serialization));
saveUnhandledTags(item, data);
customItemCache.put(UUID.fromString(customItem._uuid), customItem);
_customItemCache.put(UUID.fromString(customItem._uuid), customItem);
return customItem;
}
catch (Exception e)
@ -730,7 +725,26 @@ public class GearManager extends MiniPlugin implements IPacketHandler, Runnable
saveUnhandledTags(itemStack, data);
if (remove)
{
customItemCache.remove(item);
_customItemCache.remove(item);
}
}
}
public static void cleanup()
{
Iterator<CustomItem> it = _customItemCache.values().iterator();
while (it.hasNext())
{
CustomItem item = it.next();
if (item.OriginalOwner != null)
{
UUID uuid = UUID.fromString(item.OriginalOwner);
Player player = Bukkit.getPlayer(uuid);
if (player == null || !player.isOnline())
{
UtilServer.Unregister(item);
it.remove();
}
}
}
}

View File

@ -54,6 +54,7 @@ public class ItemListener implements Listener, Runnable
{
save(player, false);
}
GearManager.cleanup();
}
@EventHandler(priority = EventPriority.LOWEST)

View File

@ -184,15 +184,6 @@ public class PlayerGear
*/
public boolean cleanup()
{
if (_cleanedUp)
{
throw new RuntimeException("Already cleaned up... bug?");
}
if (getPlayer().isOnline())
{
return false;
}
forEachGear(item -> UtilServer.Unregister(item));
return true;
return !getPlayer().isOnline();
}
}