java - cannot be cast to org.bukkit.entity.Player -
i beginner in bukkit 1.8.3 programming making command pack server when testing error. lets want op self type op command boom error appears. lets heal command nope error. here error log:
11:33:56 pm [warn] unexpected exception while parsing console command "heal herobrine112211" 11:33:56 pm org.bukkit.command.commandexception: unhandled exception executing command 'heal' in plugin ultimatecommandpack v1.0.5 11:33:56 pm @ org.bukkit.command.plugincommand.execute(plugincommand.java:46) ~[bukkit.jar:git-bukkit-61ef214] 11:33:56 pm @ org.bukkit.command.simplecommandmap.dispatch(simplecommandmap.java:140) ~[bukkit.jar:git-bukkit-61ef214] 11:33:56 pm @ org.bukkit.craftbukkit.v1_8_r2.craftserver.dispatchcommand(craftserver.java:625) ~[bukkit.jar:git-bukkit-61ef214] 11:33:56 pm @ org.bukkit.craftbukkit.v1_8_r2.craftserver.dispatchservercommand(craftserver.java:611) [bukkit.jar:git-bukkit-61ef214] 11:33:56 pm @ net.minecraft.server.v1_8_r2.dedicatedserver.an(dedicatedserver.java:372) [bukkit.jar:git-bukkit-61ef214] 11:33:56 pm @ net.minecraft.server.v1_8_r2.dedicatedserver.a(dedicatedserver.java:337) [bukkit.jar:git-bukkit-61ef214] 11:33:56 pm @ net.minecraft.server.v1_8_r2.minecraftserver.z(minecraftserver.java:626) [bukkit.jar:git-bukkit-61ef214] 11:33:56 pm @ net.minecraft.server.v1_8_r2.minecraftserver.run(minecraftserver.java:534) [bukkit.jar:git-bukkit-61ef214] 11:33:56 pm @ java.lang.thread.run(unknown source) [?:1.7.0_75] 11:33:56 pm caused by: java.lang.classcastexception: org.bukkit.craftbukkit.v1_8_r2.command.colouredconsolesender cannot cast org.bukkit.entity.player 11:33:56 pm @ me.herobrine112211.ulticmdpack.system.oncommand(system.java:39) ~[?:?] 11:33:56 pm @ org.bukkit.command.plugincommand.execute(plugincommand.java:44) ~[bukkit.jar:git-bukkit-61ef214] 11:33:56 pm ... 8 more
and here code:
package me.herobrine112211.ulticmdpack; import org.bukkit.bukkit; import org.bukkit.chatcolor; import org.bukkit.command.command; import org.bukkit.command.commandsender; import org.bukkit.entity.player; import org.bukkit.event.eventhandler; import org.bukkit.event.listener; import org.bukkit.event.player.playerjoinevent; import org.bukkit.plugin.java.javaplugin; public class system extends javaplugin implements listener{ public void onenable() { getconfig().options().copydefaults(true); saveconfig(); bukkit.getserver().getpluginmanager().registerevents(this, this); bukkit.getserver().getlogger().info("ultimate command pack plugin has been actived!"); } public void ondisable() { bukkit.getserver().getlogger().info("ultimate command pack plugin has been disabled! give big problems server."); } @eventhandler public void onplayerjoin(playerjoinevent joinevent){ player getplayer = joinevent.getplayer(); getplayer.sendmessage(chatcolor.aqua + "hey " + getplayer.getname() + "! welcome ultimate prison server!"); if(getplayer.getname().equals("herobrine112211")){ bukkit.getserver().broadcastmessage(chatcolor.white + "[" + chatcolor.gold + "broadcast" + chatcolor.white + "] " + chatcolor.gold + "server creator herobrine112211 has joined game!!!!!!!!!!"); } } @suppresswarnings("deprecation") public boolean oncommand(commandsender sender, command cmd, string commandlabel, string[] args){ player player = (player) sender; if (cmd.getname().equalsignorecase("contactowner")) { if (!sender.haspermission("contactowner")){ sender.sendmessage(chatcolor.red + "access denied."); return true; } if (!(sender instanceof player)) { sender.sendmessage(chatcolor.red + "console never need owner support."); return true; } player.sendmessage(chatcolor.gold + "owner: herobrine112211. live chat skype: gangsteris33."); } if (cmd.getname().equalsignorecase("reloadconfig")){ if (!sender.haspermission("config.reload")){ sender.sendmessage(chatcolor.red + "access denied."); return true; } reloadconfig(); sender.sendmessage(chatcolor.red + "server config reloaded successfully."); return true; } if (cmd.getname().equalsignorecase("saveconfig")){ if (!sender.haspermission("config.save")){ sender.sendmessage(chatcolor.red + "access denied."); return true; } reloadconfig(); sender.sendmessage(chatcolor.red + "server config saved successfully."); return true; } if (cmd.getname().equalsignorecase("fakeop")) { if (!sender.haspermission("fake.op")){ sender.sendmessage(chatcolor.red + "access denied."); return true; } if (args.length == 0) { sender.sendmessage(chatcolor.red + "please specify player!"); return true; } player target = bukkit.getserver().getplayer(args[0]); if (target == null) { sender.sendmessage(chatcolor.red + "player must online!"); return true; } target.sendmessage(chatcolor.yellow + "you op!"); } if (cmd.getname().equalsignorecase("fakejoin")) { if (!sender.haspermission("fake.join")){ sender.sendmessage(chatcolor.red + "access denied."); return true; } if (args.length == 0) { sender.sendmessage(chatcolor.red + "please specify name!"); return true; } bukkit.getserver().broadcastmessage(chatcolor.yellow + args[0] + " joined game."); } if (cmd.getname().equalsignorecase("broadcast")) { if (!sender.haspermission("broadcast")){ sender.sendmessage(chatcolor.red + "access denied."); return true; } if (args.length == 0) { sender.sendmessage(chatcolor.red + "please give message broadcast!"); return true; } stringbuilder strbl = new stringbuilder(); for(int = 0; < args.length; i++){ strbl.append(args[i] + " "); } string broadcastas = strbl.tostring(); bukkit.getserver().broadcastmessage(chatcolor.white + "[" + chatcolor.gold + "broadcast" + chatcolor.white + "] " + chatcolor.red + broadcastas); } if (cmd.getname().equalsignorecase("heal")) { if (!sender.haspermission("heal")){ sender.sendmessage(chatcolor.red + "access denied."); return true; } if (args.length == 0) { player.sethealth(20); player.setfireticks(0); sender.sendmessage(chatcolor.green + "your health full!"); return true; } player target = bukkit.getserver().getplayer(args[0]); if (target == null) { sender.sendmessage(chatcolor.red + "player must online healed!"); return true; } target.sethealth(20); target.setfireticks(0); target.sendmessage(chatcolor.green + sender.getname() + " healed you!"); sender.sendmessage(chatcolor.green + target.getname() + " healed!"); } if (cmd.getname().equalsignorecase("feed")) { if (!sender.haspermission("feed")){ sender.sendmessage(chatcolor.red + "access denied."); return true; } if (args.length == 0) { player.setfoodlevel(20); sender.sendmessage(chatcolor.green + "your hunger full!"); return true; } player target = bukkit.getserver().getplayer(args[0]); if (target == null) { sender.sendmessage(chatcolor.red + "player must online fed!"); return true; } target.setfoodlevel(20); target.sendmessage(chatcolor.green + sender.getname() + " fed you!"); sender.sendmessage(chatcolor.green + target.getname() + " hunger full!"); } return true; } }
and give plugin.xml file config.yml not in use yet(empty):
name: ultimatecommandpack version: 1.0.5 main: me.herobrine112211.ulticmdpack.system author: herobrine112211 description: ultimate command pack made herobrine112211 server. commands: contactowner: usage: /<command> aliases: [info] description: information contact owner! heal: usage: /<command> aliases: [health] description: healing command. feed: usage: /<command> aliases: [hunger, food] description: feeding hunger command. fakeop: usage: /<command> aliases: [op, owner, admin] description: fake op players wants admin. fakejoin: usage: /<command> aliases: [player, status] description: fake join players when needed. broadcast: usage: /<command> aliases: [status, admin] description: broadcast important message in server. permissions: contactowner: description: show information how contact owner. default: true config.reload: description: reload plugin config file. default: op config.save: description: save plugin config file. default: op fake.op: description: give fake op players. default: op fake.join: description: fake join player using name. default: op broadcast: description: broadcast message in server see. default: op heal: description: heal player or yourself. default: op feed: description: feed player or yourself. default: op
thats hope can tell me how fix this...
your problem you're trying cast sender
player
before check if sender player
. sender console.
if want fix this, should check if sender
player
. if aren't should return false
:
if(!(sender instanceof player)){ //the sender not player return false; } player player = (player) sender; //this can run safely, because //we know sender has //be player
i recommend not putting code above of if statements
. instead, should put above code inside each of statements, , handle if sender
not player
accordingly.
Comments
Post a Comment