Java Code Examples for com.mojang.brigadier.context.CommandContext#getSource()

The following examples show how to use com.mojang.brigadier.context.CommandContext#getSource() . 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: 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 2
Source File: EntityArgumentType_1_12_2.java    From multiconnect with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
    if (!(context.getSource() instanceof CommandSource))
        return builder.buildFuture();

    StringReader reader = new StringReader(builder.getInput());
    reader.setCursor(builder.getStart());

    CompletableFuture<Suggestions> playerCompletions;
    if ((reader.canRead() && reader.peek() == '@') || !suggestPlayerNames) {
        playerCompletions = Suggestions.empty();
    } else {
        playerCompletions = ((CommandSource) context.getSource()).getCompletions((CommandContext<CommandSource>) context, builder.restart());
    }

    EntitySelectorParser parser = new EntitySelectorParser(reader, singleTarget, playersOnly);
    try {
        parser.parse();
    } catch (CommandSyntaxException ignore) {
    }
    CompletableFuture<Suggestions> selectorCompletions = parser.suggestor.apply(builder.restart());

    return CompletableFuture.allOf(playerCompletions, selectorCompletions)
            .thenCompose(v -> UnionArgumentType.mergeSuggestions(playerCompletions.join(), selectorCompletions.join()));
}
 
Example 3
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 4
Source File: ScriptCommand.java    From fabric-carpet with MIT License 5 votes vote down vote up
private static int listGlobals(CommandContext<ServerCommandSource> context, boolean all)
{
    CarpetScriptHost host = getHost(context);
    ServerCommandSource source = context.getSource();

    Messenger.m(source, "lb Stored functions"+((host == CarpetServer.scriptServer.globalHost)?":":" in "+host.getName()+":"));
    host.globaFunctionNames(host.main, (str) -> all || !str.startsWith("__")).sorted().forEach( (s) -> {
        FunctionValue fun = host.getFunction(s);
        if (fun == null)
        {
            Messenger.m(source, "gb "+s, "g  - unused import");
            Messenger.m(source, "gi ----------------");
            return;
        }
        Expression expr = fun.getExpression();
        Tokenizer.Token tok = fun.getToken();
        List<String> snippet = expr.getExpressionSnippet(tok);
        Messenger.m(source, "wb "+fun.fullName(),"t  defined at: line "+(tok.lineno+1)+" pos "+(tok.linepos+1));
        for (String snippetLine: snippet)
        {
            Messenger.m(source, "w "+snippetLine);
        }
        Messenger.m(source, "gi ----------------");
    });
    //Messenger.m(source, "w "+code);
    Messenger.m(source, "w  ");
    Messenger.m(source, "lb Global variables"+((host == CarpetServer.scriptServer.globalHost)?":":" in "+host.getName()+":"));
    host.globaVariableNames(host.main, (s) -> s.startsWith("global_")).sorted().forEach( (s) -> {
        LazyValue variable = host.getGlobalVariable(s);
        if (variable == null)
        {
            Messenger.m(source, "gb "+s, "g  - unused import");
        }
        else
        {
            Messenger.m(source, "wb "+s+": ", "w "+ variable.evalValue(null).getPrettyString());
        }
    });
    return 1;
}
 
Example 5
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 6
Source File: ScriptCommand.java    From fabric-carpet with MIT License 5 votes vote down vote up
private static int compute(CommandContext<ServerCommandSource> context, String expr)
{
    ServerCommandSource source = context.getSource();
    CarpetScriptHost host = getHost(context);
    handleCall(source, host, () -> {
        CarpetExpression ex = new CarpetExpression(host.main, expr, source, new BlockPos(0, 0, 0));
        return ex.scriptRunCommand(host, new BlockPos(source.getPosition()));
    });
    return 1;
}
 
Example 7
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 8
Source File: NMS_1_13.java    From 1.13-Command-API with Apache License 2.0 4 votes vote down vote up
private CommandListenerWrapper getCLW(CommandContext cmdCtx) {
    return (CommandListenerWrapper) cmdCtx.getSource();
}
 
Example 9
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 10
Source File: NMS_1_13_1.java    From 1.13-Command-API with Apache License 2.0 4 votes vote down vote up
private CommandListenerWrapper getCLW(CommandContext cmdCtx) {
    return (CommandListenerWrapper) cmdCtx.getSource();
}
 
Example 11
Source File: NMS_1_14_3.java    From 1.13-Command-API with Apache License 2.0 4 votes vote down vote up
private CommandListenerWrapper getCLW(CommandContext cmdCtx) {
    return (CommandListenerWrapper) cmdCtx.getSource();
}
 
