Skip to content

Commit

Permalink
Reworked item tooltip event, removed chanced item
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Jul 23, 2024
1 parent 7f88b8e commit dca1a11
Show file tree
Hide file tree
Showing 43 changed files with 710 additions and 327 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
import dev.latvian.mods.kubejs.fluid.ThickFluidBuilder;
import dev.latvian.mods.kubejs.fluid.ThinFluidBuilder;
import dev.latvian.mods.kubejs.item.ArmorMaterialBuilder;
import dev.latvian.mods.kubejs.item.ChancedItem;
import dev.latvian.mods.kubejs.item.ItemEnchantmentsWrapper;
import dev.latvian.mods.kubejs.item.ItemPredicate;
import dev.latvian.mods.kubejs.item.ItemStackJS;
Expand Down Expand Up @@ -148,6 +147,7 @@
import dev.latvian.mods.kubejs.util.ScheduledEvents;
import dev.latvian.mods.kubejs.util.SlotFilter;
import dev.latvian.mods.kubejs.util.TimeJS;
import dev.latvian.mods.kubejs.util.Tristate;
import dev.latvian.mods.kubejs.util.UtilsJS;
import dev.latvian.mods.kubejs.util.registrypredicate.RegistryPredicate;
import dev.latvian.mods.rhino.type.RecordTypeInfo;
Expand Down Expand Up @@ -484,7 +484,6 @@ public void registerBindings(BindingRegistry bindings) {
bindings.add("FluidAmounts", FluidAmounts.class);
bindings.add("Notification", NotificationToastData.class);
bindings.add("SizedIngredient", SizedIngredientWrapper.class);
bindings.add("ChancedItem", ChancedItem.class);
bindings.add("ParticleOptions", ParticleOptionsWrapper.class);
bindings.add("Registry", RegistryWrapper.class);

Expand Down Expand Up @@ -592,7 +591,7 @@ public void registerTypeWrappers(TypeWrapperRegistry registry) {
registry.register(ParticleOptions.class, ParticleOptionsWrapper::wrap);
registry.register(ItemTintFunction.class, ItemTintFunction::of);
registry.register(BlockTintFunction.class, BlockTintFunction::of);
registry.register(ChancedItem.class, ChancedItem::wrap);
registry.register(Tristate.class, Tristate::wrap);

// components //
registry.register(Component.class, TextWrapper::of);
Expand Down Expand Up @@ -648,8 +647,6 @@ public void registerRecipeComponents(RecipeComponentFactoryRegistry registry) {
registry.register(SizedFluidIngredientComponent.FLAT);
registry.register(SizedFluidIngredientComponent.NESTED);

registry.register(ChancedItem.RECIPE_COMPONENT);

registry.register(BlockComponent.BLOCK);

registry.register(BlockStateComponent.BLOCK);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/KubeJSCommon.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.latvian.mods.kubejs;

import dev.latvian.mods.kubejs.net.KubeServerData;
import dev.latvian.mods.kubejs.script.ConsoleLine;
import dev.latvian.mods.kubejs.script.ScriptType;
import dev.latvian.mods.kubejs.script.data.ExportablePackResources;
Expand Down Expand Up @@ -58,4 +59,7 @@ public void runInMainThread(Runnable runnable) {
runnable.run();
}
}

public void updateServerData(KubeServerData data) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import dev.latvian.mods.kubejs.event.EventHandler;
import dev.latvian.mods.kubejs.event.EventTargetType;
import dev.latvian.mods.kubejs.event.TargetedEventHandler;
import dev.latvian.mods.kubejs.item.DynamicItemTooltipsKubeEvent;
import dev.latvian.mods.kubejs.item.FoodEatenKubeEvent;
import dev.latvian.mods.kubejs.item.ItemClickedKubeEvent;
import dev.latvian.mods.kubejs.item.ItemCraftedKubeEvent;
Expand All @@ -15,7 +16,7 @@
import dev.latvian.mods.kubejs.item.ItemPickedUpKubeEvent;
import dev.latvian.mods.kubejs.item.ItemSmeltedKubeEvent;
import dev.latvian.mods.kubejs.item.ItemStackJS;
import dev.latvian.mods.kubejs.item.ItemTooltipKubeEvent;
import dev.latvian.mods.kubejs.item.ModifyItemTooltipsKubeEvent;
import dev.latvian.mods.kubejs.item.custom.ItemToolTierRegistryKubeEvent;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
Expand All @@ -35,7 +36,8 @@ public interface ItemEvents {
TargetedEventHandler<ResourceKey<Item>> CRAFTED = GROUP.common("crafted", () -> ItemCraftedKubeEvent.class).supportsTarget(TARGET);
TargetedEventHandler<ResourceKey<Item>> SMELTED = GROUP.common("smelted", () -> ItemSmeltedKubeEvent.class).supportsTarget(TARGET);
TargetedEventHandler<ResourceKey<Item>> FOOD_EATEN = GROUP.common("foodEaten", () -> FoodEatenKubeEvent.class).hasResult().supportsTarget(TARGET);
EventHandler TOOLTIP = GROUP.client("tooltip", () -> ItemTooltipKubeEvent.class);
EventHandler MODIFY_TOOLTIPS = GROUP.common("modifyTooltips", () -> ModifyItemTooltipsKubeEvent.class);
TargetedEventHandler<String> DYNAMIC_TOOLTIPS = GROUP.client("dynamicTooltips", () -> DynamicItemTooltipsKubeEvent.class).requiredTarget(EventTargetType.STRING);
EventHandler MODEL_PROPERTIES = GROUP.startup("modelProperties", () -> ItemModelPropertiesKubeEvent.class);
TargetedEventHandler<ResourceKey<Item>> FIRST_RIGHT_CLICKED = GROUP.common("firstRightClicked", () -> ItemClickedKubeEvent.class).supportsTarget(TARGET);
TargetedEventHandler<ResourceKey<Item>> FIRST_LEFT_CLICKED = GROUP.common("firstLeftClicked", () -> ItemClickedKubeEvent.class).supportsTarget(TARGET);
Expand Down
19 changes: 18 additions & 1 deletion src/main/java/dev/latvian/mods/kubejs/client/KubeJSClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
import dev.latvian.mods.kubejs.KubeJS;
import dev.latvian.mods.kubejs.KubeJSCommon;
import dev.latvian.mods.kubejs.KubeJSPaths;
import dev.latvian.mods.kubejs.bindings.event.ItemEvents;
import dev.latvian.mods.kubejs.bindings.event.NetworkEvents;
import dev.latvian.mods.kubejs.item.ModifyItemTooltipsKubeEvent;
import dev.latvian.mods.kubejs.kubedex.KubedexHighlight;
import dev.latvian.mods.kubejs.net.KubeServerData;
import dev.latvian.mods.kubejs.net.NetworkKubeEvent;
import dev.latvian.mods.kubejs.script.ConsoleLine;
import dev.latvian.mods.kubejs.script.ScriptType;
import dev.latvian.mods.kubejs.script.data.ExportablePackResources;
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 net.minecraft.SharedConstants;
import net.minecraft.Util;
import net.minecraft.client.Minecraft;
Expand All @@ -36,6 +40,7 @@
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
Expand All @@ -46,6 +51,7 @@ public class KubeJSClient extends KubeJSCommon {
public static final ResourceLocation RECIPE_BUTTON_TEXTURE = ResourceLocation.parse("textures/gui/recipe_button.png");

public static final Map<GeneratedDataStage, VirtualAssetPack> CLIENT_PACKS = new EnumMap<>(GeneratedDataStage.class);
public static List<ItemTooltipData> clientItemTooltips = List.of();

static {
for (var stage : GeneratedDataStage.values()) {
Expand All @@ -59,8 +65,10 @@ public void reloadClientInternal() {
}

public static void reloadClientScripts() {
KubeJSClientEventHandler.staticItemTooltips = null;
KubeJS.getClientScriptManager().reload();
var list = new ArrayList<ItemTooltipData>();
ItemEvents.MODIFY_TOOLTIPS.post(ScriptType.CLIENT, new ModifyItemTooltipsKubeEvent(list::add));
clientItemTooltips = List.copyOf(list);
}

public static void copyDefaultOptionsFile(File optionsFile) {
Expand Down Expand Up @@ -177,6 +185,15 @@ public void runInMainThread(Runnable runnable) {
}
}

@Override
public void updateServerData(KubeServerData data) {
var sessionData = KubeSessionData.of(Minecraft.getInstance());

if (sessionData != null) {
sessionData.sync(data);
}
}

public static void loadPostChains(Minecraft mc) {
KubedexHighlight.INSTANCE.loadPostChains(mc);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
import dev.latvian.mods.kubejs.bindings.TextIcons;
import dev.latvian.mods.kubejs.bindings.event.ClientEvents;
import dev.latvian.mods.kubejs.bindings.event.ItemEvents;
import dev.latvian.mods.kubejs.item.ItemTooltipKubeEvent;
import dev.latvian.mods.kubejs.item.DynamicItemTooltipsKubeEvent;
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.util.Tristate;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.ImageButton;
import net.minecraft.client.gui.screens.Screen;
Expand All @@ -26,8 +30,6 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.BucketItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.SpawnEggItem;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.world.level.material.Fluids;
Expand All @@ -45,15 +47,10 @@
import net.neoforged.neoforge.event.entity.player.ItemTooltipEvent;
import org.jetbrains.annotations.Nullable;

import java.util.IdentityHashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@EventBusSubscriber(modid = KubeJS.MOD_ID, value = Dist.CLIENT)
public class KubeJSClientEventHandler {
public static Map<Item, List<ItemTooltipKubeEvent.StaticTooltipHandler>> staticItemTooltips = null;

@SubscribeEvent
public static void debugInfo(CustomizeGuiOverlayEvent.DebugText event) {
var mc = Minecraft.getInstance();
Expand Down Expand Up @@ -84,18 +81,82 @@ private static <T> void appendComponentValue(DynamicOps<Tag> ops, MutableCompone
}
}

@SubscribeEvent
public static boolean testRequirements(Minecraft mc, DynamicItemTooltipsKubeEvent event, TooltipRequirements r) {
if (!r.advanced().test(event.advanced)) {
return false;
}

if (!r.creative().test(event.creative)) {
return false;
}

if (!r.shift().test(event.shift)) {
return false;
}

if (!r.ctrl().test(event.ctrl)) {
return false;
}

if (!r.alt().test(event.alt)) {
return false;
}

if (!r.stages().isEmpty()) {
var stages = mc.player.kjs$getStages();

for (var entry : r.stages().entrySet()) {
if (entry.getValue() != Tristate.DEFAULT && !entry.getValue().test(stages.has(entry.getKey()))) {
return false;
}
}
}

return true;
}

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) {
try {
ItemEvents.DYNAMIC_TOOLTIPS.post(ScriptType.CLIENT, dynamic.id(), event);
} catch (Exception ex) {
ConsoleJS.CLIENT.error("Item " + event.item.kjs$getId() + " dynamic tooltip error", ex);
}
} else {
action.apply(event.lines);
}
}
}
}

@SubscribeEvent(priority = EventPriority.LOW)
public static void onItemTooltip(ItemTooltipEvent event) {
var mc = Minecraft.getInstance();
var stack = event.getItemStack();
var lines = event.getToolTip();
var flag = event.getFlags();

if (stack.isEmpty()) {
return;
}

var advanced = flag.isAdvanced();
var mc = Minecraft.getInstance();
var lines = event.getToolTip();
var flags = event.getFlags();
var sessionData = KubeSessionData.of(mc);

var dynamicEvent = new DynamicItemTooltipsKubeEvent(stack, flags, lines, sessionData == null);

for (var tooltip : KubeJSClient.clientItemTooltips) {
handleItemTooltips(mc, tooltip, dynamicEvent);
}

if (sessionData != null) {
for (var tooltip : sessionData.itemTooltips) {
handleItemTooltips(mc, tooltip, dynamicEvent);
}
}

var advanced = flags.isAdvanced();

if (mc.level != null && advanced && ClientProperties.get().showComponents && Screen.hasAltDown()) {
var components = BuiltInRegistries.DATA_COMPONENT_TYPE;
Expand Down Expand Up @@ -165,36 +226,6 @@ public static void onItemTooltip(ItemTooltipEvent event) {
lines.add(instance.toText());
}
}

if (staticItemTooltips == null) {
var staticItemTooltips0 = new IdentityHashMap<Item, List<ItemTooltipKubeEvent.StaticTooltipHandler>>();
ItemEvents.TOOLTIP.post(ScriptType.CLIENT, new ItemTooltipKubeEvent(staticItemTooltips0));
staticItemTooltips = staticItemTooltips0;
}

try {
var handlers = staticItemTooltips.get(Items.AIR);

if (handlers != null && !handlers.isEmpty()) {
for (var handler : handlers) {
handler.tooltip(stack, advanced, lines);
}
}
} catch (Exception ex) {
ConsoleJS.CLIENT.error("Error while gathering tooltip for " + stack, ex);
}

try {
var handlers = staticItemTooltips.get(stack.getItem());

if (handlers != null && !handlers.isEmpty()) {
for (var handler : handlers) {
handler.tooltip(stack, advanced, lines);
}
}
} catch (Exception ex) {
ConsoleJS.CLIENT.error("Error while gathering tooltip for " + stack, ex);
}
}

@SubscribeEvent
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/client/KubeSessionData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package dev.latvian.mods.kubejs.client;

import dev.latvian.mods.kubejs.core.ClientPacketListenerKJS;
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 net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientPacketListener;
import net.minecraft.resources.ResourceLocation;
import net.neoforged.neoforge.common.NeoForge;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class KubeSessionData {
@Nullable
public static KubeSessionData of(ClientPacketListener listener) {
return listener == null ? null : ((ClientPacketListenerKJS) listener).kjs$sessionData();
}

@Nullable
public static KubeSessionData of(Minecraft mc) {
return mc == null ? null : of(mc.getConnection());
}

public ResourceLocation activePostShader = null;
public RecipeViewerData recipeViewerData = null;
public List<ItemTooltipData> itemTooltips = List.of();

public void sync(KubeServerData data) {
recipeViewerData = data.recipeViewerData().orElse(null);
itemTooltips = List.copyOf(data.itemTooltipData());

NeoForge.EVENT_BUS.post(new RemoteRecipeViewerDataUpdatedEvent(recipeViewerData));
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package dev.latvian.mods.kubejs.core;

import net.minecraft.resources.ResourceLocation;
import org.apache.commons.lang3.mutable.Mutable;
import dev.latvian.mods.kubejs.client.KubeSessionData;

public interface ClientPacketListenerKJS {
default Mutable<ResourceLocation> kjs$activePostShader() {
default KubeSessionData kjs$sessionData() {
throw new NoMixinException();
}
}
6 changes: 0 additions & 6 deletions src/main/java/dev/latvian/mods/kubejs/core/ItemStackKJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.mojang.serialization.Codec;
import com.mojang.serialization.DynamicOps;
import dev.latvian.mods.kubejs.bindings.DataComponentWrapper;
import dev.latvian.mods.kubejs.item.ChancedItem;
import dev.latvian.mods.kubejs.item.ItemStackJS;
import dev.latvian.mods.kubejs.level.BlockContainerJS;
import dev.latvian.mods.kubejs.recipe.match.ItemMatch;
Expand All @@ -25,7 +24,6 @@
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.valueproviders.FloatProvider;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
Expand Down Expand Up @@ -248,10 +246,6 @@ default Codec<ItemStack> getCodec(Context cx) {
return ItemStack.CODEC;
}

default ChancedItem kjs$withChance(FloatProvider chance) {
return new ChancedItem(kjs$self(), chance);
}

@ReturnsSelf(copy = true)
default ItemStack kjs$withLore(Component[] lines) {
var is = kjs$self().copy();
Expand Down
Loading

0 comments on commit dca1a11

Please sign in to comment.