Jump to content

[1.10.2] Command Help.


Differentiation

Recommended Posts

Hey!

So I wanted to make this command called "boom" and its purpose is to launch all players to the air. However, it doesn't seem to work :/ 

My code:

package me.mycustommod.command;

import net.minecraft.command.CommandBase;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.text.TextComponentString;

public class CommandBoom extends CommandBase {

	@Override
	public String getCommandName() {
		return "boom";
	}

	@Override
	public String getCommandUsage(ICommandSender sender) {
		return "/boom";
	}

	@Override
	public void execute(MinecraftServer server, ICommandSender sender, String[] args) {
	    if (sender instanceof EntityPlayer) {
	       	((EntityPlayer) sender).addChatMessage(new TextComponentString(TextFormatting.GREEN + "Command successfully executed"));
	       	((EntityPlayer) sender).addVelocity(0, 10, 0);
	    }
	}

	@Override
	public int getRequiredPermissionLevel() {
		return 0;
	}

	@Override
	public boolean checkPermission(MinecraftServer server, ICommandSender sender) {
		return sender instanceof EntityPlayer;
	}
}

 

*Yes, I did register the command.

 

So whenever I run the command, it only sends the message "Command successfully executed" to the player, but doesn't launch the players to the air. Please help.

Thanks!

Edited by Differentiation
Link to comment
Share on other sites

1. I believe you must do stuff by adding a scheduled task to server world:

 

@Override
    public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException {
        WorldServer world= (WorldServer) sender.getEntityWorld();
        world.addScheduledTask(() -> {
            EntityPlayer player= (EntityPlayer) sender.getCommandSenderEntity();
			player.addVelocity(0,3,0); //the value 10 is too much, players will be sent into sky with it
        });
    }

 

2. You act only on the player who ran the command. If you want to launch all players, retrieve them from world.playerEntities or from bounding box.

  • Like 1
Link to comment
Share on other sites

4 hours ago, Alexiy said:

1. I believe you must do stuff by adding a scheduled task to server world:

 

That's only necessary when the original method is being called on a thread other than the main client/server thread, e.g. packet handlers are run on a Netty thread.

 

Commands are run on the main server thread, so scheduling a task like that just runs it immediately on the same thread; making it completely pointless to do so.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

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.

×
×
  • Create New...

Important Information

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