Skip to content

Commit

Permalink
i don't think you need to save and load gl states anymore
Browse files Browse the repository at this point in the history
add a JsonConfig constructor that accept direct file
make fabric api a compile only dependency
  • Loading branch information
deirn committed Mar 25, 2021
1 parent 13febd8 commit 7509488
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 37 deletions.
2 changes: 1 addition & 1 deletion common/src/main/java/mcp/mobius/waila/WailaPlugins.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void initializePlugins() {

protected static void createPlugin(String id, String initializer) {
try {
IWailaPlugin plugin = (IWailaPlugin) Class.forName(initializer).newInstance();
IWailaPlugin plugin = (IWailaPlugin) Class.forName(initializer).getConstructor().newInstance();
PLUGINS.put(id, plugin);
Waila.LOGGER.info("Discovered plugin {} at {}", id, plugin.getClass().getCanonicalName());
} catch (Exception e) {
Expand Down
31 changes: 0 additions & 31 deletions common/src/main/java/mcp/mobius/waila/overlay/OverlayRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,14 @@
import mcp.mobius.waila.api.impl.config.WailaConfig;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.ChatScreen;
import net.minecraft.client.render.DiffuseLighting;
import net.minecraft.client.util.math.MatrixStack;
import org.lwjgl.opengl.GL11;

import static mcp.mobius.waila.overlay.DisplayUtil.drawGradientRect;
import static mcp.mobius.waila.overlay.DisplayUtil.enable2DRender;
import static mcp.mobius.waila.overlay.DisplayUtil.renderStack;

public class OverlayRenderer {

protected static boolean hasLight;
protected static boolean hasDepthTest;
protected static boolean depthMask;
protected static int depthFunc;

public static Function<Tooltip, Rectangle> onPreRender;
public static Consumer<Rectangle> onPostRender;

Expand Down Expand Up @@ -62,7 +55,6 @@ public static void renderOverlay(MatrixStack matrices, Tooltip tooltip) {

MinecraftClient.getInstance().getProfiler().push("Waila Overlay");
RenderSystem.pushMatrix();
saveGLState();

float scale = Waila.CONFIG.get().getOverlay().getScale();
RenderSystem.scalef(scale, scale, 1.0F);
Expand All @@ -72,7 +64,6 @@ public static void renderOverlay(MatrixStack matrices, Tooltip tooltip) {
Rectangle rect = onPreRender.apply(tooltip);

if (rect == null) {
loadGLState();
RenderSystem.enableDepthTest();
RenderSystem.popMatrix();
MinecraftClient.getInstance().getProfiler().pop();
Expand All @@ -91,33 +82,11 @@ public static void renderOverlay(MatrixStack matrices, Tooltip tooltip) {

onPostRender.accept(rect);

loadGLState();
RenderSystem.enableDepthTest();
RenderSystem.popMatrix();
MinecraftClient.getInstance().getProfiler().pop();
}

public static void saveGLState() {
hasLight = GL11.glGetBoolean(GL11.GL_LIGHTING);
hasDepthTest = GL11.glGetBoolean(GL11.GL_DEPTH_TEST);
depthFunc = GL11.glGetInteger(GL11.GL_DEPTH_FUNC);
depthMask = GL11.glGetBoolean(GL11.GL_DEPTH_WRITEMASK);
}

public static void loadGLState() {
RenderSystem.depthMask(depthMask);
RenderSystem.depthFunc(depthFunc);
if (hasLight)
DiffuseLighting.enable();
else
DiffuseLighting.disable();

if (hasDepthTest)
RenderSystem.enableDepthTest();
else
RenderSystem.disableDepthTest();
}

public static void drawTooltipBox(MatrixStack matrices, int x, int y, int w, int h, int bg, int grad1, int grad2) {
drawGradientRect(matrices, x + 1, y, w - 1, 1, bg, bg);
drawGradientRect(matrices, x + 1, y + h, w - 1, 1, bg, bg);
Expand Down
12 changes: 8 additions & 4 deletions common/src/main/java/mcp/mobius/waila/utils/JsonConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public class JsonConfig<T> {
private final CachedSupplier<T> configGetter;
private Gson gson = DEFAULT_GSON;

public JsonConfig(String fileName, Class<T> configClass, Supplier<T> defaultFactory) {
this.configFile = Waila.configDir.resolve(fileName + (fileName.endsWith(".json") ? "" : ".json")).toFile();
public JsonConfig(File file, Class<T> configClass, Supplier<T> defaultFactory) {
this.configFile = file;
this.configGetter = new CachedSupplier<>(() -> {
if (!configFile.exists()) {
T def = defaultFactory.get();
Expand All @@ -36,11 +36,15 @@ public JsonConfig(String fileName, Class<T> configClass, Supplier<T> defaultFact
});
}

public JsonConfig(String fileName, Class<T> configClass, Supplier<T> defaultFactory) {
this(Waila.configDir.resolve(fileName + (fileName.endsWith(".json") ? "" : ".json")).toFile(), configClass, defaultFactory);
}

public JsonConfig(String fileName, Class<T> configClass) {
this(fileName, configClass, () -> {
try {
return configClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
return configClass.getConstructor().newInstance();
} catch (Exception e) {
throw new RuntimeException("Failed to create new config instance", e);
}
});
Expand Down
3 changes: 2 additions & 1 deletion fabric/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ repositories {
dependencies {
modImplementation "net.fabricmc:fabric-loader:$fabricLoader"

modImplementation "net.fabricmc.fabric-api:fabric-api:$fabricApi"
modCompileOnly "net.fabricmc.fabric-api:fabric-api:$fabricApi"
modRuntime "net.fabricmc.fabric-api:fabric-api:$fabricApi"

modCompileOnly "com.terraformersmc:modmenu:$modMenu"
modRuntime "com.terraformersmc:modmenu:$modMenu"
Expand Down

0 comments on commit 7509488

Please sign in to comment.