Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
name: Build & Test (No Deploy)
name: Build & Test

on:
pull_request:
push:
branches:
- main
- dev

jobs:
build:
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# SingularityLib

[![](https://jitpack.io/v/Pinont/SingularityLib.svg)](https://jitpack.io/#Pinont/SingularityLib) ![GitHub Tag](https://img.shields.io/github/v/tag/pinont/singularitylib)
[![license](https://img.shields.io/github/license/pinont/singularitylib)](https://github.com/Pinont/SingularityLib/blob/main/LICENSE)

A fork of [ExperienceLib](https://github.com/pinont/ExperienceLib)


> ## ⚠️ Disclaimer
> 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)

[![](https://jitpack.io/v/Pinont/SingularityLib.svg)](https://jitpack.io/#Pinont/SingularityLib) ![GitHub Tag](https://img.shields.io/github/v/tag/pinont/singularitylib)
[![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)

---

A Minecraft plugin api that provides a lot of benefit to develop Minecraft plugin much easier
Expand Down
16 changes: 14 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.pinont</groupId>
<artifactId>singularitylib</artifactId>
<version>2.2.1-SNAPSHOT</version>
<version>2.2.2-SNAPSHOT</version>
<packaging>jar</packaging>

<name>SingularityLib</name>
Expand Down Expand Up @@ -144,7 +144,7 @@
<dependency>
<groupId>io.papermc.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.21.5-R0.1-SNAPSHOT</version>
<version>1.21.8-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand All @@ -162,5 +162,17 @@
<artifactId>JDA</artifactId>
<version>5.0.0-beta.21</version>
</dependency>
<dependency>
<groupId>org.mockbukkit.mockbukkit</groupId>
<artifactId>mockbukkit-v1.21</artifactId>
<version>4.77.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.16</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public CrossbowCreator() {
}

public ItemCreator addChargedProjectile(ItemStack arrow) {
ItemMeta meta = this.getMeta();
ItemMeta meta = this.getItemMeta();
CrossbowMeta crossbowMeta = (CrossbowMeta) meta;
crossbowMeta.addChargedProjectile(arrow);
this.setItemMeta(meta);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.pinont.singularitylib.api.items;

import com.github.pinont.singularitylib.api.utils.Console;
import com.google.common.collect.Sets;
import com.github.pinont.singularitylib.api.enums.AttributeType;
import com.github.pinont.singularitylib.api.enums.PersisDataType;
Expand All @@ -18,6 +19,7 @@
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;

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

public ItemCreator clone() {
return new ItemCreator(this.create());
}

public ItemCreator(Material type) {
this(new ItemStack(type));
}
Expand All @@ -51,60 +57,49 @@ public ItemCreator(Material type, int amount) {
this(new ItemStack(type, amount));
}

public ItemCreator(@NotNull Material type, int amount, short damage) {
this(new ItemStack(type, amount, damage, null));
}

public ItemCreator(@NotNull Material type, int amount, short damage, Byte data) {
this(new ItemStack(type, amount, damage, data));
}

public ItemCreator(@NotNull ItemStack item) {
this.item = item;
this.meta = item.getItemMeta();
this.type = item.getType();
this.amount = item.getAmount();
this.name = item.getItemMeta().getDisplayName().isEmpty() ? Common.normalizeStringName(item.getType().name()) : item.getItemMeta().getDisplayName();
data = meta != null ? meta.getPersistentDataContainer() : null;
}

public String getName() {
return name;
return this.create().getItemMeta().getDisplayName();
}

public ItemMeta getItemMeta() {
return meta;
return this.create().getItemMeta();
}

public Material getType() {
return type;
return this.create().getType();
}

public int getAmount() {
return amount;
return this.create().getAmount();
}

public ItemStack create() {
item.setType(type);
if (meta == null) {meta = item.getItemMeta();}
if (meta != null) {
meta.lore(lore);
this.item.setType(type);
if (this.meta == null) {this.meta = this.item.getItemMeta();}
if (this.meta != null) {
this.meta.lore(this.lore);
}
item.setItemMeta(meta);
item.setDurability(durability);
item.setAmount(amount);
return item;
this.item.setItemMeta(this.meta);
this.item.setDurability(this.durability);
this.item.setAmount(this.amount);
return this.item;
}

public Boolean hasTag(String tag) {
return Objects.requireNonNull(item.getItemMeta()).getPersistentDataContainer().has(new NamespacedKey(plugin, tag), PersistentDataType.STRING);
}

public String getTag(String key) {
return getKey(key);
return data.has(new NamespacedKey(plugin, tag), PersistentDataType.STRING);
}

public String getKey(String key) {
return data.get(new NamespacedKey(plugin, key), PersistentDataType.STRING);
return getTagValue(key);
}

public ItemCreator addLore(String lore) {
Expand Down Expand Up @@ -156,13 +151,12 @@ public ItemCreator setAmount(int amount) {
return this;
}

public ItemCreator setName(String name) {
meta.displayName(common.colorize(name));
meta.itemName(common.colorize(name));
return this;
public ItemCreator setName(@Nullable String name) {
return this.setName(common.colorize(name != null ? name : ""));
}

public ItemCreator setName(Component name) {
public ItemCreator setName(@Nullable Component name) {
if (name == null) name = common.colorize("");
meta.displayName(name);
meta.itemName(name);
return this;
Expand Down Expand Up @@ -208,23 +202,86 @@ public ItemCreator setModelData(int data) {
return this;
}

public ItemCreator setTag(String tag) {
data.set(new NamespacedKey(plugin, tag), PersistentDataType.STRING, tag);
private void sendTagNamingPatternWarn(String tag) {
Console.logWarning("Tag naming pattern warning: '" + tag + "' tag should not contain spaces and should be lowercase. It has been converted to '" + tag.replace(" ", "_").toLowerCase() + "'");
}

private String formatTag(String tag) {
String tagFormatted = String.join(" ", tag.replace(" ", "_"));
if (!tagFormatted.equals(tag)) {
sendTagNamingPatternWarn(tag);
tag = tagFormatted;
}
return tag;
}

public ItemCreator addTags(String... tags) {
for (String tag : tags) {
String formatTag = formatTag(tag);
data.set(new NamespacedKey(plugin, formatTag), PersistentDataType.STRING, tag);
}
return this;
}

public ItemCreator setTag(String key, String value) {
public ItemCreator addTag(String key) {
String formatTag = formatTag(key);
addTag(formatTag, key);
return this;
}

public ItemCreator addTag(String key, String value) {
key = formatTag(key);
data.set(new NamespacedKey(plugin, key), PersistentDataType.STRING, value);
return this;
}

public ItemCreator setDurability(int durability) {
this.durability = durability < 0 ? 0 : (short) durability;
public ItemCreator replaceTag(String oldKey, String newKey) {
oldKey = formatTag(oldKey);
newKey = formatTag(newKey);
if (hasTag(oldKey)) {
if (oldKey.equals(getTagValue(oldKey))) {
removeTag(oldKey);
data.set(new NamespacedKey(plugin, newKey), PersistentDataType.STRING, newKey);
return this;
}
String value = getTagValue(oldKey);
removeTag(oldKey);
data.set(new NamespacedKey(plugin, newKey), PersistentDataType.STRING, value);
} else {
sendConsoleMessage(ChatColor.RED + "Item tag not found: " + oldKey);
}
return this;
}

public String getTagValue(String key) {
key = formatTag(key);
return data.get(new NamespacedKey(plugin, key), PersistentDataType.STRING);
}

public ItemCreator setTagValue(String key, String newValue) {
key = formatTag(key);
if (hasTag(key)) {
removeTag(key);
data.set(new NamespacedKey(plugin, key), PersistentDataType.STRING, newValue);
} else {
sendConsoleMessage(ChatColor.RED + "Item tag not found: " + key);
}
return this;
}

public ItemCreator removeTag(String tag) {
tag = formatTag(tag);
if (hasTag(tag)) {
data.remove(new NamespacedKey(plugin, tag));
} else {
sendConsoleMessage(ChatColor.RED + "Item tag not found: " + tag);
}
return this;
}

public ItemMeta getMeta() {
return Objects.requireNonNull(item).getItemMeta();
public ItemCreator setDurability(int durability) {
this.durability = durability < 0 ? 0 : (short) durability;
return this;
}

public static Object getItemPersistData(ItemStack item, String key, PersistentDataType type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,23 @@
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;

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

public class Common {

public @NotNull Component colorize(String message, boolean noItalic) {
public @NotNull Component colorize(@NotNull String message, boolean noItalic) {
MiniMessage miniMessage = MiniMessage.miniMessage();
if (noItalic) {
message = "<!italic>" + message;
}
return miniMessage.deserialize(message);
}

public @NotNull Component colorize(String message) {
public @NotNull Component colorize(@NotNull String message) {
return colorize(message, false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public abstract class CorePlugin extends JavaPlugin {

private static String prefix;
private static Long startTime;
public boolean isTest = false;

public static Long getStartTime() {
return startTime;
Expand Down Expand Up @@ -77,7 +78,7 @@ public static JavaPlugin getInstance() {

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

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

// Register Command, CustomItem, and Listeners.
Register register = new Register();
register.scanAndCollect(this.getClass().getPackageName());
register.registerAll(this);
if (!isTest) {
Register register = new Register();
register.scanAndCollect(this.getClass().getPackageName());
register.registerAll(this);
}
}

private void registerAPIListener(Plugin plugin, Listener... listener) {
Expand Down
48 changes: 48 additions & 0 deletions src/test/java/com/github/pinont/singularitylib/CorePluginTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.github.pinont.singularitylib;

import org.junit.jupiter.api.*;
import org.mockbukkit.mockbukkit.MockBukkit;
import org.mockbukkit.mockbukkit.ServerMock;

public class CorePluginTest {

private ServerMock server;
private TestPlugin plugin;

@BeforeEach
public void setUp() {
// Start the mock server
this.server = MockBukkit.mock();
// Load your plugin
this.plugin = MockBukkit.load(TestPlugin.class);
}

@AfterEach
public void tearDown() {
// Stop the mock server
MockBukkit.unmock();
}

@Test
@DisplayName("Test CorePlugin Initialization")
public void load() {
// Check if the plugin is loaded
Assertions.assertEquals(TestPlugin.getInstance(), this.plugin);
// Check if the plugin is enabled
assert this.plugin.isEnabled();
// Check if the plugin name is correct
assert this.plugin.getName().equals("TestPlugin");
// Check if the plugin is marked as test
assert this.plugin.isTest;
}

@Test
@DisplayName("Test MySQL Dependency Absence")
public void noMySQL() {
// Check if the MySQL dependency is absent
Assertions.assertNull(this.plugin.getConfig().getString("mysql.host"));
Assertions.assertNull(this.plugin.getConfig().getString("mysql.database"));
Assertions.assertNull(this.plugin.getConfig().getString("mysql.username"));
Assertions.assertNull(this.plugin.getConfig().getString("mysql.password"));
}
}
Loading