Solved Console and Block Help

Discussion in 'Plugin Development' started by Stix, Mar 29, 2017.

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

    Stix

    Okay so basically my first issue is that when console tries to run my plugin's command it throws and error

    Error:

    Code:
    [22:03:30] [Server thread/WARN]: Unexpected exception while parsing console command "pickaxe give ImOnABoat"
    org.bukkit.command.CommandException: Unhandled exception executing command 'pickaxe' in plugin ShockwavePickaxe v1.0.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:627) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:412) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:375) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_121]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R3.command.ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
        at com.Willis.ShockwavePickaxe.Main.onCommand(Main.java:51) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
        ... 8 more
    
    My 2nd issue is that I have a pickaxe setup so it will allow me to break a 3x3 area but I need to stop it from being able to break bedrock so player's can't get into the void..

    Code:
    @Overridepublic boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    if ((!(sender instanceof Player) || (sender instanceof Player && sender.hasPermission("pickaxe.access")))) {
    if (command.getName().equalsIgnoreCase("pickaxe")) {
    Player p = (Player) sender;
    
    if (args.length == 0) {
    givePickaxe(p);
    return true;
    }
    
    if (args.length == 1) {
    if (args[0].equalsIgnoreCase("give")) {
    
    sender.sendMessage("Pleave provide the player.");
    return true;
    }
    }
    Player p1 = Bukkit.getServer().getPlayer(args[1]);
    
    if (p1 == null) {
    sender.sendMessage("Player isn't online");
    return true;
    }
    
    givePickaxe(p1);
    }
    }
    return false;
    }
    
    @EventHandlerpublic void mineBock(BlockBreakEvent event) {
    
    Player player = (Player) event.getPlayer();
    
    Block block = event.getBlock();
    
    if (player.getItemInHand().equals(Material.AIR)) {
    return;
    }
    
    if (!(player.getItemInHand().hasItemMeta())) {
    return;
    }
    
    if (player.getItemInHand().getItemMeta() == null) {
    return;
    }
    
    if (player.getItemInHand().getItemMeta().getDisplayName() == null) {
    return;
    }
    
    if (player.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase("KOOL PICKAXE")) {
    Block currentBlock = null;
    
    for (int xOff = -1; xOff <= 1; ++xOff) {
    for (int yOff = -2; yOff <= 0; ++yOff) {
    for (int zOff = -1; zOff <= 1; ++zOff) {
    currentBlock = block.getRelative(xOff, yOff, zOff);
    currentBlock.breakNaturally();
    }
    }
    }
    }else {
    
    return;
    
    }
    }
    }
    
     
  2. Offline

    Zombie_Striker

    @Stix
    1. Why are you still on 1.8? Please read this;
    2. You are allowing things that are NOT A PLAYER to be EQUAL TO A PLAYER. Remove the !sender instanceof Player bit.
     
  3. Offline

    Stix

    But if I remove that then it will check if the player is instance is equal to the player and if it's not then what will console do when I try to run the command? Also, I don't see the big deal about 1.8.8 many massive servers still run 1.8.8
     
  4. Offline

    Drkmaster83

    What will console do? Whatever you tell it to. Instead of givePickaxe, maybe check for a target player to give a pickaxe to, requiring more arguments. But, a console isn't a player, so that's where you get your ClassCastException.

    1.8.8... is old. @Zombie_Striker has made an entire post about why you should use the most recent version, it doesn't really bother me much, but it's primarily to do with security. If it's already mainstream and you can code pvp back to the way it was in 1.8, why not take the extra blocks and features?
     
    Zombie_Striker likes this.
  5. Offline

    Stix

    Yea My givePickaxe method consists of

    Code:
    public void givePickaxe(Player player) {
    
    ItemStack pick = new ItemStack(Material.DIAMOND_PICKAXE);
    ItemMeta pickm = pick.getItemMeta();
    pickm.setDisplayName("KOOL PICKAXE");
    pick.setItemMeta(pickm);
    
    player.getInventory().addItem(pick);
    
    player.sendMessage("You recieved a KOOL PICKAXE");
    
    }
    
     
  6. Offline

    Drkmaster83

    I wasn't asking about the method, though. Think about this:
    Your console is literally just a text output/input. Does it have a hand or an inventory to put the pickaxe in? No. So the server gets mad at you for making it think it could. You can't use the method on a console just because you tell the server the console is a player.
     
  7. Offline

    Zombie_Striker

    @Stix
    Simply put, there will be a point where you will be almost forced to stay on 1.8, staying with only the plugins you currently have. There are plugins out there that already no longer support 1.8, and there any many of devs that refuse to help 1.8 servers. This means that you will only have the plugins that were created for 1.8, and if any of them break, you may be out of luck. Not only this, but you are also susceptible to hacks and exploits. The longer you stay on an update, the more time people have to develop or find mods potentially harm your server.

    The thing is, the main reason why users stay on 1.8 is because of the PVP system, something that can easily be reverted back to in 1.11.

    AS Drkmaster83 posted, you can not use consoles. You Have to make sure the sender is a player before you cast it.
     
  8. Offline

    Stix

    Yes I know that, but I have it also setup so I can do /pickaxe give <PlayerName> why wouldn't that work?
     
  9. Offline

    Zombie_Striker

    @Stix
    Because you are still casting sender to a player. If you want to allow consoles to access the command, do not cast the sender to a player. Since the only time you need to cast sender to a player is when there are no args, check the args length first before checking the instance. If the args length is equal to 0 or 1, check to make sure it is a player. If the args is equal to 2, do not check and do not create the player variable.
     
Thread Status:
Not open for further replies.

Share This Page