Skip to content

Commit

Permalink
Merge pull request #78 from CanadianBaconBoi/dev
Browse files Browse the repository at this point in the history
Implement RTP minimum and maximum distance arguments
  • Loading branch information
desht authored Jan 10, 2025
2 parents 6bc0b18 + 66668fe commit e245496
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.ftb.mods.ftbessentials.commands.groups;

import com.mojang.authlib.GameProfile;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import dev.architectury.event.EventResult;
import dev.ftb.mods.ftbessentials.FTBEssentials;
import dev.ftb.mods.ftbessentials.FTBEssentialsEvents;
Expand Down Expand Up @@ -60,7 +61,17 @@ public class TeleportingCommands {

// Random teleport command
new SimpleConfigurableCommand(FTBEConfig.RTP, Commands.literal("rtp")
.executes(context -> rtp(context.getSource().getPlayerOrException()))),
.then(Commands.argument("maxDistance", IntegerArgumentType.integer(FTBEConfig.RTP_MIN_DISTANCE.get(), FTBEConfig.RTP_MAX_DISTANCE.get()))
.requires(context -> FTBEConfig.RTP_MAX_DISTANCE_CUSTOM.get(context.getPlayer()))
.executes(context -> rtp(context.getSource().getPlayerOrException(), FTBEConfig.RTP_MIN_DISTANCE.get(), IntegerArgumentType.getInteger(context, "maxDistance")))
)
.then(Commands.argument("minDistance", IntegerArgumentType.integer(0, FTBEConfig.RTP_MAX_DISTANCE.get()))
.requires(context -> FTBEConfig.RTP_MIN_DISTANCE_CUSTOM.get(context.getPlayer()))
.then(Commands.argument("maxDistance", IntegerArgumentType.integer(0, FTBEConfig.RTP_MAX_DISTANCE.get()))
.executes(context -> rtp(context.getSource().getPlayerOrException(), IntegerArgumentType.getInteger(context, "minDistance"), IntegerArgumentType.getInteger(context, "maxDistance")))
)
)
.executes(context -> rtp(context.getSource().getPlayerOrException(), FTBEConfig.RTP_MIN_DISTANCE.get(), FTBEConfig.RTP_MAX_DISTANCE.get()))),

// Teleport to the last location of a player
new SimpleConfigurableCommand(FTBEConfig.TPL, Commands.literal("teleport_last")
Expand Down Expand Up @@ -109,21 +120,25 @@ private static int spawn(ServerPlayer player) {
}

//#region RTP
private static int rtp(ServerPlayer player) {
private static int rtp(ServerPlayer player, int minDistance, int maxDistance) {
if (maxDistance < minDistance) {
player.displayClientMessage(Component.literal("Maximum teleport distance cannot be less than minimum!"), false);
return 0;
}
if (!player.hasPermissions(2) && !DimensionFilter.isRtpDimensionOK(player.level().dimension())) {
player.displayClientMessage(Component.literal("You may not use /rtp in this dimension!").withStyle(ChatFormatting.RED), false);
return 0;
}
return FTBEPlayerData.getOrCreate(player).map(data -> data.rtpTeleporter.teleport(player, p -> {
p.displayClientMessage(Component.literal("Looking for random location..."), false);
return findBlockPos((ServerLevel) player.level(), p);
return findBlockPos((ServerLevel) player.level(), p, minDistance, maxDistance);
}).runCommand(player))
.orElse(0);
}

private static TeleportPos findBlockPos(ServerLevel world, ServerPlayer player) {
private static TeleportPos findBlockPos(ServerLevel world, ServerPlayer player, int minDistance, int maxDistance) {
for (int attempt = 0; attempt < FTBEConfig.RTP_MAX_TRIES.get(); attempt++) {
double dist = FTBEConfig.RTP_MIN_DISTANCE.get() + world.random.nextDouble() * (FTBEConfig.RTP_MAX_DISTANCE.get() - FTBEConfig.RTP_MIN_DISTANCE.get());
double dist = minDistance + world.random.nextDouble() * (maxDistance - minDistance);
double angle = world.random.nextDouble() * Math.PI * 2D;

int x = Mth.floor(Math.cos(angle) * dist);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ public interface FTBEConfig {
StringListValue RTP_DIMENSION_BLACKLIST = RTP.config.addStringList("dimension_blacklist", List.of("minecraft:the_end"))
.comment("Blacklisted dimension ID's for /rtp (player *must not* be in any of these dimensions)",
"Wildcarded dimensions (e.g. 'somemod:*') are supported");

PermissionBasedBooleanValue RTP_MAX_DISTANCE_CUSTOM = new PermissionBasedBooleanValue(
RTP.config.addBoolean("allow_custom_max_distance", false),
"ftbessentials.rtp.custom_max",
"Allow player to specify (only) custom max distance in rtp command"
);

PermissionBasedBooleanValue RTP_MIN_DISTANCE_CUSTOM = new PermissionBasedBooleanValue(
RTP.config.addBoolean("allow_custom_min_max_distance", false),
"ftbessentials.rtp.custom_min_max",
"Allow player to specify custom min and max distance in rtp command"
);
// tpl
ToggleableConfig TPL = new ToggleableConfig(TELEPORTATION, "tpl")
.comment("Allows admins to teleport to the location a user was last seen at");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package dev.ftb.mods.ftbessentials.config;

import dev.ftb.mods.ftbessentials.integration.PermissionsHelper;
import dev.ftb.mods.ftblibrary.snbt.config.BooleanValue;
import net.minecraft.server.level.ServerPlayer;

public class PermissionBasedBooleanValue {
public final BooleanValue value;
public final String permission;

public PermissionBasedBooleanValue(BooleanValue value, String permission, String... comment) {
this.value = value
.comment(comment)
.comment("You can override this with FTB Ranks using " + permission);
this.permission = permission;
}

public boolean get(ServerPlayer player) {
return PermissionsHelper.getInstance().getBool(player, value.get(), permission);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ public class FTBRanksIntegration implements PermissionsProvider {
public int getInt(ServerPlayer player, int def, String node) {
return Math.max(FTBRanksAPI.getPermissionValue(player, node).asInteger().orElse(def), 0);
}

public boolean getBool(ServerPlayer player, boolean def, String node) {
return FTBRanksAPI.getPermissionValue(player, node).asBoolean().orElse(def);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public class LuckPermsIntegration implements PermissionsProvider {
public int getInt(ServerPlayer player, int def, String node) {
return Math.max(getMetaData(player.getUUID(), node).map(Integer::parseInt).orElse(def), 0);
}

public boolean getBool(ServerPlayer player, boolean def, String node) {
return getMetaData(player.getUUID(), node).map(Boolean::parseBoolean).orElse(def);
}

private static Optional<String> getMetaData(UUID uuid, String meta) {
LuckPerms luckperms = LuckPermsProvider.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ public interface PermissionsProvider {
default int getInt(ServerPlayer player, int def, String node) {
return def;
}

default boolean getBool(ServerPlayer player, boolean def, String node) {
return def;
}
}

0 comments on commit e245496

Please sign in to comment.