Cleanup CustomItems after a while
This commit is contained in:
parent
1e5d91e392
commit
67f4361959
|
@ -43,16 +43,16 @@ public class CustomItem implements Listener
|
||||||
private AttributeContainer _attributes;
|
private AttributeContainer _attributes;
|
||||||
|
|
||||||
protected String _uuid;
|
protected String _uuid;
|
||||||
|
|
||||||
protected boolean _dullEnchantment;
|
protected boolean _dullEnchantment;
|
||||||
|
|
||||||
public String OriginalOwner = null;
|
public String OriginalOwner = null;
|
||||||
|
|
||||||
public String getUuid()
|
public String getUuid()
|
||||||
{
|
{
|
||||||
return _uuid;
|
return _uuid;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CustomItem(String displayName, String[] description, Material material)
|
public CustomItem(String displayName, String[] description, Material material)
|
||||||
{
|
{
|
||||||
_displayName = displayName;
|
_displayName = displayName;
|
||||||
|
@ -62,12 +62,12 @@ public class CustomItem implements Listener
|
||||||
_uuid = UUID.randomUUID().toString();
|
_uuid = UUID.randomUUID().toString();
|
||||||
UtilServer.RegisterEvents(this);
|
UtilServer.RegisterEvents(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public CustomItem(Material material)
|
public CustomItem(Material material)
|
||||||
{
|
{
|
||||||
this(prettifyName(material), null, material);
|
this(prettifyName(material), null, material);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the name displayed to players for the item.
|
* @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);
|
return ChatColor.RESET.toString() + TITLE_COLOR + _attributes.formatItemName(_displayName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String[] getDescription()
|
public String[] getDescription()
|
||||||
{
|
{
|
||||||
return _description;
|
return _description;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> getLore()
|
public List<String> getLore()
|
||||||
{
|
{
|
||||||
List<String> lore = new ArrayList<String>();
|
List<String> lore = new ArrayList<String>();
|
||||||
|
|
||||||
if (getDescription() != null)
|
if (getDescription() != null)
|
||||||
{
|
{
|
||||||
for (String desc : getDescription())
|
for (String desc : getDescription())
|
||||||
|
@ -92,45 +92,45 @@ public class CustomItem implements Listener
|
||||||
lore.add(ATTRIBUTE_COLOR + desc);
|
lore.add(ATTRIBUTE_COLOR + desc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Display attribute descriptions and stats in lore
|
// Display attribute descriptions and stats in lore
|
||||||
for (ItemAttribute attribute : _attributes.getAttributes())
|
for (ItemAttribute attribute : _attributes.getAttributes())
|
||||||
{
|
{
|
||||||
String attributeLine = ATTRIBUTE_COLOR + "• " + attribute.getDescription();
|
String attributeLine = ATTRIBUTE_COLOR + "• " + attribute.getDescription();
|
||||||
lore.add(attributeLine);
|
lore.add(attributeLine);
|
||||||
}
|
}
|
||||||
|
|
||||||
return lore;
|
return lore;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ItemStack toItemStack(int amount)
|
public ItemStack toItemStack(int amount)
|
||||||
{
|
{
|
||||||
ItemStack item = new ItemStack(_material, amount);
|
ItemStack item = new ItemStack(_material, amount);
|
||||||
update(item);
|
update(item);
|
||||||
|
|
||||||
if (_dullEnchantment)
|
if (_dullEnchantment)
|
||||||
{
|
{
|
||||||
UtilInv.addDullEnchantment(item);
|
UtilInv.addDullEnchantment(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ItemStack toItemStack()
|
public ItemStack toItemStack()
|
||||||
{
|
{
|
||||||
return toItemStack(1);
|
return toItemStack(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addDullEnchantment()
|
public void addDullEnchantment()
|
||||||
{
|
{
|
||||||
_dullEnchantment = true;
|
_dullEnchantment = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeDullEnchantment()
|
public void removeDullEnchantment()
|
||||||
{
|
{
|
||||||
_dullEnchantment = false;
|
_dullEnchantment = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onInteract(PlayerInteractEvent event)
|
public void onInteract(PlayerInteractEvent event)
|
||||||
{
|
{
|
||||||
for (ItemAttribute attribute : _attributes.getAttributes())
|
for (ItemAttribute attribute : _attributes.getAttributes())
|
||||||
|
@ -138,7 +138,7 @@ public class CustomItem implements Listener
|
||||||
attribute.onInteract(event);
|
attribute.onInteract(event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onAttack(CustomDamageEvent event)
|
public void onAttack(CustomDamageEvent event)
|
||||||
{
|
{
|
||||||
for (ItemAttribute attribute : _attributes.getAttributes())
|
for (ItemAttribute attribute : _attributes.getAttributes())
|
||||||
|
@ -146,7 +146,7 @@ public class CustomItem implements Listener
|
||||||
attribute.onAttack(event);
|
attribute.onAttack(event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onAttacked(CustomDamageEvent event)
|
public void onAttacked(CustomDamageEvent event)
|
||||||
{
|
{
|
||||||
for (ItemAttribute attribute : _attributes.getAttributes())
|
for (ItemAttribute attribute : _attributes.getAttributes())
|
||||||
|
@ -154,7 +154,7 @@ public class CustomItem implements Listener
|
||||||
attribute.onAttacked(event);
|
attribute.onAttacked(event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param item - the item to check for a matching link
|
* @param item - the item to check for a matching link
|
||||||
* @return true, if {@code item} matches this CustomItem via UUID, false
|
* @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);
|
return item.getUuid().equals(_uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update {@code item} with the proper meta properties suited for this
|
* Update {@code item} with the proper meta properties suited for this
|
||||||
* {@link CustomItem}.
|
* {@link CustomItem}.
|
||||||
*
|
*
|
||||||
* @param item - the item whose meta properties are being updated to become
|
* @param item - the item whose meta properties are being updated to become
|
||||||
* a version of this updated custom item.
|
* a version of this updated custom item.
|
||||||
*/
|
*/
|
||||||
public void update(ItemStack item)
|
public void update(ItemStack item)
|
||||||
{
|
{
|
||||||
ItemMeta meta = item.getItemMeta();
|
ItemMeta meta = item.getItemMeta();
|
||||||
|
|
||||||
String displayName = getDisplayName();
|
String displayName = getDisplayName();
|
||||||
List<String> lore = getLore();
|
List<String> lore = getLore();
|
||||||
|
|
||||||
meta.setDisplayName(displayName);
|
meta.setDisplayName(displayName);
|
||||||
meta.setLore(lore);
|
meta.setLore(lore);
|
||||||
|
|
||||||
item.setItemMeta(meta);
|
item.setItemMeta(meta);
|
||||||
|
|
||||||
GearManager.writeNBT(this, item);
|
GearManager.writeNBT(this, item);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String prettifyName(Material material)
|
public static String prettifyName(Material material)
|
||||||
{
|
{
|
||||||
String name = "";
|
String name = "";
|
||||||
String[] words = material.toString().split("_");
|
String[] words = material.toString().split("_");
|
||||||
|
|
||||||
for (String word : words)
|
for (String word : words)
|
||||||
{
|
{
|
||||||
word = word.toLowerCase();
|
word = word.toLowerCase();
|
||||||
name += word.substring(0, 1).toUpperCase() + word.substring(1) + " ";
|
name += word.substring(0, 1).toUpperCase() + word.substring(1) + " ";
|
||||||
}
|
}
|
||||||
|
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
package mineplex.game.clans.items;
|
package mineplex.game.clans.items;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
@ -22,9 +20,6 @@ import org.bukkit.Material;
|
||||||
import org.bukkit.OfflinePlayer;
|
import org.bukkit.OfflinePlayer;
|
||||||
import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack;
|
import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack;
|
||||||
import org.bukkit.entity.Player;
|
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.ItemStack;
|
||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
@ -35,8 +30,6 @@ import com.google.gson.GsonBuilder;
|
||||||
import mineplex.core.MiniPlugin;
|
import mineplex.core.MiniPlugin;
|
||||||
import mineplex.core.account.CoreClientManager;
|
import mineplex.core.account.CoreClientManager;
|
||||||
import mineplex.core.common.util.C;
|
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.util.UtilServer;
|
||||||
import mineplex.core.common.weight.Weight;
|
import mineplex.core.common.weight.Weight;
|
||||||
import mineplex.core.common.weight.WeightSet;
|
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();
|
GSON = new GsonBuilder().registerTypeAdapterFactory(attributeFactory).registerTypeAdapterFactory(customItemType).create();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Map<UUID, CustomItem> _customItemCache = new HashMap<>();
|
||||||
private static GearManager _instance; // Singleton instance
|
private static GearManager _instance; // Singleton instance
|
||||||
|
|
||||||
// Mapping of player names (key) to cached gear set (value).
|
// 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 GearShop _shop;
|
||||||
|
|
||||||
private static Map<UUID, CustomItem> customItemCache = new HashMap<>();
|
|
||||||
|
|
||||||
public GearManager(JavaPlugin plugin, PacketHandler packetHandler, CoreClientManager clientManager, DonationManager donationManager)
|
public GearManager(JavaPlugin plugin, PacketHandler packetHandler, CoreClientManager clientManager, DonationManager donationManager)
|
||||||
{
|
{
|
||||||
super("CustomGear", plugin);
|
super("CustomGear", plugin);
|
||||||
|
@ -411,13 +402,15 @@ public class GearManager extends MiniPlugin implements IPacketHandler, Runnable
|
||||||
String strUUID = ((NBTTagString) data.get("gearmanager.uuid")).a_();
|
String strUUID = ((NBTTagString) data.get("gearmanager.uuid")).a_();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
System.out.println("Is new item: " + strUUID);
|
||||||
UUID uuid = UUID.fromString(strUUID);
|
UUID uuid = UUID.fromString(strUUID);
|
||||||
CustomItem customItem = customItemCache.get(uuid);
|
CustomItem customItem = _customItemCache.get(uuid);
|
||||||
if (customItem == null)
|
if (customItem == null)
|
||||||
{
|
{
|
||||||
|
System.out.println("Reparsing");
|
||||||
String json = ((NBTTagString) data.get("gearmanager.json")).a_();
|
String json = ((NBTTagString) data.get("gearmanager.json")).a_();
|
||||||
customItem = deserialize(json);
|
customItem = deserialize(json);
|
||||||
customItemCache.put(uuid, customItem);
|
_customItemCache.put(uuid, customItem);
|
||||||
}
|
}
|
||||||
return customItem;
|
return customItem;
|
||||||
}
|
}
|
||||||
|
@ -478,11 +471,13 @@ public class GearManager extends MiniPlugin implements IPacketHandler, Runnable
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
CustomItem customItem = deserialize(serialization);
|
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.uuid", new NBTTagString(customItem._uuid));
|
||||||
data.put("gearmanager.json", new NBTTagString(serialization));
|
data.put("gearmanager.json", new NBTTagString(serialization));
|
||||||
saveUnhandledTags(item, data);
|
saveUnhandledTags(item, data);
|
||||||
customItemCache.put(UUID.fromString(customItem._uuid), customItem);
|
_customItemCache.put(UUID.fromString(customItem._uuid), customItem);
|
||||||
return customItem;
|
return customItem;
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
|
@ -730,7 +725,26 @@ public class GearManager extends MiniPlugin implements IPacketHandler, Runnable
|
||||||
saveUnhandledTags(itemStack, data);
|
saveUnhandledTags(itemStack, data);
|
||||||
if (remove)
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,6 +54,7 @@ public class ItemListener implements Listener, Runnable
|
||||||
{
|
{
|
||||||
save(player, false);
|
save(player, false);
|
||||||
}
|
}
|
||||||
|
GearManager.cleanup();
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler(priority = EventPriority.LOWEST)
|
@EventHandler(priority = EventPriority.LOWEST)
|
||||||
|
|
|
@ -184,15 +184,6 @@ public class PlayerGear
|
||||||
*/
|
*/
|
||||||
public boolean cleanup()
|
public boolean cleanup()
|
||||||
{
|
{
|
||||||
if (_cleanedUp)
|
return !getPlayer().isOnline();
|
||||||
{
|
|
||||||
throw new RuntimeException("Already cleaned up... bug?");
|
|
||||||
}
|
|
||||||
if (getPlayer().isOnline())
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
forEachGear(item -> UtilServer.Unregister(item));
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue