Repository navigation
Expand file tree
/
Copy pathItems.kt
More file actions
117 lines (105 loc) · 5.16 KB
/
Copy pathItems.kt
File metadata and controls
117 lines (105 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright (c) 2026 Osip Fatkullin, EndlessCode Group and contributors
// SPDX-License-Identifier: MIT
package ru.endlesscode.mimic.items
import net.kyori.adventure.key.Key
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack
import ru.endlesscode.mimic.LookupResult
import ru.endlesscode.mimic.LookupStatus
/**
* Vanilla items and custom items from all plugins supported by Mimic.
* Get it via [Mimic.items][ru.endlesscode.mimic.Mimic.items].
* To make your own plugin's items available here, implement [ItemProvider] instead.
*
* ### Quick start
*
* ```java
* Key excalibur = Key.key("mmoitems:sword/excalibur");
*
* // Check the ID read from a config
* if (!items.hasItem(excalibur)) { ... }
*
* // Create an item for the player
* ItemStack sword = items.createItem(excalibur, 1, player);
*
* // Identify an item
* if (items.isItem(player.getInventory().getItemInMainHand(), excalibur)) { ... }
* ```
*
* ### IDs
*
* Items are identified by [Key], which consists of a namespace and a path:
* - The namespace is the ID of the plugin providing the item: `minecraft`, `mmoitems`, `craftengine`.
* - The path is the item within that plugin: `diamond`, `sword/excalibur`, `default/palm_log`.
*
* The namespace is required.
* Note that `Key.key("excalibur")` turns into `minecraft:excalibur`,
* so read item IDs from configs with [lookupItem].
*
* ### Ownership
*
* An item belongs to the plugin with the highest priority that recognizes it, and vanilla items belong to `minecraft`.
* So a custom item is never recognized as the vanilla item it is built on:
* a ruby built on a diamond is not `minecraft:diamond`.
* To check the vanilla item regardless of plugins, use [ItemStack.getType].
*
* ### Rules
*
* - [createItem] must be called on the main thread, other methods may be called from any thread.
* - Item IDs may be unknown until their plugins finish loading, and may change when a plugin reloads.
* Don't validate configs in `onLoad`, and don't keep created stacks across reloads.
* - `null` or `false` means "not possible": the item doesn't exist, no plugin recognizes it, or the payload is invalid.
*
* @since 0.11.0
*/
public interface Items {
/**
* Returns IDs of all items of all providers. Use it for config validation and tab completion.
* IDs are sorted by provider priority, then in the order the provider returns them.
*/
public fun getAllItems(): List<Key>
/** Returns `true` if the item with [itemId] exists. */
public fun hasItem(itemId: Key): Boolean
/**
* Looks up the item ID by a [value] from a config. The result has only IDs of existing items.
* Use it instead of `Key.key(String)`, which turns `excalibur` into `minecraft:excalibur`.
*
* The lookup is lenient:
* - Letter case doesn't matter, and characters not allowed in IDs are replaced with `_`:
* `Minecraft:Diamond Sword` is `minecraft:diamond_sword`.
* - The namespace may be omitted if only one plugin has an item with this name: `diamond` is `minecraft:diamond`.
* If several plugins have it, the result is [LookupStatus.AMBIGUOUS].
*
* The name should be the same as in the item ID: `excalibur` doesn't match `mmoitems:sword/excalibur`.
*
* @since 0.12.0
*/
public fun lookupItem(value: String): LookupResult
/** Returns ID of the [item] in the provider owning it, or `null` if no provider recognizes the item. */
public fun getItemId(item: ItemStack): Key?
/**
* Returns `true` if the [item] has [itemId]. Same as `getItemId(item) == itemId`:
* amount, stats, durability and lore of the stack are ignored.
*/
public fun isItem(item: ItemStack, itemId: Key): Boolean
/** Creates a new stack with a single item with [itemId]. */
public fun createItem(itemId: Key): ItemStack? = createItem(itemId, amount = 1)
/** Creates a new stack of the item with [itemId] and the given [amount]. */
public fun createItem(itemId: Key, amount: Int): ItemStack? = createItem(itemId, amount, player = null)
/** Creates a new stack of the item with [itemId] and the given [amount] for the [player]. */
public fun createItem(itemId: Key, amount: Int, player: Player?): ItemStack? =
createItem(itemId, amount, player, payload = null)
/**
* Creates a new stack of the item with [itemId]. The caller owns the returned stack.
*
* @param amount Amount of items. It is kept within `1` and the maximum stack size of the item.
* @param player The player the item is created for, or `null` if there is no such player.
* Some items depend on the player, like items scaled to the player's level.
* @param payload Additional configuration of the item in the format of its provider.
* A blank payload is the same as `null`.
* For example, `minecraft` takes item components like `[damage=10]`.
* Providers taking no payload ignore it. If the payload is invalid, the item is not created and a warning is logged.
* @return the stack, or `null` if the ID is unknown or the payload is invalid.
*/
public fun createItem(itemId: Key, amount: Int = 1, player: Player? = null, payload: String? = null): ItemStack?
}