From 761b33e27b8cdc96ee3cbb3029c8137c4ba244b8 Mon Sep 17 00:00:00 2001 From: alpha Date: Wed, 3 Apr 2024 21:39:59 -0500 Subject: [PATCH] Creative tab api part 1. I'll do the rest tomorrow I'm too tired --- modules/entity/build.gradle | 5 +- modules/items/build.gradle | 1 + .../api/extensions/CreativeModeTabExt.java | 15 +++ .../api/itemgroup/PortingLibCreativeTab.java | 102 ++++++++++++++++++ .../CreativeModeInventoryScreenMixin.java | 35 ++++++ .../mixin/common/CreativeModeTabMixin.java | 25 +++++ .../resources/porting_lib_items.mixins.json | 2 + .../item/testmod/PortingLibItemsTestmod.java | 44 ++++++++ .../src/testmod/resources/fabric.mod.json | 17 +++ 9 files changed, 245 insertions(+), 1 deletion(-) create mode 100644 modules/items/src/main/java/io/github/fabricators_of_create/porting_lib/item/api/extensions/CreativeModeTabExt.java create mode 100644 modules/items/src/main/java/io/github/fabricators_of_create/porting_lib/item/api/itemgroup/PortingLibCreativeTab.java create mode 100644 modules/items/src/main/java/io/github/fabricators_of_create/porting_lib/item/impl/mixin/client/CreativeModeInventoryScreenMixin.java create mode 100644 modules/items/src/main/java/io/github/fabricators_of_create/porting_lib/item/impl/mixin/common/CreativeModeTabMixin.java create mode 100644 modules/items/src/testmod/java/io/github/fabricators_of_create/porting_lib/item/testmod/PortingLibItemsTestmod.java create mode 100644 modules/items/src/testmod/resources/fabric.mod.json diff --git a/modules/entity/build.gradle b/modules/entity/build.gradle index 5f15dd7be..efb9d82b2 100644 --- a/modules/entity/build.gradle +++ b/modules/entity/build.gradle @@ -1 +1,4 @@ -portingLib.addModuleDependency("mixin_extensions") +portingLib { + addModuleDependency("mixin_extensions") + enableTestMod() +} diff --git a/modules/items/build.gradle b/modules/items/build.gradle index e69de29bb..52bbd2ac5 100644 --- a/modules/items/build.gradle +++ b/modules/items/build.gradle @@ -0,0 +1 @@ +portingLib.enableTestMod() diff --git a/modules/items/src/main/java/io/github/fabricators_of_create/porting_lib/item/api/extensions/CreativeModeTabExt.java b/modules/items/src/main/java/io/github/fabricators_of_create/porting_lib/item/api/extensions/CreativeModeTabExt.java new file mode 100644 index 000000000..ed1b802cc --- /dev/null +++ b/modules/items/src/main/java/io/github/fabricators_of_create/porting_lib/item/api/extensions/CreativeModeTabExt.java @@ -0,0 +1,15 @@ +package io.github.fabricators_of_create.porting_lib.item.api.extensions; + +import io.github.fabricators_of_create.porting_lib.item.api.itemgroup.PortingLibCreativeTab; +import net.minecraft.resources.ResourceLocation; + +import javax.annotation.Nullable; + +import java.util.List; + +public interface CreativeModeTabExt { + void setPortingData(PortingLibCreativeTab.TabData data); + + @Nullable + PortingLibCreativeTab.TabData getPortingTabData(); +} diff --git a/modules/items/src/main/java/io/github/fabricators_of_create/porting_lib/item/api/itemgroup/PortingLibCreativeTab.java b/modules/items/src/main/java/io/github/fabricators_of_create/porting_lib/item/api/itemgroup/PortingLibCreativeTab.java new file mode 100644 index 000000000..76643f07e --- /dev/null +++ b/modules/items/src/main/java/io/github/fabricators_of_create/porting_lib/item/api/itemgroup/PortingLibCreativeTab.java @@ -0,0 +1,102 @@ +package io.github.fabricators_of_create.porting_lib.item.api.itemgroup; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Stream; + +import io.github.fabricators_of_create.porting_lib.item.api.extensions.CreativeModeTabExt; +import net.minecraft.network.chat.Component; +import net.minecraft.resources.ResourceKey; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.CreativeModeTab; + +public class PortingLibCreativeTab { + public static PortingLibCreativeTabBuilder builder() { + return new PortingLibCreativeTabBuilder(); + } + + public static class PortingLibCreativeTabBuilder extends CreativeModeTab.Builder { + public static final ResourceLocation CREATIVE_TABS_LOCATION = new ResourceLocation("textures/gui/container/creative_inventory/tabs.png"); + + private boolean hasDisplayName = false; + private ResourceLocation tabsImage = CREATIVE_TABS_LOCATION; + private int labelColor = 4210752; + private int slotColor = -2130706433; + private final List tabsBefore = new ArrayList<>(); + private final List tabsAfter = new ArrayList<>(); + + public PortingLibCreativeTabBuilder() { + // Set when building. + super(null, -1); + } + + @Override + public PortingLibCreativeTabBuilder title(Component displayName) { + hasDisplayName = true; + return (PortingLibCreativeTabBuilder) super.title(displayName); + } + + @Override + public CreativeModeTab build() { + if (!hasDisplayName) { + throw new IllegalStateException("No display name set for ItemGroup"); + } + + CreativeModeTab tab = super.build(); + ((CreativeModeTabExt) tab).setPortingData(new TabData(tabsImage, labelColor, slotColor, tabsBefore, tabsAfter)); + return tab; + } + + /** + * Sets the image of the tab to a custom resource location, instead of an item's texture. + */ + public PortingLibCreativeTabBuilder withTabsImage(ResourceLocation tabsImage) { + this.tabsImage = tabsImage; + return this; + } + + /** + * Sets the color of the tab label. + */ + public PortingLibCreativeTabBuilder withLabelColor(int labelColor) { + this.labelColor = labelColor; + return this; + } + + /** + * Sets the color of tab's slots. + */ + public PortingLibCreativeTabBuilder withSlotColor(int slotColor) { + this.slotColor = slotColor; + return this; + } + + /** Define tabs that should come before this tab. This tab will be placed after the {@code tabs}. **/ + public PortingLibCreativeTabBuilder withTabsBefore(ResourceLocation... tabs) { + this.tabsBefore.addAll(List.of(tabs)); + return this; + } + + /** Define tabs that should come after this tab. This tab will be placed before the {@code tabs}.**/ + public PortingLibCreativeTabBuilder withTabsAfter(ResourceLocation... tabs) { + this.tabsAfter.addAll(List.of(tabs)); + return this; + } + + /** Define tabs that should come before this tab. This tab will be placed after the {@code tabs}. **/ + @SafeVarargs + public final PortingLibCreativeTabBuilder withTabsBefore(ResourceKey... tabs) { + Stream.of(tabs).map(ResourceKey::location).forEach(this.tabsBefore::add); + return this; + } + + /** Define tabs that should come after this tab. This tab will be placed before the {@code tabs}.**/ + @SafeVarargs + public final PortingLibCreativeTabBuilder withTabsAfter(ResourceKey... tabs) { + Stream.of(tabs).map(ResourceKey::location).forEach(this.tabsAfter::add); + return this; + } + } + + public record TabData(ResourceLocation tabsImage, int labelColor, int slotColor, List tabsBefore, List tabsAfter) {} +} diff --git a/modules/items/src/main/java/io/github/fabricators_of_create/porting_lib/item/impl/mixin/client/CreativeModeInventoryScreenMixin.java b/modules/items/src/main/java/io/github/fabricators_of_create/porting_lib/item/impl/mixin/client/CreativeModeInventoryScreenMixin.java new file mode 100644 index 000000000..16824d11d --- /dev/null +++ b/modules/items/src/main/java/io/github/fabricators_of_create/porting_lib/item/impl/mixin/client/CreativeModeInventoryScreenMixin.java @@ -0,0 +1,35 @@ +package io.github.fabricators_of_create.porting_lib.item.impl.mixin.client; + +import io.github.fabricators_of_create.porting_lib.item.api.extensions.CreativeModeTabExt; +import io.github.fabricators_of_create.porting_lib.item.api.itemgroup.PortingLibCreativeTab; +import net.minecraft.client.gui.screens.inventory.CreativeModeInventoryScreen; + +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.CreativeModeTab; + +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.ModifyArg; + +@Mixin(CreativeModeInventoryScreen.class) +public class CreativeModeInventoryScreenMixin { + @Shadow + private static CreativeModeTab selectedTab; + + @ModifyArg(method = "renderLabels", index = 4, at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiGraphics;drawString(Lnet/minecraft/client/gui/Font;Lnet/minecraft/network/chat/Component;IIIZ)I")) + private int modifyLabelColor(int labelColor) { + PortingLibCreativeTab.TabData data = ((CreativeModeTabExt) selectedTab).getPortingTabData(); + if (data != null) + return data.labelColor(); + return labelColor; + } + + @ModifyArg(method = "renderBg", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiGraphics;blit(Lnet/minecraft/resources/ResourceLocation;IIIIII)V", ordinal = 1)) + private ResourceLocation modifyTabImage(ResourceLocation original) { + PortingLibCreativeTab.TabData data = ((CreativeModeTabExt) selectedTab).getPortingTabData(); + if (data != null) + return data.tabsImage(); + return original; + } +} diff --git a/modules/items/src/main/java/io/github/fabricators_of_create/porting_lib/item/impl/mixin/common/CreativeModeTabMixin.java b/modules/items/src/main/java/io/github/fabricators_of_create/porting_lib/item/impl/mixin/common/CreativeModeTabMixin.java new file mode 100644 index 000000000..0c5ed11a4 --- /dev/null +++ b/modules/items/src/main/java/io/github/fabricators_of_create/porting_lib/item/impl/mixin/common/CreativeModeTabMixin.java @@ -0,0 +1,25 @@ +package io.github.fabricators_of_create.porting_lib.item.impl.mixin.common; + +import javax.annotation.Nullable; + +import org.spongepowered.asm.mixin.Mixin; + +import io.github.fabricators_of_create.porting_lib.item.api.extensions.CreativeModeTabExt; +import io.github.fabricators_of_create.porting_lib.item.api.itemgroup.PortingLibCreativeTab; +import net.minecraft.world.item.CreativeModeTab; + +@Mixin(CreativeModeTab.class) +public class CreativeModeTabMixin implements CreativeModeTabExt { + @Nullable + private PortingLibCreativeTab.TabData porting$data; + + @Override + public void setPortingData(PortingLibCreativeTab.TabData data) { + this.porting$data = data; + } + + @Override + public PortingLibCreativeTab.TabData getPortingTabData() { + return this.porting$data; + } +} diff --git a/modules/items/src/main/resources/porting_lib_items.mixins.json b/modules/items/src/main/resources/porting_lib_items.mixins.json index 774165564..0e5a9909a 100644 --- a/modules/items/src/main/resources/porting_lib_items.mixins.json +++ b/modules/items/src/main/resources/porting_lib_items.mixins.json @@ -10,6 +10,8 @@ "client.GuiGraphicsMixin" ], "mixins": [ + "client.CreativeModeInventoryScreenMixin", + "common.CreativeModeTabMixin", "common.RepairItemRecipeMixin" ] } diff --git a/modules/items/src/testmod/java/io/github/fabricators_of_create/porting_lib/item/testmod/PortingLibItemsTestmod.java b/modules/items/src/testmod/java/io/github/fabricators_of_create/porting_lib/item/testmod/PortingLibItemsTestmod.java new file mode 100644 index 000000000..ab6b9ffef --- /dev/null +++ b/modules/items/src/testmod/java/io/github/fabricators_of_create/porting_lib/item/testmod/PortingLibItemsTestmod.java @@ -0,0 +1,44 @@ +package io.github.fabricators_of_create.porting_lib.item.testmod; + +import io.github.fabricators_of_create.porting_lib.core.PortingLib; +import io.github.fabricators_of_create.porting_lib.item.api.itemgroup.PortingLibCreativeTab; +import net.fabricmc.api.ModInitializer; +import net.minecraft.core.Registry; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.network.chat.Component; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.BlockItem; +import net.minecraft.world.item.HoeItem; +import net.minecraft.world.level.block.SkullBlock; + +public class PortingLibItemsTestmod implements ModInitializer { + public static final ResourceLocation TAB1 = PortingLib.id("tab1"); + public static final ResourceLocation TAB2 = PortingLib.id("tab2"); + @Override + public void onInitialize() { + var tab1 = PortingLibCreativeTab.builder() + .title(Component.literal("Test tab 1")) + .withTabsBefore(TAB2) + .withLabelColor(14525) + .withSlotColor(2526789) + .displayItems((itemDisplayParameters, output) -> { + BuiltInRegistries.ITEM.forEach(item -> { + if (item instanceof HoeItem) + output.accept(item); + }); + }) + .build(); + var tab2 = PortingLibCreativeTab.builder() + .title(Component.literal("Test tab 2")) + .displayItems((itemDisplayParameters, output) -> { + BuiltInRegistries.ITEM.forEach(item -> { + if (item instanceof BlockItem blockItem && blockItem.getBlock() instanceof SkullBlock) + output.accept(item); + }); + }) + .build(); + + Registry.register(BuiltInRegistries.CREATIVE_MODE_TAB, TAB1, tab1); + Registry.register(BuiltInRegistries.CREATIVE_MODE_TAB, TAB2, tab2); + } +} diff --git a/modules/items/src/testmod/resources/fabric.mod.json b/modules/items/src/testmod/resources/fabric.mod.json new file mode 100644 index 000000000..35540b373 --- /dev/null +++ b/modules/items/src/testmod/resources/fabric.mod.json @@ -0,0 +1,17 @@ +{ + "schemaVersion": 1, + "id": "porting_lib_items_testmod", + "version": "${version}", + "name": "Porting Lib Items Testmod", + "description": "Tests the items module", + "authors": [ + "The Create Fabric Team" + ], + "license": "LGPL", + "environment": "*", + "entrypoints": { + "main": [ + "io.github.fabricators_of_create.porting_lib.item.testmod.PortingLibItemsTestmod" + ] + } +}