mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2025-02-19 05:41:22 +01:00
Keeps heads for 3 minutes in cache
It appears that heads are being "forgotten" by the server after a few minutes (not sure how long) so caching forever doesn't help. This causes blocking calls when the head is set. https://github.com/BentoBoxWorld/Level/issues/159
This commit is contained in:
parent
0e3535d0cc
commit
4e56ff8c92
@ -2,7 +2,6 @@ package world.bentobox.bentobox.api.commands.island;
|
|||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
||||||
import world.bentobox.bentobox.api.user.User;
|
import world.bentobox.bentobox.api.user.User;
|
||||||
import world.bentobox.bentobox.panels.LanguagePanel;
|
import world.bentobox.bentobox.panels.LanguagePanel;
|
||||||
|
@ -5,7 +5,6 @@ import java.util.List;
|
|||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.event.inventory.ClickType;
|
|
||||||
import org.eclipse.jdt.annotation.NonNull;
|
import org.eclipse.jdt.annotation.NonNull;
|
||||||
|
|
||||||
import world.bentobox.bentobox.BentoBox;
|
import world.bentobox.bentobox.BentoBox;
|
||||||
|
@ -0,0 +1,31 @@
|
|||||||
|
package world.bentobox.bentobox.util.heads;
|
||||||
|
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
|
public class HeadCache {
|
||||||
|
private final ItemStack head;
|
||||||
|
private final long timestamp;
|
||||||
|
/**
|
||||||
|
* @param head
|
||||||
|
* @param timestamp
|
||||||
|
*/
|
||||||
|
public HeadCache(ItemStack head, long timestamp) {
|
||||||
|
super();
|
||||||
|
this.head = head;
|
||||||
|
this.timestamp = timestamp;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @return the head
|
||||||
|
*/
|
||||||
|
public ItemStack getHead() {
|
||||||
|
return head;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @return the timestamp
|
||||||
|
*/
|
||||||
|
public long getTimestamp() {
|
||||||
|
return timestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -16,8 +16,9 @@ import world.bentobox.bentobox.BentoBox;
|
|||||||
import world.bentobox.bentobox.api.panels.PanelItem;
|
import world.bentobox.bentobox.api.panels.PanelItem;
|
||||||
|
|
||||||
public class HeadGetter {
|
public class HeadGetter {
|
||||||
private static Map<String,ItemStack> cachedHeads = new HashMap<>();
|
private static Map<String,HeadCache> cachedHeads = new HashMap<>();
|
||||||
private static final Map<String, PanelItem> names = new HashMap<>();
|
private static final Map<String, PanelItem> names = new HashMap<>();
|
||||||
|
private static final long TOO_LONG = 360000; // 3 minutes
|
||||||
private BentoBox plugin;
|
private BentoBox plugin;
|
||||||
private static Map<String,Set<HeadRequester>> headRequesters = new HashMap<>();
|
private static Map<String,Set<HeadRequester>> headRequesters = new HashMap<>();
|
||||||
|
|
||||||
@ -42,7 +43,7 @@ public class HeadGetter {
|
|||||||
meta.setOwner(en.getKey());
|
meta.setOwner(en.getKey());
|
||||||
playerSkull.setItemMeta(meta);
|
playerSkull.setItemMeta(meta);
|
||||||
// Save in cache
|
// Save in cache
|
||||||
cachedHeads.put(en.getKey(), playerSkull);
|
cachedHeads.put(en.getKey(), new HeadCache(playerSkull, System.currentTimeMillis()));
|
||||||
// Tell requesters the head came in
|
// Tell requesters the head came in
|
||||||
if (headRequesters.containsKey(en.getKey())) {
|
if (headRequesters.containsKey(en.getKey())) {
|
||||||
for (HeadRequester req : headRequesters.get(en.getKey())) {
|
for (HeadRequester req : headRequesters.get(en.getKey())) {
|
||||||
@ -61,9 +62,11 @@ public class HeadGetter {
|
|||||||
* @param requester - callback class
|
* @param requester - callback class
|
||||||
*/
|
*/
|
||||||
public static void getHead(PanelItem panelItem, HeadRequester requester) {
|
public static void getHead(PanelItem panelItem, HeadRequester requester) {
|
||||||
|
// Freshen cache
|
||||||
|
cachedHeads.values().removeIf(c -> System.currentTimeMillis() - c.getTimestamp() > TOO_LONG);
|
||||||
// Check if in cache
|
// Check if in cache
|
||||||
if (cachedHeads.containsKey(panelItem.getPlayerHeadName())) {
|
if (cachedHeads.containsKey(panelItem.getPlayerHeadName())) {
|
||||||
panelItem.setHead(cachedHeads.get(panelItem.getPlayerHeadName()).clone());
|
panelItem.setHead(cachedHeads.get(panelItem.getPlayerHeadName()).getHead().clone());
|
||||||
requester.setHead(panelItem);
|
requester.setHead(panelItem);
|
||||||
} else {
|
} else {
|
||||||
// Get the name
|
// Get the name
|
||||||
|
Loading…
Reference in New Issue
Block a user