Skip to content

Commit

Permalink
Fixed item search using wrong cache keys, added render-icons query pa…
Browse files Browse the repository at this point in the history
…ram, custom item name providers
  • Loading branch information
LatvianModder committed Dec 25, 2024
1 parent 5162282 commit 622bdad
Show file tree
Hide file tree
Showing 8 changed files with 292 additions and 69 deletions.
81 changes: 81 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
import dev.latvian.mods.kubejs.util.JsonUtils;
import dev.latvian.mods.kubejs.util.KubeResourceLocation;
import dev.latvian.mods.kubejs.util.NBTIOWrapper;
import dev.latvian.mods.kubejs.util.NameProvider;
import dev.latvian.mods.kubejs.util.NotificationToastData;
import dev.latvian.mods.kubejs.util.RegExpKJS;
import dev.latvian.mods.kubejs.util.RegistryAccessContainer;
Expand All @@ -161,23 +162,29 @@
import dev.latvian.mods.kubejs.web.local.KubeJSWeb;
import dev.latvian.mods.rhino.type.RecordTypeInfo;
import dev.latvian.mods.rhino.type.TypeInfo;
import net.minecraft.Util;
import net.minecraft.commands.arguments.selector.EntitySelector;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Holder;
import net.minecraft.core.Vec3i;
import net.minecraft.core.component.DataComponentMap;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.CollectionTag;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.NbtOps;
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.ChatType;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.TextColor;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.stats.Stat;
import net.minecraft.stats.Stats;
Expand All @@ -190,6 +197,7 @@
import net.minecraft.world.damagesource.DamageType;
import net.minecraft.world.entity.MobCategory;
import net.minecraft.world.entity.animal.WolfVariant;
import net.minecraft.world.entity.decoration.Painting;
import net.minecraft.world.entity.decoration.PaintingVariant;
import net.minecraft.world.item.ArmorItem;
import net.minecraft.world.item.Item;
Expand All @@ -205,6 +213,7 @@
import net.minecraft.world.item.component.Fireworks;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.item.enchantment.ItemEnchantments;
import net.minecraft.world.item.enchantment.providers.EnchantmentProvider;
import net.minecraft.world.level.ItemLike;
Expand Down Expand Up @@ -735,4 +744,76 @@ public void registerDataComponentTypeDescriptions(DataComponentTypeInfoRegistry
public void registerLocalWebServer(LocalWebServerRegistry registry) {
KubeJSWeb.register(registry);
}

@Override
public void registerItemNameProviders(NameProvider.Registry<Item, ItemStack> registry) {
registry.register(Items.ENCHANTED_BOOK, (registries, stack) -> {
var enchants = EnchantmentHelper.getEnchantmentsForCrafting(stack);

if (enchants.isEmpty()) {
return null;
}

var c = Component.empty();
c.append(stack.getHoverName());
boolean first = true;

for (var e : enchants.entrySet()) {
if (first) {
first = false;
c.append(": ");
} else {
c.append(", ");
}

c.append(Enchantment.getFullname(e.getKey(), e.getIntValue()));
}

return c;
});

/*
registry.register(List.of(Items.POTION, Items.SPLASH_POTION, Items.LINGERING_POTION), (registries, stack) -> {
var potions = stack.get(DataComponents.POTION_CONTENTS);
if (potions == null || potions.potion().isEmpty()) {
return null;
}
var c = Component.empty();
c.append(stack.getHoverName());
// c.append(Potion.getName(potions.potion().get(), potions.));
return c;
});
*/

for (var item : BuiltInRegistries.ITEM) {
var song = item.components().get(DataComponents.JUKEBOX_PLAYABLE);

if (song != null) {
registry.register(item, (registries, stack) -> {
var key = Util.makeDescriptionId("jukebox_song", song.song().key().location());
return Component.empty().append(stack.getHoverName()).append(": ").append(Component.translatable(key));
});
}
}

registry.register(Items.PAINTING, (registries, stack) -> {
var customData = stack.getOrDefault(DataComponents.ENTITY_DATA, CustomData.EMPTY);

if (!customData.isEmpty()) {
var key = customData.read(registries.createSerializationContext(NbtOps.INSTANCE), Painting.VARIANT_MAP_CODEC)
.result()
.flatMap(Holder::unwrapKey)
.map(ResourceKey::location)
.orElse(null);

if (key != null) {
return Component.empty().append(stack.getHoverName()).append(": ").append(Component.translatable(key.toLanguageKey("painting", "author"))).append(" - ").append(Component.translatable(key.toLanguageKey("painting", "title")));
}
}

return null;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@
import dev.latvian.mods.kubejs.script.TypeWrapperRegistry;
import dev.latvian.mods.kubejs.server.DataExport;
import dev.latvian.mods.kubejs.util.AttachedData;
import dev.latvian.mods.kubejs.util.NameProvider;
import dev.latvian.mods.kubejs.web.LocalWebServerRegistry;
import dev.latvian.mods.rhino.Context;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.RecipeHolder;
import net.minecraft.world.level.Level;

Expand Down Expand Up @@ -96,6 +99,9 @@ default void registerDataComponentTypeDescriptions(DataComponentTypeInfoRegistry
default void registerLocalWebServer(LocalWebServerRegistry registry) {
}

default void registerItemNameProviders(NameProvider.Registry<Item, ItemStack> registry) {
}

default void attachServerData(AttachedData<MinecraftServer> event) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.material.Fluid;
import net.neoforged.neoforge.fluids.FluidStack;
import org.apache.commons.lang3.mutable.Mutable;
import org.apache.commons.lang3.mutable.MutableObject;

import java.util.UUID;

public record CachedComponentObject<T extends RegistryObjectKJS<T>, S>(UUID cacheKey, T value, S stack, DataComponentPatch components) {
public record CachedComponentObject<T extends RegistryObjectKJS<T>, S>(UUID cacheKey, T value, S stack, DataComponentPatch components, Mutable<String> iconPath) {
public static <T extends RegistryObjectKJS<T>> void writeCacheKey(FriendlyByteBuf buf, T value, DataComponentPatch components) {
buf.writeUtf(value.kjs$getId());
buf.writeVarInt(components.size());
Expand All @@ -33,7 +35,7 @@ public static <T extends RegistryObjectKJS<T>> void writeCacheKey(FriendlyByteBu
public static <T extends RegistryObjectKJS<T>, S> CachedComponentObject<T, S> of(T value, S stack, DataComponentPatch components) {
var buf = new FriendlyByteBuf(Unpooled.buffer());
writeCacheKey(buf, value, components);
return new CachedComponentObject<>(UUID.nameUUIDFromBytes(buf.array()), value, stack, components);
return new CachedComponentObject<>(UUID.nameUUIDFromBytes(buf.array()), value, stack, components, new MutableObject<>());
}

public static CachedComponentObject<Item, ItemStack> ofItemStack(ItemStack stack, boolean visual) {
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/util/NameProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package dev.latvian.mods.kubejs.util;

import net.minecraft.core.RegistryAccess;
import net.minecraft.network.chat.Component;
import org.jetbrains.annotations.Nullable;

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

public interface NameProvider<T> {
interface Registry<K, T> {
void register(K key, NameProvider<T> provider);

default void register(List<K> keys, NameProvider<T> provider) {
for (var key : keys) {
register(key, provider);
}
}
}

static <K, T> Map<K, NameProvider<T>> create(Consumer<Registry<K, T>> registry) {
var map = new HashMap<K, NameProvider<T>>();
registry.accept(map::put);
return map;
}

@Nullable
Component getName(RegistryAccess registries, T value);
}
7 changes: 7 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/web/KJSHTTPRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.serialization.DynamicOps;
import dev.latvian.apps.tinyserver.http.HTTPRequest;
import dev.latvian.apps.tinyserver.http.response.HTTPPayload;
import dev.latvian.apps.tinyserver.http.response.HTTPResponse;
import dev.latvian.mods.kubejs.component.DataComponentWrapper;
import dev.latvian.mods.kubejs.util.RegistryAccessContainer;
import net.minecraft.core.component.DataComponentPatch;
Expand Down Expand Up @@ -42,4 +44,9 @@ public DataComponentPatch components(DynamicOps<Tag> ops) throws CommandSyntaxEx
var str = query("components").asString();
return str.isEmpty() ? DataComponentPatch.EMPTY : DataComponentWrapper.patchOrEmptyOf(ops, "[" + str + "]");
}

@Override
public HTTPResponse handleResponse(HTTPPayload payload, HTTPResponse response, @Nullable Throwable error) {
return response.cors();
}
}
2 changes: 2 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/web/LocalWebServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -44,6 +45,7 @@ public static void start(BlockableEventLoop<?> eventLoop) {
registry.server.setPort(WebServerProperties.get().port);
registry.server.setMaxPortShift(10);
registry.server.setMaxKeepAliveConnections(3);
registry.server.setKeepAliveTimeout(Duration.ofMinutes(5L));

int port = registry.server.start();
var url = "http://localhost:" + port;
Expand Down
Loading

0 comments on commit 622bdad

Please sign in to comment.