Solved Console paying in-game players

Discussion in 'Plugin Development' started by OPsteffOP, Jan 25, 2016.

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

    OPsteffOP

    Hello,

    I trying to make a plugin that gives donators DTokens.
    This command must be executed from my console but this doesn't work.
    Can someone help me?

    Code: https://gist.github.com/anonymous/a71b0f521b334f6cee82

    (Sorry for bad english, I'm dutch)

    Thanks!
    ~OPsteffOP
     
  2. Offline

    BobTheHamster9

    Code:
     
    if(sender instanceof ConsoleCommandSender){
    ConsoleCommandSender console = Bukkit.getServer().getConsoleSender();
    console.sendMessage("Example");
    }
    
    I am not sure if you can use permissions with the console. I just tend not to as if the sender is the console then he has full control over permissions and ops
     
  3. Offline

    OPsteffOP

    Thanks for your reply but the command still doesn't work :/
     
  4. Offline

    Zombie_Striker

    Yes, the console has every permission.


    @OPsteffOP
    • You do not need to check if the args length are 0 or 1. Simply put the following:
    Code:
    If (args > 1){
    //Do stuff
    }else{
    //Nope
    }
    • You don't null check to make sure those players/tokens actually exist.
    • You never save the config.
     
  5. Offline

    OPsteffOP

  6. Offline

    Zombie_Striker

    ColouredConsoleSender cannot be cast to org.bukkit.entity.Player

    DO NOT BLIND CAST! Check to make sure the sender is actually a player before casting
     
  7. Offline

    OPsteffOP

    But the sender must be the console. I'm really sorry but I'm new to bukkit plugins and I'm trying to learn it.
    I tried to add if(t instanceof Player){ but this didn't work.
     
  8. Offline

    Zombie_Striker

    @OPsteffOP
    What you said does not follow what you put down. What you are doing here is checking if "t" (which should have a more descriptive name) is a player, but you want to check if it's the console.

    BTW: Looking at the code you provided, you reference a "p" as though it's a player, but what you posted does not contain the creation of "p". You are not showing your full code. Please post your full code, and don't create the player instance until you know the sender is a player.

    Then you should take a look at this, because this is not a bukkit problem, this is a problem with Java. Please pick one of the tutorials from the link below, takes some time to learn Java, and come back. If you learn Java first, everything about bukkit will make sense.
    https://bukkit.org/threads/plugin-dev-sticky-learning-java-where-to-learn.395662/
     
  9. Offline

    OPsteffOP

    Ow sorry, "t" = target (Player)
    I have to send DTokens with a command that only can be executed from console to "t" (target)
    The target player is the player who bought those DTokens

    And "p" = sender (Player)
    Used if a player tried to execute this command.

    I'm trying to make it that only the console can execute this command and give "t" the amount of DTokens he paid for.

    Btw thanks for helping me and I'll take a look at that java link

    FULL CODE: https://gist.github.com/anonymous/12ff9904c34ea0920f4b
     
  10. Offline

    Zombie_Striker

    Code:
    public boolean onCommand(....){
    Player p = (Player)sender;
    Aaand there's your problem. Your not even checking if the sender is actually a player before casting. You are blindly casting a sender to a Player (Blind Casting). Please check if the sender is actually an instanceof player before casting.

    The thing is though, you also have this line in your onCommand
    Code:
    public boolean onCommand(...
    Player p = (Player)sender;
    ....
    if(sender instanceof ConsoleCommandSender){
    How can sender be both a player and a console? You may want to look over your encapsulation some more.
     
  11. Offline

    OPsteffOP

    Oww thank you soo much!!!
    You really helped me alot!!

    I fixed it and it works :)
    Thank you!!! :D
     
  12. Offline

    BobTheHamster9

    Can you mark this thread as solved?
     
Thread Status:
Not open for further replies.

Share This Page