Skip to content

Commit

Permalink
Added Locale.ROOT to toLowerCase() and toUpperCase(), changed some st…
Browse files Browse the repository at this point in the history
…ring IDs into RLs, started working on RC validators, added simple auth to web server
  • Loading branch information
LatvianModder committed Jan 21, 2025
1 parent 494bd5f commit 841690e
Show file tree
Hide file tree
Showing 60 changed files with 509 additions and 150 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ neoforge_version=21.1.83
parchment_mc_version=1.21
parchment_mapping_version=2024.11.10
rhino_version=2101.2.6-build.58
tiny_server_version=1.0.0-build.25
tiny_server_version=1.0.0-build.26
gif_lib_version=1.7

architectury_version=13.0.8
Expand Down
74 changes: 46 additions & 28 deletions src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@
import dev.latvian.mods.kubejs.recipe.component.StringGridComponent;
import dev.latvian.mods.kubejs.recipe.component.TagKeyComponent;
import dev.latvian.mods.kubejs.recipe.component.TimeComponent;
import dev.latvian.mods.kubejs.recipe.component.validator.AlwaysValidValidator;
import dev.latvian.mods.kubejs.recipe.component.validator.AndValidator;
import dev.latvian.mods.kubejs.recipe.component.validator.NonEmptyValidator;
import dev.latvian.mods.kubejs.recipe.component.validator.OrValidator;
import dev.latvian.mods.kubejs.recipe.component.validator.RecipeComponentValidatorTypeRegistry;
import dev.latvian.mods.kubejs.recipe.filter.RecipeFilter;
import dev.latvian.mods.kubejs.recipe.ingredientaction.ConsumeAction;
import dev.latvian.mods.kubejs.recipe.ingredientaction.CustomIngredientAction;
Expand Down Expand Up @@ -277,44 +282,44 @@ public void registerBuilderTypes(BuilderTypeRegistry registry) {
registry.addDefault(Registries.BLOCK, BasicBlockJS.Builder.class, BasicBlockJS.Builder::new);

registry.of(Registries.BLOCK, reg -> {
reg.add("detector", DetectorBlock.Builder.class, DetectorBlock.Builder::new);
reg.add("slab", SlabBlockBuilder.class, SlabBlockBuilder::new);
reg.add("stairs", StairBlockBuilder.class, StairBlockBuilder::new);
reg.add("fence", FenceBlockBuilder.class, FenceBlockBuilder::new);
reg.add("wall", WallBlockBuilder.class, WallBlockBuilder::new);
reg.add("fence_gate", FenceGateBlockBuilder.class, FenceGateBlockBuilder::new);
reg.add("pressure_plate", PressurePlateBlockBuilder.class, PressurePlateBlockBuilder::new);
reg.add("button", ButtonBlockBuilder.class, ButtonBlockBuilder::new);
reg.add("falling", FallingBlockBuilder.class, FallingBlockBuilder::new);
reg.add("crop", CropBlockBuilder.class, CropBlockBuilder::new);
reg.add("cardinal", HorizontalDirectionalBlockBuilder.class, HorizontalDirectionalBlockBuilder::new);
reg.add("carpet", CarpetBlockBuilder.class, CarpetBlockBuilder::new);
reg.add("door", DoorBlockBuilder.class, DoorBlockBuilder::new);
reg.add("trapdoor", TrapdoorBlockBuilder.class, TrapdoorBlockBuilder::new);
reg.add(KubeJS.id("detector"), DetectorBlock.Builder.class, DetectorBlock.Builder::new);
reg.add(KubeJS.id("slab"), SlabBlockBuilder.class, SlabBlockBuilder::new);
reg.add(KubeJS.id("stairs"), StairBlockBuilder.class, StairBlockBuilder::new);
reg.add(KubeJS.id("fence"), FenceBlockBuilder.class, FenceBlockBuilder::new);
reg.add(KubeJS.id("wall"), WallBlockBuilder.class, WallBlockBuilder::new);
reg.add(KubeJS.id("fence_gate"), FenceGateBlockBuilder.class, FenceGateBlockBuilder::new);
reg.add(KubeJS.id("pressure_plate"), PressurePlateBlockBuilder.class, PressurePlateBlockBuilder::new);
reg.add(KubeJS.id("button"), ButtonBlockBuilder.class, ButtonBlockBuilder::new);
reg.add(KubeJS.id("falling"), FallingBlockBuilder.class, FallingBlockBuilder::new);
reg.add(KubeJS.id("crop"), CropBlockBuilder.class, CropBlockBuilder::new);
reg.add(KubeJS.id("cardinal"), HorizontalDirectionalBlockBuilder.class, HorizontalDirectionalBlockBuilder::new);
reg.add(KubeJS.id("carpet"), CarpetBlockBuilder.class, CarpetBlockBuilder::new);
reg.add(KubeJS.id("door"), DoorBlockBuilder.class, DoorBlockBuilder::new);
reg.add(KubeJS.id("trapdoor"), TrapdoorBlockBuilder.class, TrapdoorBlockBuilder::new);
});

registry.addDefault(Registries.ITEM, ItemBuilder.class, ItemBuilder::new);

registry.of(Registries.ITEM, reg -> {
reg.add("sword", SwordItemBuilder.class, SwordItemBuilder::new);
reg.add("pickaxe", DiggerItemBuilder.Pickaxe.class, DiggerItemBuilder.Pickaxe::new);
reg.add("axe", DiggerItemBuilder.Axe.class, DiggerItemBuilder.Axe::new);
reg.add("shovel", DiggerItemBuilder.Shovel.class, DiggerItemBuilder.Shovel::new);
reg.add("hoe", DiggerItemBuilder.Hoe.class, DiggerItemBuilder.Hoe::new);
reg.add("shears", ShearsItemBuilder.class, ShearsItemBuilder::new);
reg.add("helmet", ArmorItemBuilder.Helmet.class, ArmorItemBuilder.Helmet::new);
reg.add("chestplate", ArmorItemBuilder.Chestplate.class, ArmorItemBuilder.Chestplate::new);
reg.add("leggings", ArmorItemBuilder.Leggings.class, ArmorItemBuilder.Leggings::new);
reg.add("boots", ArmorItemBuilder.Boots.class, ArmorItemBuilder.Boots::new);
reg.add("animal_armor", ArmorItemBuilder.AnimalArmor.class, ArmorItemBuilder.AnimalArmor::new);
reg.add("smithing_template", SmithingTemplateItemBuilder.class, SmithingTemplateItemBuilder::new);
reg.add(KubeJS.id("sword"), SwordItemBuilder.class, SwordItemBuilder::new);
reg.add(KubeJS.id("pickaxe"), DiggerItemBuilder.Pickaxe.class, DiggerItemBuilder.Pickaxe::new);
reg.add(KubeJS.id("axe"), DiggerItemBuilder.Axe.class, DiggerItemBuilder.Axe::new);
reg.add(KubeJS.id("shovel"), DiggerItemBuilder.Shovel.class, DiggerItemBuilder.Shovel::new);
reg.add(KubeJS.id("hoe"), DiggerItemBuilder.Hoe.class, DiggerItemBuilder.Hoe::new);
reg.add(KubeJS.id("shears"), ShearsItemBuilder.class, ShearsItemBuilder::new);
reg.add(KubeJS.id("helmet"), ArmorItemBuilder.Helmet.class, ArmorItemBuilder.Helmet::new);
reg.add(KubeJS.id("chestplate"), ArmorItemBuilder.Chestplate.class, ArmorItemBuilder.Chestplate::new);
reg.add(KubeJS.id("leggings"), ArmorItemBuilder.Leggings.class, ArmorItemBuilder.Leggings::new);
reg.add(KubeJS.id("boots"), ArmorItemBuilder.Boots.class, ArmorItemBuilder.Boots::new);
reg.add(KubeJS.id("animal_armor"), ArmorItemBuilder.AnimalArmor.class, ArmorItemBuilder.AnimalArmor::new);
reg.add(KubeJS.id("smithing_template"), SmithingTemplateItemBuilder.class, SmithingTemplateItemBuilder::new);
});

registry.addDefault(Registries.FLUID, FluidBuilder.class, FluidBuilder::new);

registry.of(Registries.FLUID, reg -> {
reg.add("thin", ThinFluidBuilder.class, ThinFluidBuilder::new);
reg.add("thick", ThickFluidBuilder.class, ThickFluidBuilder::new);
reg.add(KubeJS.id("thin"), ThinFluidBuilder.class, ThinFluidBuilder::new);
reg.add(KubeJS.id("thick"), ThickFluidBuilder.class, ThickFluidBuilder::new);
});

registry.addDefault(NeoForgeRegistries.Keys.FLUID_TYPES, FluidTypeBuilder.class, FluidTypeBuilder::new);
Expand Down Expand Up @@ -745,6 +750,11 @@ public void registerLocalWebServer(LocalWebServerRegistry registry) {
KubeJSWeb.register(registry);
}

@Override
public void registerLocalWebServerWithAuth(LocalWebServerRegistry registry) {
KubeJSWeb.registerWithAuth(registry);
}

@Override
public void registerItemNameProviders(NameProvider.Registry<Item, ItemStack> registry) {
registry.register(Items.ENCHANTED_BOOK, (registries, stack) -> {
Expand Down Expand Up @@ -816,4 +826,12 @@ public void registerItemNameProviders(NameProvider.Registry<Item, ItemStack> reg
return null;
});
}

@Override
public void registerRecipeComponentValidatorTypes(RecipeComponentValidatorTypeRegistry registry) {
registry.register(NonEmptyValidator.TYPE);
registry.register(AlwaysValidValidator.TYPE);
registry.register(AndValidator.TYPE);
registry.register(OrValidator.TYPE);
}
}
7 changes: 5 additions & 2 deletions src/main/java/dev/latvian/mods/kubejs/KubeJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class KubeJS {
public static final String MC_VERSION_STRING = "1.21.1";
public static String QUERY;
public static String VERSION = "0";
public static String DISPLAY_NAME = "KubeJS";

public static ResourceLocation id(String path) {
return ResourceLocation.fromNamespaceAndPath(MOD_ID, path);
Expand All @@ -70,6 +71,7 @@ public KubeJS(IEventBus bus, Dist dist, ModContainer mod) throws Throwable {
modEventBus = bus;
thisMod = mod;
VERSION = mod.getModInfo().getVersion().toString();
DISPLAY_NAME = "KubeJS " + VERSION;
QUERY = "source=kubejs&mc=" + MC_VERSION_NUMBER + "&loader=neoforge&v=" + URLEncoder.encode(mod.getModInfo().getVersion().toString(), StandardCharsets.UTF_8);

if (Files.notExists(KubeJSPaths.README)) {
Expand Down Expand Up @@ -114,8 +116,9 @@ public KubeJS(IEventBus bus, Dist dist, ModContainer mod) throws Throwable {
var pluginTimer = Stopwatch.createStarted();
LOGGER.info("Looking for KubeJS plugins...");
var allMods = new ArrayList<>(ModList.get().getMods().stream().map(IModInfo::getOwningFile).map(IModFileInfo::getFile).toList());
allMods.remove(thisMod.getModInfo().getOwningFile().getFile());
allMods.addFirst(thisMod.getModInfo().getOwningFile().getFile());
var thisModFile = mod.getModInfo().getOwningFile().getFile();
allMods.remove(thisModFile);
allMods.addFirst(thisModFile);
KubeJSPlugins.load(allMods, dist == Dist.CLIENT);
LOGGER.info("Done in " + pluginTimer.stop());

Expand Down
1 change: 1 addition & 0 deletions src/main/java/dev/latvian/mods/kubejs/KubeJSCodecs.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

public interface KubeJSCodecs {
Codec<Character> CHARACTER = Codec.STRING.xmap(str -> str.charAt(0), Object::toString);
Codec<ResourceLocation> KUBEJS_ID = Codec.STRING.xmap(str -> str.indexOf(':') == -1 ? KubeJS.id(str) : ResourceLocation.parse(str), s -> s.getNamespace().equals(KubeJS.MOD_ID) ? s.getPath() : s.toString());

StreamCodec<? super RegistryFriendlyByteBuf, IntProvider> INT_PROVIDER_STREAM_CODEC = ByteBufCodecs.fromCodecWithRegistries(IntProvider.CODEC);

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/KubeJSCommon.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import dev.latvian.mods.kubejs.script.ConsoleLine;
import dev.latvian.mods.kubejs.script.ScriptType;
import dev.latvian.mods.kubejs.script.data.ExportablePackResources;
import dev.latvian.mods.kubejs.web.WebServerProperties;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.player.Player;
Expand Down Expand Up @@ -36,6 +37,7 @@ public void generateTypings(CommandSourceStack source) {
public void reloadConfig() {
CommonProperties.reload();
DevProperties.reload();
WebServerProperties.reload();
}

public void reloadStartupScripts(boolean dedicated) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.latvian.mods.kubejs.bindings;

import dev.latvian.mods.kubejs.KubeJS;
import dev.latvian.mods.kubejs.block.BlockBuilder;
import dev.latvian.mods.kubejs.block.custom.ButtonOrPressurePlateBuilder;
import dev.latvian.mods.kubejs.registry.RegistryKubeEvent;
Expand Down Expand Up @@ -58,39 +59,39 @@ public void register(Context cx, RegistryKubeEvent<Block> event, KubeResourceLoc
}

if (add(Blocks::slab)) {
builder.add((BlockBuilder) event.create(cx, id.withPath(p -> p + "_slab"), "slab"));
builder.add((BlockBuilder) event.create(cx, id.withPath(p -> p + "_slab"), new KubeResourceLocation(KubeJS.id("slab"))));
}

if (add(Blocks::stairs)) {
builder.add((BlockBuilder) event.create(cx, id.withPath(p -> p + "_stairs"), "stairs"));
builder.add((BlockBuilder) event.create(cx, id.withPath(p -> p + "_stairs"), new KubeResourceLocation(KubeJS.id("stairs"))));
}

if (add(Blocks::fence)) {
builder.add((BlockBuilder) event.create(cx, id.withPath(p -> p + "_fence"), "fence"));
builder.add((BlockBuilder) event.create(cx, id.withPath(p -> p + "_fence"), new KubeResourceLocation(KubeJS.id("fence"))));
}

if (add(Blocks::fenceGate)) {
builder.add((BlockBuilder) event.create(cx, id.withPath(p -> p + "_fence_gate"), "fence_gate"));
builder.add((BlockBuilder) event.create(cx, id.withPath(p -> p + "_fence_gate"), new KubeResourceLocation(KubeJS.id("fence_gate"))));
}

if (add(Blocks::wall)) {
builder.add((BlockBuilder) event.create(cx, id.withPath(p -> p + "_wall"), "wall"));
builder.add((BlockBuilder) event.create(cx, id.withPath(p -> p + "_wall"), new KubeResourceLocation(KubeJS.id("wall"))));
}

if (add(Blocks::pressurePlate)) {
builder.add((BlockBuilder) event.create(cx, id.withPath(p -> p + "_pressure_plate"), "pressure_plate"));
builder.add((BlockBuilder) event.create(cx, id.withPath(p -> p + "_pressure_plate"), new KubeResourceLocation(KubeJS.id("pressure_plate"))));
}

if (add(Blocks::button)) {
builder.add((BlockBuilder) event.create(cx, id.withPath(p -> p + "_button"), "button"));
builder.add((BlockBuilder) event.create(cx, id.withPath(p -> p + "_button"), new KubeResourceLocation(KubeJS.id("button"))));
}

if (add(Blocks::trapdoor)) {
builder.add((BlockBuilder) event.create(cx, id.withPath(p -> p + "_trapdoor"), "trapdoor"));
builder.add((BlockBuilder) event.create(cx, id.withPath(p -> p + "_trapdoor"), new KubeResourceLocation(KubeJS.id("trapdoor"))));
}

if (add(Blocks::door)) {
builder.add((BlockBuilder) event.create(cx, id.withPath(p -> p + "_door"), "door"));
builder.add((BlockBuilder) event.create(cx, id.withPath(p -> p + "_door"), new KubeResourceLocation(KubeJS.id("door"))));
}

for (var b : builder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.util.Arrays;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import java.util.StringJoiner;
import java.util.function.Function;
Expand Down Expand Up @@ -130,7 +131,7 @@ static String toTitleCase(String s, boolean ignoreSpecial) {
} else if (!ignoreSpecial && ALWAYS_LOWER_CASE.contains(s)) {
return s;
} else if (s.length() == 1) {
return s.toUpperCase();
return s.toUpperCase(Locale.ROOT);
}

char[] chars = s.toCharArray();
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/dev/latvian/mods/kubejs/bindings/TextIcons.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public interface TextIcons {
ResourceLocation FONT = KubeJS.id("icons");
Style STYLE = Style.EMPTY.withFont(FONT).applyFormat(ChatFormatting.WHITE);
Component NAME = Component.empty().append(icons("K.")).append(Component.literal(KubeJS.MOD_NAME));
String ALL_ICONS = "!+~ABCDEFIJKNPQTWY";

static MutableComponent icon(MutableComponent character) {
return character.setStyle(STYLE);
Expand Down Expand Up @@ -40,14 +41,14 @@ static MutableComponent tilde() {
return icons("~");
}

static MutableComponent blockTagIcon() {
return icons("B");
}

static MutableComponent crafting() {
return icons("A");
}

static MutableComponent blockTagIcon() {
return icons("B");
}

static MutableComponent copy() {
return icons("C");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.joml.Vector3f;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.function.Function;

Expand Down Expand Up @@ -101,7 +102,7 @@ public static MapColor wrap(Object o) {
} else if (s.charAt(0) == '#') {
return findClosest(Integer.decode(s.toString())).color;
} else {
return NAME_MAP.getOrDefault(s.toString().toLowerCase(), NONE).color;
return NAME_MAP.getOrDefault(s.toString().toLowerCase(Locale.ROOT), NONE).color;
}
} else if (o instanceof Number n) {
return findClosest(n.intValue()).color;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.lang.reflect.Modifier;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;

public class SoundTypeWrapper implements TypeWrapperFactory<SoundType> {
Expand All @@ -25,7 +26,7 @@ public Map<String, SoundType> getMap() {
for (var field : SoundType.class.getFields()) {
if (field.getType() == SoundType.class && Modifier.isPublic(field.getModifiers()) && Modifier.isStatic(field.getModifiers())) {
try {
map.put(field.getName().toLowerCase(), (SoundType) field.get(null));
map.put(field.getName().toLowerCase(Locale.ROOT), (SoundType) field.get(null));
} catch (Exception ex) {
ex.printStackTrace();
}
Expand All @@ -46,7 +47,7 @@ public SoundType wrap(Context cx, Object o, TypeInfo target) {
} else if (o == null || Undefined.isUndefined(o)) {
return SoundType.EMPTY;
} else {
return getMap().getOrDefault((o instanceof JsonElement j ? j.getAsString() : o.toString()).toLowerCase(), SoundType.EMPTY);
return getMap().getOrDefault((o instanceof JsonElement j ? j.getAsString() : o.toString()).toLowerCase(Locale.ROOT), SoundType.EMPTY);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.latvian.mods.kubejs.item.ItemPredicate;
import dev.latvian.mods.kubejs.script.ConsoleJS;
import dev.latvian.mods.kubejs.util.KubeResourceLocation;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.rhino.util.HideFromJS;
import net.minecraft.core.Direction;
Expand All @@ -17,8 +18,8 @@ public interface BlockEntityAttachmentHandler {
@HideFromJS
void attach(String id, BlockEntityAttachmentType type, Set<Direction> directions, BlockEntityAttachmentFactory factory);

default void attach(Context cx, String id, String type, Set<Direction> directions, Object args) {
var att = BlockEntityAttachmentType.ALL.get().get(type);
default void attach(Context cx, String id, KubeResourceLocation type, Set<Direction> directions, Object args) {
var att = BlockEntityAttachmentType.ALL.get().get(type.wrapped());

if (att != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package dev.latvian.mods.kubejs.block.entity;

import dev.latvian.mods.rhino.type.TypeInfo;
import net.minecraft.resources.ResourceLocation;

@FunctionalInterface
public interface BlockEntityAttachmentRegistry {
default void register(String name, Class<? extends BlockEntityAttachmentFactory> factory) {
register(new BlockEntityAttachmentType(name, TypeInfo.of(factory)));
default void register(ResourceLocation id, Class<? extends BlockEntityAttachmentFactory> factory) {
register(new BlockEntityAttachmentType(id, TypeInfo.of(factory)));
}

void register(BlockEntityAttachmentType type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@
import dev.latvian.mods.kubejs.plugin.KubeJSPlugins;
import dev.latvian.mods.kubejs.util.Lazy;
import dev.latvian.mods.rhino.type.TypeInfo;
import net.minecraft.resources.ResourceLocation;

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

public record BlockEntityAttachmentType(String name, TypeInfo typeInfo) {
public static final Lazy<Map<String, BlockEntityAttachmentType>> ALL = Lazy.of(() -> {
var map = new HashMap<String, BlockEntityAttachmentType>();
KubeJSPlugins.forEachPlugin(type -> map.put(type.name, type), KubeJSPlugin::registerBlockEntityAttachments);
public record BlockEntityAttachmentType(ResourceLocation id, TypeInfo typeInfo) {
public static final Lazy<Map<ResourceLocation, BlockEntityAttachmentType>> ALL = Lazy.of(() -> {
var map = new HashMap<ResourceLocation, BlockEntityAttachmentType>();
KubeJSPlugins.forEachPlugin(type -> map.put(type.id, type), KubeJSPlugin::registerBlockEntityAttachments);
return Map.copyOf(map);
});

public BlockEntityAttachmentType(String name, Class<?> type) {
this(name, TypeInfo.of(type));
public BlockEntityAttachmentType(ResourceLocation id, Class<?> type) {
this(id, TypeInfo.of(type));
}

@Override
public String toString() {
return name;
return id.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package dev.latvian.mods.kubejs.block.entity;

import dev.latvian.mods.kubejs.KubeJS;
import net.neoforged.neoforge.capabilities.BlockCapability;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.function.Supplier;

public record CustomCapabilityAttachment(BlockCapability<?, ?> capability, Object data) implements BlockEntityAttachment {
public static final BlockEntityAttachmentType TYPE = new BlockEntityAttachmentType("custom_capability", Factory.class);
public static final BlockEntityAttachmentType TYPE = new BlockEntityAttachmentType(KubeJS.id("custom_capability"), Factory.class);

public record Factory(BlockCapability<?, ?> type, Supplier<?> dataFactory) implements BlockEntityAttachmentFactory {
@Override
Expand Down
Loading

0 comments on commit 841690e

Please sign in to comment.