/* * Minecraft Forge - Forge Development LLC * SPDX-License-Identifier: LGPL-2.1-only */ package net.minecraftforge.client; import net.minecraft.Util; import net.minecraft.advancements.Advancement; import net.minecraft.client.Minecraft; import net.minecraft.commands.CommandSource; import net.minecraft.commands.CommandSourceStack; import net.minecraft.core.RegistryAccess; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerLevel; import net.minecraft.world.entity.Entity; import net.minecraft.world.item.crafting.RecipeManager; import net.minecraft.world.level.Level; import net.minecraft.world.phys.Vec2; import net.minecraft.world.phys.Vec3; import net.minecraft.world.scores.Scoreboard; import java.util.Collection; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; /** * overrides for {@link CommandSourceStack} so that the methods will run successfully client side */ public class ClientCommandSourceStack extends CommandSourceStack { public ClientCommandSourceStack(CommandSource source, Vec3 position, Vec2 rotation, int permission, String plainTextName, Component displayName, Entity executing) { super(source, position, rotation, null, permission, plainTextName, displayName, null, executing); } /** * Sends a success message without attempting to get the server side list of admins */ @Override public void sendSuccess(Component message, boolean sendToAdmins) { Minecraft.getInstance().player.sendMessage(message, Util.NIL_UUID); } /** * Gets the list of teams from the client side */ @Override public Collection getAllTeams() { return Minecraft.getInstance().level.getScoreboard().getTeamNames(); } /** * Gets the list of online player names from the client side */ @Override public Collection getOnlinePlayerNames() { return Minecraft.getInstance().getConnection().getOnlinePlayers().stream().map((player) -> player.getProfile().getName()).collect(Collectors.toList()); } /** * Gets a {@link Stream} of recipe ids that are available on the client */ @Override public Stream getRecipeNames() { return Minecraft.getInstance().getConnection().getRecipeManager().getRecipeIds(); } /** * Gets a set of {@link ResourceKey} for levels from the client side */ @Override public Set> levels() { return Minecraft.getInstance().getConnection().levels(); } /** * Gets the {@link RegistryAccess} from the client side */ @Override public RegistryAccess registryAccess() { return Minecraft.getInstance().getConnection().registryAccess(); } /** * Gets the scoreboard from the client side */ @Override public Scoreboard getScoreboard() { return Minecraft.getInstance().level.getScoreboard(); } /** * Gets the advancement from the id from the client side where the advancement needs to be visible to the player */ @Override public Advancement getAdvancement(ResourceLocation id) { return Minecraft.getInstance().getConnection().getAdvancements().getAdvancements().get(id); } /** * Gets the {@link RecipeManager} from the client side */ @Override public RecipeManager getRecipeManager() { return Minecraft.getInstance().getConnection().getRecipeManager(); } /** * Gets the level from the client side */ @Override public Level getUnsidedLevel() { return Minecraft.getInstance().level; } /** * @throws UnsupportedOperationException * because the server isn't available on the client */ @Override public MinecraftServer getServer() { throw new UnsupportedOperationException("Attempted to get server in client command"); } /** * @throws UnsupportedOperationException * because the server side level isn't available on the client side */ @Override public ServerLevel getLevel() { throw new UnsupportedOperationException("Attempted to get server level in client command"); } }