"An internal error occurred while attempting to perform this command"

Discussion in 'Plugin Development' started by _Fush_, May 31, 2015.

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

    _Fush_

    Hello! I am a fairly new bukkit programmer, and I am trying to create a "Economy" plugin which gives the user money. I was recieving the error "An internal error occurred while attempting to perform this command" when entering in the command that was suppost to add to a players balance.
    Spoiler: Code (open)

    package pack.eco;

    import java.util.HashMap;

    import net.md_5.bungee.api.ChatColor;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin {

    @Override
    public void onEnable() {
    getLogger().info("You have enabled this plugin.");
    }
    @Override
    public void onDisable() {
    getLogger().info("You have disabled this plugin.");
    }

    HashMap<String, Integer> EcoF = new HashMap<String, Integer>();

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    if(cmd.getName().equalsIgnoreCase("EcoF")){
    Player player = (Player) sender;

    if ( !args[1].matches("-?\\d+"))
    // ** To not allow negative integers, remove '-?' **
    sender.sendMessage("Please insert a positive integer.");
    else {
    int amount = Integer.parseInt(args[1]);

    EcoF.put(player.getName(), amount);

    }
    }
    return false;











    }

    }



    I feel like I am missing something, but can't find it. What does this error mean, and how can I rid it? A small explanation would be appreciated :)
     
  2. Offline

    Konato_K

    @_Fush_ The error is shown to the players whenever an uncatched exception occurred in the execution of a command.

    The first thing I can see in your code that can thrown an exception is Player player = (Player)sender;

    The command sender is not always a player, this will thrown a ClassCastException if executed by the server console, a command block, or other types of command senders.

    You're also trying to get the second argument (args[1]) without even checking if it's there, this throws an ArrayIndexOutOfBoundsException.

    Also, why don't you just parte the argument as integer and check if it's negative?
     
  3. Offline

    nverdier

    @_Fush_ Most likely it's that you didn't provide 2 arguments. Remember, Java uses a 0-indexed system! And add a check to make sure the length of 'args' is long enough to get the amount of arguments you want!

    Another thing is that if the sender isn't instance of Player (e.g. console, commandblock), it will throw an error when you try to cast to Player.
     
  4. Offline

    _Fush_

    Thank you two for the help! I will change those 3 things.
     
  5. Offline

    CoolGamerXD

    Yup, That's very true like @nverdier said. Always check if sender is instanceof Player , before casting a Player is a player. Secondly, You only have just 1 argument right? just /ecof [amount] . So since in Java, numbers starts from 0, You would have:
    Code:
     int amount = Integer.parseInt(args[0]); 
     
  6. Offline

    _Fush_

    @CoolGamerXD Yes, that is what I wanted. If you wanted to know (because you can't tell from looking at my weird code) I am trying to make a HashMap for each player which stores a balance. The command is suppost to add money according to the number after EcoF. I am going to add a command that displays the users balance, but I am not quite sure how to call for that specific persons number. I also want a counter to make sure no balance goes under zero, but this should be easier. Hopefully some more research will prevail.

    This is the error I get when trying to use this new code, I also got this same error before.

    EDIT: I solved this problem. I will mark the thread as solved and ask if I have another question on another thread. Thank you for all the help!

    New Code (open)

    package pack.eco;

    import java.util.HashMap;

    import net.md_5.bungee.api.ChatColor;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin {

    @Override
    public void onEnable() {
    getLogger().info("You have enabled this plugin.");
    }
    @Override
    public void onDisable() {
    getLogger().info("You have enabled this plugin.");
    }

    HashMap<String, Integer> EcoF = new HashMap<String, Integer>();

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){

    if(!(sender instanceof Player)) {
    sender.sendMessage(ChatColor.RED + "Sorry, but this command can only be run by a player.");
    }

    if(cmd.getName().equalsIgnoreCase("EcoF")){
    Player player = (Player) sender;
    String myString = "";

    for(int i = 0; i < args.length; i++){
    String arg = args + " ";
    myString = myString + arg;
    sender.sendMessage(ChatColor.RED + myString + " was added to your balance.");

    }



    }

    return false;











    }

    }




    Error Spoiler (open)

    Java HotSpot(TM) Client VM warning: ignoring option MaxPermSize=128M; support wa
    s removed in 8.0
    Loading libraries, please wait...
    [17:09:03 INFO]: Starting minecraft server version 1.8.3
    [17:09:03 INFO]: Loading properties
    [17:09:03 INFO]: Default game type: SURVIVAL
    [17:09:03 INFO]: This server is running CraftBukkit version git-Spigot-0b997a9-3
    0d751b (MC: 1.8.3) (Implementing API version 1.8.3-R0.1-SNAPSHOT)
    [17:09:03 INFO]: Debug logging is disabled
    [17:09:03 INFO]: Server Ping Player Sample Count: 12
    [17:09:03 INFO]: Using 4 threads for Netty based IO
    [17:09:03 INFO]: Generating keypair
    [17:09:04 INFO]: Starting Minecraft server on *:25565
    [17:09:04 INFO]: Using default channel type
    [17:09:04 INFO]: Set PluginClassLoader as parallel capable
    [17:09:04 INFO]: [EcoF] Loading EcoF v1.0
    [17:09:04 INFO]: **** Beginning UUID conversion, this may take A LONG time ****
    [17:09:04 INFO]: Preparing level "world"
    [17:09:04 INFO]: -------- World Settings For [world] --------
    [17:09:04 INFO]: Item Despawn Rate: 6000
    [17:09:04 INFO]: Item Merge Radius: 2.5
    [17:09:04 INFO]: Allow Zombie Pigmen to spawn from portal blocks: true
    [17:09:04 INFO]: Arrow Despawn Rate: 1200
    [17:09:04 INFO]: Mob Spawn Range: 4
    [17:09:04 INFO]: Nerfing mobs spawned from spawners: false
    [17:09:04 INFO]: Cactus Growth Modifier: 100%
    [17:09:04 INFO]: Cane Growth Modifier: 100%
    [17:09:04 INFO]: Melon Growth Modifier: 100%
    [17:09:04 INFO]: Mushroom Growth Modifier: 100%
    [17:09:04 INFO]: Pumpkin Growth Modifier: 100%
    [17:09:04 INFO]: Sapling Growth Modifier: 100%
    [17:09:04 INFO]: Wheat Growth Modifier: 100%
    [17:09:04 INFO]: Entity Activation Range: An 32 / Mo 32 / Mi 16
    [17:09:04 INFO]: Entity Tracking Range: Pl 48 / An 48 / Mo 48 / Mi 32 / Other 64

    [17:09:04 INFO]: Random Lighting Updates: false
    [17:09:04 INFO]: Structure Info Saving: true
    [17:09:04 INFO]: Sending up to 5 chunks per packet
    [17:09:04 INFO]: Max TNT Explosions: 100
    [17:09:04 INFO]: Tile Max Tick Time: 50ms Entity max Tick Time: 50ms
    [17:09:04 INFO]: View Distance: 10
    [17:09:04 INFO]: Zombie Aggressive Towards Villager: true
    [17:09:04 INFO]: Experience Merge Radius: 3.0
    [17:09:04 INFO]: Chunks to Grow per Tick: 650
    [17:09:04 INFO]: Clear tick list: false
    [17:09:04 INFO]: Max Entity Collisions: 8
    [17:09:04 INFO]: Custom Map Seeds: Village: 10387312 Feature: 14357617
    [17:09:04 INFO]: Anti X-Ray: true
    [17:09:04 INFO]: Engine Mode: 1
    [17:09:04 INFO]: Hidden Blocks: [14, 15, 16, 21, 48, 49, 54, 56, 73, 74,
    82, 129, 130]
    [17:09:04 INFO]: Replace Blocks: [1, 5]
    [17:09:04 INFO]: Hopper Transfer: 8 Hopper Check: 8 Hopper Amount: 1
    [17:09:04 INFO]: -------- World Settings For [world_nether] --------
    [17:09:04 INFO]: Item Despawn Rate: 6000
    [17:09:04 INFO]: Item Merge Radius: 2.5
    [17:09:04 INFO]: Allow Zombie Pigmen to spawn from portal blocks: true
    [17:09:04 INFO]: Arrow Despawn Rate: 1200
    [17:09:04 INFO]: Mob Spawn Range: 4
    [17:09:04 INFO]: Nerfing mobs spawned from spawners: false
    [17:09:04 INFO]: Cactus Growth Modifier: 100%
    [17:09:04 INFO]: Cane Growth Modifier: 100%
    [17:09:04 INFO]: Melon Growth Modifier: 100%
    [17:09:04 INFO]: Mushroom Growth Modifier: 100%
    [17:09:04 INFO]: Pumpkin Growth Modifier: 100%
    [17:09:04 INFO]: Sapling Growth Modifier: 100%
    [17:09:04 INFO]: Wheat Growth Modifier: 100%
    [17:09:04 INFO]: Entity Activation Range: An 32 / Mo 32 / Mi 16
    [17:09:04 INFO]: Entity Tracking Range: Pl 48 / An 48 / Mo 48 / Mi 32 / Other 64

    [17:09:04 INFO]: Random Lighting Updates: false
    [17:09:04 INFO]: Structure Info Saving: true
    [17:09:04 INFO]: Sending up to 5 chunks per packet
    [17:09:04 INFO]: Max TNT Explosions: 100
    [17:09:04 INFO]: Tile Max Tick Time: 50ms Entity max Tick Time: 50ms
    [17:09:04 INFO]: View Distance: 10
    [17:09:04 INFO]: Zombie Aggressive Towards Villager: true
    [17:09:04 INFO]: Experience Merge Radius: 3.0
    [17:09:04 INFO]: Chunks to Grow per Tick: 650
    [17:09:04 INFO]: Clear tick list: false
    [17:09:04 INFO]: Max Entity Collisions: 8
    [17:09:04 INFO]: Custom Map Seeds: Village: 10387312 Feature: 14357617
    [17:09:04 INFO]: Anti X-Ray: true
    [17:09:04 INFO]: Engine Mode: 1
    [17:09:04 INFO]: Hidden Blocks: [14, 15, 16, 21, 48, 49, 54, 56, 73, 74,
    82, 129, 130]
    [17:09:04 INFO]: Replace Blocks: [1, 5]
    [17:09:04 INFO]: Hopper Transfer: 8 Hopper Check: 8 Hopper Amount: 1
    [17:09:04 INFO]: -------- World Settings For [world_the_end] --------
    [17:09:04 INFO]: Item Despawn Rate: 6000
    [17:09:04 INFO]: Item Merge Radius: 2.5
    [17:09:04 INFO]: Allow Zombie Pigmen to spawn from portal blocks: true
    [17:09:04 INFO]: Arrow Despawn Rate: 1200
    [17:09:04 INFO]: Mob Spawn Range: 4
    [17:09:04 INFO]: Nerfing mobs spawned from spawners: false
    [17:09:04 INFO]: Cactus Growth Modifier: 100%
    [17:09:04 INFO]: Cane Growth Modifier: 100%
    [17:09:04 INFO]: Melon Growth Modifier: 100%
    [17:09:04 INFO]: Mushroom Growth Modifier: 100%
    [17:09:04 INFO]: Pumpkin Growth Modifier: 100%
    [17:09:04 INFO]: Sapling Growth Modifier: 100%
    [17:09:04 INFO]: Wheat Growth Modifier: 100%
    [17:09:04 INFO]: Entity Activation Range: An 32 / Mo 32 / Mi 16
    [17:09:04 INFO]: Entity Tracking Range: Pl 48 / An 48 / Mo 48 / Mi 32 / Other 64

    [17:09:04 INFO]: Random Lighting Updates: false
    [17:09:04 INFO]: Structure Info Saving: true
    [17:09:04 INFO]: Sending up to 5 chunks per packet
    [17:09:04 INFO]: Max TNT Explosions: 100
    [17:09:04 INFO]: Tile Max Tick Time: 50ms Entity max Tick Time: 50ms
    [17:09:04 INFO]: View Distance: 10
    [17:09:04 INFO]: Zombie Aggressive Towards Villager: true
    [17:09:04 INFO]: Experience Merge Radius: 3.0
    [17:09:04 INFO]: Chunks to Grow per Tick: 650
    [17:09:04 INFO]: Clear tick list: false
    [17:09:04 INFO]: Max Entity Collisions: 8
    [17:09:04 INFO]: Custom Map Seeds: Village: 10387312 Feature: 14357617
    [17:09:04 INFO]: Anti X-Ray: true
    [17:09:04 INFO]: Engine Mode: 1
    [17:09:04 INFO]: Hidden Blocks: [14, 15, 16, 21, 48, 49, 54, 56, 73, 74,
    82, 129, 130]
    [17:09:04 INFO]: Replace Blocks: [1, 5]
    [17:09:04 INFO]: Hopper Transfer: 8 Hopper Check: 8 Hopper Amount: 1
    [17:09:04 INFO]: Preparing start region for level 0 (Seed: 7183208181278876073)
    [17:09:05 INFO]: Preparing spawn area: 99%
    [17:09:05 INFO]: Preparing start region for level 1 (Seed: 5019038948302057346)
    [17:09:06 INFO]: Preparing spawn area: 45%
    [17:09:07 INFO]: Preparing spawn area: 98%
    [17:09:07 INFO]: Preparing start region for level 2 (Seed: 5019038948302057346)
    [17:09:08 INFO]: [EcoF] Enabling EcoF v1.0
    [17:09:08 INFO]: [EcoF] You have enabled this plugin.
    [17:09:08 INFO]: Server permissions file permissions.yml is empty, ignoring it
    [17:09:08 INFO]: Done (3.935s)! For help, type "help" or "?"
    >ecof
    [17:09:22 INFO]: Sorry, but this command can only be run by a player.
    [17:09:22 WARN]: Unexpected exception while parsing console command "ecof"
    org.bukkit.command.CommandException: Unhandled exception executing command 'ecof
    ' in plugin EcoF v1.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spi
    got-1.8.3.jar:git-Spigot-0b997a9-30d751b]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:14
    1) ~[spigot-1.8.3.jar:git-Spigot-0b997a9-30d751b]
    at org.bukkit.craftbukkit.v1_8_R2.CraftServer.dispatchCommand(CraftServe
    r.java:646) ~[spigot-1.8.3.jar:git-Spigot-0b997a9-30d751b]
    at org.bukkit.craftbukkit.v1_8_R2.CraftServer.dispatchServerCommand(Craf
    tServer.java:632) [spigot-1.8.3.jar:git-Spigot-0b997a9-30d751b]
    at net.minecraft.server.v1_8_R2.DedicatedServer.aN(DedicatedServer.java:
    405) [spigot-1.8.3.jar:git-Spigot-0b997a9-30d751b]
    at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:3
    69) [spigot-1.8.3.jar:git-Spigot-0b997a9-30d751b]
    at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:6
    51) [spigot-1.8.3.jar:git-Spigot-0b997a9-30d751b]
    at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java
    :554) [spigot-1.8.3.jar:git-Spigot-0b997a9-30d751b]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R2.command.
    ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
    at pack.eco.Main.onCommand(Main.java:33) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spi
    got-1.8.3.jar:git-Spigot-0b997a9-30d751b]
    ... 8 more
    >
     
    Last edited by a moderator: Jun 1, 2015
  7. Offline

    FloopThatGuy5

    @_Fush_ It is because you imported the wrong chat color,
    net.md_5.bungee.api.ChatColor; <- is for Bungee.
    You need to import the other choice which is the bukkit one! :)

    and you need to move the sender instance stuff to inside the actual command. (if(cmd.getName()....)
     
  8. Offline

    Konato_K

    @FloopThatGuy5 Yes, he imported the wrong ChatColor, but he is not calling it anywhere, so that won't throw any errors.
     
  9. Offline

    I Al Istannen

    @_Fush_ You CAN'T cast a console to a player. You do check, which is nice but you don't stop the execution then. You will need to add an "return true" after sending the message that the console is no player.
     
Thread Status:
Not open for further replies.

Share This Page