Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ public enum Message {
COMMAND_TELEPORT_WORLD("<error>You need to be in the same world to teleport."),
COMMAND_TPA_ERROR_TO_LATE("<error>You do not have a teleport request."),
COMMAND_TPA_ERROR_TO_LATE_2("<error>The request has expired."),
COMMAND_TPA_ERROR_NO_REQUEST_FROM_PLAYER("<error>You do not have a teleport request from &f%player%<error>."),
COMMAND_TPA_ACCEPT_ALL_SUCCESS("<success>You have accepted &f%count% <success>teleport request(s)."),
COMMAND_TPA_DENY_ALL_SUCCESS("<success>You have denied &f%count% <success>teleport request(s)."),
COMMAND_TP_DENY_SENDER("Denied %player% teleport request."),
COMMAND_TP_DENY_RECEIVER("%player% has denied your teleport request"),
COMMAND_TP_CANCEL_ERROR("<error>You did not send a teleport request at &f%player%<error>."),
Expand Down
29 changes: 29 additions & 0 deletions API/src/main/java/fr/maxlego08/essentials/api/user/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,35 @@ public interface User {
*/
void removeTeleportRequest(User user);

/**
* Gets teleport request from specific player.
*
* @param fromUser The player who sent the request
* @return The request from specified player, or null if none exists
*/
TeleportRequest getTeleportRequestFrom(User fromUser);

/**
* Gets all incoming teleport requests.
*
* @return Collection of all valid incoming requests
*/
Collection<TeleportRequest> getIncomingTeleportRequests();

/**
* Adds or updates incoming teleport request.
*
* @param teleportRequest The request to add/update
*/
void setIncomingTeleportRequest(TeleportRequest teleportRequest);

/**
* Removes incoming request from specific player.
*
* @param fromUser The player whose request should be removed
*/
void removeIncomingTeleportRequest(User fromUser);

/**
* Teleports the user to the specified location immediately.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@
import fr.maxlego08.essentials.api.commands.Permission;
import fr.maxlego08.essentials.api.messages.Message;
import fr.maxlego08.essentials.api.user.TeleportRequest;
import fr.maxlego08.essentials.api.user.User;
import fr.maxlego08.essentials.module.modules.TeleportationModule;
import fr.maxlego08.essentials.zutils.utils.commands.VCommand;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.Collection;

public class CommandTeleportAccept extends VCommand {

Expand All @@ -15,26 +21,90 @@ public CommandTeleportAccept(EssentialsPlugin plugin) {
this.setModule(TeleportationModule.class);
this.setPermission(Permission.ESSENTIALS_TPA_ACCEPT);
this.setDescription(Message.DESCRIPTION_TPA_ACCEPT);
this.addOptionalArg("player");
this.onlyPlayers();
}

@Override
protected CommandResultType perform(EssentialsPlugin plugin) {

TeleportRequest teleportRequest = this.user.getTeleportRequest();
if (teleportRequest == null) {
message(sender, Message.COMMAND_TPA_ERROR_TO_LATE);
return CommandResultType.DEFAULT;
}
String arg = this.argAsString(0, null);

if (!teleportRequest.isValid()) {
this.user.setTeleportRequest(null);
message(sender, Message.COMMAND_TPA_ERROR_TO_LATE_2);
return CommandResultType.DEFAULT;
}
if (arg == null) {
// accept latest request
TeleportRequest teleportRequest = this.user.getTeleportRequest();
if (teleportRequest == null) {
message(sender, Message.COMMAND_TPA_ERROR_TO_LATE);
return CommandResultType.DEFAULT;
}

if (!teleportRequest.isValid()) {
this.user.removeIncomingTeleportRequest(teleportRequest.getFromUser());
message(sender, Message.COMMAND_TPA_ERROR_TO_LATE_2);
return CommandResultType.DEFAULT;
}

teleportRequest.accept();
this.user.removeIncomingTeleportRequest(teleportRequest.getFromUser());

} else if (arg.equals("*")) {
// accept all pending requests
Collection<TeleportRequest> requests = this.user.getIncomingTeleportRequests();
if (requests.isEmpty()) {
message(sender, Message.COMMAND_TPA_ERROR_TO_LATE);
return CommandResultType.DEFAULT;
}

int count = 0;
for (TeleportRequest request : new ArrayList<>(requests)) {
if (request.isValid()) {
request.accept();
this.user.removeIncomingTeleportRequest(request.getFromUser());
count++;
}
}

teleportRequest.accept();
this.user.setTeleportRequest(null);
if (count > 0) {
message(sender, Message.COMMAND_TPA_ACCEPT_ALL_SUCCESS, "%count%", count);
} else {
message(sender, Message.COMMAND_TPA_ERROR_TO_LATE);
return CommandResultType.DEFAULT;
}

} else {
// accept request from that player
Player targetPlayer = this.argAsPlayer(0);
User targetUser = null;

if (targetPlayer != null) {
targetUser = plugin.getStorageManager().getStorage().getUser(targetPlayer.getUniqueId());
} else {
OfflinePlayer offlinePlayer = this.argAsOfflinePlayer(0);
if (offlinePlayer != null && offlinePlayer.hasPlayedBefore()) {
targetUser = plugin.getStorageManager().getStorage().getUser(offlinePlayer.getUniqueId());
}
}

if (targetUser == null) {
message(sender, Message.PLAYER_NOT_FOUND, "%player%", arg);
return CommandResultType.DEFAULT;
}

TeleportRequest teleportRequest = this.user.getTeleportRequestFrom(targetUser);
if (teleportRequest == null) {
message(sender, Message.COMMAND_TPA_ERROR_NO_REQUEST_FROM_PLAYER, "%player%", targetUser.getName());
return CommandResultType.DEFAULT;
}

if (!teleportRequest.isValid()) {
this.user.removeIncomingTeleportRequest(targetUser);
message(sender, Message.COMMAND_TPA_ERROR_TO_LATE_2);
return CommandResultType.DEFAULT;
}

teleportRequest.accept();
this.user.removeIncomingTeleportRequest(targetUser);
}

return CommandResultType.SUCCESS;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@
import fr.maxlego08.essentials.api.commands.Permission;
import fr.maxlego08.essentials.api.messages.Message;
import fr.maxlego08.essentials.api.user.TeleportRequest;
import fr.maxlego08.essentials.api.user.User;
import fr.maxlego08.essentials.module.modules.TeleportationModule;
import fr.maxlego08.essentials.zutils.utils.commands.VCommand;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.Collection;

public class CommandTeleportDeny extends VCommand {

Expand All @@ -15,26 +21,90 @@ public CommandTeleportDeny(EssentialsPlugin plugin) {
this.setModule(TeleportationModule.class);
this.setPermission(Permission.ESSENTIALS_TPA_DENY);
this.setDescription(Message.DESCRIPTION_TPA_DENY);
this.addOptionalArg("player");
this.onlyPlayers();
}

@Override
protected CommandResultType perform(EssentialsPlugin plugin) {

TeleportRequest teleportRequest = this.user.getTeleportRequest();
if (teleportRequest == null) {
message(sender, Message.COMMAND_TPA_ERROR_TO_LATE);
return CommandResultType.DEFAULT;
}
String arg = this.argAsString(0, null);

if (!teleportRequest.isValid()) {
this.user.setTeleportRequest(null);
message(sender, Message.COMMAND_TPA_ERROR_TO_LATE_2);
return CommandResultType.DEFAULT;
}
if (arg == null) {
// deny latest request
TeleportRequest teleportRequest = this.user.getTeleportRequest();
if (teleportRequest == null) {
message(sender, Message.COMMAND_TPA_ERROR_TO_LATE);
return CommandResultType.DEFAULT;
}

if (!teleportRequest.isValid()) {
this.user.removeIncomingTeleportRequest(teleportRequest.getFromUser());
message(sender, Message.COMMAND_TPA_ERROR_TO_LATE_2);
return CommandResultType.DEFAULT;
}

teleportRequest.deny();
this.user.removeIncomingTeleportRequest(teleportRequest.getFromUser());

} else if (arg.equals("*")) {
// deny all pending requests
Collection<TeleportRequest> requests = this.user.getIncomingTeleportRequests();
if (requests.isEmpty()) {
message(sender, Message.COMMAND_TPA_ERROR_TO_LATE);
return CommandResultType.DEFAULT;
}

int count = 0;
for (TeleportRequest request : new ArrayList<>(requests)) {
if (request.isValid()) {
request.deny();
this.user.removeIncomingTeleportRequest(request.getFromUser());
count++;
}
}

teleportRequest.deny();
this.user.setTeleportRequest(null);
if (count > 0) {
message(sender, Message.COMMAND_TPA_DENY_ALL_SUCCESS, "%count%", count);
} else {
message(sender, Message.COMMAND_TPA_ERROR_TO_LATE);
return CommandResultType.DEFAULT;
}

} else {
// deny request from that player
Player targetPlayer = this.argAsPlayer(0);
User targetUser = null;

if (targetPlayer != null) {
targetUser = plugin.getStorageManager().getStorage().getUser(targetPlayer.getUniqueId());
} else {
OfflinePlayer offlinePlayer = this.argAsOfflinePlayer(0);
if (offlinePlayer != null && offlinePlayer.hasPlayedBefore()) {
targetUser = plugin.getStorageManager().getStorage().getUser(offlinePlayer.getUniqueId());
}
}

if (targetUser == null) {
message(sender, Message.PLAYER_NOT_FOUND, "%player%", arg);
return CommandResultType.DEFAULT;
}

TeleportRequest teleportRequest = this.user.getTeleportRequestFrom(targetUser);
if (teleportRequest == null) {
message(sender, Message.COMMAND_TPA_ERROR_NO_REQUEST_FROM_PLAYER, "%player%", targetUser.getName());
return CommandResultType.DEFAULT;
}

if (!teleportRequest.isValid()) {
this.user.removeIncomingTeleportRequest(targetUser);
message(sender, Message.COMMAND_TPA_ERROR_TO_LATE_2);
return CommandResultType.DEFAULT;
}

teleportRequest.deny();
this.user.removeIncomingTeleportRequest(targetUser);
}

return CommandResultType.SUCCESS;
}
Expand Down
52 changes: 49 additions & 3 deletions src/main/java/fr/maxlego08/essentials/user/ZUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class ZUser extends ZUtils implements User {

private final EssentialsPlugin plugin;
private final Map<UUID, TeleportRequest> teleports = new HashMap<>();
private final Map<UUID, TeleportRequest> incomingTeleportRequests = new LinkedHashMap<>();
private final Map<String, Long> cooldowns = new HashMap<>();
private final UUID uniqueId;
private final Map<Option, Boolean> options = new HashMap<>();
Expand All @@ -53,7 +54,6 @@ public class ZUser extends ZUtils implements User {
private final Selection selection = new ZSelection();
private WorldEditTask worldEditTask;
private String name;
private TeleportRequest teleportRequest;
private User targetUser;
private BigDecimal targetAmount;
private Economy targetEconomy;
Expand Down Expand Up @@ -251,19 +251,65 @@ public Collection<TeleportRequest> getTeleportRequests() {

@Override
public TeleportRequest getTeleportRequest() {
return teleportRequest;
this.incomingTeleportRequests.entrySet().removeIf(entry -> !entry.getValue().isValid());

TeleportRequest latestRequest = null;
for (TeleportRequest request : this.incomingTeleportRequests.values()) {
if (request.isValid()) {
latestRequest = request;
}
}
return latestRequest;
}

@Override
public void setTeleportRequest(TeleportRequest teleportRequest) {
this.teleportRequest = teleportRequest;
if (teleportRequest != null) {
this.setIncomingTeleportRequest(teleportRequest);
}
}

@Override
public void removeTeleportRequest(User user) {
this.teleports.remove(user.getUniqueId());
}

@Override
public TeleportRequest getTeleportRequestFrom(User fromUser) {
if (fromUser == null) return null;

// Cleanup expired requests
this.incomingTeleportRequests.entrySet().removeIf(entry -> !entry.getValue().isValid());

TeleportRequest request = this.incomingTeleportRequests.get(fromUser.getUniqueId());
return (request != null && request.isValid()) ? request : null;
}

@Override
public Collection<TeleportRequest> getIncomingTeleportRequests() {
this.incomingTeleportRequests.entrySet().removeIf(entry -> !entry.getValue().isValid());

return this.incomingTeleportRequests.values().stream()
.filter(TeleportRequest::isValid)
.collect(Collectors.toList());
}

@Override
public void setIncomingTeleportRequest(TeleportRequest teleportRequest) {
if (teleportRequest == null) return;

this.incomingTeleportRequests.entrySet().removeIf(entry -> !entry.getValue().isValid());

UUID fromUserId = teleportRequest.getFromUser().getUniqueId();
this.incomingTeleportRequests.put(fromUserId, teleportRequest);
}

@Override
public void removeIncomingTeleportRequest(User fromUser) {
if (fromUser == null) return;
this.incomingTeleportRequests.remove(fromUser.getUniqueId());
}

@Override
public void teleportNow(Location location) {
// ToDo, https://github.com/PaperMC/Folia/?tab=readme-ov-file#current-broken-api
Expand Down