Example 12
Source File: NMS_1_14_4.java    From 1.13-Command-API with Apache License 2.0 4 votes vote down vote up
private CommandListenerWrapper getCLW(CommandContext cmdCtx) {
    return (CommandListenerWrapper) cmdCtx.getSource();
}
 
Example 13
Source File: NMS_1_13_2.java    From 1.13-Command-API with Apache License 2.0 4 votes vote down vote up
private CommandListenerWrapper getCLW(CommandContext cmdCtx) {
    return (CommandListenerWrapper) cmdCtx.getSource();
}
 
Example 14
Source File: NMS_1_15.java    From 1.13-Command-API with Apache License 2.0 4 votes vote down vote up
private CommandListenerWrapper getCLW(CommandContext cmdCtx) {
	return (CommandListenerWrapper) cmdCtx.getSource();
}
 
Example 15
Source File: NMS_1_14.java    From 1.13-Command-API with Apache License 2.0 4 votes vote down vote up
private CommandListenerWrapper getCLW(CommandContext cmdCtx) {
	return (CommandListenerWrapper) cmdCtx.getSource();
}
 
Example 16
Source File: NMS_1_16_R1.java    From 1.13-Command-API with Apache License 2.0 4 votes vote down vote up
private CommandListenerWrapper getCLW(CommandContext cmdCtx) {
	return (CommandListenerWrapper) cmdCtx.getSource();
}
 
Example 17
Source File: ScriptCommand.java    From fabric-carpet with MIT License 4 votes vote down vote up
private static int scriptScan(CommandContext<ServerCommandSource> context, BlockPos origin, BlockPos a, BlockPos b, String expr)
{
    ServerCommandSource source = context.getSource();
    CarpetScriptHost host = getHost(context);
    BlockBox area = new BlockBox(a, b);
    CarpetExpression cexpr = new CarpetExpression(host.main, expr, source, origin);
    int int_1 = area.getBlockCountX() * area.getBlockCountY() * area.getBlockCountZ();
    if (int_1 > CarpetSettings.fillLimit)
    {
        Messenger.m(source, "r too many blocks to evaluate: " + int_1);
        return 1;
    }
    int successCount = 0;
    CarpetSettings.impendingFillSkipUpdates = !CarpetSettings.fillUpdates;
    try
    {
        for (int x = area.minX; x <= area.maxX; x++)
        {
            for (int y = area.minY; y <= area.maxY; y++)
            {
                for (int z = area.minZ; z <= area.maxZ; z++)
                {
                    try
                    {
                        if (cexpr.fillAndScanCommand(host, x, y, z)) successCount++;
                    }
                    catch (ArithmeticException ignored)
                    {
                    }
                }
            }
        }
    }
    catch (CarpetExpressionException exc)
    {
        host.handleErrorWithStack("Error while processing command", exc);
        return 0;
    }
    finally
    {
        CarpetSettings.impendingFillSkipUpdates = false;
    }
    Messenger.m(source, "w Expression successful in " + successCount + " out of " + int_1 + " blocks");
    return successCount;

}
 
Example 18
Source File: DrawCommand.java    From fabric-carpet with MIT License 4 votes vote down vote up
private static int drawPrism(CommandContext<ServerCommandSource> ctx, String base){
    BlockPos pos;
    double radius;
    int height;
    String orientation;
    BlockStateArgument block;
    Predicate<CachedBlockPosition> replacement;
    try
    {
        pos = getArg(ctx, BlockPosArgumentType::getBlockPos, "center");
        radius = getArg(ctx, IntegerArgumentType::getInteger, "radius")+0.5D;
        height = getArg(ctx, IntegerArgumentType::getInteger, "height");
        orientation = getArg(ctx, StringArgumentType::getString,"orientation");
        block = getArg(ctx, BlockStateArgumentType::getBlockState, "block");
        replacement = getArg(ctx, BlockPredicateArgumentType::getBlockPredicate, "filter", true);
    }
    catch (ErrorHandled | CommandSyntaxException ignored) { return 0; }

    ServerCommandSource source = ctx.getSource();

    int affected = 0;
    BlockPos.Mutable mbpos = new BlockPos.Mutable(pos);

    List<BlockPos> list = Lists.newArrayList();

    ServerWorld world = source.getWorld();

    CarpetSettings.impendingFillSkipUpdates = !CarpetSettings.fillUpdates;

    boolean isSquare = base.equalsIgnoreCase("square");

    for(int i =0; i<height;++i)
    {
        affected+= fillFlat(world, pos, i, radius, isSquare, orientation, block, replacement, list, mbpos);
    }

    CarpetSettings.impendingFillSkipUpdates = false;

    if (CarpetSettings.fillUpdates) {

        for (BlockPos blockpos1 : list) {
            Block blokc = world.getBlockState(blockpos1).getBlock();
            world.updateNeighbors(blockpos1, blokc);
        }
    }

    Messenger.m(source, "gi Filled " + affected + " blocks");

    return affected;
}
 
