Skip to content

Commit 354c0c3

Browse files
committed
Init
1 parent cabf1f2 commit 354c0c3

File tree

4 files changed

+300
-0
lines changed

4 files changed

+300
-0
lines changed

.gitignore

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# User-specific stuff
2+
.idea/
3+
4+
*.iml
5+
*.ipr
6+
*.iws
7+
8+
# IntelliJ
9+
out/
10+
11+
# Compiled class file
12+
*.class
13+
14+
# Log file
15+
*.log
16+
17+
# BlueJ files
18+
*.ctxt
19+
20+
# Package Files #
21+
*.jar
22+
*.war
23+
*.nar
24+
*.ear
25+
*.zip
26+
*.tar.gz
27+
*.rar
28+
29+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
30+
hs_err_pid*
31+
32+
*~
33+
34+
# temporary files which can be created if a process still has a handle open of a deleted file
35+
.fuse_hidden*
36+
37+
# KDE directory preferences
38+
.directory
39+
40+
# Linux trash folder which might appear on any partition or disk
41+
.Trash-*
42+
43+
# .nfs files are created when an open file is removed but is still being accessed
44+
.nfs*
45+
46+
# General
47+
.DS_Store
48+
.AppleDouble
49+
.LSOverride
50+
51+
# Icon must end with two \r
52+
Icon
53+
54+
# Thumbnails
55+
._*
56+
57+
# Files that might appear in the root of a volume
58+
.DocumentRevisions-V100
59+
.fseventsd
60+
.Spotlight-V100
61+
.TemporaryItems
62+
.Trashes
63+
.VolumeIcon.icns
64+
.com.apple.timemachine.donotpresent
65+
66+
# Directories potentially created on remote AFP share
67+
.AppleDB
68+
.AppleDesktop
69+
Network Trash Folder
70+
Temporary Items
71+
.apdisk
72+
73+
# Windows thumbnail cache files
74+
Thumbs.db
75+
Thumbs.db:encryptable
76+
ehthumbs.db
77+
ehthumbs_vista.db
78+
79+
# Dump file
80+
*.stackdump
81+
82+
# Folder config file
83+
[Dd]esktop.ini
84+
85+
# Recycle Bin used on file shares
86+
$RECYCLE.BIN/
87+
88+
# Windows Installer files
89+
*.cab
90+
*.msi
91+
*.msix
92+
*.msm
93+
*.msp
94+
95+
# Windows shortcuts
96+
*.lnk
97+
98+
target/
99+
100+
pom.xml.tag
101+
pom.xml.releaseBackup
102+
pom.xml.versionsBackup
103+
pom.xml.next
104+
105+
release.properties
106+
dependency-reduced-pom.xml
107+
buildNumber.properties
108+
.mvn/timing.properties
109+
.mvn/wrapper/maven-wrapper.jar
110+
.flattened-pom.xml
111+
112+
# Common working directory
113+
run/

