简体   繁体   English

Bukkit-为什么某些代码片段仅适用于OP?

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

For some reason a piece of my code only works with OPs. 出于某种原因,我的一段代码仅适用于OP。 And this piece of code is probably one of the only ones that is meant to NOT work with OPs. 而且这段代码可能是唯一不适合使用OP的代码之一。

My code is at: http://pastebin.com/sQeeXRNN 我的代码在: 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. if(!event.getWhoClicked().isOp()) {明确指出仅适用于非Op的人。 I have tried without the if statement and it still only works for OPs. 我尝试了没有if语句,它仍然仅适用于OP。 Any ideas? 有任何想法吗?

Thanks, Jay 谢谢,周杰伦

using 使用

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

will not work because InventoryClickEvent#getWhoClicked() returns a HumanEntity. 将不会起作用,因为InventoryClickEvent#getWhoClicked()返回了HumanEntity。 This is not a player. 这不是玩家。

To fix this issue you would first cast getWhoClicked() to a player like so: 要解决此问题,您首先需要将getWhoClicked()投射到播放器上,如下所示:

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. 在检查是否为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 : 并将该权限授予您plugin.yml内部的plugin.yml

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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