net.minecraft.command.CommandSource Java Examples

The following examples show how to use net.minecraft.command.CommandSource. 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: ForgeServerSparkPlugin.java    From spark with GNU General Public License v3.0 6 votes vote down vote up
public static void register(ForgeSparkMod mod, FMLServerStartingEvent event) {
    MinecraftServer server = event.getServer();
    ForgeServerSparkPlugin plugin = new ForgeServerSparkPlugin(mod, server);

    CommandDispatcher<CommandSource> dispatcher = event.getCommandDispatcher();
    registerCommands(dispatcher, plugin, plugin, "spark");
    PermissionAPI.registerNode("spark", DefaultPermissionLevel.OP, "Access to the spark command");
}
 
Example #2
Source File: ServerCommandConfig.java    From Better-Sprinting with Mozilla Public License 2.0 6 votes vote down vote up
private static int execSetting(CommandContext<CommandSource> ctx){
	String setting = ctx.getArgument(ARG_SETTINGS, String.class);
	boolean value = ctx.getArgument(ARG_BOOLEAN, Boolean.class);
	
	CommandSource source = ctx.getSource();
	
	if (setting.equalsIgnoreCase(SETTING_SURVIVAL_FLY_BOOST)){
		BetterSprintingMod.config.set(ServerSettings.enableSurvivalFlyBoost, value);
		BetterSprintingMod.config.save();
		
		sendMessageTranslated(source, ServerSettings.enableSurvivalFlyBoost.get() ? "bs.command.enableFlyBoost" : "bs.command.disableFlyBoost", true);
		ServerNetwork.sendToAll(source.getServer().getPlayerList().getPlayers(), ServerNetwork.writeSettings(ServerSettings.enableSurvivalFlyBoost.get(), ServerSettings.enableAllDirs.get()));
	}
	else if (setting.equalsIgnoreCase(SETTING_RUN_IN_ALL_DIRS)){
		BetterSprintingMod.config.set(ServerSettings.enableAllDirs, value);
		BetterSprintingMod.config.save();
		
		sendMessageTranslated(source, ServerSettings.enableAllDirs.get() ? "bs.command.enableAllDirs" : "bs.command.disableAllDirs", true);
		ServerNetwork.sendToAll(source.getServer().getPlayerList().getPlayers(), ServerNetwork.writeSettings(ServerSettings.enableSurvivalFlyBoost.get(), ServerSettings.enableAllDirs.get()));
	}
	else{
		execHelp(ctx);
	}
	
	return 0;
}
 
Example #3
Source File: ConfigCommand.java    From BoundingBoxOutlineReloaded with MIT License 6 votes vote down vote up
private static LiteralArgumentBuilder<CommandSource> setCommandForSetting(Setting<?> setting) {
    LiteralArgumentBuilder<CommandSource> command = Commands.literal(setting.getName());
    switch (setting.getType()) {
        case 'B':
            buildSetSettingCommand(command, (Setting<Boolean>) setting, Arguments.bool(), Boolean.class);
            break;
        case 'I':
            buildSetSettingCommand(command, (Setting<Integer>) setting, Arguments.integer(), Integer.class);
            break;
        case 'S':
            buildSetSettingCommand(command, (Setting<String>) setting, Arguments.string(), String.class);
            break;
        case 'H':
            buildSetSettingCommand(command, (Setting<HexColor>) setting, Arguments.hexColor(), HexColor.class);
            break;
    }
    return command;
}
 
Example #4
Source File: LineCommandBuilder.java    From BoundingBoxOutlineReloaded with MIT License 5 votes vote down vote up
static LiteralArgumentBuilder<CommandSource> build(String command) {
    return Commands.literal(command)
            .then(Commands.literal(ArgumentNames.ADD)
                    .then(Commands.argument(ArgumentNames.FROM, Arguments.point())
                            .then(Commands.argument(ArgumentNames.TO, Arguments.point())
                                    .executes(LineCommandBuilder::addLine)
                                    .then(Commands.argument(WIDTH, Arguments.doubleArg())
                                            .executes(LineCommandBuilder::addLine)))))
            .then(Commands.literal(ArgumentNames.CLEAR)
                    .executes(context -> {
                        CustomLineProvider.clear();

                        CommandHelper.feedback(context, "bbor.commands.line.cleared.all");
                        return 0;
                    })
                    .then(Commands.argument(ArgumentNames.FROM, Arguments.coords())
                            .then(Commands.argument(ArgumentNames.TO, Arguments.coords())
                                    .executes(context -> {
                                        Point from = Arguments.getPoint(context, ArgumentNames.FROM).snapXZ(0.5d);
                                        Point to = Arguments.getPoint(context, ArgumentNames.TO).snapXZ(0.5d);
                                        boolean removed = CustomLineProvider.remove(from, to);

                                        String format = removed ? "bbor.commands.line.cleared" : "bbor.commands.line.notFound";
                                        CommandHelper.feedback(context, format,
                                                from.getX(), from.getY(), from.getZ(),
                                                to.getX(), to.getY(), to.getZ());
                                        return 0;
                                    }))));
}
 
