Skip to content

Commit 936a256

Browse files
committed
2.0
0 parents  commit 936a256

File tree

20 files changed

+1773
-0
lines changed

20 files changed

+1773
-0
lines changed

.gitignore

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
.gradle
2+
build/
3+
!gradle/wrapper/gradle-wrapper.jar
4+
!**/src/main/**/build/
5+
!**/src/test/**/build/
6+
7+
### IntelliJ IDEA ###
8+
.idea/
9+
*.iws
10+
*.iml
11+
*.ipr
12+
out/
13+
!**/src/main/**/out/
14+
!**/src/test/**/out/
15+
16+
### Eclipse ###
17+
.apt_generated
18+
.classpath
19+
.factorypath
20+
.project
21+
.settings
22+
.springBeans
23+
.sts4-cache
24+
bin/
25+
!**/src/main/**/bin/
26+
!**/src/test/**/bin/
27+
28+
### NetBeans ###
29+
/nbproject/private/
30+
/nbbuild/
31+
/dist/
32+
/nbdist/
33+
/.nb-gradle/
34+
35+
### VS Code ###
36+
.vscode/
37+
38+
### Mac OS ###
39+
.DS_Store

LICENSE

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
Cardboard Box
2+
=============
3+
4+
A simple library for storing and retrieving ItemStacks in Bukkit-related environments.
5+
6+
Example Code
7+
-------
8+
9+
Example demonstration of storing and retrieving the item in a player's hand:
10+
```java
11+
if (CardboardBox.isReady()) {
12+
ItemStack item = player.getInventory().getItemInMainHand();
13+
byte[] data = CardboardBox.serializeItem(item);
14+
this.getLogger().info(Base64.getEncoder().encodeToString(data));
15+
ItemStack item2 = CardboardBox.deserializeItem(data);
16+
this.getLogger().info(item2.toString());
17+
}
18+
```
19+
20+
Adding to your project
21+
-----
22+
23+
### Repository
24+
25+
```kotlin
26+
maven {
27+
name = "dependencyDownload"
28+
url = uri("https://dependency.download/releases")
29+
}
30+
```
31+
```xml
32+
<repository>
33+
<id>dependency.download</id>
34+
<url>https://dependency.download/repository/public/</url>
35+
</repository>
36+
```
37+
38+
### Dependency
39+
40+
41+
```kotlin
42+
implementation("dev.kitteh:cardboardbox:2.0.0")
43+
```
44+
```xml
45+
<dependency>
46+
<groupId>dev.kitteh</groupId>
47+
<artifactId>cardboardbox</artifactId>
48+
<version>2.0.0</version>
49+
</dependency>
50+
```

