Skip to content

Commit

Permalink
initialize module after waila loaded to avoid rng crash
Browse files Browse the repository at this point in the history
  • Loading branch information
deirn committed Mar 30, 2021
1 parent fd6d857 commit 0e693ca
Show file tree
Hide file tree
Showing 15 changed files with 115 additions and 131 deletions.
2 changes: 1 addition & 1 deletion megane-api/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"icon" : "assets/megane/icon.png",
"environment" : "*",
"depends" : {
"waila": "*"
"wthit": "*"
},
"custom" : {
"modmenu": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package badasintended.megane.runtime;

import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;

import badasintended.megane.api.MeganeModule;
import badasintended.megane.config.MeganeConfig;
import badasintended.megane.runtime.component.AlignResetComponent;
import badasintended.megane.runtime.component.block.BeaconComponent;
import badasintended.megane.runtime.component.block.BeeHiveComponent;
Expand Down Expand Up @@ -27,8 +33,13 @@
import badasintended.megane.runtime.renderer.InventoryRenderer;
import badasintended.megane.runtime.renderer.ProgressRenderer;
import badasintended.megane.runtime.renderer.StatusEffectRenderer;
import mcp.mobius.waila.Waila;
import mcp.mobius.waila.api.IRegistrar;
import mcp.mobius.waila.api.IWailaPlugin;
import net.fabricmc.api.EnvType;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.metadata.CustomValue;
import net.fabricmc.loader.api.metadata.ModMetadata;
import net.minecraft.block.BeaconBlock;
import net.minecraft.block.BeehiveBlock;
import net.minecraft.block.Block;
Expand All @@ -38,11 +49,19 @@
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.Identifier;

import static badasintended.megane.runtime.util.RuntimeUtils.oldConfigVersion;
import static badasintended.megane.runtime.util.RuntimeUtils.showUpdatedConfigToast;
import static badasintended.megane.util.MeganeUtils.CONFIG;
import static badasintended.megane.util.MeganeUtils.CONFIG_VERSION;
import static badasintended.megane.util.MeganeUtils.LOGGER;
import static badasintended.megane.util.MeganeUtils.MODID;
import static badasintended.megane.util.MeganeUtils.config;
import static badasintended.megane.util.MeganeUtils.hasMod;
import static badasintended.megane.util.MeganeUtils.id;
import static mcp.mobius.waila.api.TooltipPosition.HEAD;
import static mcp.mobius.waila.api.TooltipPosition.TAIL;

public class MeganeWaila implements IWailaPlugin {
public class Megane implements IWailaPlugin {

public static final Identifier INVENTORY = id("inventory");
public static final Identifier BAR = id("bar");
Expand Down Expand Up @@ -98,6 +117,73 @@ public void register(IRegistrar r) {
r.registerEntityDataProvider(new EntityInventoryData(), ENTITY);
r.registerEntityDataProvider(new PetOwnerData(), ENTITY);
r.registerEntityDataProvider(new StatusEffectData(), ENTITY);

// Modules
FabricLoader loader = FabricLoader.getInstance();

Path conf = loader.getConfigDir();
File file = conf.resolve(Waila.MODID + "/" + MODID + ".json").toFile();
if (file.exists()) {
int version = config().configVersion;
if (version != CONFIG_VERSION)
try {
File old = conf.resolve(Waila.MODID + "/" + MODID + ".json.old").normalize().toFile();
old.delete();
file.renameTo(old);

MeganeConfig config = new MeganeConfig();
config.configVersion = CONFIG_VERSION;
CONFIG.write(config, true);

LOGGER.warn(
"[megane] Config reset because of different version ({} instead of {}), old config is available at {}",
version, CONFIG_VERSION, old
);
oldConfigVersion = version;
showUpdatedConfigToast = true;
} catch (Exception e) {
// no-op
}
} else {
config().configVersion = CONFIG_VERSION;
CONFIG.save();
}

loader.getAllMods().forEach(mod -> {
ModMetadata metadata = mod.getMetadata();
String modId = metadata.getId();
if (metadata.containsCustomValue("megane:modules")) {
metadata.getCustomValue("megane:modules").getAsArray().forEach(value -> {
boolean satisfied = true;
String className;
if (value.getType() == CustomValue.CvType.OBJECT) {
CustomValue.CvObject object = value.getAsObject();
className = object.get("init").getAsString();
if (object.containsKey("deps"))
for (CustomValue dep : object.get("deps").getAsArray()) {
satisfied = satisfied && hasMod(dep.getAsString());
}
} else {
className = value.getAsString();
}
satisfied = satisfied && config().modules
.computeIfAbsent(modId, s -> new HashMap<>())
.computeIfAbsent(className, s -> true);
CONFIG.save();
if (satisfied)
try {
LOGGER.info("[megane] Loading {} from {}", className, modId);
MeganeModule entry = (MeganeModule) Class.forName(className).getDeclaredConstructor().newInstance();
entry.initialize();
if (loader.getEnvironmentType() == EnvType.CLIENT)
entry.initializeClient();
} catch (Exception e) {
LOGGER.error("[megane] error when loading {} from {}", className, modId);
e.printStackTrace();
}
});
}
});
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.List;

import badasintended.megane.runtime.MeganeWaila;
import badasintended.megane.runtime.Megane;
import mcp.mobius.waila.api.IDataAccessor;
import mcp.mobius.waila.api.RenderableTextComponent;
import net.minecraft.nbt.CompoundTag;
Expand Down Expand Up @@ -30,7 +30,7 @@ protected void append(List<Text> tooltip, IDataAccessor accessor) {
data.putString(S_LV_STR + i, str);
}

tooltip.add(new RenderableTextComponent(MeganeWaila.EFFECT, data));
tooltip.add(new RenderableTextComponent(Megane.EFFECT, data));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.List;

import badasintended.megane.runtime.MeganeWaila;
import badasintended.megane.runtime.Megane;
import mcp.mobius.waila.api.IDataAccessor;
import mcp.mobius.waila.api.RenderableTextComponent;
import net.minecraft.block.BeehiveBlock;
Expand Down Expand Up @@ -36,7 +36,7 @@ protected void append(List<Text> tooltip, IDataAccessor accessor) {
TAG.putDouble(B_STORED, level);
TAG.putDouble(B_MAX, 5);
TAG.putString(B_PREFIX, I18n.translate("megane.level"));
tooltip.add(new RenderableTextComponent(MeganeWaila.BAR, TAG));
tooltip.add(new RenderableTextComponent(Megane.BAR, TAG));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.List;

import badasintended.megane.runtime.MeganeWaila;
import badasintended.megane.runtime.Megane;
import mcp.mobius.waila.api.IDataAccessor;
import mcp.mobius.waila.api.RenderableTextComponent;
import net.minecraft.nbt.CompoundTag;
Expand All @@ -26,7 +26,7 @@ protected void append(List<Text> tooltip, IDataAccessor accessor) {
if (data.getBoolean(I_HAS) && data.getInt(P_PERCENT) == 0 && data.getInt("progress") == 0 && !config().progress.isShowWhenZero()) {
data.putInt(I_MAX_W, config().inventory.getMaxWidth());
data.putInt(I_MAX_H, config().inventory.getMaxHeight());
tooltip.add(new RenderableTextComponent(MeganeWaila.INVENTORY, data));
tooltip.add(new RenderableTextComponent(Megane.INVENTORY, data));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.List;

import badasintended.megane.runtime.MeganeWaila;
import badasintended.megane.runtime.Megane;
import mcp.mobius.waila.api.IDataAccessor;
import mcp.mobius.waila.api.RenderableTextComponent;
import net.minecraft.block.BlockState;
Expand Down Expand Up @@ -36,7 +36,7 @@ protected void append(List<Text> tooltip, IDataAccessor accessor) {
TAG.putDouble(B_STORED, level);
TAG.putDouble(B_MAX, 3);
TAG.putString(B_PREFIX, I18n.translate("megane.level"));
tooltip.add(new RenderableTextComponent(MeganeWaila.BAR, TAG));
tooltip.add(new RenderableTextComponent(Megane.BAR, TAG));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.List;

import badasintended.megane.runtime.MeganeWaila;
import badasintended.megane.runtime.Megane;
import mcp.mobius.waila.api.IDataAccessor;
import mcp.mobius.waila.api.RenderableTextComponent;
import net.minecraft.block.BlockState;
Expand Down Expand Up @@ -36,7 +36,7 @@ protected void append(List<Text> tooltip, IDataAccessor accessor) {
TAG.putDouble(B_STORED, level);
TAG.putDouble(B_MAX, 7);
TAG.putString(B_PREFIX, I18n.translate("megane.level"));
tooltip.add(new RenderableTextComponent(MeganeWaila.BAR, TAG));
tooltip.add(new RenderableTextComponent(Megane.BAR, TAG));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import badasintended.megane.api.provider.EnergyInfoProvider;
import badasintended.megane.config.MeganeConfig;
import badasintended.megane.runtime.MeganeWaila;
import badasintended.megane.runtime.Megane;
import mcp.mobius.waila.api.IDataAccessor;
import mcp.mobius.waila.api.RenderableTextComponent;
import net.minecraft.client.resource.language.I18n;
Expand Down Expand Up @@ -78,7 +78,7 @@ protected void append(List<Text> tooltip, IDataAccessor accessor) {
TAG.putDouble(B_MAX, max);
TAG.putBoolean(B_LONG, expand);
TAG.putString(B_UNIT, unit);
tooltip.add(new RenderableTextComponent(MeganeWaila.BAR, TAG));
tooltip.add(new RenderableTextComponent(Megane.BAR, TAG));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import badasintended.megane.api.provider.FluidInfoProvider;
import badasintended.megane.api.registry.TooltipRegistry;
import badasintended.megane.runtime.MeganeWaila;
import badasintended.megane.runtime.Megane;
import mcp.mobius.waila.api.IDataAccessor;
import mcp.mobius.waila.api.RenderableTextComponent;
import net.minecraft.fluid.Fluid;
Expand Down Expand Up @@ -90,7 +90,7 @@ protected void append(List<Text> tooltip, IDataAccessor accessor) {
TAG.putBoolean(B_LONG, expand);
TAG.putString(B_PREFIX, name);

tooltip.add(new RenderableTextComponent(MeganeWaila.BAR, TAG));
tooltip.add(new RenderableTextComponent(Megane.BAR, TAG));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.List;

import badasintended.megane.runtime.MeganeWaila;
import badasintended.megane.runtime.Megane;
import mcp.mobius.waila.api.IDataAccessor;
import mcp.mobius.waila.api.RenderableTextComponent;
import net.minecraft.nbt.CompoundTag;
Expand All @@ -22,7 +22,7 @@ public ProgressComponent() {
protected void append(List<Text> tooltip, IDataAccessor accessor) {
CompoundTag data = accessor.getServerData();
if (data.getBoolean(P_HAS) && (data.getInt(P_PERCENT) > 0 || config().progress.isShowWhenZero())) {
tooltip.add(new RenderableTextComponent(MeganeWaila.PROGRESS, data));
tooltip.add(new RenderableTextComponent(Megane.PROGRESS, data));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.List;

import badasintended.megane.runtime.MeganeWaila;
import badasintended.megane.runtime.Megane;
import mcp.mobius.waila.api.IEntityAccessor;
import mcp.mobius.waila.api.RenderableTextComponent;
import net.minecraft.nbt.CompoundTag;
Expand All @@ -25,7 +25,7 @@ protected void append(List<Text> tooltip, IEntityAccessor accessor) {
if (data.getBoolean(I_HAS)) {
data.putInt(I_MAX_W, config().entityInventory.getMaxWidth());
data.putInt(I_MAX_H, config().entityInventory.getMaxHeight());
tooltip.add(new RenderableTextComponent(MeganeWaila.INVENTORY, data));
tooltip.add(new RenderableTextComponent(Megane.INVENTORY, data));
}
}

Expand Down
Loading

0 comments on commit 0e693ca

Please sign in to comment.