I
I
ilyageekdays2019-11-17 16:33:32
Java
ilyageekdays, 2019-11-17 16:33:32

What is the error in the Minecraft plugin code?

In the game, instead of the message string, it displays null. It's near the end of the code.

package ru.zhigul.chat;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;

public class Commands implements CommandExecutor{
  
  private main plugin;
  protected String message;
  protected String arg;
  protected String name;
  public Commands(main plugin) {
    this.plugin = plugin;
  }

  @Override
  public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
       if(args.length == 0) {
         sender.sendMessage(ChatColor.RED + "Используй эту команду вот так: /try <action>");
         return false;
       }
       
       for(int i = 0; args.length == i; i=i+1)
       {	
         message = message + args[i] + " ";
       }
       	name = sender.getName().toString();
       	sender.sendMessage(ChatColor.DARK_PURPLE + "*" + name + " " + message); 
       	return true;
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-11-17
@ilyageekdays

Such a loop for(int i = 0; args.length == i; i=i+1)will never be executed, so the field messagewill not be initialized.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question