Use SignCacheItem

Indicate whether it is real or not.
This commit is contained in:
tastybento 2021-01-09 11:19:35 -08:00
parent dcd5bd1514
commit 4ad5d864ab
4 changed files with 96 additions and 65 deletions

View File

@ -12,6 +12,7 @@ import com.google.gson.annotations.Expose;
*
*/
public class SignCacheItem {
@Expose
private final List<String> signText;
@Expose
@ -24,6 +25,15 @@ public class SignCacheItem {
this.signText = signText;
this.type = type;
}
/**
* This sign is not real
*/
public SignCacheItem() {
this.signText = null;
this.type = null;
}
/**
* @return the signText
*/
@ -36,5 +46,12 @@ public class SignCacheItem {
public Material getType() {
return type;
}
/**
* @return the isReal
*/
public boolean isReal() {
return getType() != null;
}
}

View File

@ -1,14 +1,15 @@
package world.bentobox.warps;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.World;
import org.eclipse.jdt.annotation.NonNull;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.database.Database;
import world.bentobox.warps.objects.SignCache;
@ -29,6 +30,7 @@ public class SignCacheManager {
handler.loadObjects().forEach(w -> {
World world = Bukkit.getWorld(w.getUniqueId());
if (world != null) {
w.getSigns().values().removeIf(sci -> sci.getType().equals(Material.AIR));
cachedSigns.put(world, w.getSigns());
}
});
@ -38,32 +40,30 @@ public class SignCacheManager {
cachedSigns.forEach((w, m) -> handler.saveObjectAsync(new SignCache(w, m)));
}
Material getSignIcon(World world, UUID warpOwner) {
// Add the worlds if we haven't seen this before
cachedSigns.putIfAbsent(world, new HashMap<>());
if (cachedSigns.get(world).containsKey(warpOwner)) {
return cachedSigns.get(world).get(warpOwner).getType();
}
// Not in cache
SignCacheItem sc = addon.getWarpSignsManager().getSignInfo(world, warpOwner);
cachedSigns.get(world).put(warpOwner, sc);
return sc.getType();
}
/**
* Gets sign text and cache it
* @param playerUUID
* @return sign text in a list
* Get the sign item from cache or get it from the world if it is not in the cache
* @param world - world
* @param warpOwner - warp owner
* @return SignCacheItem
*/
List<String> getSign(World world, UUID playerUUID) {
@NonNull
SignCacheItem getSignItem(World world, UUID warpOwner) {
// Add the worlds if we haven't seen this before
cachedSigns.putIfAbsent(world, new HashMap<>());
if (cachedSigns.get(world).containsKey(playerUUID)) {
return cachedSigns.get(world).get(playerUUID).getSignText();
// Get from cache if available
if (cachedSigns.get(world).containsKey(warpOwner)) {
return cachedSigns.get(world).get(warpOwner);
}
SignCacheItem result = addon.getWarpSignsManager().getSignInfo(world, playerUUID);
cachedSigns.get(world).put(playerUUID, result);
return result.getSignText();
// Generate and add to cache
SignCacheItem result = addon.getWarpSignsManager().getSignInfo(world, warpOwner);
if (result.isReal()) {
BentoBox.getInstance().logDebug("Warp is real - caching");
cachedSigns.get(world).put(warpOwner, result);
} else {
BentoBox.getInstance().logDebug("Warp is not real - removing");
addon.getWarpSignsManager().removeWarp(world, warpOwner);
}
return result;
}
/**

View File

@ -8,7 +8,9 @@ import java.util.concurrent.CompletableFuture;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.inventory.ItemStack;
import org.eclipse.jdt.annotation.NonNull;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.panels.PanelItem;
import world.bentobox.bentobox.api.panels.builders.PanelBuilder;
import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
@ -26,12 +28,13 @@ public class WarpPanelManager {
signCacheManager = new SignCacheManager(addon);
}
private PanelItem getPanelItem(World world, UUID warpOwner) {
private PanelItem getPanelItem(World world, UUID warpOwner, SignCacheItem sign) {
PanelItemBuilder pib = new PanelItemBuilder()
.name(addon.getSettings().getNameFormat() + addon.getPlugin().getPlayers().getName(warpOwner))
.description(signCacheManager.getSign(world, warpOwner))
.description(sign.getSignText())
.clickHandler((panel, clicker, click, slot) -> hander(world, clicker, warpOwner));
Material icon = signCacheManager.getSignIcon(world, warpOwner);
Material icon = sign.getType();
if (icon.equals(Material.PLAYER_HEAD)) {
return pib.icon(addon.getPlayers().getName(warpOwner)).build();
} else {
@ -96,10 +99,18 @@ public class WarpPanelManager {
int i = index * PANEL_MAX_SIZE;
for (; i < (index * PANEL_MAX_SIZE + PANEL_MAX_SIZE) && i < warps.size(); i++) {
UUID warpOwner = warps.get(i);
if (randomWarp && i == 0) {
panelBuilder.item(getRandomButton(world, user, warps.get(i)));
panelBuilder.item(getRandomButton(world, user, warpOwner));
} else {
panelBuilder.item(getPanelItem(world, warps.get(i)));
@NonNull
SignCacheItem sign = signCacheManager.getSignItem(world, warpOwner);
if (sign.isReal()) {
BentoBox.getInstance().logDebug("Sign is real - adding to panel");
panelBuilder.item(getPanelItem(world, warpOwner, sign));
} else {
BentoBox.getInstance().logDebug("Sign is not real - not adding to panel");
}
}
}
return i;

View File

@ -3,7 +3,6 @@ package world.bentobox.warps;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
@ -291,44 +290,48 @@ public class WarpSignsManager {
List<String> result = new ArrayList<>();
//get the sign info
Location signLocation = getWarp(world, uuid);
if (signLocation != null && signLocation.getBlock().getType().name().contains("SIGN")) {
Sign sign = (Sign)signLocation.getBlock().getState();
result.addAll(Arrays.asList(sign.getLines()));
// Clean up - remove the [WELCOME] line
result.remove(0);
// Remove any trailing blank lines
result.removeIf(String::isEmpty);
// Set the initial color per lore setting
for (int i = 0; i< result.size(); i++) {
result.set(i, ChatColor.translateAlternateColorCodes('&', addon.getSettings().getLoreFormat()) + result.get(i));
}
// Get the sign type
String prefix = plugin.getIWM().getAddon(world).map(Addon::getPermissionPrefix).orElse("");
Material icon;
if (!prefix.isEmpty())
{
icon = Material.matchMaterial(
this.getPermissionValue(User.getInstance(uuid),
prefix + "island.warp",
this.addon.getSettings().getIcon()));
}
else
{
icon = Material.matchMaterial(this.addon.getSettings().getIcon());
}
if (icon == null || icon.name().contains("SIGN")) {
return new SignCacheItem(result, Material.valueOf(sign.getType().name().replace("WALL_", "")));
} else {
return new SignCacheItem(result, icon);
}
} else {
addon.getWarpSignsManager().removeWarp(world, uuid);
if (signLocation == null) {
plugin.logDebug("Null warp found");
return new SignCacheItem();
}
return new SignCacheItem(Collections.emptyList(), Material.AIR);
if (!signLocation.getBlock().getType().name().contains("SIGN")) {
plugin.logDebug("Sign block is not");
return new SignCacheItem();
}
plugin.logDebug("Sign block is a sign");
Sign sign = (Sign)signLocation.getBlock().getState();
result.addAll(Arrays.asList(sign.getLines()));
// Clean up - remove the [WELCOME] line
result.remove(0);
// Remove any trailing blank lines
result.removeIf(String::isEmpty);
// Set the initial color per lore setting
for (int i = 0; i< result.size(); i++) {
result.set(i, ChatColor.translateAlternateColorCodes('&', addon.getSettings().getLoreFormat()) + result.get(i));
}
// Get the sign type
String prefix = plugin.getIWM().getAddon(world).map(Addon::getPermissionPrefix).orElse("");
Material icon;
if (!prefix.isEmpty())
{
icon = Material.matchMaterial(
this.getPermissionValue(User.getInstance(uuid),
prefix + "island.warp",
this.addon.getSettings().getIcon()));
}
else
{
icon = Material.matchMaterial(this.addon.getSettings().getIcon());
}
if (icon == null || icon.name().contains("SIGN")) {
return new SignCacheItem(result, Material.valueOf(sign.getType().name().replace("WALL_", "")));
}
return new SignCacheItem(result, icon);
}
/**