Make the Client UI API naming scheme a little less confusing

This commit is contained in:
ME1312 2018-08-12 17:23:21 -04:00
parent cb7b3964f2
commit 811cc891a0
No known key found for this signature in database
GPG Key ID: FEFFE2F698E88FA8
9 changed files with 200 additions and 42 deletions

View File

@ -25,11 +25,11 @@ import java.util.HashMap;
import java.util.UUID;
/**
* Internal GUI Listener
* Default GUI Listener
*/
public class InternalUIHandler implements UIHandler, Listener {
public class DefaultUIHandler implements UIHandler, Listener {
private HashMap<UUID, Callback<YAMLSection>> input = new HashMap<UUID, Callback<YAMLSection>>();
private HashMap<UUID, InternalUIRenderer> gui = new HashMap<UUID, InternalUIRenderer>();
private HashMap<UUID, DefaultUIRenderer> gui = new HashMap<UUID, DefaultUIRenderer>();
private boolean enabled = true;
private SubPlugin plugin;
@ -38,14 +38,14 @@ public class InternalUIHandler implements UIHandler, Listener {
*
* @param plugin Event
*/
public InternalUIHandler(SubPlugin plugin) {
public DefaultUIHandler(SubPlugin plugin) {
if (Util.isNull(plugin)) throw new NullPointerException();
this.plugin = plugin;
Bukkit.getPluginManager().registerEvents(this, plugin);
}
public InternalUIRenderer getRenderer(Player player) {
if (!gui.keySet().contains(player.getUniqueId())) gui.put(player.getUniqueId(), new InternalUIRenderer(plugin, player.getUniqueId()));
public DefaultUIRenderer getRenderer(Player player) {
if (!gui.keySet().contains(player.getUniqueId())) gui.put(player.getUniqueId(), new DefaultUIRenderer(plugin, player.getUniqueId()));
return gui.get(player.getUniqueId());
}
@ -57,7 +57,7 @@ public class InternalUIHandler implements UIHandler, Listener {
public void click(InventoryClickEvent event) {
Player player = (Player) event.getWhoClicked();
if (!event.isCancelled() && enabled && gui.keySet().contains(player.getUniqueId())) {
InternalUIRenderer gui = this.gui.get(player.getUniqueId());
DefaultUIRenderer gui = this.gui.get(player.getUniqueId());
if (gui.open && event.getClickedInventory() != null && event.getClickedInventory().getTitle() != null) {
if (plugin.subdata == null) {
new IllegalStateException("SubData is not connected").printStackTrace();
@ -211,8 +211,8 @@ public class InternalUIHandler implements UIHandler, Listener {
gui.back();
} else {
player.closeInventory();
final Container<Renderer<Host>> plugin = new Container<Renderer<Host>>(null);
for (Renderer<Host> renderer : InternalUIRenderer.hostPlugins.values()) {
final Container<PluginRenderer<Host>> plugin = new Container<PluginRenderer<Host>>(null);
for (PluginRenderer<Host> renderer : DefaultUIRenderer.hostPlugins.values()) {
if (item.equals(renderer.getIcon().getItemMeta().getDisplayName())) plugin.set(renderer);
}
if (plugin.get() == null) {
@ -403,8 +403,8 @@ public class InternalUIHandler implements UIHandler, Listener {
gui.back();
} else {
player.closeInventory();
Container<Renderer<SubServer>> plugin = new Container<Renderer<SubServer>>(null);
for (Renderer<SubServer> renderer : InternalUIRenderer.subserverPlugins.values()) {
Container<PluginRenderer<SubServer>> plugin = new Container<PluginRenderer<SubServer>>(null);
for (PluginRenderer<SubServer> renderer : DefaultUIRenderer.subserverPlugins.values()) {
if (item.equals(renderer.getIcon().getItemMeta().getDisplayName())) plugin.set(renderer);
}
if (plugin.get() == null) {

View File

@ -19,9 +19,9 @@ import java.text.DecimalFormat;
import java.util.*;
/**
* Internal GUI Renderer Class
* Default GUI Renderer Class
*/
public class InternalUIRenderer extends UIRenderer {
public class DefaultUIRenderer extends UIRenderer {
private static int MAX_VISITED_OBJECTS = 2;
private List<Runnable> windowHistory = new LinkedList<Runnable>();
protected Object[] lastVisitedObjects = new Object[MAX_VISITED_OBJECTS];
@ -31,7 +31,7 @@ public class InternalUIRenderer extends UIRenderer {
protected final UUID player;
private SubPlugin plugin;
protected InternalUIRenderer(SubPlugin plugin, UUID player) {
protected DefaultUIRenderer(SubPlugin plugin, UUID player) {
super(plugin, player);
this.plugin = plugin;
this.player = player;

View File

@ -4,9 +4,9 @@ import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
/**
* GUI Renderer Layout Class
* Plugin GUI Renderer Layout Class
*/
public interface Renderer<T> {
public interface PluginRenderer<T> {
/**
* Open the GUI

View File

@ -1,4 +1,5 @@
package net.ME1312.SubServers.Client.Bukkit.Graphic;
import net.ME1312.SubServers.Client.Bukkit.Library.Container;
import net.ME1312.SubServers.Client.Bukkit.Library.NamedContainer;
import net.ME1312.SubServers.Client.Bukkit.Library.Util;
@ -19,8 +20,8 @@ import java.util.regex.Pattern;
* GUI Renderer Layout Class
*/
public abstract class UIRenderer {
protected static HashMap<String, Renderer<Host>> hostPlugins = new HashMap<String, Renderer<Host>>();
protected static HashMap<String, Renderer<SubServer>> subserverPlugins = new HashMap<String, Renderer<SubServer>>();
protected static HashMap<String, PluginRenderer<Host>> hostPlugins = new HashMap<String, PluginRenderer<Host>>();
protected static HashMap<String, PluginRenderer<SubServer>> subserverPlugins = new HashMap<String, PluginRenderer<SubServer>>();
private NamedContainer<String, Integer> tdownload = null;
private int download = -1;
private final UUID player;
@ -267,7 +268,7 @@ public abstract class UIRenderer {
* @param handle Handle to bind
* @param renderer Renderer
*/
public static void addHostPlugin(String handle, Renderer<Host> renderer) {
public static void addHostPlugin(String handle, PluginRenderer<Host> renderer) {
if (Util.isNull(handle, renderer)) throw new NullPointerException();
hostPlugins.put(handle, renderer);
}
@ -277,8 +278,8 @@ public abstract class UIRenderer {
*
* @return Host Plugins
*/
public static Map<String, Renderer> getHostPlugins() {
return new HashMap<String, Renderer>(hostPlugins);
public static Map<String, PluginRenderer> getHostPlugins() {
return new HashMap<String, PluginRenderer>(hostPlugins);
}
/**
@ -297,7 +298,7 @@ public abstract class UIRenderer {
* @param handle Handle to bind
* @param renderer Renderer
*/
public static void addSubServerPlugin(String handle, Renderer<SubServer> renderer) {
public static void addSubServerPlugin(String handle, PluginRenderer<SubServer> renderer) {
if (Util.isNull(handle, renderer)) throw new NullPointerException();
subserverPlugins.put(handle, renderer);
}
@ -307,8 +308,8 @@ public abstract class UIRenderer {
*
* @return SubServer Plugins
*/
public static Map<String, Renderer> getSubServerPlugins() {
return new HashMap<String, Renderer>(subserverPlugins);
public static Map<String, PluginRenderer> getSubServerPlugins() {
return new HashMap<String, PluginRenderer>(subserverPlugins);
}
/**

View File

@ -112,7 +112,7 @@ public class BungeeChat {
}
hover.setColor(ChatColor.WHITE);
hoverm.add(hover);
message.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, label + " open SubServer/ " + server));
message.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, label + " open SubServer/ " + server.getName()));
} else {
message.setColor(ChatColor.WHITE);
hover.setColor(ChatColor.WHITE);
@ -122,9 +122,9 @@ public class BungeeChat {
hover.setColor(ChatColor.GRAY);
}
hoverm.add(hover);
hover = new TextComponent(plugin.api.getLang("SubServers", "Interface.Server-Menu.Server-External"));
hover = new TextComponent(plugin.api.getLang("SubServers", "Interface.Server-Menu.Server-External") + '\n');
hoverm.add(hover);
hover = new TextComponent(plugin.api.getLang("SubServers", "Interface.Server-Menu.Server-Player-Count").replace("$int$", new DecimalFormat("#,###").format(server.getPlayers().size())) + '\n');
hover = new TextComponent(plugin.api.getLang("SubServers", "Interface.Server-Menu.Server-Player-Count").replace("$int$", new DecimalFormat("#,###").format(server.getPlayers().size())));
hoverm.add(hover);
if (plugin.config.get().getSection("Settings").getBoolean("Show-Addresses", false)) {
hover = new TextComponent('\n' + server.getAddress().getAddress().getHostAddress() + ':' + server.getAddress().getPort());

View File

@ -1,6 +1,6 @@
package net.ME1312.SubServers.Client.Bukkit;
import net.ME1312.SubServers.Client.Bukkit.Graphic.InternalUIHandler;
import net.ME1312.SubServers.Client.Bukkit.Graphic.DefaultUIHandler;
import net.ME1312.SubServers.Client.Bukkit.Graphic.UIHandler;
import net.ME1312.SubServers.Client.Bukkit.Library.Config.YAMLConfig;
import net.ME1312.SubServers.Client.Bukkit.Library.Config.YAMLSection;
@ -78,7 +78,7 @@ public final class SubPlugin extends JavaPlugin {
reload(false);
if (config.get().getSection("Settings").getBoolean("Ingame-Access", true)) {
gui = new InternalUIHandler(this);
gui = new DefaultUIHandler(this);
SubCommand cmd = new SubCommand(this);
getCommand("subservers").setExecutor(cmd);
getCommand("subserver").setExecutor(cmd);

View File

@ -4,9 +4,9 @@ import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.item.inventory.ItemStack;
/**
* GUI Renderer Layout Class
* Plugin GUI Renderer Layout Class
*/
public interface Renderer<T> {
public interface PluginRenderer<T> {
/**
* Open the GUI

View File

@ -1,21 +1,30 @@
package net.ME1312.SubServers.Client.Sponge.Graphic;
import net.ME1312.SubServers.Client.Sponge.Library.ChatColor;
import net.ME1312.SubServers.Client.Sponge.Library.Container;
import net.ME1312.SubServers.Client.Sponge.Library.NamedContainer;
import net.ME1312.SubServers.Client.Sponge.Library.Util;
import net.ME1312.SubServers.Client.Sponge.Library.Version.Version;
import net.ME1312.SubServers.Client.Sponge.Network.API.Host;
import net.ME1312.SubServers.Client.Sponge.Network.API.SubServer;
import net.ME1312.SubServers.Client.Sponge.SubPlugin;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.item.ItemType;
import org.spongepowered.api.item.ItemTypes;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.text.title.Title;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* GUI Renderer Layout Class
*/
public abstract class UIRenderer {
protected static HashMap<String, Renderer<Host>> hostPlugins = new HashMap<String, Renderer<Host>>();
protected static HashMap<String, Renderer<SubServer>> subserverPlugins = new HashMap<String, Renderer<SubServer>>();
protected static HashMap<String, PluginRenderer<Host>> hostPlugins = new HashMap<String, PluginRenderer<Host>>();
protected static HashMap<String, PluginRenderer<SubServer>> subserverPlugins = new HashMap<String, PluginRenderer<SubServer>>();
private NamedContainer<String, Integer> tdownload = null;
private int download = -1;
private UUID download = null;
private final UUID player;
private SubPlugin plugin;
@ -58,7 +67,155 @@ public abstract class UIRenderer {
*/
public abstract void back();
// TODO Re-Add GUI API Methods that belong here
/**
* Attempt to send a Title Message
*
* @param str Message
* @return Success Status
*/
public boolean sendTitle(String str) {
return sendTitle(str, -1);
}
/**
* Attempt to send a Title Message
*
* @param str Message
* @param stay How long the message should stay
* @return Success Status
*/
public boolean sendTitle(String str, int stay) {
return sendTitle(str, -1, stay, -1);
}
/**
* Attempt to send a Title Message
*
* @param str Message
* @param fadein FadeIn Transition length (in ticks)
* @param stay How long the message should stay (in ticks)
* @param fadeout FadeOut Transition length (in ticks)
* @return Success Status
*/
public boolean sendTitle(String str, int fadein, int stay, int fadeout) {
if (Util.isNull(str, fadein, stay, fadeout)) throw new NullPointerException();
if (plugin.config.get().getSection("Settings").getBoolean("Use-Title-Messages", true)) {
String line1, line2;
if (!str.startsWith("\n") && str.contains("\n")) {
line1 = str.split("\\n")[0];
line2 = str.split("\\n")[1];
} else {
line1 = str.replace("\n", "");
line2 = ChatColor.RESET.toString();
}
try {
if (ChatColor.stripColor(line1).length() == 0 && ChatColor.stripColor(line2).length() == 0) {
Sponge.getServer().getPlayer(player).get().resetTitle();
} else {
Sponge.getServer().getPlayer(player).get().sendTitle(Title.builder().title(ChatColor.convertColor(line1)).subtitle(ChatColor.convertColor(line2)).fadeIn((fadein >= 0)?fadein:10).stay((stay >= 0)?stay:70).fadeOut((fadeout >= 0)?fadeout:20).build());
}
return true;
} catch (Throwable e) {
return false;
}
} else return false;
}
/**
* Shows/Hides the Downloading Title Message
*
* @param subtitle Subtitle to display (or null to hide)
*/
public void setDownloading(String subtitle) {
if (subtitle != null && !plugin.config.get().getSection("Settings").getBoolean("Use-Title-Messages", true)) {
if (download != null && Sponge.getScheduler().getTaskById(download).isPresent()) Sponge.getScheduler().getTaskById(download).get().cancel();
download = Sponge.getScheduler().createTaskBuilder().execute(() -> {
if (tdownload != null) Sponge.getServer().getPlayer(player).get().sendMessage(ChatColor.convertColor(plugin.api.getLang("SubServers", "Interface.Generic.Downloading").replace("$str$", subtitle)));
download = null;
}).delay(2500, TimeUnit.MILLISECONDS).submit(plugin).getUniqueId();
} if (subtitle != null && tdownload == null) {
tdownload = new NamedContainer<String, Integer>(subtitle, 0);
final Container<Integer> delay = new Container<Integer>(0);
Sponge.getScheduler().createTaskBuilder().execute(new Runnable() {
@Override
public void run() {
if (tdownload != null) {
String word = ChatColor.stripColor(plugin.api.getLang("SubServers", "Interface.Generic.Downloading.Title"));
int i = 0;
int start = (tdownload.get() - 3 < 0)?0: tdownload.get()-3;
int end = (tdownload.get() >= word.length())?word.length(): tdownload.get();
String str = plugin.api.getLang("SubServers", (delay.get() > 7 && start == 0)?"Interface.Generic.Downloading.Title-Color-Alt":"Interface.Generic.Downloading.Title-Color");
delay.set(delay.get() + 1);
if (delay.get() > 7) tdownload.set(tdownload.get() + 1);
if (tdownload.get() >= word.length() + 3) {
tdownload.set(0);
delay.set(0);
}
for (char c : word.toCharArray()) {
i++;
if (i == start) str += plugin.api.getLang("SubServers", "Interface.Generic.Downloading.Title-Color-Alt");
str += c;
if (i == end) str += plugin.api.getLang("SubServers", "Interface.Generic.Downloading.Title-Color");
}
str += '\n' + plugin.api.getLang("SubServers", "Interface.Generic.Downloading.Title-Color-Alt") + tdownload.name();
sendTitle(str, 0, 10, 5);
Sponge.getScheduler().createTaskBuilder().execute(this).delay(50, TimeUnit.MILLISECONDS).submit(plugin);
} else {
sendTitle(ChatColor.RESET.toString(), 0, 1, 0);
}
}
}).submit(plugin);
} else if (subtitle != null) {
tdownload.rename(subtitle);
} else {
if (tdownload != null) {
tdownload = null;
}
if (download != null) {
if (Sponge.getScheduler().getTaskById(download).isPresent()) Sponge.getScheduler().getTaskById(download).get().cancel();
download = null;
}
}
}
/**
* Parse an ItemStack from a String
*
* @param str String to parse
* @return ItemStack
*/
public ItemStack parseItem(String str) {
return parseItem(str, ItemStack.builder().itemType(ItemTypes.NONE).quantity(1).build());
}
/**
* Parse an ItemStack from a String
*
* @param str String to parse
* @param def Default to return if unable to parse
* @return ItemStack
*/
public ItemStack parseItem(String str, ItemStack def) {
final Container<String> item = new Container<String>(str);
// minecraft:name
if (item.get().toLowerCase().startsWith("minecraft:")) {
item.set(item.get().substring(10));
} else
// bukkit:name (ignored on sponge)
if (item.get().toLowerCase().startsWith("bukkit:")) {
item.set(item.get().substring(7));
}
// material name
if (!Util.isException(() -> ItemTypes.class.getDeclaredField(item.get().toUpperCase()).get(null))) {
return ItemStack.builder().itemType((ItemType) Util.getDespiteException(() -> ItemTypes.class.getDeclaredField(item.get().toUpperCase()).get(null), null)).quantity(1).build();
}
return def;
}
/**
* Add Host Plugin
@ -66,7 +223,7 @@ public abstract class UIRenderer {
* @param handle Handle to bind
* @param renderer Renderer
*/
public static void addHostPlugin(String handle, Renderer<Host> renderer) {
public static void addHostPlugin(String handle, PluginRenderer<Host> renderer) {
if (Util.isNull(handle, renderer)) throw new NullPointerException();
hostPlugins.put(handle, renderer);
}
@ -76,8 +233,8 @@ public abstract class UIRenderer {
*
* @return Host Plugins
*/
public static Map<String, Renderer<Host>> getHostPlugins() {
return new HashMap<String, Renderer<Host>>(hostPlugins);
public static Map<String, PluginRenderer<Host>> getHostPlugins() {
return new HashMap<String, PluginRenderer<Host>>(hostPlugins);
}
/**
@ -96,7 +253,7 @@ public abstract class UIRenderer {
* @param handle Handle to bind
* @param renderer Renderer
*/
public static void addSubServerPlugin(String handle, Renderer<SubServer> renderer) {
public static void addSubServerPlugin(String handle, PluginRenderer<SubServer> renderer) {
if (Util.isNull(handle, renderer)) throw new NullPointerException();
subserverPlugins.put(handle, renderer);
}
@ -106,8 +263,8 @@ public abstract class UIRenderer {
*
* @return SubServer Plugins
*/
public static Map<String, Renderer<SubServer>> getSubServerPlugins() {
return new HashMap<String, Renderer<SubServer>>(subserverPlugins);
public static Map<String, PluginRenderer<SubServer>> getSubServerPlugins() {
return new HashMap<String, PluginRenderer<SubServer>>(subserverPlugins);
}
/**

View File

@ -225,7 +225,7 @@ public final class SubCommand implements CommandExecutor {
hover.append(Text.builder(server.getName() + '\n').color(TextColors.GRAY).build());
}
hover.append(
ChatColor.convertColor(plugin.api.getLang("SubServers","Interface.Server-Menu.Server-Player-Count").replace("$int$", new DecimalFormat("#,###").format(server.getPlayers().size())) + '\n')
ChatColor.convertColor(plugin.api.getLang("SubServers","Interface.Server-Menu.Server-Player-Count").replace("$int$", new DecimalFormat("#,###").format(server.getPlayers().size())))
);
} else if (((SubServer) server).isEnabled() && ((SubServer) server).getCurrentIncompatibilities().size() == 0) {
message.color(TextColors.YELLOW);