Updated some ChatUtil methods + some refactoring

This commit is contained in:
Marc Baloup 2021-09-21 23:54:32 +02:00
parent 152ae89024
commit bb61819332
Signed by: marcbal
GPG Key ID: BBC0FE3ABC30B893
6 changed files with 114 additions and 45 deletions

View File

@ -68,7 +68,6 @@
<groupId>org.geysermc.floodgate</groupId>
<artifactId>api</artifactId>
<version>2.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>

View File

@ -184,29 +184,27 @@ public class ChatUtil {
public static Chat centerText(Chat text, char repeatedChar, TextColor decorationColor,
boolean console) {
/**
* @param decorationColor support null values
*/
public static Chat centerText(Chat text, char repeatedChar, TextColor decorationColor, boolean console) {
return centerText(text, repeatedChar, decorationColor, console, console ? CONSOLE_NB_CHAR_DEFAULT : DEFAULT_CHAT_WIDTH);
}
public static Chat centerText(Chat text, char repeatedChar, TextColor decorationColor, boolean console, int maxWidth) {
return centerText(text, repeatedChar, decorationColor, false, console, console ? CONSOLE_NB_CHAR_DEFAULT : DEFAULT_CHAT_WIDTH);
}
/**
*
* @param text
* @param repeatedChar
* @param decorationColor support null values
* @param console
* @param maxWidth
* @return
*/
public static Chat centerText(Chat text, char repeatedChar, TextColor decorationColor,
boolean console, int maxWidth) {
public static Chat centerText(Chat text, char repeatedChar, TextColor decorationColor, boolean decorationBold, boolean console, int maxWidth) {
int textWidth = componentWidth(text.getAdv(), console);
if (textWidth > maxWidth)
return text;
int repeatedCharWidth = charW(repeatedChar, console, false);
int repeatedCharWidth = charW(repeatedChar, console, decorationBold);
int sideWidth = (maxWidth - textWidth) / 2;
int sideNbChar = sideWidth / repeatedCharWidth;
@ -214,24 +212,25 @@ public class ChatUtil {
return text;
String sideChars = repeatedChar(repeatedChar, sideNbChar);
FormatableChat side = text(sideChars).color(decorationColor);
if (decorationBold)
side.bold();
Chat d = Chat.chat()
.then(text(sideChars).color(decorationColor))
.then(side)
.then(text);
if (repeatedChar != ' ')
d.then(text(sideChars).color(decorationColor));
d.then(side);
return d;
}
public static Chat leftText(Chat text, char repeatedChar, TextColor decorationColor, int nbLeft,
boolean console) {
public static Chat leftText(Chat text, char repeatedChar, TextColor decorationColor, int nbLeft, boolean console) {
return leftText(text, repeatedChar, decorationColor, nbLeft, console, console ? CONSOLE_NB_CHAR_DEFAULT : DEFAULT_CHAT_WIDTH);
}
public static Chat leftText(Chat text, char repeatedChar, TextColor decorationColor, int nbLeft,
boolean console, int maxWidth) {
public static Chat leftText(Chat text, char repeatedChar, TextColor decorationColor, int nbLeft, boolean console, int maxWidth) {
int textWidth = componentWidth(text.getAdv(), console);
int repeatedCharWidth = charW(repeatedChar, console, false);
@ -280,8 +279,19 @@ public class ChatUtil {
}
public static Chat emptyLine(char repeatedChar, TextColor decorationColor, boolean console) {
int count = ((console) ? CONSOLE_NB_CHAR_DEFAULT : DEFAULT_CHAT_WIDTH) / charW(repeatedChar, console, false);
return text(repeatedChar(repeatedChar, count)).color(decorationColor);
return emptyLine(repeatedChar, decorationColor, false, console);
}
public static Chat emptyLine(char repeatedChar, TextColor decorationColor, boolean decorationBold, boolean console) {
return emptyLine(repeatedChar, decorationColor, decorationBold, console, (console) ? CONSOLE_NB_CHAR_DEFAULT : DEFAULT_CHAT_WIDTH);
}
public static Chat emptyLine(char repeatedChar, TextColor decorationColor, boolean decorationBold, boolean console, int maxWidth) {
int count = maxWidth / charW(repeatedChar, console, decorationBold);
FormatableChat line = text(repeatedChar(repeatedChar, count)).color(decorationColor);
if (decorationBold)
line.bold();
return line;
}
private static String repeatedChar(char repeatedChar, int count) {
@ -338,7 +348,7 @@ public class ChatUtil {
return (count < 0) ? 0 : count;
}
private static int charW(char c, boolean console, boolean bold) {
public static int charW(char c, boolean console, boolean bold) {
if (console) return (c == '§') ? -1 : 1;
for (int px : CHARS_SIZE.keySet())
if (CHARS_SIZE.get(px).indexOf(c) >= 0) return px + (bold ? 1 : 0);

View File

@ -1,6 +0,0 @@
package fr.pandacube.lib.core.util;
@FunctionalInterface
public interface Callback<T> {
public void done(T arg);
}

View File

@ -4,6 +4,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Consumer;
import org.bukkit.Bukkit;
import org.bukkit.Material;
@ -21,7 +22,8 @@ import org.bukkit.plugin.Plugin;
import com.google.common.collect.ImmutableMap;
import fr.pandacube.lib.core.chat.Chat;
import fr.pandacube.lib.core.util.Callback;
import fr.pandacube.lib.core.players.IPlayerManager;
import fr.pandacube.lib.core.util.Log;
import fr.pandacube.lib.paper.util.ItemStackBuilder;
public class GUIInventory implements Listener {
@ -30,21 +32,20 @@ public class GUIInventory implements Listener {
private Player player;
private Inventory inv;
private Callback<InventoryCloseEvent> onCloseEvent;
private Consumer<InventoryCloseEvent> onCloseEvent;
private boolean isOpened = false;
private Map<Integer, Callback<InventoryClickEvent>> onClickEvents;
private Map<Integer, Consumer<InventoryClickEvent>> onClickEvents;
@Deprecated
public GUIInventory(Player p, int nbLines, String title, Callback<InventoryCloseEvent> closeEventAction,
Plugin pl) {
this(p, nbLines, title == null ? null : Chat.legacyText(title), closeEventAction, pl);
}
public GUIInventory(Player p, int nbLines, Chat title, Callback<InventoryCloseEvent> closeEventAction,
public GUIInventory(Player p, int nbLines, Chat title, Consumer<InventoryCloseEvent> closeEventAction,
Plugin pl) {
if (title == null)
inv = Bukkit.createInventory(null, nbLines * 9);
else
inv = Bukkit.createInventory(null, nbLines * 9, title.getAdv());
if (IPlayerManager.getInstance().get(p.getUniqueId()).isBedrockClient()) {
Log.warning("Opening GUI inventory for player on Bedrock client " + p.getName() + " (" + p.getUniqueId() + "). Please use a Form instead.", new Throwable());
}
setCloseEvent(closeEventAction);
@ -56,7 +57,7 @@ public class GUIInventory implements Listener {
}
protected void setCloseEvent(Callback<InventoryCloseEvent> closeEventAction) {
protected void setCloseEvent(Consumer<InventoryCloseEvent> closeEventAction) {
onCloseEvent = closeEventAction;
}
@ -65,7 +66,7 @@ public class GUIInventory implements Listener {
* été pré-annulée. Pour la rétablir, faites un
* event.setCancelled(false)).
*/
public void setButtonIfEmpty(int p, ItemStack iStack, Callback<InventoryClickEvent> clickEventActions) {
public void setButtonIfEmpty(int p, ItemStack iStack, Consumer<InventoryClickEvent> clickEventActions) {
if (inv.getItem(p) == null)
setButton(p, iStack, clickEventActions);
}
@ -75,7 +76,7 @@ public class GUIInventory implements Listener {
* été pré-annulée. Pour la rétablir, faites un
* event.setCancelled(false)).
*/
public void setButton(int p, ItemStack iStack, Callback<InventoryClickEvent> clickEventActions) {
public void setButton(int p, ItemStack iStack, Consumer<InventoryClickEvent> clickEventActions) {
inv.setItem(p, iStack);
changeClickEventAction(p, clickEventActions);
}
@ -85,7 +86,7 @@ public class GUIInventory implements Listener {
* été pré-annulée. Pour la rétablir, faites un
* event.setCancelled(false)).
*/
public void changeClickEventAction(int p, Callback<InventoryClickEvent> clickEventActions) {
public void changeClickEventAction(int p, Consumer<InventoryClickEvent> clickEventActions) {
onClickEvents.put(p, clickEventActions);
}
@ -135,8 +136,9 @@ public class GUIInventory implements Listener {
// on ne réagit pas aux clics hors de l'inventaire du dessus.
if (event.getClickedInventory() != event.getView().getTopInventory()) return;
Callback<InventoryClickEvent> callback = onClickEvents.get(event.getSlot());
if (callback != null) callback.done(event);
Consumer<InventoryClickEvent> callback = onClickEvents.get(event.getSlot());
if (callback != null)
callback.accept(event);
}
@ -149,7 +151,7 @@ public class GUIInventory implements Listener {
HandlerList.unregisterAll(this);
if (onCloseEvent != null)
onCloseEvent.done(event);
onCloseEvent.accept(event);
isOpened = false;
}

View File

@ -0,0 +1,64 @@
package fr.pandacube.lib.paper.scheduler;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitTask;
import fr.pandacube.lib.paper.PandaLibPaper;
public class AutoUpdatedObject {
private static Plugin plugin = PandaLibPaper.getPlugin();
private Runnable updater;
private List<BukkitTask> tasks = new ArrayList<>();
protected AutoUpdatedObject() { }
public AutoUpdatedObject(Runnable updater) {
this.updater = Objects.requireNonNull(updater);
}
public synchronized void updateSync() {
tasks.add(Bukkit.getScheduler().runTask(plugin, this::update));
}
public synchronized void updateAsync() {
tasks.add(Bukkit.getScheduler().runTaskAsynchronously(plugin, this::update));
}
public synchronized void updateLater(long delay)
throws IllegalArgumentException, IllegalStateException {
tasks.add(Bukkit.getScheduler().runTaskLater(plugin, this::update, delay));
}
public synchronized void updateLaterAsync(long delay)
throws IllegalArgumentException, IllegalStateException {
tasks.add(Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, this::update, delay));
}
public synchronized void updateTimer(long delay, long period)
throws IllegalArgumentException, IllegalStateException {
tasks.add(Bukkit.getScheduler().runTaskTimer(plugin, this::update, delay, period));
}
public synchronized void updateTimerAsync(long delay, long period)
throws IllegalArgumentException, IllegalStateException {
tasks.add(Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, this::update, delay, period));
}
public synchronized void cancel() {
tasks.forEach(t -> t.cancel());
tasks.clear();
}
public void update() {
Objects.requireNonNull(updater, "Please use new AutoUpdatedObject(Runnable) or override the run method.");
updater.run();
}
}

View File

@ -1,4 +1,4 @@
package fr.pandacube.lib.paper.util;
package fr.pandacube.lib.paper.scheduler;
import java.util.concurrent.Callable;
@ -7,7 +7,7 @@ import org.bukkit.Bukkit;
import fr.pandacube.lib.core.util.Log;
import fr.pandacube.lib.paper.PandaLibPaper;
public class ThreadUtil {
public class SchedulerUtil {
public static void runOnServerThread(Runnable task) {
if (Bukkit.isPrimaryThread())