api/build.gradle.kts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
plugins {
2+
id("java")
3+
}
4+
5+
repositories {
6+
mavenCentral()
7+
maven("https://repo.papermc.io/repository/maven-public/")
8+
}
9+
10+
dependencies {
11+
compileOnly("com.destroystokyo.paper:paper-api:1.16.5-R0.1-SNAPSHOT")
12+
}
13+
14+
java {
15+
targetCompatibility = JavaVersion.VERSION_17
16+
sourceCompatibility = JavaVersion.VERSION_17
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package dev.kitteh.cardboardbox.api;
2+
3+
import org.bukkit.inventory.ItemStack;
4+
5+
public interface Box {
6+
default boolean check(ItemStack itemStack) throws Exception {
7+
return itemStack.equals(this.deserializeItem(this.serializeItem(itemStack)));
8+
}
9+
10+
ItemStack deserializeItem(byte[] data);
11+
12+
byte[] serializeItem(ItemStack item);
13+
}

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
group = "dev.kitteh"

combined/build.gradle.kts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
plugins {
2+
id("java")
3+
id("io.github.goooler.shadow") version "8.1.7"
4+
id("maven-publish")
5+
}
6+
7+
repositories {
8+
mavenCentral()
9+
maven("https://repo.papermc.io/repository/maven-public/")
10+
}
11+
12+
dependencies {
13+
compileOnly("com.destroystokyo.paper:paper-api:1.16.5-R0.1-SNAPSHOT")
14+
compileOnly("com.mojang:datafixerupper:1.0.20")
15+
implementation(project(":api"))
16+
implementation(project(":v1.20.6"))
17+
implementation(project(":v1.21"))
18+
}
19+
20+
java {
21+
targetCompatibility = JavaVersion.VERSION_17
22+
sourceCompatibility = JavaVersion.VERSION_17
23+
}
24+
25+
tasks.jar {
26+
archiveClassifier = "original"
27+
}
28+
29+
tasks.shadowJar {
30+
archiveClassifier = ""
31+
}
32+
33+
publishing {
34+
publications {
35+
create<MavenPublication>("cardboardbox") {
36+
artifact(tasks.shadowJar)
37+
//artifact(tasks["shadowJar"])
38+
groupId = "dev.kitteh"
39+
artifactId = "cardboardbox"
40+
version = "2.0.0"
41+
pom {
42+
name = "CardboardBox"
43+
description = "A Bukkit-related data storage handler"
44+
licenses {
45+
license {
46+
name = "GNU General Public License (GPL) version 3"
47+
url = "https://www.gnu.org/licenses/gpl-3.0.txt"
48+
}
49+
}
50+
developers {
51+
developer {
52+
id = "mbaxter"
53+
name = "Matt Baxter"
54+
email = "matt@kitteh.org"
55+
url = "https://www.kitteh.org/"
56+
organization = "Kitteh"
57+
organizationUrl = "https://www.kitteh.org"
58+
roles = setOf("Lead Developer", "Cat Wrangler")
59+
}
60+
}
61+
issueManagement {
62+
system = "GitHub"
63+
url = "https://github.com/KittehOrg/CardboardBox/issues"
64+
}
65+
scm {
66+
connection = "scm:git:git://github.com/KittehOrg/CardboardBox.git"
67+
developerConnection = "scm:git:git://github.com/KittehOrg/CardboardBox.git"
68+
url = "git@github.com:KittehOrg/CardboardBox.git"
69+
}
70+
repositories {
71+
maven {
72+
name = "DependencyDownload"
73+
val rel = "https://dependency.download/releases"
74+
val snap = "https://dependency.download/snapshots"
75+
url = uri(if (version.toString().endsWith("SNAPSHOT")) snap else rel)
76+
credentials(PasswordCredentials::class)
77+
}
78+
}
79+
}
80+
}
81+
}
82+
}
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package dev.kitteh.cardboardbox;
2+
3+
import dev.kitteh.cardboardbox.api.Box;
4+
import org.bukkit.Bukkit;
5+
import org.bukkit.Material;
6+
import org.bukkit.enchantments.Enchantment;
7+
import org.bukkit.inventory.ItemStack;
8+
import org.bukkit.inventory.meta.ItemMeta;
9+
import org.bukkit.plugin.java.JavaPlugin;
10+
11+
import java.util.Arrays;
12+
import java.util.regex.Matcher;
13+
import java.util.regex.Pattern;
14+
15+
public class CardboardBox {
16+
private static boolean ready = false;
17+
private static boolean modernPaper = false;
18+
private static Exception exception;
19+
20+
private static Box chosenBox;
21+
22+
static {
23+
init();
24+
}
25+
26+
private static void init() {
27+
try {
28+
ItemStack.class.getDeclaredMethod("deserializeBytes", byte[].class);
29+
ItemStack.class.getDeclaredMethod("serializeAsBytes");
30+
modernPaper = true;
31+
ready = true;
32+
return;
33+
} catch (Exception ignored) {
34+
}
35+
try {
36+
int mcVersion = getMcVersion();
37+
if (mcVersion < 808) {
38+
JavaPlugin.getProvidingPlugin(CardboardBox.class).getLogger().warning("CardboardBox could not identify Minecraft version.");
39+
ready = false;
40+
}
41+
if (mcVersion >= 2100) {
42+
chosenBox = getBox("v1_21");
43+
} else if (mcVersion == 2006) {
44+
chosenBox = getBox("v1_20_6");
45+
} else {
46+
chosenBox = new dev.kitteh.cardboardbox.pre_v1_20_6.Box();
47+
}
48+
ItemStack itemStack = new ItemStack(Material.DIAMOND_SWORD);
49+
ItemMeta meta = itemStack.getItemMeta();
50+
meta.addEnchant(Enchantment.KNOCKBACK, 3, false);
51+
meta.setDisplayName("Meow meow!");
52+
meta.setLore(Arrays.asList("New", "line", " here"));
53+
itemStack.setItemMeta(meta);
54+
if (!itemStack.equals(chosenBox.deserializeItem(chosenBox.serializeItem(itemStack)))) {
55+
throw new IllegalStateException("Failed to deserialize serialized content properly");
56+
}
57+
ready = true;
58+
} catch (Exception e) {
59+
exception = e;
60+
}
61+
}
62+
63+
private static Box getBox(String ver) throws Exception {
64+
return (Box) Class.forName(CardboardBox.class.getPackage().getName() + "." + ver + ".Box").getConstructor().newInstance();
65+
}
66+
67+
private static int getMcVersion() {
68+
Pattern versionPattern = Pattern.compile("1\\.(\\d{1,2})(?:\\.(\\d{1,2}))?");
69+
Matcher versionMatcher = versionPattern.matcher(Bukkit.getVersion());
70+
71+
int mcVersion = 0;
72+
if (versionMatcher.find()) {
73+
try {
74+
int minor = Integer.parseInt(versionMatcher.group(1));
75+
String patchS = versionMatcher.group(2);
76+
int patch = (patchS == null || patchS.isEmpty()) ? 0 : Integer.parseInt(patchS);
77+
mcVersion = (minor * 100) + patch;
78+
} catch (NumberFormatException ignored) {
79+
}
80+
}
81+
return mcVersion;
82+
}
83+
84+
/**
85+
* Gets if Cardboard Box will just use Paper's built-in functionality.
86+
*
87+
* @return true if the built-in methods exist, eliminating stress
88+
*/
89+
public static boolean isModernPaperSupport() {
90+
return modernPaper;
91+
}
92+
93+
/**
94+
* Gets if Cardboard Box is ready to work, or failed to initialize.
95+
*
96+
* @return true if ready
97+
*/
98+
public static boolean isReady() {
99+
return ready;
100+
}
101+
102+
/**
103+
* Gets the exception showing the failure to load, if not ready.
104+
*
105+
* @return exception if failed to load properly
106+
*/
107+
public static Exception getException() {
108+
return exception;
109+
}
110+
111+
/**
112+
* Serializes an ItemStack to bytes. Will store air and null the same, as
113+
* just a single byte of 0x0.
114+
*
115+
* @param item item to serialize
116+
* @return bytes of the item serialized
117+
* @throws IllegalStateException if CardboardBox failed to initialize
118+
* (check with {@link #isReady()})
119+
* @throws RuntimeException if serialization failed for any reason
120+
*/
121+
public static byte[] serializeItem(ItemStack item) {
122+
if (!ready) {
123+
throw new IllegalStateException("Cardboard Box failed to initialize. Cannot serialize without risk.", exception);
124+
}
125+
if (item == null || item.getType() == Material.AIR) {
126+
return new byte[]{0x0};
127+
}
128+
if (modernPaper) {
129+
return item.serializeAsBytes();
130+
}
131+
return chosenBox.serializeItem(item);
132+
}
133+
134+
/**
135+
* Deserializes an ItemStack previously serialized by Cardboard Box. Will
136+
* return air for a null or empty array of data.
137+
*
138+
* @param data data to deserialize
139+
* @return the ItemStack
140+
* @throws IllegalArgumentException if the stored item's version is
141+
* greater than the current server data version
142+
* @throws IllegalStateException if CardboardBox failed to initialize
143+
* (check with {@link #isReady()})
144+
* @throws RuntimeException if deserialization failed for any reason
145+
*/
146+
public static ItemStack deserializeItem(byte[] data) {
147+
if (!ready) {
148+
throw new IllegalStateException("Cardboard Box failed to initialize. Cannot serialize without risk.", exception);
149+
}
150+
if (data == null || data.length == 0 || (data.length == 1 && data[0] == 0x0)) {
151+
return new ItemStack(Material.AIR);
152+
}
153+
if (modernPaper) {
154+
return ItemStack.deserializeBytes(data);
155+
}
156+
return chosenBox.deserializeItem(data);
157+
}
158+
}

0 commit comments

Comments
 (0)