Skip to content

Commit

Permalink
Rewrite item groups in Java
Browse files Browse the repository at this point in the history
  • Loading branch information
Juuxel committed Aug 1, 2024
1 parent 7b954d0 commit ba87de8
Show file tree
Hide file tree
Showing 13 changed files with 317 additions and 296 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public Text getAmountText(long max, FluidUnit maxUnit) {
}

private static FluidUnit getDefaultDisplayUnit() {
return ConfigManager.Companion.config().client.displayedFluidUnit;
return ConfigManager.config().client.displayedFluidUnit;
}

private static long getUnitDenominator(FluidUnit from, FluidUnit to) {
Expand Down
256 changes: 256 additions & 0 deletions common/src/main/java/juuxel/adorn/item/group/AdornItemGroups.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
package juuxel.adorn.item.group;

import juuxel.adorn.AdornCommon;
import juuxel.adorn.block.AdornBlocks;
import juuxel.adorn.block.variant.BlockKind;
import juuxel.adorn.block.variant.BlockVariant;
import juuxel.adorn.block.variant.BlockVariantSets;
import juuxel.adorn.config.ConfigManager;
import juuxel.adorn.item.AdornItems;
import juuxel.adorn.lib.registry.Registered;
import juuxel.adorn.lib.registry.Registrar;
import juuxel.adorn.lib.registry.RegistrarFactory;
import juuxel.adorn.platform.ItemGroupBridge;
import net.minecraft.block.Block;
import net.minecraft.item.ItemConvertible;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemGroups;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.registry.Registries;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.text.Text;
import net.minecraft.util.DyeColor;
import net.minecraft.util.Util;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public final class AdornItemGroups {
public static final Registrar<ItemGroup> ITEM_GROUPS = RegistrarFactory.get().create(RegistryKeys.ITEM_GROUP);

// Block kinds for each vanilla item group
private static final List<BlockKind> BUILDING_KINDS = List.of(
BlockKind.POST,
BlockKind.PLATFORM,
BlockKind.STEP
);
private static final List<BlockKind> FUNCTIONAL_KINDS = List.of(
BlockKind.CHAIR,
BlockKind.TABLE,
BlockKind.DRAWER,
BlockKind.KITCHEN_COUNTER,
BlockKind.KITCHEN_CUPBOARD,
BlockKind.KITCHEN_SINK,
BlockKind.SHELF,
BlockKind.COFFEE_TABLE,
BlockKind.BENCH
);

private static final String GROUP_ID = "items";
public static final Registered<ItemGroup> GROUP = ITEM_GROUPS.register(GROUP_ID,
() -> ItemGroupBridge.get().builder()
.displayName(Text.translatable(Util.createTranslationKey("itemGroup", AdornCommon.id(GROUP_ID))))
.icon(() -> new ItemStack(AdornBlocks.SOFAS.get().get(DyeColor.LIME)))
.entries((displayContext, entries) -> {
ItemGroupBuildContext context = entries::add;
switch (ConfigManager.config().groupItems) {
case BY_MATERIAL -> addByKinds(context, Arrays.asList(BlockKind.values()));
case BY_SHAPE -> {
for (var kind : BlockKind.values()) {
for (var block : BlockVariantSets.get(kind)) {
context.add(block);
}
}
}
}
addColoredBlocks(context);
addChimneys(context);
addFences(context);
addCopperPipes(context);
addCrates(context);
addMiscDecorations(context);
context.add(AdornBlocks.TRADING_STATION);
context.add(AdornBlocks.BREWER);
addFoodAndDrink(context);
addIngredients(context);
addTools(context);
})
.build());

public static void init() {
if (ConfigManager.get().config().client.showItemsInStandardGroups) {
addToVanillaItemGroups();
}
}

private static void addToVanillaItemGroups() {
var itemGroups = ItemGroupBridge.get();
itemGroups.addItems(ItemGroups.BUILDING_BLOCKS, context -> {
for (var variant : BlockVariantSets.allVariants()) {
var after = findLastBuildingBlockEntry(variant);

if (after != null) {
List<ItemConvertible> items = new ArrayList<>();
for (var kind : BUILDING_KINDS) {
var block = BlockVariantSets.get(kind, variant);
if (block != null) items.add(block.get());
}
context.addAfter(after, items);
} else {
addByKinds(context, variant, BUILDING_KINDS);
}
}

context.addAfter(Items.CUT_COPPER_SLAB, AdornBlocks.COPPER_PIPE);
context.addAfter(Items.EXPOSED_CUT_COPPER_SLAB, AdornBlocks.EXPOSED_COPPER_PIPE);
context.addAfter(Items.WEATHERED_CUT_COPPER_SLAB, AdornBlocks.WEATHERED_COPPER_PIPE);
context.addAfter(Items.OXIDIZED_CUT_COPPER_SLAB, AdornBlocks.OXIDIZED_COPPER_PIPE);
context.addAfter(Items.WAXED_CUT_COPPER_SLAB, AdornBlocks.WAXED_COPPER_PIPE);
context.addAfter(Items.WAXED_EXPOSED_CUT_COPPER_SLAB, AdornBlocks.WAXED_EXPOSED_COPPER_PIPE);
context.addAfter(Items.WAXED_WEATHERED_CUT_COPPER_SLAB, AdornBlocks.WAXED_WEATHERED_COPPER_PIPE);
context.addAfter(Items.WAXED_OXIDIZED_CUT_COPPER_SLAB, AdornBlocks.WAXED_OXIDIZED_COPPER_PIPE);
});
itemGroups.addItems(ItemGroups.COLORED_BLOCKS, AdornItemGroups::addColoredBlocks);
itemGroups.addItems(ItemGroups.FUNCTIONAL, context -> {
addByKinds(context, FUNCTIONAL_KINDS);
addColoredBlocks(context);
addChimneys(context);
addFences(context);
addCrates(context);
addMiscDecorations(context);
context.add(AdornBlocks.TRADING_STATION);
context.add(AdornBlocks.BREWER);
});
itemGroups.addItems(ItemGroups.FOOD_AND_DRINK, AdornItemGroups::addFoodAndDrink);
itemGroups.addItems(ItemGroups.INGREDIENTS, AdornItemGroups::addIngredients);
itemGroups.addItems(ItemGroups.TOOLS, AdornItemGroups::addTools);
}

private static void addByKinds(ItemGroupBuildContext context, List<BlockKind> kinds) {
for (var variant : BlockVariantSets.allVariants()) {
addByKinds(context, variant, kinds);
}
}

private static void addByKinds(ItemGroupBuildContext context, BlockVariant variant, List<BlockKind> kinds) {
for (var kind : kinds) {
var block = BlockVariantSets.get(kind, variant);
if (block != null) context.add(block);
}
}

private static void addColoredBlocks(ItemGroupBuildContext context) {
for (var sofa : AdornBlocks.SOFAS.get().values()) {
context.add(sofa);
}
for (var lamp : AdornBlocks.TABLE_LAMPS.get().values()) {
context.add(lamp);
}
context.add(AdornBlocks.CANDLELIT_LANTERN);
for (var lantern : AdornBlocks.DYED_CANDLELIT_LANTERNS.get().values()) {
context.add(lantern);
}
}

private static void addCrates(ItemGroupBuildContext context) {
context.add(AdornBlocks.CRATE);
context.add(AdornBlocks.APPLE_CRATE);
context.add(AdornBlocks.WHEAT_CRATE);
context.add(AdornBlocks.CARROT_CRATE);
context.add(AdornBlocks.POTATO_CRATE);
context.add(AdornBlocks.MELON_CRATE);
context.add(AdornBlocks.WHEAT_SEED_CRATE);
context.add(AdornBlocks.MELON_SEED_CRATE);
context.add(AdornBlocks.PUMPKIN_SEED_CRATE);
context.add(AdornBlocks.BEETROOT_CRATE);
context.add(AdornBlocks.BEETROOT_SEED_CRATE);
context.add(AdornBlocks.SWEET_BERRY_CRATE);
context.add(AdornBlocks.COCOA_BEAN_CRATE);
context.add(AdornBlocks.NETHER_WART_CRATE);
context.add(AdornBlocks.SUGAR_CANE_CRATE);
context.add(AdornBlocks.EGG_CRATE);
context.add(AdornBlocks.HONEYCOMB_CRATE);
context.add(AdornBlocks.LIL_TATER_CRATE);
}

private static void addChimneys(ItemGroupBuildContext context) {
context.add(AdornBlocks.BRICK_CHIMNEY);
context.add(AdornBlocks.STONE_BRICK_CHIMNEY);
context.add(AdornBlocks.NETHER_BRICK_CHIMNEY);
context.add(AdornBlocks.RED_NETHER_BRICK_CHIMNEY);
context.add(AdornBlocks.COBBLESTONE_CHIMNEY);
context.add(AdornBlocks.PRISMARINE_CHIMNEY);
context.add(AdornBlocks.MAGMATIC_PRISMARINE_CHIMNEY);
context.add(AdornBlocks.SOULFUL_PRISMARINE_CHIMNEY);
}

private static void addFences(ItemGroupBuildContext context) {
context.add(AdornBlocks.PICKET_FENCE);
context.add(AdornBlocks.CHAIN_LINK_FENCE);
}

private static void addCopperPipes(ItemGroupBuildContext context) {
context.add(AdornBlocks.COPPER_PIPE);
context.add(AdornBlocks.EXPOSED_COPPER_PIPE);
context.add(AdornBlocks.WEATHERED_COPPER_PIPE);
context.add(AdornBlocks.OXIDIZED_COPPER_PIPE);
context.add(AdornBlocks.WAXED_COPPER_PIPE);
context.add(AdornBlocks.WAXED_EXPOSED_COPPER_PIPE);
context.add(AdornBlocks.WAXED_WEATHERED_COPPER_PIPE);
context.add(AdornBlocks.WAXED_OXIDIZED_COPPER_PIPE);
}

private static void addFoodAndDrink(ItemGroupBuildContext context) {
context.add(AdornItems.MUG);
context.add(AdornItems.HOT_CHOCOLATE);
context.add(AdornItems.SWEET_BERRY_JUICE);
context.add(AdornItems.GLOW_BERRY_TEA);
context.add(AdornItems.NETHER_WART_COFFEE);
}

private static void addIngredients(ItemGroupBuildContext context) {
if (context instanceof ItemGroupModifyContext modifyContext) {
modifyContext.addAfter(Items.STICK, AdornItems.STONE_ROD);
modifyContext.addAfter(Items.IRON_NUGGET, AdornItems.COPPER_NUGGET);
} else {
context.add(AdornItems.STONE_ROD);
context.add(AdornItems.COPPER_NUGGET);
}
}

private static void addTools(ItemGroupBuildContext context) {
context.add(AdornItems.WATERING_CAN);
context.add(AdornItems.GUIDE_BOOK);
context.add(AdornItems.TRADERS_MANUAL);
}

private static void addMiscDecorations(ItemGroupBuildContext context) {
context.add(AdornItems.STONE_TORCH);
context.add(AdornBlocks.STONE_LADDER);
}

private static @Nullable Block findLastBuildingBlockEntry(BlockVariant variant) {
// Buttons are the last entry in "building blocks" for each material currently,
// then walls and finally slabs
return findBaseBlock(variant, "button")
.or(() -> findBaseBlock(variant, "wall"))
.or(() -> findBaseBlock(variant, "slab"))
.or(() -> findBaseBlock(variant, null))
.orElse(null);
}

private static Optional<Block> findBaseBlock(BlockVariant variant, @Nullable String suffix) {
var variantId = variant.nameAsIdentifier();
var buttonId = suffix != null ? variantId.withSuffixedPath("_" + suffix) : variantId;

if (Registries.BLOCK.containsId(buttonId)) {
return Optional.of(Registries.BLOCK.get(buttonId));
}

return Optional.empty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package juuxel.adorn.item.group;

import juuxel.adorn.lib.registry.Registered;
import net.minecraft.item.ItemConvertible;

@FunctionalInterface
public interface ItemGroupBuildContext {
void add(ItemConvertible item);

default void add(Registered<? extends ItemConvertible> item) {
add(item.get());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package juuxel.adorn.item.group;

import juuxel.adorn.lib.registry.Registered;
import net.minecraft.item.ItemConvertible;

import java.util.List;

public interface ItemGroupModifyContext extends ItemGroupBuildContext {
void addAfter(ItemConvertible after, List<? extends ItemConvertible> items);

default void addAfter(ItemConvertible after, ItemConvertible item) {
addAfter(after, List.of(item));
}

default void addAfter(ItemConvertible after, Registered<? extends ItemConvertible> item) {
addAfter(after, item.get());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package juuxel.adorn.item.group;

import juuxel.adorn.util.Displayable;
import net.minecraft.text.Text;

public enum ItemGroupingOption implements Displayable {
BY_MATERIAL("by_material"),
BY_SHAPE("by_shape");

private final Text displayName;

ItemGroupingOption(String id) {
displayName = Text.translatable("gui.adorn.item_grouping_option." + id);
}

@Override
public Text getDisplayName() {
return displayName;
}
}
2 changes: 1 addition & 1 deletion common/src/main/java/juuxel/adorn/lib/AdornGameRules.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static void init() {
}

private static Type<BooleanRule> createBooleanRule(Predicate<Config.GameRuleDefaults> defaultGetter) {
return BooleanRule.create(defaultGetter.test(ConfigManager.Companion.config().gameRuleDefaults));
return BooleanRule.create(defaultGetter.test(ConfigManager.config().gameRuleDefaults));
}

private static <T extends Rule<T>> Key<T> register(String name, Category category, Type<T> type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ abstract class ConfigManager {
private val DEFAULT = JANKSON.toJson(Config()) as JsonObject
private val LOGGER = logger()

@JvmStatic
fun config() = INSTANCE.config
}
}
Loading

0 comments on commit ba87de8

Please sign in to comment.