Skip to content

Commit

Permalink
Added web api tag rendering, changed web api registry a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Aug 20, 2024
1 parent 6413fd4 commit 5c89d99
Show file tree
Hide file tree
Showing 23 changed files with 474 additions and 185 deletions.
9 changes: 9 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@ dependencies {
}

additionalRuntimeClasspath("dev.latvian.apps:tiny-java-server:$tiny_server_version")

jarJar(implementation 'com.madgag:animated-gif-lib') {
version {
strictly '[1.4,)'
prefer '1.4'
}
}

additionalRuntimeClasspath("com.madgag:animated-gif-lib:1.4")
}

processResources {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ neoforge_version=21.1.12
parchment_mc_version=1.21
parchment_mapping_version=2024.07.28
rhino_version=2100.2.5-build.46
tiny_server_version=1.0.0-build.3
tiny_server_version=1.0.0-build.6

architectury_version=13.0.6
rei_version=16.0.744
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import dev.latvian.apps.tinyserver.ServerRegistry;
import dev.latvian.mods.kubejs.bindings.AABBWrapper;
import dev.latvian.mods.kubejs.bindings.BlockWrapper;
import dev.latvian.mods.kubejs.bindings.ColorWrapper;
Expand Down Expand Up @@ -154,7 +153,7 @@
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.kubejs.web.KJSHTTPRequest;
import dev.latvian.mods.kubejs.web.LocalWebServerRegistry;
import dev.latvian.mods.kubejs.web.local.KubeJSWeb;
import dev.latvian.mods.rhino.type.RecordTypeInfo;
import dev.latvian.mods.rhino.type.TypeInfo;
Expand Down Expand Up @@ -723,7 +722,7 @@ public void registerDataComponentTypeDescriptions(DataComponentTypeInfoRegistry
}

@Override
public void registerLocalWebServer(ServerRegistry<KJSHTTPRequest> registry) {
public void registerLocalWebServer(LocalWebServerRegistry registry) {
KubeJSWeb.register(registry);
}
}
6 changes: 0 additions & 6 deletions src/main/java/dev/latvian/mods/kubejs/KubeJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import dev.latvian.mods.kubejs.script.ScriptType;
import dev.latvian.mods.kubejs.script.data.KubeFileResourcePack;
import dev.latvian.mods.kubejs.util.RecordDefaults;
import dev.latvian.mods.kubejs.web.KubeJSLocalWebServer;
import dev.latvian.mods.kubejs.web.WebServerProperties;
import net.minecraft.resources.ResourceLocation;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.bus.api.IEventBus;
Expand Down Expand Up @@ -161,9 +159,5 @@ public KubeJS(IEventBus bus, Dist dist, ModContainer mod) throws Throwable {

StartupEvents.INIT.post(ScriptType.STARTUP, KubeStartupEvent.BASIC);
// KubeJSRegistries.chunkGenerators().register(new ResourceLocation(KubeJS.MOD_ID, "flat"), () -> KJSFlatLevelSource.CODEC);

if (!datagen && WebServerProperties.get().enabled && (dist == Dist.CLIENT || !WebServerProperties.get().publicAddress.isEmpty())) {
KubeJSLocalWebServer.start();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package dev.latvian.mods.kubejs.client;

import dev.latvian.apps.tinyserver.ServerRegistry;
import dev.latvian.mods.kubejs.KubeJS;
import dev.latvian.mods.kubejs.bindings.event.ClientEvents;
import dev.latvian.mods.kubejs.event.EventGroupRegistry;
import dev.latvian.mods.kubejs.plugin.KubeJSPlugin;
import dev.latvian.mods.kubejs.script.BindingRegistry;
import dev.latvian.mods.kubejs.script.PlatformWrapper;
import dev.latvian.mods.kubejs.util.ScheduledEvents;
import dev.latvian.mods.kubejs.web.KJSHTTPRequest;
import dev.latvian.mods.kubejs.web.LocalWebServerRegistry;
import dev.latvian.mods.kubejs.web.local.client.KubeJSClientWeb;
import net.minecraft.client.Minecraft;
import net.neoforged.fml.ModList;
Expand All @@ -34,7 +33,7 @@ public void registerBindings(BindingRegistry bindings) {
}

@Override
public void registerLocalWebServer(ServerRegistry<KJSHTTPRequest> registry) {
public void registerLocalWebServer(LocalWebServerRegistry registry) {
KubeJSClientWeb.register(registry);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
import dev.latvian.mods.kubejs.item.ModifyItemTooltipsKubeEvent;
import dev.latvian.mods.kubejs.kubedex.KubedexHighlight;
import dev.latvian.mods.kubejs.registry.RegistryObjectStorage;
import dev.latvian.mods.kubejs.script.PlatformWrapper;
import dev.latvian.mods.kubejs.script.ScriptType;
import dev.latvian.mods.kubejs.text.tooltip.ItemTooltipData;
import dev.latvian.mods.kubejs.util.ID;
import dev.latvian.mods.kubejs.web.LocalWebServer;
import dev.latvian.mods.kubejs.web.WebServerProperties;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.ItemBlockRenderTypes;
Expand Down Expand Up @@ -65,6 +68,10 @@ public static void addClientPacks(AddPackFindersEvent event) {
}

private static void setupClient0() {
if (!PlatformWrapper.isGeneratingData() && Minecraft.getInstance() != null && WebServerProperties.get().enabled) {
LocalWebServer.start(Minecraft.getInstance());
}

ItemEvents.MODEL_PROPERTIES.post(ScriptType.STARTUP, new ItemModelPropertiesKubeEvent());

for (var builder : RegistryObjectStorage.BLOCK) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dev/latvian/mods/kubejs/core/BlockStateKJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import dev.latvian.mods.kubejs.recipe.match.Replaceable;
import dev.latvian.mods.kubejs.script.ScriptType;
import dev.latvian.mods.kubejs.util.ID;
import dev.latvian.mods.kubejs.web.KubeJSLocalWebServer;
import dev.latvian.mods.kubejs.web.LocalWebServer;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.rhino.util.RemapPrefixForJS;
import net.minecraft.core.BlockPos;
Expand Down Expand Up @@ -77,6 +77,6 @@ default Object replaceThisWith(Context cx, Object with) {

default String kjs$getWebIconURL(int size) {
var url = "/img/" + size + "/block/" + ID.url(kjs$getIdLocation());
return KubeJSLocalWebServer.getURL(url, Map.of());
return LocalWebServer.getURL(url, Map.of());
}
}
4 changes: 2 additions & 2 deletions src/main/java/dev/latvian/mods/kubejs/core/FluidStackKJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import dev.latvian.mods.kubejs.util.ID;
import dev.latvian.mods.kubejs.util.RegistryAccessContainer;
import dev.latvian.mods.kubejs.util.WithCodec;
import dev.latvian.mods.kubejs.web.KubeJSLocalWebServer;
import dev.latvian.mods.kubejs.web.LocalWebServer;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.rhino.util.SpecialEquality;
import net.minecraft.core.Holder;
Expand Down Expand Up @@ -148,6 +148,6 @@ default boolean matches(Context cx, FluidIngredient ingredient, boolean exact) {
default String kjs$getWebIconURL(DynamicOps<Tag> ops, int size) {
var url = "/img/" + size + "/fluid/" + ID.url(kjs$getIdLocation());
var c = DataComponentWrapper.patchToString(new StringBuilder(), ops, DataComponentWrapper.visualPatch(kjs$self().getComponentsPatch())).toString();
return KubeJSLocalWebServer.getURL(url, c.equals("[]") ? Map.of() : Map.of("components", c.substring(1, c.length() - 1)));
return LocalWebServer.getURL(url, c.equals("[]") ? Map.of() : Map.of("components", c.substring(1, c.length() - 1)));
}
}
4 changes: 2 additions & 2 deletions src/main/java/dev/latvian/mods/kubejs/core/ItemStackKJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import dev.latvian.mods.kubejs.util.ID;
import dev.latvian.mods.kubejs.util.RegistryAccessContainer;
import dev.latvian.mods.kubejs.util.WithCodec;
import dev.latvian.mods.kubejs.web.KubeJSLocalWebServer;
import dev.latvian.mods.kubejs.web.LocalWebServer;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.rhino.util.RemapPrefixForJS;
import dev.latvian.mods.rhino.util.ReturnsSelf;
Expand Down Expand Up @@ -282,6 +282,6 @@ default boolean matches(Context cx, ItemLike itemLike, boolean exact) {
default String kjs$getWebIconURL(DynamicOps<Tag> ops, int size) {
var url = "/img/" + size + "/item/" + ID.url(kjs$getIdLocation());
var c = DataComponentWrapper.patchToString(new StringBuilder(), ops, DataComponentWrapper.visualPatch(kjs$self().getComponentsPatch())).toString();
return KubeJSLocalWebServer.getURL(url, c.equals("[]") ? Map.of() : Map.of("components", c.substring(1, c.length() - 1)));
return LocalWebServer.getURL(url, c.equals("[]") ? Map.of() : Map.of("components", c.substring(1, c.length() - 1)));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.latvian.mods.kubejs.plugin;

import com.google.gson.JsonElement;
import dev.latvian.apps.tinyserver.ServerRegistry;
import dev.latvian.mods.kubejs.block.entity.BlockEntityAttachmentType;
import dev.latvian.mods.kubejs.client.LangKubeEvent;
import dev.latvian.mods.kubejs.core.RecipeManagerKJS;
Expand All @@ -25,7 +24,7 @@
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.web.KJSHTTPRequest;
import dev.latvian.mods.kubejs.web.LocalWebServerRegistry;
import dev.latvian.mods.rhino.Context;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
Expand Down Expand Up @@ -95,7 +94,7 @@ default void registerRecipeViewerEntryTypes(Consumer<RecipeViewerEntryType> regi
default void registerDataComponentTypeDescriptions(DataComponentTypeInfoRegistry registry) {
}

default void registerLocalWebServer(ServerRegistry<KJSHTTPRequest> registry) {
default void registerLocalWebServer(LocalWebServerRegistry registry) {
}

default void attachServerData(AttachedData<MinecraftServer> event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public IngredientActionType<?> getType() {

@Override
public ItemStack transform(ItemStack old, int index, CraftingInput input) {
old.setCount(1);
return old;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,16 @@
import dev.latvian.mods.kubejs.entity.AttributeBuilder;
import dev.latvian.mods.kubejs.script.ConsoleJS;
import dev.latvian.mods.kubejs.script.ScriptType;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.neoforged.bus.api.EventPriority;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.event.entity.EntityAttributeModificationEvent;
import net.neoforged.neoforge.registries.RegisterEvent;

import java.util.List;
import java.util.function.Predicate;

@EventBusSubscriber(modid = KubeJS.MOD_ID, bus = EventBusSubscriber.Bus.MOD)
public class RegistryEventHandler {
@SuppressWarnings({"rawtypes", "unchecked"})
Expand All @@ -34,8 +28,7 @@ public static void registerAll(RegisterEvent event) {
@SubscribeEvent(priority = EventPriority.LOW)
public static void registerEntityAttributes(EntityAttributeModificationEvent event) {
var objStorage = RegistryObjectStorage.of(Registries.ATTRIBUTE);
List<Pair<Predicate<EntityType<?>>, Holder<Attribute>>> predicatePair = objStorage.objects.values().stream().filter(AttributeBuilder.class::isInstance).map(AttributeBuilder.class::cast).flatMap(b -> b.getPredicateList().stream().map(p -> Pair.of(p, BuiltInRegistries.ATTRIBUTE.wrapAsHolder(b.get())))).toList();

var predicatePair = objStorage.objects.values().stream().filter(AttributeBuilder.class::isInstance).map(AttributeBuilder.class::cast).flatMap(b -> b.getPredicateList().stream().map(p -> Pair.of(p, BuiltInRegistries.ATTRIBUTE.wrapAsHolder(b.get())))).toList();
event.getTypes().forEach(type -> predicatePair.stream().filter(p -> p.getFirst().test(type)).forEach(p -> event.add(type, p.getSecond())));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
import dev.latvian.mods.kubejs.command.CommandRegistryKubeEvent;
import dev.latvian.mods.kubejs.command.KubeJSCommands;
import dev.latvian.mods.kubejs.level.SimpleLevelKubeEvent;
import dev.latvian.mods.kubejs.script.PlatformWrapper;
import dev.latvian.mods.kubejs.script.ScriptType;
import dev.latvian.mods.kubejs.util.RegistryAccessContainer;
import dev.latvian.mods.kubejs.web.LocalWebServer;
import dev.latvian.mods.kubejs.web.WebServerProperties;
import net.minecraft.Util;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
Expand All @@ -17,8 +20,10 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.storage.LevelResource;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.fml.loading.FMLEnvironment;
import net.neoforged.neoforge.event.AddReloadListenerEvent;
import net.neoforged.neoforge.event.CommandEvent;
import net.neoforged.neoforge.event.RegisterCommandsEvent;
Expand Down Expand Up @@ -49,6 +54,10 @@ public static void registerCommands(RegisterCommandsEvent event) {
public static void serverBeforeStart(ServerAboutToStartEvent event) {
var server = event.getServer();

if (FMLEnvironment.dist == Dist.DEDICATED_SERVER && !PlatformWrapper.isGeneratingData() && WebServerProperties.get().enabled && !WebServerProperties.get().publicAddress.isEmpty()) {
LocalWebServer.start(server);
}

var p = server.getWorldPath(PERSISTENT_DATA);

if (Files.exists(p)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
import java.util.UUID;

public record CachedComponentObject<T extends RegistryObjectKJS<T>, S>(UUID cacheKey, T value, S stack, DataComponentPatch components) {
public static <T extends RegistryObjectKJS<T>, S> CachedComponentObject<T, S> of(T value, S stack, DataComponentPatch components) {
var buf = new FriendlyByteBuf(Unpooled.buffer());
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 @@ -29,7 +28,11 @@ public static <T extends RegistryObjectKJS<T>, S> CachedComponentObject<T, S> of
buf.writeVarInt(entry.getValue().get().hashCode());
}
}
}

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);
}

Expand Down
16 changes: 10 additions & 6 deletions src/main/java/dev/latvian/mods/kubejs/web/KJSHTTPRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,35 @@
import dev.latvian.apps.tinyserver.http.HTTPRequest;
import dev.latvian.mods.kubejs.component.DataComponentWrapper;
import dev.latvian.mods.kubejs.util.RegistryAccessContainer;
import net.minecraft.client.Minecraft;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.nbt.Tag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.thread.BlockableEventLoop;
import org.jetbrains.annotations.Nullable;

import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;

public class KJSHTTPRequest extends HTTPRequest {
public BlockableEventLoop<?> eventLoop;

public RegistryAccessContainer registries() {
return RegistryAccessContainer.current;
}

public void runInRenderThread(Runnable task) {
Minecraft.getInstance().executeBlocking(task);
public void runInMainThread(Runnable task) {
eventLoop.executeBlocking(task);
}

public <T> T supplyInRenderThread(Supplier<T> task) {
return CompletableFuture.supplyAsync(task, Minecraft.getInstance()).join();
public <T> T supplyInMainThread(Supplier<T> task) {
return CompletableFuture.supplyAsync(task, eventLoop).join();
}

public ResourceLocation id(String ns, String path) {
return ResourceLocation.fromNamespaceAndPath(variables().get(ns), variables().get(path));
return ResourceLocation.fromNamespaceAndPath(variable(ns), variable(path));
}

@Nullable
public ResourceLocation id() {
return id("namespace", "path");
}
Expand Down
Loading

0 comments on commit 5c89d99

Please sign in to comment.