Solved cant load main class in commands class

Discussion in 'Spigot Plugin Development' started by tntbaz, Mar 10, 2020.

  1. here's my code
    Loader:
    Code (Java):
    package me.dodolmanager;

    import org.bukkit.Bukkit;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;

    import me.dodolmanager.CustomItems.Custom;
    import me.dodolmanager.Events.EventsClass;
    import net.md_5.bungee.api.ChatColor;

    public class Loader extends JavaPlugin implements Listener {
        private Commands commands = new Commands();
        public void onEnable() {
            Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "\n\n\nServer Kiri Started\n\n\n");
            Bukkit.getPluginManager().registerEvents(new EventsClass(), this);
            Custom items = new Custom();
            if(getConfig().getBoolean("customitem")) items.customitem(); // load custom item
            getCommand(commands.cmd1).setExecutor(commands);
            loadconfig();
        }
        public void onDisable() {
            Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "\n\n\nServer Kiri Stopped\n\n\n");
        }
        public void loadconfig() {
            saveDefaultConfig();
            //getConfig().options().copyDefaults(true);
            //saveConfig();
        }
    }
     
    Commands:
    Code (Java):
    package me.dodolmanager;

    import java.util.ArrayList;

    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.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemFlag;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.Plugin;

    import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader;

    import net.md_5.bungee.api.ChatColor;
    import net.minecraft.server.v1_12_R1.CommandExecute;

    public class Commands extends CommandExecute implements Listener, CommandExecutor {
        String prefix = ChatColor.RED + "[" + ChatColor.GOLD + "DodolManager" + ChatColor.RED +  "] ";
        public String cmd1 = "godpotato";
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(sender instanceof Player) {
                if(cmd.getName() == cmd1) {
                    Player plr = ((Player) sender).getPlayer();
                    plr.setHealth(0.5);
                    Bukkit.getServer().broadcastMessage(cmd.getName() + prefix + org.bukkit.ChatColor.LIGHT_PURPLE + plr.getName() + ChatColor.YELLOW + " Talash kard Ta Bezane /godpotato");
                    return true;
                   
                }
                return true;
            }else {
                Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "koskesh to ke player nisti bache koni");
                return true;
            }
        }
        public ItemStack getgodpotato() {
            ItemStack item = new ItemStack(Material.POTATO_ITEM);
            ItemMeta meta = item.getItemMeta();
            ArrayList<String> lore = new ArrayList<String>();
            lore.add("God Of Potatoes -_-");
           
            meta.setLore(lore);
            meta.setDisplayName("GOD-POTATO");
            meta.addEnchant(Enchantment.DAMAGE_ALL, 20, true);
            meta.addEnchant(Enchantment.KNOCKBACK, 40, true);
            meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
            item.setItemMeta(meta);
            return item;
        }
    }

     
     
  2. What do you mean you can't load the main class in the commands class?
    Why does your Commands class implement Listener if there are no events in it? Also, what's up with extending CommandExecute? There doesn't seem to be any events in your main class either, so why implement Listener there too?
     
  3. i mean i want to insert this code but i cant insert it
    Code (Java):
    Loader main = Loader.getPlugin(Loader.class);
     
  4. i mean i want to change the plugin config file but i have to insert that code to use config right? like this
    Code (Java):
    config.set("tplogin.enabled", true);
                main.getConfig.set("tplogin.world",event.getPlayer().getWorld().getName());
                main.getConfig.set("tplogin.x",event.getPlayer().getLocation().getX());
                main.getConfig.set("tplogin.y",event.getPlayer().getLocation().getY());
                main.getConfig.set("tplogin.z",event.getPlayer().getLocation().getZ());
                main.saveConfig();
     
  5. getPlugin is not how you get your plugin. You should pass an instance of your main class to a constructor in your Commands class to access the config. The part where you are setting config values has been done right, only if you've stored the passed instance in a variable named "main". I also recommend cleaning up your code a bit - like storing event.getPlayer().getLocation() and then using something like loc.getX() when setting the config values.
     
  6. DavidDevelops

    Supporter

    Ahhh, I hate being "That guy" but its pretty clear you don't understand basic Java nor coding principles in general, You are very repetitive with the code and are Implementing things for no reason, You don't SEEM (Not trying to judge your knowledge) to understand how any of this works, Almost like you're just following a tutorial but I'd suggest taking a quick java crash course (They are extremely easy to find) It'll take an hour or two at most and you'll understand a lot more and have an idea of what you are asking at the very least.
    Sorry if this came off rude or snarky it's just not very easy to explain if people don't understand.
     
  7. can u say an example? i used these things because it said inside the video tutorial
     
  8. i know java but i just did just like the video said
     
  9. DavidDevelops

    Supporter

    If you know java you shouldn't follow the bad practice part of the video but respectable then I guess, Sorry for the inconvenience, No hard feelings.
     
  10. can you tell me a good video tutorial?
     
  11. umm i think i found the problem i just imported the
    Code (Java):
    import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader;
    instead of
    Code (Java):
    import me.dodolmanager.Loader;
     
  12. but can you tell me a good video tutorial?
     
  13. That still is not how you do it..
    Use
    Code (Text):
    server#getPluginCommand("cmdhere").setExecutor(new command class here)
    https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Server.html#getPluginCommand-java.lang.String-
    Now, pass an instance of your main class to the constructor of your command class. (You must actually make one first)