Skip to content

Commit

Permalink
Start implementing item data components
Browse files Browse the repository at this point in the history
Nothing is hooked up yet, the components are just registered
  • Loading branch information
artemisSystem committed Nov 11, 2024
1 parent cdfa029 commit 2ab92e1
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
import vazkii.botania.common.brew.BotaniaBrews;
import vazkii.botania.common.brew.BotaniaMobEffects;
import vazkii.botania.common.command.SkyblockCommand;
import vazkii.botania.common.component.BotaniaDataComponents;
import vazkii.botania.common.config.ConfigDataManagerImpl;
import vazkii.botania.common.crafting.BotaniaRecipeTypes;
import vazkii.botania.common.crafting.StateIngredients;
Expand Down Expand Up @@ -177,6 +178,7 @@ private void registryInit() {
Blocks.FARMLAND.defaultBlockState());
FlattenableBlockRegistry.register(b, Blocks.DIRT_PATH.defaultBlockState());
}
BotaniaDataComponents.registerComponents(bind(BuiltInRegistries.DATA_COMPONENT_TYPE));

int blazeTime = 2400;
FuelRegistry.INSTANCE.add(BotaniaBlocks.blazeBlock.asItem(), blazeTime * (XplatAbstractions.INSTANCE.gogLoaded() ? 5 : 10));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
import vazkii.botania.common.brew.BotaniaMobEffects;
import vazkii.botania.common.brew.effect.SoulCrossMobEffect;
import vazkii.botania.common.command.SkyblockCommand;
import vazkii.botania.common.component.BotaniaDataComponents;
import vazkii.botania.common.config.ConfigDataManagerImpl;
import vazkii.botania.common.crafting.BotaniaRecipeTypes;
import vazkii.botania.common.entity.BotaniaEntities;
Expand Down Expand Up @@ -194,6 +195,7 @@ private void registryInit() {
bind(Registries.BLOCK, BotaniaFlowerBlocks::registerBlocks);
bindForItems(BotaniaFlowerBlocks::registerItemBlocks);
bind(Registries.BLOCK_ENTITY_TYPE, BotaniaFlowerBlocks::registerTEs);
bind(Registries.DATA_COMPONENT_TYPE, BotaniaDataComponents::registerComponents);

// GUI and Recipe
bind(Registries.MENU, BotaniaItems::registerMenuTypes);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package vazkii.botania.common.component;

import net.minecraft.network.chat.Component;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.TooltipFlag;

import java.util.List;
import java.util.function.Consumer;

public interface AdvancedTooltipProvider {
void addToTooltip(
Item.TooltipContext context,
Consumer<Component> tooltipAdder,
TooltipFlag tooltipFlag,
List<Component> fullTooltip
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package vazkii.botania.common.component;

import net.minecraft.core.component.DataComponentType;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.ExtraCodecs;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.UnaryOperator;

import static vazkii.botania.api.BotaniaAPI.botaniaRL;

public class BotaniaDataComponents {
private static final Map<String, DataComponentType<?>> toRegister = new HashMap<>();

public static final DataComponentType<Integer> MAX_MANA = schedule(
"max_mana",
builder -> builder.persistent(ExtraCodecs.POSITIVE_INT).networkSynchronized(ByteBufCodecs.VAR_INT)
);
public static final DataComponentType<Integer> MANA = schedule(
"mana",
builder -> builder.persistent(ExtraCodecs.NON_NEGATIVE_INT).networkSynchronized(ByteBufCodecs.VAR_INT)
);
public static final DataComponentType<WandOfTheForestComponent> WAND_OF_THE_FOREST = schedule(
"wand_of_the_forest",
builder -> builder.persistent(WandOfTheForestComponent.CODEC).networkSynchronized(WandOfTheForestComponent.STREAM_CODEC)
);

private static <T> DataComponentType<T> schedule(String name, UnaryOperator<DataComponentType.Builder<T>> builder) {
DataComponentType<T> type = builder.apply(DataComponentType.builder()).build();
toRegister.put(name, type);
return type;
};

public static void registerComponents(BiConsumer<DataComponentType<?>, ResourceLocation> biConsumer) {
for (Map.Entry<String, DataComponentType<?>> entry : toRegister.entrySet()) {
biConsumer.accept(entry.getValue(), botaniaRL(entry.getKey()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package vazkii.botania.common.component;

import com.mojang.serialization.Codec;
import io.netty.buffer.ByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.util.ByIdMap;
import net.minecraft.util.StringRepresentable;
import org.jetbrains.annotations.NotNull;

import java.util.function.IntFunction;

public enum TooltipVisibility implements StringRepresentable {
HIDDEN(0, "hidden"),
IN_NAME(1, "in_name"),
IN_LIST(2, "in_list");

private final int id;
private final String name;
public static final Codec<TooltipVisibility> CODEC = StringRepresentable.fromValues(TooltipVisibility::values);
public static final IntFunction<TooltipVisibility> BY_ID = ByIdMap.continuous(visibility -> visibility.id, values(), ByIdMap.OutOfBoundsStrategy.ZERO);
public static final StreamCodec<ByteBuf, TooltipVisibility> STREAM_CODEC = ByteBufCodecs.idMapper(BY_ID, visibility -> visibility.id);

TooltipVisibility(int id, String name) {
this.id = id;
this.name = name;
}

@Override
public @NotNull String getSerializedName() {
return name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package vazkii.botania.common.component;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import io.netty.buffer.ByteBuf;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentUtils;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.Style;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.util.ByIdMap;
import net.minecraft.util.StringRepresentable;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.TooltipFlag;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.function.Consumer;
import java.util.function.IntFunction;

public record WandOfTheForestComponent(WandMode wandMode, TooltipVisibility tooltipVisibility) implements AdvancedTooltipProvider {
public static Codec<WandOfTheForestComponent> CODEC = RecordCodecBuilder.create(instance ->
instance.group(
WandMode.CODEC.optionalFieldOf("mode", WandMode.BIND).forGetter(WandOfTheForestComponent::wandMode),
TooltipVisibility.CODEC.optionalFieldOf("tooltip_visibility", TooltipVisibility.IN_NAME).forGetter(WandOfTheForestComponent::tooltipVisibility)
).apply(instance, WandOfTheForestComponent::new)
);
public static StreamCodec<ByteBuf, WandOfTheForestComponent> STREAM_CODEC = StreamCodec.composite(
WandMode.STREAM_CODEC, WandOfTheForestComponent::wandMode,
TooltipVisibility.STREAM_CODEC, WandOfTheForestComponent::tooltipVisibility,
WandOfTheForestComponent::new
);

@Override
public void addToTooltip(Item.TooltipContext context, Consumer<Component> tooltipAdder, TooltipFlag tooltipFlag, List<Component> fullTooltip) {
if (tooltipVisibility == TooltipVisibility.IN_NAME) {
MutableComponent name = fullTooltip.get(0).copy();
Style style = name.getStyle();
name.append(" (");
name.append(wandMode.getDisplay());
// TODO: 1.21 this probably has the wrong style
name.append(")");
fullTooltip.set(0, name);
} else if (tooltipVisibility == TooltipVisibility.IN_LIST) {
tooltipAdder.accept(wandMode.getDisplay());
}
}

public enum WandMode implements StringRepresentable {
BIND(0, "bind", "botaniamisc.wandMode.bind"),
FUNCTION(1, "function", "botaniamisc.wandMode.function");

public static final Codec<WandMode> CODEC = StringRepresentable.fromValues(WandMode::values);
public static final IntFunction<WandMode> BY_ID = ByIdMap.continuous(mode -> mode.id, values(), ByIdMap.OutOfBoundsStrategy.ZERO);
public static final StreamCodec<ByteBuf, WandMode> STREAM_CODEC = ByteBufCodecs.idMapper(BY_ID, mode -> mode.id);
private final int id;
private final String name;
private final Component display;

WandMode(int id, String name, String translationKey) {
this.id = id;
this.name = name;
this.display = Component.translatable(translationKey).withStyle(ChatFormatting.DARK_GREEN);
}

@Override
public @NotNull String getSerializedName() {
return name;
}

public Component getDisplay() {
return display;
}
}
}

0 comments on commit 2ab92e1

Please sign in to comment.