Skip to content

GH-926 Add teleport timer to TeleportBackCommand, add teleport-here functionality. #926

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ object Versions {

const val TRIUMPH_GUI = "3.1.11"

const val LUCKPERMS = "5.4.156"

const val BSTATS = "3.1.0"

const val CAFFEINE = "3.2.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
package com.eternalcode.core.feature.teleport.command;

import com.eternalcode.annotations.scan.command.DescriptionDocs;
import com.eternalcode.commons.bukkit.position.PositionAdapter;
import com.eternalcode.core.feature.teleport.TeleportService;
import com.eternalcode.core.feature.teleport.TeleportTaskService;
import com.eternalcode.core.feature.teleportrequest.TeleportRequestSettings;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.notice.NoticeService;
import com.eternalcode.core.feature.teleport.TeleportService;
import com.eternalcode.core.viewer.Viewer;
import dev.rollczi.litecommands.annotations.argument.Arg;
import dev.rollczi.litecommands.annotations.command.Command;
import dev.rollczi.litecommands.annotations.context.Context;
import dev.rollczi.litecommands.annotations.execute.Execute;
import dev.rollczi.litecommands.annotations.permission.Permission;
import dev.rollczi.litecommands.annotations.command.Command;
import org.bukkit.Location;
import org.bukkit.entity.Player;

import java.util.Optional;

import static com.eternalcode.core.feature.teleportrequest.TeleportRequestPermissionConstant.TELEPORT_TIMER_BYPASS_PERMISSION;

@Command(name = "back")
class TeleportBackCommand {

private final TeleportService teleportService;
private final TeleportTaskService teleportTaskService;
private final TeleportRequestSettings settings;
private final NoticeService noticeService;

@Inject
TeleportBackCommand(TeleportService teleportService, NoticeService noticeService) {
TeleportBackCommand(TeleportService teleportService, TeleportTaskService teleportTaskService, TeleportRequestSettings settings, NoticeService noticeService) {
this.teleportService = teleportService;
this.teleportTaskService = teleportTaskService;
this.settings = settings;
this.noticeService = noticeService;
}

Expand All @@ -39,7 +48,12 @@ void execute(@Context Player player) {
return;
}

this.teleportService.teleport(player, location.get());
if (player.hasPermission(TELEPORT_TIMER_BYPASS_PERMISSION)) {
this.teleportService.teleport(player, location.get());
} else {
this.teleportTaskService.createTeleport(player.getUniqueId(), PositionAdapter.convert(player.getLocation()), PositionAdapter.convert(location.get()), this.settings.teleportTime());
}

this.noticeService.player(player.getUniqueId(), translation -> translation.teleport().teleportedToLastLocation());
}

Expand All @@ -55,7 +69,11 @@ void execute(@Context Viewer viewer, @Arg Player player) {
return;
}

this.teleportService.teleport(player, location.get());
if (player.hasPermission(TELEPORT_TIMER_BYPASS_PERMISSION)){
this.teleportService.teleport(player, location.get());
} else {
this.teleportTaskService.createTeleport(player.getUniqueId(), PositionAdapter.convert(player.getLocation()), PositionAdapter.convert(location.get()), this.settings.teleportTime());
}

this.noticeService.player(player.getUniqueId(), translation -> translation.teleport().teleportedToLastLocation());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.eternalcode.core.feature.teleportrequest;

public class TeleportRequestPermissionConstant {

public static final String TELEPORT_REQUEST_PERMISSION = "eternalcore.tpa";
public static final String TELEPORT_REQUEST_ACCEPT_PERMISSION = "eternalcore.tpaccept";
public static final String TELEPORT_REQUEST_DENY_PERMISSION = "eternalcore.tpdeny";
public static final String TELEPORT_REQUEST_HERE_PERMISSION = "eternalcore.tpahere";
public static final String TELEPORT_REQUEST_HERE_ACCEPT_PERMISSION = "eternalcore.tpahere.accept";
public static final String TELEPORT_REQUEST_HERE_DENY_PERMISSION = "eternalcore.tpahere.deny";

public static final String TELEPORT_TIMER_BYPASS_PERMISSION = "eternalcore.teleport.bypass";

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@ public class ENTeleportRequestMessages implements TeleportRequestMessages {
public Notice tpaAlreadySentMessage =
Notice.chat("<red>✘ <dark_red>You have already sent a teleportation request!");
public Notice tpaSentMessage =
Notice.chat("<green>► <white>You have sent a request for teleportation to a player: <green>{PLAYER}<white>!");
Notice.chat("<green>► <white>You have sent a request for player <green>{PLAYER}<white>{PLAYER} to teleport to you!");

public Notice tpaHereSentMessage = Notice.chat("<green>► <white>You have sent a request for teleportation to you for a player: <green>{PLAYER}<white>!");
public Notice tpaHereReceivedMessage = Notice.builder()
.chat("<green>► <white>You have received a request for teleportation TO a player: <gray>{PLAYER}<green>!")
.chat(
"<hover:show_text:'<green>Teleport to the player?</green>'><gold><click:suggest_command:'/tpahereaccept {PLAYER}'><dark_gray>» <gold>/tpahereaccept {PLAYER} <green>to accept! <gray>(Click)</gray></click></gold></hover>")
.chat(
"<hover:show_text:'<red>Decline a teleportation request?</red>'><gold><click:suggest_command:'/tpaheredeny {PLAYER}'><dark_gray>» <gold>/tpaheredeny {PLAYER} <red><green>to deny! <gray>(Click)</gray></click></gold></hover>")
.build();

@Description({
" ",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ public class PLTeleportRequestMessages implements TeleportRequestMessages {
public Notice tpaSentMessage =
Notice.chat("<green>► <white>Wysłałeś prośbę o teleportację do gracza: <green>{PLAYER}<white>!");

public Notice tpaHereSentMessage = Notice.chat("<green>► <white>Wysłałeś prośbę o teleportację gracza <green>{PLAYER}<white> do twojej lokalizacji!");
public Notice tpaHereReceivedMessage = Notice.builder()
.chat("<green>► <white>Otrzymałeś prośbę o teleportację do gracza: <green>{PLAYER}<white>!")
.chat(
"<hover:show_text:'<green>Teleportować się do gracza?</green>'><gold><click:suggest_command:'/tpahereaccept {PLAYER}'><dark_gray>» <gold>/tpahereaccept {PLAYER} <green>by ją zaakceptować! <gray>(Kliknij)</gray></click></gold></hover>")
.chat(
"<hover:show_text:'<red>Odrzucić prośbę o teleportacje?</red>'><gold><click:suggest_command:'/tpaheredeny {PLAYER}'><dark_gray>» <gold>/tpaheredeny {PLAYER} <red>by ją odrzucić! <gray>(Kliknij)</gray></click></gold></hover>")
.build();
@Description({
" ",
"# W tych wiadomościach użyliśmy formatowania MiniMessages",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ public interface TeleportRequestMessages {
Notice tpaReceivedMessage();
Notice tpaTargetIgnoresYou();

Notice tpaHereSentMessage();
Notice tpaHereReceivedMessage();

Notice tpaDenyNoRequestMessage();
Notice tpaDenyDoneMessage();
Notice tpaDenyReceivedMessage();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.eternalcode.core.feature.teleportrequest.self;

import com.eternalcode.core.bridge.litecommand.argument.AbstractViewerArgument;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.injector.annotations.lite.LiteArgument;
import com.eternalcode.core.translation.Translation;
import com.eternalcode.core.translation.TranslationManager;
import dev.rollczi.litecommands.argument.Argument;
import dev.rollczi.litecommands.argument.parser.ParseResult;
import dev.rollczi.litecommands.invocation.Invocation;
import dev.rollczi.litecommands.suggestion.SuggestionContext;
import dev.rollczi.litecommands.suggestion.SuggestionResult;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;

import java.util.Objects;

@LiteArgument(type = Player.class, name = SelfRequesterArgument.KEY)
class SelfRequesterArgument extends AbstractViewerArgument<Player> {

static final String KEY = "selfrequester";

private final TeleportHereRequestService requestService;
private final Server server;

@Inject
SelfRequesterArgument(TeleportHereRequestService requestService, TranslationManager translationManager, Server server) {
super(translationManager);
this.requestService = requestService;
this.server = server;
}

@Override
public ParseResult<Player> parse(Invocation<CommandSender> invocation, String argument, Translation translation) {
Player target = this.server.getPlayer(argument);

if (!(invocation.sender() instanceof Player player)) {
return ParseResult.failure(translation.argument().onlyPlayer());
}

if (target == null || !this.requestService.hasRequest(target.getUniqueId(), player.getUniqueId())) {
return ParseResult.failure(translation.tpa().tpaDenyNoRequestMessage());
}

return ParseResult.success(target);
}

@Override
public SuggestionResult suggest(Invocation<CommandSender> invocation, Argument<Player> argument, SuggestionContext context) {
if (!(invocation.sender() instanceof Player player)) {
return SuggestionResult.empty();
}

return this.requestService.findRequests(player.getUniqueId()).stream()
.map(this.server::getPlayer)
.filter(Objects::nonNull)
.map(HumanEntity::getName)
.collect(SuggestionResult.collector());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.eternalcode.core.feature.teleportrequest.self;

import com.eternalcode.core.feature.teleportrequest.TeleportRequestSettings;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.injector.annotations.component.Service;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
class TeleportHereRequestService {

private final Cache<UUID, UUID> requests;

@Inject
TeleportHereRequestService(TeleportRequestSettings settings) {
this.requests = CacheBuilder
.newBuilder()
.expireAfterWrite(settings.teleportExpire())
.build();
}

void createRequest(UUID requester, UUID target) {
this.requests.put(requester, target);
}

void removeRequest(UUID requester) {
this.requests.asMap().remove(requester);
}

boolean hasRequest(UUID requester, UUID target) {
Map<UUID, UUID> map = this.requests.asMap();

for (Map.Entry<UUID, UUID> entry : map.entrySet()) {
if (entry.getKey().equals(requester) && entry.getValue().equals(target)) {
return true;
}
}

return false;
}

List<UUID> findRequests(UUID target) {
Map<UUID, UUID> map = this.requests.asMap();

List<UUID> requesters = new ArrayList<>();

for (Map.Entry<UUID, UUID> entry : map.entrySet()) {
if (entry.getValue().equals(target)) {
requesters.add(entry.getKey());
}
}

return requesters;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package com.eternalcode.core.feature.teleportrequest.self;

import com.eternalcode.annotations.scan.command.DescriptionDocs;
import com.eternalcode.commons.bukkit.position.PositionAdapter;
import com.eternalcode.core.feature.teleport.TeleportTaskService;
import com.eternalcode.core.feature.teleportrequest.TeleportRequestSettings;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.notice.NoticeService;
import dev.rollczi.litecommands.annotations.argument.Arg;
import dev.rollczi.litecommands.annotations.command.RootCommand;
import dev.rollczi.litecommands.annotations.context.Context;
import dev.rollczi.litecommands.annotations.execute.Execute;
import dev.rollczi.litecommands.annotations.permission.Permission;
import org.bukkit.Server;
import org.bukkit.entity.Player;

import java.util.List;
import java.util.UUID;

import static com.eternalcode.core.feature.teleportrequest.TeleportRequestPermissionConstant.TELEPORT_REQUEST_ACCEPT_PERMISSION;
import static com.eternalcode.core.feature.teleportrequest.TeleportRequestPermissionConstant.TELEPORT_REQUEST_HERE_DENY_PERMISSION;

@RootCommand
public class TpaHereActionCommand {

private final TeleportHereRequestService requestService;
private final TeleportTaskService teleportTaskService;
private final TeleportRequestSettings settings;
private final NoticeService noticeService;
private final Server server;

@Inject
TpaHereActionCommand(TeleportHereRequestService requestService, TeleportTaskService teleportTaskService, TeleportRequestSettings settings, NoticeService noticeService, Server server) {
this.requestService = requestService;
this.teleportTaskService = teleportTaskService;
this.settings = settings;
this.noticeService = noticeService;
this.server = server;
}

@Execute(name = "tpahereaccept")
@Permission(TELEPORT_REQUEST_ACCEPT_PERMISSION)
void accept(@Context Player player, @Arg(SelfRequesterArgument.KEY) Player target) {
this.teleportTaskService.createTeleport(
player.getUniqueId(),
PositionAdapter.convert(player.getLocation()),
PositionAdapter.convert(target.getLocation()),
this.settings.teleportTime()
);

this.requestService.removeRequest(target.getUniqueId());

this.noticeService
.create()
.player(player.getUniqueId())
.notice(translation -> translation.tpa().tpaAcceptMessage())
.placeholder("{PLAYER}", target.getName())
.send();

this.noticeService
.create()
.player(target.getUniqueId())
.notice(translation -> translation.tpa().tpaAcceptReceivedMessage())
.placeholder("{PLAYER}", player.getName())
.send();
}

@Execute(name = "tpaheredeny")
@Permission(TELEPORT_REQUEST_HERE_DENY_PERMISSION)
@DescriptionDocs(description = "Deny a teleport here request")
void executeTarget(@Context Player player, @Arg(SelfRequesterArgument.KEY) Player target) {
this.requestService.removeRequest(target.getUniqueId());

this.noticeService
.create()
.player(player.getUniqueId())
.notice(translation -> translation.tpa().tpaDenyDoneMessage())
.placeholder("{PLAYER}", target.getName())
.send();

this.noticeService
.create()
.player(target.getUniqueId())
.notice(translation -> translation.tpa().tpaDenyReceivedMessage())
.placeholder("{PLAYER}", player.getName())
.send();
}

@Execute(name = "tpaheredeny -all")
@Permission(TELEPORT_REQUEST_HERE_DENY_PERMISSION)
@DescriptionDocs(description = "Deny all teleport here requests")
void executeAll(@Context Player player) {
List<UUID> requests = this.requestService.findRequests(player.getUniqueId());

if (requests.isEmpty()) {

this.noticeService.player(player.getUniqueId(), translation -> translation.tpa().tpaDenyNoRequestMessage());

return;
}

for (UUID uniqueId : requests) {
Player requester = this.server.getPlayer(uniqueId);

this.requestService.removeRequest(uniqueId);

if (requester != null) {

this.noticeService
.create()
.player(uniqueId)
.notice(translation -> translation.tpa().tpaDenyReceivedMessage())
.placeholder("{PLAYER}", player.getName())
.send();
}
}

this.noticeService.player(player.getUniqueId(), translation -> translation.tpa().tpaDenyAllDenied());
}

}
Loading