Skip to content

Commit

Permalink
Fluid asset generation cleanup, tint will now produce a real texture,…
Browse files Browse the repository at this point in the history
… replaced IdentityHashMap with Reference2ObjectOpenHashMap
  • Loading branch information
LatvianModder committed Jul 27, 2024
1 parent 3f55a8b commit e803f55
Show file tree
Hide file tree
Showing 38 changed files with 275 additions and 229 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,15 +29,14 @@

import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.util.IdentityHashMap;
import java.util.Map;

public interface DataComponentWrapper {
DynamicCommandExceptionType ERROR_UNKNOWN_COMPONENT = new DynamicCommandExceptionType((object) -> Component.translatableEscape("arguments.item.component.unknown", object));
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<Map<DataComponentType<?>, TypeInfo>> TYPE_INFOS = Lazy.of(() -> {
var map = new IdentityHashMap<DataComponentType<?>, TypeInfo>();
var map = new Reference2ObjectOpenHashMap<DataComponentType<?>, TypeInfo>();

try {
for (var field : DataComponents.class.getDeclaredFields()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

import java.util.UUID;

/**
* @author LatvianModder
*/
public interface UUIDWrapper {
static String toString(@Nullable UUID id) {
if (id != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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());

Expand All @@ -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<ResourceLocation, TagInstance>();
TagInstance.Type.ITEM.append(tempTagNames, stack.getItem().builtInRegistryHolder().tags());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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());
}
}
}

Expand All @@ -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());
}
}
}
}
35 changes: 35 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/client/LoadedTexture.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
10 changes: 5 additions & 5 deletions src/main/java/dev/latvian/mods/kubejs/core/LivingEntityKJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,18 +256,18 @@ public interface LivingEntityKJS extends EntityKJS {
}
}

default void kjs$modifyAttribute(Holder<Attribute> attribute, ResourceLocation identifier, double d, AttributeModifier.Operation operation) {
default void kjs$modifyAttribute(Holder<Attribute> 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> attribute, ResourceLocation identifier) {
default void kjs$removeAttribute(Holder<Attribute> attribute, ResourceLocation id) {
AttributeInstance instance = kjs$self().getAttribute(attribute);
if (instance != null) {
instance.removeModifier(identifier);
instance.removeModifier(id);
}
}

Expand Down
24 changes: 0 additions & 24 deletions src/main/java/dev/latvian/mods/kubejs/core/TagLoaderKJS.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -79,27 +76,6 @@ public interface TagLoaderKJS<T> {
}

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<String>();

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<T> registry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
});
}
}
Loading

0 comments on commit e803f55

Please sign in to comment.