Example #5
Source File: BoxCommandBuilder.java    From BoundingBoxOutlineReloaded with MIT License 5 votes vote down vote up
private static int addBox(CommandContext<CommandSource> context) throws CommandSyntaxException {
    Coords from = Arguments.getCoords(context, ArgumentNames.FROM);
    Coords to = Arguments.getCoords(context, ArgumentNames.TO);
    Coords minCoords = getMinCoords(from, to);
    Coords maxCoords = getMaxCoords(from, to);
    CustomBoxProvider.add(minCoords, maxCoords);

    CommandHelper.feedback(context, "bbor.commands.box.added",
            from.getX(), from.getY(), from.getZ(),
            to.getX(), to.getY(), to.getZ());
    return 0;
}
 
Example #6
Source File: BoxCommandBuilder.java    From BoundingBoxOutlineReloaded with MIT License 5 votes vote down vote up
static LiteralArgumentBuilder<CommandSource> build(String command) {
    return Commands.literal(command)
            .then(Commands.literal(ArgumentNames.ADD)
                    .then(Commands.argument(ArgumentNames.FROM, Arguments.coords())
                            .then(Commands.argument(ArgumentNames.TO, Arguments.coords())
                                    .executes(BoxCommandBuilder::addBox))))
            .then(Commands.literal(ArgumentNames.CLEAR)
                    .executes(context -> {
                        CustomBoxProvider.clear();

                        CommandHelper.feedback(context, "bbor.commands.box.cleared.all");
                        return 0;
                    })
                    .then(Commands.argument(ArgumentNames.FROM, Arguments.coords())
                            .then(Commands.argument(ArgumentNames.TO, Arguments.coords())
                                    .executes(context -> {
                                        Coords from = Arguments.getCoords(context, ArgumentNames.FROM);
                                        Coords to = Arguments.getCoords(context, ArgumentNames.TO);
                                        Coords minCoords = getMinCoords(from, to);
                                        Coords maxCoords = getMaxCoords(from, to);
                                        boolean removed = CustomBoxProvider.remove(minCoords, maxCoords);

                                        String format = removed ? "bbor.commands.box.cleared" : "bbor.commands.box.notFound";
                                        CommandHelper.feedback(context, format,
                                                from.getX(), from.getY(), from.getZ(),
                                                to.getX(), to.getY(), to.getZ());
                                        return 0;
                                    }))));
}
 
Example #7
Source File: BeaconCommandBuilder.java    From BoundingBoxOutlineReloaded with MIT License 5 votes vote down vote up
private static int addBeacon(CommandContext<CommandSource> context) throws CommandSyntaxException {
    Coords coords = Arguments.getCoords(context, ArgumentNames.POS);
    int level = Arguments.getInteger(context, LEVEL);

    CustomBeaconProvider.add(coords, level);
    CommandHelper.feedback(context, "bbor.commands.beacon.added", coords.getX(), coords.getY(), coords.getZ(), level);
    return 0;
}
 
Example #8
Source File: ConfigCommand.java    From BoundingBoxOutlineReloaded with MIT License 5 votes vote down vote up
private static <T> void buildSetSettingCommand(LiteralArgumentBuilder<CommandSource> command,
                                               Setting<T> setting, ArgumentType<T> argument, Class<T> clazz) {
    Command<CommandSource> setSettingCommand = context -> {
        setting.set(context.getArgument(VALUE, clazz));
        if (CommandHelper.lastNodeIsLiteral(context, SAVE)) {
            ConfigManager.saveConfig();
        }
        return 0;
    };
    command.then(Commands.argument(VALUE, argument)
            .executes(setSettingCommand)
            .then(Commands.literal(SAVE)
                    .executes(setSettingCommand)));
}
 
