下面列出了java.util.function.ToIntBiFunction#com.mojang.brigadier.context.CommandContext 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
default <S> CompletableFuture<Suggestions> listSuggestions(S source, CommandContext<S> context, SuggestionsBuilder builder) {
var remaining = builder.getRemaining();
var parts = remaining.isBlank() ? EMPTY : remaining.split(" ");
if (source instanceof Player) {
var block = ((Player) source).getTargetBlockExact(5);
if (block != null) {
suggest(builder, parts, block.getLocation());
}
}
suggest(builder, parts);
return builder.buildFuture();
}
public int prioritizeRenderTaskCommand(CommandContext<S> context) {
CommandSource source = commandSourceInterface.apply(context.getSource());
String uuidString = context.getArgument("uuid", String.class);
Optional<UUID> taskUUID = parseUUID(uuidString);
if (!taskUUID.isPresent()) {
source.sendMessage(Text.of(TextColor.RED, "Not a valid UUID: " + uuidString));
return 0;
}
for (RenderTask task : plugin.getRenderManager().getRenderTasks()) {
if (task.getUuid().equals(taskUUID.get())) {
plugin.getRenderManager().prioritizeRenderTask(task);
source.sendMessages(helper.createStatusMessage());
return 1;
}
}
source.sendMessage(Text.of(TextColor.RED, "There is no render-task with this UUID: " + uuidString));
return 0;
}
private static boolean cantSpawn(CommandContext<ServerCommandSource> context)
{
String playerName = StringArgumentType.getString(context, "player");
MinecraftServer server = context.getSource().getMinecraftServer();
PlayerManager manager = server.getPlayerManager();
PlayerEntity player = manager.getPlayer(playerName);
if (player != null)
{
Messenger.m(context.getSource(), "r Player ", "rb " + playerName, "r is already logged on");
return true;
}
GameProfile profile = server.getUserCache().findByName(playerName);
if (manager.getUserBanList().contains(profile))
{
Messenger.m(context.getSource(), "r Player ", "rb " + playerName, "r is banned");
return true;
}
if (manager.isWhitelistEnabled() && profile != null && manager.isWhitelisted(profile) && !context.getSource().hasPermissionLevel(2))
{
Messenger.m(context.getSource(), "r Whitelisted players can only be spawned by operators");
return true;
}
return false;
}
@Override
public FunctionWrapper[] getFunction(CommandContext cmdCtx, String str) throws CommandSyntaxException {
Collection<CustomFunction> customFuncList = ArgumentTag.a(cmdCtx, str);
FunctionWrapper[] result = new FunctionWrapper[customFuncList.size()];
CustomFunctionData customFunctionData = getCLW(cmdCtx).getServer().getFunctionData();
CommandListenerWrapper commandListenerWrapper = getCLW(cmdCtx).a().b(2);
int count = 0;
for(CustomFunction customFunction : customFuncList) {
@SuppressWarnings("deprecation")
NamespacedKey minecraftKey = new NamespacedKey(customFunction.a().b(), customFunction.a().getKey());
ToIntBiFunction<CustomFunction, CommandListenerWrapper> obj = customFunctionData::a;
FunctionWrapper wrapper = new FunctionWrapper(minecraftKey, obj, customFunction, commandListenerWrapper, e -> {
return (Object) getCLW(cmdCtx).a(((CraftEntity) e).getHandle());
});
result[count] = wrapper;
count++;
}
return result;
}
@Override
public EnumSet<Axis> getAxis(CommandContext cmdCtx, String key) {
EnumSet<Axis> set = EnumSet.noneOf(Axis.class);
EnumSet<EnumAxis> parsedEnumSet = ArgumentRotationAxis.a(cmdCtx, key);
for (EnumAxis element : parsedEnumSet) {
switch (element) {
case X:
set.add(Axis.X);
break;
case Y:
set.add(Axis.Y);
break;
case Z:
set.add(Axis.Z);
break;
}
}
return set;
}
@Override
public CompletableFuture<Suggestions> getSuggestions(CommandContext<ISuggestionProvider> context, SuggestionsBuilder builder) throws CommandSyntaxException {
String chat = context.getInput();
String[] split = chat.split(" ");
if (split.length == 0 || (!split[0].equals("/sparkc") && !split[0].equals("/sparkclient"))) {
return Suggestions.empty();
}
String[] args = Arrays.copyOfRange(split, 1, split.length);
return CompletableFuture.supplyAsync(() -> {
for (String suggestion : this.platform.tabCompleteCommand(new ForgeCommandSender(this.minecraft.player, this), args)) {
builder.suggest(suggestion);
}
return builder.build();
});
}
@Override
public EnumSet<Axis> getAxis(CommandContext cmdCtx, String key) {
EnumSet<Axis> set = EnumSet.noneOf(Axis.class);
EnumSet<EnumAxis> parsedEnumSet = ArgumentRotationAxis.a(cmdCtx, key);
for (EnumAxis element : parsedEnumSet) {
switch (element) {
case X:
set.add(Axis.X);
break;
case Y:
set.add(Axis.Y);
break;
case Z:
set.add(Axis.Z);
break;
}
}
return set;
}
public static int run(CommandContext<ServerCommandSource> ctx) {
long daysLeft = TimeUtil.daysTillSpooktober();
if (daysLeft != 0) {
ctx.getSource().sendFeedback(new TranslatableText("thehallow.cmd.tillspooktober", daysLeft).formatted(Formatting.GRAY), false);
return 0;
}
ctx.getSource().sendFeedback(new TranslatableText("thehallow.cmd.spooktober").formatted(Formatting.GOLD, Formatting.BOLD), false);
return SINGLE_SUCCESS;
}
public static int run(CommandContext<ServerCommandSource> ctx) {
ctx.getSource().sendFeedback(Texts.bracketed(new TranslatableText("thehallow.about.name").formatted(Formatting.GOLD)), false);
ctx.getSource().sendFeedback(new TranslatableText("thehallow.about.description").formatted(Formatting.LIGHT_PURPLE), false);
ctx.getSource().sendFeedback(new TranslatableText("thehallow.about.github").formatted(Formatting.YELLOW)
.append(new TranslatableText("thehallow.github").formatted(Formatting.GREEN)
.styled(style -> style.setClickEvent(new ClickEvent(Action.OPEN_URL, "https://github.com/fabric-community/the-hallow")))),
false);
return SINGLE_SUCCESS;
}
@Override
public Location2D getLocation2D(CommandContext cmdCtx, String key, LocationType locationType2d, CommandSender sender) throws CommandSyntaxException {
switch (locationType2d) {
case BLOCK_POSITION:
ArgumentVec2I.a blockPos = ArgumentVec2I.a(cmdCtx, key);
return new Location2D(getCommandSenderWorld(sender), blockPos.a, blockPos.b);
case PRECISE_POSITION:
Vec2F vecPos = ArgumentVec2.a(cmdCtx, key);
return new Location2D(getCommandSenderWorld(sender), vecPos.i, vecPos.j);
}
return null;
}
@Override
public Player getPlayer(CommandContext cmdCtx, String str) throws CommandSyntaxException {
Player target = Bukkit.getPlayer(((GameProfile) ArgumentProfile.a(cmdCtx, str).iterator().next()).getId());
if (target == null) {
throw ArgumentProfile.a.create();
} else {
return target;
}
}
public int resumeCommand(CommandContext<S> context) {
CommandSource source = commandSourceInterface.apply(context.getSource());
if (!plugin.getRenderManager().isRunning()) {
plugin.getRenderManager().start();
source.sendMessage(Text.of(TextColor.GREEN, "BlueMap renders resumed!"));
return 1;
} else {
source.sendMessage(Text.of(TextColor.RED, "BlueMap renders are already running!"));
return 0;
}
}
@Override
public Sound getSound(CommandContext cmdCtx, String key) {
MinecraftKey minecraftKey = ArgumentMinecraftKeyRegistered.d(cmdCtx, key);
for (CraftSound sound : CraftSound.values()) {
try {
if (CommandAPIHandler.getField(CraftSound.class, "minecraftKey").get(sound)
.equals(minecraftKey.getKey())) {
return Sound.valueOf(sound.name());
}
} catch (IllegalArgumentException | IllegalAccessException e1) {
e1.printStackTrace();
}
}
return null;
}
@SuppressWarnings("deprecation")
@Override
public LootTable getLootTable(CommandContext cmdCtx, String str) {
MinecraftKey minecraftKey = ArgumentMinecraftKeyRegistered.c(cmdCtx, str);
String namespace = minecraftKey.b();
String key = minecraftKey.getKey();
net.minecraft.server.v1_13_R1.LootTable lootTable = getCLW(cmdCtx).getServer().aP().a(minecraftKey);
return new CraftLootTable(new NamespacedKey(namespace, key), lootTable);
}
@Override
public FloatRange getFloatRange(CommandContext<?> cmdCtx, String key) {
CriterionConditionValue.c range = cmdCtx.getArgument(key, CriterionConditionValue.c.class);
float low = range.a() == null
? -Float.MAX_VALUE
: range.a();
float high = range.b() == null
? Float.MAX_VALUE
: range.b();
return new FloatRange(low, high);
}
@Override
public IntegerRange getIntRange(CommandContext cmdCtx, String key) {
net.minecraft.server.v1_15_R1.CriterionConditionValue.IntegerRange range = ArgumentCriterionValue.b.a(cmdCtx,
key);
int low = range.a() == null ? Integer.MIN_VALUE : range.a();
int high = range.b() == null ? Integer.MAX_VALUE : range.b();
return new IntegerRange(low, high);
}
@Override
public FloatRange getFloatRange(CommandContext<?> cmdCtx, String key) {
CriterionConditionValue.c range = cmdCtx.getArgument(key, CriterionConditionValue.c.class);
float low = range.a() == null
? -Float.MAX_VALUE
: range.a();
float high = range.b() == null
? Float.MAX_VALUE
: range.b();
return new dev.jorel.commandapi.wrappers.FloatRange(low, high);
}
@Override
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
for (var particles : PARTICLES.prefixedKeys(builder.getRemaining())) {
builder.suggest(particles);
}
return builder.buildFuture();
}
@Override
public Player getPlayer(CommandContext cmdCtx, String str) throws CommandSyntaxException {
Player target = Bukkit.getPlayer(((GameProfile) ArgumentProfile.a(cmdCtx, str).iterator().next()).getId());
if (target == null) {
throw ArgumentProfile.a.create();
} else {
return target;
}
}
@SuppressWarnings("unchecked")
@Test
public void testCreateAndExecuteCommand() throws Exception {
subject.register(literal("foo").executes(command));
assertThat(subject.execute("foo", source), is(42));
verify(command).run(any(CommandContext.class));
}
private static int stop(CommandContext<ServerCommandSource> context)
{
if (cantManipulate(context)) return 0;
ServerPlayerEntity player = getPlayer(context);
((ServerPlayerEntityInterface) player).getActionPack().stopAll();
return 1;
}
@Override
public Environment getDimension(CommandContext cmdCtx, String key) {
DimensionManager manager = ArgumentDimension.a(cmdCtx, key);
switch (manager.getDimensionID()) {
case 0:
return Environment.NORMAL;
case -1:
return Environment.NETHER;
case 1:
return Environment.THE_END;
}
return null;
}
@Override
public Player getPlayer(CommandContext cmdCtx, String str) throws CommandSyntaxException {
Player target = Bukkit.getPlayer(((GameProfile) ArgumentProfile.a(cmdCtx, str).iterator().next()).getId());
if (target == null) {
throw ArgumentProfile.a.create();
} else {
return target;
}
}
@SuppressWarnings("unchecked")
@Test
public void testCreateAndMergeCommands() throws Exception {
subject.register(literal("base").then(literal("foo").executes(command)));
subject.register(literal("base").then(literal("bar").executes(command)));
assertThat(subject.execute("base foo", source), is(42));
assertThat(subject.execute("base bar", source), is(42));
verify(command, times(2)).run(any(CommandContext.class));
}
private static int invoke(CommandContext<ServerCommandSource> context, String call, BlockPos pos1, BlockPos pos2, String args)
{
ServerCommandSource source = context.getSource();
CarpetScriptHost host = getHost(context);
if (call.startsWith("__"))
{
Messenger.m(source, "r Hidden functions are only callable in scripts");
return 0;
}
List<Integer> positions = new ArrayList<>();
if (pos1 != null)
{
positions.add(pos1.getX());
positions.add(pos1.getY());
positions.add(pos1.getZ());
}
if (pos2 != null)
{
positions.add(pos2.getX());
positions.add(pos2.getY());
positions.add(pos2.getZ());
}
//if (!(args.trim().isEmpty()))
// arguments.addAll(Arrays.asList(args.trim().split("\\s+")));
handleCall(source, host, () -> host.call(source, call, positions, args));
return 1;
}
@Override
public CommandSender getSenderForCommand(CommandContext cmdCtx) {
CommandSender sender = getCLW(cmdCtx).getBukkitSender();
Entity proxyEntity = getCLW(cmdCtx).getEntity();
if (proxyEntity != null) {
CommandSender proxy = ((Entity) proxyEntity).getBukkitEntity();
if (!proxy.equals(sender)) {
sender = new ProxiedNativeCommandSender(getCLW(cmdCtx), sender, proxy);
}
}
return sender;
}
private ParsedRule<?> contextRule(CommandContext<ServerCommandSource> ctx) throws CommandSyntaxException
{
String ruleName = StringArgumentType.getString(ctx, "rule");
ParsedRule<?> rule = getRule(ruleName);
if (rule == null)
throw new SimpleCommandExceptionType(Messenger.c("rb "+ tr("ui.unknown_rule","Unknown rule")+": "+ruleName)).create();
return rule;
}
@Override
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
for (var world : Bukkit.getWorlds()) {
if (world.getName().startsWith(builder.getRemaining())) {
builder.suggest(world.getName().contains(" ") ? '"' + world.getName() + '"' : world.getName());
}
}
return builder.buildFuture();
}
@Override
public MathOperation getMathOperation(CommandContext cmdCtx, String key) throws CommandSyntaxException {
ArgumentMathOperation.a result = ArgumentMathOperation.a(cmdCtx, key);
net.minecraft.server.v1_15_R1.Scoreboard board = new net.minecraft.server.v1_15_R1.Scoreboard();
ScoreboardScore tester_left = new ScoreboardScore(board, null, null);
ScoreboardScore tester_right = new ScoreboardScore(board, null, null);
tester_left.setScore(6);
tester_right.setScore(2);
result.apply(tester_left, tester_right);
switch (tester_left.getScore()) {
case 8:
return MathOperation.ADD;
case 4:
return MathOperation.SUBTRACT;
case 12:
return MathOperation.MULTIPLY;
case 3:
return MathOperation.DIVIDE;
case 0:
return MathOperation.MOD;
case 6:
return MathOperation.MAX;
case 2: {
if (tester_right.getScore() == 6)
return MathOperation.SWAP;
tester_left.setScore(2);
tester_right.setScore(6);
result.apply(tester_left, tester_right);
if (tester_left.getScore() == 2)
return MathOperation.MIN;
return MathOperation.ASSIGN;
}
}
return null;
}
@Override
public FunctionWrapper[] getFunction(CommandContext cmdCtx, String str) throws CommandSyntaxException {
Collection<CustomFunction> customFuncList = ArgumentTag.a(cmdCtx, str);
FunctionWrapper[] result = new FunctionWrapper[customFuncList.size()];
CustomFunctionData customFunctionData = getCLW(cmdCtx).getServer().getFunctionData();
CommandListenerWrapper commandListenerWrapper = getCLW(cmdCtx).a().b(2);
int count = 0;
for (CustomFunction customFunction : customFuncList) {
@SuppressWarnings("deprecation")
NamespacedKey minecraftKey = new NamespacedKey(customFunction.a().getNamespace(),
customFunction.a().getKey());
ToIntBiFunction<CustomFunction, CommandListenerWrapper> obj = customFunctionData::a;
FunctionWrapper wrapper = new FunctionWrapper(minecraftKey, obj, customFunction, commandListenerWrapper,
e -> {
return (Object) getCLW(cmdCtx).a(((CraftEntity) e).getHandle());
});
result[count] = wrapper;
count++;
}
return result;
}