Skip to content

Commit

Permalink
Add an interface for creative tab filling logic on items.
Browse files Browse the repository at this point in the history
  • Loading branch information
noeppi-noeppi committed Dec 10, 2023
1 parent a23f488 commit 345c4e6
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 33 deletions.
46 changes: 27 additions & 19 deletions src/main/java/org/moddingx/libx/base/BlockBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import net.minecraft.world.level.block.Block;
import net.minecraftforge.client.extensions.common.IClientItemExtensions;
import net.minecraftforge.registries.ForgeRegistries;
import org.moddingx.libx.creativetab.CreativeTabX;
import org.moddingx.libx.creativetab.CreativeTabItemProvider;
import org.moddingx.libx.mod.ModX;
import org.moddingx.libx.mod.ModXRegistration;
import org.moddingx.libx.registration.Registerable;
Expand All @@ -24,7 +24,7 @@
* Base class for {@link Block blocks} for mods using {@link ModXRegistration}. This will automatically set the
* creative tab if it's defined in the mod and register a {@link BlockItem block item}.
*/
public class BlockBase extends Block implements Registerable {
public class BlockBase extends Block implements Registerable, CreativeTabItemProvider {

protected final ModX mod;

Expand Down Expand Up @@ -52,18 +52,7 @@ public BlockBase(ModX mod, Properties properties, @Nullable Item.Properties item
this.item = null;
} else {
this.hasItem = true;
this.item = new BlockItem(this, itemProperties) {

@Override
public void initializeClient(@Nonnull Consumer<IClientItemExtensions> consumer) {
BlockBase.this.initializeItemClient(consumer);
}

@Override
public int getBurnTime(ItemStack stack, @Nullable RecipeType<?> recipeType) {
return BlockBase.this.getBurnTime(stack, recipeType);
}
};
this.item = new BaseBlockItem(this, itemProperties);
}
}

Expand All @@ -78,11 +67,8 @@ public void initializeItemClient(@Nonnull Consumer<IClientItemExtensions> consum
public int getBurnTime(ItemStack stack, @Nullable RecipeType<?> recipeType) {
return -1;
}

/**
* Returns a {@link Stream} of {@link ItemStack item stacks} to add to a creative tab.
* {@link CreativeTabX} respects these by default.
*/

@Override
public Stream<ItemStack> makeCreativeTabStacks() {
return Stream.of(new ItemStack(this));
}
Expand All @@ -102,4 +88,26 @@ public void initTracking(RegistrationContext ctx, TrackingCollector builder) thr
builder.track(ForgeRegistries.ITEMS, BlockBase.class.getDeclaredField("item"));
}
}

private class BaseBlockItem extends BlockItem implements CreativeTabItemProvider {

public BaseBlockItem(Block block, Properties itemProperties) {
super(block, itemProperties);
}

@Override
public void initializeClient(@Nonnull Consumer<IClientItemExtensions> consumer) {
BlockBase.this.initializeItemClient(consumer);
}

@Override
public int getBurnTime(ItemStack stack, @Nullable RecipeType<?> recipeType) {
return BlockBase.this.getBurnTime(stack, recipeType);
}

@Override
public Stream<ItemStack> makeCreativeTabStacks() {
return BlockBase.this.makeCreativeTabStacks();
}
}
}
9 changes: 3 additions & 6 deletions src/main/java/org/moddingx/libx/base/ItemBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import org.moddingx.libx.creativetab.CreativeTabX;
import org.moddingx.libx.creativetab.CreativeTabItemProvider;
import org.moddingx.libx.mod.ModX;
import org.moddingx.libx.mod.ModXRegistration;

Expand All @@ -12,7 +12,7 @@
* Base class for {@link Item items} for mods using {@link ModXRegistration}. This will automatically set the
* creative tab if it's defined in the mod.
*/
public class ItemBase extends Item {
public class ItemBase extends Item implements CreativeTabItemProvider {

protected final ModX mod;

Expand All @@ -21,10 +21,7 @@ public ItemBase(ModX mod, Properties properties) {
this.mod = mod;
}

/**
* Returns a {@link Stream} of {@link ItemStack item stacks} to add to a creative tab.
* {@link CreativeTabX} respects these by default.
*/
@Override
public Stream<ItemStack> makeCreativeTabStacks() {
return Stream.of(new ItemStack(this));
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/moddingx/libx/codec/MoreCodecs.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public static <T> Codec<T> lazy(Supplier<Codec<T>> codec) {

/**
* Behaves the same as {@link Codec#optionalFieldOf(String)} but instead of silently using the default value
* if decoding the element prodices an error, propagates the error through. The only way this yields an empty
* if decoding the element produces an error, propagates the error through. The only way this yields an empty
* {@link Optional} is if the key is completely missing.
*/
public static <T> MapCodec<Optional<T>> optionalFieldOf(Codec<T> codec, String name) {
Expand All @@ -132,7 +132,7 @@ public static <T> MapCodec<Optional<T>> optionalFieldOf(Codec<T> codec, String n

/**
* Behaves the same as {@link Codec#optionalFieldOf(String, Object)} but instead of silently using the default
* value if decoding the element prodices an error, propagates the error through. The only way this yields an
* value if decoding the element produces an error, propagates the error through. The only way this yields an
* empty {@link Optional} is if the key is completely missing.
*/
public static <T> MapCodec<T> optionalFieldOf(Codec<T> codec, String name, T defaultValue) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.moddingx.libx.creativetab;

import net.minecraft.world.item.ItemStack;
import org.moddingx.libx.base.BlockBase;
import org.moddingx.libx.base.ItemBase;

import java.util.stream.Stream;

/**
* Can be implemented on items to provide custom logic on how {@link CreativeTabX} should fill them by default.
* Both {@link ItemBase} and the block item generated by {@link BlockBase} implement this by default.
*/
public interface CreativeTabItemProvider {

/**
* Returns a {@link Stream} of {@link ItemStack item stacks} to add to a creative tab.
*/
Stream<ItemStack> makeCreativeTabStacks();
}
8 changes: 2 additions & 6 deletions src/main/java/org/moddingx/libx/creativetab/CreativeTabX.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.ForgeRegistry;
import net.minecraftforge.registries.RegisterEvent;
import org.moddingx.libx.base.BlockBase;
import org.moddingx.libx.base.ItemBase;
import org.moddingx.libx.impl.ModInternal;
import org.moddingx.libx.mod.ModX;

Expand Down Expand Up @@ -129,10 +127,8 @@ private void registerCreativeTab(RegisterEvent event) {
}

private Stream<ItemStack> itemStream(Item item) {
if (item instanceof ItemBase base) {
return base.makeCreativeTabStacks();
} else if (item instanceof BlockItem blockItem && blockItem.getBlock() instanceof BlockBase base) {
return base.makeCreativeTabStacks();
if (item instanceof CreativeTabItemProvider provider) {
return provider.makeCreativeTabStacks();
} else {
return Stream.of(new ItemStack(item));
}
Expand Down

0 comments on commit 345c4e6

Please sign in to comment.