Skip to content

Commit

Permalink
Moved some tooltip classes around, added clear action
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Jul 24, 2024
1 parent 6375e00 commit 383a24e
Show file tree
Hide file tree
Showing 26 changed files with 221 additions and 205 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import dev.latvian.mods.kubejs.script.data.GeneratedData;
import dev.latvian.mods.kubejs.script.data.GeneratedDataStage;
import dev.latvian.mods.kubejs.script.data.VirtualAssetPack;
import dev.latvian.mods.kubejs.tooltip.ItemTooltipData;
import dev.latvian.mods.kubejs.text.tooltip.ItemTooltipData;
import net.minecraft.SharedConstants;
import net.minecraft.Util;
import net.minecraft.client.Minecraft;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import dev.latvian.mods.kubejs.kubedex.KubedexHighlight;
import dev.latvian.mods.kubejs.script.ConsoleJS;
import dev.latvian.mods.kubejs.script.ScriptType;
import dev.latvian.mods.kubejs.tooltip.ItemTooltipData;
import dev.latvian.mods.kubejs.tooltip.TooltipRequirements;
import dev.latvian.mods.kubejs.tooltip.action.DynamicTooltipAction;
import dev.latvian.mods.kubejs.text.action.DynamicTextAction;
import dev.latvian.mods.kubejs.text.tooltip.ItemTooltipData;
import dev.latvian.mods.kubejs.text.tooltip.TooltipRequirements;
import dev.latvian.mods.kubejs.util.Tristate;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.ImageButton;
Expand Down Expand Up @@ -118,7 +118,7 @@ public static boolean testRequirements(Minecraft mc, DynamicItemTooltipsKubeEven
private static void handleItemTooltips(Minecraft mc, ItemTooltipData tooltip, DynamicItemTooltipsKubeEvent event) {
if ((tooltip.filter().isEmpty() || tooltip.filter().get().test(event.item)) && (tooltip.requirements().isEmpty() || testRequirements(mc, event, tooltip.requirements().get()))) {
for (var action : tooltip.actions()) {
if (action instanceof DynamicTooltipAction dynamic) {
if (action instanceof DynamicTextAction dynamic) {
try {
ItemEvents.DYNAMIC_TOOLTIPS.post(ScriptType.CLIENT, dynamic.id(), event);
} catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import dev.latvian.mods.kubejs.net.KubeServerData;
import dev.latvian.mods.kubejs.recipe.viewer.server.RecipeViewerData;
import dev.latvian.mods.kubejs.recipe.viewer.server.RemoteRecipeViewerDataUpdatedEvent;
import dev.latvian.mods.kubejs.tooltip.ItemTooltipData;
import dev.latvian.mods.kubejs.text.tooltip.ItemTooltipData;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientPacketListener;
import net.minecraft.resources.ResourceLocation;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package dev.latvian.mods.kubejs.item;

import dev.latvian.mods.kubejs.event.KubeEvent;
import dev.latvian.mods.kubejs.tooltip.ItemTooltipData;
import dev.latvian.mods.kubejs.tooltip.TooltipActionBuilder;
import dev.latvian.mods.kubejs.tooltip.TooltipRequirements;
import dev.latvian.mods.kubejs.text.action.TextActionBuilder;
import dev.latvian.mods.kubejs.text.tooltip.ItemTooltipData;
import dev.latvian.mods.kubejs.text.tooltip.TooltipRequirements;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.crafting.Ingredient;
import org.jetbrains.annotations.Nullable;
Expand All @@ -19,29 +19,33 @@ public ModifyItemTooltipsKubeEvent(Consumer<ItemTooltipData> callback) {
this.callback = callback;
}

private void modify(@Nullable Ingredient filter, Optional<TooltipRequirements> requirements, Consumer<TooltipActionBuilder> consumer) {
var builder = new TooltipActionBuilder();
private void modify(@Nullable Ingredient filter, Optional<TooltipRequirements> requirements, Consumer<TextActionBuilder> consumer) {
var builder = new TextActionBuilder();
consumer.accept(builder);
callback.accept(new ItemTooltipData(filter == null || filter.isEmpty() || filter.kjs$isWildcard() ? Optional.empty() : Optional.of(filter), requirements, List.copyOf(builder.actions)));
}

public void modify(Ingredient filter, TooltipRequirements requirements, Consumer<TooltipActionBuilder> consumer) {
public void modify(Ingredient filter, TooltipRequirements requirements, Consumer<TextActionBuilder> consumer) {
modify(filter, Optional.ofNullable(requirements), consumer);
}

public void modify(Ingredient filter, Consumer<TooltipActionBuilder> consumer) {
public void modify(Ingredient filter, Consumer<TextActionBuilder> consumer) {
modify(filter, Optional.empty(), consumer);
}

public void modifyAll(TooltipRequirements requirements, Consumer<TooltipActionBuilder> consumer) {
public void modifyAll(TooltipRequirements requirements, Consumer<TextActionBuilder> consumer) {
modify(null, Optional.ofNullable(requirements), consumer);
}

public void modifyAll(Consumer<TooltipActionBuilder> consumer) {
public void modifyAll(Consumer<TextActionBuilder> consumer) {
modify(null, Optional.empty(), consumer);
}

public void add(Ingredient filter, List<Component> text) {
modify(filter, builder -> builder.add(text));
}

public void add(Ingredient filter, TooltipRequirements requirements, List<Component> text) {
modify(filter, requirements, builder -> builder.add(text));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import dev.latvian.mods.kubejs.kubedex.KubedexHighlight;
import dev.latvian.mods.kubejs.registry.RegistryObjectStorage;
import dev.latvian.mods.kubejs.script.ScriptType;
import dev.latvian.mods.kubejs.tooltip.ItemTooltipData;
import dev.latvian.mods.kubejs.text.tooltip.ItemTooltipData;
import dev.latvian.mods.kubejs.util.ID;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.renderer.ItemBlockRenderTypes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import dev.latvian.mods.kubejs.item.ModifyItemTooltipsKubeEvent;
import dev.latvian.mods.kubejs.recipe.viewer.server.RecipeViewerData;
import dev.latvian.mods.kubejs.script.ScriptType;
import dev.latvian.mods.kubejs.tooltip.ItemTooltipData;
import dev.latvian.mods.kubejs.text.tooltip.ItemTooltipData;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package dev.latvian.mods.kubejs.text.action;

import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentSerialization;
import net.minecraft.network.codec.ByteBufCodecs;

import java.util.List;

public record AddTextAction(List<Component> lines) implements TextAction {
public static final TooltipActionType<AddTextAction> TYPE = new TooltipActionType<>(1, ComponentSerialization.STREAM_CODEC.apply(ByteBufCodecs.list()).map(AddTextAction::new, AddTextAction::lines));

@Override
public TooltipActionType<?> type() {
return TYPE;
}

@Override
public void apply(List<Component> lines) {
lines.addAll(this.lines);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package dev.latvian.mods.kubejs.text.action;

import net.minecraft.network.chat.Component;
import net.minecraft.network.codec.StreamCodec;

import java.util.List;

public record ClearTextAction() implements TextAction {
public static final ClearTextAction INSTANCE = new ClearTextAction();
public static final TooltipActionType<ClearTextAction> TYPE = new TooltipActionType<>(6, StreamCodec.unit(INSTANCE));

@Override
public TooltipActionType<?> type() {
return TYPE;
}

@Override
public void apply(List<Component> lines) {
lines.clear();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.latvian.mods.kubejs.text.action;

import net.minecraft.network.chat.Component;
import net.minecraft.network.codec.ByteBufCodecs;

import java.util.List;

public record DynamicTextAction(String id) implements TextAction {
public static final TooltipActionType<DynamicTextAction> TYPE = new TooltipActionType<>(0, ByteBufCodecs.STRING_UTF8.map(DynamicTextAction::new, DynamicTextAction::id));

@Override
public TooltipActionType<?> type() {
return TYPE;
}

@Override
public void apply(List<Component> lines) {
lines.add(Component.literal("Not supported!").kjs$red());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package dev.latvian.mods.kubejs.text.action;

import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentSerialization;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;

import java.util.List;

public record InsertTextAction(int line, List<Component> lines) implements TextAction {
public static final TooltipActionType<InsertTextAction> TYPE = new TooltipActionType<>(2, StreamCodec.composite(
ByteBufCodecs.VAR_INT, InsertTextAction::line,
ComponentSerialization.STREAM_CODEC.apply(ByteBufCodecs.list()), InsertTextAction::lines,
InsertTextAction::new
));

@Override
public TooltipActionType<?> type() {
return TYPE;
}

@Override
public void apply(List<Component> lines) {
lines.addAll(line, this.lines);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.latvian.mods.kubejs.text.action;

import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentSerialization;

import java.util.List;

public record RemoveExactTextTextAction(Component match) implements TextAction {
public static final TooltipActionType<RemoveExactTextTextAction> TYPE = new TooltipActionType<>(5, ComponentSerialization.STREAM_CODEC.map(RemoveExactTextTextAction::new, RemoveExactTextTextAction::match));

@Override
public TooltipActionType<?> type() {
return TYPE;
}

@Override
public void apply(List<Component> lines) {
lines.removeIf(match::equals);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package dev.latvian.mods.kubejs.text.action;

import net.minecraft.network.chat.Component;
import net.minecraft.network.codec.ByteBufCodecs;

import java.util.List;

public record RemoveLineTextAction(int line) implements TextAction {
public static final TooltipActionType<RemoveLineTextAction> TYPE = new TooltipActionType<>(3, ByteBufCodecs.VAR_INT.map(RemoveLineTextAction::new, RemoveLineTextAction::line));

@Override
public TooltipActionType<?> type() {
return TYPE;
}

@Override
public void apply(List<Component> lines) {
if (line >= 0 && line < lines.size()) {
lines.remove(line);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package dev.latvian.mods.kubejs.tooltip.action;
package dev.latvian.mods.kubejs.text.action;

import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentContents;
import net.minecraft.network.chat.ComponentSerialization;

import java.util.List;

public record RemoveTextTooltipAction(Component match) implements TooltipAction {
public static final TooltipActionType<RemoveTextTooltipAction> TYPE = new TooltipActionType<>(4, ComponentSerialization.STREAM_CODEC.map(RemoveTextTooltipAction::new, RemoveTextTooltipAction::match));
public record RemoveTextTextAction(Component match) implements TextAction {
public static final TooltipActionType<RemoveTextTextAction> TYPE = new TooltipActionType<>(4, ComponentSerialization.STREAM_CODEC.map(RemoveTextTextAction::new, RemoveTextTextAction::match));

@Override
public TooltipActionType<?> type() {
return TYPE;
}

@Override
public void apply(List<Component> tooltip) {
tooltip.removeIf(component -> RemoveTextTooltipAction.equals(component, match.getContents()));
public void apply(List<Component> lines) {
lines.removeIf(component -> RemoveTextTextAction.equals(component, match.getContents()));
}

private static boolean equals(Component c, ComponentContents contents) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.latvian.mods.kubejs.tooltip.action;
package dev.latvian.mods.kubejs.text.action;

import dev.latvian.mods.kubejs.util.Cast;
import net.minecraft.network.RegistryFriendlyByteBuf;
Expand All @@ -11,31 +11,32 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

public interface TooltipAction {
public interface TextAction {
Map<Integer, TooltipActionType<?>> MAP = Stream.of(
DynamicTooltipAction.TYPE,
AddTooltipAction.TYPE,
InsertTooltipAction.TYPE,
RemoveLineTooltipAction.TYPE,
RemoveTextTooltipAction.TYPE,
RemoveExactTextTooltipAction.TYPE
DynamicTextAction.TYPE,
AddTextAction.TYPE,
InsertTextAction.TYPE,
RemoveLineTextAction.TYPE,
RemoveTextTextAction.TYPE,
RemoveExactTextTextAction.TYPE,
ClearTextAction.TYPE
).collect(Collectors.toMap(TooltipActionType::type, Function.identity()));

StreamCodec<RegistryFriendlyByteBuf, TooltipAction> STREAM_CODEC = new StreamCodec<>() {
StreamCodec<RegistryFriendlyByteBuf, TextAction> STREAM_CODEC = new StreamCodec<>() {
@Override
public TooltipAction decode(RegistryFriendlyByteBuf buf) {
public TextAction decode(RegistryFriendlyByteBuf buf) {
int id = buf.readByte();
return MAP.get(id).streamCodec().decode(buf);
}

@Override
public void encode(RegistryFriendlyByteBuf buf, TooltipAction value) {
public void encode(RegistryFriendlyByteBuf buf, TextAction value) {
buf.writeByte(value.type().type());
value.type().streamCodec().encode(buf, Cast.to(value));
}
};

TooltipActionType<?> type();

void apply(List<Component> tooltip);
void apply(List<Component> lines);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package dev.latvian.mods.kubejs.text.action;

import dev.latvian.mods.rhino.util.HideFromJS;
import net.minecraft.network.chat.Component;

import java.util.ArrayList;
import java.util.List;

public class TextActionBuilder {
@HideFromJS
public List<TextAction> actions = new ArrayList<>(1);

public void dynamic(String id) {
actions.add(new DynamicTextAction(id));
}

public void add(List<Component> text) {
actions.add(new AddTextAction(text));
}

public void insert(int line, List<Component> text) {
actions.add(new InsertTextAction(line, text));
}

public void removeLine(int line) {
actions.add(new RemoveLineTextAction(line));
}

public void removeText(Component match) {
actions.add(new RemoveTextTextAction(match));
}

public void removeExactText(Component match) {
actions.add(new RemoveExactTextTextAction(match));
}

public void clear() {
actions.add(ClearTextAction.INSTANCE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.latvian.mods.kubejs.text.action;

import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;

public record TooltipActionType<T extends TextAction>(int type, StreamCodec<? super RegistryFriendlyByteBuf, ? extends T> streamCodec) {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.latvian.mods.kubejs.tooltip;
package dev.latvian.mods.kubejs.text.tooltip;

import dev.latvian.mods.kubejs.tooltip.action.TooltipAction;
import dev.latvian.mods.kubejs.text.action.TextAction;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
Expand All @@ -12,12 +12,12 @@
public record ItemTooltipData(
Optional<Ingredient> filter,
Optional<TooltipRequirements> requirements,
List<TooltipAction> actions
List<TextAction> actions
) {
public static final StreamCodec<RegistryFriendlyByteBuf, ItemTooltipData> STREAM_CODEC = StreamCodec.composite(
ByteBufCodecs.optional(Ingredient.CONTENTS_STREAM_CODEC), ItemTooltipData::filter,
ByteBufCodecs.optional(TooltipRequirements.STREAM_CODEC), ItemTooltipData::requirements,
TooltipAction.STREAM_CODEC.apply(ByteBufCodecs.list()), ItemTooltipData::actions,
TextAction.STREAM_CODEC.apply(ByteBufCodecs.list()), ItemTooltipData::actions,
ItemTooltipData::new
);
}
Loading

0 comments on commit 383a24e

Please sign in to comment.