Example 19
Source File: DrawCommand.java    From fabric-carpet with MIT License 4 votes vote down vote up
private static int drawPyramid(CommandContext<ServerCommandSource> ctx, String base, boolean solid) throws CommandSyntaxException
{
    BlockPos pos;
    double radius;
    int height;
    boolean pointup;
    String orientation;
    BlockStateArgument block;
    Predicate<CachedBlockPosition> replacement;
    try
    {
        pos = getArg(ctx, BlockPosArgumentType::getBlockPos, "center");
        radius = getArg(ctx, IntegerArgumentType::getInteger, "radius")+0.5D;
        height = getArg(ctx, IntegerArgumentType::getInteger, "height");
        pointup = getArg(ctx, StringArgumentType::getString, "pointing").equalsIgnoreCase("up");
        orientation = getArg(ctx, StringArgumentType::getString,"orientation");
        block = getArg(ctx, BlockStateArgumentType::getBlockState, "block");
        replacement = getArg(ctx, BlockPredicateArgumentType::getBlockPredicate, "filter", true);
    }
    catch (ErrorHandled ignored) { return 0; }

    ServerCommandSource source = ctx.getSource();

    int affected = 0;
    BlockPos.Mutable mbpos = new BlockPos.Mutable(pos);

    List<BlockPos> list = Lists.newArrayList();

    ServerWorld world = source.getWorld();

    CarpetSettings.impendingFillSkipUpdates = !CarpetSettings.fillUpdates;

    boolean isSquare = base.equalsIgnoreCase("square");

    for(int i =0; i<height;++i)
    {
        double r = pointup ? radius - radius * i / height - 1 : radius * i / height;
        affected+= fillFlat(world, pos, i, r, isSquare, orientation, block, replacement, list, mbpos);
    }
    
    CarpetSettings.impendingFillSkipUpdates = false;

    if (CarpetSettings.fillUpdates) {

        for (BlockPos blockpos1 : list) {
            Block blokc = world.getBlockState(blockpos1).getBlock();
            world.updateNeighbors(blockpos1, blokc);
        }
    }

    Messenger.m(source, "gi Filled " + affected + " blocks");

    return affected;
}
 
Example 20
Source File: DrawCommand.java    From fabric-carpet with MIT License 4 votes vote down vote up
private static int drawDiamond(CommandContext<ServerCommandSource> ctx, boolean solid) throws CommandSyntaxException
{
    BlockPos pos;
    int radius;
    BlockStateArgument block;
    Predicate<CachedBlockPosition> replacement;
    try
    {
        pos = getArg(ctx, BlockPosArgumentType::getBlockPos, "center");
        radius = getArg(ctx, IntegerArgumentType::getInteger, "radius");
        block = getArg(ctx, BlockStateArgumentType::getBlockState, "block");
        replacement = getArg(ctx, BlockPredicateArgumentType::getBlockPredicate, "filter", true);
    }
    catch (ErrorHandled ignored) { return 0; }

    ServerCommandSource source = ctx.getSource();

    int affected=0;

    BlockPos.Mutable mbpos = new BlockPos.Mutable(pos);
    List<BlockPos> list = Lists.newArrayList();

    ServerWorld world = source.getWorld();

    CarpetSettings.impendingFillSkipUpdates = !CarpetSettings.fillUpdates;

    for (int r = 0; r < radius; ++r)
    {
        int y=r-radius+1;
        for (int x = -r; x <= r; ++x)
        {
            int z=r-Math.abs(x);

            affected+= setBlock(world, mbpos, pos.getX()+x, pos.getY()-y, pos.getZ()+z, block, replacement, list);
            affected+= setBlock(world, mbpos, pos.getX()+x, pos.getY()-y, pos.getZ()-z, block, replacement, list);
            affected+= setBlock(world, mbpos, pos.getX()+x, pos.getY()+y, pos.getZ()+z, block, replacement, list);
            affected+= setBlock(world, mbpos, pos.getX()+x, pos.getY()+y, pos.getZ()-z, block, replacement, list);
        }
    }

    CarpetSettings.impendingFillSkipUpdates = false;

    if (CarpetSettings.fillUpdates)
    {
        list.forEach(p -> world.updateNeighbors(p, world.getBlockState(p).getBlock()));
    }

    Messenger.m(source, "gi Filled " + affected + " blocks");

    return affected;
}