简体   繁体   中英

Bukkit - Why does a certain piece of code only work on OPs?

For some reason a piece of my code only works with OPs. And this piece of code is probably one of the only ones that is meant to NOT work with OPs.

My code is at: http://pastebin.com/sQeeXRNN

Now, the bit that isn't working is the

@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
if(!event.getWhoClicked().isOp()) {
//rest of it

Which as the if(!event.getWhoClicked().isOp()) { is clearly stating as only working for people who aren't Op. I have tried without the if statement and it still only works for OPs. Any ideas?

Thanks, Jay

using

if(!e.getWhoClicked().isOp())
{
    // code
}

will not work because InventoryClickEvent#getWhoClicked() returns a HumanEntity. This is not a player.

To fix this issue you would first cast getWhoClicked() to a player like so:

Player player = (Player) e.getWhoClicked();
if(!player.isOp())
{
    // continue code
}

You must always make sure that something is returning a player before checking if it is op.

Last time I checked, using these kind of checks are deprecated, use the permission system instead:

if (!event.getWhoClicked().hasPermission("yourplugin.bypassclick"))

and give that permission to ops inside your plugin.yml :

permissions:
  yourplugin.*:
    description: Gives all permission
    children:
      yourplugin.bypassclick: true
  yourplugin.bypassclick:
    description: Gives the permission to <do something>
    default: op

奇怪的是,服务器的简单重启解决了该问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM