Last active
January 14, 2022 23:07
Star
You must be signed in to star a gist
A typescript wrapper for [AnvilGUI](https://github.com/WesJD/AnvilGUI); a class to "Easily use anvil guis to get a user's input".
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as types from '@grakkit/types-paper'; | |
interface onComplete<R>{ | |
/** | |
* Called when the inventory output slot is clicked | |
* | |
* @param player **The player who the Anvil GUI was opened for** | |
* *** | |
* @param text **The text entered by the player in the renaming field of the anvil GUI** | |
* *** | |
* @param response **The response object** | |
* *** | |
*/ | |
(player: types.obePlayer, text: string, response: AnvilGUIResponse ): R | |
} | |
interface callback<R>{ | |
/** | |
* @param player **The player who the Anvil GUI was opened for** | |
* *** | |
*/ | |
(player: types.obePlayer): R | |
} | |
interface AnvilGUIResponse { | |
/** | |
* @method close the inventory | |
*/ | |
close(): AnvilGUIResponse | |
/** | |
* @method set the text to be send to the user | |
*/ | |
text(text:string): AnvilGUIResponse | |
} | |
/** | |
* @class AnvilGUI | |
* @summary A typescript wrapper for [AnvilGUI](https://github.com/WesJD/AnvilGUI); a class to "Easily use anvil guis to get a user's input". | |
* @description create an anvil GUI for players to interact with. First import the Builder and Response AnvilGUI classes from the anvilGUI `.jar` file with `load()` | |
* @param {any} Builder - The imported Builder class from the anvilGUI .jar file | |
* @param {any} Response - The imported Response class from the anvilGUI .jar file | |
* @example | |
* ```ts | |
* import {load, type, plugin, server, command} from '@grakkit/stdlib-paper' | |
* import AnvilGUI from 'utils/translations/anvilGUI' | |
* const Builder = load('./plugins/grakkit/javaLibs/anvilgui.jar', 'net.wesjd.anvilgui.AnvilGUI$Builder') | |
* const Response = load('./plugins/grakkit/javaLibs/anvilgui.jar', 'net.wesjd.anvilgui.AnvilGUI$Response') | |
* const Material = type('org.bukkit.Material') | |
* const ItemStack = type('org.bukkit.inventory.ItemStack') | |
* command({ | |
* name: 'anvilGUIExample', | |
* execute: (sender, args) => { | |
* const player = server.getPlayer(sender.getName()) | |
* const anvilGUI = new AnvilGUI(Builder, Response) | |
* .onClose((player) => player.sendRawMessage("You closed the inventory.")) | |
* .onComplete((player, text,response) => { | |
* if(text.toLowerCase() == "you") { | |
* player.sendRawMessage("You have magical powers!"); | |
* return response.close(); | |
* } else { | |
* return response.text("Wrong."); | |
* } | |
* }) | |
* .preventClose() | |
* .text("What is the meaning of life?") | |
* .itemLeft(new ItemStack(Material.DIAMOND_SWORD)) | |
* .itemRight(new ItemStack(Material.DIAMOND_SWORD)) | |
* .onRightInputClick(player => player.sendRawMessage("second sword")) | |
* .onLeftInputClick(player => player.sendRawMessage("first sword")) | |
* .title("Enter your answer.") | |
* .plugin(plugin) | |
* .open(player) | |
* } | |
* }) | |
* ``` | |
* @author [WesJD](https://github.com/WesJD) -- Wrapped with 💙 by [GodBleak](t.me/GodBleak) for [Grakkit](https://github.com/grakkit/grakkit) | |
*/ | |
export default class AnvilGUI { | |
declare private Builder: AnvilGUI; | |
declare private Response; | |
constructor(Builder:any, Response:any) { | |
this.Builder = new Builder(); | |
this.Response = Response; | |
} | |
/** | |
* The callback to be executed when the player clicks the output slot. | |
* Expects a method of AnvilGUIResponse to be returned | |
* | |
* @param {onComplete} callback - The callback to be executed when the player clicks the output slot | |
* @returns {AnvilGUI} this AnvilGUI instance | |
* @throws java.lang.IllegalArgumentException - when the callback is null | |
* | |
* @see `onComplete` on [GitHub](https://github.com/WesJD/AnvilGUI#oncompletebifunctionplayer-string-anvilguiresponse) | |
* @see the [JavaDocs](https://docs.wesjd.net/AnvilGUI/net/wesjd/anvilgui/AnvilGUI.Builder.html#onComplete-java.util.function.BiFunction-) | |
* | |
* @example | |
* ```ts | |
* … | |
* .onComplete((player, text, response) => { | |
* player.setDisplayName(text) | |
* return response.close() | |
* }) | |
* … | |
* ``` | |
*/ | |
onComplete (callback: onComplete<AnvilGUIResponse>): AnvilGUI{ | |
this.Builder.onComplete((player: types.obePlayer, text: string) => | |
callback(player, text, this.Response.static)) | |
return this; | |
} | |
/** | |
* Sets the item to be loaded into the left item slot | |
* | |
* @param {types.obiItemStack} item - The ItemStack to be put in the first slot | |
* @returns {AnvilGUI} this AnvilGUI instance | |
* @throws java.lang.IllegalArgumentException - when the method is called but item is null | |
* | |
* @see `itemLeft` on [GitHub](https://github.com/WesJD/AnvilGUI#itemleftitemstack) | |
* @see the [JavaDocs](https://docs.wesjd.net/AnvilGUI/net/wesjd/anvilgui/AnvilGUI.Builder.html#itemLeft-org.bukkit.inventory.ItemStack-) | |
* | |
* @example | |
* ```ts | |
* … | |
* const Material = type('org.bukkit.Material') | |
* const ItemStack = type('org.bukkit.inventory.ItemStack') | |
* … | |
* .itemLeft(new ItemStack(Material.DIAMOND_HOE, 1)) | |
* … | |
* ``` | |
*/ | |
itemLeft(item: types.obiItemStack): AnvilGUI{ | |
this.Builder.itemLeft(item); | |
return this; | |
} | |
/** | |
* Sets the item to be loaded into the left item slot | |
* | |
* @param {types.obiItemStack} item - The ItemStack to be put in the first slot | |
* @returns {AnvilGUI} this AnvilGUI instance | |
* @throws java.lang.IllegalArgumentException - when the method is called but item is null | |
* | |
* @see `itemRight` on [GitHub](https://github.com/WesJD/AnvilGUI#itemrightitemstack) | |
* @see the [JavaDocs](https://docs.wesjd.net/AnvilGUI/net/wesjd/anvilgui/AnvilGUI.Builder.html#itemRight-org.bukkit.inventory.ItemStack-) | |
* | |
* @example | |
* ```ts | |
* … | |
* const Material = type('org.bukkit.Material'); | |
* const ItemStack = type('org.bukkit.inventory.ItemStack'); | |
* … | |
* .itemRight(new ItemStack(Material.DIAMOND_HOE, 1)) | |
* … | |
* ``` | |
*/ | |
itemRight(item: types.obiItemStack): AnvilGUI{ | |
this.Builder.itemRight(item); | |
return this; | |
} | |
/** | |
* Sets the title to the provided string | |
* | |
* @param {string} title - The title that is to be displayed to the user | |
* @returns {AnvilGUI} this AnvilGUI instance | |
* @throws java.lang.IllegalArgumentException - when the method is called but title is null | |
* | |
* @see `title` on [GitHub](https://github.com/WesJD/AnvilGUI#titlestring) | |
* @see the [JavaDocs](https://docs.wesjd.net/AnvilGUI/net/wesjd/anvilgui/AnvilGUI.Builder.html#title-java.lang.String-) | |
* | |
* @example | |
* ```ts | |
* … | |
* .title('Set Nickname') | |
* … | |
* ``` | |
*/ | |
title(title: string): AnvilGUI{ | |
this.Builder.title(title); | |
return this; | |
} | |
/** | |
* Your plugin object, required to register listeners. | |
* | |
* @param {types.obpPlugin} plugin - The Plugin this anvil GUI is associated with | |
* @returns {AnvilGUI} this AnvilGUI instance | |
* @throws java.lang.IllegalArgumentException - if the plugin is null | |
* | |
* @see `plugin` on [GitHub](https://github.com/WesJD/AnvilGUI#pluginplugin) | |
* @see the [JavaDocs](https://docs.wesjd.net/AnvilGUI/net/wesjd/anvilgui/AnvilGUI.Builder.html#plugin-org.bukkit.plugin.Plugin-) | |
* | |
* @example | |
* ```ts | |
* import {…, plugin} from '@grakkit/stdlib-paper' | |
* … | |
* .plugin(plugin) | |
* … | |
* ``` | |
*/ | |
plugin(plugin: types.obpPlugin): AnvilGUI { | |
this.Builder.plugin(plugin); | |
return this; | |
} | |
/** | |
* Creates and opens the inventory for the provided player. | |
* | |
* **This method may only be the last method in the chain** | |
* | |
* @param {types.obePlayer} player - The Player the anvil GUI should open for | |
* @returns {void} void | |
* @throws java.lang.IllegalArgumentException when the onComplete function, plugin, or player is null | |
* | |
* @see `open` on [GitHub](https://github.com/WesJD/AnvilGUI#openplayer) | |
* @see the [JavaDocs](https://docs.wesjd.net/AnvilGUI/net/wesjd/anvilgui/AnvilGUI.Builder.html#open-org.bukkit.entity.Player-) | |
* | |
* @example | |
* ```ts | |
* const player = server.getPlayer(…) | |
* | |
* … | |
* .onComplete(…) | |
* .plugin(…) | |
* .open(player) | |
* ``` | |
*/ | |
open(player: types.obePlayer): void { | |
this.Builder.open(player); | |
} | |
/** | |
* Takes a callback to be executed when the player closes the inventory. | |
* | |
* @param {callback} callback - A callback that is called when the anvil GUI is closed | |
* @returns {AnvilGUI} this AnvilGUI instance | |
* @throws java.lang.IllegalArgumentException when the method is called, but the callback function is null | |
* | |
* @see `onClose` on [GitHub](https://github.com/WesJD/AnvilGUI#oncloseconsumerplayer) | |
* @see the [JavaDocs](https://docs.wesjd.net/AnvilGUI/net/wesjd/anvilgui/AnvilGUI.Builder.html#onClose-java.util.function.Consumer-) | |
* | |
* @example | |
* ```ts | |
* … | |
* .onClose((player) => { | |
* player.sendMessage(`You've closed the anvil GUI`) | |
* }) | |
* … | |
* ``` | |
*/ | |
onClose(callback: callback<void>): AnvilGUI { | |
this.Builder.onClose(callback); | |
return this; | |
} | |
/** | |
* Tells AnvilGUI to prevent the player from closing the inventory. | |
* | |
* Useful to implement mechanics like requiring a password to play. | |
* | |
* @returns {AnvilGUI} this AnvilGUI instance | |
* | |
* @see `preventClose` on [GitHub](https://github.com/WesJD/AnvilGUI#preventclose) | |
* @see the [JavaDocs](https://docs.wesjd.net/AnvilGUI/net/wesjd/anvilgui/AnvilGUI.Builder.html#preventClose--) | |
* | |
* @example | |
* ```ts | |
* … | |
* .onComplete((player, text, response) => { | |
* if(text !== 'password') return response.text('Incorrect password') | |
* player.sendMessage("You have magical powers!"); | |
* return response.close() | |
* ) | |
* .preventClose() | |
* … | |
* ``` | |
*/ | |
preventClose(): AnvilGUI { | |
this.Builder.preventClose(); | |
return this; | |
} | |
/** | |
* Sets the default text to be displayed in the renaming field | |
* | |
* @param {string} text - The initial name of the item in the anvil | |
* @returns {AnvilGUI} this AnvilGUI instance | |
* @throws java.lang.IllegalArgumentException when the method is called but text is null | |
* | |
* @see `text` on [GitHub](https://github.com/WesJD/AnvilGUI#textstring) | |
* @see the [JavaDocs](https://docs.wesjd.net/AnvilGUI/net/wesjd/anvilgui/AnvilGUI.Builder.html#text-java.lang.String-) | |
* | |
* @example | |
* ```ts | |
* … | |
* .defaultText('Enter your new nickname here') | |
* … | |
* ``` | |
*/ | |
text(text: string): AnvilGUI { | |
this.Builder.text(text); | |
return this; | |
} | |
/** | |
* Takes a callback to be executed when the item in the left input slot is clicked. | |
* | |
* @param {callback<void>} callback - A callback that is called when the first input slot is clicked | |
* @returns {AnvilGUI} this AnvilGUI instance | |
* @throws java.lang.IllegalArgumentException when the method is called but callback is null | |
* | |
* @see `onLeftInputClick` on [GitHub](https://github.com/WesJD/AnvilGUI#onleftinputclickconsumerplayer) | |
* @see the [JavaDocs](https://docs.wesjd.net/AnvilGUI/net/wesjd/anvilgui/AnvilGUI.Builder.html#onLeftInputClick-java.util.function.Consumer-) | |
* | |
* @example | |
* ```ts | |
* … | |
* .onLeftInputClick((player) => { | |
* player.sendMessage('You clicked the left input slot') | |
* }) | |
* … | |
* ``` | |
*/ | |
onLeftInputClick(callback: callback<void>): AnvilGUI { | |
this.Builder.onLeftInputClick(callback); | |
return this; | |
} | |
/** | |
* Takes a callback to be executed when the item in the right input slot is clicked. | |
* | |
* @param {callback<void>} callback - A callback that is called when the second input slot is clicked | |
* @returns {AnvilGUI} this AnvilGUI instance | |
* @throws java.lang.IllegalArgumentException when the method is called but callback is null | |
* | |
* @see `onRightInputClick` on [GitHub](https://github.com/WesJD/AnvilGUI#onrightinputclickconsumerplayer) | |
* @sse the [JavaDocs](https://docs.wesjd.net/AnvilGUI/net/wesjd/anvilgui/AnvilGUI.Builder.html#onRightInputClick-java.util.function.Consumer-) | |
* | |
* @example | |
* ```ts | |
* … | |
* .onRightInputClick((player) => { | |
* player.sendMessage('You clicked the right input slot') | |
* }) | |
* … | |
* ``` | |
*/ | |
onRightInputClick(callback: callback<void>): AnvilGUI { | |
this.Builder.onRightInputClick(callback); | |
return this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment