Skip to content

Commit

Permalink
Completely redone the stack modifying:
Browse files Browse the repository at this point in the history
It now runs only for all items you want to change (instead of all registered items)
You can now use tags to modify the stack size of all items with that tag, includes convention tags (#c:<tag_name>)
You no longer need to restart the game after making a change to the config, simply pressing the done button will make changes take effect
  • Loading branch information
MrGII committed Jul 29, 2024
1 parent ec808a1 commit 2d7c68e
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 19 deletions.
12 changes: 10 additions & 2 deletions src/main/java/github/mrgii/itemsstack/ItemsStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,23 @@
import github.mrgii.itemsstack.util.ItemsStackSizeModifier;
import net.fabricmc.api.ModInitializer;

import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.tag.convention.v2.ConventionalItemTags;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ItemsStack implements ModInitializer {
public static final Logger LOGGER = LoggerFactory.getLogger("potionsstack");
public static final Logger LOGGER = LoggerFactory.getLogger("itemsstack");
public static final ItemsStackSizeConfig CONFIG = ItemsStackSizeConfig.createAndLoad();

@Override
public void onInitialize() {
ItemsStackSizeModifier.ModifyItemStackSize(CONFIG.items(), CONFIG.maxStackSizes());
String tagName = ConventionalItemTags.TOOLS.getName().getString();
LOGGER.info(tagName);
ServerLifecycleEvents.SERVER_STARTED.register((minecraftServer) -> {
ItemsStackSizeModifier.updateValuesAndModifyStackSizes();
ItemsStackSizeModifier.registerCallbacks();
});

}
}
9 changes: 9 additions & 0 deletions src/main/java/github/mrgii/itemsstack/ItemsStackClient.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package github.mrgii.itemsstack;

import github.mrgii.itemsstack.util.ItemsStackSizeModifier;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;

public class ItemsStackClient implements ClientModInitializer {
@Override
public void onInitializeClient() {
ClientPlayConnectionEvents.JOIN.register((clientPlayNetworkHandler, packetSender, client) -> {
ItemsStackSizeModifier.updateValuesAndModifyStackSizes();
});
ClientLifecycleEvents.CLIENT_STARTED.register((client) -> {
ItemsStackSizeModifier.registerCallbacks();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
@Modmenu(modId = "itemsstack")
@Config(name = "items-stack-size-config", wrapperName = "ItemsStackSizeConfig")
public class ItemsStackSizeConfigModel {
@RestartRequired
@Sync(SyncMode.OVERRIDE_CLIENT)
public List<String> items = List.of();

@RestartRequired
@Sync(SyncMode.OVERRIDE_CLIENT)
public List<Integer> maxStackSizes = List.of();

@Sync(SyncMode.OVERRIDE_CLIENT)
public List<String> itemTags = List.of();

@Sync(SyncMode.OVERRIDE_CLIENT)
public List<Integer> maxTagStackSizes = List.of();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
import net.minecraft.entity.ItemEntity;
import net.minecraft.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

Expand Down
100 changes: 89 additions & 11 deletions src/main/java/github/mrgii/itemsstack/util/ItemsStackSizeModifier.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,110 @@
package github.mrgii.itemsstack.util;

import github.mrgii.itemsstack.ItemsStack;
import github.mrgii.itemsstack.config.ItemsStackSizeConfig.Keys;
import io.wispforest.owo.config.Option;
import net.fabricmc.fabric.api.tag.convention.v2.ConventionalItemTags;
import net.fabricmc.fabric.mixin.item.ItemAccessor;
import net.minecraft.component.ComponentMap;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.item.Item;
import net.minecraft.registry.Registries;
import net.minecraft.registry.tag.ItemTags;
import net.minecraft.registry.tag.TagKey;
import net.minecraft.util.Identifier;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

public class ItemsStackSizeModifier {
public static void ModifyItemStackSize(List<String> itemNames, List<Integer> maxStackSizes) {
Map<String, Integer> itemToMaxStackSize = createItemToStackSizeMap(itemNames, maxStackSizes);
ItemsStack.LOGGER.info("Changing stack size of minecraft potions.");
for (Item item : Registries.ITEM) {
String name = item.getName().getString();
if (itemToMaxStackSize.containsKey(name)) {
((ItemAccessor) item).setComponents(ComponentMap.of(item.getComponents(), ComponentMap.builder().add(DataComponentTypes.MAX_STACK_SIZE, itemToMaxStackSize.get(name)).build()));
}
public static List<String> itemNames;
public static List<Integer> maxStackSizes;
public static List<String> itemTagNames;
public static List<Integer> maxTagStackSizes;

public static void ModifyItemStackSizes() {
Map<Item, Integer> itemToMaxStackSize = createItemToStackSizeMap(itemNames, maxStackSizes);
Map<TagKey<Item>, Integer> itemTagToMaxStackSize = createItemTagToStackSizeMap(itemTagNames, maxTagStackSizes);
ItemsStack.LOGGER.info("Changing stack size of minecraft item tags.");
for (TagKey<Item> tagKey : itemTagToMaxStackSize.keySet()) {
Registries.ITEM.iterateEntries(tagKey).forEach((itemRegistryEntry) -> {
Item item = itemRegistryEntry.value();
((ItemAccessor) item).setComponents(ComponentMap.of(item.getComponents(), ComponentMap.builder().add(DataComponentTypes.MAX_STACK_SIZE, itemTagToMaxStackSize.get(tagKey)).build()));
});
}
ItemsStack.LOGGER.info("Changing stack size of minecraft items.");
for (Item item : itemToMaxStackSize.keySet()) {
((ItemAccessor) item).setComponents(ComponentMap.of(item.getComponents(), ComponentMap.builder().add(DataComponentTypes.MAX_STACK_SIZE, itemToMaxStackSize.get(item)).build()));
}
}

private static Map<String, Integer> createItemToStackSizeMap(List<String> itemNames, List<Integer> maxStackSizes) {
Map<String, Integer> itemToMaxStackSize = new java.util.HashMap<>(Map.of());
private static Map<Item, Integer> createItemToStackSizeMap(List<String> itemNames, List<Integer> maxStackSizes) {
Map<Item, Integer> itemToMaxStackSize = new java.util.HashMap<>(Map.of());
for (int i = 0; i < Math.min(itemNames.size(), maxStackSizes.size()); i++) {
itemToMaxStackSize.put(itemNames.get(i), maxStackSizes.get(i));
itemToMaxStackSize.put(Registries.ITEM.get(Identifier.of(itemNames.get(i))), maxStackSizes.get(i));
}
return itemToMaxStackSize;
}

private static Map<TagKey<Item>, Integer> createItemTagToStackSizeMap(List<String> itemTagNames, List<Integer> maxStackSizes) {
Map<String, Integer> itemTagToMaxStackSize = new java.util.HashMap<>(Map.of());
for (int i = 0; i < Math.min(itemTagNames.size(), maxStackSizes.size()); i++) {
itemTagToMaxStackSize.put(itemTagNames.get(i), maxStackSizes.get(i));
}
Map<TagKey<Item>, Integer> itemTagKeyToMaxStackSize = new java.util.HashMap<>(Map.of());
Field[] conTagKeyFields = ConventionalItemTags.class.getDeclaredFields();
for (Field conTagKeyField : conTagKeyFields) {
conTagKeyField.setAccessible(true);
TagKey<Item> conTagKey;
try {
conTagKey = (TagKey<Item>) conTagKeyField.get(null);
} catch (IllegalAccessException e) {
ItemsStack.LOGGER.error("Could not access tagKeyField {}!", conTagKeyField.getName());
throw new RuntimeException(e);
}
String conTagName = "#" + conTagKey.id().toString();
if (itemTagToMaxStackSize.containsKey(conTagName)) {
itemTagKeyToMaxStackSize.put(conTagKey, itemTagToMaxStackSize.get(conTagName));
}
}
Field[] tagKeyFields = ItemTags.class.getDeclaredFields();
for (Field tagKeyField : tagKeyFields) {
tagKeyField.setAccessible(true);
TagKey<Item> tagKey;
try {
tagKey = (TagKey<Item>) tagKeyField.get(null);
} catch (IllegalAccessException e) {
ItemsStack.LOGGER.error("Could not access tagKeyField {}!", tagKeyField.getName());
throw new RuntimeException(e);
}
String tagName = tagKey.getName().getString();
if (itemTagToMaxStackSize.containsKey(tagName)) {
itemTagKeyToMaxStackSize.put(tagKey, itemTagToMaxStackSize.get(tagName));
}
}
return itemTagKeyToMaxStackSize;
}

public static void updateValuesAndModifyStackSizes() {
ItemsStackSizeModifier.itemNames = ItemsStack.CONFIG.items();
ItemsStackSizeModifier.maxStackSizes = ItemsStack.CONFIG.maxStackSizes();
ItemsStackSizeModifier.itemTagNames = ItemsStack.CONFIG.itemTags();
ItemsStackSizeModifier.maxTagStackSizes = ItemsStack.CONFIG.maxTagStackSizes();

ModifyItemStackSizes();
}

public static void registerCallbacks() {
Keys keys = ItemsStack.CONFIG.keys;
Option<List<String>> itemsOption = ItemsStack.CONFIG.optionForKey(keys.items);
Option<List<String>> maxStackSizesOption = ItemsStack.CONFIG.optionForKey(keys.maxStackSizes);
Option<List<String>> itemTagsOption = ItemsStack.CONFIG.optionForKey(keys.itemTags);
Option<List<String>> maxTagStackSizesOption = ItemsStack.CONFIG.optionForKey(keys.maxTagStackSizes);

itemsOption.observe(newValue -> ItemsStackSizeModifier.updateValuesAndModifyStackSizes());
maxStackSizesOption.observe(newValue -> ItemsStackSizeModifier.updateValuesAndModifyStackSizes());
itemTagsOption.observe(newValue -> ItemsStackSizeModifier.updateValuesAndModifyStackSizes());
maxTagStackSizesOption.observe(newValue -> ItemsStackSizeModifier.updateValuesAndModifyStackSizes());
}
}
10 changes: 7 additions & 3 deletions src/main/resources/assets/itemsstack/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
"text.config.items-stack-size-config.title": "Items Stack Config",

"text.config.items-stack-size-config.option.items": "Items",
"text.config.items-stack-size-config.option.items.tooltip": "The Names of Items to change the Max Stack Size of.\nThis is the Item's Id, without the 'minecraft:' (or '<modname>:' for mod items),\nevery _ is replaced with a space and the first letter of each word is capitalized.\nEach Item needs to have a matching entry in Max Stack Sizes.",

"text.config.items-stack-size-config.option.items.tooltip": "The ids of Items to change the Max Stack Size of.\nEach Item needs to have a matching entry in Max Stack Sizes.",
"text.config.items-stack-size-config.option.maxStackSizes": "Max Stack Sizes",
"text.config.items-stack-size-config.option.maxStackSizes.tooltip": "The Max Stack Sizes of each Item in Items.\nThis should be a positive integer.\nThe Max Stack Size is applied to the Item in the same place in Items."
"text.config.items-stack-size-config.option.maxStackSizes.tooltip": "The Max Stack Sizes of each Item in Items.\nThis should be a positive integer.\nThe Max Stack Size is applied to the Item at the same place in Items.",

"text.config.items-stack-size-config.option.itemTags": "Item Tags",
"text.config.items-stack-size-config.option.itemTags.tooltip": "The ids of Item Tags to change the Max Stack Size of.\nEach Item Tag needs to have a matching entry in Max Tag Stack Sizes.\nItems that are both part of an Item Tag in this list and are in the Items list\nwill get the value assigned to them from the Items list",
"text.config.items-stack-size-config.option.maxTagStackSizes": "Max Tag Stack Sizes",
"text.config.items-stack-size-config.option.maxTagStackSizes.tooltip": "The Max Stack Sizes of each Item Tag in Item Tags.\nThis should be a positive integer.\nThe Max Stack Size is applied to the Item Tag at the same place in Item Tags."
}

0 comments on commit 2d7c68e

Please sign in to comment.