Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
minecraftforge
GitHub Repository: minecraftforge/minecraftforge
Path: blob/1.20.x/src/test_old/java/net/minecraftforge/debug/chat/ClientCommandTest.java
6513 views
1
/*
2
* Copyright (c) Forge Development LLC and contributors
3
* SPDX-License-Identifier: LGPL-2.1-only
4
*/
5
6
package net.minecraftforge.debug.chat;
7
8
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
9
import com.mojang.brigadier.context.CommandContext;
10
import net.minecraft.commands.CommandSourceStack;
11
import net.minecraft.commands.Commands;
12
import net.minecraft.commands.SharedSuggestionProvider;
13
import net.minecraft.commands.arguments.ObjectiveArgument;
14
import net.minecraft.commands.arguments.ResourceLocationArgument;
15
import net.minecraft.commands.arguments.TeamArgument;
16
import net.minecraft.commands.arguments.coordinates.BlockPosArgument;
17
import net.minecraft.commands.synchronization.SuggestionProviders;
18
import net.minecraft.network.chat.Component;
19
import net.minecraftforge.debug.client.TestScreen;
20
import net.minecraftforge.client.event.RegisterClientCommandsEvent;
21
import net.minecraftforge.common.MinecraftForge;
22
import net.minecraftforge.fml.common.Mod;
23
import net.minecraftforge.registries.ForgeRegistries;
24
25
import java.util.List;
26
27
@Mod("client_command_test")
28
public class ClientCommandTest
29
{
30
public ClientCommandTest()
31
{
32
MinecraftForge.EVENT_BUS.addListener(this::init);
33
}
34
35
private void init(RegisterClientCommandsEvent event)
36
{
37
event.getDispatcher().register(
38
Commands.literal("clientcommandtest")
39
// Used for checking suggestion providers that aren't registered
40
.then(Commands.literal("rawsuggest")
41
.then(Commands.argument("block", ResourceLocationArgument.id())
42
.suggests((c, b) -> SharedSuggestionProvider.suggestResource(ForgeRegistries.BLOCKS.getKeys(), b))
43
.executes(this::testCommand)))
44
// Used for checking suggestion providers that are registered
45
.then(Commands.literal("registeredsuggest").then(
46
Commands.argument("block", ResourceLocationArgument.id())
47
.suggests(SuggestionProviders.ALL_RECIPES)
48
.executes(this::testCommand)))
49
// Used for checking if attempting to get the server on the client side errors
50
.then(Commands.literal("server")
51
.executes((context) -> {
52
context.getSource().getServer();
53
context.getSource().sendSuccess(() ->Component.literal("Successfully called getServer should have errored"), false);
54
return 1;
55
}))
56
// Used for checking if attempting to get the server level on the client side errors
57
.then(Commands.literal("level")
58
.executes((context) -> {
59
context.getSource().getLevel();
60
context.getSource().sendSuccess(() ->Component.literal("Successfully called getLevel should have errored"), false);
61
return 1;
62
}))
63
// Used for checking if getting a known objective argument works on the client side
64
.then(Commands.literal("get_objective")
65
.then(Commands.argument("objective", ObjectiveArgument.objective())
66
.executes((context) -> {
67
final Component msg = Component.literal("Regular: ")
68
.append(ObjectiveArgument.getObjective(context, "objective").getFormattedDisplayName());
69
context.getSource().sendSuccess(() -> msg, false);
70
return 1;
71
})))
72
// Used for checking if getting a known advancement works on the client side
73
.then(Commands.literal("get_advancement")
74
.then(Commands.argument("advancement", ResourceLocationArgument.id())
75
.executes((context) -> {
76
final Component msg = ResourceLocationArgument.getAdvancement(context, "advancement").value().name().get();
77
context.getSource().sendSuccess(() -> msg, false);
78
return 1;
79
})))
80
// Used for checking if getting a known recipe works on the client side
81
.then(Commands.literal("get_recipe")
82
.then(Commands.argument("recipe", ResourceLocationArgument.id())
83
.executes((context) -> {
84
final Component msg = ResourceLocationArgument.getRecipe(context, "recipe").value().getResultItem(context.getSource().registryAccess()).getDisplayName();
85
context.getSource()
86
.sendSuccess(() -> msg, false);
87
return 1;
88
})))
89
// Used for checking if getting a team works on the client side
90
.then(Commands.literal("get_team")
91
.then(Commands.argument("team", TeamArgument.team())
92
.executes((context) -> {
93
final Component msg = TeamArgument.getTeam(context, "team").getFormattedDisplayName();
94
context.getSource().sendSuccess(() -> msg, false);
95
return 1;
96
})))
97
// Used for checking if a block position is valid works on the client side
98
.then(Commands.literal("get_loaded_blockpos")
99
.then(Commands.argument("blockpos", BlockPosArgument.blockPos())
100
.executes((context) -> {
101
final Component msg = Component.literal(BlockPosArgument.getLoadedBlockPos(context, "blockpos").toString());
102
context.getSource()
103
.sendSuccess(() -> msg, false);
104
return 1;
105
})))
106
// Used for checking if a command can have a requirement
107
.then(Commands.literal("requires")
108
.requires((source) -> false)
109
.executes((context) -> {
110
context.getSource().sendSuccess(() ->Component.literal("Executed command"), false);
111
return 1;
112
}))
113
// Used for testing the screen after using commands
114
.then(Commands.literal("screentest")
115
.executes((stack) -> TestScreen.open())));
116
117
// Used for testing that client command redirects can only be used with client commands
118
LiteralArgumentBuilder<CommandSourceStack> fork = Commands.literal("clientcommandfork");
119
fork.fork(event.getDispatcher().getRoot(), (context) -> List.of(context.getSource(), context.getSource()))
120
.executes((context) -> {
121
context.getSource().sendSuccess(() ->Component.literal("Executing forked command"), false);
122
return 1;
123
});
124
event.getDispatcher().register(fork);
125
}
126
127
private int testCommand(CommandContext<CommandSourceStack> context)
128
{
129
context.getSource().sendSuccess(() ->Component.literal("Input: " + ResourceLocationArgument.getId(context, "block")), false);
130
context.getSource().sendSuccess(() ->Component.literal("Teams: " + context.getSource().getAllTeams()), false);
131
context.getSource().sendSuccess(() ->Component.literal("Players: " + context.getSource().getOnlinePlayerNames()), false);
132
context.getSource().sendSuccess(() ->Component.literal("First recipe: " + context.getSource().getRecipeNames().findFirst().get()), false);
133
context.getSource().sendSuccess(() ->Component.literal("Levels: " + context.getSource().levels()), false);
134
context.getSource().sendSuccess(() ->Component.literal("Registry Access: " + context.getSource().registryAccess()), false);
135
return 0;
136
}
137
}
138
139