Example #9
Source File: LineCommandBuilder.java    From BoundingBoxOutlineReloaded with MIT License 5 votes vote down vote up
private static int addLine(CommandContext<CommandSource> context) throws CommandSyntaxException {
    Point from = Arguments.getPoint(context, ArgumentNames.FROM).snapXZ(0.5d);
    Point to = Arguments.getPoint(context, ArgumentNames.TO).snapXZ(0.5d);
    Double width = Arguments.getDouble(context, WIDTH);
    CustomLineProvider.add(from, to, width);

    CommandHelper.feedback(context, "bbor.commands.line.added",
            from.getX(), from.getY(), from.getZ(),
            to.getX(), to.getY(), to.getZ());
    return 0;
}
 
Example #10
Source File: ClientInterop.java    From BoundingBoxOutlineReloaded with MIT License 5 votes vote down vote up
public static boolean interceptChatMessage(String message) {
    if (message.startsWith("/bbor:")) {
        ClientPlayNetHandler connection = Minecraft.getInstance().getConnection();
        if (connection != null) {
            CommandDispatcher<ISuggestionProvider> commandDispatcher = connection.func_195515_i();
            CommandSource commandSource = Minecraft.getInstance().player.getCommandSource();
            try {
                commandDispatcher.execute(message.substring(1), commandSource);
            } catch (CommandSyntaxException exception) {
                commandSource.sendErrorMessage(TextComponentUtils.toTextComponent(exception.getRawMessage()));
                if (exception.getInput() != null && exception.getCursor() >= 0) {
                    ITextComponent suggestion = new StringTextComponent("")
                            .applyTextStyle(TextFormatting.GRAY)
                            .applyTextStyle(style -> style.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, message)));
                    int textLength = Math.min(exception.getInput().length(), exception.getCursor());
                    if (textLength > 10) {
                        suggestion.appendText("...");
                    }

                    suggestion.appendText(exception.getInput().substring(Math.max(0, textLength - 10), textLength));
                    if (textLength < exception.getInput().length()) {
                        suggestion.appendSibling(new StringTextComponent(exception.getInput().substring(textLength))
                                .applyTextStyles(TextFormatting.RED, TextFormatting.UNDERLINE));
                    }

                    suggestion.appendSibling(new TranslationTextComponent("command.context.here")
                            .applyTextStyles(TextFormatting.RED, TextFormatting.ITALIC));
                    commandSource.sendErrorMessage(suggestion);
                }
            }
        }
        return true;
    }
    return false;
}
 
Example #11
Source File: ConfigCommand.java    From BoundingBoxOutlineReloaded with MIT License 5 votes vote down vote up
private static LiteralArgumentBuilder<CommandSource> getCommandForSetting(Setting<?> setting) {
    return Commands.literal(setting.getName())
            .executes(context -> {
                CommandHelper.feedback(context, "%s: %s", setting.getName(), setting.get());
                return 0;
            });
}
 
Example #12
Source File: ConfigCommand.java    From BoundingBoxOutlineReloaded with MIT License 5 votes vote down vote up
private static LiteralArgumentBuilder<CommandSource> buildCommands(String commandName, CommandBuilder commandBuilder) {
    LiteralArgumentBuilder<CommandSource> command = Commands.literal(commandName);
    for (Setting<?> setting : ConfigManager.getSettings()) {
        command.then(commandBuilder.apply(setting));
    }
    return command;
}
 
Example #13
Source File: ConfigCommand.java    From BoundingBoxOutlineReloaded with MIT License 5 votes vote down vote up
private static LiteralArgumentBuilder<CommandSource> resetCommandForSetting(Setting<?> setting) {
    return Commands.literal(setting.getName())
            .executes(context -> {
                setting.reset();
                ConfigManager.saveConfig();
                return 0;
            });
}
 
Example #14
Source File: CommandHelper.java    From BoundingBoxOutlineReloaded with MIT License 5 votes vote down vote up
static boolean lastNodeIsLiteral(CommandContext<CommandSource> context, String literal) {
    CommandNode lastNode = getLastNode(context);
    if (lastNode instanceof LiteralCommandNode) {
        LiteralCommandNode literalCommandNode = (LiteralCommandNode) lastNode;
        return literalCommandNode.getLiteral().equals(literal);
    }
    return false;
}
 
