Updated some ChatUtil methods + some refactoring

This commit is contained in:
2021-09-21 23:54:32 +02:00
parent 152ae89024
commit bb61819332
6 changed files with 114 additions and 45 deletions

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())