Skip to content

Commit

Permalink
Revamp tab adder to comply with trash order
Browse files Browse the repository at this point in the history
  • Loading branch information
StavWasPlayZ committed Oct 10, 2023
1 parent 6646e5c commit d844eed
Showing 1 changed file with 30 additions and 24 deletions.
54 changes: 30 additions & 24 deletions src/main/java/com/cstav/evenmoreinstruments/item/ModItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.cstav.genshinstrument.ModCreativeModeTabs;
import com.cstav.genshinstrument.item.InstrumentItem;

import io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.Consumer;
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
Expand All @@ -24,18 +25,17 @@
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.properties.NoteBlockInstrument;
public class ModItems {

// How can I declare my mod to load only after another mod?
public static void load() {}


@SuppressWarnings("unchecked")
private static final ResourceKey<CreativeModeTab>[] DEFAULT_INSTRUMENTS_TABS = new ResourceKey[] {
getKey(ModCreativeModeTabs.INSTRUMENTS_TAB), CreativeModeTabs.BUILDING_BLOCKS
};
@SuppressWarnings("unchecked")
private static final ResourceKey<CreativeModeTab>[] DEFAULT_INSTRUMENT_BLOCK_TABS = new ResourceKey[] {
getKey(ModCreativeModeTabs.INSTRUMENTS_TAB), CreativeModeTabs.TOOLS_AND_UTILITIES, CreativeModeTabs.FUNCTIONAL_BLOCKS
};
private static void defaultInstrumentsTabs(final Item item) {
addToTab(ModCreativeModeTabs.INSTRUMENTS_TAB, item);
addToTab(CreativeModeTabs.BUILDING_BLOCKS, item);
}
private static void defaultInstrumentBlocksTab(final Item item) {
defaultInstrumentsTabs(item);
addToTab(CreativeModeTabs.FUNCTIONAL_BLOCKS, item);
}


public static final Item
Expand All @@ -55,7 +55,7 @@ public static void load() {}

KEYBOARD = register("keyboard",
new KeyboardBlockItem(ModBlocks.KEYBOARD, new Properties()),
DEFAULT_INSTRUMENT_BLOCK_TABS
ModItems::defaultInstrumentBlocksTab
),


Expand All @@ -67,8 +67,8 @@ public static void load() {}
// LOOPER_ADAPTER = register("looper_adapter", new LooperAdapterItem(new Properties()),
// CreativeModeTabs.REDSTONE_BLOCKS, getKey(EMIModCreativeModeTabs.INSTRUMENT_ACCESSORY_TAB)
// ),
KEYBOARD_STAND = registerBlockItem(ModBlocks.KEYBOARD_STAND,
getKey(EMIModCreativeModeTabs.INSTRUMENT_ACCESSORY_TAB)
KEYBOARD_STAND = registerBlockItem(ModBlocks.KEYBOARD_STAND, (item) ->
addToTab(EMIModCreativeModeTabs.INSTRUMENT_ACCESSORY_TAB, item)
)
;

Expand Down Expand Up @@ -96,29 +96,35 @@ public static HashMap<NoteBlockInstrument, Item> initNoteBlockInstruments() {
// private static RegistryObject<Item> registerBlockItem(final RegistryObject<Block> block) {
// return registerBlockItem(block, DEFAULT_INSTRUMENT_BLOCK_TABS);
// }
@SafeVarargs
private static Item registerBlockItem(Block block, ResourceKey<CreativeModeTab>... tabs) {
private static Item registerBlockItem(Block block, Consumer<Item> tabAdder) {
return register(BuiltInRegistries.BLOCK.getKey(block).getPath(),
new BlockItem(block, new Properties())
, tabs);
, tabAdder);
}

@SafeVarargs
private static Item register(String name, Item item, ResourceKey<CreativeModeTab>... tabs) {

private static Item register(String name, Item item, Consumer<Item> tabAdder) {
Registry.register(BuiltInRegistries.ITEM, new ResourceLocation(Main.MODID, name), item);

for (final ResourceKey<CreativeModeTab> tabKey : tabs)
addToTab(tabKey, item);
tabAdder.accept(item);

return item;
}
private static Item register(String name, Item item) {
return register(name, item, DEFAULT_INSTRUMENTS_TABS);
return register(name, item, ModItems::defaultInstrumentsTabs);
}


private static void addToTab(final ResourceKey<CreativeModeTab> tab, final Item item) {
ItemGroupEvents.modifyEntriesEvent(tab).register((content) -> content.accept(item));
private static void addToTab(CreativeModeTab tab, final Item item) {
ItemGroupEvents.MODIFY_ENTRIES_ALL.register((_tab, entries) -> {
if (getKey(tab).equals(getKey(_tab)))
entries.accept(item);
});
}
private static void addToTab(final ResourceKey<CreativeModeTab> tabKey, final Item item) {
ItemGroupEvents.MODIFY_ENTRIES_ALL.register((tab, entries) -> {
if (getKey(tab).equals(tabKey))
entries.accept(item);
});
}


Expand Down

0 comments on commit d844eed

Please sign in to comment.