Sorry, I am New in java D:

Discussion in 'Spigot Plugin Development' started by Newpredator, May 4, 2015.

  1. Pls my code is bad and the plugin not start

    Menu (main)

    Code (Text):
    package me.newpredator;

    import java.util.logging.Level;
    import java.util.logging.Logger;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Sound;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Menu extends JavaPlugin {
        public static final Logger log = Logger.getLogger("Minecraft:");
        public static Inventory inv = Bukkit.createInventory(null, 18, (ChatColor.translateAlternateColorCodes('&',Config.menuname)));

        public void onEnable() {
            log.log(Level.INFO, "Plugin Activated");
            if (!Config.configFile.exists()) {
              Config.save();
        }
        Config.load();
      }
       
        public void onDisable() {  
            log.log(Level.INFO, "Plugin Desactivated");
           
        }
       
        @Override
        public boolean onCommand (CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("menu"));
            Player p = (Player) sender;
            if (p.hasPermission("menu.open")){
            if(args.length ==0);
            p.openInventory(inv);
            p.sendMessage(ChatColor.AQUA + "You are opening the inventory");
            p.playSound(p.getLocation(), Sound.ENDERDRAGON_WINGS, 1.0F, 1.0F);
            }else{
            p.sendMessage(ChatColor.RED + "You don´t have permission");
            p.playSound(p.getLocation(), Sound.VILLAGER_NO, 1.0F, 1.0F);
            return true;
           
        }
            return false;
        }
    }
           


     
    Config

     
  2. Code (Text):
    package me.newpredator;

    import java.io.File;
    import java.util.logging.Level;
    import java.util.logging.Logger;

    import org.bukkit.configuration.file.YamlConfiguration;


    public class Config {
        public static String menuname = "&bMenu";
        public static final Logger log = Logger.getLogger("Minecraft:");
        //Archive
        public static File configFile = new File("plugins/Menu/Config.yml");
       
        public static void load() {
            YamlConfiguration config = YamlConfiguration.loadConfiguration(configFile);
           
            menuname = config.getString("Menu.Name");
           
        }
       
        public static void save() {
            YamlConfiguration config = new YamlConfiguration();
            config.set("Menu.Name", menuname);

    try {
               
                config.save(configFile);
                Menu.log.log(Level.INFO, "Config enable");
            } catch (Exception e) {
                Menu.log.log(Level.WARNING, "Error in configurate (0)", e.getMessage());
               
            }
           
         }
    }
     
     
  3. Please, we can never help you like this. Give information: what is the error, what for plugin are you making etc.
     
    • Agree Agree x 1
  4. Have you create plugin.yml file?
     
    • Agree Agree x 1
  5. If you're new at Java, like you say, then play around with it before creating plugins.
     
    • Agree Agree x 5
  6. Probably is this your problem: Player p = (Player) sender;. Because you are casting it before you ever knows who that sender is. Make this if statement if (sender instanceof Player){do stuff}
     
  7. joehot200

    Supporter

    That's bad practice, but also not inherently going to cause the plugin not to load in the first place.
     
  8. Check before you cast and er... wtf : if(args.length ==0);
     
  9. Interestingly enough, that is a valid snippet, however it isn't doing what he expects.

    The underlying definition of an if statement in java is
    Code (Text):
     if ( condition ) statement
    The curly brackets commonly employed with if constructs denote a block, which is then used as the single 'statement' in the definition.

    If instead you write
    Code (Text):
    if ( condition ) ;
    The statement is interpreted as a lesser known java construct - ';' on its own is the 'empty statement' - it does nothing.

    This means while he was attempting to do something along the lines of
    Code (Text):
    if (p.hasPermission("menu.open") && args.length == 0) {
            p.openInventory(inv);
            p.sendMessage(ChatColor.AQUA + "You are opening the inventory");
            p.playSound(p.getLocation(), Sound.ENDERDRAGON_WINGS, 1.0F, 1.0F);
    } else {
            p.sendMessage(ChatColor.RED + "You don´t have permission");
            p.playSound(p.getLocation(), Sound.VILLAGER_NO, 1.0F, 1.0F);
            return true;
    }
    He has actually written
    Code (Text):
    if (p.hasPermission("menu.open")) {
            if(args.length ==0) {
                    // Let's just ignore this if statement
            }
            p.openInventory(inv);
            p.sendMessage(ChatColor.AQUA + "You are opening the inventory");
            p.playSound(p.getLocation(), Sound.ENDERDRAGON_WINGS, 1.0F, 1.0F);
    } else {
            p.sendMessage(ChatColor.RED + "You don´t have permission");
            p.playSound(p.getLocation(), Sound.VILLAGER_NO, 1.0F, 1.0F);
            return true;
    }
    - flamin
     
  10. If statements that end with: ';'. Sure, because that will have no effect on code whatsoever.

    EDIT:

    Ninja'd by @flamin_scotsman
     
  11. Depends, you can do if ( 5 > 2) doo somethinngggg; or if (boolean) do someting;
     
  12. What? Lol.

    It is an Empty statement.
     
  13. no.

    if (4 == 4) {
    System.out.println("HI");
    }

    is the same as if (4 == 4) System.out.println("HI");
     
  14. Yes, however he has nothing before the semicolon, so your argument is moot. He is testing a condition, then doing nothing with it.

    - flamin
     
    • Like Like x 1
  15. I know?
     
  16. Code (Text):
    package me.newpredator;

    import java.util.logging.Level;
    import java.util.logging.Logger;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Sound;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Menu extends JavaPlugin {
        public static final Logger log = Logger.getLogger("Minecraft:");
        public static Inventory inv = Bukkit.createInventory(null, 18, (ChatColor.translateAlternateColorCodes('&',Config.menuname)));

        public void onEnable() {
            log.log(Level.INFO, "Plugin Activated");
            if (!Config.configFile.exists()) {
              Config.save();
        }
        Config.load();
      }
       
        public void onDisable() {  
            log.log(Level.INFO, "Plugin Desactivated");
           
        }
       
        @Override
        public boolean onCommand (CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("menu"));
            Player p = (Player) sender;
            if (p.hasPermission("menu.open") && args.length == 0) {
                p.openInventory(inv);
                p.sendMessage(ChatColor.AQUA + "You are opening the inventory");
                p.playSound(p.getLocation(), Sound.ENDERDRAGON_WINGS, 1.0F, 1.0F);
        } else {
                p.sendMessage(ChatColor.RED + "You don´t have permission");
                p.playSound(p.getLocation(), Sound.VILLAGER_NO, 1.0F, 1.0F);
                return true;
        }
            return false;
        }
    }
    ?
     
  17. <sigh..>
    Please look up the definition of an empty statement.

    The code given below, will print out as follows: 4 Is equal to 4.

    Because i is equal to c.

    PHP:
     int i = 4,c = 4;
            if(i == c) {
             System.out.println(i + " Is equal to " + c);
            }

    This will also print out "4 Is equal to 4."
    Even though we added the semicolon to the end.

    PHP:

    int i=4,c=4;
    if(i == c);
            {
                System.out.println(i + " Is equal to " + c);
            }
    Now...

    We changed the value of i, it now equals 44.
    When we run this code, it prints out 44 Is equal to 4. Which is NOT true.


    PHP:
    int i = 44,c = 4;
            if(i == c);
            {
                System.out.println(i + " Is equal to " + c);
            }
    Right from the documentation...
    http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.6

    When testing a value, this is useless, and you should not do it.
    @TheGamesHawk2001
     
  18. @Newpredator


    To start, remove this:
    PHP:
    public static final Logger log = Logger.getLogger("Minecraft:");
    Don't use static unless you know what you are doing with it.