Skip to content
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

Added commands to allow admins to view/teleport to other player's homes. #31

Open
wants to merge 1 commit into
base: 1.18/dev
Choose a base branch
from
Open
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
92 changes: 88 additions & 4 deletions src/main/java/dev/ftb/mods/ftbessentials/command/HomeCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.common.UsernameCache;

import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.*;

/**
* @author LatvianModder
Expand Down Expand Up @@ -59,6 +58,29 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
.executes(context -> listhomes(context.getSource(), GameProfileArgument.getGameProfiles(context, "player").iterator().next()))
)
);

dispatcher.register(Commands.literal("homefor")
.requires(FTBEConfig.HOME_FOR.enabledAndOp())
.then(Commands.argument("player", StringArgumentType.string())
.requires(source -> source.hasPermission(2))
.suggests((context, builder) -> SharedSuggestionProvider.suggest(getAllPlayerNameSuggestion(context.getSource().getPlayerOrException()), builder))
.executes(context -> listHomesFor(context.getSource(), StringArgumentType.getString(context, "player")))
.then(Commands.argument("name", StringArgumentType.greedyString())
.requires(source -> source.hasPermission(2))
.suggests((context, builder) -> SharedSuggestionProvider.suggest(getOfflineHomeSuggestions(StringArgumentType.getString(context, "player")), builder))
.executes(context -> homeFor(context.getSource().getPlayerOrException(), StringArgumentType.getString(context, "player"), StringArgumentType.getString(context, "name")))
)
)
);

dispatcher.register(Commands.literal("listhomesfor")
.requires(FTBEConfig.HOME_FOR.enabledAndOp())
.then(Commands.argument("player", StringArgumentType.string())
.requires(source -> source.hasPermission(2))
.suggests((context, builder) -> SharedSuggestionProvider.suggest(getAllPlayerNameSuggestion(context.getSource().getPlayerOrException()), builder))
.executes(context -> listHomesFor(context.getSource(), StringArgumentType.getString(context, "player")))
)
);
}

public static Set<String> getHomeSuggestions(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
Expand Down Expand Up @@ -124,8 +146,10 @@ public static int delhome(ServerPlayer player, String name) {
}

public static int listhomes(CommandSourceStack source, GameProfile of) {
FTBEPlayerData data = FTBEPlayerData.get(of);
return listhomes(source, FTBEPlayerData.get(of));
}

public static int listhomes(CommandSourceStack source, FTBEPlayerData data) {
if (data == null) {
return 0;
}
Expand All @@ -143,4 +167,64 @@ public static int listhomes(CommandSourceStack source, GameProfile of) {

return 1;
}

private static Iterable<String> getAllPlayerNameSuggestion(ServerPlayer player) {
List<String> names = new ArrayList<>(UsernameCache.getMap().values());
names.remove(player.getGameProfile().getName());
return names;
}

private static UUID getOfflineUUIDByName(String name) {
return UsernameCache.getMap().entrySet().stream()
.filter(entry -> entry.getValue().equalsIgnoreCase(name))
.map(Map.Entry::getKey)
.findFirst()
.orElse(null);
}

private static FTBEPlayerData getOfflinePlayerData(UUID uuid) {
if (uuid == null) return null;
FTBEPlayerData data = FTBEPlayerData.MAP.get(uuid);
if (data == null) {
data = new FTBEPlayerData(uuid);
data.load();
}
return data;
}

private static int listHomesFor(CommandSourceStack source, String name) throws CommandSyntaxException {
UUID uuid = getOfflineUUIDByName(name);
if (uuid == null) {
source.getPlayerOrException().displayClientMessage(new TextComponent("No player found with name " + name + " !"), false);
return 0;
}
FTBEPlayerData data = getOfflinePlayerData(uuid);
return listhomes(source, data);
}

private static int homeFor(ServerPlayer player, String offlineName, String homeName) {
UUID uuid = getOfflineUUIDByName(offlineName);
if (uuid == null) {
player.displayClientMessage(new TextComponent("No player with name " + offlineName + " found!"), false);
return 0;
}
FTBEPlayerData data = getOfflinePlayerData(uuid);
TeleportPos pos = data.homes.get(homeName.toLowerCase());

if (pos == null) {
player.displayClientMessage(new TextComponent("Home not found!"), false);
return 0;
}

return data.homeTeleporter.teleport(player, p -> pos).runCommand(player);
}

private static Set<String> getOfflineHomeSuggestions(String offlineName) {
UUID uuid = getOfflineUUIDByName(offlineName);
if (uuid == null) {
return Collections.emptySet();
}
FTBEPlayerData data = getOfflinePlayerData(uuid);
return data.homes.keySet();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public interface FTBEConfig {
.comment("Allows admins to view other users' inventories using a command");
ToggleableConfig MUTE = new ToggleableConfig(ADMIN, "mute") // todo: temp mute?
.comment("Allows admins to restrict players from chatting by using a command to mute (or unmute) them");
ToggleableConfig HOME_FOR = new ToggleableConfig(ADMIN, "homefor")
.comment("Allows admins to view and teleport to other users' homes");

SNBTConfig MISC = CONFIG.getGroup("misc").comment("Miscellaneous features and utilities");
ToggleableConfig KICKME = new ToggleableConfig(MISC, "kickme")
Expand Down