Jump to content

[1.15.2] Command tree examples


squidlex

Recommended Posts

I'm looking to update a command tree I made for 1.12 to 1.15.2.

Obviously Commands have changed to the CommandDispatcher now, so does anybody know of any good/simple source code I could look at for a basic command tree?

 

Thanks for your help!

Link to comment
Share on other sites

Update:

I'm definitely misunderstanding this. Here's my code:

public class Command {

	public static void register(CommandDispatcher<CommandSource> dispatcher) {

		final LiteralArgumentBuilder<CommandSource> root = Commands.literal("commandtree");
		root.then(Commands.literal("edit_a_float").then(Commands.argument("float", FloatArgumentType.floatArg()))).executes(source -> {
			return editPower(source.getSource(), source.getSource().asPlayer(), FloatArgumentType.getFloat(source, "float"));
			}).then(Commands.argument("player", EntityArgument.player())).executes(source -> {
			return editPower(source.getSource(), EntityArgument.getPlayer(source, "player"), FloatArgumentType.getFloat(source, "float"));
			});	
		root.then(Commands.literal("edit_a_cooldown").then(Commands.argument("ticks", IntegerArgumentType.integer()))).executes(source -> {
			return editCooldown(source.getSource(), source.getSource().asPlayer(), IntegerArgumentType.getInteger(source, "ticks"));
			}).then(Commands.argument("player", EntityArgument.player())).executes(source -> {
			return editCooldown(source.getSource(), EntityArgument.getPlayer(source, "player"), IntegerArgumentType.getInteger(source, "ticks"));
			});
		dispatcher.register(root);
	}

	private static int editPower(CommandSource source, PlayerEntity player, float amount) {
		System.out.println("Adjusted power by " + amount + "!");
		return 1;
	}
	
	private static int editCooldown(CommandSource source, PlayerEntity player, int ticks) {
		System.out.println("Adjusted cooldown by " + ticks + "!");
		return 1;
	}
}

 

When entering my command, edit_a_float and edit_a_cooldown are recommended, but it also recommends usernames and all the @s.

 

When typing in a command I get the message 'Unknown Command' or 'Incorrect argument for command'.

Please could someone explain to me where I'm going wrong?

Edited by squidlex
Link to comment
Share on other sites

Because that's how commands work. @p and not supplying a target means that it affects the player typing the command. But what if you wanted to affect a different player?

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

33 minutes ago, Draco18s said:

Because that's how commands work. @p and not supplying a target means that it affects the player typing the command. But what if you wanted to affect a different player?

Thanks for your reply, but I'm now seeing either I worded my question poorly or I don't quite understand your reply.

It doesn't sugget @p or anything like that at all when I type /commandtree edit_a_float but it does when I type /commandtree.

 

When I type /commandtree edit_a_float and then type anything after that it returns Unknown Command.

Link to comment
Share on other sites

I misunderstood the problem.  I am not sure, then.

  • Like 1

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

If my understanding is correct, .executes() needs to put inside the bracket of the .then (all "connected" action needs to do that)which should look like this

		root.then(Commands.literal("edit_a_float")
                  .then(Commands.argument("float", FloatArgumentType.floatArg()) -> removed two brackets here
          .executes(source -> {
			return editPower(source.getSource(), source.getSource().asPlayer(), FloatArgumentType.getFloat(source, "float"));
			}))); -> added two brackets here

 

Edited by poopoodice
  • Thanks 1
Link to comment
Share on other sites

1 hour ago, poopoodice said:

If my understanding is correct, .executes() needs to put inside the bracket of the .then (all "connected" action needs to do that)which should looks like this


		root.then(Commands.literal("edit_a_float")
                  .then(Commands.argument("float", FloatArgumentType.floatArg()) -> removed two brackets here
          .executes(source -> {
			return editPower(source.getSource(), source.getSource().asPlayer(), FloatArgumentType.getFloat(source, "float"));
			}))); -> added two brackets here

 

That was the problem!

Thanks for spotting that, I'm really bad at spotting little errors like this.

 

Thank you so much I can finally finish porting my mod! :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.