1.16.1 Necesito ayuda programando un plugin

Discussion in 'Spigot Plugin Development' started by Topocai, Aug 1, 2020 at 10:08 PM.

  1. Hola, es la primera vez que programo un plugin solo estoy haciendo uno de pruebas, pero al iniciar el sv me tira este error:


    Error occurred while enabling Testplugin v1.2 (Is it up to date?)
    java.lang.NullPointerException: null
    at topocai.testplugin.Testplugin.onEnable(Testplugin.java:24) ~[?:?]
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263) ~[spigot-1.16.1.jar:git-Spigot-9639cf7-8fb6585]
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:351) ~[spigot-1.16.1.jar:git-Spigot-9639cf7-8fb6585]
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:480) ~[spigot-1.16.1.jar:git-Spigot-9639cf7-8fb6585]
    at org.bukkit.craftbukkit.v1_16_R1.CraftServer.enablePlugin(CraftServer.java:493) ~[spigot-1.16.1.jar:git-Spigot-9639cf7-8fb6585]
    at org.bukkit.craftbukkit.v1_16_R1.CraftServer.enablePlugins(CraftServer.java:407) ~[spigot-1.16.1.jar:git-Spigot-9639cf7-8fb6585]
    at net.minecraft.server.v1_16_R1.MinecraftServer.loadWorld(MinecraftServer.java:438) ~[spigot-1.16.1.jar:git-Spigot-9639cf7-8fb6585]
    at net.minecraft.server.v1_16_R1.DedicatedServer.init(DedicatedServer.java:219) ~[spigot-1.16.1.jar:git-Spigot-9639cf7-8fb6585]
    at net.minecraft.server.v1_16_R1.MinecraftServer.v(MinecraftServer.java:810) ~[spigot-1.16.1.jar:git-Spigot-9639cf7-8fb6585]
    at net.minecraft.server.v1_16_R1.MinecraftServer.lambda$0(MinecraftServer.java:164) ~[spigot-1.16.1.jar:git-Spigot-9639cf7-8fb6585]
    at java.lang.Thread.run(Thread.java:834) [?:?]


    este es el codigo del plugin:
    Code (Text):

    package topocai.testplugin;

    import org.bukkit.Bukkit;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;


    public class Testplugin extends JavaPlugin{
       
    PluginDescriptionFile pdffile = getDescription();
    public String version = pdffile.getVersion();
    public String nombre = pdffile.getName();
       
        private final Commands CommandExecutor = new Commands(this);
       
       
    @Override
    public void onEnable(){
        Bukkit.getConsoleSender().sendMessage("==============");
        Bukkit.getConsoleSender().sendMessage(" TestPlugin ");
        Bukkit.getConsoleSender().sendMessage("     "+version+" ");
        Bukkit.getConsoleSender().sendMessage("==============");
        getCommand("fly").setExecutor(CommandExecutor);
        getCommand("testplugin").setExecutor(CommandExecutor);
    }

    @Override
    public void onDisable(){
    }

    }
     
    este es el de los comandos:
    Code (Text):
    package topocai.testplugin;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;


    public class Commands implements CommandExecutor{

    private Testplugin plugin;
       
        public Commands(Testplugin plugin) {
            this.plugin = plugin;
        }
       
    @Override
        public boolean onCommand(CommandSender sender, Command cmd, String laber, String[] args) {
                Player p = (Player)sender;
               if (cmd.getName().equalsIgnoreCase("fly")) {
                if (args.length == 0) {                        
                        if (p.getAllowFlight()) {
                           
                            p.setAllowFlight(false);
                            p.sendMessage("§lModo de vuelo §4§lDesactivado.");
                        } else if (!p.getAllowFlight()) {
                         
                            p.setAllowFlight(true);
                            p.sendMessage("§lModo de vuelo §a§lActivado.");
                        }
                       
                    } else {
                        p.sendMessage("No tienes permisos suficientes.");
                    }
                                 
                return true;
        }        /////////////////////testplugin command////////////////////////////
               if (cmd.getName().equalsIgnoreCase("testplugin")) {
                   if(args.length > 0) {
                       if(args[0].equalsIgnoreCase("version")){
                           p.sendMessage("La Version del plugin es: "+plugin.version);
                       }else {
                           p.sendMessage(plugin.nombre+"§4Este comando no existe");
                       }
                   }else{
                       p.sendMessage(plugin.nombre+"Usa §2/testplugin version §fPara ver la vesion del §lPlugin");
                   }
             
           return true;
       }
            return false;
       
       
        }
       
       
    }
     
    y este el plugin.yml
    Code (Text):
    name: Testplugin
    version: 1.2
    main: topocai.testplugin.Testplugin
    author: Topocai
    commands:
    fly:
    description: Activar modo de vuelo
    usage: /fly
    testplugin:
    description: comando central
    usage: /testplugin
    linea 24:
    Code (Text):
    getCommand("fly").setExecutor(CommandExecutor);
     
  2. Change
    Code (Java):
    getCommand("fly").setExecutor(CommandExecutor);
    To
    Code (Java):
    getCommand("fly").setExecutor(new Commands(this));
     
  3. do not do this.

    can you show us your plugin.yml with proper formatting? or is that how you have formatted it?
    your plugin.yml should look like this:
    Code (Text):
    name: Testplugin
    version: 1.2
    main: topocai.testplugin.Testplugin
    author: Topocai
    commands:
      fly:
        description: Activar modo de vuelo
        usage: /fly
      testplugin:
        description: comando central
        usage: /testplugin
    if you do not indent the lines correctly, your plugin.yml will not load as its suppose to.
     
  4. Why shouldn't he do it?
     
  5. Thank you very much, I have modified the plugin.yml as you told me and it works.
    I did it without spaces because when using "TAB" to do them, it sent me an error saying not to do them.
     
  6. CommandExecutor is the reference to the object of Commands itself in his Main class.

    Code (Java):
    private final Commands CommandExecutor = new Commands(this);
     
  7. Yeah I know.
    He should remove that variable and use my method instead. It's faster to load and requires only one line.
    So why should he not use it?
     
  8. That is indeed true. In my opinion, you should explain why you are telling him to do this instead of what he has done.
     
  9. Well he didnt explain anything either.
    He only posted the code and the console log (not much to work with).
    Oh yeah and these weird strings of characters inbetween.
     
  10. because then he would be declaring two instances for no reason. he doesnt need two different instances of the same command handler. he just needs the one which gets separately registered to two commands. wasting memory creating a whole new object, and potentially having oop issues down the road.
    Code (Java):
    @Override
    public void onEnable(){
        ...
        getCommand("fly").setExecutor(new Commands(this); // remove it here?
        getCommand("testplugin").setExecutor(CommandExecutor)); // but not here?
    }
    it doesnt make much sense to create the same object identity twice.

    yes. you can not use tab in yaml. you can only use spaces. the general format is 2 or 4 spaces for every indentation.
     
  11. Thats not what I said, the only thing I said is that he should create the "Commands" object inside onEnable.
    I never said he should use multiple instances for different commands.
     
  12. no.. all you said was change one instance of the commandexecutor to a new instance.. you didnt say to include it in the onEnable. and you didnt account for the other instance. the code that you provided would create two instances.

    additionally, your code is not any faster as you have said it would be. its exactly the same. if that WERE the only command being registered, it would be nicer to have it as a one liner, but it is by no circumstances a requirement, as it may be possible he needs access to the instance for other reasons.