Skip to content

Commit

Permalink
Fixed client script loading, added local startup and server scripts f…
Browse files Browse the repository at this point in the history
…or dedicated servers, fixed stencil icons
  • Loading branch information
LatvianModder committed Jun 18, 2024
1 parent 2328e65 commit bca8686
Show file tree
Hide file tree
Showing 35 changed files with 297 additions and 196 deletions.
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ dependencies {
modApi("dev.architectury:architectury-neoforge:$architectury_version")
modApi("dev.latvian.mods:rhino:$rhino_version") { transitive(false) }

// todo: reevaluate if we want to depend on jei
modCompileOnly("mezz.jei:jei-1.21-neoforge:$jei_version") { transitive(false) }
modCompileOnly("me.shedaniel:RoughlyEnoughItems-neoforge:$rei_version") { transitive(false) }
}
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ rei_version=16.0.729
#rei_comp_version=9.0.16
#forge_bookshelf_version=11.0.3
#forge_gamestages_version=7.+
jei_version=19.0.0.1
jei_version=19.0.0.3
18 changes: 2 additions & 16 deletions src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,19 @@
import dev.latvian.mods.kubejs.helpers.IngredientHelper;
import dev.latvian.mods.kubejs.item.ArmorMaterialBuilder;
import dev.latvian.mods.kubejs.item.ChancedItem;
import dev.latvian.mods.kubejs.item.ItemBuilder;
import dev.latvian.mods.kubejs.item.ItemEnchantmentsWrapper;
import dev.latvian.mods.kubejs.item.ItemStackJS;
import dev.latvian.mods.kubejs.item.ItemTintFunction;
import dev.latvian.mods.kubejs.item.ItemToolTiers;
import dev.latvian.mods.kubejs.item.JukeboxSongBuilder;
import dev.latvian.mods.kubejs.item.creativetab.CreativeTabBuilder;
import dev.latvian.mods.kubejs.item.custom.ArmorItemBuilder;
import dev.latvian.mods.kubejs.item.custom.BasicItemJS;
import dev.latvian.mods.kubejs.item.custom.DiggerItemBuilder;
import dev.latvian.mods.kubejs.item.custom.ItemToolTierRegistryKubeEvent;
import dev.latvian.mods.kubejs.item.custom.ShearsItemBuilder;
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.level.ruletest.KubeJSRuleTests;
import dev.latvian.mods.kubejs.misc.BasicMobEffect;
import dev.latvian.mods.kubejs.misc.CustomStatBuilder;
import dev.latvian.mods.kubejs.misc.PaintingVariantBuilder;
Expand Down Expand Up @@ -218,18 +216,6 @@
public class BuiltinKubeJSPlugin implements KubeJSPlugin {
public static final HashMap<String, Object> GLOBAL = new HashMap<>();

@Override
public void initStartup() {
ItemEvents.TOOL_TIER_REGISTRY.post(ScriptType.STARTUP, new ItemToolTierRegistryKubeEvent(ItemBuilder.TOOL_TIERS));
KubeJSRuleTests.init();

/*
for (var types : RegistryObjectBuilderTypes.MAP.values()) {
// types.postEvent();
}
*/
}

@Override
public void registerBuilderTypes(BuilderTypeRegistry registry) {
registry.addDefault(Registries.SOUND_EVENT, SoundEventBuilder.class, SoundEventBuilder::new);
Expand Down Expand Up @@ -533,7 +519,7 @@ public void registerTypeWrappers(TypeWrapperRegistry registry) {
registry.register(FluidIngredient.class, FluidWrapper::wrapIngredient);
registry.register(RecipeFilter.class, RecipeFilter::of);
registry.register(SlotFilter.class, SlotFilter::wrap);
registry.register(Tier.class, ItemBuilder::toolTierOf);
registry.register(Tier.class, ItemToolTiers::wrap);
registry.register(PlayerSelector.class, PlayerSelector::of);
registry.register(DamageSource.class, DamageSourceWrapper::of);
registry.register(EntitySelector.class, UtilsJS::entitySelector);
Expand Down
22 changes: 15 additions & 7 deletions src/main/java/dev/latvian/mods/kubejs/KubeJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import com.google.common.base.Stopwatch;
import dev.latvian.mods.kubejs.bindings.event.StartupEvents;
import dev.latvian.mods.kubejs.client.ClientScriptManager;
import dev.latvian.mods.kubejs.client.KubeJSClient;
import dev.latvian.mods.kubejs.event.KubeStartupEvent;
import dev.latvian.mods.kubejs.gui.KubeJSMenus;
import dev.latvian.mods.kubejs.ingredient.KubeJSIngredients;
import dev.latvian.mods.kubejs.item.creativetab.CreativeTabCallbackForge;
import dev.latvian.mods.kubejs.item.creativetab.CreativeTabKubeEvent;
import dev.latvian.mods.kubejs.item.creativetab.KubeJSCreativeTabs;
import dev.latvian.mods.kubejs.level.ruletest.KubeJSRuleTests;
import dev.latvian.mods.kubejs.recipe.KubeJSRecipeSerializers;
import dev.latvian.mods.kubejs.registry.BuilderTypeRegistryHandler;
import dev.latvian.mods.kubejs.registry.RegistryKubeEvent;
Expand Down Expand Up @@ -131,6 +133,7 @@ public KubeJS(IEventBus bus, Dist dist, ModContainer mod) throws Throwable {
new KubeJSBackgroundThread().start();
// Required to be called this way because ConsoleJS class hasn't been initialized yet
ScriptType.STARTUP.console.setCapturingErrors(true);
ScriptType.CLIENT.console.setCapturingErrors(true);
}

LOGGER.info("Loading vanilla registries...");
Expand All @@ -147,23 +150,27 @@ public KubeJS(IEventBus bus, Dist dist, ModContainer mod) throws Throwable {
KubeJSPlugins.forEachPlugin(KubeJSPlugin::init);
KubeJSPlugins.forEachPlugin(new BuilderTypeRegistryHandler(), KubeJSPlugin::registerBuilderTypes);

startupScriptManager = new ScriptManager(ScriptType.STARTUP);
clientScriptManager = new ScriptManager(ScriptType.CLIENT);

startupScriptManager = new StartupScriptManager();
startupScriptManager.reload();

if (dist.isClient()) {
clientScriptManager = new ClientScriptManager();
clientScriptManager.reload();
}

KubeJSPlugins.forEachPlugin(KubeJSPlugin::initStartup);

for (var key : StartupEvents.REGISTRY.findUniqueExtraIds(ScriptType.STARTUP)) {
StartupEvents.REGISTRY.post(new RegistryKubeEvent<>((ResourceKey) key), key);
}

GeneratedResourcePack.scanForInvalidFiles("kubejs/assets/", KubeJSPaths.ASSETS);
if (dist.isClient()) {
GeneratedResourcePack.scanForInvalidFiles("kubejs/assets/", KubeJSPaths.ASSETS);
}

GeneratedResourcePack.scanForInvalidFiles("kubejs/data/", KubeJSPaths.DATA);

if (CommonProperties.get().serverOnly) {
// FIXME ModLoadingContext.get().registerExtensionPoint(DisplayTest.class, () -> new DisplayTest(() -> DisplayTest.IGNORESERVERONLY, (a, b) -> true));
} else {
if (dist.isClient() || !CommonProperties.get().serverOnly) {
// See NeoForgeRegistriesSetup.VANILLA_SYNC_REGISTRIES
NeoForgeMod.enableMilkFluid();
KubeJSIngredients.REGISTRY.register(bus);
Expand All @@ -173,6 +180,7 @@ public KubeJS(IEventBus bus, Dist dist, ModContainer mod) throws Throwable {
}

KubeJSCreativeTabs.REGISTRY.register(bus);
KubeJSRuleTests.REGISTRY.register(bus);

StartupEvents.INIT.post(ScriptType.STARTUP, KubeStartupEvent.BASIC);
// KubeJSRegistries.chunkGenerators().register(new ResourceLocation(KubeJS.MOD_ID, "flat"), () -> KJSFlatLevelSource.CODEC);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/KubeJSPaths.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ static Path dir(Path dir) {
Path LOCAL_DEV_PROPERTIES = LOCAL.resolve("dev.json");
Path EXPORT = dir(LOCAL.resolve("export"));
Path EXPORTED_PACKS = dir(LOCAL.resolve("exported_packs"));
Path LOCAL_STARTUP_SCRIPTS = dir(LOCAL.resolve("local_server_scripts"));
Path LOCAL_SERVER_SCRIPTS = dir(LOCAL.resolve("local_server_scripts"));

static Path get(PackType type) {
return type == PackType.CLIENT_RESOURCES ? ASSETS : DATA;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/KubeJSPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import dev.latvian.mods.kubejs.recipe.viewer.RecipeViewerEntryType;
import dev.latvian.mods.kubejs.registry.BuilderTypeRegistry;
import dev.latvian.mods.kubejs.script.BindingRegistry;
import dev.latvian.mods.kubejs.script.ScriptManager;
import dev.latvian.mods.kubejs.script.ScriptType;
import dev.latvian.mods.kubejs.script.TypeDescriptionRegistry;
import dev.latvian.mods.kubejs.script.TypeWrapperRegistry;
Expand Down Expand Up @@ -113,4 +114,7 @@ default void exportServerData(DataExport export) {
*/
default void injectRuntimeRecipes(RecipesKubeEvent event, RecipeManagerKJS manager, Map<ResourceLocation, RecipeHolder<?>> recipesByName) {
}

default void beforeScriptsLoaded(ScriptManager manager) {
}
}
20 changes: 20 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/StartupScriptManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.latvian.mods.kubejs;

import dev.latvian.mods.kubejs.script.ScriptManager;
import dev.latvian.mods.kubejs.script.ScriptType;
import net.neoforged.fml.loading.FMLLoader;

public class StartupScriptManager extends ScriptManager {
public StartupScriptManager() {
super(ScriptType.STARTUP);
}

@Override
public void loadFromDirectory() {
super.loadFromDirectory();

if (FMLLoader.getDist().isDedicatedServer()) {
loadPackFromDirectory(KubeJSPaths.LOCAL_STARTUP_SCRIPTS, "local startup", true);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dev.latvian.mods.kubejs.client;

import dev.latvian.mods.kubejs.script.ScriptManager;
import dev.latvian.mods.kubejs.script.ScriptType;

public class ClientScriptManager extends ScriptManager {
public ClientScriptManager() {
super(ScriptType.CLIENT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import net.minecraft.Util;
import net.minecraft.resources.ResourceLocation;

import java.io.IOException;
import java.util.Map;
import java.util.function.Consumer;

Expand Down Expand Up @@ -42,7 +41,7 @@ public void addMultipartBlockState(ResourceLocation id, Consumer<MultipartBlockS
add(ResourceLocation.fromNamespaceAndPath(id.getNamespace(), "blockstates/" + id.getPath()), gen.toJson());
}

public void stencil(ResourceLocation target, ResourceLocation stencil, Map<Color, Color> remap) throws IOException {
public void stencil(ResourceLocation target, ResourceLocation stencil, Map<Color, Color> remap) {
generator.stencil(target, stencil, remap);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ public void export(List<ExportablePackResources> packs) {

@Override
public void openErrors(ScriptType type) {
Minecraft.getInstance().execute(() -> Minecraft.getInstance().setScreen(new KubeJSErrorScreen(Minecraft.getInstance().screen, type.console)));
Minecraft.getInstance().execute(() -> Minecraft.getInstance().setScreen(new KubeJSErrorScreen(Minecraft.getInstance().screen, type.console, true)));
}

@Override
public void openErrors(ScriptType type, List<ConsoleLine> errors, List<ConsoleLine> warnings) {
Minecraft.getInstance().execute(() -> Minecraft.getInstance().setScreen(new KubeJSErrorScreen(Minecraft.getInstance().screen, type, null, errors, warnings)));
Minecraft.getInstance().execute(() -> Minecraft.getInstance().setScreen(new KubeJSErrorScreen(Minecraft.getInstance().screen, type, null, errors, warnings, true)));
}

public static void loadPostChains(Minecraft mc) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,11 @@ public static void worldRender(RenderLevelStageEvent event) {
@Nullable
public static Screen setScreen(Screen screen) {
if (screen instanceof TitleScreen && !ConsoleJS.STARTUP.errors.isEmpty() && CommonProperties.get().startupErrorGUI) {
return new KubeJSErrorScreen(screen, ConsoleJS.STARTUP);
return new KubeJSErrorScreen(screen, ConsoleJS.STARTUP, false);
}

if (screen instanceof TitleScreen && !ConsoleJS.CLIENT.errors.isEmpty() && CommonProperties.get().startupErrorGUI) {
return new KubeJSErrorScreen(screen, ConsoleJS.CLIENT, false);
}

return screen;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,24 @@ public class KubeJSErrorScreen extends Screen {
public final Path logFile;
public final List<ConsoleLine> errors;
public final List<ConsoleLine> warnings;
public final boolean canClose;
public List<ConsoleLine> viewing;
private ErrorList list;

public KubeJSErrorScreen(Screen lastScreen, ScriptType scriptType, @Nullable Path logFile, List<ConsoleLine> errors, List<ConsoleLine> warnings) {
public KubeJSErrorScreen(Screen lastScreen, ScriptType scriptType, @Nullable Path logFile, List<ConsoleLine> errors, List<ConsoleLine> warnings, boolean canClose) {
super(Component.empty());
this.lastScreen = lastScreen;
this.scriptType = scriptType;
this.logFile = logFile;
this.errors = errors;
this.warnings = warnings;
this.canClose = canClose;

this.viewing = errors.isEmpty() && !warnings.isEmpty() ? warnings : errors;
}

public KubeJSErrorScreen(Screen lastScreen, ConsoleJS console) {
this(lastScreen, console.scriptType, console.scriptType.getLogFile(), new ArrayList<>(console.errors), new ArrayList<>(console.warnings));
public KubeJSErrorScreen(Screen lastScreen, ConsoleJS console, boolean canClose) {
this(lastScreen, console.scriptType, console.scriptType.getLogFile(), new ArrayList<>(console.errors), new ArrayList<>(console.warnings), canClose);
}

@Override
Expand All @@ -72,11 +74,11 @@ protected void init() {

if (CommonProperties.get().startupErrorReportUrl.isBlank()) {
openLog = this.addRenderableWidget(Button.builder(Component.literal("Open Log File"), this::openLog).bounds(this.width / 2 - 155, i, 150, 20).build());
this.addRenderableWidget(Button.builder(Component.literal(scriptType.isStartup() ? "Quit" : "Close"), this::quit).bounds(this.width / 2 - 155 + 160, i, 150, 20).build());
this.addRenderableWidget(Button.builder(Component.literal(canClose ? "Close" : "Quit"), this::quit).bounds(this.width / 2 - 155 + 160, i, 150, 20).build());
} else {
openLog = this.addRenderableWidget(Button.builder(Component.literal("Open Log File"), this::openLog).bounds(this.width / 4 - 55, i, 100, 20).build());
this.addRenderableWidget(Button.builder(Component.literal("Report"), this::report).bounds(this.width / 2 - 50, i, 100, 20).build());
this.addRenderableWidget(Button.builder(Component.literal(scriptType.isStartup() ? "Quit" : "Close"), this::quit).bounds(this.width * 3 / 4 - 45, i, 100, 20).build());
this.addRenderableWidget(Button.builder(Component.literal(canClose ? "Close" : "Quit"), this::quit).bounds(this.width * 3 / 4 - 45, i, 100, 20).build());
}

openLog.active = logFile != null;
Expand All @@ -89,10 +91,10 @@ protected void init() {
}

private void quit(Button button) {
if (scriptType.isStartup()) {
minecraft.stop();
} else {
if (canClose) {
onClose();
} else {
minecraft.stop();
}
}

Expand Down Expand Up @@ -121,7 +123,7 @@ public void render(GuiGraphics guiGraphics, int mx, int my, float delta) {

@Override
public boolean shouldCloseOnEsc() {
return !scriptType.isStartup();
return canClose;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ public LoadedTexture(BufferedImage img, @Nullable byte[] mcmeta) {
}

public byte[] toBytes() {
if (width == 0 || height == 0) {
return new byte[0];
}

var img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
img.setRGB(0, 0, width, height, pixels, 0, width);

Expand Down
9 changes: 4 additions & 5 deletions src/main/java/dev/latvian/mods/kubejs/event/EventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,10 @@ public boolean hasListeners() {
}

public void listen(@Nullable Context cx, ScriptType type, @Nullable Object extraId, IEventHandler handler) {
if (!type.manager.get().canListenEvents) {
throw new IllegalStateException("Event handler '" + this + "' can only be registered during script loading!");
if (cx != null) {
if (!((KubeJSContext) cx).kjsFactory.manager.canListenEvents) {
throw new IllegalStateException("Event handler '" + this + "' can only be registered during script loading!");
}
}

if (!scriptTypePredicate.test(type)) {
Expand Down Expand Up @@ -136,10 +138,7 @@ protected EventHandlerContainer[] createMap(@Nullable Object extraId) {

@HideFromJS
public void listenJava(ScriptType type, @Nullable Object extraId, IEventHandler handler) {
var b = type.manager.get().canListenEvents;
type.manager.get().canListenEvents = true;
listen(null, type, extraId, handler);
type.manager.get().canListenEvents = b;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public static ResourceLocation asItemModelLocation(ResourceLocation id) {
}

public void texture(ResourceLocation target, LoadedTexture texture) {
if (texture.width <= 0 || texture.height <= 0) {
ConsoleJS.CLIENT.error("Failed to load texture " + target);
return;
}

add(ResourceLocation.fromNamespaceAndPath(target.getNamespace(), "textures/" + target.getPath() + ".png"), texture::toBytes);

if (texture.mcmeta != null) {
Expand All @@ -59,7 +64,8 @@ public void texture(ResourceLocation target, LoadedTexture texture) {
}

public void stencil(ResourceLocation target, ResourceLocation stencil, Map<Color, Color> colors) {
texture(target, loadTexture(stencil).remap(colors));
var stencilTexture = loadTexture(stencil);
texture(target, stencilTexture.remap(colors));
}

public boolean mask(ResourceLocation target, ResourceLocation mask, ResourceLocation input) {
Expand Down
Loading

0 comments on commit bca8686

Please sign in to comment.