gpt4 book ai didi

java - 如何查看 bukkit 插件中调用 setCancelled() 的类/插件?

转载 作者:行者123 更新时间:2023-11-30 06:52:00 29 4
gpt4 key购买 nike

我的 bukkit/spigot 插件中有一个自定义事件,它扩展了 PlayerInteractEvent它试图打开玩家周围附近区域的箱子。

当前代码使用此事件来确保没有其他插件(例如悲伤预防)反对玩家能够打开箱子。如果玩家可以打开箱子,我的插件将尝试将元素放入箱子中。如果 setCancelled() 由某个插件(理想情况下)或类(作为解决方法)调用,我想忽略它

来自this question我可以看到要获取我可以使用的类

String callerClassName = new Exception().getStackTrace()[1].getClassName();
String calleeClassName = new Exception().getStackTrace()[0].getClassName();

获取类名。或者我可以在这个调用中使用一些东西:

StackTraceElement[] stElements = Thread.currentThread().getStackTrace();

但是,关于该问题的所有评论都表明,除了这样做之外,可能还有更好的方法。

Bukkit 有更好的方法吗?

作为引用,这是我的自定义玩家交互事件的全部内容:

public class FakePlayerInteractEvent extends PlayerInteractEvent {
public FakePlayerInteractEvent(Player player, Action rightClickBlock, ItemStack itemInHand, Block clickedBlock, BlockFace blockFace) {
super(player, rightClickBlock, itemInHand, clickedBlock, blockFace);
}
}

以及围绕事件使用的代码:

PlayerInteractEvent fakeEvent = AutomaticInventory.getInstance().new FakePlayerInteractEvent(player, Action.RIGHT_CLICK_BLOCK, player.getInventory().getItemInMainHand(), block, BlockFace.UP);
Bukkit.getServer().getPluginManager().callEvent(fakeEvent);
if(!fakeEvent.isCancelled()){ ... do stuff }

最佳答案

问得好!暂时让我忽略刺激的原因这个问题。 Bukkit 并未“发布”确定方法事件取消的来源。然而,你的“评估”方法 Activity 正在步入正轨。

正如您已经知道或怀疑的那样,使用堆栈跟踪并不是一个好的解决方案。它们的生成和描述特定于实现的成本相对较高不一定保证保持不变的细节。一个更好的方法是模仿 Bukkit 调用时使用的事件触发过程callEvent()

虽然事件触发过程的实现不能由Bukkit API 多年来一直稳定且未发生变化很多。这是我们过去5年的工作,只需要一名未成年人当 callEvent() 拆分为 callEvent()/fireEvent() 时进行重构。

我希望能给你整个 EventUtils 辅助类,但是我由于版权问题,不得不对其进行编辑。我确实验证了这一点减少的类(class)通过了适当的单元测试。你或其他人是可以随意使用此代码。它的评论解释了操作更多细节。我应该注意,我们使用 Doxygen,而不是 JavaDoc,文档生成。

public class EventUtils {

/**
* @brief Determine if the given event will be cancelled.
*
* This method emulates Bukkit's SimplePluginManager.fireEvent() to evaluate whether it will
* be cancelled. This is preferred over using callEvent() as this method can limit the scope
* of evaluation to only plugins of interest. Furthermore, this method will terminate as soon
* as the event is cancelled to minimize any *side effects* from plugins further down the event
* chain (e.g. mcMMO). No evaluation will be performed for events that do not
* implement Cancellable.
*
* The given plugin set is interpreted as either an Allow set or a Deny set, as follows:
*
* - \c allowDeny = \c false - Allow mode. Only enabled plugins included in the given plugin
* set will be evaluated.
* - \c allowDeny = \c false - Deny mode. Only enabled plugins *not* included in the given
* plugin set will be evaluated.
*
* @warning Care should be taken when using this method from within a plugin's event handler for
* the same event or event type (e.g. "faked" events). As this may result in an unending
* recursion that will crash the server. To prevent this situation, the event handler should
* (given in order of preference): 1) restrict evaluation to a specific Allow set not including
* its own plugin; or, 2) add its own plugin to a Deny set. See overloaded convenience methods
* for more details.
*
* @param evt event under test
* @param plugins Allow/Deny plugin set
* @param allowDeny \c false - evaluate using an Allow set; or \c true - evaluate using a
* Deny set.
* @return first plugin that cancelled given event; or \c if none found/did
*/

public static Plugin willCancel( Event evt, Set<Plugin> plugins, boolean allowDeny ) {
PluginManager piMgr = Bukkit.getPluginManager();

/*
* 1. From SimplePluginManager.callEvent(). Check thread-safety and requirements as if this
* were a normal event call.
*/
if ( evt.isAsynchronous() ) {
if ( Thread.holdsLock( piMgr ) ) {
throw new IllegalStateException( evt.getEventName()
+ " cannot be triggered asynchronously from inside synchronized code." );
}
if ( Bukkit.isPrimaryThread() ) {
throw new IllegalStateException( evt.getEventName()
+ " cannot be triggered asynchronously from primary server thread." );
}
return fireUntilCancelled( evt, plugins, allowDeny );
}
else {
synchronized ( piMgr ) {
return fireUntilCancelled( evt, plugins, allowDeny );
}
}

}


/**
* @brief See willCancel() for details.
*
* @note Scoped as `protected` method for unit testing without reflection.
*
* @param evt event under test
* @param plugins Allow/Deny plugin set
* @param allowDeny \c false - evaluate using an Allow set; or \c true - evaluate using a
* Deny set.
* @return first plugin that cancelled given event; or \c if none found/did
*/
protected static Plugin fireUntilCancelled( Event evt, Set<Plugin> plugins, boolean allowDeny ) {

/*
* 1. If event cannot be canceled, nothing will cancel it.
*/

if ( !(evt instanceof Cancellable) )
return null;

/*
* 2. Iterate over the event's "baked" event handler list.
*/

HandlerList handlers = evt.getHandlers();
for ( RegisteredListener l : handlers.getRegisteredListeners() ) {

/*
* A. Is associated plugin applicable? If not, move to next listener.
*/

if ( !ofInterest( l.getPlugin(), plugins, allowDeny ) )
continue;

/*
* B. Call registered plugin listener. If event is marked cancelled afterwards, return
* reference to canceling plugin.
*/

try {
l.callEvent( evt );
if ( ((Cancellable) evt).isCancelled() )
return l.getPlugin();
}
catch ( EventException e ) {

/*
* Can be safely ignored as it is only used to nag developer about legacy events
* and similar matters.
*/
}
}
return null;
}


/**
* @brief Determine whether the given plugin is of interest.
*
* This method determines whether the given plugin is of interest. A plugin is of no interest
* if any of the following conditions are met:
*
* - the plugin is disabled
* - \c allowDeny is \c false (allow) and set does not contains plugin
* - \c allowDeny is \c true (deny) and set contains plugin
*
* @note Scoped as `protected` method for unit testing without reflection.
*
* @param plugin plugin to evaluate
* @param plugins plugin allow/deny set
* @param allowDeny \c false validate against allow set; \c true validate against deny set
* @return \c true plugin is of interest; \c false otherwise
*/

protected static boolean ofInterest( Plugin plugin, Set<Plugin> plugins, boolean allowDeny ) {
if ( !plugin.isEnabled() )
return false;

return allowDeny ^ plugins.contains( plugin );
}
}

关于java - 如何查看 bukkit 插件中调用 setCancelled() 的类/插件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42570796/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com