Example #15
Source File: SphereCommandBuilder.java    From BoundingBoxOutlineReloaded with MIT License 5 votes vote down vote up
private static int addSphere(CommandContext<CommandSource> context) throws CommandSyntaxException {
    Point pos = Arguments.getPoint(context, ArgumentNames.POS).snapXZ(0.5d);
    int radius = Arguments.getInteger(context, RADIUS);
    CustomSphereProvider.add(pos, radius);

    CommandHelper.feedback(context, "bbor.commands.sphere.added",
            pos.getX(), pos.getY(), pos.getZ(), radius);
    return 0;
}
 
Example #16
Source File: SphereCommandBuilder.java    From BoundingBoxOutlineReloaded with MIT License 5 votes vote down vote up
static LiteralArgumentBuilder<CommandSource> build(String command) {
    return Commands.literal(command)
            .then(Commands.literal(ArgumentNames.ADD)
                    .then(Commands.argument(ArgumentNames.POS, Arguments.point())
                            .then(Commands.argument(RADIUS, Arguments.integer())
                                    .executes(SphereCommandBuilder::addSphere)))
                    .then(Commands.argument(RADIUS, Arguments.integer())
                            .executes(SphereCommandBuilder::addSphere)))
            .then(Commands.literal(ArgumentNames.CLEAR)
                    .executes(context -> {
                        CustomSphereProvider.clear();

                        CommandHelper.feedback(context, "bbor.commands.sphere.cleared.all");
                        return 0;
                    })
                    .then(Commands.argument(ArgumentNames.FROM, Arguments.coords())
                            .then(Commands.argument(ArgumentNames.TO, Arguments.coords())
                                    .executes(context -> {
                                        Point pos = Arguments.getPoint(context, ArgumentNames.POS).snapXZ(0.5d);
                                        boolean removed = CustomSphereProvider.remove(pos);

                                        String format = removed ? "bbor.commands.sphere.cleared" : "bbor.commands.sphere.notFound";
                                        CommandHelper.feedback(context, format,
                                                pos.getX(), pos.getY(), pos.getZ());
                                        return 0;
                                    }))));
}
 
Example #17
Source File: Arguments.java    From BoundingBoxOutlineReloaded with MIT License 5 votes vote down vote up
private static <T> T getArgumentValueOrDefault(CommandContext<CommandSource> context,
                                               String name,
                                               ArgumentFetcher<T> getValue,
                                               Supplier<T> defaultValue) throws CommandSyntaxException {
    try {
        return getValue.get(context, name);
    } catch (IllegalArgumentException exception) {
        return defaultValue.get();
    }
}
 
Example #18
Source File: ForgeServerSparkPlugin.java    From spark with GNU General Public License v3.0 5 votes vote down vote up
private static String /* Nullable */[] processArgs(CommandContext<CommandSource> context) {
    String[] split = context.getInput().split(" ");
    if (split.length == 0 || !split[0].equals("/spark") && !split[0].equals("spark")) {
        return null;
    }

    return Arrays.copyOfRange(split, 1, split.length);
}
 
Example #19
Source File: ForgeServerSparkPlugin.java    From spark with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int run(CommandContext<CommandSource> context) throws CommandSyntaxException {
    String[] args = processArgs(context);
    if (args == null)
        return 0;
    this.platform.executeCommand(new ForgeCommandSender(context.getSource().source, this), args);
    return Command.SINGLE_SUCCESS;
}
 
Example #20
Source File: ForgeServerSparkPlugin.java    From spark with GNU General Public License v3.0 5 votes vote down vote up
@Override
public CompletableFuture<Suggestions> getSuggestions(CommandContext<CommandSource> context, SuggestionsBuilder builder) throws CommandSyntaxException {
    String[] args = processArgs(context);
    if (args == null) {
        return Suggestions.empty();
    }

    ServerPlayerEntity player = context.getSource().asPlayer();
    return CompletableFuture.supplyAsync(() -> {
        for (String suggestion : this.platform.tabCompleteCommand(new ForgeCommandSender(player, this), args)) {
            builder.suggest(suggestion);
        }
        return builder.build();
    });
}
 
