-
-
Notifications
You must be signed in to change notification settings - Fork 511
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start implementing item data components
Nothing is hooked up yet, the components are just registered
- Loading branch information
1 parent
cdfa029
commit 2ab92e1
Showing
6 changed files
with
173 additions
and
0 deletions.
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
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
17 changes: 17 additions & 0 deletions
17
Xplat/src/main/java/vazkii/botania/common/component/AdvancedTooltipProvider.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,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 | ||
); | ||
} |
42 changes: 42 additions & 0 deletions
42
Xplat/src/main/java/vazkii/botania/common/component/BotaniaDataComponents.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,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())); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
Xplat/src/main/java/vazkii/botania/common/component/TooltipVisibility.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,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; | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
Xplat/src/main/java/vazkii/botania/common/component/WandOfTheForestComponent.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,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; | ||
} | ||
} | ||
} |