Skip to content

Commit

Permalink
Fix KubeJS compat, refactor to prepare for more advanced integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Technici4n committed Nov 26, 2022
1 parent 7713aa2 commit 37e7deb
Show file tree
Hide file tree
Showing 12 changed files with 446 additions and 178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
public class ModernIndustrializationClient implements ClientModInitializer {
@Override
public void onInitializeClient() {
MIStartup.onClientStartup();

setupScreens();
MIFluidsRender.setupFluidRenders();
RenderHelper.setupRenderHelper();
Expand Down
62 changes: 62 additions & 0 deletions src/main/java/aztech/modern_industrialization/MIStartup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* MIT License
*
* Copyright (c) 2020 Azercoco & Technici4n
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package aztech.modern_industrialization;

import net.fabricmc.api.ModInitializer;
import net.fabricmc.loader.api.FabricLoader;

/**
* Class to work around Fabric not allowing ordering between loaded mods.
* If KubeJS is loaded and the integration is enabled, we only init MI from the KubeJS plugin.
*/
public class MIStartup implements ModInitializer {
private static boolean initialized = false;

private static void initialize() {
if (initialized) {
throw new IllegalStateException("MIStartup#initialize should only be called once");
}

initialized = true;
ModernIndustrialization.initialize();
}

@Override
public void onInitialize() {
if (!FabricLoader.getInstance().isModLoaded("kubejs")) {
initialize();
}
}

public static void onClientStartup() {
// Sanity check
if (!initialized) {
throw new IllegalStateException("MI client init is called but MI hasn't been initialized yet?");
}
}

public static void onKubejsPluginLoaded() {
initialize();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import aztech.modern_industrialization.proxy.CommonProxy;
import java.util.Comparator;
import java.util.Map;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.client.itemgroup.FabricItemGroupBuilder;
import net.fabricmc.fabric.api.entity.event.v1.ServerEntityWorldChangeEvents;
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
Expand All @@ -69,7 +68,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class ModernIndustrialization implements ModInitializer {
public class ModernIndustrialization {

public static final String MOD_ID = "modern_industrialization";
public static final Logger LOGGER = LogManager.getLogger("Modern Industrialization");
Expand All @@ -87,11 +86,7 @@ public class ModernIndustrialization implements ModInitializer {
public static final MenuType<ForgeHammerScreenHandler> SCREEN_HANDLER_FORGE_HAMMER = ScreenHandlerRegistry
.registerSimple(new MIIdentifier("forge_hammer"), ForgeHammerScreenHandler::new);

@Override
public void onInitialize() {
// This code runs as soon as Minecraft is in a mod-load-ready state.
// However, some things (like resources) may still be uninitialized.
// Proceed with mild caution.
public static void initialize() {
MIMaterials.init();
MIMachineRecipeTypes.init();
SingleBlockCraftingMachines.init();
Expand Down Expand Up @@ -128,21 +123,21 @@ public void onInitialize() {
LOGGER.info("Modern Industrialization setup done!");
}

private void setupItems() {
private static void setupItems() {
MIItem.ITEMS.entrySet().stream().sorted(Comparator.comparing(e -> e.getValue().sortOrder)).forEach(entry -> {
Registry.register(Registry.ITEM, entry.getKey(), entry.getValue().asItem());
entry.getValue().onRegister();
});
}

private void setupBlocks() {
private static void setupBlocks() {
for (Map.Entry<ResourceLocation, BlockDefinition<?>> entry : MIBlock.BLOCKS.entrySet()) {
Registry.register(Registry.BLOCK, entry.getKey(), entry.getValue().asBlock());
entry.getValue().onRegister();
}
}

private void setupFluids() {
private static void setupFluids() {
for (Map.Entry<ResourceLocation, FluidDefinition> entry : MIFluids.FLUIDS.entrySet()) {
Registry.register(Registry.BLOCK, entry.getKey(), entry.getValue().fluidBlock);
Registry.register(Registry.FLUID, entry.getKey(), entry.getValue().asFluid());
Expand All @@ -153,7 +148,7 @@ private void setupFluids() {
}
}

private void setupPackets() {
private static void setupPackets() {
ServerPlayNetworking.registerGlobalReceiver(ConfigurableInventoryPackets.SET_LOCKING_MODE,
ConfigurableInventoryPacketHandlers.C2S.SET_LOCKING_MODE);
ServerPlayNetworking.registerGlobalReceiver(ConfigurableInventoryPackets.DO_SLOT_DRAGGING,
Expand All @@ -175,7 +170,7 @@ private static void addFuel(String id, int burnTicks) {
FuelRegistry.INSTANCE.add(item, burnTicks);
}

private void setupFuels() {
private static void setupFuels() {
addFuel("coke", 6400);
addFuel("coke_dust", 6400);
addFuel("coke_block", Short.MAX_VALUE); // F*** YOU VANILLA ! (Should be 6400*9 but it overflows ...)
Expand Down Expand Up @@ -205,7 +200,7 @@ private void setupFuels() {
FluidFuelRegistry.register(MIFluids.BOOSTED_DIESEL, 800);
}

private void setupWrench() {
private static void setupWrench() {
UseBlockCallback.EVENT.register((player, world, hand, hitResult) -> {
if (player.isSpectator() || !world.mayInteract(player, hitResult.getBlockPos())) {
return InteractionResult.PASS;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* MIT License
*
* Copyright (c) 2020 Azercoco & Technici4n
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package aztech.modern_industrialization.compat.kubejs;

public class KubeJSFacade {
public static KubeJSFacade instance = new KubeJSFacade();

public void fireAddMaterialsEvent() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* MIT License
*
* Copyright (c) 2020 Azercoco & Technici4n
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package aztech.modern_industrialization.compat.kubejs;

import aztech.modern_industrialization.compat.kubejs.event.AddMaterialsEventJS;
import aztech.modern_industrialization.compat.kubejs.event.MIMaterialKubeJSEvents;

public class LoadedKubeJSFacade extends KubeJSFacade {
@Override
public void fireAddMaterialsEvent() {
MIMaterialKubeJSEvents.ADD_MATERIALS.post(new AddMaterialsEventJS());
}
}
Loading

0 comments on commit 37e7deb

Please sign in to comment.