Revert to instance checking for signs

This commit is contained in:
ME1312 2022-03-19 12:25:19 -04:00
parent b7c89d26d5
commit 6fc8732003
No known key found for this signature in database
GPG Key ID: FEFFE2F698E88FA8
2 changed files with 95 additions and 78 deletions

View File

@ -324,13 +324,9 @@ public class ConfigUpdater {
existing = updated.clone(); existing = updated.clone();
i++; i++;
} if (was.compareTo(new Version("22w11a")) <= 0) { } if (was.compareTo(new Version("22w07c")) <= 0) {
if (existing.contains("Lang")) { if (existing.contains("Lang")) {
updated.getMap("Lang").remove("Command.Teleport"); updated.getMap("Lang").remove("Command.Teleport");
LinkedList<String> keys = new LinkedList<>(existing.getMap("Lang").getKeys());
for (String key : keys) if (key.startsWith("Signs.")) {
updated.getMap("Lang").remove(key);
}
} }
existing = updated.clone(); existing = updated.clone();

View File

@ -31,13 +31,15 @@ import java.nio.charset.StandardCharsets;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.util.*; import java.util.*;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.function.Supplier;
/** /**
* SubServers Signs Class * SubServers Signs Class
*/ */
public class SubSigns implements Listener { public class SubSigns implements Listener {
private static final HashMap<OfflineBlock, String> data = new HashMap<OfflineBlock, String>(); private static final HashMap<OfflineBlock, String> data = new HashMap<OfflineBlock, String>();
private static HashMap<Location, String> signs = new HashMap<Location, String>(); private static final HashMap<String, Location> locations = new HashMap<String, Location>();
private static HashMap<Location, Supplier<?>> signs = new HashMap<Location, Supplier<?>>();
private static File file; private static File file;
private final SubPlugin plugin; private final SubPlugin plugin;
private boolean active = false; private boolean active = false;
@ -86,16 +88,17 @@ public class SubSigns implements Listener {
break; break;
case '\u0003': // [ETX] (End of String character) case '\u0003': // [ETX] (End of String character)
magic.position(0); magic.position(0);
String object = string.toString(StandardCharsets.UTF_8.name()); String name = string.toString(StandardCharsets.UTF_8.name());
OfflineBlock location = new OfflineBlock(new UUID(magic.getLong(), magic.getLong()), magic.getInt(), magic.getInt(), magic.getInt()); OfflineBlock location = new OfflineBlock(new UUID(magic.getLong(), magic.getLong()), magic.getInt(), magic.getInt(), magic.getInt());
Location loaded = location.load(); Location loaded = location.load();
if (loaded == null) { if (loaded == null) {
data.put(location, object); data.put(location, name);
} else if (loaded.getBlock().getState() instanceof Sign) { } else if (loaded.getBlock().getState() instanceof Sign) {
data.put(location, object); data.put(location, name);
signs.put(loaded, object); signs.put(loaded, translate(name));
locations.put(name.toLowerCase(), loaded);
} else { } else {
Bukkit.getLogger().warning("SubServers > Removed invalid sign data: [\"" + loaded.getWorld().getName() + "\", " + location.x + ", " + location.y + ", " + location.z + "] -> \"" + object + '\"'); Bukkit.getLogger().warning("SubServers > Removed invalid sign data: [\"" + loaded.getWorld().getName() + "\", " + location.x + ", " + location.y + ", " + location.z + "] -> \"" + name + '\"');
} }
magic.clear(); magic.clear();
string.reset(); string.reset();
@ -119,25 +122,45 @@ public class SubSigns implements Listener {
@EventHandler(priority = EventPriority.MONITOR) @EventHandler(priority = EventPriority.MONITOR)
public void create(SignChangeEvent e) { public void create(SignChangeEvent e) {
if (!e.isCancelled() && plugin.lang != null && e.getLine(0).trim().equalsIgnoreCase("[SubServers]")) { if (!e.isCancelled() && e.getLine(0).trim().equalsIgnoreCase("[SubServers]") && plugin.lang != null) {
Player player = e.getPlayer(); Player player = e.getPlayer();
String object = e.getLine(1).trim(); String name = e.getLine(1).trim();
if (object.length() > 0 && player.hasPermission("subservers.signs")) { if (name.length() > 0 && player.hasPermission("subservers.signs")) {
Location pos = e.getBlock().getLocation(); Location location = e.getBlock().getLocation();
if (pos.getBlock().getState() instanceof Sign) { if (location.getBlock().getState() instanceof Sign) {
HashMap<Location, String> signs = new HashMap<Location, String>(SubSigns.signs); HashMap<Location, Supplier<?>> signs = new HashMap<Location, Supplier<?>>(SubSigns.signs);
signs.put(pos, object); Supplier<?> translator = translate(name);
signs.put(location, translator);
SubSigns.data.put(new OfflineBlock(location), name);
SubSigns.signs = signs; SubSigns.signs = signs;
data.put(new OfflineBlock(pos), object); SubSigns.locations.put(name.toLowerCase(), location);
listen(); listen();
refresh(e.getBlock(), object); refresh(e.getBlock(), translator);
Bukkit.getLogger().info("SubServers > Server sign created: [\"" + pos.getWorld().getName() + "\", " + pos.getBlockX() + ", " + pos.getBlockY() + ", " + pos.getBlockZ() + "] -> \"" + object + '\"'); Bukkit.getLogger().info("SubServers > Server sign created: [\"" + location.getWorld().getName() + "\", " + location.getBlockX() + ", " + location.getBlockY() + ", " + location.getBlockZ() + "] -> \"" + name + '\"');
player.sendMessage(plugin.api.getLang("SubServers", "Signs.Create")); player.sendMessage(plugin.api.getLang("SubServers", "Signs.Create"));
} }
} }
} }
} }
@SuppressWarnings({"rawtypes", "unchecked"})
private Supplier<?> translate(String name) {
if (name.startsWith("::") && name.length() > 2) {
final String translated = name.substring(2).toLowerCase();
return () -> ((Map<String, Object>) (Map) plugin.phi.cache.getHosts()).getOrDefault(translated, name);
} else if (name.startsWith(":") && name.length() > 1) {
final String translated = name.substring(1);
return () -> {
Pair<String, List<Server>> group = plugin.phi.cache.getGroup(translated);
return (group == null)? name : group;
};
} else {
final String translated = name.toLowerCase();
return () -> ((Map<String, Object>) (Map) plugin.phi.cache.getServers()).getOrDefault(translated, name);
}
}
private void listen() { private void listen() {
if (!active && !signs.isEmpty()) { if (!active && !signs.isEmpty()) {
active = true; active = true;
@ -148,30 +171,26 @@ public class SubSigns implements Listener {
private void refresh() { private void refresh() {
if (plugin.lang != null) { if (plugin.lang != null) {
for (Entry<Location, String> pos : signs.entrySet()) { for (Entry<Location, Supplier<?>> sign : signs.entrySet()) {
refresh(pos.getKey().getBlock(), pos.getValue()); refresh(sign.getKey().getBlock(), sign.getValue());
} }
} }
} }
private void refresh(SubServer server) { private void refresh(SubServer server) {
if (server != null && plugin.lang != null) { if (server != null && plugin.lang != null) {
List<String> groups = server.getGroups(); Location location = locations.get(server.getName().toLowerCase());
String[] names = new String[groups.size() + 2]; if (location != null) {
names[0] = server.getName(); refresh(location.getBlock(), () -> server);
int i = 0; for (String group : groups) names[++i] = ':' + group; }
names[++i] = "::" + server.getHost(); for (String group : server.getGroups()) {
if ((location = locations.get(':' + group.toLowerCase())) != null) {
String object; refresh(location.getBlock(), signs.get(location));
for (Entry<Location, String> pos : signs.entrySet()) {
object = pos.getValue();
for (String name : names) {
if (name.equalsIgnoreCase(object)) {
refresh(pos.getKey().getBlock(), object);
break;
}
} }
} }
if ((location = locations.get("::" + server.getHost().toLowerCase())) != null) {
refresh(location.getBlock(), signs.get(location));
}
} }
} }
@ -212,29 +231,26 @@ public class SubSigns implements Listener {
} }
} }
private void refresh(Block block, String name) { @SuppressWarnings("unchecked")
private void refresh(Block block, Supplier<?> translator) {
if (block.getState() instanceof Sign) { if (block.getState() instanceof Sign) {
Sign sign = (Sign) block.getState(); Object object = translator.get();
Text state = Text.UNKNOWN; String name;
int players = 0; int players = 0;
if (name.startsWith("::") && name.length() > 2) { Sign sign = (Sign) block.getState();
Host host = plugin.phi.cache.getHost(name.substring(2)); Text state = Text.UNKNOWN;
if (host == null) return;
name = host.getDisplayName(); if (object instanceof Server) {
Text incoming; Server server = (Server) object;
for (SubServer server : host.getSubServers().values()) { state = Text.determine(server);
players += server.getRemotePlayers().size(); name = server.getDisplayName();
incoming = Text.determine(server); players = server.getRemotePlayers().size();
if (incoming.priority > state.priority)
state = incoming;
}
} else if (name.startsWith(":") && name.length() > 1) {
Pair<String, List<Server>> group = plugin.phi.cache.getGroup(name.substring(1));
if (group == null) return;
} else if (object instanceof Pair) {
Pair<String, List<Server>> group = (Pair<String, List<Server>>) object;
name = group.key(); name = group.key();
Text incoming; Text incoming;
for (Server server : group.value()) { for (Server server : group.value()) {
players += server.getRemotePlayers().size(); players += server.getRemotePlayers().size();
@ -242,13 +258,21 @@ public class SubSigns implements Listener {
if (incoming.priority > state.priority) if (incoming.priority > state.priority)
state = incoming; state = incoming;
} }
} else { } else if (object instanceof Host) {
Server server = plugin.phi.cache.getServer(name); Host host = (Host) object;
if (server == null) return; name = host.getDisplayName();
state = Text.determine(server); Text incoming;
name = server.getDisplayName(); for (SubServer server : host.getSubServers().values()) {
players = server.getRemotePlayers().size(); players += server.getRemotePlayers().size();
incoming = Text.determine(server);
if (incoming.priority > state.priority)
state = incoming;
}
} else if (object instanceof String) {
name = (String) object;
} else {
return;
} }
String[] text = plugin.phi.replace(null, plugin.api.getLang("SubServers", state.text).replace("$str$", name).replace("$int$", NumberFormat.getInstance().format(players))).split("\n", 4); String[] text = plugin.phi.replace(null, plugin.api.getLang("SubServers", state.text).replace("$str$", name).replace("$int$", NumberFormat.getInstance().format(players))).split("\n", 4);
@ -264,24 +288,20 @@ public class SubSigns implements Listener {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@EventHandler(priority = EventPriority.MONITOR) @EventHandler(priority = EventPriority.MONITOR)
public void interact(PlayerInteractEvent e) { public void interact(PlayerInteractEvent e) {
if (!e.isCancelled() && e.getClickedBlock() != null && plugin.lang != null && plugin.api.getSubDataNetwork()[0] != null && !plugin.api.getSubDataNetwork()[0].isClosed() && signs.containsKey(e.getClickedBlock().getLocation())) { if (!e.isCancelled() && e.getClickedBlock() != null && signs.containsKey(e.getClickedBlock().getLocation()) && plugin.lang != null) {
Player player = e.getPlayer(); Player player = e.getPlayer();
if ((e.getAction() == Action.RIGHT_CLICK_BLOCK || !player.hasPermission("subservers.signs")) && player.hasPermission("subservers.teleport")) { if (player.hasPermission("subservers.teleport") && (e.getAction() == Action.RIGHT_CLICK_BLOCK || !player.hasPermission("subservers.signs"))) {
String name = signs.get(e.getClickedBlock().getLocation()); Object object = signs.get(e.getClickedBlock().getLocation()).get();
Collection<? extends Server> servers; Collection<? extends Server> servers;
if (name.startsWith("::") && name.length() > 2) { if (object instanceof Server) {
Host host = plugin.phi.cache.getHost(name.substring(2)); servers = Collections.singleton((Server) object);
if (host == null) return; } else if (object instanceof Pair) {
servers = host.getSubServers().values(); servers = ((Pair<String, List<Server>>) object).value();
} else if (name.startsWith(":") && name.length() > 1) { } else if (object instanceof Host) {
Pair<String, List<Server>> group = plugin.phi.cache.getGroup(name.substring(1)); servers = ((Host) object).getSubServers().values();
if (group == null) return;
servers = group.value();
} else { } else {
Server server = plugin.phi.cache.getServer(name); return;
if (server == null) return;
servers = Collections.singleton(server);
} }
Text incoming, state = Text.UNKNOWN; Text incoming, state = Text.UNKNOWN;
@ -323,12 +343,13 @@ public class SubSigns implements Listener {
if (!e.isCancelled() && e.getBlock().getState() instanceof Sign && signs.containsKey(e.getBlock().getLocation())) { if (!e.isCancelled() && e.getBlock().getState() instanceof Sign && signs.containsKey(e.getBlock().getLocation())) {
Player player = e.getPlayer(); Player player = e.getPlayer();
if (plugin.lang != null && player.hasPermission("subservers.signs")) { if (plugin.lang != null && player.hasPermission("subservers.signs")) {
Location pos = e.getBlock().getLocation(); Location location = e.getBlock().getLocation();
String name = data.remove(new OfflineBlock(location));
if (name != null) locations.remove(name.toLowerCase());
HashMap<Location, String> signs = new HashMap<Location, String>(SubSigns.signs); HashMap<Location, Supplier<?>> signs = new HashMap<Location, Supplier<?>>(SubSigns.signs);
signs.remove(pos); signs.remove(location);
SubSigns.signs = signs; SubSigns.signs = signs;
data.remove(new OfflineBlock(pos));
player.sendMessage(plugin.api.getLang("SubServers", "Signs.Delete")); player.sendMessage(plugin.api.getLang("SubServers", "Signs.Delete"));
} else { } else {