mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-01 14:38:53 +00:00
Add support for receiving multiple teleport requests, Close #74
This commit is contained in:
1 parent
255b66c4bd
commit
e22367a2ec
10 files changed
+120
-29
No files matched your search
@@ -1,6 +1,7 @@
|
||||
package me.william278.huskhomes2.commands;
|
||||
|
||||
import me.william278.huskhomes2.teleport.TeleportRequestHandler;
|
||||
import me.william278.huskhomes2.util.NameAutoCompleter;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@@ -8,6 +9,11 @@ public class TpAcceptCommand extends CommandBase {
|
||||
|
||||
@Override
|
||||
protected void onCommand(Player p, Command command, String label, String[] args) {
|
||||
TeleportRequestHandler.replyTpRequest(p, true);
|
||||
if (args.length == 1) {
|
||||
String targetPlayer = args[0];
|
||||
TeleportRequestHandler.replyTpRequest(p, NameAutoCompleter.getAutoCompletedName(targetPlayer), true);
|
||||
} else {
|
||||
TeleportRequestHandler.replyTpRequest(p, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package me.william278.huskhomes2.commands;
|
||||
|
||||
import me.william278.huskhomes2.teleport.TeleportRequestHandler;
|
||||
import me.william278.huskhomes2.util.NameAutoCompleter;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@@ -8,6 +9,11 @@ public class TpDenyCommand extends CommandBase {
|
||||
|
||||
@Override
|
||||
protected void onCommand(Player p, Command command, String label, String[] args) {
|
||||
TeleportRequestHandler.replyTpRequest(p, false);
|
||||
if (args.length == 1) {
|
||||
String targetPlayer = args[0];
|
||||
TeleportRequestHandler.replyTpRequest(p, NameAutoCompleter.getAutoCompletedName(targetPlayer), false);
|
||||
} else {
|
||||
TeleportRequestHandler.replyTpRequest(p, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@ import org.bukkit.entity.Player;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.StringJoiner;
|
||||
import java.util.logging.Level;
|
||||
|
||||
@@ -41,7 +42,13 @@ public class CrossServerMessageHandler {
|
||||
case TPA_REQUEST -> {
|
||||
if (!VanishChecker.isVanished(recipient)) {
|
||||
if (!HuskHomes.isIgnoringTeleportRequests(recipient.getUniqueId())) {
|
||||
TeleportRequestHandler.teleportRequests.put(recipient, new TeleportRequest(receivedMessage.getMessageData(), TeleportRequest.RequestType.TPA));
|
||||
if (TeleportRequestHandler.teleportRequests.containsKey(recipient)) {
|
||||
if (TeleportRequestHandler.teleportRequests.get(recipient).containsKey(receivedMessage.getMessageData())) {
|
||||
return; // Prevent request spam
|
||||
}
|
||||
TeleportRequestHandler.teleportRequests.put(recipient, new HashMap<>());
|
||||
}
|
||||
TeleportRequestHandler.teleportRequests.get(recipient).put(receivedMessage.getMessageData(), new TeleportRequest(receivedMessage.getMessageData(), TeleportRequest.RequestType.TPA));
|
||||
MessageManager.sendMessage(recipient, "tpa_request_ask", receivedMessage.getMessageData());
|
||||
MessageManager.sendMessage(recipient, "teleport_request_options");
|
||||
}
|
||||
@@ -50,7 +57,13 @@ public class CrossServerMessageHandler {
|
||||
case TPA_HERE_REQUEST -> {
|
||||
if (!VanishChecker.isVanished(recipient)) {
|
||||
if (!HuskHomes.isIgnoringTeleportRequests(recipient.getUniqueId())) {
|
||||
TeleportRequestHandler.teleportRequests.put(recipient, new TeleportRequest(receivedMessage.getMessageData(), TeleportRequest.RequestType.TPA_HERE));
|
||||
if (TeleportRequestHandler.teleportRequests.containsKey(recipient)) {
|
||||
if (TeleportRequestHandler.teleportRequests.get(recipient).containsKey(receivedMessage.getMessageData())) {
|
||||
return; // Prevent request spam
|
||||
}
|
||||
TeleportRequestHandler.teleportRequests.put(recipient, new HashMap<>());
|
||||
}
|
||||
TeleportRequestHandler.teleportRequests.get(recipient).put(receivedMessage.getMessageData(), new TeleportRequest(receivedMessage.getMessageData(), TeleportRequest.RequestType.TPA_HERE));
|
||||
MessageManager.sendMessage(recipient, "tpahere_request_ask", receivedMessage.getMessageData());
|
||||
MessageManager.sendMessage(recipient, "teleport_request_options");
|
||||
}
|
||||
|
||||
@@ -8,13 +8,14 @@ public class TeleportRequest {
|
||||
|
||||
private final String senderName;
|
||||
private final long requestExpiryTime;
|
||||
private final long requestTime;
|
||||
private final RequestType requestType;
|
||||
|
||||
public TeleportRequest(String senderName, RequestType requestType) {
|
||||
this.senderName = senderName;
|
||||
this.requestType = requestType;
|
||||
long currentUnixTime = Instant.now().getEpochSecond();
|
||||
requestExpiryTime = currentUnixTime + HuskHomes.getSettings().getTeleportRequestExpiryTime();
|
||||
this.requestTime = Instant.now().getEpochSecond();
|
||||
requestExpiryTime = requestTime + HuskHomes.getSettings().getTeleportRequestExpiryTime();
|
||||
}
|
||||
|
||||
public String getSenderName() {
|
||||
@@ -25,6 +26,8 @@ public class TeleportRequest {
|
||||
return requestType;
|
||||
}
|
||||
|
||||
public long getRequestTime() { return requestTime; }
|
||||
|
||||
public boolean isExpired() {
|
||||
long currentUnixTime = Instant.now().getEpochSecond();
|
||||
return currentUnixTime > requestExpiryTime;
|
||||
|
||||
@@ -11,15 +11,14 @@ import org.bukkit.entity.Player;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class TeleportRequestHandler {
|
||||
|
||||
private static final HuskHomes plugin = HuskHomes.getInstance();
|
||||
|
||||
// Target player and teleport request to them hashmap
|
||||
public static Map<Player, TeleportRequest> teleportRequests = new HashMap<>();
|
||||
// Target player and teleport request(s) to them hashmap
|
||||
public static HashMap<Player, HashMap<String,TeleportRequest>> teleportRequests = new HashMap<>();
|
||||
|
||||
private static void sendTeleportRequestCrossServer(Player requester, String targetPlayerName, TeleportRequest.RequestType requestType) {
|
||||
if (requestType == TeleportRequest.RequestType.TPA) {
|
||||
@@ -44,7 +43,11 @@ public class TeleportRequestHandler {
|
||||
if (!VanishChecker.isVanished(targetPlayer)) {
|
||||
MessageManager.sendMessage(requester, "tpa_request_sent", targetPlayerName);
|
||||
if (!HuskHomes.isIgnoringTeleportRequests(targetPlayer.getUniqueId())) {
|
||||
teleportRequests.put(targetPlayer, new TeleportRequest(requester.getName(), TeleportRequest.RequestType.TPA));
|
||||
if (teleportRequests.containsKey(targetPlayer)) {
|
||||
teleportRequests.put(targetPlayer, new HashMap<>());
|
||||
}
|
||||
teleportRequests.get(targetPlayer).put(requester.getName(), new TeleportRequest(requester.getName(),
|
||||
TeleportRequest.RequestType.TPA));
|
||||
MessageManager.sendMessage(targetPlayer, "tpa_request_ask", requester.getName());
|
||||
MessageManager.sendMessage(targetPlayer, "teleport_request_options");
|
||||
}
|
||||
@@ -70,7 +73,11 @@ public class TeleportRequestHandler {
|
||||
if (targetPlayer.getUniqueId() != requester.getUniqueId()) {
|
||||
MessageManager.sendMessage(requester, "tpahere_request_sent", targetPlayerName);
|
||||
if (!HuskHomes.isIgnoringTeleportRequests(targetPlayer.getUniqueId())) {
|
||||
teleportRequests.put(targetPlayer, new TeleportRequest(requester.getName(), TeleportRequest.RequestType.TPA_HERE));
|
||||
if (teleportRequests.containsKey(targetPlayer)) {
|
||||
teleportRequests.put(targetPlayer, new HashMap<>());
|
||||
}
|
||||
teleportRequests.get(targetPlayer).put(requester.getName(), new TeleportRequest(requester.getName(),
|
||||
TeleportRequest.RequestType.TPA_HERE));
|
||||
MessageManager.sendMessage(targetPlayer, "tpahere_request_ask", requester.getName());
|
||||
MessageManager.sendMessage(targetPlayer, "teleport_request_options");
|
||||
}
|
||||
@@ -87,18 +94,12 @@ public class TeleportRequestHandler {
|
||||
}
|
||||
}
|
||||
|
||||
public static void replyTpRequest(Player p, boolean accepted) {
|
||||
if (!teleportRequests.containsKey(p)) {
|
||||
MessageManager.sendMessage(p, "error_tpa_no_pending_request");
|
||||
return;
|
||||
}
|
||||
if (HuskHomes.isIgnoringTeleportRequests(p.getUniqueId())) {
|
||||
MessageManager.sendMessage(p, "error_ignoring_teleport_requests");
|
||||
return;
|
||||
}
|
||||
TeleportRequest teleportRequest = teleportRequests.get(p);
|
||||
private static void handleRequestReply(Player p, TeleportRequest teleportRequest, boolean accepted) {
|
||||
if (teleportRequest.isExpired()) {
|
||||
teleportRequests.remove(p);
|
||||
teleportRequests.get(p).remove(teleportRequest.getSenderName());
|
||||
if (teleportRequests.get(p).isEmpty()) {
|
||||
teleportRequests.remove(p);
|
||||
}
|
||||
MessageManager.sendMessage(p, "error_tp_request_expired");
|
||||
return;
|
||||
}
|
||||
@@ -138,10 +139,72 @@ public class TeleportRequestHandler {
|
||||
MessageManager.sendMessage(p, "error_player_not_found", requesterName);
|
||||
}
|
||||
}
|
||||
teleportRequests.remove(p);
|
||||
teleportRequests.get(p).remove(teleportRequest.getSenderName());
|
||||
if (teleportRequests.get(p).isEmpty()) {
|
||||
teleportRequests.remove(p);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
plugin.getLogger().log(Level.SEVERE, "An SQL exception occurred!", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static void replyTpRequest(Player p, boolean accepted) {
|
||||
if (!teleportRequests.containsKey(p)) {
|
||||
MessageManager.sendMessage(p, "error_tpa_no_pending_request");
|
||||
return;
|
||||
}
|
||||
if (teleportRequests.get(p).isEmpty()) {
|
||||
MessageManager.sendMessage(p, "error_tpa_no_pending_request");
|
||||
return;
|
||||
}
|
||||
if (HuskHomes.isIgnoringTeleportRequests(p.getUniqueId())) {
|
||||
MessageManager.sendMessage(p, "error_ignoring_teleport_requests");
|
||||
return;
|
||||
}
|
||||
HashMap<String,TeleportRequest> playerRequests = teleportRequests.get(p);
|
||||
long firstRequestTime = Long.MAX_VALUE;
|
||||
TeleportRequest targetRequest = null;
|
||||
for (String playerName : playerRequests.keySet()) {
|
||||
TeleportRequest request = playerRequests.get(playerName);
|
||||
if (request.getRequestTime() < firstRequestTime) {
|
||||
targetRequest = request;
|
||||
firstRequestTime = targetRequest.getRequestTime();
|
||||
}
|
||||
}
|
||||
if (targetRequest == null) {
|
||||
MessageManager.sendMessage(p, "error_tpa_no_pending_request");
|
||||
return;
|
||||
}
|
||||
handleRequestReply(p, targetRequest, accepted);
|
||||
}
|
||||
|
||||
public static void replyTpRequest(Player p, String requesterName, boolean accepted) {
|
||||
if (!teleportRequests.containsKey(p)) {
|
||||
MessageManager.sendMessage(p, "error_tpa_no_pending_request");
|
||||
return;
|
||||
}
|
||||
if (teleportRequests.get(p).isEmpty()) {
|
||||
MessageManager.sendMessage(p, "error_tpa_no_pending_request");
|
||||
return;
|
||||
}
|
||||
if (HuskHomes.isIgnoringTeleportRequests(p.getUniqueId())) {
|
||||
MessageManager.sendMessage(p, "error_ignoring_teleport_requests");
|
||||
return;
|
||||
}
|
||||
HashMap<String,TeleportRequest> playerRequests = teleportRequests.get(p);
|
||||
TeleportRequest targetRequest = null;
|
||||
for (String playerName : playerRequests.keySet()) {
|
||||
TeleportRequest request = playerRequests.get(playerName);
|
||||
if (request.getSenderName().equalsIgnoreCase(requesterName)) {
|
||||
targetRequest = request;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (targetRequest == null) {
|
||||
MessageManager.sendMessage(p, "error_invalid_player");
|
||||
return;
|
||||
}
|
||||
handleRequestReply(p, targetRequest, accepted);
|
||||
}
|
||||
}
|
||||
@@ -71,7 +71,7 @@ error_tpa_no_pending_request: '[Error:](#ff3300) [You do not have a pending tele
|
||||
error_tpa_request_expired: '[Error:](#ff3300) [Your pending teleport request has expired.](#ff7e5e)'
|
||||
error_player_not_found: '[Error:](#ff3300) [Could not find the player %1%.](#ff7e5e)'
|
||||
return_by_death: '[Return to where you died?](gray) [/back](#00fb9a show_text=�fb9a&Click to return to where you died run_command=/back)'
|
||||
teleport_request_options: '[Click an option:](gray) [[Accept]](#00fb9a show_text=�fb9a&Accept the teleport request; /tpaccept run_command=/tpaccept) [•](gray) [[Decline]](#ff7e5e show_text=&#ff7e5e&Decline the teleport request; /tpdeny run_command=/tpdeny)'
|
||||
teleport_request_options: '[Click an option:](gray) [[Accept]](#00fb9a show_text=�fb9a&Accept the teleport request; /tpaccept %1% run_command=/tpaccept %1%) [•](gray) [[Decline]](#ff7e5e show_text=&#ff7e5e&Decline the teleport request; /tpdeny %1% run_command=/tpdeny %1%)'
|
||||
tpa_you_accepted: '[You accepted %1%''s teleport request](#00fb9a)'
|
||||
tpa_you_declined: '[You declined %1%''s teleport request](#ff7e5e)'
|
||||
tpa_has_accepted: '[%1% accepted your teleport request](#00fb9a)'
|
||||
|
||||
@@ -69,7 +69,7 @@ error_tpa_no_pending_request: '[Errore:](#ff3300) [Non hai una richiesta di tele
|
||||
error_tpa_request_expired: '[Errore:](#ff3300) [La tua richiesta di teletrasporto è scaduta.](#ff7e5e)'
|
||||
error_player_not_found: '[Errore:](#ff3300) [Non trovo il giocatore: %1%.](#ff7e5e)'
|
||||
return_by_death: '[Torna dove sei morto.](gray) [/back](#00fb9a show_text=�fb9a&Clicca per tornare al tuo punto di morte run_command=/back)'
|
||||
teleport_request_options: '[Scegli se:](gray) [[Accettare]](#00fb9a show_text=�fb9a&Accetta la richiesta; /tpaccept run_command=/tpaccept) [•](gray) [[Rifiutare]](#ff7e5e show_text=&#ff7e5e&Declina la richiesta; /tpdeny run_command=/tpdeny)'
|
||||
teleport_request_options: '[Scegli se:](gray) [[Accettare]](#00fb9a show_text=�fb9a&Accetta la richiesta; /tpaccept %1% run_command=/tpaccept %1%) [•](gray) [[Rifiutare]](#ff7e5e show_text=&#ff7e5e&Declina la richiesta; /tpdeny %1% run_command=/tpdeny %1%)'
|
||||
tpa_you_accepted: '[Hai accettato la richiesta di teletrasporto di %1%](#00fb9a)'
|
||||
tpa_you_declined: '[Hai rifiutato la richiesta di teletrasporto di %1%](#ff7e5e)'
|
||||
tpa_has_accepted: '[%1% ha accettato la tua richiesta di teletrasporto.](#00fb9a)'
|
||||
|
||||
@@ -69,7 +69,7 @@ error_tpa_no_pending_request: '[error:](#ff3300) [Nie masz oczekujących próśb
|
||||
error_tpa_request_expired: '[error:](#ff3300) [Twoja prośba o teleporacje wygasła.](#ff7e5e)'
|
||||
error_player_not_found: '[error:](#ff3300) [Nie odnalazłem gracza %1%.](#ff7e5e)'
|
||||
return_by_death: '[Powrót w miejsce śmierci?](gray) [/back](#00fb9a show_text=�fb9a&Kliknij aby wrócić do ostatniej pozycji smierci. run_command=/back)'
|
||||
teleport_request_options: '[Wybierz opcję:](gray) [[Zaakceptuj]](#00fb9a show_text=�fb9a&Zaakceptuj prośbę o teleporacje; /tpaccept run_command=/tpaccept) [•](gray) [[Decline]](#ff7e5e show_text=&#ff7e5e&Odrzuć prośbę o teleportacje; /tpdeny run_command=/tpdeny)'
|
||||
teleport_request_options: '[Wybierz opcję:](gray) [[Zaakceptuj]](#00fb9a show_text=�fb9a&Zaakceptuj prośbę o teleporacje; /tpaccept %1% run_command=/tpaccept %1%) [•](gray) [[Decline]](#ff7e5e show_text=&#ff7e5e&Odrzuć prośbę o teleportacje; /tpdeny %1% run_command=/tpdeny %1%)'
|
||||
tpa_you_accepted: '[Zaakceptowałeś prośbę o teleportacje od %1%](#00fb9a)'
|
||||
tpa_you_declined: '[Odrzuciłeś prośbę o teleporacje od %1%''](#ff7e5e)'
|
||||
tpa_has_accepted: '[%1% zaakceptował twoją prośbę o teleportacje](#00fb9a)'
|
||||
|
||||
@@ -69,7 +69,7 @@ error_tpa_no_pending_request: '[错误:](#ff3300) [你没有需要处理的传
|
||||
error_tpa_request_expired: '[错误:](#ff3300) [你的传送请求已经过期.](#ff7e5e)'
|
||||
error_player_not_found: '[错误:](#ff3300) [没有找到玩家 %1%.](#ff7e5e)'
|
||||
return_by_death: '[回到上一个死亡点?](gray) [/back](#00fb9a show_text=�fb9a&点击来回到上一次死亡的地点 run_command=/back)'
|
||||
teleport_request_options: '[传送请求:](gray) [[同意传送]](#00fb9a show_text=�fb9a&同意这次传送; /tpaccept run_command=/tpaccept) [•](gray) [[残忍拒绝]](#ff7e5e show_text=&#ff7e5e&拒绝本次传送; /tpdeny run_command=/tpdeny)'
|
||||
teleport_request_options: '[传送请求:](gray) [[同意传送]](#00fb9a show_text=�fb9a&同意这次传送; /tpaccept %1% run_command=/tpaccept %1%) [•](gray) [[残忍拒绝]](#ff7e5e show_text=&#ff7e5e&拒绝本次传送; /tpdeny %1% run_command=/tpdeny %1%)'
|
||||
tpa_you_accepted: '[你接受了 %1% 的传送请求](#00fb9a)'
|
||||
tpa_you_declined: '[你拒绝了 %1% 的传送请求](#ff7e5e)'
|
||||
tpa_has_accepted: '[%1% 接受了你的传送请求](#00fb9a)'
|
||||
|
||||
@@ -68,7 +68,7 @@ error_tpa_no_pending_request: '[錯誤:](#ff3300) [你沒有需要處理的傳
|
||||
error_tpa_request_expired: '[錯誤:](#ff3300) [你的傳送請求已經過期.](#ff7e5e)'
|
||||
error_player_not_found: '[錯誤:](#ff3300) [沒有找到玩家 %1%.](#ff7e5e)'
|
||||
return_by_death: '[回到上一個死亡點?](gray) [/back](#00fb9a show_text=�fb9a&點擊來回到上一次死亡的地點 run_command=/back)'
|
||||
teleport_request_options: '[傳送請求:](gray) [[同意傳送]](#00fb9a show_text=�fb9a&同意這次傳送 request; /tpaccept run_command=/tpaccept) [•](gray) [[殘忍拒絕]](#ff7e5e show_text=&#ff7e5e&拒絕本次傳送 request; /tpdeny run_command=/tpdeny)'
|
||||
teleport_request_options: '[傳送請求:](gray) [[同意傳送]](#00fb9a show_text=�fb9a&同意這次傳送 request; /tpaccept %1% run_command=/tpaccept %1%) [•](gray) [[殘忍拒絕]](#ff7e5e show_text=&#ff7e5e&拒絕本次傳送 request; /tpdeny %1% run_command=/tpdeny %1%)'
|
||||
tpa_you_accepted: '[你接受了 %1% 的傳送請求](#00fb9a)'
|
||||
tpa_you_declined: '[你拒絕了 %1% 的傳送請求](#ff7e5e)'
|
||||
tpa_has_accepted: '[%1% 接受了你的傳送請求](#00fb9a)'
|
||||
|
||||
Reference in new issue
Block a user