从另一个类文件运行Java .class文件(更具体)

时间:2013-08-01 23:27:21

标签: java class

我想在adminchat.java中运行AsyncPlayerChatEvent.java,特别是当人员输入命令/ ac时,它会使用AsycPlayerChatEvent.java来处理它。我用谷歌搜索它但它似​​乎没有成功,我尝试添加AsyncPlayerChatEvent.AsyncPlayerChatEvent();但它没有用,这是代码。

adminchat.java

    package alo.adminchat;

import java.util.IllegalFormatException;
import java.util.Set;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;

import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;

import org.bukkit.plugin.java.JavaPlugin;

public final class adminchat extends JavaPlugin
{

     @Override
        public void onEnable()
     {
         System.out.println("Adminchat by Alo k2ivitus!");
            // TODO Insert logic to be performed when the plugin is enabled
     }

        @Override
        public void onDisable()
        {
            System.out.println("Adminchat by Alo sulgus!");
            // TODO Insert logic to be performed when the plugin is disabled
        }
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        {
            if(cmd.getName().equalsIgnoreCase("achelp"))
            { // If the player typed /ac then do the following...
                // doSomething
                return false;
            } //If this has happened the function will return true. 
                // If this hasn't happened the a value of false will be returned.
            return false; 
        }
        public boolean onCommand2(CommandSender sender, Command cmd, String label, String[] args)
        {
            if(cmd.getName().equalsIgnoreCase("ac"))
            { // If the player typed /ac then do the following...
                AsyncPlayerChatEvent.AsyncPlayerChatEvent(); //This is what needs fixing
                 return true;
            } //If this has happened the function will return true. 
                // If this hasn't happened the a value of false will be returned.
            return false; 
        }


}

AsyncPlayerChatEvent.java

package alo.adminchat;

import java.util.IllegalFormatException;
import java.util.Set;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;

 public class AsyncPlayerChatEvent extends PlayerEvent implements Cancellable {
     private static final HandlerList handlers = new HandlerList();
     private boolean cancel = false;
     private String message;
     private String format = "<%1$s> %2$s";
     private final boolean recipients;


     public AsyncPlayerChatEvent(final boolean async, final Player who, final String message, final Set<Player> players) {
         super(who, async);
         this.message = message;
        recipients = player.hasPermission("adminchat.use");
     }

     public String getMessage() {
         return message;
     }

     public void setMessage(String message) {
         this.message = message;
     }

     public String getFormat() {
         return format;
     }

     public void setFormat(final String format) throws IllegalFormatException, NullPointerException {
         // Oh for a better way to do this!
         try {
             String.format(format, player, message);
         } catch (RuntimeException ex) {
             ex.fillInStackTrace();
             throw ex;
         }

         this.format = format;
     }

     public boolean getRecipients() {
         return recipients;
     }

     public boolean isCancelled() {
         return cancel ;
     }

     public void setCancelled(boolean cancel) {
         this.cancel = cancel;
     }

     @Override
     public HandlerList getHandlers() {
         return handlers;
     }

     public static HandlerList getHandlerList() {
         return handlers;
     }
 }

2 个答案:

答案 0 :(得分:3)

我认为你的意思是你想创建一个AsyncPlayerChatEvent的实例吗?

new AsyncPlayerChatEvent();

但你需要传递你的论点:

boolean async, Player who, String message, Set<Player> players

答案 1 :(得分:0)

您应该使用AsyncPlayerChatEvent.AsyncPlayerChatEvent()而不是使用new AsyncPlayerChatEvent()调用构造函数。这将创建一个新的AsyncPlayerChatEvent并在其上调用构造函数。