Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
dima-dencep committed Aug 22, 2023
1 parent a5d8ae7 commit bf0009c
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 70 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Expand Up @@ -42,9 +42,9 @@ allprojects {
maven { url "https://maven.terraformersmc.com/releases/" }
}

tasks.withType(JavaCompile) {
tasks.withType(JavaCompile).configureEach {
options.encoding = "UTF-8"
options.release = 17
options.release.set(17)
}

java {
Expand Down
14 changes: 8 additions & 6 deletions common/src/main/java/com/github/dimadencep/mods/rrls/Rrls.java
Expand Up @@ -10,20 +10,22 @@
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;

public class Rrls {
public static final StackWalker classWalker = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);
public static final ModConfig config = AutoConfig.register(ModConfig.class, Toml4jConfigSerializer::new).get();
public static final Logger logger = LogManager.getLogger("RRLS");

protected final MinecraftClient client = MinecraftClient.getInstance();
public static final ModConfig MOD_CONFIG = AutoConfig.register(ModConfig.class, Toml4jConfigSerializer::new).get();
public static final Logger LOGGER = LogManager.getLogger("rrls");

@Nullable
public static Overlay tryGetOverlay(Overlay original) {
if (original instanceof SplashAccessor accessor && accessor.isAttached()) {
if (original instanceof SplashAccessor accessor && accessor.rrls$isAttached()) {
return null;
}

return original;
}

public static Optional<SplashAccessor> getAccessor() {
return Optional.ofNullable(MinecraftClient.getInstance().overlay instanceof SplashAccessor accessor && accessor.rrls$isAttached() ? accessor : null);
}
}
Expand Up @@ -3,9 +3,15 @@
import net.minecraft.client.gui.DrawContext;

public interface SplashAccessor {
boolean isAttached();

void render(DrawContext context, boolean isGame);
@Deprecated(forRemoval = true, since = "2.2.0")
default boolean isAttached() {
return rrls$isAttached();
}

void reload();
boolean rrls$isAttached();

void rrls$render(DrawContext context, boolean isGame);

void rrls$reload();
}
@@ -1,7 +1,6 @@
package com.github.dimadencep.mods.rrls.mixins;

import com.github.dimadencep.mods.rrls.Rrls;
import com.github.dimadencep.mods.rrls.accessor.SplashAccessor;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Overlay;
import net.minecraft.text.Text;
Expand All @@ -12,7 +11,6 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.concurrent.CompletableFuture;

Expand All @@ -32,8 +30,8 @@ public abstract class MinecraftClientMixin {
cancellable = true
)
public void onResourceReloadFailure(Throwable exception, Text resourceName, CallbackInfo ci) {
if (!Rrls.config.resetResources) {
Rrls.logger.error("Caught error loading resourcepacks!", exception);
if (!Rrls.MOD_CONFIG.resetResources) {
Rrls.LOGGER.error("Caught error loading resourcepacks!", exception);

this.reloadResources(true).thenRun(() -> this.showResourceReloadFailureToast(resourceName));

Expand All @@ -54,18 +52,4 @@ public void onResourceReloadFailure(Throwable exception, Text resourceName, Call
public Overlay fixOverlayUsage(MinecraftClient instance) {
return Rrls.tryGetOverlay(instance.overlay);
}

@Inject(
method = "getOverlay",
at = @At(
value = "HEAD"
)
)
public void getOverlay(CallbackInfoReturnable<Overlay> cir) { // TODO rewrite
if (cir.getReturnValue() instanceof SplashAccessor accessor && accessor.isAttached()) {
Class<?> callerClass = Rrls.classWalker.getCallerClass();

Rrls.logger.warn("Illegal getOverlay access: {}", callerClass.getName());
}
}
}
Expand Up @@ -55,11 +55,11 @@ public abstract class SplashOverlayMixin extends Overlay implements SplashAccess
)
)
private void init(MinecraftClient client, ResourceReload monitor, Consumer<Optional<Throwable>> exceptionHandler, boolean reloading, CallbackInfo ci) {
this.rrls_attach = (reloading && Rrls.config.enabled) || (!reloading && Rrls.config.loadingScreenHide);
this.rrls_attach = (reloading && Rrls.MOD_CONFIG.enabled) || (!reloading && Rrls.MOD_CONFIG.loadingScreenHide);
}

@Override
public boolean isAttached() {
public boolean rrls$isAttached() {
return this.rrls_attach;
}

Expand All @@ -76,8 +76,8 @@ public void pauses(CallbackInfoReturnable<Boolean> cir) {
}

@Override
public void render(DrawContext context, boolean isGame) {
if (!Rrls.config.showIn.canShow(isGame)) return;
public void rrls$render(DrawContext context, boolean isGame) {
if (!Rrls.MOD_CONFIG.showIn.canShow(isGame)) return;

int i = context.getScaledWindowWidth();
int j = context.getScaledWindowHeight();
Expand All @@ -89,7 +89,7 @@ public void render(DrawContext context, boolean isGame) {
}

@Override
public void reload() {
public void rrls$reload() {
long l = Util.getMeasuringTimeMs();
if (this.reloading && this.reloadStartTime == -1L) {
this.reloadStartTime = l;
Expand All @@ -113,7 +113,7 @@ public void reload() {

this.reloadCompleteTime = Util.getMeasuringTimeMs();

if (Rrls.config.reInitScreen && this.client.currentScreen != null) {
if (Rrls.MOD_CONFIG.reInitScreen && this.client.currentScreen != null) {
this.client.currentScreen.init(this.client, this.client.getWindow().getScaledWidth(), this.client.getWindow().getScaledHeight());
}
}
Expand All @@ -139,7 +139,7 @@ public void render(DrawContext context, int mouseX, int mouseY, float delta, Cal
)
)
public int rainbowProgress(int alpha, int red, int green, int blue) {
if (Rrls.config.rgbProgress && this.rrls_attach) {
if (Rrls.MOD_CONFIG.rgbProgress && this.rrls_attach) {
int baseColor = ThreadLocalRandom.current().nextInt(0, 0xFFFFFF);

return ColorHelper.Argb.getArgb(alpha, ColorHelper.Argb.getRed(baseColor), ColorHelper.Argb.getGreen(baseColor), ColorHelper.Argb.getBlue(baseColor));
Expand Down
@@ -1,7 +1,6 @@
package com.github.dimadencep.mods.rrls.fabric;

import com.github.dimadencep.mods.rrls.Rrls;
import com.github.dimadencep.mods.rrls.accessor.SplashAccessor;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents;
Expand All @@ -10,14 +9,14 @@ public class RrlsFabric extends Rrls implements ClientModInitializer {

@Override
public void onInitializeClient() {
ScreenEvents.AFTER_INIT.register((client1, screen, scaledWidth, scaledHeight) -> ScreenEvents.afterRender(screen).register((screen1, drawContext, mouseX, mouseY, tickDelta) -> {
if (this.client.overlay instanceof SplashAccessor accessor && accessor.isAttached())
accessor.render(drawContext, false);
}));
ScreenEvents.AFTER_INIT.register((client1, screen, scaledWidth, scaledHeight) ->
ScreenEvents.afterRender(screen).register((screen1, drawContext, mouseX, mouseY, tickDelta) ->
getAccessor().ifPresent(splashAccessor -> splashAccessor.rrls$render(drawContext, false))
)
);

HudRenderCallback.EVENT.register((drawContext, tickDelta) -> {
if (this.client.overlay instanceof SplashAccessor accessor && accessor.isAttached())
accessor.render(drawContext, true);
});
HudRenderCallback.EVENT.register((drawContext, tickDelta) ->
getAccessor().ifPresent(splashAccessor -> splashAccessor.rrls$render(drawContext, true))
);
}
}
@@ -1,29 +1,23 @@
package com.github.dimadencep.mods.rrls.fabric.mixins;

import com.github.dimadencep.mods.rrls.accessor.SplashAccessor;
import com.github.dimadencep.mods.rrls.fabric.RrlsFabric;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Overlay;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(MinecraftClient.class)
public class MinecraftClientMixin {
@Shadow
public Overlay overlay;

@Inject(
method = "render",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/client/render/GameRenderer;render(FJZ)V",
shift = At.Shift.AFTER
target = "Lnet/minecraft/client/render/GameRenderer;render(FJZ)V"
)
)
public void handleReload(boolean tick, CallbackInfo ci) {
if (this.overlay instanceof SplashAccessor accessor && accessor.isAttached())
accessor.reload();
RrlsFabric.getAccessor().ifPresent(SplashAccessor::rrls$reload);
}
}
11 changes: 4 additions & 7 deletions fabric/src/main/resources/fabric.mod.json
Expand Up @@ -3,14 +3,14 @@
"id": "rrls",
"version": "${version}",
"name": "Remove Reloading Screen",
"description": "Removes the resources reload screen, and leaves the game available",
"description": "Makes resource packs load in the background, allowing you to do other things while waiting!",
"authors": [
"dima_dencep"
],
"contact": {
"homepage": "https://modrinth.com/mod/rrls",
"sources": "https://github.com/dimadencep/RemoveReloadingScreen",
"issues": "https://github.com/dimadencep/RemoveReloadingScreen/issues"
"homepage": "https://curseforge.com/minecraft/mc-mods/rrls",
"sources": "https://github.com/dima-dencep/rrls",
"issues": "https://github.com/dima-dencep/rrls/issues"
},
"license": "AGPL-3.0-only",
"icon": "icon.png",
Expand All @@ -32,8 +32,5 @@
"fabric-api": "*",
"minecraft": "~1.20",
"cloth-config": "*"
},
"recommends": {
"modmenu": "*"
}
}
Expand Up @@ -38,19 +38,17 @@ public RrlsForge() {

@SubscribeEvent
public void onRenderGui(RenderGuiEvent.Pre event) {
if (this.client.overlay instanceof SplashAccessor accessor && accessor.isAttached())
accessor.render(event.getGuiGraphics(), true);
getAccessor().ifPresent(splashAccessor -> splashAccessor.rrls$render(event.getGuiGraphics(), true));
}

@SubscribeEvent
public void onScreenRender(ScreenEvent.Render event) {
if (this.client.overlay instanceof SplashAccessor accessor && accessor.isAttached())
accessor.render(event.getGuiGraphics(), false);
getAccessor().ifPresent(splashAccessor -> splashAccessor.rrls$render(event.getGuiGraphics(), false));
}

@SubscribeEvent
public void onTick(TickEvent.RenderTickEvent event) {
if (event.phase == TickEvent.Phase.END && this.client.overlay instanceof SplashAccessor accessor && accessor.isAttached())
accessor.reload();
if (event.phase == TickEvent.Phase.START)
getAccessor().ifPresent(SplashAccessor::rrls$reload);
}
}
6 changes: 3 additions & 3 deletions forge/src/main/resources/META-INF/mods.toml
@@ -1,16 +1,16 @@
modLoader = "javafml"
loaderVersion = "*"
issueTrackerURL = "https://github.com/dimadencep/RemoveReloadingScreen/issues"
issueTrackerURL = "https://github.com/dima-dencep/rrls/issues"
license = "AGPL-3.0-only"

[[mods]]
modId = "rrls"
version = "${version}"
displayURL = "https://modrinth.com/mod/rrls"
displayURL = "https://curseforge.com/minecraft/mc-mods/rrls"
displayName = "Remove Reloading Screen"
updateJSONURL = "https://api.modrinth.com/updates/rrls/forge_updates.json"
authors = "dima_dencep"
description = "Removes the resources reload screen, and leaves the game available"
description = "Makes resource packs load in the background, allowing you to do other things while waiting!"
logoFile = "icon.png"

[[dependencies.rrls]]
Expand Down

0 comments on commit bf0009c

Please sign in to comment.