Skip to content

Commit

Permalink
Renamed Color to KubeColor, added kjs$ prefix to methods, changed som…
Browse files Browse the repository at this point in the history
…e String arguments for model builders to ResourceLocations
  • Loading branch information
LatvianModder committed Sep 2, 2024
1 parent 7b3aaa3 commit 439d707
Show file tree
Hide file tree
Showing 61 changed files with 484 additions and 411 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
import dev.latvian.mods.kubejs.block.entity.BlockEntityAttachmentType;
import dev.latvian.mods.kubejs.block.entity.InventoryAttachment;
import dev.latvian.mods.kubejs.block.state.BlockStatePredicate;
import dev.latvian.mods.kubejs.color.Color;
import dev.latvian.mods.kubejs.color.KubeColor;
import dev.latvian.mods.kubejs.component.DataComponentWrapper;
import dev.latvian.mods.kubejs.core.PlayerSelector;
import dev.latvian.mods.kubejs.entity.AttributeBuilder;
Expand Down Expand Up @@ -180,6 +180,7 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.stats.Stat;
import net.minecraft.stats.Stats;
import net.minecraft.util.ColorRGBA;
import net.minecraft.util.Unit;
import net.minecraft.util.valueproviders.FloatProvider;
import net.minecraft.util.valueproviders.IntProvider;
Expand Down Expand Up @@ -579,6 +580,7 @@ public void registerTypeWrappers(TypeWrapperRegistry registry) {
registry.register(BlockSetType.class, BlockWrapper::setTypeOf);
registry.register(BlockState.class, BlockWrapper::wrapBlockState);
registry.register(ItemAbility.class, ItemWrapper::itemAbilityOf);
registry.register(ColorRGBA.class, ColorWrapper::colorRGBAOf);

// KubeJS //
registry.register(Map.class, MapJS::of);
Expand Down Expand Up @@ -614,7 +616,7 @@ public void registerTypeWrappers(TypeWrapperRegistry registry) {
// components //
registry.register(Component.class, TextWrapper::of);
registry.register(MutableComponent.class, TextWrapper::of);
registry.register(Color.class, ColorWrapper::of);
registry.register(KubeColor.class, ColorWrapper::of);
registry.register(TextColor.class, ColorWrapper::textColorOf);
registry.register(ClickEvent.class, TextWrapper::clickEventOf);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static Map<String, Direction> getFacing() {
if (facingMap == null) {
facingMap = new HashMap<>(6);

for (var facing : Direction.values()) {
for (var facing : DirectionWrapper.VALUES) {
facingMap.put(facing.getSerializedName(), facing);
}
}
Expand Down
94 changes: 49 additions & 45 deletions src/main/java/dev/latvian/mods/kubejs/bindings/ColorWrapper.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package dev.latvian.mods.kubejs.bindings;

import dev.latvian.mods.kubejs.color.Color;
import dev.latvian.mods.kubejs.color.KubeColor;
import dev.latvian.mods.kubejs.color.NoColor;
import dev.latvian.mods.kubejs.color.SimpleColor;
import dev.latvian.mods.kubejs.color.SimpleColorWithAlpha;
import net.minecraft.ChatFormatting;
import net.minecraft.Util;
import net.minecraft.network.chat.TextColor;
import net.minecraft.util.ColorRGBA;
import net.minecraft.world.item.DyeColor;

import java.util.HashMap;
import java.util.Map;

public interface ColorWrapper {
Map<String, Color> MAP = new HashMap<>();
Map<String, KubeColor> MAP = new HashMap<>();
Map<String, ChatFormatting> TEXT = Util.make(new HashMap<>(), map -> {
for (ChatFormatting c : ChatFormatting.values()) {
map.put(c.getName(), c);
Expand All @@ -25,12 +26,12 @@ public interface ColorWrapper {
}
});

static Color of(Object o) {
if (o instanceof Color) {
return (Color) o;
static KubeColor of(Object o) {
if (o instanceof KubeColor) {
return (KubeColor) o;
} else if (o instanceof String) {
String s = o.toString();
Color c = MAP.get(s);
KubeColor c = MAP.get(s);

if (c != null) {
return c;
Expand All @@ -56,11 +57,15 @@ static Color of(Object o) {
}

static TextColor textColorOf(Object o) {
return of(o).createTextColorJS();
return of(o).kjs$createTextColor();
}

static Color createMapped(Object o, String... names) {
Color c = of(o);
static ColorRGBA colorRGBAOf(Object o) {
return new ColorRGBA(of(o).kjs$getARGB());
}

static KubeColor createMapped(Object o, String... names) {
KubeColor c = of(o);

for (String s : names) {
MAP.put(s, c);
Expand All @@ -69,46 +74,45 @@ static Color createMapped(Object o, String... names) {
return c;
}

Color NONE = createMapped(new NoColor(), "NONE", "none", "", "-", "transparent");
KubeColor NONE = createMapped(new NoColor(), "NONE", "none", "", "-", "transparent");

static Color rgba(int r, int g, int b, int a) {
static KubeColor rgba(int r, int g, int b, int a) {
return new SimpleColor((r << 16) | (g << 8) | b | (a << 24));
}

Color BLACK = createMapped(ChatFormatting.BLACK, "BLACK", "black");
Color DARK_BLUE = createMapped(ChatFormatting.DARK_BLUE, "DARK_BLUE", "dark_blue", "darkBlue");
Color DARK_GREEN = createMapped(ChatFormatting.DARK_GREEN, "DARK_GREEN", "dark_green", "darkGreen");
Color DARK_AQUA = createMapped(ChatFormatting.DARK_AQUA, "DARK_AQUA", "dark_aqua", "darkAqua");
Color DARK_RED = createMapped(ChatFormatting.DARK_RED, "DARK_RED", "dark_red", "darkRed");
Color DARK_PURPLE = createMapped(ChatFormatting.DARK_PURPLE, "DARK_PURPLE", "dark_purple", "darkPurple");
Color GOLD = createMapped(ChatFormatting.GOLD, "GOLD", "gold");
Color GRAY = createMapped(ChatFormatting.GRAY, "GRAY", "gray");
Color DARK_GRAY = createMapped(ChatFormatting.DARK_GRAY, "DARK_GRAY", "dark_gray", "darkGray");
Color BLUE = createMapped(ChatFormatting.BLUE, "BLUE", "blue");
Color GREEN = createMapped(ChatFormatting.GREEN, "GREEN", "green");
Color AQUA = createMapped(ChatFormatting.AQUA, "AQUA", "aqua");
Color RED = createMapped(ChatFormatting.RED, "RED", "red");
Color LIGHT_PURPLE = createMapped(ChatFormatting.LIGHT_PURPLE, "LIGHT_PURPLE", "light_purple", "lightPurple");
Color YELLOW = createMapped(ChatFormatting.YELLOW, "YELLOW", "yellow");
Color WHITE = createMapped(ChatFormatting.WHITE, "WHITE", "white");


Color WHITE_DYE = createMapped(DyeColor.WHITE, "WHITE_DYE", "white_dye", "whiteDye");
Color ORANGE_DYE = createMapped(DyeColor.ORANGE, "ORANGE_DYE", "orange_dye", "orangeDye");
Color MAGENTA_DYE = createMapped(DyeColor.MAGENTA, "MAGENTA_DYE", "magenta_dye", "magentaDye");
Color LIGHT_BLUE_DYE = createMapped(DyeColor.LIGHT_BLUE, "LIGHT_BLUE_DYE", "light_blue_dye", "lightBlueDye");
Color YELLOW_DYE = createMapped(DyeColor.YELLOW, "YELLOW_DYE", "yellow_dye", "yellowDye");
Color LIME_DYE = createMapped(DyeColor.LIME, "LIME_DYE", "lime_dye", "limeDye");
Color PINK_DYE = createMapped(DyeColor.PINK, "PINK_DYE", "pink_dye", "pinkDye");
Color GRAY_DYE = createMapped(DyeColor.GRAY, "GRAY_DYE", "gray_dye", "grayDye");
Color LIGHT_GRAY_DYE = createMapped(DyeColor.LIGHT_GRAY, "LIGHT_GRAY_DYE", "lightGrayDye", "lightGrayDye");
Color CYAN_DYE = createMapped(DyeColor.CYAN, "CYAN_DYE", "cyan_dye", "cyanDye");
Color PURPLE_DYE = createMapped(DyeColor.PURPLE, "PURPLE_DYE", "purple_dye", "purpleDye");
Color BLUE_DYE = createMapped(DyeColor.BLUE, "BLUE_DYE", "blue_dye", "blueDye");
Color BROWN_DYE = createMapped(DyeColor.BROWN, "BROWN_DYE", "brown_dye", "brownDye");
Color GREEN_DYE = createMapped(DyeColor.GREEN, "GREEN_DYE", "green_dye", "greenDye");
Color RED_DYE = createMapped(DyeColor.RED, "RED_DYE", "red_dye", "redDye");
Color BLACK_DYE = createMapped(DyeColor.BLACK, "BLACK_DYE", "black_dye", "blackDye");
KubeColor BLACK = createMapped(ChatFormatting.BLACK, "BLACK", "black");
KubeColor DARK_BLUE = createMapped(ChatFormatting.DARK_BLUE, "DARK_BLUE", "dark_blue", "darkBlue");
KubeColor DARK_GREEN = createMapped(ChatFormatting.DARK_GREEN, "DARK_GREEN", "dark_green", "darkGreen");
KubeColor DARK_AQUA = createMapped(ChatFormatting.DARK_AQUA, "DARK_AQUA", "dark_aqua", "darkAqua");
KubeColor DARK_RED = createMapped(ChatFormatting.DARK_RED, "DARK_RED", "dark_red", "darkRed");
KubeColor DARK_PURPLE = createMapped(ChatFormatting.DARK_PURPLE, "DARK_PURPLE", "dark_purple", "darkPurple");
KubeColor GOLD = createMapped(ChatFormatting.GOLD, "GOLD", "gold");
KubeColor GRAY = createMapped(ChatFormatting.GRAY, "GRAY", "gray");
KubeColor DARK_GRAY = createMapped(ChatFormatting.DARK_GRAY, "DARK_GRAY", "dark_gray", "darkGray");
KubeColor BLUE = createMapped(ChatFormatting.BLUE, "BLUE", "blue");
KubeColor GREEN = createMapped(ChatFormatting.GREEN, "GREEN", "green");
KubeColor AQUA = createMapped(ChatFormatting.AQUA, "AQUA", "aqua");
KubeColor RED = createMapped(ChatFormatting.RED, "RED", "red");
KubeColor LIGHT_PURPLE = createMapped(ChatFormatting.LIGHT_PURPLE, "LIGHT_PURPLE", "light_purple", "lightPurple");
KubeColor YELLOW = createMapped(ChatFormatting.YELLOW, "YELLOW", "yellow");
KubeColor WHITE = createMapped(ChatFormatting.WHITE, "WHITE", "white");

KubeColor WHITE_DYE = createMapped(DyeColor.WHITE, "WHITE_DYE", "white_dye", "whiteDye");
KubeColor ORANGE_DYE = createMapped(DyeColor.ORANGE, "ORANGE_DYE", "orange_dye", "orangeDye");
KubeColor MAGENTA_DYE = createMapped(DyeColor.MAGENTA, "MAGENTA_DYE", "magenta_dye", "magentaDye");
KubeColor LIGHT_BLUE_DYE = createMapped(DyeColor.LIGHT_BLUE, "LIGHT_BLUE_DYE", "light_blue_dye", "lightBlueDye");
KubeColor YELLOW_DYE = createMapped(DyeColor.YELLOW, "YELLOW_DYE", "yellow_dye", "yellowDye");
KubeColor LIME_DYE = createMapped(DyeColor.LIME, "LIME_DYE", "lime_dye", "limeDye");
KubeColor PINK_DYE = createMapped(DyeColor.PINK, "PINK_DYE", "pink_dye", "pinkDye");
KubeColor GRAY_DYE = createMapped(DyeColor.GRAY, "GRAY_DYE", "gray_dye", "grayDye");
KubeColor LIGHT_GRAY_DYE = createMapped(DyeColor.LIGHT_GRAY, "LIGHT_GRAY_DYE", "lightGrayDye", "lightGrayDye");
KubeColor CYAN_DYE = createMapped(DyeColor.CYAN, "CYAN_DYE", "cyan_dye", "cyanDye");
KubeColor PURPLE_DYE = createMapped(DyeColor.PURPLE, "PURPLE_DYE", "purple_dye", "purpleDye");
KubeColor BLUE_DYE = createMapped(DyeColor.BLUE, "BLUE_DYE", "blue_dye", "blueDye");
KubeColor BROWN_DYE = createMapped(DyeColor.BROWN, "BROWN_DYE", "brown_dye", "brownDye");
KubeColor GREEN_DYE = createMapped(DyeColor.GREEN, "GREEN_DYE", "green_dye", "greenDye");
KubeColor RED_DYE = createMapped(DyeColor.RED, "RED_DYE", "red_dye", "redDye");
KubeColor BLACK_DYE = createMapped(DyeColor.BLACK, "BLACK_DYE", "black_dye", "blackDye");


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import net.minecraft.core.Direction;

import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand All @@ -21,5 +20,6 @@ public interface DirectionWrapper {
Direction SOUTH = Direction.SOUTH;
Direction WEST = Direction.WEST;
Direction EAST = Direction.EAST;
Map<String, Direction> ALL = Collections.unmodifiableMap(Arrays.stream(Direction.values()).collect(Collectors.toMap(Direction::getSerializedName, Function.identity())));
Direction[] VALUES = Direction.values();
Map<String, Direction> ALL = Map.copyOf(Arrays.stream(VALUES).collect(Collectors.toMap(Direction::getSerializedName, Function.identity())));
}
55 changes: 30 additions & 25 deletions src/main/java/dev/latvian/mods/kubejs/block/BlockBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.google.gson.JsonObject;
import com.mojang.serialization.JsonOps;
import dev.latvian.mods.kubejs.bindings.AABBWrapper;
import dev.latvian.mods.kubejs.bindings.DirectionWrapper;
import dev.latvian.mods.kubejs.block.callbacks.AfterEntityFallenOnBlockCallbackJS;
import dev.latvian.mods.kubejs.block.callbacks.BlockExplodedCallbackJS;
import dev.latvian.mods.kubejs.block.callbacks.BlockStateMirrorCallbackJS;
Expand All @@ -26,6 +28,7 @@
import dev.latvian.mods.kubejs.script.ScriptType;
import dev.latvian.mods.kubejs.typings.Info;
import dev.latvian.mods.kubejs.util.Cast;
import dev.latvian.mods.kubejs.util.ID;
import dev.latvian.mods.rhino.util.HideFromJS;
import dev.latvian.mods.rhino.util.ReturnsSelf;
import net.minecraft.core.Direction;
Expand Down Expand Up @@ -55,8 +58,10 @@
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
Expand All @@ -79,8 +84,8 @@ public abstract class BlockBuilder extends BuilderBase<Block> {
public transient boolean requiresTool;
public transient BlockRenderType renderType;
public transient BlockTintFunction tint;
public transient final JsonObject textures;
public transient String model;
public transient final Map<String, String> textures;
public transient ResourceLocation model;
public transient ItemBuilder itemBuilder;
public transient List<AABB> customShape;
public transient boolean noCollision;
Expand Down Expand Up @@ -122,9 +127,9 @@ public BlockBuilder(ResourceLocation i) {
fullBlock = false;
requiresTool = false;
renderType = BlockRenderType.SOLID;
textures = new JsonObject();
textureAll(id.getNamespace() + ":block/" + id.getPath());
model = "";
textures = new HashMap<>(0);
textureAll(id.withPath("block/" + id.getPath()).toString());
model = null;
itemBuilder = getOrCreateItemBuilder();

if (itemBuilder instanceof BlockItemBuilder b) {
Expand Down Expand Up @@ -185,7 +190,7 @@ public void generateData(KubeDataGenerator generator) {
var table = generateLootTable();

if (table != null) {
generator.json(newID("loot_table/blocks/", ""), LootTable.CODEC.encodeStart(JsonOps.INSTANCE, new Holder.Direct<>(table)).getOrThrow());
generator.json(id.withPath(ID.BLOCK_LOOT_TABLE), LootTable.CODEC.encodeStart(JsonOps.INSTANCE, new Holder.Direct<>(table)).getOrThrow());
}
}

Expand Down Expand Up @@ -229,21 +234,21 @@ public LootTable generateLootTable() {
@Override
public void generateAssets(KubeAssetGenerator generator) {
if (blockstateJson != null) {
generator.json(newID("blockstates/", ""), blockstateJson);
generator.json(id.withPath(ID.BLOCKSTATE), blockstateJson);
} else {
generator.blockState(id, this::generateBlockStateJson);
}

if (modelJson != null) {
generator.json(newID("models/block/", ""), modelJson);
generator.json(id.withPath(ID.BLOCK_MODEL), modelJson);
} else {
// This is different because there can be multiple models, so we should let the block handle those
generateBlockModelJsons(generator);
}

if (itemBuilder != null) {
if (itemBuilder.modelJson != null) {
generator.json(newID("models/item/", ""), itemBuilder.modelJson);
generator.json(id.withPath(ID.ITEM_MODEL), itemBuilder.modelJson);
} else {
generator.itemModel(itemBuilder.id, this::generateItemModelJson);
}
Expand All @@ -252,37 +257,37 @@ public void generateAssets(KubeAssetGenerator generator) {
}

protected void generateItemModelJson(ModelGenerator m) {
if (!model.isEmpty()) {
if (model != null) {
m.parent(model);
} else {
m.parent(newID("block/", "").toString());
m.parent(id.withPath(ID.BLOCK));
}
}

protected void generateBlockModelJsons(KubeAssetGenerator generator) {
generator.blockModel(id, mg -> {
var particle = textures.get("particle").getAsString();
var particle = textures.get("particle");

if (areAllTexturesEqual(textures, particle)) {
mg.parent("minecraft:block/cube_all");
if (areAllTexturesEqual(particle)) {
mg.parent(KubeAssetGenerator.CUBE_ALL_BLOCK_MODEL);
mg.texture("all", particle);
} else {
mg.parent("block/cube");
mg.parent(KubeAssetGenerator.CUBE_BLOCK_MODEL);
mg.textures(textures);
}

if (tint != null || !customShape.isEmpty()) {
List<AABB> boxes = new ArrayList<>(customShape);

if (boxes.isEmpty()) {
boxes.add(new AABB(0D, 0D, 0D, 1D, 1D, 1D));
boxes.add(AABBWrapper.CUBE);
}

for (var box : boxes) {
mg.element(e -> {
e.box(box);

for (var direction : Direction.values()) {
for (var direction : DirectionWrapper.VALUES) {
e.face(direction, face -> {
face.tex("#" + direction.getSerializedName());
face.cull();
Expand All @@ -299,12 +304,12 @@ protected void generateBlockModelJsons(KubeAssetGenerator generator) {
}

protected void generateBlockStateJson(VariantBlockStateGenerator bs) {
bs.simpleVariant("", model.isEmpty() ? (id.getNamespace() + ":block/" + id.getPath()) : model);
bs.simpleVariant("", model == null ? id.withPath(ID.BLOCK) : model);
}

protected boolean areAllTexturesEqual(JsonObject tex, String t) {
for (var direction : Direction.values()) {
if (!tex.get(direction.getSerializedName()).getAsString().equals(t)) {
protected boolean areAllTexturesEqual(String t) {
for (var direction : DirectionWrapper.VALUES) {
if (!textures.get(direction.getSerializedName()).equals(t)) {
return false;
}
}
Expand Down Expand Up @@ -461,11 +466,11 @@ public BlockBuilder color(BlockTintFunction color) {
Texture the block on all sides with the same texture.
""")
public BlockBuilder textureAll(String tex) {
for (var direction : Direction.values()) {
for (var direction : DirectionWrapper.VALUES) {
textureSide(direction, tex);
}

textures.addProperty("particle", tex);
textures.put("particle", tex);
return this;
}

Expand All @@ -480,14 +485,14 @@ public BlockBuilder textureSide(Direction direction, String tex) {
Texture a specific texture key of the block.
""")
public BlockBuilder texture(String id, String tex) {
textures.addProperty(id, tex);
textures.put(id, tex);
return this;
}

@Info("""
Set the block's model.
""")
public BlockBuilder model(String m) {
public BlockBuilder model(ResourceLocation m) {
model = m;
if (itemBuilder != null) {
itemBuilder.parentModel(m);
Expand Down
Loading

0 comments on commit 439d707

Please sign in to comment.