Skip to content

Commit 964586b

Browse files
authored
Merge pull request #4 from Pinont/dev
Add comprehensive unit tests for ItemStack metadata methods
2 parents e850f50 + 162a83f commit 964586b

File tree

11 files changed

+620
-51
lines changed

11 files changed

+620
-51
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
name: Build & Test (No Deploy)
1+
name: Build & Test
22

33
on:
44
pull_request:
55
push:
66
branches:
77
- main
8+
- dev
89

910
jobs:
1011
build:

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# SingularityLib
22

3-
[![](https://jitpack.io/v/Pinont/SingularityLib.svg)](https://jitpack.io/#Pinont/SingularityLib) ![GitHub Tag](https://img.shields.io/github/v/tag/pinont/singularitylib)
4-
[![license](https://img.shields.io/github/license/pinont/singularitylib)](https://github.com/Pinont/SingularityLib/blob/main/LICENSE)
5-
63
A fork of [ExperienceLib](https://github.com/pinont/ExperienceLib)
74

85

96
> ## ⚠️ Disclaimer
107
> This project is still in development, so expect some bugs and missing features. If you find any bugs or have any feature requests, please open an issue on the [GitHub repository](https://github.com/Pinont/SingularityLib/issues)
118
9+
[![](https://jitpack.io/v/Pinont/SingularityLib.svg)](https://jitpack.io/#Pinont/SingularityLib) ![GitHub Tag](https://img.shields.io/github/v/tag/pinont/singularitylib)
10+
[![license](https://img.shields.io/github/license/pinont/singularitylib)](https://github.com/Pinont/SingularityLib/blob/main/LICENSE) [![Build & Test (No Deploy)](https://github.com/Pinont/SingularityLib/actions/workflows/build.yml/badge.svg)](https://github.com/Pinont/SingularityLib/actions/workflows/build.yml)
11+
1212
---
1313

1414
A Minecraft plugin api that provides a lot of benefit to develop Minecraft plugin much easier

pom.xml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.github.pinont</groupId>
88
<artifactId>singularitylib</artifactId>
9-
<version>2.2.1-SNAPSHOT</version>
9+
<version>2.2.2-SNAPSHOT</version>
1010
<packaging>jar</packaging>
1111

1212
<name>SingularityLib</name>
@@ -144,7 +144,7 @@
144144
<dependency>
145145
<groupId>io.papermc.paper</groupId>
146146
<artifactId>paper-api</artifactId>
147-
<version>1.21.5-R0.1-SNAPSHOT</version>
147+
<version>1.21.8-R0.1-SNAPSHOT</version>
148148
<scope>provided</scope>
149149
</dependency>
150150
<dependency>
@@ -162,5 +162,17 @@
162162
<artifactId>JDA</artifactId>
163163
<version>5.0.0-beta.21</version>
164164
</dependency>
165+
<dependency>
166+
<groupId>org.mockbukkit.mockbukkit</groupId>
167+
<artifactId>mockbukkit-v1.21</artifactId>
168+
<version>4.77.0</version>
169+
<scope>test</scope>
170+
</dependency>
171+
<dependency>
172+
<groupId>org.slf4j</groupId>
173+
<artifactId>slf4j-simple</artifactId>
174+
<version>2.0.16</version>
175+
<scope>test</scope>
176+
</dependency>
165177
</dependencies>
166178
</project>

src/main/java/com/github/pinont/singularitylib/api/items/CrossbowCreator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public CrossbowCreator() {
1212
}
1313

1414
public ItemCreator addChargedProjectile(ItemStack arrow) {
15-
ItemMeta meta = this.getMeta();
15+
ItemMeta meta = this.getItemMeta();
1616
CrossbowMeta crossbowMeta = (CrossbowMeta) meta;
1717
crossbowMeta.addChargedProjectile(arrow);
1818
this.setItemMeta(meta);

src/main/java/com/github/pinont/singularitylib/api/items/ItemCreator.java

Lines changed: 95 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.pinont.singularitylib.api.items;
22

3+
import com.github.pinont.singularitylib.api.utils.Console;
34
import com.google.common.collect.Sets;
45
import com.github.pinont.singularitylib.api.enums.AttributeType;
56
import com.github.pinont.singularitylib.api.enums.PersisDataType;
@@ -18,6 +19,7 @@
1819
import org.bukkit.persistence.PersistentDataType;
1920
import org.bukkit.plugin.Plugin;
2021
import org.jetbrains.annotations.NotNull;
22+
import org.jetbrains.annotations.Nullable;
2123

2224
import java.util.*;
2325

@@ -43,6 +45,10 @@ public static Set<ItemInteraction> getInteractions() {
4345
return ITEM_INTERACTIONS;
4446
}
4547

48+
public ItemCreator clone() {
49+
return new ItemCreator(this.create());
50+
}
51+
4652
public ItemCreator(Material type) {
4753
this(new ItemStack(type));
4854
}
@@ -51,60 +57,49 @@ public ItemCreator(Material type, int amount) {
5157
this(new ItemStack(type, amount));
5258
}
5359

54-
public ItemCreator(@NotNull Material type, int amount, short damage) {
55-
this(new ItemStack(type, amount, damage, null));
56-
}
57-
58-
public ItemCreator(@NotNull Material type, int amount, short damage, Byte data) {
59-
this(new ItemStack(type, amount, damage, data));
60-
}
61-
6260
public ItemCreator(@NotNull ItemStack item) {
6361
this.item = item;
6462
this.meta = item.getItemMeta();
6563
this.type = item.getType();
64+
this.amount = item.getAmount();
6665
this.name = item.getItemMeta().getDisplayName().isEmpty() ? Common.normalizeStringName(item.getType().name()) : item.getItemMeta().getDisplayName();
6766
data = meta != null ? meta.getPersistentDataContainer() : null;
6867
}
6968

7069
public String getName() {
71-
return name;
70+
return this.create().getItemMeta().getDisplayName();
7271
}
7372

7473
public ItemMeta getItemMeta() {
75-
return meta;
74+
return this.create().getItemMeta();
7675
}
7776

7877
public Material getType() {
79-
return type;
78+
return this.create().getType();
8079
}
8180

8281
public int getAmount() {
83-
return amount;
82+
return this.create().getAmount();
8483
}
8584

8685
public ItemStack create() {
87-
item.setType(type);
88-
if (meta == null) {meta = item.getItemMeta();}
89-
if (meta != null) {
90-
meta.lore(lore);
86+
this.item.setType(type);
87+
if (this.meta == null) {this.meta = this.item.getItemMeta();}
88+
if (this.meta != null) {
89+
this.meta.lore(this.lore);
9190
}
92-
item.setItemMeta(meta);
93-
item.setDurability(durability);
94-
item.setAmount(amount);
95-
return item;
91+
this.item.setItemMeta(this.meta);
92+
this.item.setDurability(this.durability);
93+
this.item.setAmount(this.amount);
94+
return this.item;
9695
}
9796

9897
public Boolean hasTag(String tag) {
99-
return Objects.requireNonNull(item.getItemMeta()).getPersistentDataContainer().has(new NamespacedKey(plugin, tag), PersistentDataType.STRING);
100-
}
101-
102-
public String getTag(String key) {
103-
return getKey(key);
98+
return data.has(new NamespacedKey(plugin, tag), PersistentDataType.STRING);
10499
}
105100

106101
public String getKey(String key) {
107-
return data.get(new NamespacedKey(plugin, key), PersistentDataType.STRING);
102+
return getTagValue(key);
108103
}
109104

110105
public ItemCreator addLore(String lore) {
@@ -156,13 +151,12 @@ public ItemCreator setAmount(int amount) {
156151
return this;
157152
}
158153

159-
public ItemCreator setName(String name) {
160-
meta.displayName(common.colorize(name));
161-
meta.itemName(common.colorize(name));
162-
return this;
154+
public ItemCreator setName(@Nullable String name) {
155+
return this.setName(common.colorize(name != null ? name : ""));
163156
}
164157

165-
public ItemCreator setName(Component name) {
158+
public ItemCreator setName(@Nullable Component name) {
159+
if (name == null) name = common.colorize("");
166160
meta.displayName(name);
167161
meta.itemName(name);
168162
return this;
@@ -208,23 +202,86 @@ public ItemCreator setModelData(int data) {
208202
return this;
209203
}
210204

211-
public ItemCreator setTag(String tag) {
212-
data.set(new NamespacedKey(plugin, tag), PersistentDataType.STRING, tag);
205+
private void sendTagNamingPatternWarn(String tag) {
206+
Console.logWarning("Tag naming pattern warning: '" + tag + "' tag should not contain spaces and should be lowercase. It has been converted to '" + tag.replace(" ", "_").toLowerCase() + "'");
207+
}
208+
209+
private String formatTag(String tag) {
210+
String tagFormatted = String.join(" ", tag.replace(" ", "_"));
211+
if (!tagFormatted.equals(tag)) {
212+
sendTagNamingPatternWarn(tag);
213+
tag = tagFormatted;
214+
}
215+
return tag;
216+
}
217+
218+
public ItemCreator addTags(String... tags) {
219+
for (String tag : tags) {
220+
String formatTag = formatTag(tag);
221+
data.set(new NamespacedKey(plugin, formatTag), PersistentDataType.STRING, tag);
222+
}
213223
return this;
214224
}
215225

216-
public ItemCreator setTag(String key, String value) {
226+
public ItemCreator addTag(String key) {
227+
String formatTag = formatTag(key);
228+
addTag(formatTag, key);
229+
return this;
230+
}
231+
232+
public ItemCreator addTag(String key, String value) {
233+
key = formatTag(key);
217234
data.set(new NamespacedKey(plugin, key), PersistentDataType.STRING, value);
218235
return this;
219236
}
220237

221-
public ItemCreator setDurability(int durability) {
222-
this.durability = durability < 0 ? 0 : (short) durability;
238+
public ItemCreator replaceTag(String oldKey, String newKey) {
239+
oldKey = formatTag(oldKey);
240+
newKey = formatTag(newKey);
241+
if (hasTag(oldKey)) {
242+
if (oldKey.equals(getTagValue(oldKey))) {
243+
removeTag(oldKey);
244+
data.set(new NamespacedKey(plugin, newKey), PersistentDataType.STRING, newKey);
245+
return this;
246+
}
247+
String value = getTagValue(oldKey);
248+
removeTag(oldKey);
249+
data.set(new NamespacedKey(plugin, newKey), PersistentDataType.STRING, value);
250+
} else {
251+
sendConsoleMessage(ChatColor.RED + "Item tag not found: " + oldKey);
252+
}
253+
return this;
254+
}
255+
256+
public String getTagValue(String key) {
257+
key = formatTag(key);
258+
return data.get(new NamespacedKey(plugin, key), PersistentDataType.STRING);
259+
}
260+
261+
public ItemCreator setTagValue(String key, String newValue) {
262+
key = formatTag(key);
263+
if (hasTag(key)) {
264+
removeTag(key);
265+
data.set(new NamespacedKey(plugin, key), PersistentDataType.STRING, newValue);
266+
} else {
267+
sendConsoleMessage(ChatColor.RED + "Item tag not found: " + key);
268+
}
269+
return this;
270+
}
271+
272+
public ItemCreator removeTag(String tag) {
273+
tag = formatTag(tag);
274+
if (hasTag(tag)) {
275+
data.remove(new NamespacedKey(plugin, tag));
276+
} else {
277+
sendConsoleMessage(ChatColor.RED + "Item tag not found: " + tag);
278+
}
223279
return this;
224280
}
225281

226-
public ItemMeta getMeta() {
227-
return Objects.requireNonNull(item).getItemMeta();
282+
public ItemCreator setDurability(int durability) {
283+
this.durability = durability < 0 ? 0 : (short) durability;
284+
return this;
228285
}
229286

230287
public static Object getItemPersistData(ItemStack item, String key, PersistentDataType type) {

src/main/java/com/github/pinont/singularitylib/api/utils/Common.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,23 @@
1010
import org.bukkit.inventory.ItemStack;
1111
import org.bukkit.plugin.Plugin;
1212
import org.jetbrains.annotations.NotNull;
13+
import org.jetbrains.annotations.Nullable;
1314

1415
import java.util.*;
1516

1617
import static com.github.pinont.singularitylib.plugin.CorePlugin.getInstance;
1718

1819
public class Common {
1920

20-
public @NotNull Component colorize(String message, boolean noItalic) {
21+
public @NotNull Component colorize(@NotNull String message, boolean noItalic) {
2122
MiniMessage miniMessage = MiniMessage.miniMessage();
2223
if (noItalic) {
2324
message = "<!italic>" + message;
2425
}
2526
return miniMessage.deserialize(message);
2627
}
2728

28-
public @NotNull Component colorize(String message) {
29+
public @NotNull Component colorize(@NotNull String message) {
2930
return colorize(message, false);
3031
}
3132

src/main/java/com/github/pinont/singularitylib/plugin/CorePlugin.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public abstract class CorePlugin extends JavaPlugin {
2929

3030
private static String prefix;
3131
private static Long startTime;
32+
public boolean isTest = false;
3233

3334
public static Long getStartTime() {
3435
return startTime;
@@ -77,7 +78,7 @@ public static JavaPlugin getInstance() {
7778

7879
public static void sendDebugMessage(String message) {
7980
if (getInstance().getConfig().getBoolean("debug")) {
80-
sendConsoleMessage(ChatColor.ITALIC + "" + ChatColor.LIGHT_PURPLE + getPrefix() + ChatColor.YELLOW + " [DEV] " + ChatColor.WHITE + message);
81+
Bukkit.getConsoleSender().sendMessage(ChatColor.ITALIC + "" + ChatColor.LIGHT_PURPLE + getPrefix() + ChatColor.YELLOW + " [DEV] " + ChatColor.WHITE + message);
8182
}
8283
}
8384

@@ -119,9 +120,11 @@ public final void onEnable() {
119120
// new CommandManager().register(this, this.simpleCommands);
120121

121122
// Register Command, CustomItem, and Listeners.
122-
Register register = new Register();
123-
register.scanAndCollect(this.getClass().getPackageName());
124-
register.registerAll(this);
123+
if (!isTest) {
124+
Register register = new Register();
125+
register.scanAndCollect(this.getClass().getPackageName());
126+
register.registerAll(this);
127+
}
125128
}
126129

127130
private void registerAPIListener(Plugin plugin, Listener... listener) {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.github.pinont.singularitylib;
2+
3+
import org.junit.jupiter.api.*;
4+
import org.mockbukkit.mockbukkit.MockBukkit;
5+
import org.mockbukkit.mockbukkit.ServerMock;
6+
7+
public class CorePluginTest {
8+
9+
private ServerMock server;
10+
private TestPlugin plugin;
11+
12+
@BeforeEach
13+
public void setUp() {
14+
// Start the mock server
15+
this.server = MockBukkit.mock();
16+
// Load your plugin
17+
this.plugin = MockBukkit.load(TestPlugin.class);
18+
}
19+
20+
@AfterEach
21+
public void tearDown() {
22+
// Stop the mock server
23+
MockBukkit.unmock();
24+
}
25+
26+
@Test
27+
@DisplayName("Test CorePlugin Initialization")
28+
public void load() {
29+
// Check if the plugin is loaded
30+
Assertions.assertEquals(TestPlugin.getInstance(), this.plugin);
31+
// Check if the plugin is enabled
32+
assert this.plugin.isEnabled();
33+
// Check if the plugin name is correct
34+
assert this.plugin.getName().equals("TestPlugin");
35+
// Check if the plugin is marked as test
36+
assert this.plugin.isTest;
37+
}
38+
39+
@Test
40+
@DisplayName("Test MySQL Dependency Absence")
41+
public void noMySQL() {
42+
// Check if the MySQL dependency is absent
43+
Assertions.assertNull(this.plugin.getConfig().getString("mysql.host"));
44+
Assertions.assertNull(this.plugin.getConfig().getString("mysql.database"));
45+
Assertions.assertNull(this.plugin.getConfig().getString("mysql.username"));
46+
Assertions.assertNull(this.plugin.getConfig().getString("mysql.password"));
47+
}
48+
}

0 commit comments

Comments
 (0)