Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'fabric-loom' version '1.5-SNAPSHOT'
id 'fabric-loom' version "${loom_version}"
id 'maven-publish'
}

Expand All @@ -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}"
Expand All @@ -42,7 +42,7 @@ processResources {
}

tasks.withType(JavaCompile).configureEach {
it.options.release = 17
it.options.release = 21
}

java {
Expand All @@ -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 {
Expand All @@ -76,4 +76,4 @@ publishing {
// The repositories here will be used for publishing your artifact, not for
// retrieving dependencies.
}
}
}
9 changes: 5 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ 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
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
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/walksy/clientkits/manager/ConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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()) {
Expand All @@ -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());
}
}
}
Expand Down
69 changes: 52 additions & 17 deletions src/main/java/walksy/clientkits/manager/KitCommandManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -80,7 +89,12 @@ public KitCommandManager() {


void handleSaveCommand(CommandContext<FabricClientCommandSource> 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<StackWithSlot> 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);
}
Expand All @@ -95,16 +109,36 @@ void handleDeleteCommand(String name) {
}
}

private static void writeEquipment(WriteView.ListAppender<StackWithSlot> list, PlayerInventory inventory) {
for(Integer equipmentSlot : PlayerInventory.EQUIPMENT_SLOTS.keySet()) {
list.add(new StackWithSlot(equipmentSlot, inventory.getStack(equipmentSlot)));
}
}

private static void readInventory(ReadView.TypedListReadView<StackWithSlot> list, Inventory inventory) {
inventory.clear();

for(StackWithSlot slot : list) {
inventory.setStack(slot.slot(), slot.stack());
}
}

private static ReadView.TypedListReadView<StackWithSlot> 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) {
Expand All @@ -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<Slot> 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);
}
}
Expand All @@ -163,8 +200,7 @@ public static void tick() {
}
}

static void reset()
{
static void reset() {
loadKit = false;
shouldChangeBack = false;
tempName = null;
Expand All @@ -174,8 +210,7 @@ static void reset()
}


class PreviewScreen extends AbstractInventoryScreen<PlayerScreenHandler> {

static class PreviewScreen extends HandledScreen<PlayerScreenHandler> {
public PreviewScreen(PlayerScreenHandler playerScreenHandler, PlayerInventory inventory, String name) {
super(playerScreenHandler, inventory, Text.literal(name).styled(style -> style.withColor(Formatting.BOLD)));
this.titleX = 80;
Expand All @@ -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;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/walksy/clientkits/manager/KitManager.java
Original file line number Diff line number Diff line change
@@ -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<String, NbtList> kits = new HashMap<>();
public static final Map<String, NbtCompound> kits = new HashMap<>();
}
2 changes: 1 addition & 1 deletion src/main/resources/clientkits.mixins.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"required": true,
"package": "walksy.clientkits.mixin",
"compatibilityLevel": "JAVA_17",
"compatibilityLevel": "JAVA_21",
"client": [
"ClientPlayerEntityMixin",
"ChatHudMixin",
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"depends": {
"fabricloader": "*",
"minecraft": "*",
"java": ">=17",
"java": ">=21",
"fabric-api": "*"
}
}