From 7dc892bdf1c50738d553c80c6ad6a44605cdf298 Mon Sep 17 00:00:00 2001 From: UselessBullets <80850784+UselessBullets@users.noreply.github.com> Date: Sat, 17 Feb 2024 18:23:47 -0600 Subject: [PATCH 1/4] priority system in item ordering --- .../java/turniplabs/halplibe/HalpLibe.java | 21 +++++ .../halplibe/helper/CreativeHelper.java | 17 +++++ .../mixins/ContainerPlayerCreativeMixin.java | 76 +++++++++++++++++++ .../halplibe/util/CreativeEntry.java | 29 +++++++ src/main/resources/halplibe.mixins.json | 9 ++- 5 files changed, 148 insertions(+), 4 deletions(-) create mode 100644 src/main/java/turniplabs/halplibe/helper/CreativeHelper.java create mode 100644 src/main/java/turniplabs/halplibe/mixin/mixins/ContainerPlayerCreativeMixin.java create mode 100644 src/main/java/turniplabs/halplibe/util/CreativeEntry.java diff --git a/src/main/java/turniplabs/halplibe/HalpLibe.java b/src/main/java/turniplabs/halplibe/HalpLibe.java index 24b99ac..c42eb62 100644 --- a/src/main/java/turniplabs/halplibe/HalpLibe.java +++ b/src/main/java/turniplabs/halplibe/HalpLibe.java @@ -10,6 +10,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import turniplabs.halplibe.helper.AchievementHelper; +import turniplabs.halplibe.helper.CreativeHelper; import turniplabs.halplibe.helper.ModVersionHelper; import turniplabs.halplibe.helper.NetworkHelper; import turniplabs.halplibe.util.TomlConfigHandler; @@ -85,6 +86,26 @@ public void onInitialize() { AchievementHelper.addPage(VANILLA_ACHIEVEMENTS); NetworkHelper.register(PacketModList.class, false, true); LOGGER.info("HalpLibe initialized."); + + CreativeHelper.setPriority(Block.planksOak, 990); + for (int i = 0; i < 16; i++) { + CreativeHelper.setPriority(Block.planksOakPainted, i, 990); + } + + CreativeHelper.setPriority(Block.brickBasalt, 999); + CreativeHelper.setPriority(Block.brickClay, 999); + CreativeHelper.setPriority(Block.brickGold, 999); + CreativeHelper.setPriority(Block.brickGranite, 999); + CreativeHelper.setPriority(Block.brickIron, 999); + CreativeHelper.setPriority(Block.brickLimestone, 999); + + CreativeHelper.setPriority(Item.ingotIron, 999); + CreativeHelper.setPriority(Item.ingotGold, 999); + CreativeHelper.setPriority(Item.ingotSteel, 999); + CreativeHelper.setPriority(Item.ingotSteelCrude, 999); + CreativeHelper.setPriority(Item.dustRedstone, 998); + CreativeHelper.setPriority(Item.dustGlowstone, 998); + CreativeHelper.setPriority(Item.dustSugar, 998); } @Override diff --git a/src/main/java/turniplabs/halplibe/helper/CreativeHelper.java b/src/main/java/turniplabs/halplibe/helper/CreativeHelper.java new file mode 100644 index 0000000..680cba2 --- /dev/null +++ b/src/main/java/turniplabs/halplibe/helper/CreativeHelper.java @@ -0,0 +1,17 @@ +package turniplabs.halplibe.helper; + +import net.minecraft.core.item.IItemConvertible; +import net.minecraft.core.item.ItemStack; +import turniplabs.halplibe.util.CreativeEntry; + +public class CreativeHelper { + public static void setPriority(IItemConvertible item, int priority){ + setPriority(item.getDefaultStack(), priority); + } + public static void setPriority(IItemConvertible item, int meta, int priority){ + setPriority(new ItemStack(item, 1, meta), priority); + } + public static void setPriority(ItemStack item, int priority){ + CreativeEntry.addEntry(new CreativeEntry(item, priority)); + } +} diff --git a/src/main/java/turniplabs/halplibe/mixin/mixins/ContainerPlayerCreativeMixin.java b/src/main/java/turniplabs/halplibe/mixin/mixins/ContainerPlayerCreativeMixin.java new file mode 100644 index 0000000..90f37e4 --- /dev/null +++ b/src/main/java/turniplabs/halplibe/mixin/mixins/ContainerPlayerCreativeMixin.java @@ -0,0 +1,76 @@ +package turniplabs.halplibe.mixin.mixins; + +import net.minecraft.core.block.Block; +import net.minecraft.core.item.ItemStack; +import net.minecraft.core.player.inventory.ContainerPlayerCreative; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import turniplabs.halplibe.util.CreativeEntry; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +@Mixin(value = ContainerPlayerCreative.class, remap = false, priority = 900) +public class ContainerPlayerCreativeMixin { + @Shadow public static int creativeItemsCount; + + @Shadow public static List creativeItems; + + @Inject(method = "", at = @At("TAIL")) + private static void test(CallbackInfo ci){ + List trimmedList = new ArrayList<>(creativeItems); + List newList = new ArrayList<>(); + for (ItemStack stack : creativeItems){ + if (CreativeEntry.entryMap.containsKey(stack.toString())){ + trimmedList.remove(stack); + } + } + + List trimmedBlocks = new ArrayList<>(); + List trimmedItems = new ArrayList<>(); + + for (ItemStack stack : trimmedList){ + if (stack.itemID < Block.blocksList.length){ + trimmedBlocks.add(stack); + } else { + trimmedItems.add(stack); + } + } + + List entries = CreativeEntry.entryMap.values().stream().sorted().collect(Collectors.toList()); + boolean hasAddedVanillaBlocks = false; + for (CreativeEntry entry : entries){ + if (entry.itemStack.itemID < Block.blocksList.length){ + if (entry.priority > 1000 && !hasAddedVanillaBlocks){ + hasAddedVanillaBlocks = true; + newList.addAll(trimmedBlocks); + } + newList.add(entry.itemStack); + } + } + if (!hasAddedVanillaBlocks){ + newList.addAll(trimmedBlocks); + } + + boolean hasAddedVanillaItems = false; + for (CreativeEntry entry : entries){ + if (entry.itemStack.itemID >= Block.blocksList.length){ + if (entry.priority > 1000 && !hasAddedVanillaItems){ + hasAddedVanillaItems = true; + newList.addAll(trimmedItems); + } + newList.add(entry.itemStack); + } + } + if (!hasAddedVanillaItems){ + newList.addAll(trimmedItems); + } + + creativeItems = newList; + creativeItemsCount = creativeItems.size(); + } +} diff --git a/src/main/java/turniplabs/halplibe/util/CreativeEntry.java b/src/main/java/turniplabs/halplibe/util/CreativeEntry.java new file mode 100644 index 0000000..481beff --- /dev/null +++ b/src/main/java/turniplabs/halplibe/util/CreativeEntry.java @@ -0,0 +1,29 @@ +package turniplabs.halplibe.util; + +import net.minecraft.core.item.ItemStack; +import org.jetbrains.annotations.NotNull; + +import java.util.HashMap; + +public class CreativeEntry implements Comparable { + public static final HashMap entryMap = new HashMap<>(); + public static void addEntry(CreativeEntry entry){ + entryMap.put(entry.itemStack.toString(), entry); + } + public int priority; + public final ItemStack itemStack; + public CreativeEntry(ItemStack stack){ + this(stack, 1000); + } + public CreativeEntry(ItemStack stack, int priority){ + this.itemStack = stack.copy(); + this.itemStack.stackSize = 1; + this.priority = priority; + addEntry(this); + } + + @Override + public int compareTo(@NotNull CreativeEntry o) { + return priority - o.priority; + } +} diff --git a/src/main/resources/halplibe.mixins.json b/src/main/resources/halplibe.mixins.json index 0e6da53..75e64a9 100644 --- a/src/main/resources/halplibe.mixins.json +++ b/src/main/resources/halplibe.mixins.json @@ -14,6 +14,7 @@ "mixins.BlockFireMixin", "mixins.BlockMixin", "mixins.BlockSoundDispatcherMixin", + "mixins.ContainerPlayerCreativeMixin", "mixins.I18nMixin", "mixins.ItemStackJsonAdapterMixin", "mixins.RegistryMixin", @@ -34,17 +35,17 @@ "mixins.PlayerRendererMixin", "mixins.RenderEngineMixin", "mixins.RenderGlobalMixin", + "mixins.commands.CommandsClientMixin", "mixins.network.MinecraftMixin", "mixins.registry.MinecraftMixin", - "mixins.version.NetClientHandlerMixin", - "mixins.commands.CommandsClientMixin" + "mixins.version.NetClientHandlerMixin" ], "server": [ "mixins.MinecraftServerMixin", + "mixins.commands.CommandsServerMixin", "mixins.network.MinecraftServerMixin", "mixins.registry.MinecraftServerMixin", - "mixins.version.NetLoginHandlerMixin", - "mixins.commands.CommandsServerMixin" + "mixins.version.NetLoginHandlerMixin" ], "injectors": { "defaultRequire": 1 From 2b446647eaa61e834da137b7266f5814862f8db5 Mon Sep 17 00:00:00 2001 From: UselessBullets <80850784+UselessBullets@users.noreply.github.com> Date: Sat, 17 Feb 2024 19:18:03 -0600 Subject: [PATCH 2/4] added ChildEntries --- .../java/turniplabs/halplibe/HalpLibe.java | 4 +++ .../halplibe/helper/CreativeHelper.java | 9 ++++++ .../mixins/ContainerPlayerCreativeMixin.java | 32 +++++++++++++++---- .../halplibe/util/CreativeEntry.java | 26 +++++++++++---- 4 files changed, 59 insertions(+), 12 deletions(-) diff --git a/src/main/java/turniplabs/halplibe/HalpLibe.java b/src/main/java/turniplabs/halplibe/HalpLibe.java index c42eb62..bd63329 100644 --- a/src/main/java/turniplabs/halplibe/HalpLibe.java +++ b/src/main/java/turniplabs/halplibe/HalpLibe.java @@ -106,6 +106,10 @@ public void onInitialize() { CreativeHelper.setPriority(Item.dustRedstone, 998); CreativeHelper.setPriority(Item.dustGlowstone, 998); CreativeHelper.setPriority(Item.dustSugar, 998); + + CreativeHelper.setParent(Item.seat, Item.toolHoeSteel); + CreativeHelper.setParent(Item.basket, Item.seat); + CreativeHelper.setParent(Item.armorChestplateDiamond, Item.basket); } @Override diff --git a/src/main/java/turniplabs/halplibe/helper/CreativeHelper.java b/src/main/java/turniplabs/halplibe/helper/CreativeHelper.java index 680cba2..ff82723 100644 --- a/src/main/java/turniplabs/halplibe/helper/CreativeHelper.java +++ b/src/main/java/turniplabs/halplibe/helper/CreativeHelper.java @@ -14,4 +14,13 @@ public static void setPriority(IItemConvertible item, int meta, int priority){ public static void setPriority(ItemStack item, int priority){ CreativeEntry.addEntry(new CreativeEntry(item, priority)); } + public static void setParent(IItemConvertible itemToAdd, IItemConvertible itemParent){ + setParent(itemToAdd.getDefaultStack(), itemParent.getDefaultStack()); + } + public static void setParent(IItemConvertible itemToAdd, int metaToAdd, IItemConvertible itemParent, int metaParent){ + setParent(new ItemStack(itemToAdd, 1, metaToAdd), new ItemStack(itemParent, metaParent)); + } + public static void setParent(ItemStack itemToAdd, ItemStack parentStack){ + CreativeEntry.addEntry(new CreativeEntry(itemToAdd, parentStack)); + } } diff --git a/src/main/java/turniplabs/halplibe/mixin/mixins/ContainerPlayerCreativeMixin.java b/src/main/java/turniplabs/halplibe/mixin/mixins/ContainerPlayerCreativeMixin.java index 90f37e4..6302fc3 100644 --- a/src/main/java/turniplabs/halplibe/mixin/mixins/ContainerPlayerCreativeMixin.java +++ b/src/main/java/turniplabs/halplibe/mixin/mixins/ContainerPlayerCreativeMixin.java @@ -1,5 +1,6 @@ package turniplabs.halplibe.mixin.mixins; +import com.google.common.collect.Lists; import net.minecraft.core.block.Block; import net.minecraft.core.item.ItemStack; import net.minecraft.core.player.inventory.ContainerPlayerCreative; @@ -8,9 +9,11 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import turniplabs.halplibe.HalpLibe; import turniplabs.halplibe.util.CreativeEntry; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.stream.Collectors; @@ -25,7 +28,7 @@ private static void test(CallbackInfo ci){ List trimmedList = new ArrayList<>(creativeItems); List newList = new ArrayList<>(); for (ItemStack stack : creativeItems){ - if (CreativeEntry.entryMap.containsKey(stack.toString())){ + if (CreativeEntry.priorityEntryMap.containsKey(stack.toString()) || CreativeEntry.childEntryMap.containsKey(stack.toString())){ trimmedList.remove(stack); } } @@ -41,15 +44,15 @@ private static void test(CallbackInfo ci){ } } - List entries = CreativeEntry.entryMap.values().stream().sorted().collect(Collectors.toList()); + List entries = CreativeEntry.priorityEntryMap.values().stream().sorted().collect(Collectors.toList()); boolean hasAddedVanillaBlocks = false; for (CreativeEntry entry : entries){ - if (entry.itemStack.itemID < Block.blocksList.length){ + if (entry.stackToAdd.itemID < Block.blocksList.length){ if (entry.priority > 1000 && !hasAddedVanillaBlocks){ hasAddedVanillaBlocks = true; newList.addAll(trimmedBlocks); } - newList.add(entry.itemStack); + newList.add(entry.stackToAdd); } } if (!hasAddedVanillaBlocks){ @@ -58,18 +61,35 @@ private static void test(CallbackInfo ci){ boolean hasAddedVanillaItems = false; for (CreativeEntry entry : entries){ - if (entry.itemStack.itemID >= Block.blocksList.length){ + if (entry.stackToAdd.itemID >= Block.blocksList.length){ if (entry.priority > 1000 && !hasAddedVanillaItems){ hasAddedVanillaItems = true; newList.addAll(trimmedItems); } - newList.add(entry.itemStack); + newList.add(entry.stackToAdd); } } if (!hasAddedVanillaItems){ newList.addAll(trimmedItems); } + List entriesChild = Lists.reverse(new ArrayList<>(CreativeEntry.childEntryMap.values())); + for (CreativeEntry entry : entriesChild){ + int index = -1; + for (ItemStack stack : newList){ + if (stack.toString().equals(entry.parentStack.toString())){ + index = newList.indexOf(stack); + break; + } + } + if (index != -1){ + newList.add(index + 1, entry.stackToAdd); + } else { + newList.add(entry.stackToAdd); + HalpLibe.LOGGER.warn("Could not find parent stack of '" + entry.parentStack + "' in the list! does it exist? adding stack to end of list!"); + } + } + creativeItems = newList; creativeItemsCount = creativeItems.size(); } diff --git a/src/main/java/turniplabs/halplibe/util/CreativeEntry.java b/src/main/java/turniplabs/halplibe/util/CreativeEntry.java index 481beff..0aa9138 100644 --- a/src/main/java/turniplabs/halplibe/util/CreativeEntry.java +++ b/src/main/java/turniplabs/halplibe/util/CreativeEntry.java @@ -6,20 +6,34 @@ import java.util.HashMap; public class CreativeEntry implements Comparable { - public static final HashMap entryMap = new HashMap<>(); + public static final HashMap priorityEntryMap = new HashMap<>(); + public static final HashMap childEntryMap = new HashMap<>(); public static void addEntry(CreativeEntry entry){ - entryMap.put(entry.itemStack.toString(), entry); + String key = entry.stackToAdd.toString(); + if (entry.parentStack != null){ + childEntryMap.put(key, entry); + priorityEntryMap.remove(key); + } else { + priorityEntryMap.put(key, entry); + childEntryMap.remove(key); + } } public int priority; - public final ItemStack itemStack; + public final ItemStack stackToAdd; + public ItemStack parentStack; public CreativeEntry(ItemStack stack){ this(stack, 1000); } public CreativeEntry(ItemStack stack, int priority){ - this.itemStack = stack.copy(); - this.itemStack.stackSize = 1; + this.stackToAdd = stack.copy(); + this.stackToAdd.stackSize = 1; this.priority = priority; - addEntry(this); + } + public CreativeEntry(ItemStack stackToAdd, ItemStack parentStack){ + this.stackToAdd = stackToAdd.copy(); + this.stackToAdd.stackSize = 1; + this.parentStack = parentStack.copy(); + this.parentStack.stackSize = 1; } @Override From bd890dfa7331b109854f5481f45f63b866e3f69e Mon Sep 17 00:00:00 2001 From: UselessBullets <80850784+UselessBullets@users.noreply.github.com> Date: Sat, 17 Feb 2024 19:29:11 -0600 Subject: [PATCH 3/4] java docs --- .../halplibe/helper/CreativeHelper.java | 51 ++++++++++++++++--- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/src/main/java/turniplabs/halplibe/helper/CreativeHelper.java b/src/main/java/turniplabs/halplibe/helper/CreativeHelper.java index ff82723..206aea2 100644 --- a/src/main/java/turniplabs/halplibe/helper/CreativeHelper.java +++ b/src/main/java/turniplabs/halplibe/helper/CreativeHelper.java @@ -5,22 +5,57 @@ import turniplabs.halplibe.util.CreativeEntry; public class CreativeHelper { - public static void setPriority(IItemConvertible item, int priority){ - setPriority(item.getDefaultStack(), priority); + /** + * @param itemToAdd The itemstack to be added to the creative inventory list + * @param priority the priority for the item to be added, lower numbers appear higher in the list. Default is 1000 + */ + @SuppressWarnings("unused") + public static void setPriority(IItemConvertible itemToAdd, int priority){ + setPriority(itemToAdd.getDefaultStack(), priority); } - public static void setPriority(IItemConvertible item, int meta, int priority){ - setPriority(new ItemStack(item, 1, meta), priority); + + /** + * @param itemToAdd The itemstack to be added to the creative inventory list + * @param meta the meta value for the itemToAdd + * @param priority the priority for the item to be added, lower numbers appear higher in the list. Default is 1000 + */ + @SuppressWarnings("unused") + public static void setPriority(IItemConvertible itemToAdd, int meta, int priority){ + setPriority(new ItemStack(itemToAdd, 1, meta), priority); } - public static void setPriority(ItemStack item, int priority){ - CreativeEntry.addEntry(new CreativeEntry(item, priority)); + /** + * @param itemToAdd The itemstack to be added to the creative inventory list + * @param priority the priority for the item to be added, lower numbers appear higher in the list. Default is 1000 + */ + @SuppressWarnings("unused") + public static void setPriority(ItemStack itemToAdd, int priority){ + CreativeEntry.addEntry(new CreativeEntry(itemToAdd, priority)); } + + /** + * @param itemToAdd The itemstack to be added to the creative inventory list + * @param itemParent The itemstack that the itemToAdd will be placed after + */ + @SuppressWarnings("unused") public static void setParent(IItemConvertible itemToAdd, IItemConvertible itemParent){ setParent(itemToAdd.getDefaultStack(), itemParent.getDefaultStack()); } + /** + * @param itemToAdd The itemstack to be added to the creative inventory list + * @param metaToAdd the meta value for the itemToAdd + * @param itemParent The itemstack that the itemToAdd will be placed after + * @param metaParent the meta value for the itemParent + */ + @SuppressWarnings("unused") public static void setParent(IItemConvertible itemToAdd, int metaToAdd, IItemConvertible itemParent, int metaParent){ setParent(new ItemStack(itemToAdd, 1, metaToAdd), new ItemStack(itemParent, metaParent)); } - public static void setParent(ItemStack itemToAdd, ItemStack parentStack){ - CreativeEntry.addEntry(new CreativeEntry(itemToAdd, parentStack)); + /** + * @param itemToAdd The itemstack to be added to the creative inventory list + * @param itemParent The itemstack that the itemToAdd will be placed after + */ + @SuppressWarnings("unused") + public static void setParent(ItemStack itemToAdd, ItemStack itemParent){ + CreativeEntry.addEntry(new CreativeEntry(itemToAdd, itemParent)); } } From e392b0151fc4e78e78ba666692ed2b88c2b1874e Mon Sep 17 00:00:00 2001 From: UselessBullets <80850784+UselessBullets@users.noreply.github.com> Date: Sat, 17 Feb 2024 19:29:29 -0600 Subject: [PATCH 4/4] Update HalpLibe.java --- .../java/turniplabs/halplibe/HalpLibe.java | 24 ------------------- 1 file changed, 24 deletions(-) diff --git a/src/main/java/turniplabs/halplibe/HalpLibe.java b/src/main/java/turniplabs/halplibe/HalpLibe.java index bd63329..d921c48 100644 --- a/src/main/java/turniplabs/halplibe/HalpLibe.java +++ b/src/main/java/turniplabs/halplibe/HalpLibe.java @@ -86,30 +86,6 @@ public void onInitialize() { AchievementHelper.addPage(VANILLA_ACHIEVEMENTS); NetworkHelper.register(PacketModList.class, false, true); LOGGER.info("HalpLibe initialized."); - - CreativeHelper.setPriority(Block.planksOak, 990); - for (int i = 0; i < 16; i++) { - CreativeHelper.setPriority(Block.planksOakPainted, i, 990); - } - - CreativeHelper.setPriority(Block.brickBasalt, 999); - CreativeHelper.setPriority(Block.brickClay, 999); - CreativeHelper.setPriority(Block.brickGold, 999); - CreativeHelper.setPriority(Block.brickGranite, 999); - CreativeHelper.setPriority(Block.brickIron, 999); - CreativeHelper.setPriority(Block.brickLimestone, 999); - - CreativeHelper.setPriority(Item.ingotIron, 999); - CreativeHelper.setPriority(Item.ingotGold, 999); - CreativeHelper.setPriority(Item.ingotSteel, 999); - CreativeHelper.setPriority(Item.ingotSteelCrude, 999); - CreativeHelper.setPriority(Item.dustRedstone, 998); - CreativeHelper.setPriority(Item.dustGlowstone, 998); - CreativeHelper.setPriority(Item.dustSugar, 998); - - CreativeHelper.setParent(Item.seat, Item.toolHoeSteel); - CreativeHelper.setParent(Item.basket, Item.seat); - CreativeHelper.setParent(Item.armorChestplateDiamond, Item.basket); } @Override