From e803f55dd26d58aee14fd3e292d7af814c228f19 Mon Sep 17 00:00:00 2001 From: LatvianModder Date: Sat, 27 Jul 2024 17:08:33 +0300 Subject: [PATCH] Fluid asset generation cleanup, tint will now produce a real texture, replaced IdentityHashMap with Reference2ObjectOpenHashMap --- gradle.properties | 2 +- .../mods/kubejs/BuiltinKubeJSPlugin.java | 4 +- .../mods/kubejs/bindings/AABBWrapper.java | 3 - .../kubejs/bindings/DataComponentWrapper.java | 4 +- .../kubejs/bindings/DirectionWrapper.java | 3 - .../mods/kubejs/bindings/UUIDWrapper.java | 3 - .../client/KubeJSGameClientEventHandler.java | 6 +- .../client/KubeJSModClientEventHandler.java | 41 ++++++++++-- .../mods/kubejs/client/LoadedTexture.java | 35 +++++++++++ .../mods/kubejs/core/LivingEntityKJS.java | 10 +-- .../mods/kubejs/core/TagLoaderKJS.java | 24 ------- .../kubejs/event/TargetedEventHandler.java | 4 +- .../mods/kubejs/fluid/FluidBlockBuilder.java | 5 -- .../kubejs/fluid/FluidBucketItemBuilder.java | 25 -------- .../mods/kubejs/fluid/FluidBuilder.java | 55 +++++++++++++--- .../mods/kubejs/fluid/FluidTypeBuilder.java | 39 ++---------- .../kubejs/generator/KubeAssetGenerator.java | 4 -- .../generator/KubeResourceGenerator.java | 4 ++ .../jei/JEIRemoveRecipesKubeEvent.java | 4 +- .../latvian/mods/kubejs/item/ItemBuilder.java | 2 +- .../mods/kubejs/misc/BasicMobEffect.java | 48 +++++++------- .../mods/kubejs/misc/MobEffectBuilder.java | 25 ++++---- .../mods/kubejs/recipe/CachedTagLookup.java | 11 ++-- .../mods/kubejs/recipe/RecipesKubeEvent.java | 4 +- .../recipe/component/ComponentValueMap.java | 5 +- .../recipe/schema/JsonRecipeSchemaLoader.java | 6 +- .../recipe/schema/RecipeConstructor.java | 4 +- .../registry/BuilderTypeRegistryHandler.java | 4 +- .../registry/RegistryObjectStorage.java | 7 ++- .../mods/kubejs/registry/RegistryType.java | 6 +- .../kubejs/script/data/VirtualAssetPack.java | 11 +++- .../script/data/VirtualResourcePack.java | 6 ++ .../kubejs/server/ServerScriptManager.java | 10 ++- .../mods/kubejs/util/ChangeListener.java | 9 --- .../latvian/mods/kubejs/util/CountingMap.java | 3 - .../latvian/mods/kubejs/util/JsonUtils.java | 3 - .../kubejs/util/RegistryAccessContainer.java | 62 ++++++++++++++----- .../kubejs/util/StringBuilderAppendable.java | 3 - 38 files changed, 275 insertions(+), 229 deletions(-) delete mode 100644 src/main/java/dev/latvian/mods/kubejs/util/ChangeListener.java diff --git a/gradle.properties b/gradle.properties index fcd6f9be8..4f7fb8993 100644 --- a/gradle.properties +++ b/gradle.properties @@ -12,7 +12,7 @@ modrinth_id=umyGl7zF minecraft_version=1.21 mod_version=2100.7.0 -neoforge_version=21.0.114-beta +neoforge_version=21.0.139-beta parchment_version=2024.07.07 rhino_version=2100.2.5-build.44 diff --git a/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java b/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java index be4917f3f..ea4ad4017 100644 --- a/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java +++ b/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java @@ -73,8 +73,8 @@ import dev.latvian.mods.kubejs.item.custom.SmithingTemplateItemBuilder; import dev.latvian.mods.kubejs.item.custom.SwordItemBuilder; import dev.latvian.mods.kubejs.item.ingredient.IngredientJS; -import dev.latvian.mods.kubejs.misc.BasicMobEffect; import dev.latvian.mods.kubejs.misc.CustomStatBuilder; +import dev.latvian.mods.kubejs.misc.MobEffectBuilder; import dev.latvian.mods.kubejs.misc.PaintingVariantBuilder; import dev.latvian.mods.kubejs.misc.ParticleTypeBuilder; import dev.latvian.mods.kubejs.misc.PoiTypeBuilder; @@ -296,7 +296,7 @@ public void registerBuilderTypes(BuilderTypeRegistry registry) { }); registry.addDefault(NeoForgeRegistries.Keys.FLUID_TYPES, FluidTypeBuilder.class, FluidTypeBuilder::new); - registry.addDefault(Registries.MOB_EFFECT, BasicMobEffect.Builder.class, BasicMobEffect.Builder::new); + registry.addDefault(Registries.MOB_EFFECT, MobEffectBuilder.class, MobEffectBuilder::new); registry.addDefault(Registries.POTION, PotionBuilder.class, PotionBuilder::new); registry.addDefault(Registries.PARTICLE_TYPE, ParticleTypeBuilder.class, ParticleTypeBuilder::new); registry.addDefault(Registries.CUSTOM_STAT, CustomStatBuilder.class, CustomStatBuilder::new); diff --git a/src/main/java/dev/latvian/mods/kubejs/bindings/AABBWrapper.java b/src/main/java/dev/latvian/mods/kubejs/bindings/AABBWrapper.java index 352fc49a7..5bf7e222f 100644 --- a/src/main/java/dev/latvian/mods/kubejs/bindings/AABBWrapper.java +++ b/src/main/java/dev/latvian/mods/kubejs/bindings/AABBWrapper.java @@ -4,9 +4,6 @@ import net.minecraft.world.phys.AABB; import net.minecraft.world.phys.Vec3; -/** - * @author LatvianModder - */ public interface AABBWrapper { AABB EMPTY = new AABB(0D, 0D, 0D, 0D, 0D, 0D); AABB CUBE = new AABB(0D, 0D, 0D, 1D, 1D, 1D); diff --git a/src/main/java/dev/latvian/mods/kubejs/bindings/DataComponentWrapper.java b/src/main/java/dev/latvian/mods/kubejs/bindings/DataComponentWrapper.java index c19949b6d..af0ca7341 100644 --- a/src/main/java/dev/latvian/mods/kubejs/bindings/DataComponentWrapper.java +++ b/src/main/java/dev/latvian/mods/kubejs/bindings/DataComponentWrapper.java @@ -14,6 +14,7 @@ import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import dev.latvian.mods.rhino.NativeJavaMap; import dev.latvian.mods.rhino.type.TypeInfo; +import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap; import net.minecraft.core.component.DataComponentMap; import net.minecraft.core.component.DataComponentPatch; import net.minecraft.core.component.DataComponentPredicate; @@ -28,7 +29,6 @@ import java.lang.reflect.Modifier; import java.lang.reflect.ParameterizedType; -import java.util.IdentityHashMap; import java.util.Map; public interface DataComponentWrapper { @@ -36,7 +36,7 @@ public interface DataComponentWrapper { Dynamic2CommandExceptionType ERROR_MALFORMED_COMPONENT = new Dynamic2CommandExceptionType((object, object2) -> Component.translatableEscape("arguments.item.component.malformed", object, object2)); SimpleCommandExceptionType ERROR_EXPECTED_COMPONENT = new SimpleCommandExceptionType(Component.translatable("arguments.item.component.expected")); Lazy, TypeInfo>> TYPE_INFOS = Lazy.of(() -> { - var map = new IdentityHashMap, TypeInfo>(); + var map = new Reference2ObjectOpenHashMap, TypeInfo>(); try { for (var field : DataComponents.class.getDeclaredFields()) { diff --git a/src/main/java/dev/latvian/mods/kubejs/bindings/DirectionWrapper.java b/src/main/java/dev/latvian/mods/kubejs/bindings/DirectionWrapper.java index e07f808ee..d13958503 100644 --- a/src/main/java/dev/latvian/mods/kubejs/bindings/DirectionWrapper.java +++ b/src/main/java/dev/latvian/mods/kubejs/bindings/DirectionWrapper.java @@ -8,9 +8,6 @@ import java.util.function.Function; import java.util.stream.Collectors; -/** - * @author LatvianModder - */ public interface DirectionWrapper { Direction down = Direction.DOWN; Direction up = Direction.UP; diff --git a/src/main/java/dev/latvian/mods/kubejs/bindings/UUIDWrapper.java b/src/main/java/dev/latvian/mods/kubejs/bindings/UUIDWrapper.java index 119ad6195..71eaa1dd3 100644 --- a/src/main/java/dev/latvian/mods/kubejs/bindings/UUIDWrapper.java +++ b/src/main/java/dev/latvian/mods/kubejs/bindings/UUIDWrapper.java @@ -4,9 +4,6 @@ import java.util.UUID; -/** - * @author LatvianModder - */ public interface UUIDWrapper { static String toString(@Nullable UUID id) { if (id != null) { diff --git a/src/main/java/dev/latvian/mods/kubejs/client/KubeJSGameClientEventHandler.java b/src/main/java/dev/latvian/mods/kubejs/client/KubeJSGameClientEventHandler.java index 44e3fb5be..6e5a819a8 100644 --- a/src/main/java/dev/latvian/mods/kubejs/client/KubeJSGameClientEventHandler.java +++ b/src/main/java/dev/latvian/mods/kubejs/client/KubeJSGameClientEventHandler.java @@ -158,7 +158,7 @@ public static void onItemTooltip(ItemTooltipEvent event) { var advanced = flags.isAdvanced(); - if (mc.level != null && advanced && ClientProperties.get().showComponents && Screen.hasAltDown()) { + if (mc.level != null && advanced && ClientProperties.get().showComponents && dynamicEvent.alt) { var components = BuiltInRegistries.DATA_COMPONENT_TYPE; var ops = mc.level.registryAccess().createSerializationContext(NbtOps.INSTANCE); @@ -184,7 +184,7 @@ public static void onItemTooltip(ItemTooltipEvent event) { } } - if (Screen.hasShiftDown()) { + if (dynamicEvent.shift) { for (var type : stack.getPrototype()) { var id = components.getKey(type.type()); @@ -198,7 +198,7 @@ public static void onItemTooltip(ItemTooltipEvent event) { } } } - } else if (advanced && ClientProperties.get().showTagNames && Screen.hasShiftDown()) { + } else if (advanced && ClientProperties.get().showTagNames && dynamicEvent.shift) { var tempTagNames = new LinkedHashMap(); TagInstance.Type.ITEM.append(tempTagNames, stack.getItem().builtInRegistryHolder().tags()); diff --git a/src/main/java/dev/latvian/mods/kubejs/client/KubeJSModClientEventHandler.java b/src/main/java/dev/latvian/mods/kubejs/client/KubeJSModClientEventHandler.java index c3c7d034e..8c4ab2780 100644 --- a/src/main/java/dev/latvian/mods/kubejs/client/KubeJSModClientEventHandler.java +++ b/src/main/java/dev/latvian/mods/kubejs/client/KubeJSModClientEventHandler.java @@ -7,8 +7,8 @@ import dev.latvian.mods.kubejs.bindings.event.ItemEvents; import dev.latvian.mods.kubejs.block.BlockBuilder; import dev.latvian.mods.kubejs.fluid.FluidBlockBuilder; -import dev.latvian.mods.kubejs.fluid.FluidBucketItemBuilder; import dev.latvian.mods.kubejs.fluid.FluidBuilder; +import dev.latvian.mods.kubejs.fluid.FluidTypeBuilder; import dev.latvian.mods.kubejs.gui.KubeJSMenus; import dev.latvian.mods.kubejs.gui.KubeJSScreen; import dev.latvian.mods.kubejs.item.ItemBuilder; @@ -20,9 +20,11 @@ import dev.latvian.mods.kubejs.text.tooltip.ItemTooltipData; import dev.latvian.mods.kubejs.util.ID; import net.minecraft.client.KeyMapping; +import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.ItemBlockRenderTypes; import net.minecraft.client.renderer.RenderType; import net.minecraft.client.renderer.ShaderInstance; +import net.minecraft.resources.ResourceLocation; import net.minecraft.server.packs.PackType; import net.neoforged.api.distmarker.Dist; import net.neoforged.bus.api.EventPriority; @@ -34,9 +36,12 @@ import net.neoforged.neoforge.client.event.RegisterKeyMappingsEvent; import net.neoforged.neoforge.client.event.RegisterMenuScreensEvent; import net.neoforged.neoforge.client.event.RegisterShadersEvent; +import net.neoforged.neoforge.client.extensions.common.IClientFluidTypeExtensions; +import net.neoforged.neoforge.client.extensions.common.RegisterClientExtensionsEvent; import net.neoforged.neoforge.client.settings.KeyConflictContext; import net.neoforged.neoforge.client.settings.KeyModifier; import net.neoforged.neoforge.event.AddPackFindersEvent; +import org.jetbrains.annotations.Nullable; import org.lwjgl.glfw.GLFW; import java.io.IOException; @@ -111,10 +116,6 @@ public static void itemColors(RegisterColorHandlersEvent.Item event) { if (builder instanceof ItemBuilder b && b.tint != null) { event.register(new ItemTintFunctionWrapper(b.tint), b.get()); } - - if (builder instanceof FluidBucketItemBuilder b && (b.fluidBuilder.bucketTint != null || b.fluidBuilder.fluidType.tint != null)) { - event.register((stack, index) -> index == 1 ? (b.fluidBuilder.bucketTint == null ? b.fluidBuilder.fluidType.tint : b.fluidBuilder.bucketTint).getArgbJS() : 0xFFFFFFFF, b.get()); - } } } @@ -139,4 +140,34 @@ public static void registerKeyMappings(RegisterKeyMappingsEvent event) { public static void registerCoreShaders(RegisterShadersEvent event) throws IOException { event.registerShader(new ShaderInstance(event.getResourceProvider(), ID.mc("kubejs/rendertype_highlight"), DefaultVertexFormat.POSITION_COLOR), s -> KubedexHighlight.INSTANCE.highlightShader = s); } + + @SubscribeEvent + public static void registerClientExtensions(RegisterClientExtensionsEvent event) { + for (var builder : RegistryObjectStorage.FLUID_TYPE) { + if (builder instanceof FluidTypeBuilder b) { + event.registerFluidType(new IClientFluidTypeExtensions() { + @Override + public ResourceLocation getStillTexture() { + return b.actualStillTexture; + } + + @Override + public ResourceLocation getFlowingTexture() { + return b.actualFlowingTexture; + } + + @Override + public ResourceLocation getOverlayTexture() { + return b.blockOverlayTexture; + } + + @Override + @Nullable + public ResourceLocation getRenderOverlayTexture(Minecraft mc) { + return b.screenOverlayTexture; + } + }, b.get()); + } + } + } } diff --git a/src/main/java/dev/latvian/mods/kubejs/client/LoadedTexture.java b/src/main/java/dev/latvian/mods/kubejs/client/LoadedTexture.java index 173509403..b03c46f92 100644 --- a/src/main/java/dev/latvian/mods/kubejs/client/LoadedTexture.java +++ b/src/main/java/dev/latvian/mods/kubejs/client/LoadedTexture.java @@ -5,6 +5,7 @@ import dev.latvian.mods.kubejs.color.Color; import it.unimi.dsi.fastutil.ints.Int2IntArrayMap; import net.minecraft.resources.ResourceLocation; +import net.minecraft.util.Mth; import org.jetbrains.annotations.Nullable; import javax.imageio.ImageIO; @@ -129,4 +130,38 @@ public LoadedTexture resize(int newWidth, int newHeight) { bg.dispose(); return new LoadedTexture(dst, mcmeta); } + + public LoadedTexture tint(@Nullable Color tint) { + if (tint == null) { + return this; + } + + int argb = tint.getArgbJS(); + float l = ((argb >> 24) & 0xFF) / 255F; + + if (l <= 0F) { + return this; + } else if (l > 1F) { + l = 1F; + } + + float tr = ((argb >> 16) & 0xFF) / 255F; + float tg = ((argb >> 8) & 0xFF) / 255F; + float tb = (argb & 0xFF) / 255F; + + int[] result = new int[pixels.length]; + + for (int i = 0; i < pixels.length; i++) { + float pr = ((pixels[i] >> 16) & 0xFF) / 255F; + float pg = ((pixels[i] >> 8) & 0xFF) / 255F; + float pb = (pixels[i] & 0xFF) / 255F; + + result[i] = (pixels[i] & 0xFF000000) + | ((int) (Mth.lerp(l, pr, pr * tr) * 255F) << 16) + | ((int) (Mth.lerp(l, pg, pg * tg) * 255F) << 8) + | (int) (Mth.lerp(l, pb, pb * tb) * 255F); + } + + return new LoadedTexture(width, height, result, mcmeta); + } } diff --git a/src/main/java/dev/latvian/mods/kubejs/core/LivingEntityKJS.java b/src/main/java/dev/latvian/mods/kubejs/core/LivingEntityKJS.java index d22efb1d3..5cd031b44 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/LivingEntityKJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/LivingEntityKJS.java @@ -256,18 +256,18 @@ public interface LivingEntityKJS extends EntityKJS { } } - default void kjs$modifyAttribute(Holder attribute, ResourceLocation identifier, double d, AttributeModifier.Operation operation) { + default void kjs$modifyAttribute(Holder attribute, ResourceLocation id, double amount, AttributeModifier.Operation operation) { AttributeInstance instance = kjs$self().getAttribute(attribute); if (instance != null) { - instance.removeModifier(identifier); - instance.addTransientModifier(new AttributeModifier(identifier, d, operation)); + instance.removeModifier(id); + instance.addTransientModifier(new AttributeModifier(id, amount, operation)); } } - default void kjs$removeAttribute(Holder attribute, ResourceLocation identifier) { + default void kjs$removeAttribute(Holder attribute, ResourceLocation id) { AttributeInstance instance = kjs$self().getAttribute(attribute); if (instance != null) { - instance.removeModifier(identifier); + instance.removeModifier(id); } } diff --git a/src/main/java/dev/latvian/mods/kubejs/core/TagLoaderKJS.java b/src/main/java/dev/latvian/mods/kubejs/core/TagLoaderKJS.java index 125955bd1..f38e50295 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/TagLoaderKJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/TagLoaderKJS.java @@ -1,11 +1,9 @@ package dev.latvian.mods.kubejs.core; -import com.google.gson.JsonArray; import dev.latvian.mods.kubejs.bindings.event.ServerEvents; import dev.latvian.mods.kubejs.registry.BuilderBase; import dev.latvian.mods.kubejs.registry.RegistryObjectStorage; import dev.latvian.mods.kubejs.script.ConsoleJS; -import dev.latvian.mods.kubejs.server.DataExport; import dev.latvian.mods.kubejs.server.tag.TagEventFilter; import dev.latvian.mods.kubejs.server.tag.TagKubeEvent; import dev.latvian.mods.kubejs.server.tag.TagWrapper; @@ -15,7 +13,6 @@ import net.minecraft.tags.TagLoader; import org.jetbrains.annotations.Nullable; -import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; @@ -79,27 +76,6 @@ public interface TagLoaderKJS { } kjs$resources.kjs$getServerScriptManager().getRegistries().cacheTags(reg, map); - - if (DataExport.export != null) { - var loc = "tags/" + objStorage + "/"; - - for (var entry : map.entrySet()) { - var list = new ArrayList(); - - for (var e : entry.getValue()) { - list.add(e.entry().toString()); - } - - list.sort(String.CASE_INSENSITIVE_ORDER); - var arr = new JsonArray(); - - for (var e : list) { - arr.add(e); - } - - DataExport.export.addJson(loc + entry.getKey() + ".json", arr); - } - } } void kjs$init(ReloadableServerResourcesKJS resources, Registry registry); diff --git a/src/main/java/dev/latvian/mods/kubejs/event/TargetedEventHandler.java b/src/main/java/dev/latvian/mods/kubejs/event/TargetedEventHandler.java index be99e04c0..8866c3161 100644 --- a/src/main/java/dev/latvian/mods/kubejs/event/TargetedEventHandler.java +++ b/src/main/java/dev/latvian/mods/kubejs/event/TargetedEventHandler.java @@ -6,11 +6,11 @@ import dev.latvian.mods.rhino.Context; import dev.latvian.mods.rhino.type.TypeInfo; import dev.latvian.mods.rhino.util.HideFromJS; +import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap; import org.jetbrains.annotations.Nullable; import java.util.HashMap; import java.util.HashSet; -import java.util.IdentityHashMap; import java.util.Map; import java.util.Set; import java.util.function.Consumer; @@ -108,7 +108,7 @@ protected EventHandlerContainer[] createMap(@Nullable Object extraId) { } if (extraEventContainers == null) { - extraEventContainers = target.identity ? new IdentityHashMap<>() : new HashMap<>(); + extraEventContainers = target.identity ? new Reference2ObjectOpenHashMap<>() : new HashMap<>(); } var map = extraEventContainers.get(extraId); diff --git a/src/main/java/dev/latvian/mods/kubejs/fluid/FluidBlockBuilder.java b/src/main/java/dev/latvian/mods/kubejs/fluid/FluidBlockBuilder.java index a3b942090..fb76a1ce1 100644 --- a/src/main/java/dev/latvian/mods/kubejs/fluid/FluidBlockBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/fluid/FluidBlockBuilder.java @@ -30,11 +30,6 @@ public Block createObject() { @Override public void generateAssets(KubeAssetGenerator generator) { - generator.blockState(id, m -> m.simpleVariant("", id.getNamespace() + ":block/" + id.getPath())); - generator.blockModel(id, m -> { - m.parent(""); - m.texture("particle", fluidBuilder.fluidType.stillTexture.toString()); - }); } @Override diff --git a/src/main/java/dev/latvian/mods/kubejs/fluid/FluidBucketItemBuilder.java b/src/main/java/dev/latvian/mods/kubejs/fluid/FluidBucketItemBuilder.java index 03a07349a..b1e4c1f21 100644 --- a/src/main/java/dev/latvian/mods/kubejs/fluid/FluidBucketItemBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/fluid/FluidBucketItemBuilder.java @@ -1,6 +1,5 @@ package dev.latvian.mods.kubejs.fluid; -import dev.latvian.mods.kubejs.KubeJS; import dev.latvian.mods.kubejs.generator.KubeAssetGenerator; import dev.latvian.mods.kubejs.item.ItemBuilder; import net.minecraft.world.item.BucketItem; @@ -21,29 +20,5 @@ public BucketItem createObject() { @Override public void generateAssets(KubeAssetGenerator generator) { - if (modelJson != null) { - generator.json(KubeAssetGenerator.asItemModelLocation(id), modelJson); - return; - } - - boolean maskTexture = fluidBuilder.bucketTint == null && generator.mask(newID("item/", "_bucket_fluid"), KubeJS.id("item/bucket_mask"), fluidBuilder.fluidType.stillTexture); - - generator.itemModel(id, m -> { - if (!parentModel.isEmpty()) { - m.parent(parentModel); - } else { - m.parent("kubejs:item/generated_bucket"); - } - - if (maskTexture) { - m.texture("bucket_fluid", newID("item/", "_bucket_fluid")); - } else { - m.texture("bucket_fluid", "kubejs:item/bucket_fluid"); - } - - if (!textureJson.isEmpty()) { - m.textures(textureJson); - } - }); } } diff --git a/src/main/java/dev/latvian/mods/kubejs/fluid/FluidBuilder.java b/src/main/java/dev/latvian/mods/kubejs/fluid/FluidBuilder.java index 7fa91740e..349ee4907 100644 --- a/src/main/java/dev/latvian/mods/kubejs/fluid/FluidBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/fluid/FluidBuilder.java @@ -1,8 +1,10 @@ package dev.latvian.mods.kubejs.fluid; +import dev.latvian.mods.kubejs.KubeJS; import dev.latvian.mods.kubejs.block.BlockRenderType; import dev.latvian.mods.kubejs.color.Color; import dev.latvian.mods.kubejs.color.SimpleColor; +import dev.latvian.mods.kubejs.generator.KubeAssetGenerator; import dev.latvian.mods.kubejs.registry.AdditionalObjectRegistry; import dev.latvian.mods.kubejs.registry.BuilderBase; import dev.latvian.mods.rhino.util.ReturnsSelf; @@ -20,8 +22,6 @@ public class FluidBuilder extends BuilderBase { public static final Color WATER_COLOR = new SimpleColor(0xFF3F76E4); - public transient Color bucketTint = null; - public transient int slopeFindDistance = 4; public transient int levelDecreasePerBlock = 1; public transient float explosionResistance = 1; @@ -103,11 +103,6 @@ public FluidBuilder tint(Color c) { return this; } - public FluidBuilder bucketTint(Color c) { - this.bucketTint = c; - return this; - } - public FluidBuilder stillTexture(ResourceLocation id) { fluidType.stillTexture = id; return this; @@ -156,4 +151,50 @@ public FluidBuilder noBlock() { this.block = null; return this; } + + @Override + public void generateAssets(KubeAssetGenerator generator) { + var stillTexture = generator.loadTexture(fluidType.stillTexture); + + if (stillTexture != null) { + generator.texture(fluidType.actualStillTexture, stillTexture.tint(fluidType.tint)); + } + + var flowingTexture = generator.loadTexture(fluidType.flowingTexture); + + if (flowingTexture != null) { + generator.texture(fluidType.actualFlowingTexture, flowingTexture.tint(fluidType.tint)); + } + + generator.blockState(id, m -> m.simpleVariant("", id.getNamespace() + ":block/" + id.getPath())); + generator.blockModel(id, m -> { + m.parent(""); + m.texture("particle", fluidType.actualStillTexture.toString()); + }); + + if (bucketItem != null) { + if (bucketItem.modelJson != null) { + generator.json(KubeAssetGenerator.asItemModelLocation(id), bucketItem.modelJson); + return; + } + + var fluidPath = newID("item/generated/", "_bucket_fluid"); + + generator.mask(fluidPath, KubeJS.id("item/bucket_mask"), fluidType.actualStillTexture); + + generator.itemModel(bucketItem.id, m -> { + if (!bucketItem.parentModel.isEmpty()) { + m.parent(bucketItem.parentModel); + } else { + m.parent("kubejs:item/generated_bucket"); + } + + m.texture("bucket_fluid", fluidPath); + + if (bucketItem.textureJson.size() > 0) { + m.textures(bucketItem.textureJson); + } + }); + } + } } \ No newline at end of file diff --git a/src/main/java/dev/latvian/mods/kubejs/fluid/FluidTypeBuilder.java b/src/main/java/dev/latvian/mods/kubejs/fluid/FluidTypeBuilder.java index a3185a8d5..aacc00195 100644 --- a/src/main/java/dev/latvian/mods/kubejs/fluid/FluidTypeBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/fluid/FluidTypeBuilder.java @@ -4,7 +4,6 @@ import dev.latvian.mods.kubejs.color.Color; import dev.latvian.mods.kubejs.registry.BuilderBase; import dev.latvian.mods.rhino.util.ReturnsSelf; -import net.minecraft.client.Minecraft; import net.minecraft.core.particles.ParticleOptions; import net.minecraft.resources.ResourceLocation; import net.minecraft.sounds.SoundEvent; @@ -12,14 +11,11 @@ import net.minecraft.world.item.Rarity; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.pathfinder.PathType; -import net.neoforged.neoforge.client.extensions.common.IClientFluidTypeExtensions; import net.neoforged.neoforge.common.SoundAction; import net.neoforged.neoforge.common.SoundActions; import net.neoforged.neoforge.fluids.FluidType; import org.jetbrains.annotations.Nullable; -import java.util.function.Consumer; - @ReturnsSelf public class FluidTypeBuilder extends BuilderBase { public static class KubeFluidType extends FluidType { @@ -29,42 +25,13 @@ public KubeFluidType(FluidTypeBuilder builder) { super(builder.properties); this.builder = builder; } - - @Override - public void initializeClient(Consumer consumer) { - consumer.accept(new IClientFluidTypeExtensions() { - @Override - public ResourceLocation getStillTexture() { - return builder.stillTexture; - } - - @Override - public ResourceLocation getFlowingTexture() { - return builder.flowingTexture; - } - - @Override - public ResourceLocation getOverlayTexture() { - return builder.blockOverlayTexture; - } - - @Override - @Nullable - public ResourceLocation getRenderOverlayTexture(Minecraft mc) { - return builder.screenOverlayTexture; - } - - @Override - public int getTintColor() { - return builder.tint == null ? 0xFFFFFFFF : builder.tint.getArgbJS(); - } - }); - } } public transient FluidType.Properties properties; public transient ResourceLocation stillTexture; public transient ResourceLocation flowingTexture; + public transient ResourceLocation actualStillTexture; + public transient ResourceLocation actualFlowingTexture; public transient ResourceLocation screenOverlayTexture; public transient ResourceLocation blockOverlayTexture; public transient Color tint; @@ -75,6 +42,8 @@ public FluidTypeBuilder(ResourceLocation id) { this.properties = FluidType.Properties.create(); this.stillTexture = newID("block/", "_still"); this.flowingTexture = newID("block/", "_flow"); + this.actualStillTexture = newID("block/generated/", "_still"); + this.actualFlowingTexture = newID("block/generated/", "_flow"); this.tint = null; this.renderType = BlockRenderType.SOLID; diff --git a/src/main/java/dev/latvian/mods/kubejs/generator/KubeAssetGenerator.java b/src/main/java/dev/latvian/mods/kubejs/generator/KubeAssetGenerator.java index 57f85acda..820e98292 100644 --- a/src/main/java/dev/latvian/mods/kubejs/generator/KubeAssetGenerator.java +++ b/src/main/java/dev/latvian/mods/kubejs/generator/KubeAssetGenerator.java @@ -18,10 +18,6 @@ static ResourceLocation asItemModelLocation(ResourceLocation id) { return ResourceLocation.fromNamespaceAndPath(id.getNamespace(), "models/item/" + id.getPath()); } - default void addLang(String key, String value) { - ConsoleJS.CLIENT.error("Use ClientEvents.lang('en_us', event => { event.add(key, value) }) instead!"); - } - default LoadedTexture loadTexture(ResourceLocation id) { return LoadedTexture.load(id); } diff --git a/src/main/java/dev/latvian/mods/kubejs/generator/KubeResourceGenerator.java b/src/main/java/dev/latvian/mods/kubejs/generator/KubeResourceGenerator.java index dca703adb..ae2b44a5d 100644 --- a/src/main/java/dev/latvian/mods/kubejs/generator/KubeResourceGenerator.java +++ b/src/main/java/dev/latvian/mods/kubejs/generator/KubeResourceGenerator.java @@ -4,12 +4,16 @@ import dev.latvian.mods.kubejs.event.KubeEvent; import dev.latvian.mods.kubejs.script.data.GeneratedData; import net.minecraft.resources.ResourceLocation; +import org.jetbrains.annotations.Nullable; import java.nio.charset.StandardCharsets; public interface KubeResourceGenerator extends KubeEvent { void add(GeneratedData data); + @Nullable + GeneratedData getGenerated(ResourceLocation id); + default void text(ResourceLocation id, String content) { add(new GeneratedData(id, () -> content.getBytes(StandardCharsets.UTF_8))); } diff --git a/src/main/java/dev/latvian/mods/kubejs/integration/jei/JEIRemoveRecipesKubeEvent.java b/src/main/java/dev/latvian/mods/kubejs/integration/jei/JEIRemoveRecipesKubeEvent.java index 5fff90394..7a77030de 100644 --- a/src/main/java/dev/latvian/mods/kubejs/integration/jei/JEIRemoveRecipesKubeEvent.java +++ b/src/main/java/dev/latvian/mods/kubejs/integration/jei/JEIRemoveRecipesKubeEvent.java @@ -4,6 +4,7 @@ import dev.latvian.mods.kubejs.event.EventResult; import dev.latvian.mods.kubejs.recipe.viewer.RemoveRecipesKubeEvent; import dev.latvian.mods.rhino.Context; +import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap; import mezz.jei.api.recipe.IRecipeManager; import mezz.jei.api.recipe.category.IRecipeCategory; import net.minecraft.resources.ResourceLocation; @@ -13,7 +14,6 @@ import java.util.Arrays; import java.util.Collection; import java.util.HashSet; -import java.util.IdentityHashMap; import java.util.Map; import java.util.Set; @@ -28,7 +28,7 @@ public JEIRemoveRecipesKubeEvent(IRecipeManager recipeManager, Map(); - this.removed = new IdentityHashMap<>(); + this.removed = new Reference2ObjectOpenHashMap<>(); } @Override diff --git a/src/main/java/dev/latvian/mods/kubejs/item/ItemBuilder.java b/src/main/java/dev/latvian/mods/kubejs/item/ItemBuilder.java index a001cbab8..13bf319e1 100644 --- a/src/main/java/dev/latvian/mods/kubejs/item/ItemBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/item/ItemBuilder.java @@ -136,7 +136,7 @@ public void generateAssets(KubeAssetGenerator generator) { m.parent("minecraft:item/generated"); } - if (textureJson.isEmpty()) { + if (textureJson.size() == 0) { texture(newID("item/", "").toString()); } diff --git a/src/main/java/dev/latvian/mods/kubejs/misc/BasicMobEffect.java b/src/main/java/dev/latvian/mods/kubejs/misc/BasicMobEffect.java index 5d23e7b8e..5dba4ceda 100644 --- a/src/main/java/dev/latvian/mods/kubejs/misc/BasicMobEffect.java +++ b/src/main/java/dev/latvian/mods/kubejs/misc/BasicMobEffect.java @@ -5,44 +5,53 @@ import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.effect.MobEffect; +import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.ai.attributes.Attribute; import net.minecraft.world.entity.ai.attributes.AttributeMap; import net.minecraft.world.entity.ai.attributes.AttributeModifier; import org.jetbrains.annotations.NotNull; -import java.util.Map; - public class BasicMobEffect extends MobEffect { - - private final MobEffectBuilder.EffectTickCallback effectTickCallback; - private final Map modifierMap; + public final transient MobEffectBuilder builder; private boolean modified = false; private final ResourceLocation id; private final boolean instant; - public BasicMobEffect(Builder builder) { + public BasicMobEffect(MobEffectBuilder builder) { super(builder.category, builder.color); - this.effectTickCallback = builder.effectTick; - modifierMap = builder.attributeModifiers; + this.builder = builder; this.id = builder.id; this.instant = builder.instant; } @Override - public boolean applyEffectTick(@NotNull LivingEntity livingEntity, int i) { + public boolean applyEffectTick(@NotNull LivingEntity entity, int i) { + if (builder.effectTick == null) { + return false; + } + try { - effectTickCallback.applyEffectTick(livingEntity, i); + builder.effectTick.applyEffectTick(entity, i); return true; } catch (Throwable e) { - ScriptType.STARTUP.console.error("Error while ticking mob effect " + id + " for entity " + livingEntity.getName().getString(), e); + ScriptType.STARTUP.console.error("Error while ticking mob effect " + id + " for entity " + entity.getName().getString(), e); return false; } } + @Override + public void onEffectStarted(LivingEntity livingEntity, int amplifier) { + } + + @Override + public void onMobRemoved(LivingEntity livingEntity, int amplifier, Entity.RemovalReason reason) { + super.onMobRemoved(livingEntity, amplifier, reason); + } + private void applyAttributeModifications() { if (!modified) { - modifierMap.forEach((r, m) -> BuiltInRegistries.ATTRIBUTE.getHolder(r).ifPresent(h -> attributeModifiers.put(h, m))); + builder.attributeModifiers.forEach((r, m) -> BuiltInRegistries.ATTRIBUTE.getHolder(r).ifPresent(h -> attributeModifiers.put(h, m))); modified = true; } } @@ -61,22 +70,11 @@ public MobEffect addAttributeModifier(Holder attribute, ResourceLocat @Override public boolean isInstantenous() { - return instant && this.effectTickCallback != null; + return instant && builder.effectTick != null; } @Override public boolean shouldApplyEffectTickThisTick(int i, int j) { - return this.effectTickCallback != null; - } - - public static class Builder extends MobEffectBuilder { - public Builder(ResourceLocation i) { - super(i); - } - - @Override - public MobEffect createObject() { - return new BasicMobEffect(this); - } + return builder.effectTick != null; } } diff --git a/src/main/java/dev/latvian/mods/kubejs/misc/MobEffectBuilder.java b/src/main/java/dev/latvian/mods/kubejs/misc/MobEffectBuilder.java index ed5d9ae21..0e71503a9 100644 --- a/src/main/java/dev/latvian/mods/kubejs/misc/MobEffectBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/misc/MobEffectBuilder.java @@ -12,19 +12,15 @@ import java.util.HashMap; import java.util.Map; -/** - * @author Prunoideae - */ @ReturnsSelf -public abstract class MobEffectBuilder extends BuilderBase { - +public class MobEffectBuilder extends BuilderBase { @FunctionalInterface - public interface EffectTickCallback { - void applyEffectTick(LivingEntity livingEntity, int level); + public interface EffectEntityCallback { + void applyEffectTick(LivingEntity entity, int level); } public transient MobEffectCategory category; - public transient EffectTickCallback effectTick; + public transient EffectEntityCallback effectTick; public transient Map attributeModifiers; public transient int color; public transient boolean instant; @@ -34,7 +30,12 @@ public MobEffectBuilder(ResourceLocation i) { category = MobEffectCategory.NEUTRAL; color = 0xFFFFFF; effectTick = null; - attributeModifiers = new HashMap<>(); + attributeModifiers = new HashMap<>(0); + } + + @Override + public MobEffect createObject() { + return new BasicMobEffect(this); } @Override @@ -42,8 +43,8 @@ public String getTranslationKeyGroup() { return "effect"; } - public MobEffectBuilder modifyAttribute(ResourceLocation attribute, ResourceLocation id, double d, AttributeModifier.Operation operation) { - attributeModifiers.put(attribute, new MobEffect.AttributeTemplate(id, d, operation)); + public MobEffectBuilder modifyAttribute(ResourceLocation attribute, ResourceLocation id, double amount, AttributeModifier.Operation operation) { + attributeModifiers.put(attribute, new MobEffect.AttributeTemplate(id, amount, operation)); return this; } @@ -60,7 +61,7 @@ public MobEffectBuilder beneficial() { return category(MobEffectCategory.BENEFICIAL); } - public MobEffectBuilder effectTick(EffectTickCallback effectTick) { + public MobEffectBuilder effectTick(EffectEntityCallback effectTick) { this.effectTick = effectTick; return this; } diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/CachedTagLookup.java b/src/main/java/dev/latvian/mods/kubejs/recipe/CachedTagLookup.java index cda0134ab..2dd040790 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/CachedTagLookup.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/CachedTagLookup.java @@ -2,6 +2,7 @@ import com.mojang.datafixers.util.Either; import dev.latvian.mods.kubejs.KubeJS; +import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap; import net.minecraft.core.Holder; import net.minecraft.core.Registry; import net.minecraft.core.registries.Registries; @@ -18,7 +19,6 @@ import java.util.Collection; import java.util.HashMap; import java.util.HashSet; -import java.util.IdentityHashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; @@ -28,6 +28,9 @@ import java.util.stream.Collectors; public class CachedTagLookup { + public record Entry(ResourceKey key, Registry registry, CachedTagLookup lookup) { + } + record SortingEntry(List entries) implements DependencySorter.Entry { @Override public void visitRequiredDependencies(Consumer visitor) { @@ -88,7 +91,7 @@ public Collection tag(ResourceLocation id) { private Map, Set> keyToValue() { if (keyToValue == null) { var map = build(originalMap); - keyToValue = new IdentityHashMap<>(map.size()); + keyToValue = new Reference2ObjectOpenHashMap<>(map.size()); for (var entry : map.entrySet()) { var k = TagKey.create(registry.key(), entry.getKey()); @@ -112,7 +115,7 @@ public boolean isEmpty(TagKey key) { public Set> keys(T value) { if (valueToKey == null) { - valueToKey = new IdentityHashMap<>(); + valueToKey = new Reference2ObjectOpenHashMap<>(); for (var entry : keyToValue().entrySet()) { for (var v : entry.getValue()) { @@ -126,7 +129,7 @@ public Set> keys(T value) { public Map, List>> bindingMap() { var k2v = keyToValue(); - var map = new IdentityHashMap, List>>(k2v.size()); + var map = new Reference2ObjectOpenHashMap, List>>(k2v.size()); for (var entry : k2v.entrySet()) { var list = new ArrayList>(entry.getValue().size()); diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/RecipesKubeEvent.java b/src/main/java/dev/latvian/mods/kubejs/recipe/RecipesKubeEvent.java index 8251232ad..0f7db1008 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/RecipesKubeEvent.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/RecipesKubeEvent.java @@ -36,6 +36,7 @@ import dev.latvian.mods.kubejs.util.UtilsJS; import dev.latvian.mods.rhino.Context; import dev.latvian.mods.rhino.util.HideFromJS; +import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.resources.ResourceLocation; import net.minecraft.util.GsonHelper; @@ -50,7 +51,6 @@ import java.util.Collections; import java.util.HashMap; import java.util.HashSet; -import java.util.IdentityHashMap; import java.util.LinkedHashMap; import java.util.Map; import java.util.Objects; @@ -558,7 +558,7 @@ public KubeRecipe custom(Context cx, JsonObject json) { private void printTypes(Predicate predicate) { int t = 0; - var map = new IdentityHashMap>(); + var map = new Reference2ObjectOpenHashMap>(); for (var ns : recipeSchemaStorage.namespaces.values()) { for (var type : ns.values()) { diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/component/ComponentValueMap.java b/src/main/java/dev/latvian/mods/kubejs/recipe/component/ComponentValueMap.java index a4668544c..46ae724a2 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/component/ComponentValueMap.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/component/ComponentValueMap.java @@ -4,10 +4,9 @@ import dev.latvian.mods.kubejs.recipe.KubeRecipe; import dev.latvian.mods.kubejs.recipe.RecipeKey; import dev.latvian.mods.rhino.Context; +import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap; -import java.util.IdentityHashMap; - -public class ComponentValueMap extends IdentityHashMap, Object> { +public class ComponentValueMap extends Reference2ObjectOpenHashMap, Object> { public ComponentValueMap(int init) { super(init); } diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/schema/JsonRecipeSchemaLoader.java b/src/main/java/dev/latvian/mods/kubejs/recipe/schema/JsonRecipeSchemaLoader.java index 70690b87d..4177ca899 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/schema/JsonRecipeSchemaLoader.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/schema/JsonRecipeSchemaLoader.java @@ -8,12 +8,12 @@ import dev.latvian.mods.kubejs.script.ConsoleJS; import dev.latvian.mods.kubejs.util.JsonUtils; import dev.latvian.mods.kubejs.util.RegistryAccessContainer; +import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.packs.resources.ResourceManager; import java.util.ArrayList; import java.util.HashMap; -import java.util.IdentityHashMap; import java.util.List; import java.util.Map; @@ -117,7 +117,7 @@ private RecipeSchema getSchema(DynamicOps jsonOps) { var functionMap = new HashMap(); gatherFunctions(functionMap); - var keyOverrides = new IdentityHashMap, RecipeOptional>(overrideKeys == null ? 0 : overrideKeys.size()); + var keyOverrides = new Reference2ObjectOpenHashMap, RecipeOptional>(overrideKeys == null ? 0 : overrideKeys.size()); if (overrideKeys != null) { for (var entry : overrideKeys.entrySet()) { @@ -163,7 +163,7 @@ private RecipeSchema getSchema(DynamicOps jsonOps) { var constructor = new RecipeConstructor(cKeys.toArray(new RecipeKey[0])); if (!c.overrides.isEmpty()) { - constructor.overrides = new IdentityHashMap<>(c.overrides.size()); + constructor.overrides = new Reference2ObjectOpenHashMap<>(c.overrides.size()); for (var entry : c.overrides.entrySet()) { var key = keyMap.get(entry.getKey()); diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/schema/RecipeConstructor.java b/src/main/java/dev/latvian/mods/kubejs/recipe/schema/RecipeConstructor.java index ed8873320..38cbcdaf4 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/schema/RecipeConstructor.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/schema/RecipeConstructor.java @@ -8,8 +8,8 @@ import dev.latvian.mods.kubejs.script.SourceLine; import dev.latvian.mods.kubejs.util.Cast; import dev.latvian.mods.rhino.Context; +import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap; -import java.util.IdentityHashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -29,7 +29,7 @@ public RecipeConstructor(RecipeKey... keys) { public RecipeConstructor override(RecipeKey key, RecipeOptional value) { if (overrides.isEmpty()) { - overrides = new IdentityHashMap<>(1); + overrides = new Reference2ObjectOpenHashMap<>(1); } overrides.put(key, value); diff --git a/src/main/java/dev/latvian/mods/kubejs/registry/BuilderTypeRegistryHandler.java b/src/main/java/dev/latvian/mods/kubejs/registry/BuilderTypeRegistryHandler.java index 8b7c2a13e..7f7604608 100644 --- a/src/main/java/dev/latvian/mods/kubejs/registry/BuilderTypeRegistryHandler.java +++ b/src/main/java/dev/latvian/mods/kubejs/registry/BuilderTypeRegistryHandler.java @@ -6,11 +6,11 @@ import dev.latvian.mods.kubejs.script.ConsoleJS; import dev.latvian.mods.kubejs.util.Lazy; import dev.latvian.mods.rhino.type.TypeInfo; +import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap; import net.minecraft.core.Registry; import net.minecraft.resources.ResourceKey; import org.jetbrains.annotations.Nullable; -import java.util.IdentityHashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -18,7 +18,7 @@ public record BuilderTypeRegistryHandler(Map, Info> map) implements BuilderTypeRegistry, ServerRegistryRegistry { public static final Lazy, Info>> INFO = Lazy.of(() -> { - var handler = new BuilderTypeRegistryHandler(new IdentityHashMap<>()); + var handler = new BuilderTypeRegistryHandler(new Reference2ObjectOpenHashMap<>()); KubeJSPlugins.forEachPlugin(handler, KubeJSPlugin::registerBuilderTypes); KubeJSPlugins.forEachPlugin(handler, KubeJSPlugin::registerServerRegistries); return handler.map; diff --git a/src/main/java/dev/latvian/mods/kubejs/registry/RegistryObjectStorage.java b/src/main/java/dev/latvian/mods/kubejs/registry/RegistryObjectStorage.java index 4518b2543..103e15dba 100644 --- a/src/main/java/dev/latvian/mods/kubejs/registry/RegistryObjectStorage.java +++ b/src/main/java/dev/latvian/mods/kubejs/registry/RegistryObjectStorage.java @@ -2,6 +2,7 @@ import com.mojang.serialization.Codec; import dev.latvian.mods.kubejs.util.Cast; +import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap; import net.minecraft.core.Registry; import net.minecraft.core.registries.Registries; import net.minecraft.resources.ResourceKey; @@ -9,9 +10,10 @@ import net.minecraft.world.item.Item; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.material.Fluid; +import net.neoforged.neoforge.fluids.FluidType; +import net.neoforged.neoforge.registries.NeoForgeRegistries; import org.jetbrains.annotations.NotNull; -import java.util.IdentityHashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedList; @@ -20,7 +22,7 @@ public final class RegistryObjectStorage implements Iterable> { private static final Object LOCK = new Object(); - private static final Map>, RegistryObjectStorage> MAP = new IdentityHashMap<>(); + private static final Map>, RegistryObjectStorage> MAP = new Reference2ObjectOpenHashMap<>(); public static final List> ALL_BUILDERS = new LinkedList<>(); public static final Codec> CODEC = ResourceLocation.CODEC.xmap(rl -> RegistryObjectStorage.of(ResourceKey.createRegistryKey(rl)), ri -> ri.key.location()); @@ -34,6 +36,7 @@ public static RegistryObjectStorage of(ResourceKey> key) { public static final RegistryObjectStorage FLUID = of(Registries.FLUID); public static final RegistryObjectStorage BLOCK = of(Registries.BLOCK); public static final RegistryObjectStorage ITEM = of(Registries.ITEM); + public static final RegistryObjectStorage FLUID_TYPE = of(NeoForgeRegistries.Keys.FLUID_TYPES); public final ResourceKey> key; public final Map> objects; diff --git a/src/main/java/dev/latvian/mods/kubejs/registry/RegistryType.java b/src/main/java/dev/latvian/mods/kubejs/registry/RegistryType.java index 0417297cc..62808ed8c 100644 --- a/src/main/java/dev/latvian/mods/kubejs/registry/RegistryType.java +++ b/src/main/java/dev/latvian/mods/kubejs/registry/RegistryType.java @@ -3,6 +3,7 @@ import dev.latvian.mods.kubejs.DevProperties; import dev.latvian.mods.kubejs.KubeJS; import dev.latvian.mods.rhino.type.TypeInfo; +import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap; import net.minecraft.core.Registry; import net.minecraft.core.registries.Registries; import net.minecraft.resources.ResourceKey; @@ -12,14 +13,13 @@ import java.lang.reflect.ParameterizedType; import java.util.ArrayList; import java.util.HashMap; -import java.util.IdentityHashMap; import java.util.List; import java.util.Map; public record RegistryType(ResourceKey> key, Class baseClass, TypeInfo type) { - private static final Map, RegistryType> KEY_MAP = new IdentityHashMap<>(); + private static final Map, RegistryType> KEY_MAP = new Reference2ObjectOpenHashMap<>(); private static final Map> TYPE_MAP = new HashMap<>(); - private static final Map, List>> CLASS_MAP = new IdentityHashMap<>(); + private static final Map, List>> CLASS_MAP = new Reference2ObjectOpenHashMap<>(); // This is cursed, but it's better than manually registering every type public static synchronized void init() { diff --git a/src/main/java/dev/latvian/mods/kubejs/script/data/VirtualAssetPack.java b/src/main/java/dev/latvian/mods/kubejs/script/data/VirtualAssetPack.java index 83ee726f4..e45bddd49 100644 --- a/src/main/java/dev/latvian/mods/kubejs/script/data/VirtualAssetPack.java +++ b/src/main/java/dev/latvian/mods/kubejs/script/data/VirtualAssetPack.java @@ -19,7 +19,16 @@ public VirtualAssetPack(GeneratedDataStage stage) { @Override public LoadedTexture loadTexture(ResourceLocation id) { - return loadedTextures.computeIfAbsent(id, LoadedTexture::load); + return loadedTextures.computeIfAbsent(id, KubeAssetGenerator.super::loadTexture); + } + + @Override + public void texture(ResourceLocation target, LoadedTexture texture) { + KubeAssetGenerator.super.texture(target, texture); + + if (texture.width > 0 && texture.height > 0) { + loadedTextures.put(target, texture); + } } @Override diff --git a/src/main/java/dev/latvian/mods/kubejs/script/data/VirtualResourcePack.java b/src/main/java/dev/latvian/mods/kubejs/script/data/VirtualResourcePack.java index 6294f2738..cae49dbad 100644 --- a/src/main/java/dev/latvian/mods/kubejs/script/data/VirtualResourcePack.java +++ b/src/main/java/dev/latvian/mods/kubejs/script/data/VirtualResourcePack.java @@ -64,6 +64,12 @@ public void add(GeneratedData data) { } } + @Override + @Nullable + public GeneratedData getGenerated(ResourceLocation id) { + return locationToData.get(id); + } + @Nullable @Override public IoSupplier getRootResource(String... path) { diff --git a/src/main/java/dev/latvian/mods/kubejs/server/ServerScriptManager.java b/src/main/java/dev/latvian/mods/kubejs/server/ServerScriptManager.java index 0b22a758f..c517f20f0 100644 --- a/src/main/java/dev/latvian/mods/kubejs/server/ServerScriptManager.java +++ b/src/main/java/dev/latvian/mods/kubejs/server/ServerScriptManager.java @@ -29,6 +29,7 @@ import dev.latvian.mods.kubejs.server.tag.PreTagKubeEvent; import dev.latvian.mods.kubejs.util.Cast; import dev.latvian.mods.kubejs.util.RegistryAccessContainer; +import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap; import net.minecraft.core.Registry; import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; @@ -42,7 +43,6 @@ import java.nio.file.Files; import java.util.ArrayList; import java.util.HashMap; -import java.util.IdentityHashMap; import java.util.List; import java.util.Map; import java.util.Objects; @@ -150,7 +150,7 @@ public void loadAdditional() { var ops = RegistryAccessContainer.current.json(); - var codecs = new IdentityHashMap, Codec>(); + var codecs = new Reference2ObjectOpenHashMap, Codec>(); for (var reg : DataPackRegistriesHooks.getDataPackRegistries()) { var key = (ResourceKey) reg.key(); @@ -228,7 +228,11 @@ public boolean recipes(RecipeManagerKJS recipeManager, ResourceManager resourceM boolean result = false; for (var entry : getRegistries().cachedRegistryTags.values()) { - entry.getLeft().bindTags((Map) entry.getRight().bindingMap()); + if (entry.registry() == null || entry.lookup() == null) { + continue; + } + + entry.registry().bindTags((Map) entry.lookup().bindingMap()); } recipeSchemaStorage.fireEvents(getRegistries(), resourceManager); diff --git a/src/main/java/dev/latvian/mods/kubejs/util/ChangeListener.java b/src/main/java/dev/latvian/mods/kubejs/util/ChangeListener.java deleted file mode 100644 index 24afffe28..000000000 --- a/src/main/java/dev/latvian/mods/kubejs/util/ChangeListener.java +++ /dev/null @@ -1,9 +0,0 @@ -package dev.latvian.mods.kubejs.util; - -/** - * @author LatvianModder - */ -@FunctionalInterface -public interface ChangeListener { - void onChanged(T o); -} \ No newline at end of file diff --git a/src/main/java/dev/latvian/mods/kubejs/util/CountingMap.java b/src/main/java/dev/latvian/mods/kubejs/util/CountingMap.java index 4157843e9..460594921 100644 --- a/src/main/java/dev/latvian/mods/kubejs/util/CountingMap.java +++ b/src/main/java/dev/latvian/mods/kubejs/util/CountingMap.java @@ -8,9 +8,6 @@ import java.util.Set; import java.util.function.Consumer; -/** - * @author LatvianModder - */ public class CountingMap { private final Object2LongOpenHashMap map; diff --git a/src/main/java/dev/latvian/mods/kubejs/util/JsonUtils.java b/src/main/java/dev/latvian/mods/kubejs/util/JsonUtils.java index 914781ccb..58389bc81 100644 --- a/src/main/java/dev/latvian/mods/kubejs/util/JsonUtils.java +++ b/src/main/java/dev/latvian/mods/kubejs/util/JsonUtils.java @@ -26,9 +26,6 @@ import java.util.List; import java.util.Map; -/** - * @author LatvianModder - */ public interface JsonUtils { @HideFromJS Gson GSON = new GsonBuilder().disableHtmlEscaping().setLenient().create(); diff --git a/src/main/java/dev/latvian/mods/kubejs/util/RegistryAccessContainer.java b/src/main/java/dev/latvian/mods/kubejs/util/RegistryAccessContainer.java index 25e7ab212..0826addc8 100644 --- a/src/main/java/dev/latvian/mods/kubejs/util/RegistryAccessContainer.java +++ b/src/main/java/dev/latvian/mods/kubejs/util/RegistryAccessContainer.java @@ -1,5 +1,6 @@ package dev.latvian.mods.kubejs.util; +import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.mojang.serialization.Codec; import com.mojang.serialization.JavaOps; @@ -8,8 +9,11 @@ import dev.latvian.mods.kubejs.bindings.RegistryWrapper; import dev.latvian.mods.kubejs.recipe.CachedItemTagLookup; import dev.latvian.mods.kubejs.recipe.CachedTagLookup; +import dev.latvian.mods.kubejs.script.ConsoleJS; import dev.latvian.mods.kubejs.script.KubeJSContext; +import dev.latvian.mods.kubejs.server.DataExport; import dev.latvian.mods.rhino.Context; +import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap; import net.minecraft.core.Registry; import net.minecraft.core.RegistryAccess; import net.minecraft.core.registries.BuiltInRegistries; @@ -24,11 +28,10 @@ import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.material.Fluid; -import org.apache.commons.lang3.tuple.Pair; import org.jetbrains.annotations.ApiStatus; +import java.util.ArrayList; import java.util.HashMap; -import java.util.IdentityHashMap; import java.util.List; import java.util.Map; @@ -49,7 +52,7 @@ public static RegistryAccessContainer of(Context cx) { private final RegistryOps java; private DamageSources damageSources; private final Map itemStackParseCache; - public Map, Pair, CachedTagLookup>> cachedRegistryTags; + public final Map, CachedTagLookup.Entry> cachedRegistryTags; public CachedItemTagLookup cachedItemTags; public CachedTagLookup cachedBlockTags; public CachedTagLookup cachedFluidTags; @@ -62,7 +65,7 @@ public RegistryAccessContainer(RegistryAccess.Frozen access) { this.java = access.createSerializationContext(JavaOps.INSTANCE); this.damageSources = null; this.itemStackParseCache = new HashMap<>(); - this.cachedRegistryTags = new IdentityHashMap<>(); + this.cachedRegistryTags = new Reference2ObjectOpenHashMap<>(); } public RegistryAccess.Frozen access() { @@ -94,24 +97,49 @@ public Map itemStackParseCache() { } // Currently this is the best way I can think of to have tags available at the time of recipe loading - public void cacheTags(Registry registry, Map> map) { - var key1 = (ResourceKey) registry.key(); + public synchronized void cacheTags(Registry registry, Map> map) { + var key1 = registry == null ? null : (ResourceKey) registry.key(); if (key1 == null) { return; } - if (key1 == Registries.ITEM) { - cachedItemTags = Cast.to(new CachedItemTagLookup((Registry) registry, map)); - cachedRegistryTags.put(key1, Pair.of(registry, cachedItemTags)); - } else if (key1 == Registries.BLOCK) { - cachedBlockTags = Cast.to(new CachedTagLookup<>(registry, map)); - cachedRegistryTags.put(key1, Pair.of(registry, cachedBlockTags)); - } else if (key1 == Registries.FLUID) { - cachedFluidTags = Cast.to(new CachedTagLookup<>(registry, map)); - cachedRegistryTags.put(key1, Pair.of(registry, cachedFluidTags)); - } else { - cachedRegistryTags.put(key1, Pair.of(registry, new CachedTagLookup<>(registry, map))); + try { + if (key1 == Registries.ITEM) { + cachedItemTags = Cast.to(new CachedItemTagLookup((Registry) registry, map)); + cachedRegistryTags.put(key1, new CachedTagLookup.Entry(key1, registry, cachedItemTags)); + } else if (key1 == Registries.BLOCK) { + cachedBlockTags = Cast.to(new CachedTagLookup<>(registry, map)); + cachedRegistryTags.put(key1, new CachedTagLookup.Entry(key1, registry, cachedBlockTags)); + } else if (key1 == Registries.FLUID) { + cachedFluidTags = Cast.to(new CachedTagLookup<>(registry, map)); + cachedRegistryTags.put(key1, new CachedTagLookup.Entry(key1, registry, cachedFluidTags)); + } else { + cachedRegistryTags.put(key1, new CachedTagLookup.Entry(key1, registry, new CachedTagLookup<>(registry, map))); + } + } catch (Exception ex) { + ConsoleJS.SERVER.error("Error caching tags for " + key1, ex); + } + + if (DataExport.export != null) { + var loc = "tags/" + key1.location() + "/"; + + for (var entry : map.entrySet()) { + var list = new ArrayList(); + + for (var e : entry.getValue()) { + list.add(e.entry().toString()); + } + + list.sort(String.CASE_INSENSITIVE_ORDER); + var arr = new JsonArray(); + + for (var e : list) { + arr.add(e); + } + + DataExport.export.addJson(loc + entry.getKey() + ".json", arr); + } } } diff --git a/src/main/java/dev/latvian/mods/kubejs/util/StringBuilderAppendable.java b/src/main/java/dev/latvian/mods/kubejs/util/StringBuilderAppendable.java index 897ae92e6..7a7e7738e 100644 --- a/src/main/java/dev/latvian/mods/kubejs/util/StringBuilderAppendable.java +++ b/src/main/java/dev/latvian/mods/kubejs/util/StringBuilderAppendable.java @@ -1,8 +1,5 @@ package dev.latvian.mods.kubejs.util; -/** - * @author LatvianModder - */ public interface StringBuilderAppendable { void appendString(StringBuilder builder); }