Solved Prison Prestige bug!

Discussion in 'Plugin Development' started by MrConHD, Nov 27, 2014.

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

    MrConHD

    So recently a friend of mine requested that I create a plugin that when a user reaches the Free level, they can Prestige and be granted the Suffix of 1 after their name, and so on depending on their prestige level.

    Currently, I am attempting to do this using PEX and checking if the user has the required permission needed which will be granted at each Free Rank, then if they do, granting them their new suffix and resetting their group back to A.

    Here is the code:
    [
    Code:
    package me.MrConHD.Prestiges;
     
    import net.milkbowl.vault.permission.Permission;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.OfflinePlayer;
    import org.bukkit.Server;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
     
     
     
    public class Core extends JavaPlugin {
       
        public void onEnable() {
            Bukkit.getServer().getLogger().info(ChatColor.GREEN + "PrisonPrestige has been enabled! - Developed by MrConHD");
        }
       
        public void onDisable() {
            Bukkit.getServer().getLogger().info(ChatColor.GREEN + "PrisonPrestige has been disabled! - Developed by MrConHD");
        }
       
        public static Permission permission = null;
       
        private boolean setupPermissions()
        {
            RegisteredServiceProvider<Permission> permissionProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.permission.Permission.class);
            if (permissionProvider != null) {
                permission = permissionProvider.getProvider();
            }
            return (permission != null);
        }
       
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("Prestige")) {
                if (sender.hasPermission("prison.prestige1")) {       
                    if (sender instanceof Player) {
                        Player player = (Player) sender;
                        World world = player.getServer().getWorld(getName());
                        String worldName = world.toString();
                    }
                    else {
                        sender.sendMessage(ChatColor.DARK_RED.toString() + ChatColor.BOLD + "ERROR: " + ChatColor.RESET + ChatColor.RED + " You must be a player to execute this command!");
                        return false;
                    }
               
            }
                else {
                   
                    sender.sendMessage(ChatColor.DARK_RED.toString() + ChatColor.BOLD + "ERROR: " + ChatColor.RESET + ChatColor.RED + "You are not the required rank to Prestige!");
                    return false;
                }
                if (sender.hasPermission("prison.prestige2")) {               
                    if (sender instanceof Player) {
                        Player player = (Player) sender;
                        World world = player.getServer().getWorld(getName());
                        String worldName = world.toString();
                    }
                    else {
                        sender.sendMessage(ChatColor.DARK_RED.toString() + ChatColor.BOLD + "ERROR: " + ChatColor.RESET + ChatColor.RED + " You must be a player to execute this command!");
                        return false;
                    }
            }
                else {
                   
                    sender.sendMessage(ChatColor.DARK_RED.toString() + ChatColor.BOLD + "ERROR: " + ChatColor.RESET + ChatColor.RED + "You are not the required rank to Prestige!");
                    return false;
                }
                if (sender.hasPermission("prison.prestige3")) {
                    if (sender instanceof Player) {
                        Player player = (Player) sender;
                        World world = player.getServer().getWorld(getName());
                        String worldName = world.toString();
     
                    }
                    else {
                        sender.sendMessage(ChatColor.DARK_RED.toString() + ChatColor.BOLD + "ERROR: " + ChatColor.RESET + ChatColor.RED + " You must be a player to execute this command!");
                        return false;
                    }
            }
                else {
                    sender.sendMessage(ChatColor.DARK_RED.toString() + ChatColor.BOLD + "ERROR: " + ChatColor.RESET + ChatColor.RED + "You are not the required rank to Prestige!");
                }
               
                if (sender.hasPermission("prison.prestige4")) {
                    if (sender instanceof Player) {
                        Player player = (Player) sender;
                        World world = player.getServer().getWorld(getName());
                        String worldName = world.toString();
     
                    }
                    else {
                        sender.sendMessage(ChatColor.DARK_RED.toString() + ChatColor.BOLD + "ERROR: " + ChatColor.RESET + ChatColor.RED + " You must be a player to execute this command!");
                        return false;
                    }
               
            }
                else {
                    sender.sendMessage(ChatColor.DARK_RED.toString() + ChatColor.BOLD + "ERROR: " + ChatColor.RESET + ChatColor.RED + "You are not the required rank to Prestige!");
                    return false;
                }
       
                if (sender.hasPermission("prison.prestige5")) {
                    if (sender instanceof Player) {
                        Player player = (Player) sender;
                        World world = player.getServer().getWorld(getName());
                        String worldName = world.toString();
     
                    }
                    else {
                        sender.sendMessage(ChatColor.DARK_RED.toString() + ChatColor.BOLD + "ERROR: " + ChatColor.RESET + ChatColor.RED + " You must be a player to execute this command!");
                        return false;
                    }
            }
                else {
                    sender.sendMessage(ChatColor.DARK_RED.toString() + ChatColor.BOLD + "ERROR: " + ChatColor.RESET + ChatColor.RED + "You are not the required rank to Prestige!");
                    return false;
                }
               
                if (sender.hasPermission("prison.prestige6")) {
                    if (sender instanceof Player) {
                        Player player = (Player) sender;
                        World world = player.getServer().getWorld(getName());
                        String worldName = world.toString();
     
                    }
                    else {
                        sender.sendMessage(ChatColor.DARK_RED.toString() + ChatColor.BOLD + "ERROR: " + ChatColor.RESET + ChatColor.RED + " You must be a player to execute this command!");
                        return false;
                    }
            }
                else {
                    sender.sendMessage(ChatColor.DARK_RED.toString() + ChatColor.BOLD + "ERROR: " + ChatColor.RESET + ChatColor.RED + "You are not the required rank to Prestige!");
                    return false;
                }
               
                if (sender.hasPermission("prison.prestige7")) {
                    if (sender instanceof Player) {
                        Player player = (Player) sender;
                        World world = player.getServer().getWorld(getName());
                        String worldName = world.toString();
     
                    }
                    else {
                   
                        sender.sendMessage(ChatColor.DARK_RED.toString() + ChatColor.BOLD + "ERROR: " + ChatColor.RESET + ChatColor.RED + " You must be a player to execute this command!");
                        return false;
                    }
               
            }
                else {
                    sender.sendMessage(ChatColor.DARK_RED.toString() + ChatColor.BOLD + "ERROR: " + ChatColor.RESET + ChatColor.RED + "You are not the required rank to Prestige!");
                    return false;
                }
                if (sender.hasPermission("prison.prestige8")) {
                    if (sender instanceof Player) {
                        Player player = (Player) sender;
                        World world = player.getServer().getWorld(getName());
                        String worldName = world.toString();
     
                    }
                    else {
                        sender.sendMessage(ChatColor.DARK_RED.toString() + ChatColor.BOLD + "ERROR: " + ChatColor.RESET + ChatColor.RED + " You must be a player to execute this command!");
                        return false;
                    }
            }
                else {
                    sender.sendMessage(ChatColor.DARK_RED.toString() + ChatColor.BOLD + "ERROR: " + ChatColor.RESET + ChatColor.RED + "You are not the required rank to Prestige!");
                    return false;
                }
                if (sender.hasPermission("prison.prestige9")) {
                    if (sender instanceof Player) {
                        Player player = (Player) sender;
                        World world = player.getServer().getWorld(getName());
                        String worldName = world.toString();
     
                    }
                    else {
                        sender.sendMessage(ChatColor.DARK_RED.toString() + ChatColor.BOLD + "ERROR: " + ChatColor.RESET + ChatColor.RED + " You must be a player to execute this command!");
                        return false;
    }
               
            }
                else {
                    sender.sendMessage(ChatColor.DARK_RED.toString() + ChatColor.BOLD + "ERROR: " + ChatColor.RESET + ChatColor.RED + "You are not the required rank to Prestige!");
                    return false;
                }
                if (sender.hasPermission("prison.prestige10")) {
                    if (sender instanceof Player) {
                        Player player = (Player) sender;
                        World world = player.getServer().getWorld(getName());
                        String worldName = world.toString();
     
                    }
                    else {
                        sender.sendMessage(ChatColor.DARK_RED.toString() + ChatColor.BOLD + "ERROR: " + ChatColor.RESET + ChatColor.RED + " You must be a player to execute this command!");
                        return false;
                    }
       
            }
                else {
                    sender.sendMessage(ChatColor.DARK_RED.toString() + ChatColor.BOLD + "ERROR: " + ChatColor.RESET + ChatColor.RED + "You are not the required rank to Prestige!");
                    return false;
                }
               
        }
            return true;
    }
    }
    
    I repeat that 10 times for a total of 10 prestiges. When I go to test the plugin, here is the error from console:
    Code:
    >prestige
    [15:07:05 WARN]: Unexpected exception while parsing console command "prestige"
    org.bukkit.command.CommandException: Unhandled exception executing command 'pres
    tige' in plugin PrisonPrestige v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[cra
    ftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    0) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServe
    r.java:740) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchServerCommand(Craf
    tServer.java:726) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            at net.minecraft.server.v1_7_R4.DedicatedServer.aB(DedicatedServer.java:
    294) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:2
    59) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:5
    58) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java
    :469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:6
    28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_7_R4.command.
    ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
            at me.MrConHD.Prestiges.Core.onCommand(Core.java:36) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[cra
    ftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            ... 8 more
    >
    Any ideas?
     
  2. Offline

    NovaGamingBrian

    Dont use the actual plugin it self
    Use the vault api to hook into pex
     
  3. Offline

    TGRHavoc

    MrConHD
    Yea, don't use the command as the console and try as a player! Also, if you're making a plugin that only affects the player you should make sure that the sender is an instance of a player then cast the sender to a player. Doing this will stop this error occurring again.
     
  4. Offline

    MrConHD

    I was actually in game, however I did it in console as well. I get an Internal Error Exception in game and that error is displayed in Console, I am trying what NovaGamingBrian had said currently, I will let you know of the outcome :)

    Thank you both for the insight and feedback!
     
  5. Offline

    TGRHavoc

    MrConHD
    If you're getting a different error when you issue the command in-game then post that as well, it will allow people to help you solve your problem faster.
     
  6. Offline

    MrConHD

    In-Game Command issued when I run /prestige: An internal error occurred while attempting to perform this command
     
  7. Offline

    TGRHavoc

    Paste the error that shows in the console when you type the command in-game. Not what you see in-game when you type the command.
     
    mine-care likes this.
  8. Offline

    mine-care

    Player player = (Player) sender;
    Fix it please...

    And it would be a good idea to provide full class so we could trace the lines with bugz and reply instantly :)
    Also in the error by the console it is the cast. It should fix the problem
     
  9. Offline

    MrConHD


    It is the same error that is in the original post.

    mine-care Fixed :) Full class is now provided as well.
     
  10. Offline

    TGRHavoc

    MrConHD
    It cannot be the same error as in the OP as you're not casting "ConsoleSender" to a "Player"..
     
  11. Offline

    MrConHD

    TGRHavoc

    Here it is, my apologies:

    Code:
    [15:39:44 INFO]: MrConHD issued server command: /prestige
    [15:39:44 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'pres
    tige' in plugin PrisonPrestige v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[cra
    ftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    0) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServe
    r.java:740) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            at net.minecraft.server.v1_7_R4.PlayerConnection.handleCommand(PlayerCon
    nection.java:957) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java
    :818) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            at net.minecraft.server.v1_7_R4.PacketPlayInChat.a(PacketPlayInChat.java
    :28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            at net.minecraft.server.v1_7_R4.PacketPlayInChat.handle(PacketPlayInChat
    .java:47) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:157
    ) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            at net.minecraft.server.v1_7_R4.ServerConnection.c(SourceFile:134) [craf
    tbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:6
    67) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:2
    58) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:5
    58) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java
    :469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:6
    28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
    Caused by: java.lang.NullPointerException
            at me.MrConHD.Prestiges.Core.onCommand(Core.java:51) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[cra
    ftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            ... 13 more
    >
     
  12. Offline

    Skionz

    MrConHD NPE in the 'Core' class on line 51. Not sure what it is because I need the updated code.
     
  13. Offline

    MrConHD

    Skionz

    NPE is onl ine - String worldName = world.toString();
     
  14. Offline

    Skionz

    MrConHD What is this?
    Code:
    World world = player.getServer().getWorld(getName());
    I can't find a getName() method. Are you getting a syntax error?
     
  15. Offline

    MrConHD

    Skionz

    I receive a syntax error when I remove the getName() method, not when I have it there.

    EDIT:

    Fixed, changed it to player.getWorld().getName();

    Now I am getting the ERROR messages that I would like displayed when the player does not hav permission or the console executes the command, I just need to hook into Vault API to set the users group, correct?
     
  16. Offline

    Skionz

    MrConHD That should work. The getName you were using was a method in the PluginBase class.
     
  17. Offline

    MrConHD

    No errors are being displayed in console either.

    Skionz

    Alright, I should be fine on my own implementing Vault. I appreciate your help! Thank you very much!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 29, 2016
  18. Offline

    TGRHavoc

    MrConHD
    Just in case you're still getting an error, it's "World world = player.getWorld()" the "getName()" methods returns a string and not a World.
     
Thread Status:
Not open for further replies.

Share This Page