diff --git a/build.gradle b/build.gradle index 3b9598e..4df38e0 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ plugins { - id 'fabric-loom' version '1.7-SNAPSHOT' + id 'fabric-loom' version '1.10-SNAPSHOT' id 'maven-publish' } diff --git a/gradle.properties b/gradle.properties index 40ab4f6..039b03b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,20 +2,20 @@ org.gradle.jvmargs=-Xmx1G # Fabric Properties # check these on https://modmuss50.me/fabric.html -minecraft_version=1.21.4 -yarn_mappings=1.21.4+build.1 -loader_version=0.16.9 +minecraft_version=1.21.5 +yarn_mappings=1.21.5+build.1 +loader_version=0.16.13 # Mod Properties -mod_version=1.5.3 +mod_version=1.5.4 maven_group=systems.brn archives_base_name=servershop # Dependencies # check this on https://modmuss50.me/fabric.html -fabric_version=0.110.5+1.21.4 +fabric_version=0.120.0+1.21.5 # Dependencies -polymer_version=0.11.0+1.21.4-rc1 -server_translations_api_version=2.4.0+1.21.2-rc1 -servergui_version=1.8.1+1.21.4 -placeholder_api=2.5.1+1.21.3 \ No newline at end of file +polymer_version=0.12.3+1.21.5 +server_translations_api_version=2.5.0+1.21.5-rc1 +servergui_version=1.9.0+1.21.5 +placeholder_api=2.6.2+1.21.5 \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index a441313..cea7a79 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.12-bin.zip networkTimeout=10000 validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME diff --git a/src/main/java/systems/brn/servershop/lib/Util.java b/src/main/java/systems/brn/servershop/lib/Util.java index eca1603..3ee86dd 100644 --- a/src/main/java/systems/brn/servershop/lib/Util.java +++ b/src/main/java/systems/brn/servershop/lib/Util.java @@ -7,6 +7,7 @@ import net.minecraft.entity.player.PlayerInventory; import net.minecraft.inventory.Inventory; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NbtCompound; import net.minecraft.server.MinecraftServer; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.*; @@ -19,6 +20,7 @@ import systems.brn.servershop.ServerShop; import systems.brn.servershop.lib.records.AuctionRecord; import systems.brn.servershop.lib.records.ItemPriceRecord; +import java.nio.ByteBuffer; import java.util.*; public class Util { @@ -48,8 +50,8 @@ public class Util { int maxInsert = 0; // Iterate through the slots in the player's inventory - for (int i = 0; i < inventory.main.size(); i++) { - ItemStack slotStack = inventory.main.get(i); + for (int i = 0; i < inventory.getMainStacks().size(); i++) { + ItemStack slotStack = inventory.getMainStacks().get(i); maxInsert = canInsertToStack(slotStack, itemStackIn, maxInsert); } @@ -95,7 +97,7 @@ public class Util { } public static ItemStack insertStackIntoInventory(PlayerInventory playerInventory, ItemStack stack) { - DefaultedList inventory = playerInventory.main; + DefaultedList inventory = playerInventory.getMainStacks(); // First, try to merge with existing stacks for (ItemStack slotStack : inventory) { if (canCombine(slotStack, stack)) { @@ -442,4 +444,36 @@ public class Util { } return false; } + + + public static void putUUID(NbtCompound nbtCompound, String name, UUID uuid) { + Objects.requireNonNull(uuid, "UUID cannot be null"); + ByteBuffer buffer = ByteBuffer.allocate(16); + buffer.putLong(uuid.getMostSignificantBits()); + buffer.putLong(uuid.getLeastSignificantBits()); + nbtCompound.putByteArray(name, buffer.array()); + } + + public static Optional getUUID(NbtCompound nbtCompound, String name) { + Optional bytesOpt = nbtCompound.getByteArray(name); + if (bytesOpt.isPresent()) { + byte[] bytes = bytesOpt.get(); + if (bytes.length == 16) { + ByteBuffer buffer = ByteBuffer.wrap(bytes); + long mostSigBits = buffer.getLong(); + long leastSigBits = buffer.getLong(); + return Optional.of(new UUID(mostSigBits, leastSigBits)); + } + } else { + Optional intsOpt = nbtCompound.getIntArray(name); + if (intsOpt.isPresent() && intsOpt.get().length == 4) { + int[] ints = intsOpt.get(); + long mostSigBits = ((long) ints[0] << 32) | (ints[1] & 0xFFFFFFFFL); + long leastSigBits = ((long) ints[2] << 32) | (ints[3] & 0xFFFFFFFFL); + return Optional.of(new UUID(mostSigBits, leastSigBits)); + } + } + return Optional.empty(); + } + } diff --git a/src/main/java/systems/brn/servershop/lib/records/AuctionRecord.java b/src/main/java/systems/brn/servershop/lib/records/AuctionRecord.java index cae7b0f..0e94ab7 100644 --- a/src/main/java/systems/brn/servershop/lib/records/AuctionRecord.java +++ b/src/main/java/systems/brn/servershop/lib/records/AuctionRecord.java @@ -10,7 +10,7 @@ import net.minecraft.server.MinecraftServer; import java.util.Optional; import java.util.UUID; -import static systems.brn.servershop.lib.Util.getGameProfile; +import static systems.brn.servershop.lib.Util.*; public record AuctionRecord(double buyPrice, ItemStack stack, UUID sellerUUID) { @@ -18,7 +18,7 @@ public record AuctionRecord(double buyPrice, ItemStack stack, UUID sellerUUID) { public NbtCompound toNbt(RegistryWrapper.WrapperLookup wrapperLookup) { NbtCompound nbt = new NbtCompound(); nbt.putDouble("BuyPrice", this.buyPrice); - nbt.putUuid("SellerUUID", sellerUUID); + putUUID(nbt, "SellerUUID", sellerUUID); // Serialize the ItemStack to NBT and add it to the compound NbtElement stackNbt = stack.toNbt(wrapperLookup); @@ -32,15 +32,15 @@ public record AuctionRecord(double buyPrice, ItemStack stack, UUID sellerUUID) { } public static AuctionRecord fromNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup wrapperLookup) { - double buyPrice = nbt.getFloat("BuyPrice"); - UUID sellerUUID = nbt.getUuid("SellerUUID"); + double buyPrice = nbt.getFloat("BuyPrice", 0); + + Optional sellerUUID = getUUID(nbt, "SellerUUID"); // Deserialize the ItemStack from the NBT NbtElement stackElement = nbt.get("ItemStack"); Optional stack = ItemStack.fromNbt(wrapperLookup, stackElement); - return stack.map(itemStack -> new AuctionRecord(buyPrice, itemStack, sellerUUID)).orElse(null); - + return sellerUUID.flatMap(uuid -> stack.map(itemStack -> new AuctionRecord(buyPrice, itemStack, uuid))).orElse(null); } } diff --git a/src/main/java/systems/brn/servershop/lib/records/ItemPriceRecord.java b/src/main/java/systems/brn/servershop/lib/records/ItemPriceRecord.java index 9666f79..ae7ec6c 100644 --- a/src/main/java/systems/brn/servershop/lib/records/ItemPriceRecord.java +++ b/src/main/java/systems/brn/servershop/lib/records/ItemPriceRecord.java @@ -26,8 +26,8 @@ public record ItemPriceRecord(double buyPrice, double sellPrice, ItemStack stack } public static ItemPriceRecord fromNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup wrapperLookup) { - double buyPrice = nbt.getFloat("BuyPrice"); - double sellPrice = nbt.getFloat("SellPrice"); + double buyPrice = nbt.getFloat("BuyPrice", 0); + double sellPrice = nbt.getFloat("SellPrice", 0); if (sellPrice > buyPrice && buyPrice != 0) { buyPrice = sellPrice; diff --git a/src/main/java/systems/brn/servershop/lib/storages/AuctionStorage.java b/src/main/java/systems/brn/servershop/lib/storages/AuctionStorage.java index d6ab015..c28b8f9 100644 --- a/src/main/java/systems/brn/servershop/lib/storages/AuctionStorage.java +++ b/src/main/java/systems/brn/servershop/lib/storages/AuctionStorage.java @@ -119,7 +119,7 @@ public class AuctionStorage { try { NbtCompound nbtCompound = NbtIo.read(auctionStorageFIle.toPath()); if (nbtCompound != null && nbtCompound.contains("Auctions") && auctionStorageFIle.exists()) { - NbtList nbtList = nbtCompound.getList("Auctions", 10); // 10 is the type ID for NbtCompound + NbtList nbtList = nbtCompound.getListOrEmpty("Auctions"); // 10 is the type ID for NbtCompound for (NbtElement element : nbtList) { if (element instanceof NbtCompound nbt) { AuctionRecord auctionRecord = AuctionRecord.fromNbt(nbt, wrapperLookup); diff --git a/src/main/java/systems/brn/servershop/lib/storages/BalanceStorage.java b/src/main/java/systems/brn/servershop/lib/storages/BalanceStorage.java index d437d87..04094d3 100644 --- a/src/main/java/systems/brn/servershop/lib/storages/BalanceStorage.java +++ b/src/main/java/systems/brn/servershop/lib/storages/BalanceStorage.java @@ -7,11 +7,15 @@ import net.minecraft.text.Text; import net.minecraft.util.WorldSavePath; import java.io.*; +import java.util.Optional; import java.util.Scanner; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.locks.ReentrantLock; +import static systems.brn.servershop.lib.Util.getUUID; +import static systems.brn.servershop.lib.Util.putUUID; + public class BalanceStorage { public final ConcurrentHashMap balances = new ConcurrentHashMap<>(); public final MinecraftServer server; @@ -70,7 +74,7 @@ public class BalanceStorage { NbtList nbtList = new NbtList(); for (UUID uuid : balances.keySet()) { NbtCompound nbtCompound = new NbtCompound(); - nbtCompound.putUuid("UUID", uuid); + putUUID(nbtCompound, "UUID", uuid); nbtCompound.putDouble("Balance", balances.get(uuid)); nbtList.add(nbtCompound); } @@ -98,12 +102,14 @@ public class BalanceStorage { try { NbtCompound root = NbtIo.read(balanceStorageFile.toPath()); if (root != null) { - NbtList nbtList = root.getList("Balances", 10); + NbtList nbtList = root.getListOrEmpty("Balances"); for (NbtElement element : nbtList) { if (element instanceof NbtCompound nbt) { - UUID uuid = nbt.getUuid("UUID"); - double balance = nbt.getDouble("Balance"); - balances.put(uuid, balance); + Optional uuid = getUUID(nbt, "UUID"); + if (uuid.isPresent()) { + double balance = nbt.getDouble("Balance", 0); + balances.put(uuid.get(), balance); + } } } lock.unlock(); diff --git a/src/main/java/systems/brn/servershop/lib/storages/PriceStorage.java b/src/main/java/systems/brn/servershop/lib/storages/PriceStorage.java index 7043e15..77a8eaa 100644 --- a/src/main/java/systems/brn/servershop/lib/storages/PriceStorage.java +++ b/src/main/java/systems/brn/servershop/lib/storages/PriceStorage.java @@ -84,7 +84,7 @@ public class PriceStorage { lock.lock(); NbtCompound nbtCompound = NbtIo.read(priceStorageFile.toPath()); if (nbtCompound != null && nbtCompound.contains("Prices") && priceStorageFile.exists()) { - NbtList nbtList = nbtCompound.getList("Prices", 10); // 10 is the type ID for NbtCompound + NbtList nbtList = nbtCompound.getListOrEmpty("Prices"); for (NbtElement element : nbtList) { if (element instanceof NbtCompound nbt) { ItemPriceRecord itemPriceRecord = ItemPriceRecord.fromNbt(nbt, wrapperLookup); diff --git a/src/main/java/systems/brn/servershop/screens/AuctionCreateScreen.java b/src/main/java/systems/brn/servershop/screens/AuctionCreateScreen.java index 1b9c50a..54d54ec 100644 --- a/src/main/java/systems/brn/servershop/screens/AuctionCreateScreen.java +++ b/src/main/java/systems/brn/servershop/screens/AuctionCreateScreen.java @@ -19,7 +19,7 @@ public class AuctionCreateScreen extends AnvilInputGui { super(player, parentScreen != null && parentScreen.getLockPlayerInventory()); this.parentScreen = parentScreen; setTitle(Text.translatable("gui.servershop.auction.create.title")); - this.setDefaultInputValue("Price"); + this.setDefaultInputValue("0.0"); if (parentScreen != null) { parentScreen.close(); } @@ -38,9 +38,11 @@ public class AuctionCreateScreen extends AnvilInputGui { @Override public boolean insertItem(ItemStack stack, int startIndex, int endIndex, boolean fromLast) { - int price = Integer.parseInt(this.getInput()); - ServerShop.auctionStorage.addAuction(player, price, stack.copy(), false); - close(); + try { + int price = Integer.parseInt(this.getInput()); + ServerShop.auctionStorage.addAuction(player, price, stack.copy(), false); + close(); + } catch (NumberFormatException ignored) {} return super.insertItem(stack, startIndex, endIndex, fromLast); } diff --git a/src/main/resources/assets/servershop/lang/en_us.json b/src/main/resources/assets/servershop/lang/en_us.json index a7e5d77..f3cef06 100644 --- a/src/main/resources/assets/servershop/lang/en_us.json +++ b/src/main/resources/assets/servershop/lang/en_us.json @@ -31,8 +31,8 @@ "gui.servershop.shop.editor.item.sell_price_not_sellable": "Cannot be sold, edit with right click", "gui.servershop.shop.editor.filtering.buy.not_buyable.show": "Show unbuyable items", "gui.servershop.shop.editor.filtering.buy.not_buyable.hide": "Hide unbuyable items", - "gui.servershop.shop.editor.filtering.buy.disabled.show": "Hide disabled items for buying", - "gui.servershop.shop.editor.filtering.buy.disabled.hide": "Show disabled items for buying ", + "gui.servershop.shop.editor.filtering.buy.disabled.show": "Show disabled items for buying", + "gui.servershop.shop.editor.filtering.buy.disabled.hide": "Hide disabled items for buying ", "gui.servershop.shop.editor.filtering.buy.normal.show": "Show buyable items", "gui.servershop.shop.editor.filtering.buy.normal.hide": "Hide buyable items", "gui.servershop.shop.editor.filtering.sell.normal.show": "Show sellable items",