Example #21
Source File: ServerCommandConfig.java    From Better-Sprinting with Mozilla Public License 2.0 5 votes vote down vote up
public static void register(CommandDispatcher<CommandSource> dispatcher){
	LiteralArgumentBuilder<CommandSource> builder = literal("bettersprinting").requires(source -> source.hasPermissionLevel(3));
	
	builder.executes(ServerCommandConfig::execHelp);
	builder.then(literal("info").executes(ServerCommandConfig::execInfo));
	builder.then(literal("disablemod").then(argument(ARG_BOOLEAN, bool()).executes(ServerCommandConfig::execDisableMod)));
	builder.then(literal("setting").then(argument(ARG_SETTINGS, word()).suggests(SUGGEST_SETTING).then(argument(ARG_BOOLEAN, bool()).executes(ServerCommandConfig::execSetting))));
	
	dispatcher.register(builder);
}
 
Example #22
Source File: ServerCommandConfig.java    From Better-Sprinting with Mozilla Public License 2.0 5 votes vote down vote up
private static int execHelp(CommandContext<CommandSource> ctx){
	CommandSource source = ctx.getSource();
	sendMessage(source, TextFormatting.GREEN + "[Better Sprinting]");
	sendMessage(source, "/bettersprinting info");
	sendMessage(source, "/bettersprinting disablemod <" + ARG_BOOLEAN + ">");
	sendMessage(source, "/bettersprinting setting <" + ARG_SETTINGS + "> <" + ARG_BOOLEAN + ">");
	return 0;
}
 
Example #23
Source File: ServerCommandConfig.java    From Better-Sprinting with Mozilla Public License 2.0 5 votes vote down vote up
private static void sendMessageTranslated(CommandSource source, String translationName, boolean log){
	Entity entity = source.getEntity();
	
	if (entity instanceof PlayerEntity && ServerNetwork.hasBetterSprinting((PlayerEntity)entity)){
		source.sendFeedback(new TranslationTextComponent(translationName), log);
	}
	else{
		source.sendFeedback(new StringTextComponent(LanguageMap.getInstance().translateKey(translationName)), log);
	}
}
 
Example #24
Source File: ServerCommandConfig.java    From Better-Sprinting with Mozilla Public License 2.0 5 votes vote down vote up
private static int execDisableMod(CommandContext<CommandSource> ctx){
	BetterSprintingMod.config.set(ServerSettings.disableClientMod, ctx.getArgument(ARG_BOOLEAN, Boolean.class));
	BetterSprintingMod.config.save();
	
	CommandSource source = ctx.getSource();
	sendMessageTranslated(source, ServerSettings.disableClientMod.get() ? "bs.command.disableMod" : "bs.command.enableMod", true);
	ServerNetwork.sendToAll(source.getServer().getPlayerList().getPlayers(), ServerNetwork.writeDisableMod(ServerSettings.disableClientMod.get()));
	return 0;
}
 
Example #25
Source File: ServerCommandConfig.java    From Better-Sprinting with Mozilla Public License 2.0 4 votes vote down vote up
private static void sendMessage(CommandSource source, String text){
	source.sendFeedback(new StringTextComponent(text), false);
}
 
Example #26
Source File: ForgeMod.java    From BlueMap with MIT License 4 votes vote down vote up
public Commands<CommandSource> getCommands() {
	return commands;
}
 
Example #27
Source File: SpawningSphereCommand.java    From BoundingBoxOutlineReloaded with MIT License 4 votes vote down vote up
public static int setSphere(CommandContext<CommandSource> context) throws CommandSyntaxException {
    SpawningSphereProvider.setSphere(Arguments.getPoint(context, ArgumentNames.POS).snapXZ(0.5d));

    CommandHelper.feedback(context, "bbor.commands.spawningSphere.set");
    return 0;
}
 
Example #28
Source File: ServerCommandConfig.java    From Better-Sprinting with Mozilla Public License 2.0 4 votes vote down vote up
private static int execInfo(CommandContext<CommandSource> ctx){
	CommandSource source = ctx.getSource();
	sendMessageTranslated(source, "bs.command.info", false);
	return 0;
}
 
Example #29
Source File: Arguments.java    From BoundingBoxOutlineReloaded with MIT License 4 votes vote down vote up
public static boolean getBool(CommandContext<CommandSource> context, String name) throws CommandSyntaxException {
    return getArgumentValueOrDefault(context, name, BoolArgumentType::getBool, () -> false);
}
 
Example #30
Source File: Arguments.java    From BoundingBoxOutlineReloaded with MIT License 4 votes vote down vote up
public static Coords getCoords(CommandContext<CommandSource> context, String name) throws CommandSyntaxException {
    return new Coords(getArgumentValueOrDefault(context, name, Vec3Argument::getVec3, () -> context.getSource().getPos()));
}