Skip to content

Commit

Permalink
Creative tab api part 1. I'll do the rest tomorrow I'm too tired
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaMode committed Apr 4, 2024
1 parent b234eb3 commit 761b33e
Show file tree
Hide file tree
Showing 9 changed files with 245 additions and 1 deletion.
5 changes: 4 additions & 1 deletion modules/entity/build.gradle
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
portingLib.addModuleDependency("mixin_extensions")
portingLib {
addModuleDependency("mixin_extensions")
enableTestMod()
}
1 change: 1 addition & 0 deletions modules/items/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
portingLib.enableTestMod()
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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<ResourceLocation> tabsBefore = new ArrayList<>();
private final List<ResourceLocation> 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 <i>before</i> this tab. This tab will be placed <strong>after</strong> the {@code tabs}. **/
public PortingLibCreativeTabBuilder withTabsBefore(ResourceLocation... tabs) {
this.tabsBefore.addAll(List.of(tabs));
return this;
}

/** Define tabs that should come <i>after</i> this tab. This tab will be placed <strong>before</strong> the {@code tabs}.**/
public PortingLibCreativeTabBuilder withTabsAfter(ResourceLocation... tabs) {
this.tabsAfter.addAll(List.of(tabs));
return this;
}

/** Define tabs that should come <i>before</i> this tab. This tab will be placed <strong>after</strong> the {@code tabs}. **/
@SafeVarargs
public final PortingLibCreativeTabBuilder withTabsBefore(ResourceKey<CreativeModeTab>... tabs) {
Stream.of(tabs).map(ResourceKey::location).forEach(this.tabsBefore::add);
return this;
}

/** Define tabs that should come <i>after</i> this tab. This tab will be placed <strong>before</strong> the {@code tabs}.**/
@SafeVarargs
public final PortingLibCreativeTabBuilder withTabsAfter(ResourceKey<CreativeModeTab>... tabs) {
Stream.of(tabs).map(ResourceKey::location).forEach(this.tabsAfter::add);
return this;
}
}

public record TabData(ResourceLocation tabsImage, int labelColor, int slotColor, List<ResourceLocation> tabsBefore, List<ResourceLocation> tabsAfter) {}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"client.GuiGraphicsMixin"
],
"mixins": [
"client.CreativeModeInventoryScreenMixin",
"common.CreativeModeTabMixin",
"common.RepairItemRecipeMixin"
]
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
17 changes: 17 additions & 0 deletions modules/items/src/testmod/resources/fabric.mod.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
}

0 comments on commit 761b33e

Please sign in to comment.