New Plugin Developer Issue

Discussion in 'Plugin Development' started by ak47vszombies, May 3, 2015.

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

    ak47vszombies

    I have recently started coding minecraft server plugins and I have run into a slight barrier. In order to assure I know what I am doing I am trying to learn how to make a teleporting plugin. Well that is going not so well. I watched a video on what to put in my code letter for letter and there are no errors. But as soon as I run it on my server my plugin is not fully functional. What I have added in the plugin is /weiner which asks why you're saying weiner and ive added /death or /d which takes you to half heart and tells you that that was a close call. Then I started working on my /akport command which would allow you to tp to people. I made 3 separate variables of the command, one being /akport which responds to you "please add more info" I then made the second so typing /akport (player) would tp a player to you and my third was /akport (player1) (player2) allowing you to tp to someone. The coding is just as it should be but not functional, could someone tell me what is wrong with this code below?

    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    Player player = (Player) sender;
    if(commandLabel.equalsIgnoreCase("weiner")){
    player.sendMessage(ChatColor.RED + " Why are you saying weiner? ");
    }
    if(commandLabel.equalsIgnoreCase("death") || commandLabel.equalsIgnoreCase("d")){
    player.setHealth(1);
    player.sendMessage(ChatColor.DARK_RED + " That was a brush with death! ");
    }
    if(commandLabel.equalsIgnoreCase("akport")){
    if(args.length == 0)
    player.sendMessage(ChatColor.GREEN + "Bro you're missing some info");
    }
    else if(args.length == 1){
    Player targetPlayer = player.getServer().getPlayer(args[0]);
    Location targetPlayerLocation = targetPlayer.getLocation();
    player.teleport(targetPlayerLocation);
    }
    else if(args.length == 2){
    Player targetPlayer = player.getServer().getPlayer(args[0]);
    Player targetPlayer1 = player.getServer().getPlayer(args[1]);
    Location targetPlayer1Location = targetPlayer1.getLocation();
    targetPlayer.teleport(targetPlayer1Location);
    }
     
  2. Offline

    vhbob

  3. Offline

    Garnetty

    Do [.code] yourcode [./code] without the dots
     
  4. Offline

    Gater12

    @ak47vszombies
    Does your plugin load and list your commands?

    Also it would be better to just check cmd.getName() and define command aliases in the plugin.yml instead of hardcoding a check for each alias.
     
  5. Offline

    ak47vszombies

    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            Player player = (Player) sender;
            if(commandLabel.equalsIgnoreCase("weiner")){
                player.sendMessage(ChatColor.RED + " Why are you saying weiner? ");
            }
            if(commandLabel.equalsIgnoreCase("death") || commandLabel.equalsIgnoreCase("d")){
                player.setHealth(1);
                player.sendMessage(ChatColor.DARK_RED + " That was a brush with death! ");
            }
            if(commandLabel.equalsIgnoreCase("akport")){
                if(args.length == 0)
                    player.sendMessage(ChatColor.GREEN + "Bro you're missing some info");
            }
            else if(args.length == 1){
                Player targetPlayer = player.getServer().getPlayer(args[0]);
                Location targetPlayerLocation = targetPlayer.getLocation();
                player.teleport(targetPlayerLocation);
            }
            else if(args.length == 2){
                Player targetPlayer = player.getServer().getPlayer(args[0]);
                Player targetPlayer1 = player.getServer().getPlayer(args[1]);
                Location targetPlayer1Location = targetPlayer1.getLocation();
                targetPlayer.teleport(targetPlayer1Location);
            }
    @Gater yes the commands load and list fine
     
  6. Offline

    BizarrePlatinum

    @ak47vszombies as @Gater12 said, you should be using cmd.getName() instead of commandLabel (honestly have no idea what command label is even used for). It's also good to test if the Sender is an instance of Player before declaring the player variable, of course I doubt you'd try to send this from console/command block, but, I figured this may help you in the future.
     
    Last edited: May 3, 2015
  7. Offline

    vhbob

    @ak47vszombies also, if your in eclipse please click your code and do ctrl + shift + f, it formats your code so we can read it better
     
  8. Offline

    Gater12

    @ak47vszombies
    What I now see from your code is a simple logical error. You have your arguments checked outside your akport command block (in fact it will only run if akport check failed). You will need to put this inside your akport block.
     
  9. Offline

    ak47vszombies

    @Gater12 what do i do to put it inside my akport block? And for formatted code:
    Code:
        public boolean onCommand(CommandSender sender, Command cmd,
                String commandLabel, String[] args) {
            Player player = (Player) sender;
            if (commandLabel.equalsIgnoreCase("weiner")) {
                player.sendMessage(ChatColor.RED + " Why are you saying weiner? ");
            }
            if (commandLabel.equalsIgnoreCase("death")
                    || commandLabel.equalsIgnoreCase("d")) {
                player.setHealth(1);
                player.sendMessage(ChatColor.DARK_RED
                        + " That was a brush with death! ");
            }
            if (commandLabel.equalsIgnoreCase("akport")) {
                if (args.length == 0)
                    player.sendMessage(ChatColor.GREEN
                            + "Bro you're missing some info");
            } else if (args.length == 1) {
                Player targetPlayer = player.getServer().getPlayer(args[1]);
                Location targetPlayerLocation = targetPlayer.getLocation();
                player.teleport(targetPlayerLocation);
            } else if (args.length == 2) {
                Player targetPlayer = player.getServer().getPlayer(args[1]);
                Player targetPlayer1 = player.getServer().getPlayer(args[2]);
                Location targetPlayer1Location = targetPlayer1.getLocation();
                targetPlayer.teleport(targetPlayer1Location);
            }
     
  10. Offline

    Msrules123

    That's where your code ends on the command "akport."
     
  11. Offline

    Gater12

    @ak47vszombies
    The checks for arguments because currenlty your setup looks like this:
    Code:java
    1. if command is akport {
    2. if there are no arguments {
    3. send message
    4. }
    5. }else if there is one argument and implicitly the command is NOT akport{
    6.  
    7. }else if there is two arguments also implicitly the command is NOT akport{
    8.  
    9. }
     
  12. Offline

    ak47vszombies

    Oh I understand what you're saying now, I feel so dumb xD
     
  13. Offline

    Zombie_Striker

    Never cast without checking. Add a if statemenet for your Player player to see if its an instanceof Player
     
  14. Offline

    ak47vszombies

    Now I am stuck on correcting it, could I have some help?

    Nevermind I figured out what was wrong, thank you for the help guys, wouldn't have been possible for me to figure this out without you.

    Another Issue has arised, I'm currently trying to make a block listener, as in blacklist, yes I know this is in essentials but I want to know how to do it myself. In the tutorial I am watching it requires that I import org.bukkit.event.block. But this is invalid. And in order for block listener to function that needs to work, any suggestions, I'll paste code if needed.
     
    Last edited by a moderator: May 4, 2015
  15. Offline

    vhbob

    @ak47vszombies 1) dont watch tutorials most suck, and 2) please paste code
     
  16. Offline

    ak47vszombies

    @vhbob where would I learn on how to code bukkit plugins then?
     
  17. Offline

    vhbob

    @ak47vszombies you think of what you want to do then think of how to do it, then think of how you would put that in code, and if something is confusing to you ask for help on here, and as stated before, please paste the code
     
  18. Offline

    ak47vszombies

    ok i looked into that block listener issue and that is because the tutorial was like 3 years old

    Here is the issue I face when doing that alone, when doing block manipulation plugins such as fortune blocks I know that more than one class are needed for that plugin but I am totally oblivious on how I would go around making those.
     
    Last edited by a moderator: May 4, 2015
  19. Offline

    vhbob

    my seggustion: make an eventhandler for breaking blocks, then if its the specific block you want, have it get the material type and make a new itemstack for that with the int of 2 or 3 or 4 etc...
     
  20. Offline

    Zombie_Striker

  21. Offline

    ak47vszombies

    Ok I am having issue with some code, atm I'm trying to make one command /sethealth where a player can set their health to a specific amount. Now I want to do this without making several different commands such as /sethealthone /sethealthtwo. etc... I want to be able to type /sethealth one up to /sethealth twenty. But the only idea I had to make it work is dysfunctional. Please help.
    Code:
            if(commandLabel.equalsIgnoreCase("sethealth"))
            {
                if(args[1].equalsIgnoreCase("one"))
                {
                    player.setHealth(1);
                }
                else
                {
                    return false;
                }
               
                if(args[1].equalsIgnoreCase("two"))
                {
                    player.setHealth(2);
                }
                else
                {
                    return false;
                }
           
                if(args[1].equalsIgnoreCase("three"))
                {
                    player.setHealth(3);
                }
               
            return false;
        }
     
  22. Offline

    HuliPvP

    Try this (For the sethealth plugin):
    Code:
    {
          Player p = (Player)e.getEntity();
          if (p.getHealth() - e.getFinalDamage() <= 0.0D)
          {
            e.setDamage(0.0D);
            p.setHealth(20.0D);
          {
    {
    
    Code:
    if(commandLabel.equalsIgnoreCase("sethealth")
    {
                if(args[1].equalsIgnoreCase("one"))
       {
          Player p = (Player)e.getEntity();
          if (p.getHealth() - e.getFinalDamage() <= 0.0D)
          {
            e.setDamage(0.0D);
            p.setHealth(1.0D);
          }
       {     
                if(args[1].equalsIgnoreCase("two"))
       {
          Player p = (Player)e.getEntity();
          if (p.getHealth() - e.getFinalDamage() <= 0.0D)
          {
            e.setDamage(0.0D);
            p.setHealth(2.0D);
          }
       }
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  23. Offline

    CoolGamerXD

    You must check arguments length before defining multiple arguments. Second thing rather than having other argument for example "one" makes you to type same line with 20 times so instead of it use integers like /sethealth <Player> 20.
     
Thread Status:
Not open for further replies.

Share This Page