From f9e638f63fd4282ddbc3565d0ff71abd97f05181 Mon Sep 17 00:00:00 2001 From: Jose Casas <20496401+PnsDev@users.noreply.github.com> Date: Sun, 2 Jan 2022 02:40:47 -0500 Subject: [PATCH] Fixes and Improvements + Fixed sign showing up after the GUI is closed. + Added lombok (makes my life easier.) + Made async packet process sync (so it can interact with the world directly without the need of implementing your own solution or running up the task id number.) --- pom.xml | 6 ++++ src/main/java/dev/pns/signapi/Async2Sync.java | 29 +++++++++++++++++++ src/main/java/dev/pns/signapi/SignAPI.java | 21 +++++++++----- src/main/java/dev/pns/signapi/SignGUI.java | 6 ++-- src/main/java/dev/pns/signapi/SignTask.java | 17 +++++++++++ 5 files changed, 69 insertions(+), 10 deletions(-) create mode 100644 src/main/java/dev/pns/signapi/Async2Sync.java create mode 100644 src/main/java/dev/pns/signapi/SignTask.java diff --git a/pom.xml b/pom.xml index 6bbdb5a..605de31 100644 --- a/pom.xml +++ b/pom.xml @@ -70,5 +70,11 @@ 4.7.0 provided + + org.projectlombok + lombok + RELEASE + compile + diff --git a/src/main/java/dev/pns/signapi/Async2Sync.java b/src/main/java/dev/pns/signapi/Async2Sync.java new file mode 100644 index 0000000..4ed3d6a --- /dev/null +++ b/src/main/java/dev/pns/signapi/Async2Sync.java @@ -0,0 +1,29 @@ +package dev.pns.signapi; + +import org.bukkit.Bukkit; +import org.bukkit.plugin.java.JavaPlugin; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class Async2Sync { + private final List signTasks = new ArrayList<>(); + + public Async2Sync(JavaPlugin plugin) { + Bukkit.getScheduler().runTaskTimer(plugin, () -> { + if (signTasks.isEmpty()) return; + Iterator iterator = signTasks.iterator(); + while (iterator.hasNext()) { + SignTask signTask = iterator.next(); + signTask.getPlayer().sendBlockChange(signTask.getBlock().getLocation(), signTask.getBlock().getBlockData()); + signTask.getOnClose().method(signTask.getPlayer(), signTask.getLines()); + iterator.remove(); + } + }, 0, 1); + } + + public void add(SignTask signTask) { + signTasks.add(signTask); + } +} diff --git a/src/main/java/dev/pns/signapi/SignAPI.java b/src/main/java/dev/pns/signapi/SignAPI.java index 57d927a..678ec41 100644 --- a/src/main/java/dev/pns/signapi/SignAPI.java +++ b/src/main/java/dev/pns/signapi/SignAPI.java @@ -4,6 +4,7 @@ import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.events.PacketAdapter; import com.comphenix.protocol.events.PacketEvent; +import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; import java.util.HashMap; @@ -12,19 +13,25 @@ import java.util.UUID; public class SignAPI { - private final Map openGUIs = new HashMap<>(); + private final Map openGUIs = new HashMap<>(); + private final Async2Sync async2Sync; public SignAPI(JavaPlugin plugin) { + async2Sync = new Async2Sync(plugin); + ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(plugin, PacketType.Play.Client.UPDATE_SIGN) { @Override public void onPacketReceiving(PacketEvent event) { - UUID uuid = event.getPlayer().getUniqueId(); + Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, () -> { + UUID uuid = event.getPlayer().getUniqueId(); + if (!openGUIs.containsKey(uuid)) return; + + SignTask signTask = openGUIs.get(uuid); + signTask.setLines(event.getPacket().getStringArrays().read(0)); - if (!openGUIs.containsKey(uuid)) return; - SignGUI.onClose closeEvent = openGUIs.get(uuid); - - openGUIs.remove(uuid); - closeEvent.method(event.getPlayer(), event.getPacket().getStringArrays().read(0)); + openGUIs.remove(uuid); + async2Sync.add(signTask); + }, 0); } }); } diff --git a/src/main/java/dev/pns/signapi/SignGUI.java b/src/main/java/dev/pns/signapi/SignGUI.java index 3377495..19060d7 100644 --- a/src/main/java/dev/pns/signapi/SignGUI.java +++ b/src/main/java/dev/pns/signapi/SignGUI.java @@ -17,10 +17,10 @@ public class SignGUI { private final onClose onClose; private final List text; - private final Map openGUIs; + private final Map openGUIs; private final ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager(); - public SignGUI(Map openGUIs, List text, onClose closeFunction) { + public SignGUI(Map openGUIs, List text, onClose closeFunction) { this.openGUIs = openGUIs; this.text = text; this.onClose = closeFunction; @@ -59,7 +59,7 @@ public void open(Player player) { sendPacket(player, packet); // Store the method to be called when the sign is closed - openGUIs.put(player.getUniqueId(), onClose); + openGUIs.put(player.getUniqueId(), new SignTask(player, onClose, player.getWorld().getBlockAt(bp.getX(), bp.getY(), bp.getZ()))); } public interface onClose { diff --git a/src/main/java/dev/pns/signapi/SignTask.java b/src/main/java/dev/pns/signapi/SignTask.java new file mode 100644 index 0000000..0109d78 --- /dev/null +++ b/src/main/java/dev/pns/signapi/SignTask.java @@ -0,0 +1,17 @@ +package dev.pns.signapi; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lombok.Setter; +import org.bukkit.block.Block; +import org.bukkit.entity.Player; + +@Getter +@RequiredArgsConstructor +public class SignTask { + private final Player player; + private final SignGUI.onClose onClose; + private final Block block; + @Setter + private String[] lines; +}