字符串发送命令后,发送命令名称而不是文本。

huangapple go评论87阅读模式
英文:

string sends command name instead of text after command

问题

所以我试图执行广播命令,但它发送出命令名称,我不知道为什么?如果你有任何想法以及如何解决这个问题的方法

package ml.harrytubestudios.helloworld.commands;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;

import ml.harrytubestudios.helloworld.main;

public class bro implements CommandExecutor {
    private main plugins;
    
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

        Bukkit.broadcastMessage(label);
        return false;
    }
}
英文:

so I'm trying to do broadcast command but it sends out the command name I don't know why? if you have any idea why and how to solve this problem

package ml.harrytubestudios.helloworld.commands;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;

import ml.harrytubestudios.helloworld.main;

public class bro implements CommandExecutor {
private main plugins;
	@Override
	public boolean onCommand(CommandSender sender, Command cmd, String no, String[] args) {
 
		Bukkit.broadcastMessage(no);
return false;
	}
}

答案1

得分: 0

你的问题有两个方面。如果你查看CommandExecutor的文档;在Spigot和Bukkit中都是相同的,你会看到它对于onCommand的说明如下:

> 如果返回false,则将发送此命令的“usage”插件.yml条目(如果定义了)给玩家。

因为你返回了false,这意味着命令输入不正确,应该将用法字符串发送给CommandSender。如果命令成功执行,应该返回true。

然而,你应该仍然能看到你的broadcastMessage,这在文档中再次解释为第三个参数(label):

> 命令的别名

这意味着你正在广播命令的别名(你的no参数),而不是它们的参数,我想你是想要获取它们的参数。

为了获取使用的命令的参数,你需要使用args参数,它是一个字符串数组。你可能希望将其格式化为一个字符串,以供广播使用;有不同的解决方法可以参考。

英文:

Your problems here are twofold. If you look at the documentation for CommandExecutor; it's the same in Spigot as in Bukkit, you'll see that it says for onCommand:

> If false is returned, then the "usage" plugin.yml entry for this command (if defined) will be sent to the player.

Because you're returning false, you're saying that the command wasn't entered correctly and the usage string should be sent to the CommandSender. You should return true if the command executed successfully.

However, you should still see your broadcastMessage, which you are. This is explained again in the documentation as it says for the 3rd argument (label):

>Alias of the command which was used

This means that you are broadcasting the alias of the command (your no parameter) that the CommandSender used, rather than their arguments, which is what I presume you're after.

In order to get the arguments of the command used, you'll want the args parameter which is an array of Strings. You'll probably want to format this into one string for use with your broadcast; for which there are different solutions.

答案2

得分: 0

I found out the answer its that string is the command and the args is the arguments after the command so

我发现答案是,字符串是命令,args 是命令后面的参数,所以

package ga.harrytubestudios.helloworld.commands;

导入 org.bukkit.Bukkit;
导入 org.bukkit.Material;
导入 org.bukkit.command.Command;
导入 org.bukkit.command.CommandExecutor;
导入 org.bukkit.command.CommandSender;
导入 org.bukkit.entity.Player;
导入 org.bukkit.inventory.ItemStack;

导入 ga.harrytubestudios.helloworld.main;
导入 net.md_5.bungee.api.chat.ComponentBuilder;
导入 net.md_5.bungee.api.chat.HoverEvent;
导入 net.md_5.bungee.api.chat.TextComponent;

public class shopcommand implements CommandExecutor {
private main pluign;
@Override
public boolean onCommand(CommandSender Sender, Command smd, String label, String[] args) {

        Player p = (Player)Sender;

p.sendmessgae(args[0])

英文:

I found out the answer its that string is the command and the args is the arguments after the command so

    package ga.harrytubestudios.helloworld.commands;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import ga.harrytubestudios.helloworld.main;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;

public class shopcommand implements CommandExecutor {
private main pluign;
	@Override
	public boolean onCommand(CommandSender Sender, Command smd, String label, String[] args) {
		
	
			Player p = (Player)Sender;
p.sendmessgae(args[0])
 


}

huangapple
  • 本文由 发表于 2020年8月11日 18:42:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63356511.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定