Skip to content

Commit d736c2c

Browse files
committed
🚧 Add waypoint helper
1 parent 48182ad commit d736c2c

File tree

5 files changed

+210
-0
lines changed

5 files changed

+210
-0
lines changed

API/src/main/java/fr/maxlego08/essentials/api/EssentialsPlugin.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import fr.maxlego08.essentials.api.utils.component.ComponentMessage;
3030
import fr.maxlego08.essentials.api.vault.VaultManager;
3131
import fr.maxlego08.essentials.api.vote.VoteManager;
32+
import fr.maxlego08.essentials.api.waypoint.WayPointHelper;
3233
import fr.maxlego08.essentials.api.worldedit.WorldeditManager;
3334
import fr.maxlego08.menu.api.ButtonManager;
3435
import fr.maxlego08.menu.api.InventoryManager;
@@ -442,4 +443,12 @@ public interface EssentialsPlugin extends Plugin {
442443
* @return the sanction manager
443444
*/
444445
SanctionManager getSanctionManager();
446+
447+
/**
448+
* Returns the waypoint helper.
449+
* This helper is used to create, update, and delete waypoints from the player's locator bar.
450+
*
451+
* @return the waypoint helper
452+
*/
453+
WayPointHelper getWayPointHelper();
445454
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package fr.maxlego08.essentials.api.waypoint;
2+
3+
import org.bukkit.Chunk;
4+
import org.bukkit.Location;
5+
import org.bukkit.entity.Player;
6+
7+
import java.util.UUID;
8+
9+
public interface WayPointHelper {
10+
11+
/**
12+
* Adds a waypoint to a player's map.
13+
*
14+
* @param player the player to add the waypoint to
15+
* @param uniqueId the unique ID of the waypoint
16+
* @param location the location of the waypoint
17+
* @param wayPointIcon the icon of the waypoint
18+
*/
19+
void addWayPoint(Player player, UUID uniqueId, Location location, WayPointIcon wayPointIcon);
20+
21+
/**
22+
* Updates the position of an already existing waypoint on a player's map.
23+
*
24+
* @param player the player to update the waypoint on
25+
* @param uniqueId the unique ID of the waypoint
26+
* @param location the new location of the waypoint
27+
* @param wayPointIcon the icon of the waypoint
28+
*/
29+
void updateWayPointPosition(Player player, UUID uniqueId, Location location, WayPointIcon wayPointIcon);
30+
31+
/**
32+
* Adds a waypoint to a player's map.
33+
*
34+
* @param player the player to add the waypoint to
35+
* @param uniqueId the unique ID of the waypoint
36+
* @param chunk the chunk where the waypoint is located
37+
* @param wayPointIcon the icon of the waypoint
38+
*/
39+
void addWayPoint(Player player, UUID uniqueId, Chunk chunk, WayPointIcon wayPointIcon);
40+
41+
/**
42+
* Updates the position of an already existing waypoint in a specific chunk on a player's map.
43+
*
44+
* @param player the player whose waypoint is to be updated
45+
* @param uniqueId the unique ID of the waypoint
46+
* @param chunk the new chunk where the waypoint is located
47+
* @param wayPointIcon the icon of the waypoint
48+
*/
49+
void updateWayPointPosition(Player player, UUID uniqueId, Chunk chunk, WayPointIcon wayPointIcon);
50+
51+
/**
52+
* Removes a waypoint from a player's map.
53+
*
54+
* @param player the player from whom to remove the waypoint
55+
* @param uniqueId the unique ID of the waypoint
56+
*/
57+
void removeWayPoint(Player player, UUID uniqueId);
58+
59+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package fr.maxlego08.essentials.api.waypoint;
2+
3+
import java.awt.*;
4+
5+
public record WayPointIcon(String texture, Color color) {
6+
7+
public static WayPointIcon of(Color color) {
8+
return new WayPointIcon(null, color);
9+
}
10+
11+
public static WayPointIcon of(String texture) {
12+
return new WayPointIcon(texture, Color.WHITE);
13+
}
14+
15+
public static WayPointIcon of(String texture, Color color) {
16+
return new WayPointIcon(texture, color);
17+
}
18+
19+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package fr.maxlego08.essentials.nms.v1_21_8;
2+
3+
import fr.maxlego08.essentials.api.waypoint.WayPointHelper;
4+
import fr.maxlego08.essentials.api.waypoint.WayPointIcon;
5+
import net.minecraft.core.BlockPos;
6+
import net.minecraft.network.protocol.Packet;
7+
import net.minecraft.network.protocol.game.ClientboundTrackedWaypointPacket;
8+
import net.minecraft.world.level.ChunkPos;
9+
import net.minecraft.world.waypoints.Waypoint;
10+
import net.minecraft.world.waypoints.WaypointStyleAssets;
11+
import org.bukkit.Chunk;
12+
import org.bukkit.Location;
13+
import org.bukkit.craftbukkit.entity.CraftPlayer;
14+
import org.bukkit.entity.Player;
15+
16+
import java.util.Optional;
17+
import java.util.UUID;
18+
19+
public class WayPointPacket implements WayPointHelper {
20+
21+
public void show(Player player, Location location) {
22+
23+
var waypointUUID = UUID.randomUUID();
24+
25+
var icon = new Waypoint.Icon();
26+
icon.style = WaypointStyleAssets.createId("death_waypoint");
27+
icon.color = Optional.of(16777215);
28+
29+
BlockPos blockPos = new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ());
30+
ClientboundTrackedWaypointPacket packet = ClientboundTrackedWaypointPacket.addWaypointPosition(player.getUniqueId(), icon, blockPos);
31+
((CraftPlayer) player).getHandle().connection.send(packet);
32+
}
33+
34+
@Override
35+
public void addWayPoint(Player player, UUID uniqueId, Location location, WayPointIcon wayPointIcon) {
36+
37+
var icon = toIcon(wayPointIcon);
38+
var blockPos = new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ());
39+
var packet = ClientboundTrackedWaypointPacket.addWaypointPosition(uniqueId, icon, blockPos);
40+
41+
sendPacket(player, packet);
42+
}
43+
44+
@Override
45+
public void updateWayPointPosition(Player player, UUID uniqueId, Location location, WayPointIcon wayPointIcon) {
46+
47+
var icon = toIcon(wayPointIcon);
48+
var blockPos = new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ());
49+
var packet = ClientboundTrackedWaypointPacket.updateWaypointPosition(uniqueId, icon, blockPos);
50+
51+
sendPacket(player, packet);
52+
}
53+
54+
@Override
55+
public void addWayPoint(Player player, UUID uniqueId, Chunk chunk, WayPointIcon wayPointIcon) {
56+
57+
var icon = toIcon(wayPointIcon);
58+
var chunkPos = new ChunkPos(chunk.getX(), chunk.getZ());
59+
var packet = ClientboundTrackedWaypointPacket.addWaypointChunk(uniqueId, icon, chunkPos);
60+
61+
sendPacket(player, packet);
62+
}
63+
64+
@Override
65+
public void updateWayPointPosition(Player player, UUID uniqueId, Chunk chunk, WayPointIcon wayPointIcon) {
66+
67+
var icon = toIcon(wayPointIcon);
68+
var chunkPos = new ChunkPos(chunk.getX(), chunk.getZ());
69+
var packet = ClientboundTrackedWaypointPacket.updateWaypointChunk(uniqueId, icon, chunkPos);
70+
71+
sendPacket(player, packet);
72+
}
73+
74+
@Override
75+
public void removeWayPoint(Player player, UUID uniqueId) {
76+
77+
var packet = ClientboundTrackedWaypointPacket.removeWaypoint(uniqueId);
78+
sendPacket(player, packet);
79+
}
80+
81+
private void sendPacket(Player player, Packet<?> packet) {
82+
((CraftPlayer) player).getHandle().connection.send(packet);
83+
}
84+
85+
/**
86+
* Converts a {@link WayPointIcon} to a {@link Waypoint.Icon}.
87+
* <p>
88+
* If the given {@link WayPointIcon} has a texture, then the style of the resulting icon will be set to the
89+
* death waypoint style. Otherwise, the style will be left unchanged.
90+
* <p>
91+
* The color of the resulting icon will be set to the color of the given {@link WayPointIcon}.
92+
*
93+
* @param wayPointIcon the icon to convert
94+
* @return the converted icon
95+
*/
96+
private Waypoint.Icon toIcon(WayPointIcon wayPointIcon) {
97+
var icon = new Waypoint.Icon();
98+
if (wayPointIcon.texture() != null) {
99+
icon.style = WaypointStyleAssets.createId("death_waypoint");
100+
}
101+
icon.color = Optional.of(wayPointIcon.color().getRGB() & 0xFFFFFF);
102+
return icon;
103+
}
104+
}

