Multiple Java Versions

Discussion in 'Plugin Development' started by Vidsify, Jan 31, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    Vidsify

    At the moment I have Java 8 Update 31 on my PC. Do the servers have to be running that version for my plugin to work or doesn't it matter?

    Also I have Gravitys Auto Updater with my plugin I want to create a command that changes the config option to either true or false, in other words to disable the auto updater in game but it doesn't seem to work. See my code below:

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
      {
        if (cmd.getName().equalsIgnoreCase("durexp")) {
          if (!sender.hasPermission("DoubleurEXP.use"))
          {
            sender.sendMessage(ChatColor.GOLD + "[DurEXP]" + ChatColor.RED + "You do not have permission for this command!");
          }
          else if (args.length == 0)
          {
            sender.sendMessage(ChatColor.GOLD + "[DurEXP]" + ChatColor.RED + "Use /durexp help, for more information");
          }
          else if (args[0].equalsIgnoreCase("help"))
          {
            sender.sendMessage(ChatColor.GOLD + "================" + ChatColor.AQUA + " DoubleXP By Vidsify " + ChatColor.GOLD + "================");
            sender.sendMessage(ChatColor.GOLD + "/durexp help" + ChatColor.RED + " - Displays this help menu");
            sender.sendMessage(ChatColor.GOLD + "/durexp toggle" + ChatColor.RED + " - Toggles this plugin on or off");
            sender.sendMessage(ChatColor.GOLD + "/durexp multiplier <amount>" + ChatColor.RED + " - Amount xp is multiplied by, e.g. 2 is double");
            //sender.sendMessage(ChatColor.GOLD + "/durexp update" + ChatColor.RED + " - Toggles the plugins AutoUpdater.");
            sender.sendMessage(ChatColor.GOLD + "=====================================================");
          }
         
          //Multipler Command
          else if (args[0].equalsIgnoreCase("multiplier"))
          {
            if (isDouble(args[1]))
            {
              this.config.set("Multiplier", Double.valueOf(Double.parseDouble(args[1])));
              sender.sendMessage(ChatColor.GOLD + "[DurEXP]" + ChatColor.RED + "Multiplier set to: " + args[1]);
              saveConfig();
            }
            else
            {
              sender.sendMessage(ChatColor.GOLD + "[DurEXP]" + ChatColor.RED + " Only enter a number!");
            }
          }
        
          //Toggle Command
          else if (args[0].equalsIgnoreCase("toggle"))
          {
            if (this.config.getBoolean("Enable"))
            {
              this.config.set("Enable", Boolean.valueOf(false));
              sender.sendMessage(ChatColor.GOLD + "[DurEXP]" + ChatColor.RED + " Plugin disabled!");
            }
            else
            {
              this.config.set("Enable", Boolean.valueOf(true));
              sender.sendMessage(ChatColor.GOLD + "[DurEXP]" + ChatColor.GREEN + " Plugin enabled!");
            }
          }
         
          //Update Command
          else if (args[0].equalsIgnoreCase("update"))
          {
            if (this.config.getBoolean("AutoUpdate"))
            {
              this.config.set("AutoUpdate", Boolean.valueOf(false));
             sender.sendMessage(ChatColor.GOLD + "[DurEXP]" + ChatColor.RED + " AutoUpdating Disabled!");
            }
            else
            {
              this.config.set("AutoUpdate", Boolean.valueOf(true));
              sender.sendMessage(ChatColor.GOLD + "[DurEXP]" + ChatColor.GREEN + " AutoUpdating Enabled!");
            }
          }
          else
          {
            sender.sendMessage(ChatColor.GOLD + "[DUREXP]" + ChatColor.RED + " Invalid command. /durexp help for list of commannds");
          }
        }
        return true;
      }
    Thanks in advance :)

    Just testing my new version and non of my commands are changing my config

    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Jan 31, 2015
  2. Add some debug messages, and you can just use true or false instead of Boolean.valueOf(true);
     
  3. Offline

    Vidsify

  4. Offline

    Not2EXceL

    That completely depends on what your compilation target is.
     
  5. Offline

    Europia79

    @Vidsify

    You're using a config field... but we don't see you set it. Also... we're not seeing how the Auto-Updater gets the boolean... whether it's stored in memory, or retrieved from the disk. Post all your code so we can find the bug.

    http://wiki.bukkit.org/Configuration_API_Reference

    Also, your toggle sub-command doesn't have saveConfig() after config.set(path, value)... So when you're doing config.set(path, value), it changes what is in memory... saveConfig() will write what's in memory to the hard drive. Same deal for some of your other sub-commands... they don't have saveConfig() after they have config.set()

    Overall, your onCommand() method is pretty messy... I would suggest some kind of Command Library like @Not2EXceL 's Dynamic CommandAPI

    Or, there are other command handlers out there too. BattleArena has one. I think WorldEdit has one ? I'm sure there are a bunch of other ones too. Using one will make your code a lot cleaner & more readable... you'll have one command per method instead of one method handling multiple commands.

    As far as your Java version... If you compile with Java 8, then servers will need at least Java 8 or greater to run your plugin. You can download JDK 6, 7, & 8 and have them all... Then for each project, you can tell your IDE which JDK to use.
     
  6. Offline

    Not2EXceL

    If you have java8 you can set the compilation target to target 6, 7, or 8. By default IntelliJ sets the project language level to java6, and eclipse to java5, IIRC.
     
  7. Offline

    Vidsify

  8. Offline

    Not2EXceL

    Setting the target has nothing to do with the code itself, unless it borrows specific features from a specific java version.

    Upon a quick glance of the code, I don't see anything specific to 1.7, so you can set the target for 1.6 and should be fine. If you're using a build system, like maven or gradle, then lookup how to do it. If not, then it can be set in the eclipse build settings or the intellij project settings.

    What I do see is some bad practices. Split that code into a few classes so its not one huge clusterfuck of a class. Also y are you boxing the boolean alreadyDone? There's absolutely no reason for you not to just use the primitive.
    And makes those braces consistent at least. God it looks like you copy pasted half of the code in there as they frequently change between allman and k&r style.
     
    Europia79 likes this.
  9. Offline

    Europia79

    @Not2EXceL

    He originally had two forum-threads... one about Java versions and another about a bug in his code.

    The two forum-threads were merged into one forum-thread... hence the confusion.

    EDIT:

    I agree that it should be broken up into multiple classes. Something like this ?

    https://github.com/Europia79/DoubleYourExperience

    I didn't go hog-wild on refactoring... Basically i just split Main up into multiple classes and fixed the /durexp update command bug. I left all the Boolean boxing as-is. But somehow I just couldn't resist removing literal strings for .hasPermission()... I mean the documentation said the permissions were "doubleurexp.use" & "doubleurexp.allow", but the code had "DoubleurEXP.use" & "DoubleurEXP.allow"... Not sure if this is an issue or not, but at least now, if any changes need to be made to permission nodes, you only have to make the changes in one place... And all the changes get propagated to the other classes.

    I encountered an error with WorldGuard where an exception occured during onEnable(). Which broke the plugin... So I moved his "has been enabled" message to the end of onEnable() so it doesn't get printed if a similar situation occurs.

    I have no idea why he was using copyDefaults(true) ?

    I couldn't resist swapping the logic for
    Code:java
    1. if((player.hasPermission("DoubleurEXP.multiplier."+ temp))&&(!player.isOp()))

    to
    Code:java
    1. if (!player.isOp() && player.hasPermission(Perm.MULTIPLIER + temp)) {

    I changed it because if the player is OP, then we don't need to continue to evaluate the rest of the boolean expression.

    Like I said: This is just some light-refactoring... Any other suggestions tho ?
     
    Last edited: Feb 3, 2015
  10. Offline

    Vidsify

    @Europia79 I'm in the process of testing it out. The first thing I noticed is that when you load the plugin for the first time you get a config looking like this:

    Code:
    #Welcome to Durexp Config File
    #Here you will find the default config settings feel free to change them to suit your liking! :)
    #If you have any problems please create a ticket:
    #http://dev.bukkit.org/bukkit-plugins/double-your-experience/tickets/
    
    #Change this to false if you don't want the plugin to automatically update itself when a new version is released. I recommend you leave this set to TRUE!
    AutoUpdate: false
    
    #This option will allow you to disable the plugin and enable it, it is also possible to change this in-game using /durexp toggle.
    Enable: true
    
    #Enable this setting if you want to have per perm multiples it will also disable the setting below eg DoubleurEXP.multiplier.<number> This vaule can be to a decimal place but will only go up to 10.0
    EnablePermMultiplier: false
    
    #This will allow you to set a server wide Multiplier
    Multiplier: 2.0
    
    #This is a list of the days that are enabled, shown is the different ways you can write the date, the plugin will detect them all!!!
    DaysToEnable:
    - Monday
    - tuesday
    - wed
    - thursday
    - frida
    - sat
    - SundAy
    
    #The radius that the plugin will check if the "CheckForSpawner" option is enabled, could be used to prevent mass farming. <---We don't want that now!
    CheckRadius: 40
    
    # Enables/disables the check for mob spawners.
    # true = check for nearby mob spawners before giving bonus experience.
    # false = do not check for nearby mob spawners: just give out bonus xp.
    CheckForSpawner: true
    
    #Congratulations you have successfully customized our easy to use config. Now go ahead and enjoy! :p
    But when you do /durexp toggle for example the comments are removed making look like this:

    Code:
    #
    # Enables/disables the check for mob spawners.
    # true = check for nearby mob spawners before giving bonus experience.
    # false = do not check for nearby mob spawners: just give out bonus xp.
    AutoUpdate: false
    Enable: false
    EnablePermMultiplier: false
    Multiplier: 2.0
    DaysToEnable:
    - Monday
    - tuesday
    - wed
    - thursday
    - frida
    - sat
    - SundAy
    CheckRadius: 40
    CheckForSpawner: true
    
    I have looked all over the web tried multiple things and its not seeming to work.
     
  11. Offline

    Europia79

    @Vidsify

    sorry about that.

    An easy fix is to put all documentation/comments at the top of the file.

    Any other problems, just lemme know.
     
  12. Offline

    Vidsify

    @Europia79 what do you mean as I have it set up so that it explains each option. Sorry for keep asking
     
Thread Status:
Not open for further replies.

Share This Page