Skip to content

Commit a9b50d5

Browse files
committed
Update
1 parent 160f424 commit a9b50d5

File tree

4 files changed

+16
-63
lines changed

4 files changed

+16
-63
lines changed

build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ plugins {
1010
// = = =
1111

1212
val pluginName = "NyaaCore"
13-
val paperApiName = "1.21.5-R0.1-SNAPSHOT"
13+
val paperApiName = "1.21.6-R0.1-SNAPSHOT"
1414

1515
// = = =
1616

1717
group = "cat.nyaa"
18-
version ="9.8"
18+
version ="9.9"
1919

2020
java {
2121
// Configure the java toolchain. This allows gradle to auto-provision JDK 21 on systems that only have JDK 8 installed for example.

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import org.bukkit.configuration.serialization.ConfigurationSerialization;
99
import org.bukkit.plugin.PluginDescriptionFile;
1010
import org.bukkit.plugin.java.JavaPlugin;
11-
import org.bukkit.plugin.java.JavaPluginLoader;
1211

1312
import javax.annotation.Nullable;
1413
import java.io.File;
@@ -51,7 +50,7 @@ public void onEnable() {
5150
if (!isTest) {
5251
String serverVersion = "", targetVersion;
5352
try {
54-
serverVersion = SharedConstants.getCurrentVersion().getName();
53+
serverVersion = SharedConstants.getCurrentVersion().name();
5554
} catch (Exception e) {
5655
getLogger().severe(e.getMessage());
5756
Bukkit.getPluginManager().disablePlugin(this);

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

Lines changed: 8 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import net.minecraft.util.datafix.DataFixers;
1515
import net.minecraft.util.datafix.fixes.References;
1616
import org.bukkit.Bukkit;
17+
import org.bukkit.UnsafeValues;
1718
import org.bukkit.World;
1819
import org.bukkit.craftbukkit.CraftWorld;
1920
import org.bukkit.craftbukkit.inventory.CraftItemStack;
@@ -71,18 +72,8 @@ private static CraftWorld getDefaultWorld() {
7172
* @param itemStack the item to be serialized
7273
* @return binary NBT representation of the item stack
7374
*/
74-
public static byte[] itemToBinary(ItemStack itemStack) throws IOException {
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);
79-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
80-
DataOutputStream dos = new DataOutputStream(baos);
81-
tag.write(dos);
82-
byte[] outputByteArray = baos.toByteArray();
83-
dos.close();
84-
baos.close();
85-
return outputByteArray;
75+
public static byte[] itemToBinary(ItemStack itemStack) {
76+
return itemStack.serializeAsBytes();
8677
}
8778

8879
/**
@@ -93,41 +84,12 @@ public static byte[] itemToBinary(ItemStack itemStack) throws IOException {
9384
* @return constructed item
9485
*/
9586
public static ItemStack itemFromBinary(byte[] nbt) throws IOException {
96-
return itemFromBinary(nbt, 0, nbt.length);
87+
return ItemStack.deserializeBytes(nbt);
9788
}
9889

99-
public static ItemStack itemFromBinary(byte[] nbt, int offset, int len) throws IOException {
100-
if (unlimitedNbtAccounter == null) {
101-
unlimitedNbtAccounter = NbtAccounter.unlimitedHeap();
102-
}
103-
104-
//Constructor<?> constructNativeItemStackFromCompoundTag = classNativeItemStack.getConstructor(classCompoundTag);
105-
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(nbt, offset, len);
106-
DataInputStream dataInputStream = new DataInputStream(byteArrayInputStream);
107-
CompoundTag reconstructedCompoundTag = CompoundTag.TYPE.load(dataInputStream, unlimitedNbtAccounter);
108-
dataInputStream.close();
109-
byteArrayInputStream.close();
110-
int dataVersion = reconstructedCompoundTag.getInt(NYAACORE_ITEMSTACK_DATAVERSION_KEY).orElse(NYAACORE_ITEMSTACK_DEFAULT_DATAVERSION);
111-
if (dataVersion > 0) {
112-
reconstructedCompoundTag.remove(NYAACORE_ITEMSTACK_DATAVERSION_KEY);
113-
}
114-
if (dataVersion < currentDataVersion) {
115-
// 1.12 to 1.13
116-
if (dataVersion <= 0) {
117-
dataVersion = NYAACORE_ITEMSTACK_DEFAULT_DATAVERSION;
118-
}
119-
DSL.TypeReference References_ITEM_STACK = References.ITEM_STACK;
120-
NbtOps NbtOps_instance = NbtOps.INSTANCE;
121-
DataFixer dataFixer_instance = DataFixers.getDataFixer();
122-
Dynamic<Tag> dynamicInstance = new Dynamic<>(NbtOps_instance, reconstructedCompoundTag);
123-
Dynamic<Tag> out = dataFixer_instance.update(References_ITEM_STACK, dynamicInstance, dataVersion, currentDataVersion);
124-
reconstructedCompoundTag = (CompoundTag) out.getValue();
125-
}
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());
90+
@Deprecated
91+
public static ItemStack itemFromBinary(byte[] nbt, int offset, int len) {
92+
return ItemStack.deserializeBytes(nbt);
13193
}
13294

13395
private static byte[] compress(byte[] data) {
@@ -243,19 +205,7 @@ public static ItemStack itemFromBase64(String base64) {
243205
* NOTE: this method has no corresponding deserializer.
244206
*/
245207
public static String itemToJson(ItemStack itemStack) throws RuntimeException {
246-
CompoundTag nmsCompoundTagObj; // This will just be an empty CompoundTag instance to invoke the saveNms method
247-
net.minecraft.world.item.ItemStack nmsItemStackObj; // This is the net.minecraft.server.ItemStack object received from the asNMSCopy method
248-
Tag itemAsJsonObject; // This is the net.minecraft.server.ItemStack after being put through saveNmsItem method
249-
250-
try {
251-
nmsItemStackObj = CraftItemStack.unwrap(itemStack);
252-
itemAsJsonObject = nmsItemStackObj.save(getDefaultWorld().getHandle().registryAccess());
253-
} catch (Throwable t) {
254-
throw new RuntimeException("failed to serialize itemstack to nms item", t);
255-
}
256-
257-
// Return a string representation of the serialized object
258-
return itemAsJsonObject.toString();
208+
return Bukkit.getUnsafe().serializeItemAsJson(itemStack).getAsString();
259209
}
260210

261211
/**

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package cat.nyaa.nyaacore.utils;
22

33
import com.mojang.brigadier.exceptions.CommandSyntaxException;
4+
import com.mojang.logging.LogUtils;
45
import net.minecraft.advancements.critereon.NbtPredicate;
56
import net.minecraft.core.BlockPos;
67
import net.minecraft.nbt.CompoundTag;
78
import net.minecraft.nbt.TagParser;
9+
import net.minecraft.util.ProblemReporter;
810
import net.minecraft.world.entity.player.Player;
911
import net.minecraft.world.level.Level;
1012
import net.minecraft.world.level.block.entity.BlockEntity;
13+
import net.minecraft.world.level.storage.TagValueInput;
14+
import org.bukkit.Bukkit;
1115
import org.bukkit.World;
1216
import org.bukkit.block.Block;
1317
import org.bukkit.block.BlockState;
@@ -51,7 +55,7 @@ public static void setEntityTag(Entity e, String tag) {
5155
if (nmsClonedNBT.equals(nmsOrigNBT)) {
5256
} else {
5357
UUID uuid = nmsEntity.getUUID(); // store UUID
54-
nmsEntity.load(nmsClonedNBT); // set nbt
58+
nmsEntity.load(TagValueInput.create(new ProblemReporter.ScopedCollector(LogUtils.getLogger()),nmsEntity.registryAccess(),nmsClonedNBT)); // set nbt
5559
nmsEntity.setUUID(uuid); // set uuid
5660
}
5761
}

0 commit comments

Comments
 (0)