diff --git a/src/main/java/turniplabs/halplibe/HalpLibe.java b/src/main/java/turniplabs/halplibe/HalpLibe.java index 24b99ac..d921c48 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; 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..206aea2 --- /dev/null +++ b/src/main/java/turniplabs/halplibe/helper/CreativeHelper.java @@ -0,0 +1,61 @@ +package turniplabs.halplibe.helper; + +import net.minecraft.core.item.IItemConvertible; +import net.minecraft.core.item.ItemStack; +import turniplabs.halplibe.util.CreativeEntry; + +public class CreativeHelper { + /** + * @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); + } + + /** + * @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); + } + /** + * @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)); + } + /** + * @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)); + } +} 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..6302fc3 --- /dev/null +++ b/src/main/java/turniplabs/halplibe/mixin/mixins/ContainerPlayerCreativeMixin.java @@ -0,0 +1,96 @@ +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; +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.HalpLibe; +import turniplabs.halplibe.util.CreativeEntry; + +import java.util.ArrayList; +import java.util.Collections; +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.priorityEntryMap.containsKey(stack.toString()) || CreativeEntry.childEntryMap.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.priorityEntryMap.values().stream().sorted().collect(Collectors.toList()); + boolean hasAddedVanillaBlocks = false; + for (CreativeEntry entry : entries){ + if (entry.stackToAdd.itemID < Block.blocksList.length){ + if (entry.priority > 1000 && !hasAddedVanillaBlocks){ + hasAddedVanillaBlocks = true; + newList.addAll(trimmedBlocks); + } + newList.add(entry.stackToAdd); + } + } + if (!hasAddedVanillaBlocks){ + newList.addAll(trimmedBlocks); + } + + boolean hasAddedVanillaItems = false; + for (CreativeEntry entry : entries){ + if (entry.stackToAdd.itemID >= Block.blocksList.length){ + if (entry.priority > 1000 && !hasAddedVanillaItems){ + hasAddedVanillaItems = true; + newList.addAll(trimmedItems); + } + 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 new file mode 100644 index 0000000..0aa9138 --- /dev/null +++ b/src/main/java/turniplabs/halplibe/util/CreativeEntry.java @@ -0,0 +1,43 @@ +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 priorityEntryMap = new HashMap<>(); + public static final HashMap childEntryMap = new HashMap<>(); + public static void addEntry(CreativeEntry 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 stackToAdd; + public ItemStack parentStack; + public CreativeEntry(ItemStack stack){ + this(stack, 1000); + } + public CreativeEntry(ItemStack stack, int priority){ + this.stackToAdd = stack.copy(); + this.stackToAdd.stackSize = 1; + this.priority = priority; + } + public CreativeEntry(ItemStack stackToAdd, ItemStack parentStack){ + this.stackToAdd = stackToAdd.copy(); + this.stackToAdd.stackSize = 1; + this.parentStack = parentStack.copy(); + this.parentStack.stackSize = 1; + } + + @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