Skip to content

Commit

Permalink
Add tpahere command
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCurle committed Dec 27, 2023
1 parent 3394186 commit 1970730
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ public static void registerCommands(RegisterCommandsEvent event) {
)
);

event.getDispatcher().register(
literal("tpahere")
.then(Commands.argument("user", EntityArgument.player())
.executes(CmdTeleportRequest::tpahere)
)
);

event.getDispatcher().register(
literal("spawn")
.executes((s) -> CmdSpawn.execute(s.getSource()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,43 @@ public static int tpa(CommandContext<CommandSourceStack> pSource) throws Command
target.sendSystemMessage(Component.translatable(Language.getInstance()
.getOrDefault("bareessentials.tpa.incoming"), sender.getDisplayName().getString()));

PendingTeleports.PENDING.add(new PendingTeleports.TeleportRequest(sender, target, true));
PendingTeleports.PENDING.add(new PendingTeleports.TeleportRequest(sender, target, true, false));

sender.sendSystemMessage(Component.translatable(Language.getInstance()
.getOrDefault("bareessentials.tpa.sent"), target.getDisplayName().getString()));
}
return Command.SINGLE_SUCCESS;
}

public static int tpahere(CommandContext<CommandSourceStack> pSource) throws CommandSyntaxException {
var target = EntityArgument.getPlayer(pSource, "user");
var sender = pSource.getSource().getPlayer();


Cooldowns cd = Cooldowns.getOrCreate(sender.serverLevel());
Bank bk = Bank.getOrCreate(sender.serverLevel());

if (!cd.isCooldownExpired(sender, "tpa")) {
sender.sendSystemMessage(Component.translatable(Language.getInstance().getOrDefault("bareessentials.cooldown.active"), cd.getRemainingTimeFor(sender, "tpa")/20));
} else {
if (!bk.chargePlayer(sender, sender.level().getGameRules().getInt(BareEssentials.TPA_COST)))
return 0;
cd.setCooldownFor(sender, "tpa", sender.level().getGameTime() + sender.level().getGameRules().getInt(BareEssentials.TPA_COOLDOWN));

logger.info("New Teleport Request; {} wants {} to teleport to them.", sender.getDisplayName().getString(), target.getDisplayName().getString());

if (PendingTeleports.getRequestFrom(sender) != null) {
logger.info("Player {} already has a teleport request, not considering..", sender.getDisplayName().getString());
sender.sendSystemMessage(Component.translatable(Language.getInstance()
.getOrDefault("bareessentials.tpa.toomanyrequests")));
return 0;
}

logger.info("Request valid, saving..");
target.sendSystemMessage(Component.translatable(Language.getInstance()
.getOrDefault("bareessentials.tpahere.incoming"), sender.getDisplayName().getString()));

PendingTeleports.PENDING.add(new PendingTeleports.TeleportRequest(sender, target, true, true));

sender.sendSystemMessage(Component.translatable(Language.getInstance()
.getOrDefault("bareessentials.tpa.sent"), target.getDisplayName().getString()));
Expand All @@ -91,18 +127,34 @@ public static int accept(CommandContext<CommandSourceStack> pSource) {
return 0;
}

logger.info("Request valid, teleporting {} to {}.", request.sender().getDisplayName().getString(), request.receiver().getDisplayName().getString());
request.sender().sendSystemMessage(Component.translatable(Language.getInstance()
.getOrDefault("bareessentials.tpa.inprogress"), request.receiver().getDisplayName().getString()));
target.sendSystemMessage(Component.translatable(Language.getInstance() .getOrDefault("bareessentials.tpa.inprogress"),
Component.translatable(Language.getInstance().getOrDefault("bareessentials.targetyou"))
));

if (!request.sender().randomTeleport(target.getX(), target.getY(), target.getZ(), false)) {
request.sender().sendSystemMessage(Component.translatable(Language.getInstance().getOrDefault("bareessentials.teleport.unsafe")));
cd.setCooldownFor(target, "tpa", target.serverLevel().getGameTime() - 10);
if (request.tpahere()) {
logger.info("Request valid, teleporting {} to {}.", request.receiver().getDisplayName().getString(), request.sender().getDisplayName().getString());
request.receiver().sendSystemMessage(Component.translatable(Language.getInstance()
.getOrDefault("bareessentials.tpa.inprogress"), request.sender().getDisplayName().getString()));
request.sender().sendSystemMessage(Component.translatable(Language.getInstance() .getOrDefault("bareessentials.tpa.inprogress"),
Component.translatable(Language.getInstance().getOrDefault("bareessentials.targetyou"))
));

if (!request.receiver().randomTeleport(request.sender().getX(), request.sender().getY(), request.sender().getZ(), false)) {
request.sender().sendSystemMessage(Component.translatable(Language.getInstance().getOrDefault("bareessentials.teleport.unsafe")));
request.receiver().sendSystemMessage(Component.translatable(Language.getInstance().getOrDefault("bareessentials.teleport.unsafe")));
cd.setCooldownFor(target, "tpa", target.serverLevel().getGameTime() - 10);
}
} else {
logger.info("Request valid, teleporting {} to {}.", request.sender().getDisplayName().getString(), request.receiver().getDisplayName().getString());
request.sender().sendSystemMessage(Component.translatable(Language.getInstance()
.getOrDefault("bareessentials.tpa.inprogress"), request.receiver().getDisplayName().getString()));
target.sendSystemMessage(Component.translatable(Language.getInstance() .getOrDefault("bareessentials.tpa.inprogress"),
Component.translatable(Language.getInstance().getOrDefault("bareessentials.targetyou"))
));

if (!request.sender().randomTeleport(target.getX(), target.getY(), target.getZ(), false)) {
request.sender().sendSystemMessage(Component.translatable(Language.getInstance().getOrDefault("bareessentials.teleport.unsafe")));
cd.setCooldownFor(target, "tpa", target.serverLevel().getGameTime() - 10);
}
}


PendingTeleports.removeRequestFrom(request.sender());

return Command.SINGLE_SUCCESS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
public class PendingTeleports {
private static Logger logger = LogManager.getLogger();

public record TeleportRequest(ServerPlayer sender, ServerPlayer receiver, boolean pending) {
public record TeleportRequest(ServerPlayer sender, ServerPlayer receiver, boolean pending, boolean tpahere) {
}

public static final List<TeleportRequest> PENDING = new ArrayList<>();
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/bareessentials/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"bareessentials.tpa.toomanyrequests": "You cannot send more than one teleport request at a time!",
"bareessentials.tpa.incoming": "%s wants to teleport to you.",
"bareessentials.tpahere.incoming": "%s wants you to teleport to their location.",
"bareessentials.tpa.sent": "Teleport request sent to %s.",
"bareessentials.tpa.norequests": "You have no pending teleport requests!",
"bareessentials.tpa.inprogress": "Teleport request accepted, teleporting to %s",
Expand Down

0 comments on commit 1970730

Please sign in to comment.