Issue with void methods

Discussion in 'Plugin Development' started by uyscutix, Sep 14, 2017.

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

    uyscutix

    Hi, I'm trying to make a new 'void' style method in another class for a large command I am making. But I have issues with implementing the Command sender!

    Errors occur if I put inside the method "Player p = (Player) sender;", how do I or what other ways are there to make void methods insert the command sender. Because I want to get the command sender's javadocs to work inside the method as well as the player being doomed by my custom doom plugin.

    Tl;Dr: Im making a method in another class (not in onCommand class) externally, but inserting "Player p = (Player) sender;" gives out errors.

    The method what I'm talking about:
    Code:
    public static void doom(Player player)
    {
        player.closeInventory();
        player.setFireTicks(10000);
        player.getWorld().createExplosion(player.getLocation(), 3F);
    
        int i = 1;
    
        do
        {
            player.getWorld().strikeLightning(player.getLocation());
        }
        while (i < 20);
    
        player.setHealth(0.0);
    }
    
     
  2. Offline

    timtower Administrator Administrator Moderator

    @uyscutix
    Code:
    do
    {
    player.getWorld().strikeLightning(player.getLocation());
    }
    while (i < 20);
    That will kill your server.
    And I don't get what you are saying, but it looks like the issue has nothing to do with the given function.
    What error do you get?
     
  3. Offline

    uyscutix

    nevermind, I think I resolved my own issue.

    Is this correct? I put 'CommandSender sender' into the method brackets and I finally got no more errors when I put the "Player p = (Player) sender;" variable.

    Code:
    public static void doom(CommandSender sender, Player player)
    What I was trying to say was, I'm making some actions to be done when a command is executed, but the actions are done in another class (which is this one with the void method). However it wouldn't allow access to use javadocs on the command sender.
     
  4. Offline

    timtower Administrator Administrator Moderator

    @uyscutix Why do you need to have the sender in there?
     
  5. Offline

    uyscutix

    I want this method to basically act like i'm making a command class, but it's in an external class so my command class for example named "Command_<command>.java" does not get too long with too much code in it.
     
  6. Offline

    timtower Administrator Administrator Moderator

    @uyscutix But why do you need the CommandSender AND the Player
     
  7. Offline

    uyscutix

    one of them is for example doing actions on the CommandSender.

    E.g.
    Code:
    Player p = (Player) sender;
    p.setHealth(20.0);
    the Player is the player typed in the command and if the command sender executes for example "/<command> player123", then all actions will be done on the player instead of command sender. So for example, "p.getName()" would change to "player.getName()" if I'd want the referred name to be the player who was written in the command rather than the command sender.
     
  8. Offline

    Minesuchtiiii

    You should increase i, or it runs forever
    Code:
    public static void doom(Player player)
    {
        player.closeInventory();
        player.setFireTicks(10000);
        player.getWorld().createExplosion(player.getLocation(), 3F);
    
        int i = 1;
    
        do
        {
            player.getWorld().strikeLightning(player.getLocation());
                  //i++;
        }
        while (i < 20);
    
        player.setHealth(0.0);
    }
    But I think after 20 lightning strikes he'll be dead anyways :D
     
Thread Status:
Not open for further replies.

Share This Page