com.mojang.brigadier.context.CommandContext Java Examples

The following examples show how to use com.mojang.brigadier.context.CommandContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NMS_1_15.java    From 1.13-Command-API with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #2
Source File: NMS_1_13_1.java    From 1.13-Command-API with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #3
Source File: CartesianType.java    From Chimera with MIT License 6 votes vote down vote up
@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();
}
 
Example #4
Source File: PlayerCommand.java    From fabric-carpet with MIT License 6 votes vote down vote up
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;
}
 
Example #5
Source File: NMS_1_14_4.java    From 1.13-Command-API with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #6
Source File: ForgeClientSparkPlugin.java    From spark with GNU General Public License v3.0 6 votes vote down vote up
@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();
    });
}
 
Example #7
Source File: Commands.java    From BlueMap with MIT License 6 votes vote down vote up
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;
}
 
Example #8
Source File: SettingsManager.java    From fabric-carpet with MIT License 5 votes vote down vote up
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;
}
 
Example #9
Source File: NMS_1_13_1.java    From 1.13-Command-API with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #10
Source File: NMS_1_14_4.java    From 1.13-Command-API with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #11
Source File: SpooktoberCommand.java    From the-hallow with MIT License 5 votes vote down vote up
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;
}
 
Example #12
Source File: ScriptCommand.java    From fabric-carpet with MIT License 5 votes vote down vote up
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;
}
 
Example #13
Source File: HallowedCommand.java    From the-hallow with MIT License 5 votes vote down vote up
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;
}
 
Example #14
Source File: NMS_1_13_1.java    From 1.13-Command-API with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #15
Source File: NMS_1_15.java    From 1.13-Command-API with Apache License 2.0 5 votes vote down vote up
@Override
public EntityType getEntityType(CommandContext cmdCtx, String str, CommandSender sender)
		throws CommandSyntaxException {
	Entity entity = IRegistry.ENTITY_TYPE.get(ArgumentEntitySummon.a(cmdCtx, str))
			.a(((CraftWorld) getCommandSenderWorld(sender)).getHandle());
	return entity.getBukkitEntity().getType();
}
 
Example #16
Source File: NMS_1_13_2.java    From 1.13-Command-API with Apache License 2.0 5 votes vote down vote up
@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;
    }
}
 
Example #17
Source File: Commands.java    From BlueMap with MIT License 5 votes vote down vote up
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;
	}
}
 
Example #18
Source File: NMS_1_16_R1.java    From 1.13-Command-API with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #19
Source File: NMS_1_13.java    From 1.13-Command-API with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #20
Source File: NMS_1_15.java    From 1.13-Command-API with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #21
Source File: NMS_1_15.java    From 1.13-Command-API with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #22
Source File: WorldType.java    From Chimera with MIT License 5 votes vote down vote up
@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();
}
 
Example #23
Source File: NMS_1_16_R1.java    From 1.13-Command-API with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #24
Source File: ParticleType.java    From Chimera with MIT License 5 votes vote down vote up
@Override
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
    for (var particles : PARTICLES.prefixedKeys(builder.getRemaining())) {
        builder.suggest(particles);
    }
    
    return builder.buildFuture();
}
 
Example #25
Source File: CommandDispatcherTest.java    From brigadier with MIT License 5 votes vote down vote up
@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));
}
 
Example #26
Source File: NMS_1_13.java    From 1.13-Command-API with Apache License 2.0 5 votes vote down vote up
@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;
    }
}
 
Example #27
Source File: CommandDispatcherTest.java    From brigadier with MIT License 5 votes vote down vote up
@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));
}
 
Example #28
Source File: PlayerCommand.java    From fabric-carpet with MIT License 5 votes vote down vote up
private static int stop(CommandContext<ServerCommandSource> context)
{
    if (cantManipulate(context)) return 0;
    ServerPlayerEntity player = getPlayer(context);
    ((ServerPlayerEntityInterface) player).getActionPack().stopAll();
    return 1;
}
 
Example #29
Source File: NMS_1_13_2.java    From 1.13-Command-API with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #30
Source File: NMS_1_14_4.java    From 1.13-Command-API with Apache License 2.0 5 votes vote down vote up
@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;
    }
}