src/main/java/fr/maxlego08/essentials/ZEssentialsPlugin.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import fr.maxlego08.essentials.api.utils.component.ComponentMessage;
4040
import fr.maxlego08.essentials.api.vault.VaultManager;
4141
import fr.maxlego08.essentials.api.vote.VoteManager;
42+
import fr.maxlego08.essentials.api.waypoint.WayPointHelper;
4243
import fr.maxlego08.essentials.api.worldedit.WorldeditManager;
4344
import fr.maxlego08.essentials.buttons.ButtonHomes;
4445
import fr.maxlego08.essentials.buttons.ButtonPayConfirm;
@@ -170,6 +171,7 @@ public final class ZEssentialsPlugin extends ZPlugin implements EssentialsPlugin
170171
private RandomWord randomWord;
171172
private long serverStartUptime;
172173
private BlockTracker blockTracker = new DefaultBlockTracker();
174+
private WayPointHelper wayPointHelper;
173175

174176
@Override
175177
public void onEnable() {
@@ -767,6 +769,23 @@ public SanctionManager getSanctionManager() {
767769
return getModuleManager().getModule(SanctionModule.class);
768770
}
769771

772+
@Override
773+
public WayPointHelper getWayPointHelper() {
774+
if (this.wayPointHelper == null) {
775+
String version = NmsVersion.getCurrentVersion().name().replace("V_", "v");
776+
String className = String.format("fr.maxlego08.essentials.nms.%s.WayPointPacket", version);
777+
778+
try {
779+
Class<?> clazz = Class.forName(className);
780+
this.wayPointHelper = (WayPointHelper) clazz.getConstructor().newInstance();
781+
} catch (Exception exception) {
782+
this.getLogger().severe("Cannot create a new instance for the class " + className);
783+
this.getLogger().severe(exception.getMessage());
784+
}
785+
}
786+
return this.wayPointHelper;
787+
}
788+
770789
private void loadHooks() {
771790
if (getServer().getPluginManager().isPluginEnabled("BlockTracker")) {
772791
createInstance("BlockTrackerHook").ifPresent(object -> {

0 commit comments

Comments
 (0)