generated from TropheusJ/fabric-example-mod
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Creative tab api part 1. I'll do the rest tomorrow I'm too tired
- Loading branch information
Showing
9 changed files
with
245 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
portingLib.addModuleDependency("mixin_extensions") | ||
portingLib { | ||
addModuleDependency("mixin_extensions") | ||
enableTestMod() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
portingLib.enableTestMod() |
15 changes: 15 additions & 0 deletions
15
...a/io/github/fabricators_of_create/porting_lib/item/api/extensions/CreativeModeTabExt.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
102 changes: 102 additions & 0 deletions
102
...io/github/fabricators_of_create/porting_lib/item/api/itemgroup/PortingLibCreativeTab.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} | ||
} |
35 changes: 35 additions & 0 deletions
35
...cators_of_create/porting_lib/item/impl/mixin/client/CreativeModeInventoryScreenMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...github/fabricators_of_create/porting_lib/item/impl/mixin/common/CreativeModeTabMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...java/io/github/fabricators_of_create/porting_lib/item/testmod/PortingLibItemsTestmod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
} |