WIP - moving sign checking to SignCacheManager

This commit is contained in:
tastybento 2020-01-29 13:25:46 -08:00
parent 79b8c0cd87
commit ffc227aa21
4 changed files with 81 additions and 60 deletions

View File

@ -1,14 +1,23 @@
package world.bentobox.warps;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Sign;
import org.eclipse.jdt.annotation.NonNull;
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.Database;
import world.bentobox.warps.objects.SignCache;
@ -44,8 +53,18 @@ public class SignCacheManager {
if (cachedSigns.get(world).containsKey(warpOwner)) {
return cachedSigns.get(world).get(warpOwner).getType();
}
// Not in cache
SignCacheItem sc = addon.getWarpSignsManager().getSignInfo(world, warpOwner);
// Not in cache
return cacheSign(world, warpOwner);
}
/**
* Cache the sign text
* @param world - world
* @param warpOwner - warp owner
* @return Material of sign
*/
public Material cacheSign(World world, UUID warpOwner) {
SignCacheItem sc = getSignInfo(world, warpOwner);
cachedSigns.get(world).put(warpOwner, sc);
return sc.getType();
}
@ -61,7 +80,7 @@ public class SignCacheManager {
if (cachedSigns.get(world).containsKey(playerUUID)) {
return cachedSigns.get(world).get(playerUUID).getSignText();
}
SignCacheItem result = addon.getWarpSignsManager().getSignInfo(world, playerUUID);
SignCacheItem result = getSignInfo(world, playerUUID);
cachedSigns.get(world).put(playerUUID, result);
return result.getSignText();
}
@ -76,4 +95,56 @@ public class SignCacheManager {
cachedSigns.get(world).remove(key);
}
/**
* Gets the warp sign text and material type for player's UUID in world
*
* @param world - world to look in
* @param uuid - player's uuid
* @return Sign's content and type
*/
@NonNull
public SignCacheItem getSignInfo(@NonNull World world, @NonNull UUID uuid) {
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);
}
return new SignCacheItem(Collections.emptyList(), Material.AIR);
}
}

View File

@ -135,8 +135,8 @@ public class Warp extends Addon {
if (hooked)
{
// Start warp signs
warpSignsManager = new WarpSignsManager(this, this.getPlugin());
warpPanelManager = new WarpPanelManager(this);
warpSignsManager = new WarpSignsManager(this, this.getPlugin());
// Load the listener
this.registerListener(new WarpSignsListener(this));
}

View File

@ -127,4 +127,8 @@ public class WarpPanelManager {
signCacheManager.saveCache();
}
public SignCacheManager getSignCacheManager() {
return this.signCacheManager;
}
}

View File

@ -1,9 +1,7 @@
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;
@ -18,7 +16,6 @@ import java.util.stream.Collectors;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.block.Block;
@ -29,7 +26,6 @@ import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.Database;
import world.bentobox.bentobox.database.objects.Island;
@ -187,6 +183,8 @@ public class WarpSignsManager {
if (k != null && k.getWorld() != null && k.getBlock().getType().name().contains("SIGN")) {
// Add to map
getWarpMap(k.getWorld()).put(v, k);
// Add to cache
addon.getWarpPanelManager().getSignCacheManager().cacheSign(k.getWorld(), v);
}
});
}
@ -255,58 +253,6 @@ public class WarpSignsManager {
handler.saveObject(warpsData .save(worldsWarpList));
}
/**
* Gets the warp sign text and material type for player's UUID in world
*
* @param world - world to look in
* @param uuid - player's uuid
* @return Sign's content and type
*/
@NonNull
public SignCacheItem getSignInfo(@NonNull World world, @NonNull UUID uuid) {
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);
}
return new SignCacheItem(Collections.emptyList(), Material.AIR);
}
/**
* Warps a player to a spot in front of a sign.
* @param user - user who is warping