Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes some errors with HeadGetter #1569

Merged
merged 2 commits into from
Nov 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 10 additions & 17 deletions src/main/java/world/bentobox/bentobox/api/panels/Panel.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@

import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.OptionalInt;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.eclipse.jdt.annotation.NonNull;

import world.bentobox.bentobox.api.user.User;
Expand Down Expand Up @@ -190,18 +186,15 @@ public void setUser(User user) {
@Override
public void setHead(PanelItem item) {
// Update the panel item
// Replace the item in the item list if the name is the same
items = items.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, e -> (item.getName().equals(e.getValue().getName()) ? item : e.getValue())));
// Replace the inventory slot item
for (int i = 0; i < inventory.getSize(); i++) {
ItemStack it = inventory.getItem(i);
if (it != null && it.getType().equals(Material.PLAYER_HEAD)) {
ItemMeta meta = it.getItemMeta();
if (meta != null && ChatColor.stripColor(item.getName()).equals(ChatColor.stripColor(meta.getLocalizedName()))) {
inventory.setItem(i, item.getItem());
}
}
// Find panel item index in items and replace it once more in inventory to update it.

OptionalInt index = this.items.entrySet().stream().
filter(entry -> entry.getValue() == item).
mapToInt(Map.Entry::getKey).findFirst();

if (index.isPresent()) {
// Update item inside inventory to change icon only if item is inside panel.
this.inventory.setItem(index.getAsInt(), item.getItem());
}
}

Expand Down
19 changes: 19 additions & 0 deletions src/main/java/world/bentobox/bentobox/util/Pair.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,25 @@ public Pair(X x, Z z) {
this.z = z;
}


/**
* Returns X element as key.
* @return X element
*/
public X getKey() {
return x;
}


/**
* Returns Z element as value.
* @return Z element
*/
public Z getValue() {
return z;
}


/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
Expand Down
33 changes: 15 additions & 18 deletions src/main/java/world/bentobox/bentobox/util/heads/HeadGetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Queue;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.stream.Collectors;

import org.bukkit.Bukkit;
import org.eclipse.jdt.annotation.Nullable;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.panels.PanelItem;
import world.bentobox.bentobox.util.Pair;


/**
Expand All @@ -36,7 +37,7 @@ public class HeadGetter {
/**
* Local cache for storing requested names and items which must be updated.
*/
private static final Map<String, PanelItem> names = new HashMap<>();
private static final Queue<Pair<String, PanelItem>> names = new LinkedBlockingQueue<>();

/**
* Requesters of player heads.
Expand Down Expand Up @@ -89,7 +90,7 @@ public static void getHead(PanelItem panelItem, HeadRequester requester) {
// Get the name
headRequesters.computeIfAbsent(panelItem.getPlayerHeadName(), k -> new HashSet<>()).
add(requester);
names.put(panelItem.getPlayerHeadName(), panelItem);
names.add(new Pair<>(panelItem.getPlayerHeadName(), panelItem));
}
}

Expand Down Expand Up @@ -117,21 +118,19 @@ public static void addToCache(HeadCache cache)
*/
private void runPlayerHeadGetter() {
Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, () -> {
synchronized (names)
synchronized (HeadGetter.names)
{
Iterator<Entry<String, PanelItem>> it = names.entrySet().iterator();

if (it.hasNext())
if (!HeadGetter.names.isEmpty())
{
Entry<String, PanelItem> elementEntry = it.next();
Pair<String, PanelItem> elementEntry = HeadGetter.names.poll();

// TODO: In theory BentoBox could use User instance to find existing user UUID's.
// It would avoid one API call.
final String userName = elementEntry.getKey();

// Use cached userId as userId will not change :)
UUID userId = cachedHeads.containsKey(userName) ?
cachedHeads.get(userName).getUserId() :
UUID userId = HeadGetter.cachedHeads.containsKey(userName) ?
HeadGetter.cachedHeads.get(userName).getUserId() :
HeadGetter.getUserIdFromName(userName);

// Create new cache object.
Expand All @@ -140,24 +139,22 @@ private void runPlayerHeadGetter() {
HeadGetter.getTextureFromUUID(userId));

// Save in cache
cachedHeads.put(userName, cache);
HeadGetter.cachedHeads.put(userName, cache);

// Tell requesters the head came in
if (headRequesters.containsKey(userName))
if (HeadGetter.headRequesters.containsKey(userName))
{
for (HeadRequester req : headRequesters.get(userName))
for (HeadRequester req : HeadGetter.headRequesters.get(userName))
{
elementEntry.getValue().setHead(cache.getPlayerHead());

Bukkit.getServer().getScheduler().runTaskAsynchronously(plugin,
Bukkit.getServer().getScheduler().runTaskAsynchronously(this.plugin,
() -> req.setHead(elementEntry.getValue()));
}
}

it.remove();
}
}
}, 0L, 20L);
}, 0L, 10L);
}


Expand Down