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> <groupId>org.geysermc.floodgate</groupId>
<artifactId>api</artifactId> <artifactId>api</artifactId>
<version>2.0-SNAPSHOT</version> <version>2.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency> </dependency>
</dependencies> </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); 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 decorationColor support null values
* @param console
* @param maxWidth
* @return
*/ */
public static Chat centerText(Chat text, char repeatedChar, TextColor decorationColor, public static Chat centerText(Chat text, char repeatedChar, TextColor decorationColor, boolean decorationBold, boolean console, int maxWidth) {
boolean console, int maxWidth) {
int textWidth = componentWidth(text.getAdv(), console); int textWidth = componentWidth(text.getAdv(), console);
if (textWidth > maxWidth) if (textWidth > maxWidth)
return text; return text;
int repeatedCharWidth = charW(repeatedChar, console, false); int repeatedCharWidth = charW(repeatedChar, console, decorationBold);
int sideWidth = (maxWidth - textWidth) / 2; int sideWidth = (maxWidth - textWidth) / 2;
int sideNbChar = sideWidth / repeatedCharWidth; int sideNbChar = sideWidth / repeatedCharWidth;
@ -214,24 +212,25 @@ public class ChatUtil {
return text; return text;
String sideChars = repeatedChar(repeatedChar, sideNbChar); String sideChars = repeatedChar(repeatedChar, sideNbChar);
FormatableChat side = text(sideChars).color(decorationColor);
if (decorationBold)
side.bold();
Chat d = Chat.chat() Chat d = Chat.chat()
.then(text(sideChars).color(decorationColor)) .then(side)
.then(text); .then(text);
if (repeatedChar != ' ') if (repeatedChar != ' ')
d.then(text(sideChars).color(decorationColor)); d.then(side);
return d; return d;
} }
public static Chat leftText(Chat text, char repeatedChar, TextColor decorationColor, int nbLeft, public static Chat leftText(Chat text, char repeatedChar, TextColor decorationColor, int nbLeft, boolean console) {
boolean console) {
return leftText(text, repeatedChar, decorationColor, nbLeft, console, console ? CONSOLE_NB_CHAR_DEFAULT : DEFAULT_CHAT_WIDTH); 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, public static Chat leftText(Chat text, char repeatedChar, TextColor decorationColor, int nbLeft, boolean console, int maxWidth) {
boolean console, int maxWidth) {
int textWidth = componentWidth(text.getAdv(), console); int textWidth = componentWidth(text.getAdv(), console);
int repeatedCharWidth = charW(repeatedChar, console, false); int repeatedCharWidth = charW(repeatedChar, console, false);
@ -280,8 +279,19 @@ public class ChatUtil {
} }
public static Chat emptyLine(char repeatedChar, TextColor decorationColor, boolean console) { 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 emptyLine(repeatedChar, decorationColor, false, console);
return text(repeatedChar(repeatedChar, count)).color(decorationColor); }
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) { private static String repeatedChar(char repeatedChar, int count) {
@ -338,7 +348,7 @@ public class ChatUtil {
return (count < 0) ? 0 : count; 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; if (console) return (c == '§') ? -1 : 1;
for (int px : CHARS_SIZE.keySet()) for (int px : CHARS_SIZE.keySet())
if (CHARS_SIZE.get(px).indexOf(c) >= 0) return px + (bold ? 1 : 0); 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.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.function.Consumer;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
@ -21,7 +22,8 @@ import org.bukkit.plugin.Plugin;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import fr.pandacube.lib.core.chat.Chat; 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; import fr.pandacube.lib.paper.util.ItemStackBuilder;
public class GUIInventory implements Listener { public class GUIInventory implements Listener {
@ -30,21 +32,20 @@ public class GUIInventory implements Listener {
private Player player; private Player player;
private Inventory inv; private Inventory inv;
private Callback<InventoryCloseEvent> onCloseEvent; private Consumer<InventoryCloseEvent> onCloseEvent;
private boolean isOpened = false; private boolean isOpened = false;
private Map<Integer, Callback<InventoryClickEvent>> onClickEvents; private Map<Integer, Consumer<InventoryClickEvent>> onClickEvents;
@Deprecated public GUIInventory(Player p, int nbLines, Chat title, Consumer<InventoryCloseEvent> closeEventAction,
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,
Plugin pl) { Plugin pl) {
if (title == null) if (title == null)
inv = Bukkit.createInventory(null, nbLines * 9); inv = Bukkit.createInventory(null, nbLines * 9);
else else
inv = Bukkit.createInventory(null, nbLines * 9, title.getAdv()); 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); 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; onCloseEvent = closeEventAction;
} }
@ -65,7 +66,7 @@ public class GUIInventory implements Listener {
* été pré-annulée. Pour la rétablir, faites un * été pré-annulée. Pour la rétablir, faites un
* event.setCancelled(false)). * 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) if (inv.getItem(p) == null)
setButton(p, iStack, clickEventActions); setButton(p, iStack, clickEventActions);
} }
@ -75,7 +76,7 @@ public class GUIInventory implements Listener {
* été pré-annulée. Pour la rétablir, faites un * été pré-annulée. Pour la rétablir, faites un
* event.setCancelled(false)). * 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); inv.setItem(p, iStack);
changeClickEventAction(p, clickEventActions); changeClickEventAction(p, clickEventActions);
} }
@ -85,7 +86,7 @@ public class GUIInventory implements Listener {
* été pré-annulée. Pour la rétablir, faites un * été pré-annulée. Pour la rétablir, faites un
* event.setCancelled(false)). * event.setCancelled(false)).
*/ */
public void changeClickEventAction(int p, Callback<InventoryClickEvent> clickEventActions) { public void changeClickEventAction(int p, Consumer<InventoryClickEvent> clickEventActions) {
onClickEvents.put(p, 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. // on ne réagit pas aux clics hors de l'inventaire du dessus.
if (event.getClickedInventory() != event.getView().getTopInventory()) return; if (event.getClickedInventory() != event.getView().getTopInventory()) return;
Callback<InventoryClickEvent> callback = onClickEvents.get(event.getSlot()); Consumer<InventoryClickEvent> callback = onClickEvents.get(event.getSlot());
if (callback != null) callback.done(event); if (callback != null)
callback.accept(event);
} }
@ -149,7 +151,7 @@ public class GUIInventory implements Listener {
HandlerList.unregisterAll(this); HandlerList.unregisterAll(this);
if (onCloseEvent != null) if (onCloseEvent != null)
onCloseEvent.done(event); onCloseEvent.accept(event);
isOpened = false; 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; import java.util.concurrent.Callable;
@ -7,7 +7,7 @@ import org.bukkit.Bukkit;
import fr.pandacube.lib.core.util.Log; import fr.pandacube.lib.core.util.Log;
import fr.pandacube.lib.paper.PandaLibPaper; import fr.pandacube.lib.paper.PandaLibPaper;
public class ThreadUtil { public class SchedulerUtil {
public static void runOnServerThread(Runnable task) { public static void runOnServerThread(Runnable task) {
if (Bukkit.isPrimaryThread()) if (Bukkit.isPrimaryThread())