Whats wrong with this one?

Discussion in 'Plugin Development' started by Genesiss, Jan 21, 2012.

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

    Genesiss

    PHP:
    package com.experience.genesis;
     
    import java.util.logging.Logger;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.ExperienceOrb;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerListener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.World;
    import org.bukkit.Location;
    import org.bukkit.event.world.*;
     
    public class 
    Giver extends JavaPlugin {
       
        
    Logger log Logger.getLogger("Minecraft");
       
        public 
    void onEnable(){
            
    log.info("Exp giver enabled!");
            
    PluginManager pm this.getServer().getPluginManager();
        }
       
       
        public 
    void onDisable(){
            
    log.info("Exp giver disabled");
       
        }
     
    public class 
    GiverPlayerListener extends PlayerListener {
       
       
       
        public 
    Giver plugin;
     
        public 
    void GiverPlayerListener(Giver instance) {
            
    plugin instance;
        }
       
        
    // private final GiverPlayerListener playerListener = new GiverPlayerListener(this);   
       
        
    public GiverPlayerListener(GiverPlayerListener giverPlayerListener) {
           
        }
     
        public 
    boolean onCommand(CommandSender senderCommand cmdString commandLabel,String[] args){
            if(
    cmd.getName().equalsIgnoreCase("giveexp")){ // If the player typed /giveexp then do the following...
                
    if (sender instanceof Player){
                      
    Player player = (Playersender;
                if(
    player.hasPermission("genesisexperience.giveexp")) {
                      
    int arg Integer.parseInt(args[1]);
                      
    player.giveExp(arg);
                    }
                       
                    }else{
                      return 
    false;
                    }
                return 
    true;
                }
                
    //If this has happened the function will break and return true. if this hasn't happened the a value of false will be returned.
            
    return false;
        }
     
    }
    }
    I don't know whats wrong with that code. I mean it doesn't give any error but it doesn't work either.
    Can anyone help me?
    PS:
    I know that's a messy one :p I have ideas while writing so it is how it is :D
     
  2. Offline

    Madnessium

    Have you registered your command in the plugin.yml?

    And do you have the permission?
     
  3. Offline

    ItsHarry

    This is wrong.

    Real solution:
    onCommand should be in the main class.
     
  4. Offline

    Genesiss

    PHP:
    package com.experience.genesis;
     
    import java.util.logging.Logger;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.ExperienceOrb;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerListener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.World;
    import org.bukkit.Location;
    import org.bukkit.event.world.*;
    import com.experience.genesis.*;
     
    public class 
    Giver extends JavaPlugin {
       
        
    Logger log Logger.getLogger("Minecraft");
       
        public 
    void onEnable(){
            
    log.info("Exp giver enabled!");
            
    PluginManager pm this.getServer().getPluginManager();
        }
       
       
        public 
    void onDisable(){
            
    log.info("Exp giver disabled");
       
        }
       
        public 
    boolean onCommand(CommandSender senderCommand cmdString commandLabel,String[] args){
            if(
    cmd.getName().equalsIgnoreCase("giveexp")){ // If the player typed /giveexp then do the following...
                
    if (sender instanceof Player){
                      
    Player player = (Playersender;
                if(
    player.hasPermission("genesisexperience.giveexp")) {
                      
    int arg Integer.parseInt(args[1]);
                      
    player.giveExp(arg);
                    }
                       
                    }else{
                      return 
    false;
                    }
                return 
    true;
                }
                
    //If this has happened the function will break and return true. if this hasn't happened the a value of false will be returned.
            
    return false;
        }
     
    }
        
    and there's a GiverPlayerListener.java because it said it need to be defined in its own file.

    PHP:
    package com.experience.genesis;
     
    import org.bukkit.event.player.PlayerListener;
     
    public class 
    GiverPlayerListener extends PlayerListener {
       
       
       
        public 
    Giver plugin;
     
        public 
    void GiverPlayerListener(Giver instance) {
            
    plugin instance;
        }
       
        
    // private final GiverPlayerListener playerListener = new GiverPlayerListener(this);   
       
        
    public GiverPlayerListener(GiverPlayerListener giverPlayerListener) {
           
        }
     
     
    }
    Still doesn't work :( Did i do something wrong?
     
  5. Offline

    Technius

    Make it say something when it works, otherwise it seems confusing. If they don't have the permission, tell them they don't and return true. There's also a glitch involving exp, where the exp bar doesn't update until you pick up exp orbs.
     
  6. Offline

    Genesiss

    Code:
    package com.experience.genesis;
     
    import java.util.logging.Logger;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.ExperienceOrb;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerListener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.World;
    import org.bukkit.Location;
    import org.bukkit.event.world.*;
    import com.experience.genesis.GiverPlayerListener;
     
    public class Giver extends JavaPlugin {
     
        Logger log = Logger.getLogger("Minecraft");
     
        public void onEnable(){
            log.info("Exp giver enabled!");
            PluginManager pm = this.getServer().getPluginManager();
        }
     
     
        public void onDisable(){
            log.info("Exp giver disabled");
     
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel,String[] args){
                if(cmd.getName().equalsIgnoreCase("giveexp")){
                //    if( int lenghtarg = 2 ;int lenghtarg = args.length){
                    Player player = (Bukkit.getServer().getPlayer(args[0]));
                    if (player == null) {
                      sender.sendMessage(ChatColor.RED + args[0] + " is not with us!");
                      int arg = Integer.parseInt(args[1]);
                        ((Player) sender).giveExp(arg);
                        sender.sendMessage(ChatColor.GOLD + "You got "+arg+" exp!");
                    }
                                    if(player.hasPermission("genesisexperience.giveexp")) {
                                        int arg = Integer.parseInt(args[1]);
                                        player.giveExp(arg);
                                        player.sendMessage(ChatColor.GOLD + "You got "+arg+" exp!");
                                    }
                                    else{
                                        player.sendMessage(ChatColor.RED + "You dont have a permission to do that!");
                                        return true;
                                    }
                     
                        }
                        else{
                            return false;
                        }
                        return true;
             
        }
     
    }
        
    Made some changes :) Could you tell me where's the mistake now?
     
  7. What version if CraftBukkit are you using? It might be the new event system.

    Keir
     
  8. Offline

    Genesiss

    API is the newest and Bukkit for server is the dev build for 1.1.
     
  9. Offline

    desht

    You seem to have gotten your onCommand() method pretty tangled up there. How about:
    Code:java
    1.  
    2. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel,String[] args){
    3. if (cmd.getName().equalsIgnoreCase("giveexp")) {
    4. Player player;
    5. int amount;
    6. if (args.length == 2) {
    7. player = Bukkit.getServer().getPlayer(args[0]);
    8. amount = Integer.parseInt(args[1]);
    9. } else if (args.length == 1 && sender instanceof Player) {
    10. player = (Player) sender;
    11. amount = Integer.parseInt(args[0]);
    12. } else {
    13. sender.sendMessage("Usage: /giveexp [<playername>] <amount>");
    14. return true;
    15. }
    16. player.giveExp(amount);
    17. return true;
    18. } else {
    19. return false;
    20. }
    21. }
    22.  

    Some notes:
    • Make sure "giveexp" is registered in your plugin.yml
    • The method will also throw an exception if a non-numeric value is entered. You should catch NumberFormatException when calling Integer.parseInt(), and display a friendly usage message.
    • I recommend looking at http://forums.bukkit.org/threads/experienceutils-make-giving-taking-exp-a-bit-more-intuitive.54450/ which works around some issues with player.giveExp()
    • I really really hate XenForo and its insistence that it knows how to format my code better than I do :(
     
    Genesiss likes this.
  10. Offline

    Genesiss

    Thank you :) That's i was going for :D
    Also i think I'll use that exp utils :D
     
Thread Status:
Not open for further replies.

Share This Page