forked from John-Paul-R/Essential-Commands
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTeleportAskHereCommand.java
More file actions
66 lines (54 loc) · 2.84 KB
/
Copy pathTeleportAskHereCommand.java
File metadata and controls
66 lines (54 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.fibermc.essentialcommands.commands;
import com.fibermc.essentialcommands.ManagerLocator;
import com.fibermc.essentialcommands.playerdata.PlayerData;
import com.fibermc.essentialcommands.teleportation.TeleportManager;
import com.fibermc.essentialcommands.teleportation.TeleportRequest;
import com.fibermc.essentialcommands.text.ChatConfirmationPrompt;
import com.fibermc.essentialcommands.text.ECText;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.server.level.ServerPlayer;
public class TeleportAskHereCommand implements Command<CommandSourceStack> {
public TeleportAskHereCommand() {}
@Override
public int run(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
TeleportManager tpMgr = ManagerLocator.getInstance().getTpManager();
ServerPlayer senderPlayer = context.getSource().getPlayerOrException();
ServerPlayer targetPlayer = NicknameTargetResolver.getPlayer(context, "target_player");
var senderPlayerData = PlayerData.access(senderPlayer);
var targetPlayerData = PlayerData.access(targetPlayer);
// Don't allow spamming same target.
{
var existingTeleportRequest = senderPlayerData.getSentTeleportRequests()
.getRequestToPlayer(targetPlayerData);
if (existingTeleportRequest.isPresent()) {
PlayerData.access(senderPlayer).sendCommandError(
"cmd.tpask.error.exists",
existingTeleportRequest.get().getTargetPlayer().getDisplayName());
return 0;
}
}
//inform target player of tp request via chat
var targetPlayerEcText = ECText.access(targetPlayer);
targetPlayerData.sendMessage(
"cmd.tpaskhere.receive",
targetPlayerEcText.accent(senderPlayer.getScoreboardName())
);
String senderName = senderPlayer.getGameProfile().name();
new ChatConfirmationPrompt(
targetPlayer,
"/tpaccept " + senderName,
"/tpdeny " + senderName,
targetPlayerEcText.accent("[" + ECText.getInstance().getString("generic.accept") + "]"),
targetPlayerEcText.error("[" + ECText.getInstance().getString("generic.deny") + "]")
).send();
//Mark TPRequest Sender as having requested a teleport
tpMgr.startTpRequest(senderPlayer, targetPlayer, TeleportRequest.Type.TPA_HERE);
//inform command sender that request has been sent
var targetPlayerText = ECText.access(senderPlayer).accent(targetPlayer.getScoreboardName());
senderPlayerData.sendCommandFeedback("cmd.tpask.send", targetPlayerText);
return SINGLE_SUCCESS;
}
}