Skip to content

Commit aae2595

Browse files
authored
Merge pull request #19 from EndlessCodeGroup/feature/items-payload
Items payload support
2 parents 12d4f82 + 1042b1b commit aae2595

25 files changed

Lines changed: 680 additions & 60 deletions

File tree

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
## [Unreleased]
22

3+
### API
4+
5+
- Add `MimicApiLevel` class to check current running Mimic API version:
6+
```kotlin
7+
// Specify here the version required for APIs you use.
8+
if (!MimicApiLevel.checkApiLevel(MimicApiLevel.VERSION_0_6)) {
9+
println("At least Mimic 0.6 is required. Please download it from {link here}")
10+
}
11+
```
12+
- Add optional payload to `ItemsRegistry.getItem`. It may be used to customize item.
13+
314
### Bukkit Plugin
415

516
- More detailed output of command `/mimic items info`:
@@ -12,6 +23,8 @@
1223
minecraft: 976
1324
```
1425
- Improve integration with Heroes class system (#14)
26+
- Add statistics about used items registries
27+
- Add payload support to `MinecraftItemsRegistry`.
1528

1629
### Housekeeping
1730

buildSrc/build.gradle.kts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ java {
88
}
99

1010
dependencies {
11-
implementation(kotlin("gradle-plugin", version = "1.5.21"))
11+
val kotlinVersion = "1.5.21"
12+
implementation(kotlin("gradle-plugin", version = kotlinVersion))
13+
implementation(kotlin("serialization", version = kotlinVersion))
1214
implementation("org.jetbrains.dokka:dokka-gradle-plugin:1.5.0")
1315
implementation("org.jetbrains.kotlinx:binary-compatibility-validator:0.6.0")
1416
implementation("de.undercouch:gradle-download-task:4.1.2")
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import org.gradle.api.artifacts.CacheableRule
2+
import org.gradle.api.artifacts.ComponentMetadataContext
3+
import org.gradle.api.artifacts.ComponentMetadataRule
4+
import org.gradle.api.attributes.java.TargetJvmVersion
5+
6+
@CacheableRule
7+
abstract class HoconSerializationRule : ComponentMetadataRule {
8+
override fun execute(context: ComponentMetadataContext) {
9+
context.details.allVariants {
10+
attributes {
11+
attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, 8)
12+
}
13+
}
14+
}
15+
}

buildSrc/src/main/kotlin/dependencies.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ object acf {
3333

3434
object misc {
3535
const val bstats = "org.bstats:bstats-bukkit:1.8"
36+
const val annotations = "org.jetbrains:annotations:13.0"
37+
const val serialization_hocon = "org.jetbrains.kotlinx:kotlinx-serialization-hocon:1.2.2"
3638
}
3739

3840
// Testing

mimic-api/api/mimic-api.api

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
public final class ru/endlesscode/mimic/MimicApiLevel {
2+
public static final field CURRENT I
3+
public static final field INSTANCE Lru/endlesscode/mimic/MimicApiLevel;
4+
public static final field VERSION_0_6 I
5+
public static final fun checkApiLevel (I)Z
6+
}
7+
18
public abstract interface class ru/endlesscode/mimic/MimicService {
29
public abstract fun getId ()Ljava/lang/String;
310
public abstract fun isEnabled ()Z
@@ -18,7 +25,9 @@ public abstract interface class ru/endlesscode/mimic/classes/ClassSystem {
1825

1926
public abstract interface class ru/endlesscode/mimic/items/ItemsRegistry : ru/endlesscode/mimic/MimicService {
2027
public fun getItem (Ljava/lang/String;)Ljava/lang/Object;
21-
public abstract fun getItem (Ljava/lang/String;I)Ljava/lang/Object;
28+
public fun getItem (Ljava/lang/String;I)Ljava/lang/Object;
29+
public fun getItem (Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object;
30+
public abstract fun getItem (Ljava/lang/String;Ljava/lang/Object;I)Ljava/lang/Object;
2231
public abstract fun getItemId (Ljava/lang/Object;)Ljava/lang/String;
2332
public abstract fun getKnownIds ()Ljava/util/Collection;
2433
public abstract fun isItemExists (Ljava/lang/String;)Z
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* This file is part of Mimic.
3+
* Copyright (C) 2021 Osip Fatkullin
4+
* Copyright (C) 2021 EndlessCode Group and contributors
5+
*
6+
* Mimic is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Lesser General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* Mimic is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with Mimic. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
package ru.endlesscode.mimic
21+
22+
/** Utility to check Mimic API level. */
23+
public object MimicApiLevel {
24+
25+
/**
26+
* # Version 0.6
27+
* - Mimic API levels
28+
* - Payload in ItemsRegistry.getItem
29+
*/
30+
public const val VERSION_0_6: Int = 1
31+
32+
/**
33+
* The latest version at the moment of Mimic COMPILATION.
34+
*
35+
* Usage of this constant will be inlined by the compiler, so use it only to save the version
36+
* which was used at your plugin COMPILE TIME.
37+
* Use [checkApiLevel] if you want to check that the current RUNNING Mimic API level meets to
38+
* the required.
39+
*/
40+
public const val CURRENT: Int = VERSION_0_6
41+
42+
/**
43+
* Returns 'true' if the current RUNNING Mimic API level meets to the required, otherwise `false`.
44+
* ```
45+
* // Specify here the version required for APIs you use.
46+
* if (!MimicApiLevel.checkApiLevel(MimicApiLevel.VERSION_0_6)) {
47+
* println("At least Mimic 0.6 is required. Please download it from {link here}")
48+
* }
49+
* ```
50+
*/
51+
@JvmStatic
52+
public fun checkApiLevel(apiLevel: Int): Boolean = CURRENT < apiLevel
53+
}

mimic-api/src/main/kotlin/items/ItemsRegistry.kt

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ package ru.endlesscode.mimic.items
2121

2222
import ru.endlesscode.mimic.MimicService
2323

24-
/** Service for getting items by theirs ID. Also can be used to match ID with item. */
24+
/** Service for getting items by theirs ID. Also, can be used to match ID with item. */
2525
public interface ItemsRegistry<ItemStackT : Any> : MimicService {
2626

2727
/** Returns all known item IDs. */
@@ -37,13 +37,31 @@ public interface ItemsRegistry<ItemStackT : Any> : MimicService {
3737
public fun getItemId(item: ItemStackT): String?
3838

3939
/** Returns item by given [itemId], or `null` if the ID not found in this registry. */
40-
public fun getItem(itemId: String): ItemStackT? = getItem(itemId, amount = 1)
40+
public fun getItem(itemId: String): ItemStackT? = getItem(itemId, payload = null, amount = 1)
41+
42+
/**
43+
* Returns item stack with specified [payload] by given [itemId], or `null` if the ID not found in this registry.
44+
*
45+
* If [payload] is not `null`, item will be configured using it.
46+
*/
47+
public fun getItem(itemId: String, payload: Any?): ItemStackT? = getItem(itemId, payload, amount = 1)
4148

4249
/**
4350
* Returns item stack with specified [amount] by given [itemId], or `null` if ID not found in this registry.
4451
*
4552
* If given [amount] is greater than maximum possible, will use maximum possible amount.
4653
* Amount shouldn't be less than `1`.
4754
*/
48-
public fun getItem(itemId: String, amount: Int): ItemStackT?
55+
public fun getItem(itemId: String, amount: Int): ItemStackT? = getItem(itemId, payload = null, amount)
56+
57+
/**
58+
* Returns item stack with specified [amount] and [payload] by given [itemId],
59+
* or `null` if ID not found in this registry.
60+
*
61+
* If given [amount] is greater than maximum possible, will use maximum possible amount.
62+
* Amount shouldn't be less than `1`.
63+
*
64+
* Given [payload] may be used to configure item.
65+
*/
66+
public fun getItem(itemId: String, payload: Any?, amount: Int): ItemStackT?
4967
}

mimic-bukkit-api/README.md

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,21 @@ Available services:
1313

1414
### How to use Mimic APIs?
1515

16-
Firstly you should make sure Mimic is enabled with such method:
16+
Firstly you should make sure Mimic is enabled:
1717
```java
1818
private boolean checkMimicEnabled() {
19-
return getServer().getPluginManager().isPluginEnabled("Mimic");
19+
if (!getServer().getPluginManager().isPluginEnabled("Mimic")) {
20+
getLogger().severe("Mimic is required for the plugin!");
21+
return false;
22+
}
23+
24+
// You can also check if Mimic version is right
25+
if (!MimicApiLevel.checkApiLevel(MimicApiLevel.VERSION_0_6)) {
26+
getLogger().severe("Required at least Mimic 0.6!");
27+
return false;
28+
}
29+
30+
return true;
2031
}
2132
```
2233

@@ -26,8 +37,7 @@ Another way is to add it to `softdepend` and show a clear error message to the s
2637
@Override
2738
public void onEnable() {
2839
if (!checkMimicEnabled()) {
29-
getLogger().severe("Mimic is required for the plugin!");
30-
getLogger().severe("Download it: https://www.spigotmc.org/resources/82515/");
40+
getLogger().severe("Download latest version here: https://www.spigotmc.org/resources/82515/");
3141
getServer().getPluginManager().disablePlugin(this);
3242
return;
3343
}
@@ -99,17 +109,27 @@ boolean isMagicStickExists = itemsRegistry.isItemExists("customitems:magic_wand"
99109
@Override
100110
public void onEnable() {
101111
if (!checkMimicEnabled()) {
102-
getLogger().severe("Mimic is required for the plugin!");
103-
getLogger().severe("Download it on https://www.spigotmc.org/resources/82515/");
112+
getLogger().severe("Download latest version here: https://www.spigotmc.org/resources/82515/");
104113
getServer().getPluginManager().disablePlugin(this);
105114
return;
106115
}
107116

108117
setupMimic();
109118
}
110-
119+
111120
private boolean checkMimicEnabled() {
112-
return getServer().getPluginManager().isPluginEnabled("Mimic");
121+
if (!getServer().getPluginManager().isPluginEnabled("Mimic")) {
122+
getLogger().severe("Mimic is required for the plugin!");
123+
return false;
124+
}
125+
126+
// You can also check if Mimic version is right
127+
if (!MimicApiLevel.checkApiLevel(MimicApiLevel.VERSION_0_6)) {
128+
getLogger().severe("Required at least Mimic 0.6!");
129+
return false;
130+
}
131+
132+
return true;
113133
}
114134

115135
private void setupMimic() {

mimic-bukkit/README.md

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ No configuration needed.
3232

3333
You can find code of all implementations [here](src/main/kotlin/impl).
3434

35-
#### [Level Systems][BukkitLevelSystem.Provider]
35+
### [Level Systems][BukkitLevelSystem.Provider]
3636

3737
- **[Minecraft][minecraft-exp]** _(Default)_
3838
- **[SkillAPI]**
@@ -41,27 +41,43 @@ You can find code of all implementations [here](src/main/kotlin/impl).
4141
- **[Heroes]**
4242
- **[QuantumRPG]**
4343

44-
#### [Class Systems][BukkitClassSystem.Provider]
44+
### [Class Systems][BukkitClassSystem.Provider]
4545

4646
- **Permissions-based** _(Default)_ - give permission `mimic.class.[class_name]` to assign class to player
4747
- **[SkillAPI]**
4848
- **[MMOCore]**
4949
- **[Heroes]**
5050
- **[QuantumRPG]**
5151

52-
#### [Items Registries][BukkitItemsRegistry]
52+
### [Items Registries][BukkitItemsRegistry]
5353

54-
[MimicItemsRegistry] - Items registry combining all others items registries.
55-
It uses service ID as namespace for items IDs.
56-
For example: `acacia_boat -> minecraft:acacia_boat`.
57-
If you use item ID without a namespace, it will search over all registries.
54+
#### [MimicItemsRegistry]
5855

59-
Registry | ID Structure
60-
-------------------------------------|--------------------------
61-
[Minecraft][MinecraftItemsRegistry] | `minecraft:[id]`
62-
[CustomItems] | `customitems:[id]`
63-
[MMOItems] | `mmoitems:[id]`
64-
[QuantumRPG] | `quantumrpg:[type]/[id]`
56+
Items registry combining all others items registries.
57+
It uses service ID as namespace for items IDs.\
58+
For example: `acacia_boat -> minecraft:acacia_boat`.
59+
60+
> If you use item ID without a namespace, it will search over all registries.
61+
62+
#### [Minecraft][MinecraftItemsRegistry]
63+
64+
**ID Format:** `minecraft:[id]`\
65+
**Payload:** [ItemMetaPayload]
66+
67+
#### [CustomItems]
68+
69+
**ID Format:** `customitems:[id]`\
70+
**Payload:** *Not supported*
71+
72+
#### [MMOItems]
73+
74+
**ID Format:** `mmoitems:[id]`\
75+
**Payload:** *Not supported*
76+
77+
#### [QuantumRPG]
78+
79+
**ID Format:** `quantumrpg:[type]/[id]`\
80+
**Payload:** *Not supported*
6581

6682
[minecraft-exp]: https://minecraft.gamepedia.com/Experience
6783
[skillapi]: https://www.spigotmc.org/resources/4824/
@@ -79,3 +95,4 @@ If you use item ID without a namespace, it will search over all registries.
7995
[BukkitItemsRegistry]: ../mimic-bukkit-api/src/main/kotlin/items/BukkitItemsRegistry.kt
8096
[MimicItemsRegistry]: src/main/kotlin/impl/mimic/MimicItemsRegistry.kt
8197
[MinecraftItemsRegistry]: src/main/kotlin/impl/vanilla/MinecraftItemsRegistry.kt
98+
[ItemMetaPayload]: src/main/kotlin/impl/vanilla/ItemMetaPayload.kt

0 commit comments

Comments
 (0)