Reputation: 31
I'm trying to learn Java & Bukkit at the same time ( I have learnt some basic Java previously a few months back and made a simple text game).
I know what I'm doing when I'm using 1 class but, as you may know, everything became messy so I'm re-coding everything but attempting to make it a lot cleaner but I've come across a problem, apart from the main class, no other classes work, here is my code.
MAIN CLASS:
package me.Camaloony.RoyalCosmetics;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.event.Listener;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import net.md_5.bungee.api.ChatColor;
public class Core extends JavaPlugin implements Listener {
Logger myPluginLogger = Bukkit.getLogger();
@Override
public void onEnable()
{
PluginManager manager = getServer().getPluginManager();
manager.registerEvents(this, this);
Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.RED + "=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "Royal Cosmetics has been enabled!");
Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.RED + "=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
}
@Override
public void onDisable()
{
myPluginLogger.info("Test 1 has been disabled!");
myPluginLogger.severe("Test 1 has an error and has not loaded.");
}
}
Here is my 2ND CLASS
package me.Camaloony.RoyalCosmetics;
import java.util.ArrayList;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
public class MainGUI implements Listener {
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
Player player = (Player) sender;
if (commandLabel.equalsIgnoreCase("Cosmetics")) {
createMainMenu(player);
}
return false;
}
@EventHandler
public void onInvClick(InventoryClickEvent event){
if (event.getCurrentItem().getItemMeta().getDisplayName().contains("Particles GUI")) {
event.setCancelled(true);
}
}
public void createMainMenu(Player player) {
Inventory inv = Bukkit.getServer().createInventory(null, 45, "Royal Cosmetics");
ItemStack item1 = new ItemStack(Material.REDSTONE);
ItemMeta item1Meta = item1.getItemMeta();
ArrayList<String> item1Lore = new ArrayList<String>();
item1Lore.add("Click here to go to the particles GUI");
item1Meta.setDisplayName("Particles GUI");
item1Meta.setLore(item1Lore);
item1.setItemMeta(item1Meta);
inv.setItem(0, item1);
player.openInventory(inv);
}
}
How would I make it so the 2nd class is actually 'read' ?
Thanks a lot!
Upvotes: 0
Views: 1497
Reputation: 354
Just add manager.registerEvents(new MainGUI(), this);
to your onEnable() in Core.
Upvotes: 1
Reputation: 125
You need to do two things. onCommand is not checked for non-plugin classes (and your second class should not extend plugin). So just add the onCommand method in your main class and have it do nothing but call the onCommand method in your second class. Second of all, make sure your second class is calling Bukkit.getPluginManager().registerEvents();. For this I recommend adding a static variable called "plugin" which is a JavaPlugin in your main class. You should have the main plugin do "plugin = this;" when enabled. Then you can use that variable for the argument in registerEvents(). That should fix your problem.
Upvotes: 0
Reputation: 1271
Since your second (non-main) class is a Listener
, you can register an instance of that class as well to listen to the events, similar to what you do with your main class:
manager.registerEvents(new MainGUI(), this);
The onCommand
method in your MainGUI
class however will not do much as far as I can tell. When you declare an onCommand
method with return type boolean
in your main class, you are overriding that method from the JavaPlugin
class which your main class extends (that method is then invoked from somewhere else), whereas the onCommand
method in your MainGUI
class won't be used unless you yourself invoke it somewhere. If you want to implement command behavior outside of the main class you can use the CommandExecutor
class as shown here.
Upvotes: 1