This commit is contained in:
2024-08-20 14:30:19 +02:00
parent 346a14c2be
commit 3fbc86e658
15 changed files with 443 additions and 154 deletions
@@ -1,6 +1,37 @@
package systems.brn.servershop;
public record ItemPrice(
int buyPrice, int sellPrice
) {
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.registry.RegistryWrapper;
import java.util.Optional;
public record ItemPrice(int buyPrice, int sellPrice, ItemStack stack) {
public NbtCompound toNbt(RegistryWrapper.WrapperLookup wrapperLookup) {
NbtCompound nbt = new NbtCompound();
nbt.putInt("BuyPrice", this.buyPrice);
nbt.putInt("SellPrice", this.sellPrice);
// Serialize the ItemStack to NBT and add it to the compound
NbtElement stackNbt = stack.encode(wrapperLookup);
nbt.put("ItemStack", stackNbt); // Adds the ItemStack's NBT data to the main NBT compound
return nbt;
}
public static ItemPrice fromNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup wrapperLookup) {
int buyPrice = nbt.getInt("BuyPrice");
int sellPrice = nbt.getInt("SellPrice");
// Deserialize the ItemStack from the NBT
NbtElement stackElement = nbt.get("ItemStack");
Optional<ItemStack> stack = ItemStack.fromNbt(wrapperLookup, stackElement);
return stack.map(itemStack -> new ItemPrice(buyPrice, sellPrice, itemStack)).orElse(null);
}
}