Anvilgui是一個庫,可通過砧庫來捕獲Minecraft中的用戶輸入。 Minecraft / Bukkit / Spigot / Paper API領域內的Anvil庫存非常柔軟,最終不支持將它們完全用於用戶輸入任務的能力。結果,使用砧庫存獲得用戶輸入的唯一方法是與混淆的,分解的代碼相互作用。 Anvilgui提供了一種直接,通用和易於使用的解決方案,而沒有您的項目取決於版本的特定代碼。
您需要使用Spigot映射才能使Anvilgui工作,但是您怎麼能確定自己呢?下面的問題將幫助您清除內容。
您的插件是Bukkit插件嗎?如果您的插件包含插件,則是Bukkit插件,並且不包含paper-plugin.yml
plugin.yml
。在這種情況下,默認情況下,您使用的是Spigot映射。
您的插件是紙插件嗎?如果您的插件包含paper-plugin.yml
文件,即使它還包含plugin.yml
。在這種情況下,默認情況下,您沒有使用Spigot映射,而可以根據這些說明通過清單輸入來啟用它們。
您是否正在使用Paper Weight UserDev工具鏈?在這種情況下,默認情況下,您沒有使用Spigot映射,而是可以使用ReObfartifactConfiguration選項啟用它們。
如果您是開發人員,請提交拉動請求,為您的版本添加包裝器模塊。否則,請在“問題”選項卡上創建問題。
Anvilgui需要使用Maven或Maven兼容構建系統。
< dependency >
< groupId >net.wesjd</ groupId >
< artifactId >anvilgui</ artifactId >
< version >1.10.3-SNAPSHOT</ version >
</ dependency >
< repository >
< id >codemc-snapshots</ id >
< url >https://repo.codemc.io/repository/maven-snapshots/</ url >
</ repository >
最好成為一個好公民,並將依賴性轉移到您的命名空間內,以防止與其他插件發生衝突。這是如何重新安置依賴性的示例:
< build >
< plugins >
< plugin >
< groupId >org.apache.maven.plugins</ groupId >
< artifactId >maven-shade-plugin</ artifactId >
< version >${shade.version}</ version > <!-- The version must be at least 3.5.0 -->
< executions >
< execution >
< phase >package</ phase >
< goals >
< goal >shade</ goal >
</ goals >
< configuration >
< relocations >
< relocation >
< pattern >net.wesjd.anvilgui</ pattern >
< shadedPattern >[YOUR_PLUGIN_PACKAGE].anvilgui</ shadedPattern > <!-- Replace [YOUR_PLUGIN_PACKAGE] with your namespace -->
</ relocation >
</ relocations >
< filters >
< filter >
< artifact >*:*</ artifact >
< excludeDefaults >false</ excludeDefaults >
< includes >
< include >net/wesjd/anvilgui/**</ include >
</ includes >
</ filter >
</ filters >
</ configuration >
</ execution >
</ executions >
</ plugin >
</ plugins >
</ build >
注意:為了求解<minimizeJar>
從最終罐子中刪除Anvilgui VerionWrapper
s並使庫無法使用,請確保您的<filters>
部分包含示例<filter>
,如上所述。
AnvilGUI.Builder
類是您構建Anvilgui的方式。以下方法使您可以修改顯示的GUI的各個部分。 Javadocs可在此處使用。
onClose(Consumer<StateSnapshot>)
當玩家關閉Anvil GUI時,請使用一個Consumer<StateSnapshot>
參數。
builder . onClose ( stateSnapshot -> {
stateSnapshot . getPlayer (). sendMessage ( "You closed the inventory." );
});
onClick(BiFunction<Integer, AnvilGUI.StateSnapshot, AnvilGUI.ResponseAction>)
與單擊的插槽和當前GUI狀態的快照進行BiFunction
。當玩家單擊庫存中的任何插槽時,該功能被調用。您必須返回List<AnvilGUI.ResponseAction>
,其中可能包括:
AnvilGUI.ResponseAction.close()
)AnvilGUI.ResponseAction.replaceInputText(String)
)AnvilGUI.ResponseAction.updateTitle(String, boolean)
)AnvilGUI.ResponseAction.openInventory(Inventory)
)AnvilGUI.ResponseAction.run(Runnable)
)Collections.emptyList()
)行動列表是按照提供的順序進行的。
builder . onClick (( slot , stateSnapshot ) -> {
if ( slot != AnvilGUI . Slot . OUTPUT ) {
return Collections . emptyList ();
}
if ( stateSnapshot . getText (). equalsIgnoreCase ( "you" )) {
stateSnapshot . getPlayer (). sendMessage ( "You have magical powers!" );
return Arrays . asList (
AnvilGUI . ResponseAction . close (),
AnvilGUI . ResponseAction . run (() -> myCode ( stateSnapshot . getPlayer ()))
);
} else {
return Arrays . asList ( AnvilGUI . ResponseAction . replaceInputText ( "Try again" ));
}
});
onClickAsync(ClickHandler)
取一個ClickHandler
,用於BiFunction<Integer, AnvilGui.StateSnapshot, CompletableFuture<AnvilGUI.ResponseAction>>
,它的表現與onClick()
,其差異是返回CompletableFuture
,因此允許ResponseAction
的異步計算。
builder . onClickAsync (( slot , stateSnapshot ) -> CompletedFuture . supplyAsync (() -> {
// this code is now running async
if ( slot != AnvilGUI . Slot . OUTPUT ) {
return Collections . emptyList ();
}
if ( database . isMagical ( stateSnapshot . getText ())) {
// the `ResponseAction`s will run on the main server thread
return Arrays . asList (
AnvilGUI . ResponseAction . close (),
AnvilGUI . ResponseAction . run (() -> myCode ( stateSnapshot . getPlayer ()))
);
} else {
return Arrays . asList ( AnvilGUI . ResponseAction . replaceInputText ( "Try again" ));
}
}));
allowConcurrentClickHandlerExecution()
告訴Anvilgui禁用實施的機制,以防止onClickAsync(ClickHandler)
設置的點擊處理程序同時執行。
builder . allowConcurrentClickHandlerExecution ();
interactableSlots(int... slots)
這允許或否認用戶可以在提供的砧座插槽中獲取 /輸入項目。當您嘗試使用Anvil GUI製作輸入系統時,此功能很有用。
builder . interactableSlots ( Slot . INPUT_LEFT , Slot . INPUT_RIGHT );
preventClose()
告訴Anvilgui,以防止用戶按下逃生以關閉庫存。對於要播放的密碼輸入等情況很有用。
builder . preventClose ();
geyserCompat()
這可以切換與間歇泉軟件的兼容性,特別是能夠使用基岩上具有0經驗水平的Anvilgui。默認情況下啟用。
builder . geyserCompat ();
text(String)
採用一個包含重命名字段中初始文本的String
。如果提供了itemLeft
,則將顯示名稱設置為提供的文本。如果未設置itemLeft
,則將使用一張紙。
builder . text ( "What is the meaning of life?" );
itemLeft(ItemStack)
採用自定義ItemStack
放置在左輸入插槽中。
ItemStack stack = new ItemStack ( Material . IRON_SWORD );
ItemMeta meta = stack . getItemMeta ();
meta . setLore ( Arrays . asList ( "Sharp iron sword" ));
stack . setItemMeta ( meta );
builder . itemLeft ( stack );
itemRight(ItemStack)
採用自定義ItemStack
放置在右輸入插槽中。
ItemStack stack = new ItemStack ( Material . IRON_INGOT );
ItemMeta meta = stack . getItemMeta ();
meta . setLore ( Arrays . asList ( "A piece of metal" ));
stack . setItemMeta ( meta );
builder . itemRight ( stack );
title(String)
取一個String
,將字面用作庫存標題。僅在Minecraft 1.14及以上顯示。
builder . title ( "Enter your answer" );
jsonTitle(String)
取一個包含序列化為JSON的豐富文本組件的String
。可用於使用十六進制顏色代碼或冒險組件Interop的設置標題。僅在Minecraft 1.14及以上顯示。
builder . jsonTitle ( "{ " text " : " Enter your answer " , " color " : " green " }" )
plugin(Plugin)
採用製造此砧GUI的Plugin
對象。需要註冊聽眾。
builder . plugin ( pluginInstance );
mainThreadExecutor(Executor)
取決於必須在主服務器線程上執行的Executor
。如果無法通過Bukkit調度程序(例如在Folia服務器上)訪問主服務器線程,則可以將其交換為Folia Aware Executor。
builder . mainThreadExecutor ( executor );
open(Player)
抓住一個應打開砧GUI的Player
。可以多次調用此方法,而無需創建一個新的AnvilGUI.Builder
對象。
builder . open ( player );
new AnvilGUI . Builder ()
. onClose ( stateSnapshot -> {
stateSnapshot . getPlayer (). sendMessage ( "You closed the inventory." );
})
. onClick (( slot , stateSnapshot ) -> { // Either use sync or async variant, not both
if ( slot != AnvilGUI . Slot . OUTPUT ) {
return Collections . emptyList ();
}
if ( stateSnapshot . getText (). equalsIgnoreCase ( "you" )) {
stateSnapshot . getPlayer (). sendMessage ( "You have magical powers!" );
return Arrays . asList ( AnvilGUI . ResponseAction . close ());
} else {
return Arrays . asList ( AnvilGUI . ResponseAction . replaceInputText ( "Try again" ));
}
})
. preventClose () //prevents the inventory from being closed
. text ( "What is the meaning of life?" ) //sets the text the GUI should start with
. title ( "Enter your answer." ) //set the title of the GUI (only works in 1.14+)
. plugin ( myPluginInstance ) //set the plugin instance
. open ( myPlayer ); //opens the GUI for the player provided
我們使用Maven來處理我們的依賴關係。使用Java 21運行mvn clean install
來構建項目。
該項目利用一塵不染的Maven插件來執行樣式指南。如果您的代碼不符合準則,您將無法構建該項目。要解決所有代碼格式問題,只需運行mvn spotless:apply
。
該項目已根據MIT許可獲得許可。