Alo Sepp
Alo Sepp

Reputation: 19

Run Java .class file from another class file (more specific)

I would like to run AsyncPlayerChatEvent.java inside adminchat.java, specifically when person types command /ac it would use AsycPlayerChatEvent.java to handle it. ive googled it but it doesnt seem to work out, i tried adding AsyncPlayerChatEvent.AsyncPlayerChatEvent(); but it didnt work, here's the code.

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;
     }
 }

Upvotes: 1

Views: 809

Answers (2)

Stephen Lake
Stephen Lake

Reputation: 1620

I think what you mean is you want create an instance of AsyncPlayerChatEvent?

new AsyncPlayerChatEvent();

But you need to pass in your arguments:

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

Upvotes: 3

tbodt
tbodt

Reputation: 16987

Instead of calling the constructor with AsyncPlayerChatEvent.AsyncPlayerChatEvent(), you should use new AsyncPlayerChatEvent() instead. This will create a new AsyncPlayerChatEvent and call the constructor on it.

Upvotes: 0

Related Questions