mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-02 22:17:17 +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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user