Skip to content

Commit dfe4e24

Browse files
committed
feat(navigator): ✨ implement warps command for build teams and enhance warp menu access
1 parent acf0b2a commit dfe4e24

File tree

17 files changed

+181
-98
lines changed

17 files changed

+181
-98
lines changed

src/main/java/net/buildtheearth/modules/navigation/NavUtils.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static boolean isTransferCapable(@NotNull Player player, @NotNull BuildTe
3737
UnsafeValues unsafeValues = Bukkit.getUnsafe();
3838
int serverProtocolVersion = unsafeValues.getProtocolVersion();
3939

40-
ChatHelper.logDebug("Transfer check - Player protocol: %d, Server protocol: %s, Allows transfers: %b", playerVersion, serverProtocolVersion, targetBuildTeam.isAllowsTransfers());
40+
ChatHelper.logDebug("Transfer check - Player protocol: %d, Server protocol: %s, BuildTeam: %s, Allows transfers: %b, IP: %s", playerVersion, serverProtocolVersion, targetBuildTeam.getBlankName(), targetBuildTeam.isAllowsTransfers(), targetBuildTeam.getIP());
4141

4242
return targetBuildTeam.isAllowsTransfers() && playerVersion >= 766 && serverProtocolVersion >= 766;
4343
}
@@ -68,7 +68,7 @@ public static void sendNotConnectedMessage(@NotNull Player player, String server
6868
player.sendMessage("");
6969
player.spigot().sendMessage(comp);
7070
player.sendMessage("");
71-
player.sendMessage(cClick the IP to copy → Press ESC → Back to Menu → Multiplayer → Add Server → Ctrl+V → Done → Join.");
71+
player.sendMessage(cPress Ctrl + A and Ctrl + C to copy the ip → Press ESC → Back to Menu → Multiplayer → Add Server → Ctrl+V → Done → Join.");
7272
}
7373

7474
/**
@@ -83,11 +83,9 @@ public static void sendNoIpMessage(@NotNull Player player, String buildteam) {
8383

8484
public static @Nullable NavSwitchType determineSwitchPossibilityOrMsgPlayerIfNone(@NotNull Player player, @NotNull BuildTeam targetBuildTeam) {
8585
if (targetBuildTeam.isConnected() && targetBuildTeam.getServerName() != null) {
86-
sendPlayerToConnectedServer(player, targetBuildTeam.getServerName());
8786
return NavSwitchType.NETWORK;
8887
} else if (targetBuildTeam.getIP() != null) {
8988
if (isTransferCapable(player, targetBuildTeam)) {
90-
transferPlayer(player, targetBuildTeam.getIP());
9189
return NavSwitchType.TRANSFER;
9290
} else {
9391
sendNotConnectedMessage(player, targetBuildTeam.getIP(), targetBuildTeam.getName());

src/main/java/net/buildtheearth/modules/navigation/NavigationModule.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import net.buildtheearth.modules.navigation.components.tpll.listeners.TpllListener;
1313
import net.buildtheearth.modules.navigation.components.warps.WarpsComponent;
1414
import net.buildtheearth.modules.navigation.components.warps.commands.WarpCommand;
15+
import net.buildtheearth.modules.navigation.components.warps.commands.WarpsBtCommand;
1516
import net.buildtheearth.modules.navigation.components.warps.listeners.WarpJoinListener;
1617
import net.buildtheearth.modules.network.NetworkModule;
1718

@@ -59,6 +60,7 @@ public void registerCommands() {
5960
registerCommand("warp", new WarpCommand());
6061
registerCommand("navigator", new NavigatorCommand());
6162
registerCommand("buildteam", new BuildteamCommand());
63+
registerCommand("warpsbt", new WarpsBtCommand());
6264
}
6365

6466
@Override

src/main/java/net/buildtheearth/modules/navigation/components/navigator/commands/BuildteamCommand.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@ public class BuildteamCommand implements CommandExecutor, TabCompleter {
2020

2121
@Override
2222
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
23+
return btCommand(sender, label, args, Permissions.NAVIGATOR_USE);
24+
}
25+
26+
protected boolean btCommand(@NotNull CommandSender sender, @NotNull String label, String[] args, String permission) {
2327
if (!(sender instanceof Player player)) {
2428
sender.sendMessage(ChatHelper.getErrorString("You must be a %s to %s this command!", "player", "execute"));
2529
return true;
2630
}
2731

28-
if(!player.hasPermission(Permissions.NAVIGATOR_USE)) {
32+
if (!player.hasPermission(permission)) {
2933
player.sendMessage(ChatHelper.getErrorString("You don't have permission to use this command!"));
3034
return true;
3135
}
@@ -36,23 +40,26 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
3640
} else {
3741
String buildTeamName = args[0];
3842
var teams = NetworkModule.getInstance().getBuildTeams().stream()
39-
.filter(buildTeam -> buildTeam.getTag().equalsIgnoreCase(buildTeamName) || buildTeam.getName().equalsIgnoreCase(buildTeamName)).toArray();
43+
.filter(buildTeam -> buildTeam.getTag().equalsIgnoreCase(buildTeamName) || buildTeam.getBlankName().equalsIgnoreCase(buildTeamName)).toArray();
4044
if (teams.length == 0 || !(teams[0] instanceof BuildTeam bt)) {
4145
player.sendMessage(ChatHelper.getErrorString("Build team '%s' does not exist!", buildTeamName));
4246
return true;
4347
}
44-
NavUtils.switchToTeam(bt, player);
45-
48+
execute(player, bt);
4649
}
4750
return true;
4851
}
4952

53+
public void execute(Player player, BuildTeam team) {
54+
NavUtils.switchToTeam(team, player);
55+
}
56+
5057
@Override
5158
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String @NotNull [] args) {
52-
if (args.length == 1) {
59+
if (args.length >= 1) {
5360
String partial = args[0].toLowerCase();
5461
return NetworkModule.getInstance().getBuildTeams().stream()
55-
.flatMap(bt -> Stream.of(bt.getTag(), bt.getName()))
62+
.flatMap(bt -> Stream.of(bt.getTag(), bt.getBlankName()))
5663
.filter(s -> s.toLowerCase().startsWith(partial))
5764
.toList();
5865
}

src/main/java/net/buildtheearth/modules/navigation/components/tpll/TpllComponent.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import net.buildtheearth.modules.network.NetworkModule;
1010
import net.buildtheearth.utils.ChatHelper;
1111
import net.buildtheearth.utils.GeometricUtils;
12+
import org.bukkit.Bukkit;
1213
import org.bukkit.Location;
1314
import org.bukkit.NamespacedKey;
1415
import org.bukkit.entity.Player;
@@ -87,7 +88,7 @@ public void processQueueForPlayer(Player player) {
8788
* @param coordinates The coordinates to send the player to on join.
8889
* @param targetServerName The server to send the player to.
8990
*/
90-
public void tpllPlayer(Player player, double[] coordinates, String targetServerName) {
91+
public void tpllPlayer(@NotNull Player player, double @NotNull [] coordinates, String targetServerName) {
9192
ChatHelper.logDebug("Starting universal tpll teleportation for %s to %s.", player.getDisplayName(), targetServerName);
9293
// Send a plugin message to the target server which adds the tpll to the queue
9394
ByteArrayDataOutput out = ByteStreams.newDataOutput();
@@ -111,8 +112,10 @@ public void tpllPlayerTransfer(@NotNull Player player, double @NotNull [] coordi
111112

112113
public void processCookie(@NotNull Player player, byte[] cookie) {
113114
ByteArrayDataInput in = ByteStreams.newDataInput(cookie);
114-
double targetLatitude = Double.parseDouble(in.readUTF());
115-
double targetLongitude = Double.parseDouble(in.readUTF());
116-
player.performCommand("tpll " + targetLatitude + " " + targetLongitude);
115+
double targetLatitude = in.readDouble();
116+
double targetLongitude = in.readDouble();
117+
ChatHelper.logDebug("Processing cookie for tpll event: lat: %s lon: %s", targetLatitude, targetLongitude);
118+
Bukkit.getScheduler().runTask(BuildTeamTools.getInstance(), // Needs to be delayed if not exception will be trown and nothing happens
119+
() -> player.performCommand("tpll " + targetLatitude + " " + targetLongitude));
117120
}
118121
}

src/main/java/net/buildtheearth/modules/navigation/components/tpll/listeners/TpllJoinListener.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public void onJoin(PlayerJoinEvent event) {
1717
if (cookie != null) {
1818
ChatHelper.logDebug("Player has a TPLL cookie, processing it.");
1919
NavigationModule.getInstance().getTpllComponent().processCookie(event.getPlayer(), cookie);
20+
event.getPlayer().storeCookie(TpllComponent.TPLL_COOKIE_KEY, new byte[0]); // Reset the cookie
2021
} else {
2122
ChatHelper.logDebug("Player does not have a TPLL cookie.");
2223
}

src/main/java/net/buildtheearth/modules/navigation/components/warps/WarpsComponent.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
import net.buildtheearth.modules.navigation.NavUtils;
99
import net.buildtheearth.modules.navigation.components.warps.menu.WarpEditMenu;
1010
import net.buildtheearth.modules.navigation.components.warps.menu.WarpGroupEditMenu;
11+
import net.buildtheearth.modules.navigation.components.warps.menu.WarpGroupMenu;
12+
import net.buildtheearth.modules.navigation.components.warps.menu.WarpMenu;
1113
import net.buildtheearth.modules.navigation.components.warps.model.Warp;
1214
import net.buildtheearth.modules.navigation.components.warps.model.WarpGroup;
1315
import net.buildtheearth.modules.network.NetworkModule;
1416
import net.buildtheearth.modules.network.api.OpenStreetMapAPI;
1517
import net.buildtheearth.modules.network.model.BuildTeam;
18+
import net.buildtheearth.modules.network.model.Continent;
1619
import net.buildtheearth.utils.ChatHelper;
1720
import net.buildtheearth.utils.GeometricUtils;
1821
import net.buildtheearth.utils.geo.CoordinateConversion;
@@ -22,6 +25,7 @@
2225
import org.bukkit.World;
2326
import org.bukkit.entity.Player;
2427
import org.jetbrains.annotations.NotNull;
28+
import org.jetbrains.annotations.Nullable;
2529

2630
import java.util.HashMap;
2731
import java.util.UUID;
@@ -91,9 +95,10 @@ public void processQueueForPlayer(Player player) {
9195
* @param player The player to warp
9296
* @param warp The warp to teleport the player to
9397
*/
94-
public void warpPlayer(Player player, Warp warp) {
98+
public void warpPlayer(Player player, @NotNull Warp warp) {
9599
// If the warp is in the same team, just teleport the player
96100
if(warp.getWarpGroup().getBuildTeam().getID().equals(NetworkModule.getInstance().getBuildTeam().getID())) {
101+
ChatHelper.logDebug("Warping player %s to warp %s", player.getName(), warp.getName());
97102
Location loc = GeometricUtils.getLocationFromCoordinatesYawPitch(new double[]{warp.getLat(), warp.getLon()}, warp.getYaw(), warp.getPitch());
98103

99104
if(loc.getWorld() == null) {
@@ -103,11 +108,13 @@ public void warpPlayer(Player player, Warp warp) {
103108

104109
loc.setY(warp.getY());
105110

106-
player.teleport(loc);
111+
player.teleportAsync(loc);
107112
ChatHelper.sendSuccessfulMessage(player, "Successfully warped you to %s.", warp.getName());
108113
return;
109114
}
110115

116+
ChatHelper.logDebug("Determining switch possibility for warp %s, because it's the wrong server...", warp.getName());
117+
111118
var type = NavUtils.determineSwitchPossibilityOrMsgPlayerIfNone(player, warp.getWarpGroup().getBuildTeam());
112119

113120
if (type == NavUtils.NavSwitchType.NETWORK) {
@@ -208,12 +215,28 @@ public Warp getWarpByName(@NotNull BuildTeam buildTeam, String name) {
208215

209216
public void processCookie(@NotNull Player player, byte[] cookie) {
210217
ByteArrayDataInput in = ByteStreams.newDataInput(cookie);
211-
212-
warpPlayer(player, getWarpByKey(in.readUTF()));
218+
Warp warp = getWarpByKey(in.readUTF());
219+
ChatHelper.logDebug("Processing cookie for warp %s", warp.getName());
220+
warpPlayer(player, warp);
213221
}
214222

215223
public Warp getWarpByKey(String key) {
224+
ChatHelper.logDebug("Retrieving warp with key %s", key);
216225
return NetworkModule.getInstance().getBuildTeam().getWarpGroups().stream().flatMap(warpGroup -> warpGroup.getWarps().stream())
217226
.filter(warp1 -> warp1.getId().toString().equals(key)).findFirst().orElse(null);
218227
}
228+
229+
public static void openWarpMenu(@NotNull Player player) {
230+
openWarpMenu(player, NetworkModule.getInstance().getBuildTeam(), null);
231+
}
232+
233+
public static void openWarpMenu(@NotNull Player player, @NotNull BuildTeam buildTeam, @Nullable Continent continent) {
234+
int warpGroupCount = buildTeam.getWarpGroups().size();
235+
236+
switch (warpGroupCount) {
237+
case 0 -> player.sendMessage(ChatHelper.getErrorString("This server does not have any warps yet!"));
238+
case 1 -> new WarpMenu(player, buildTeam.getWarpGroups().getFirst(), false, true);
239+
default -> new WarpGroupMenu(player, buildTeam, continent != null, true, continent);
240+
}
241+
}
219242
}

src/main/java/net/buildtheearth/modules/navigation/components/warps/commands/WarpCommand.java

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package net.buildtheearth.modules.navigation.components.warps.commands;
22

33
import net.buildtheearth.modules.navigation.NavigationModule;
4-
import net.buildtheearth.modules.navigation.components.warps.menu.WarpGroupMenu;
5-
import net.buildtheearth.modules.navigation.components.warps.menu.WarpMenu;
4+
import net.buildtheearth.modules.navigation.components.warps.WarpsComponent;
65
import net.buildtheearth.modules.navigation.components.warps.model.Warp;
76
import net.buildtheearth.modules.network.NetworkModule;
87
import net.buildtheearth.modules.network.model.Permissions;
@@ -35,33 +34,24 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
3534

3635
// If no arguments were supplied assume the player wants to open the warp menu
3736
if (args.length == 0) {
38-
int warpGroupCount = NetworkModule.getInstance().getBuildTeam().getWarpGroups().size();
39-
40-
if(warpGroupCount == 0){
41-
player.sendMessage(ChatHelper.getErrorString("This server does not have any warps yet!"));
42-
return true;
43-
}else if(warpGroupCount == 1)
44-
new WarpMenu(player, NetworkModule.getInstance().getBuildTeam().getWarpGroups().getFirst(), false, true);
45-
else
46-
new WarpGroupMenu(player, NetworkModule.getInstance().getBuildTeam(), false, true);
47-
37+
if (checkForWarpUsePermissionAndMessage(player)) WarpsComponent.openWarpMenu(player);
4838
return true;
4939
}
5040

5141
// WARP CREATE
5242
if (args[0].equalsIgnoreCase("create")) {
53-
// Check if the command has only one argument
54-
if (args.length > 1){
55-
player.sendMessage(ChatHelper.getErrorString("Usage: /warp create"));
56-
return true;
57-
}
58-
5943
// Check if the player has the required permissions
6044
if (!player.hasPermission(Permissions.WARP_CREATE)) {
6145
player.sendMessage(ChatHelper.getErrorString("You don't have the required %s to %s warps.", "permission", "create"));
6246
return true;
6347
}
6448

49+
// Check if the command has only one argument
50+
if (args.length > 1) {
51+
player.sendMessage(ChatHelper.getErrorString("Usage: /warp create"));
52+
return true;
53+
}
54+
6555
player.sendActionBar(ChatHelper.getStandardString(false, "Creating the warp..."));
6656

6757
NavigationModule.getInstance().getWarpsComponent().createWarp(player);
@@ -72,11 +62,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
7262
// Combine the args to one warp name
7363
String key = String.join(" ", args);
7464

75-
// Check if the player has the required permission
76-
if (!player.hasPermission(Permissions.WARP_USE)) {
77-
player.sendMessage(ChatHelper.getErrorString("You don't have the required %s to %s warps.", "permission", "use"));
78-
return true;
79-
}
65+
if (!checkForWarpUsePermissionAndMessage(player)) return true;
8066

8167
// Find the warp with the given key
8268
Warp warp = NavigationModule.getInstance().getWarpsComponent().getWarpByName(key);
@@ -91,6 +77,15 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
9177
return true;
9278
}
9379

80+
private static boolean checkForWarpUsePermissionAndMessage(@NotNull Player player) {
81+
// Check if the player has the required permission
82+
if (!player.hasPermission(Permissions.WARP_USE)) {
83+
player.sendMessage(ChatHelper.getErrorString("You don't have the required %s to %s warps.", "permission", "use"));
84+
return false;
85+
}
86+
return true;
87+
}
88+
9489
@Override
9590
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String @NotNull [] args) {
9691
if (args.length == 1) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package net.buildtheearth.modules.navigation.components.warps.commands;
2+
3+
import net.buildtheearth.modules.navigation.components.navigator.commands.BuildteamCommand;
4+
import net.buildtheearth.modules.navigation.components.warps.WarpsComponent;
5+
import net.buildtheearth.modules.network.model.BuildTeam;
6+
import net.buildtheearth.modules.network.model.Permissions;
7+
import org.bukkit.command.Command;
8+
import org.bukkit.command.CommandExecutor;
9+
import org.bukkit.command.CommandSender;
10+
import org.bukkit.command.TabCompleter;
11+
import org.bukkit.entity.Player;
12+
import org.jetbrains.annotations.NotNull;
13+
14+
public class WarpsBtCommand extends BuildteamCommand implements CommandExecutor, TabCompleter {
15+
@Override
16+
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
17+
return btCommand(sender, label, args, Permissions.WARP_USE);
18+
}
19+
20+
@Override
21+
public void execute(Player player, BuildTeam team) {
22+
WarpsComponent.openWarpMenu(player, team, null);
23+
}
24+
}
25+

src/main/java/net/buildtheearth/modules/navigation/components/warps/listeners/WarpJoinListener.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public void onJoin(@NotNull PlayerJoinEvent event) {
1818
if (cookie != null) {
1919
ChatHelper.logDebug("Player has a warp cookie, processing it.");
2020
NavigationModule.getInstance().getWarpsComponent().processCookie(event.getPlayer(), cookie);
21+
event.getPlayer().storeCookie(WarpsComponent.WARP_COOKIE_KEY, new byte[0]); // Reset the cookie
2122
} else {
2223
ChatHelper.logDebug("Player does not have a warp cookie.");
2324
}

src/main/java/net/buildtheearth/modules/navigation/components/warps/menu/WarpMenu.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
package net.buildtheearth.modules.navigation.components.warps.menu;
22

33
import net.buildtheearth.modules.navigation.NavigationModule;
4+
import net.buildtheearth.modules.navigation.components.warps.model.Warp;
5+
import net.buildtheearth.modules.navigation.components.warps.model.WarpGroup;
46
import net.buildtheearth.modules.network.NetworkModule;
57
import net.buildtheearth.modules.network.model.Permissions;
6-
import net.buildtheearth.utils.*;
8+
import net.buildtheearth.utils.CustomHeads;
9+
import net.buildtheearth.utils.Item;
10+
import net.buildtheearth.utils.ListUtil;
11+
import net.buildtheearth.utils.MenuItems;
712
import net.buildtheearth.utils.menus.AbstractPaginatedMenu;
8-
import net.buildtheearth.modules.navigation.components.warps.model.Warp;
9-
import net.buildtheearth.modules.navigation.components.warps.model.WarpGroup;
1013
import org.bukkit.entity.Player;
1114
import org.ipvp.canvas.mask.BinaryMask;
1215
import org.ipvp.canvas.mask.Mask;
1316
import org.jetbrains.annotations.NotNull;
1417

15-
import java.util.ArrayList;
16-
import java.util.Arrays;
1718
import java.util.List;
1819
import java.util.stream.Collectors;
1920

@@ -86,10 +87,6 @@ protected void setPaginatedPreviewItems(@NotNull List<?> source) {
8687
continue;
8788
}
8889

89-
ArrayList<String> loreLines = ListUtil.createList("", "§eAddress:");
90-
loreLines.addAll(Arrays.asList(Utils.splitStringByLineLength(warp.getAddress(), 30, ", ")));
91-
loreLines.addAll(ListUtil.createList("", "§8Left-Click to warp to this location.", "§8Right-Click to edit this warp."));
92-
9390
getMenu().getSlot(slot).setItem(warp.getMaterialItem());
9491
slot++;
9592
}

0 commit comments

Comments
 (0)