pom.xml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>dev.pns</groupId>
8+
<artifactId>SignAPI</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<packaging>jar</packaging>
11+
12+
<name>SignAPI</name>
13+
14+
<properties>
15+
<java.version>1.8</java.version>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
</properties>
18+
19+
<build>
20+
<plugins>
21+
<plugin>
22+
<groupId>org.apache.maven.plugins</groupId>
23+
<artifactId>maven-compiler-plugin</artifactId>
24+
<version>3.8.1</version>
25+
<configuration>
26+
<source>${java.version}</source>
27+
<target>${java.version}</target>
28+
</configuration>
29+
</plugin>
30+
<plugin>
31+
<groupId>org.apache.maven.plugins</groupId>
32+
<artifactId>maven-shade-plugin</artifactId>
33+
<version>3.2.4</version>
34+
<executions>
35+
<execution>
36+
<phase>package</phase>
37+
<goals>
38+
<goal>shade</goal>
39+
</goals>
40+
<configuration>
41+
<createDependencyReducedPom>false</createDependencyReducedPom>
42+
</configuration>
43+
</execution>
44+
</executions>
45+
</plugin>
46+
</plugins>
47+
</build>
48+
49+
<repositories>
50+
<repository>
51+
<id>papermc-repo</id>
52+
<url>https://papermc.io/repo/repository/maven-public/</url>
53+
</repository>
54+
<repository>
55+
<id>dmulloy2-repo</id>
56+
<url>https://repo.dmulloy2.net/repository/public/</url>
57+
</repository>
58+
</repositories>
59+
60+
<dependencies>
61+
<dependency>
62+
<groupId>io.papermc.paper</groupId>
63+
<artifactId>paper-api</artifactId>
64+
<version>1.17-R0.1-SNAPSHOT</version>
65+
<scope>provided</scope>
66+
</dependency>
67+
<dependency>
68+
<groupId>com.comphenix.protocol</groupId>
69+
<artifactId>ProtocolLib</artifactId>
70+
<version>4.7.0</version>
71+
<scope>provided</scope>
72+
</dependency>
73+
</dependencies>
74+
</project>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package dev.pns.signapi;
2+
3+
import com.comphenix.protocol.PacketType;
4+
import com.comphenix.protocol.ProtocolLibrary;
5+
import com.comphenix.protocol.events.PacketAdapter;
6+
import com.comphenix.protocol.events.PacketEvent;
7+
import org.bukkit.plugin.java.JavaPlugin;
8+
9+
import java.util.HashMap;
10+
import java.util.List;
11+
import java.util.Map;
12+
import java.util.UUID;
13+
14+
public class SignAPI {
15+
private final Map<UUID, SignGUI.onClose> openGUIs = new HashMap<>();
16+
17+
public SignAPI(JavaPlugin plugin) {
18+
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(plugin, PacketType.Play.Client.UPDATE_SIGN) {
19+
@Override
20+
public void onPacketReceiving(PacketEvent event) {
21+
UUID uuid = event.getPlayer().getUniqueId();
22+
23+
if (!openGUIs.containsKey(uuid)) return;
24+
openGUIs.get(uuid).method(event.getPlayer(), event.getPacket().getStringArrays().read(0));
25+
openGUIs.remove(uuid);
26+
}
27+
});
28+
}
29+
30+
/**
31+
* Creates a sign GUI for the player.
32+
* @param text The text to display on the sign.
33+
* @param onClose The function to call when the player closes the GUI.
34+
* @return The GUI object.
35+
*/
36+
public SignGUI createGUI(List<String> text, SignGUI.onClose onClose) {
37+
return new SignGUI(openGUIs, text, onClose);
38+
}
39+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package dev.pns.signapi;
2+
3+
import com.comphenix.protocol.PacketType;
4+
import com.comphenix.protocol.ProtocolLibrary;
5+
import com.comphenix.protocol.ProtocolManager;
6+
import com.comphenix.protocol.events.PacketContainer;
7+
import com.comphenix.protocol.wrappers.BlockPosition;
8+
import com.comphenix.protocol.wrappers.nbt.NbtCompound;
9+
import org.bukkit.Material;
10+
import org.bukkit.entity.Player;
11+
12+
import java.lang.reflect.InvocationTargetException;
13+
import java.util.List;
14+
import java.util.Map;
15+
import java.util.UUID;
16+
17+
public class SignGUI {
18+
private final onClose onClose;
19+
private final List<String> text;
20+
private final Map<UUID, onClose> openGUIs;
21+
private final ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();
22+
23+
public SignGUI(Map<UUID, onClose> openGUIs, List<String> text, onClose closeFunction) {
24+
this.openGUIs = openGUIs;
25+
this.text = text;
26+
this.onClose = closeFunction;
27+
}
28+
29+
public void open(Player player) {
30+
// Send a packet to show a sign at location
31+
BlockPosition bp = new BlockPosition(player.getLocation().getBlockX(), (255 - player.getLocation().getBlockY()), player.getLocation().getBlockZ());
32+
player.sendBlockChange(bp.toLocation(player.getWorld()), Material.OAK_SIGN.createBlockData());
33+
34+
// Update the sign with the text
35+
if (text != null && text.size() > 0) {
36+
PacketContainer updateSign = protocolManager.createPacket(PacketType.Play.Server.TILE_ENTITY_DATA);
37+
NbtCompound signNBT = (NbtCompound) updateSign.getNbtModifier().read(0);
38+
39+
// Write all nbt data
40+
signNBT.put("id", "minecraft:sign");
41+
signNBT.put("x", bp.getX());
42+
signNBT.put("y", bp.getY());
43+
signNBT.put("z", bp.getZ());
44+
45+
for (int i = 0; i < 4; i++) {
46+
signNBT.put("Text" + (i + 1), text.size() > i ? String.format("{\"text\":\"%s\"}", text.get(i)) : "");
47+
}
48+
49+
updateSign.getNbtModifier().write(0, signNBT);
50+
updateSign.getBlockPositionModifier().write(0, bp);
51+
updateSign.getIntegers().write(0, 9);
52+
53+
sendPacket(player, updateSign);
54+
}
55+
56+
// Send a packet to open the sign
57+
PacketContainer packet = protocolManager.createPacket(PacketType.Play.Server.OPEN_SIGN_EDITOR);
58+
packet.getBlockPositionModifier().write(0, bp);
59+
sendPacket(player, packet);
60+
61+
// Store the method to be called when the sign is closed
62+
openGUIs.put(player.getUniqueId(), onClose);
63+
}
64+
65+
public interface onClose {
66+
void method(Player player, String[] lines);
67+
}
68+
69+
private void sendPacket(Player player, PacketContainer packet) {
70+
try {
71+
protocolManager.sendServerPacket(player, packet);
72+
} catch (InvocationTargetException e) {e.printStackTrace();}
73+
}
74+
}

0 commit comments

Comments
 (0)