Skip to content

Commit

Permalink
Fixes and Improvements
Browse files Browse the repository at this point in the history
+ 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.)
  • Loading branch information
PnsDev committed Jan 2, 2022
1 parent 4c08772 commit f9e638f
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 10 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,11 @@
<version>4.7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
29 changes: 29 additions & 0 deletions src/main/java/dev/pns/signapi/Async2Sync.java
Original file line number Diff line number Diff line change
@@ -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<SignTask> signTasks = new ArrayList<>();

public Async2Sync(JavaPlugin plugin) {
Bukkit.getScheduler().runTaskTimer(plugin, () -> {
if (signTasks.isEmpty()) return;
Iterator<SignTask> 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);
}
}
21 changes: 14 additions & 7 deletions src/main/java/dev/pns/signapi/SignAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,19 +13,25 @@
import java.util.UUID;

public class SignAPI {
private final Map<UUID, SignGUI.onClose> openGUIs = new HashMap<>();
private final Map<UUID, SignTask> 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);
}
});
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/dev/pns/signapi/SignGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
public class SignGUI {
private final onClose onClose;
private final List<String> text;
private final Map<UUID, onClose> openGUIs;
private final Map<UUID, SignTask> openGUIs;
private final ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();

public SignGUI(Map<UUID, onClose> openGUIs, List<String> text, onClose closeFunction) {
public SignGUI(Map<UUID, SignTask> openGUIs, List<String> text, onClose closeFunction) {
this.openGUIs = openGUIs;
this.text = text;
this.onClose = closeFunction;
Expand Down Expand Up @@ -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 {
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/dev/pns/signapi/SignTask.java
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit f9e638f

Please sign in to comment.