diff --git a/build.gradle b/build.gradle index c00aee1..1788e1f 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ plugins { - id 'fabric-loom' version '1.5-SNAPSHOT' + id 'fabric-loom' version "${loom_version}" id 'maven-publish' } @@ -26,8 +26,8 @@ dependencies { // Fabric API. This is technically optional, but you probably want it anyway. modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" - - // Uncomment the following line to enable the deprecated Fabric API modules. + + // Uncomment the following line to enable the deprecated Fabric API modules. // These are included in the Fabric API production distribution and allow you to update your mod to the latest modules at a later more convenient time. // modImplementation "net.fabricmc.fabric-api:fabric-api-deprecated:${project.fabric_version}" @@ -42,7 +42,7 @@ processResources { } tasks.withType(JavaCompile).configureEach { - it.options.release = 17 + it.options.release = 21 } java { @@ -51,8 +51,8 @@ java { // If you remove this line, sources will not be generated. withSourcesJar() - sourceCompatibility = JavaVersion.VERSION_17 - targetCompatibility = JavaVersion.VERSION_17 + sourceCompatibility = JavaVersion.VERSION_21 + targetCompatibility = JavaVersion.VERSION_21 } jar { @@ -76,4 +76,4 @@ publishing { // The repositories here will be used for publishing your artifact, not for // retrieving dependencies. } -} \ No newline at end of file +} diff --git a/gradle.properties b/gradle.properties index f8bc6f9..a4f4c21 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,9 +4,10 @@ org.gradle.parallel=true # Fabric Properties # check these on https://fabricmc.net/develop -minecraft_version=1.20.3 -yarn_mappings=1.20.3+build.1 -loader_version=0.15.9 +minecraft_version=1.21.10 +yarn_mappings=1.21.10+build.3 +loader_version=0.18.1 +loom_version=1.14-SNAPSHOT # Mod Properties mod_version=1.0.0 @@ -14,4 +15,4 @@ maven_group=walksy.customkits archives_base_name=ClientKits # Dependencies -fabric_version=0.91.1+1.20.3 +fabric_version=0.138.3+1.21.10 diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index a80b22c..23449a2 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.6-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-9.2.1-bin.zip networkTimeout=10000 validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME diff --git a/src/main/java/walksy/clientkits/manager/ConfigManager.java b/src/main/java/walksy/clientkits/manager/ConfigManager.java index c29caaf..ea5f158 100644 --- a/src/main/java/walksy/clientkits/manager/ConfigManager.java +++ b/src/main/java/walksy/clientkits/manager/ConfigManager.java @@ -32,7 +32,7 @@ public static void saveKitToFile(String kitName) { kitCompound.put(kitName, KitManager.kits.get(kitName)); - dataCompound.putInt("DataVersion", SharedConstants.getGameVersion().getSaveVersion().getId()); + dataCompound.putInt("DataVersion", SharedConstants.getGameVersion().dataVersion().id()); dataCompound.put("Kit", kitCompound); File newFile = new File(configDir, kitName + ".dat"); @@ -57,7 +57,7 @@ private static void loadKits() throws IOException { return; } - final int currentVersion = SharedConstants.getGameVersion().getSaveVersion().getId(); + final int currentVersion = SharedConstants.getGameVersion().dataVersion().id(); DataFixer dataFixer = MinecraftClient.getInstance().getDataFixer(); for (File file : configDir.listFiles()) { @@ -66,14 +66,14 @@ private static void loadKits() throws IOException { if (rootTag == null) { continue; } - final int fileVersion = rootTag.getInt("DataVersion"); + final int fileVersion = rootTag.getInt("DataVersion").orElse(-1); if (fileVersion < currentVersion) { rootTag = (NbtCompound) dataFixer.update(TypeReferences.STRUCTURE, new Dynamic<>(NbtOps.INSTANCE, rootTag), fileVersion, currentVersion).getValue(); } - NbtCompound compoundTag = rootTag.getCompound("Kit"); + NbtCompound compoundTag = rootTag.getCompound("Kit").get(); for (String key : compoundTag.getKeys()) { - KitManager.kits.put(key, compoundTag.getList(key, NbtElement.COMPOUND_TYPE)); + KitManager.kits.put(key, compoundTag.getCompound(key).get()); } } } diff --git a/src/main/java/walksy/clientkits/manager/KitCommandManager.java b/src/main/java/walksy/clientkits/manager/KitCommandManager.java index 79132ec..da9d0db 100644 --- a/src/main/java/walksy/clientkits/manager/KitCommandManager.java +++ b/src/main/java/walksy/clientkits/manager/KitCommandManager.java @@ -6,28 +6,37 @@ import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gl.RenderPipelines; +import net.minecraft.client.gui.Click; import net.minecraft.client.gui.DrawContext; -import net.minecraft.client.gui.screen.ingame.AbstractInventoryScreen; +import net.minecraft.client.gui.screen.ingame.HandledScreen; import net.minecraft.client.gui.screen.ingame.InventoryScreen; +import net.minecraft.client.network.ClientPlayerEntity; import net.minecraft.client.network.PlayerListEntry; import net.minecraft.command.CommandSource; +import net.minecraft.entity.EntityEquipment; +import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerInventory; +import net.minecraft.inventory.Inventory; +import net.minecraft.inventory.StackWithSlot; import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NbtList; +import net.minecraft.nbt.NbtCompound; import net.minecraft.screen.PlayerScreenHandler; import net.minecraft.screen.slot.Slot; +import net.minecraft.storage.NbtReadView; +import net.minecraft.storage.NbtWriteView; +import net.minecraft.storage.ReadView; +import net.minecraft.storage.WriteView; import net.minecraft.text.Text; +import net.minecraft.util.ErrorReporter; import net.minecraft.util.Formatting; import net.minecraft.world.GameMode; import walksy.clientkits.main.ClientKitsMod; -import java.io.File; -import java.io.FilenameFilter; import java.util.List; public class KitCommandManager { - public static boolean loadKit = false, shouldChangeBack = false; private static GameMode oldGM = null; private static String tempName = null; @@ -80,7 +89,12 @@ public KitCommandManager() { void handleSaveCommand(CommandContext source, String name) { - KitManager.kits.put(name, source.getSource().getPlayer().getInventory().writeNbt(new NbtList())); + NbtWriteView view = NbtWriteView.create(ErrorReporter.EMPTY, source.getSource().getRegistryManager()); + WriteView.ListAppender list = view.getListAppender(name, StackWithSlot.CODEC); + PlayerInventory inventory = source.getSource().getPlayer().getInventory(); + inventory.writeData(list); + writeEquipment(list, inventory); + KitManager.kits.put(name, view.getNbt()); ConfigManager.saveKitToFile(name); ClientKitsMod.debugMessage("§aSaved kit: " + name); } @@ -95,16 +109,36 @@ void handleDeleteCommand(String name) { } } + private static void writeEquipment(WriteView.ListAppender list, PlayerInventory inventory) { + for(Integer equipmentSlot : PlayerInventory.EQUIPMENT_SLOTS.keySet()) { + list.add(new StackWithSlot(equipmentSlot, inventory.getStack(equipmentSlot))); + } + } + + private static void readInventory(ReadView.TypedListReadView list, Inventory inventory) { + inventory.clear(); + + for(StackWithSlot slot : list) { + inventory.setStack(slot.slot(), slot.stack()); + } + } + + private static ReadView.TypedListReadView getKitTypedListView(PlayerEntity player, String name) { + return NbtReadView.create(ErrorReporter.EMPTY, player.getRegistryManager(), KitManager.kits.get(name)).getTypedListView(name, StackWithSlot.CODEC); + } + void handlePreviewCommand(String name) { if (KitManager.kits.get(name) != null) { - PlayerInventory tempInv = new PlayerInventory(MinecraftClient.getInstance().player); - tempInv.readNbt(KitManager.kits.get(name)); + ClientPlayerEntity player = MinecraftClient.getInstance().player; + PlayerInventory tempInv = new PlayerInventory(player, new EntityEquipment()); + readInventory(getKitTypedListView(player, name), tempInv); MinecraftClient.getInstance().send(() -> MinecraftClient.getInstance().setScreen(new PreviewScreen(new PlayerScreenHandler(tempInv, true, MinecraftClient.getInstance().player), tempInv, name))); } else { ClientKitsMod.debugMessage("§cCannot find the kit '" + name + "' to preview."); } } + public static void tick() { if (loadKit) { if (!tempSource.getSource().getPlayer().getAbilities().creativeMode) { @@ -121,23 +155,26 @@ public static void tick() { } } if (!tempSource.getSource().getPlayer().getAbilities().creativeMode) return; - NbtList kit = KitManager.kits.get(tempName); + NbtCompound kit = KitManager.kits.get(tempName); if (kit == null) { ClientKitsMod.debugMessage("§cKit not found."); reset(); return; } - PlayerInventory tempInv = new PlayerInventory(tempSource.getSource().getPlayer()); - tempInv.readNbt(kit); + PlayerEntity player = tempSource.getSource().getPlayer(); + PlayerInventory tempInv = new PlayerInventory(player, new EntityEquipment()); + readInventory(getKitTypedListView(player, tempName), tempInv); List slots = tempSource.getSource().getPlayer().playerScreenHandler.slots; for (int i = 0; i < slots.size(); i++) { if (slots.get(i).inventory == tempSource.getSource().getPlayer().getInventory()) { ItemStack existingItemStack = tempSource.getSource().getPlayer().getInventory().getStack(slots.get(i).getIndex()); if (!existingItemStack.isEmpty()) { + player.playerScreenHandler.getSlot(i).setStackNoCallbacks(ItemStack.EMPTY); tempSource.getSource().getClient().interactionManager.clickCreativeStack(ItemStack.EMPTY, i); //clear out old items } ItemStack itemStack = tempInv.getStack(slots.get(i).getIndex()); if (!itemStack.isEmpty()) { + player.playerScreenHandler.getSlot(i).setStack(itemStack); tempSource.getSource().getClient().interactionManager.clickCreativeStack(itemStack, i); } } @@ -163,8 +200,7 @@ public static void tick() { } } - static void reset() - { + static void reset() { loadKit = false; shouldChangeBack = false; tempName = null; @@ -174,8 +210,7 @@ static void reset() } - class PreviewScreen extends AbstractInventoryScreen { - + static class PreviewScreen extends HandledScreen { public PreviewScreen(PlayerScreenHandler playerScreenHandler, PlayerInventory inventory, String name) { super(playerScreenHandler, inventory, Text.literal(name).styled(style -> style.withColor(Formatting.BOLD))); this.titleX = 80; @@ -197,12 +232,12 @@ public void render(DrawContext context, int mouseX, int mouseY, float delta) { protected void drawBackground(DrawContext context, float delta, int mouseX, int mouseY) { int i = this.x; int j = this.y; - context.drawTexture(BACKGROUND_TEXTURE, this.x, this.y, 0, 0, this.backgroundWidth, this.backgroundHeight); + context.drawTexture(RenderPipelines.GUI_TEXTURED, BACKGROUND_TEXTURE, this.x, this.y, 0, 0, this.backgroundWidth, this.backgroundHeight, 256, 256); InventoryScreen.drawEntity(context, i + 26, j + 8, i + 75, j + 78, 30, 0.0625F, mouseX, mouseY, this.client.player); } @Override - public boolean mouseClicked(double mouseX, double mouseY, int button) { + public boolean mouseClicked(Click click, boolean doubled) { return false; } } diff --git a/src/main/java/walksy/clientkits/manager/KitManager.java b/src/main/java/walksy/clientkits/manager/KitManager.java index 4d327a2..f7b5560 100644 --- a/src/main/java/walksy/clientkits/manager/KitManager.java +++ b/src/main/java/walksy/clientkits/manager/KitManager.java @@ -1,11 +1,11 @@ package walksy.clientkits.manager; -import net.minecraft.nbt.NbtList; +import net.minecraft.nbt.NbtCompound; import java.util.HashMap; import java.util.Map; public class KitManager { - public static final Map kits = new HashMap<>(); + public static final Map kits = new HashMap<>(); } diff --git a/src/main/resources/clientkits.mixins.json b/src/main/resources/clientkits.mixins.json index ca12fc3..290d2d1 100644 --- a/src/main/resources/clientkits.mixins.json +++ b/src/main/resources/clientkits.mixins.json @@ -1,7 +1,7 @@ { "required": true, "package": "walksy.clientkits.mixin", - "compatibilityLevel": "JAVA_17", + "compatibilityLevel": "JAVA_21", "client": [ "ClientPlayerEntityMixin", "ChatHudMixin", diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 82385c6..2a9254a 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -27,7 +27,7 @@ "depends": { "fabricloader": "*", "minecraft": "*", - "java": ">=17", + "java": ">=21", "fabric-api": "*" } }