Skip to content

Commit dc1759e

Browse files
authored
Merge pull request #42 from DWCarrot/main
port to 1.21, bump version to 9.4 & reformat package version
2 parents e636065 + 9e26aa0 commit dc1759e

File tree

9 files changed

+177
-251
lines changed

9 files changed

+177
-251
lines changed

build.gradle.kts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ import java.util.*
44
plugins {
55
`java-library`
66
`maven-publish`
7-
id("io.papermc.paperweight.userdev") version "1.5.11"
8-
id("xyz.jpenilla.run-paper") version "2.2.3" // Adds runServer and runMojangMappedServer tasks for testing
7+
id("io.papermc.paperweight.userdev") version "1.7.1"
8+
id("xyz.jpenilla.run-paper") version "2.3.0" // Adds runServer and runMojangMappedServer tasks for testing
99
}
1010

1111
// = = =
1212

1313
val pluginName = "NyaaCore"
1414
val majorVersion = 9
15-
val minorVersion = 3
15+
val minorVersion = 4
1616

17-
val paperApiName = "1.20.4-R0.1-SNAPSHOT"
17+
val paperApiName = "1.21-R0.1-SNAPSHOT"
1818

1919
// = = =
2020

@@ -31,11 +31,11 @@ val javaDocDirectory =
3131
group = "cat.nyaa"
3232
//archivesBaseName = "${pluginNameUpper}-mc$minecraftVersion"
3333
version =
34-
"$majorVersion.$minorVersion-${getMcVersion(paperApiName)}-b$buildNumber"
34+
"$majorVersion.$minorVersion"
3535

3636
java {
37-
// Configure the java toolchain. This allows gradle to auto-provision JDK 17 on systems that only have JDK 8 installed for example.
38-
toolchain.languageVersion.set(JavaLanguageVersion.of(17))
37+
// Configure the java toolchain. This allows gradle to auto-provision JDK 21 on systems that only have JDK 8 installed for example.
38+
toolchain.languageVersion.set(JavaLanguageVersion.of(21))
3939
}
4040

