Skip to content

Commit

Permalink
Added commands to allow admins to view/teleport to other player's homes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sintinium committed Dec 21, 2021
1 parent 2968c59 commit cb76a86
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 4 deletions.
93 changes: 89 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(context.getSource().getPlayerOrException(), 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,65 @@ 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(ServerPlayer serverPlayer, String offlineName) {
UUID uuid = getOfflineUUIDByName(offlineName);
if (uuid == null) {
serverPlayer.displayClientMessage(new TextComponent("No player with name " + offlineName + " found!"), false);
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

0 comments on commit cb76a86

Please sign in to comment.