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

@ -43,16 +43,16 @@ public class CustomItem implements Listener
private AttributeContainer _attributes;
protected String _uuid;
protected boolean _dullEnchantment;
public String OriginalOwner = null;
public String getUuid()
{
return _uuid;
}
public CustomItem(String displayName, String[] description, Material material)
{
_displayName = displayName;
@ -62,12 +62,12 @@ public class CustomItem implements Listener
_uuid = UUID.randomUUID().toString();
UtilServer.RegisterEvents(this);
}
public CustomItem(Material material)
{
this(prettifyName(material), null, material);
}
/**
* @return the name displayed to players for the item.
*/
@ -75,16 +75,16 @@ public class CustomItem implements Listener
{
return ChatColor.RESET.toString() + TITLE_COLOR + _attributes.formatItemName(_displayName);
}
public String[] getDescription()
{
return _description;
}
public List<String> getLore()
{
List<String> lore = new ArrayList<String>();
if (getDescription() != null)
{
for (String desc : getDescription())
@ -92,45 +92,45 @@ public class CustomItem implements Listener
lore.add(ATTRIBUTE_COLOR + desc);
}
}
// Display attribute descriptions and stats in lore
for (ItemAttribute attribute : _attributes.getAttributes())
{
String attributeLine = ATTRIBUTE_COLOR + "" + attribute.getDescription();
lore.add(attributeLine);
}
return lore;
}
public ItemStack toItemStack(int amount)
{
ItemStack item = new ItemStack(_material, amount);
update(item);
if (_dullEnchantment)
{
UtilInv.addDullEnchantment(item);
}
return item;
}
public ItemStack toItemStack()
{
return toItemStack(1);
}
public void addDullEnchantment()
{
_dullEnchantment = true;
}
public void removeDullEnchantment()
{
_dullEnchantment = false;
}
public void onInteract(PlayerInteractEvent event)
{
for (ItemAttribute attribute : _attributes.getAttributes())
@ -138,7 +138,7 @@ public class CustomItem implements Listener
attribute.onInteract(event);
}
}
public void onAttack(CustomDamageEvent event)
{
for (ItemAttribute attribute : _attributes.getAttributes())
@ -146,7 +146,7 @@ public class CustomItem implements Listener
attribute.onAttack(event);
}
}
public void onAttacked(CustomDamageEvent event)
{
for (ItemAttribute attribute : _attributes.getAttributes())
@ -154,7 +154,7 @@ public class CustomItem implements Listener
attribute.onAttacked(event);
}
}
/**
* @param item - the item to check for a matching link
* @return true, if {@code item} matches this CustomItem via UUID, false
@ -164,40 +164,40 @@ public class CustomItem implements Listener
{
return item.getUuid().equals(_uuid);
}
/**
* Update {@code item} with the proper meta properties suited for this
* {@link CustomItem}.
*
*
* @param item - the item whose meta properties are being updated to become
* a version of this updated custom item.
*/
public void update(ItemStack item)
{
ItemMeta meta = item.getItemMeta();
String displayName = getDisplayName();
List<String> lore = getLore();
meta.setDisplayName(displayName);
meta.setLore(lore);
item.setItemMeta(meta);
GearManager.writeNBT(this, item);
}
public static String prettifyName(Material material)
{
String name = "";
String[] words = material.toString().split("_");
for (String word : words)
{
word = word.toLowerCase();
name += word.substring(0, 1).toUpperCase() + word.substring(1) + " ";
}
return name;
}

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();
}
}