4141
repositories {
@@ -51,18 +51,18 @@ repositories {
5151

5252
dependencies {
5353
paperweightDevelopmentBundle(paperweight.paperDevBundle(paperApiName))
54-
// paperweight.foliaDevBundle("1.20.4-R0.1-SNAPSHOT")
55-
// paperweight.devBundle("com.example.paperfork", "1.20.4-R0.1-SNAPSHOT")
56-
compileOnly("net.essentialsx:EssentialsX:2.19.6") // soft dep
57-
compileOnly("org.jetbrains:annotations:23.0.0")
54+
// paperweight.foliaDevBundle("1.21-R0.1-SNAPSHOT")
55+
// paperweight.devBundle("com.example.paperfork", "1.21-R0.1-SNAPSHOT")
56+
compileOnly("net.essentialsx:EssentialsX:2.20.1") // soft dep
57+
compileOnly("org.jetbrains:annotations:24.1.0")
5858
// Testing
59-
testImplementation(platform("org.junit:junit-bom:5.9.0"))
59+
testImplementation(platform("org.junit:junit-bom:5.10.3"))
6060
testImplementation("org.junit.jupiter:junit-jupiter")
61-
testImplementation("com.github.seeseemelk:MockBukkit-v1.19:2.132.3")
62-
testImplementation("org.mockito:mockito-core:4.8.0")
63-
testImplementation("org.mockito:mockito-junit-jupiter:4.8.0")
64-
testImplementation("org.xerial:sqlite-jdbc:3.39.3.0")
65-
testImplementation("ch.vorburger.mariaDB4j:mariaDB4j:2.6.0")
61+
testImplementation("com.github.seeseemelk:MockBukkit-v1.21:3.95.1")
62+
testImplementation("org.mockito:mockito-core:5.12.0")
63+
testImplementation("org.mockito:mockito-junit-jupiter:5.12.0")
64+
testImplementation("org.xerial:sqlite-jdbc:3.46.0.0")
65+
testImplementation("ch.vorburger.mariaDB4j:mariaDB4j:3.1.0")
6666
}
6767

6868
publishing {
@@ -110,16 +110,16 @@ tasks {
110110

111111
// Set the release flag. This configures what version bytecode the compiler will emit, as well as what JDK APIs are usable.
112112
// See https://openjdk.java.net/jeps/247 for more information.
113-
options.release.set(17)
113+
options.release.set(21)
114114
}
115115
javadoc {
116116
with((options as StandardJavadocDocletOptions)) {
117117
options.encoding =
118118
Charsets.UTF_8.name() // We want UTF-8 for everything
119-
links("https://docs.oracle.com/en/java/javase/17/docs/api/")
119+
links("https://docs.oracle.com/en/java/javase/21/docs/api/")
120120
links("https://guava.dev/releases/21.0/api/docs/")
121121
links("https://ci.md-5.net/job/BungeeCord/ws/chat/target/apidocs/")
122-
links("https://jd.papermc.io/paper/1.20/")
122+
links("https://jd.papermc.io/paper/1.21/")
123123
options.locale = "en_US"
124124
options.encoding = "UTF-8"
125125
(options as StandardJavadocDocletOptions).addBooleanOption(

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
1+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStorePath=wrapper/dists

src/main/java/cat/nyaa/nyaacore/NyaaCoreLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void onEnable() {
5353
try {
5454
serverVersion = SharedConstants.getCurrentVersion().getName();
5555
} catch (Exception e) {
56-
e.printStackTrace();
56+
getLogger().severe(e.getMessage());
5757
Bukkit.getPluginManager().disablePlugin(this);
5858
}
5959
try {

src/main/java/cat/nyaa/nyaacore/utils/ItemStackUtils.java

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414
import net.minecraft.util.datafix.DataFixers;
1515
import net.minecraft.util.datafix.fixes.References;
1616
import org.bukkit.Bukkit;
17-
import org.bukkit.craftbukkit.v1_20_R3.inventory.CraftItemStack;
17+
import org.bukkit.World;
18+
import org.bukkit.craftbukkit.CraftWorld;
19+
import org.bukkit.craftbukkit.inventory.CraftItemStack;
1820
import org.bukkit.inventory.ItemStack;
1921

2022
import java.io.*;
2123
import java.util.ArrayList;
2224
import java.util.Collections;
2325
import java.util.List;
26+
import java.util.Optional;
2427
import java.util.stream.Collectors;
2528
import java.util.zip.Deflater;
2629
import java.util.zip.DeflaterInputStream;
@@ -38,6 +41,24 @@ public final class ItemStackUtils {
3841
.maximumWeight(256L * 1024 * 1024).build(); // Hard Coded 256M
3942
private static NbtAccounter unlimitedNbtAccounter = null;
4043

44+
private static CraftWorld defaultWorld;
45+
46+
private static CraftWorld getDefaultWorld() {
47+
if (defaultWorld == null) {
48+
var worlds = Bukkit.getWorlds();
49+
if (!worlds.isEmpty()) {
50+
var first = worlds.getFirst();
51+
if (first instanceof CraftWorld) {
52+
defaultWorld = (CraftWorld) first;
53+
}
54+
}
55+
if(defaultWorld == null) {
56+
throw new IllegalStateException("No world available");
57+
}
58+
}
59+
return defaultWorld;
60+
}
61+
4162
static {
4263
//noinspection deprecation
4364
currentDataVersion = Bukkit.getUnsafe().getDataVersion();
@@ -51,14 +72,13 @@ public final class ItemStackUtils {
5172
* @return binary NBT representation of the item stack
5273
*/
5374
public static byte[] itemToBinary(ItemStack itemStack) throws IOException {
54-
net.minecraft.world.item.ItemStack nativeItemStack = CraftItemStack.asNMSCopy(itemStack);
55-
CompoundTag CompoundTag = new CompoundTag();
56-
nativeItemStack.save(CompoundTag);
57-
CompoundTag.putInt(NYAACORE_ITEMSTACK_DATAVERSION_KEY, currentDataVersion);
58-
75+
net.minecraft.world.item.ItemStack nativeItemStack = CraftItemStack.unwrap(itemStack);
76+
CompoundTag tagPrefix = new CompoundTag();
77+
tagPrefix.putInt(NYAACORE_ITEMSTACK_DATAVERSION_KEY, currentDataVersion);
78+
Tag tag = nativeItemStack.save(getDefaultWorld().getHandle().registryAccess(), tagPrefix);
5979
ByteArrayOutputStream baos = new ByteArrayOutputStream();
6080
DataOutputStream dos = new DataOutputStream(baos);
61-
CompoundTag.write(dos);
81+
tag.write(dos);
6282
byte[] outputByteArray = baos.toByteArray();
6383
dos.close();
6484
baos.close();
@@ -72,7 +92,7 @@ public static byte[] itemToBinary(ItemStack itemStack) throws IOException {
7292
* @param nbt binary item nbt data
7393
* @return constructed item
7494
*/
75-
public static ItemStack itemFromBinary(byte[] nbt) throws ReflectiveOperationException, IOException {
95+
public static ItemStack itemFromBinary(byte[] nbt) throws IOException {
7696
return itemFromBinary(nbt, 0, nbt.length);
7797
}
7898

@@ -103,8 +123,11 @@ public static ItemStack itemFromBinary(byte[] nbt, int offset, int len) throws I
103123
Dynamic<Tag> out = dataFixer_instance.update(References_ITEM_STACK, dynamicInstance, dataVersion, currentDataVersion);
104124
reconstructedCompoundTag = (CompoundTag) out.getValue();
105125
}
106-
net.minecraft.world.item.ItemStack reconstructedNativeItemStack = net.minecraft.world.item.ItemStack.of(reconstructedCompoundTag);
107-
return CraftItemStack.asBukkitCopy(reconstructedNativeItemStack);
126+
Optional<net.minecraft.world.item.ItemStack> reconstructedNativeItemStack = net.minecraft.world.item.ItemStack.parse(getDefaultWorld().getHandle().registryAccess(), reconstructedCompoundTag);
127+
if(reconstructedNativeItemStack.isEmpty()) {
128+
throw new IOException("Failed to parse item from binary");
129+
}
130+
return CraftItemStack.asCraftMirror(reconstructedNativeItemStack.get());
108131
}
109132

110133
private static byte[] compress(byte[] data) {
@@ -195,7 +218,7 @@ public static List<ItemStack> itemsFromBase64(String base64) {
195218
dis.readFully(tmp);
196219
ret.add(itemFromBinary(tmp));
197220
}
198-
} catch (IOException | ReflectiveOperationException ex) {
221+
} catch (IOException ex) {
199222
throw new RuntimeException(ex);
200223
}
201224
itemDeserializerCache.put(base64, ret.stream().map(ItemStack::clone).collect(Collectors.toList()));
@@ -222,12 +245,11 @@ public static ItemStack itemFromBase64(String base64) {
222245
public static String itemToJson(ItemStack itemStack) throws RuntimeException {
223246
CompoundTag nmsCompoundTagObj; // This will just be an empty CompoundTag instance to invoke the saveNms method
224247
net.minecraft.world.item.ItemStack nmsItemStackObj; // This is the net.minecraft.server.ItemStack object received from the asNMSCopy method
225-
CompoundTag itemAsJsonObject; // This is the net.minecraft.server.ItemStack after being put through saveNmsItem method
248+
Tag itemAsJsonObject; // This is the net.minecraft.server.ItemStack after being put through saveNmsItem method
226249

227250
try {
228-
nmsCompoundTagObj = new CompoundTag();
229-
nmsItemStackObj = CraftItemStack.asNMSCopy(itemStack);
230-
itemAsJsonObject = nmsItemStackObj.save(nmsCompoundTagObj);
251+
nmsItemStackObj = CraftItemStack.unwrap(itemStack);
252+
itemAsJsonObject = nmsItemStackObj.save(getDefaultWorld().getHandle().registryAccess());
231253
} catch (Throwable t) {
232254
throw new RuntimeException("failed to serialize itemstack to nms item", t);
233255
}

0 commit comments

Comments
 (0)