Skip to content

Commit

Permalink
Conversion to AtomicReference
Browse files Browse the repository at this point in the history
  • Loading branch information
dima-dencep committed Jun 5, 2023
1 parent 88e4454 commit c526521
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
25 changes: 16 additions & 9 deletions common/src/main/java/com/github/dimadencep/mods/rrls/Rrls.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,32 @@
import net.minecraft.client.util.math.MatrixStack;

import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;

public class Rrls {
public static AtomicReference<SplashOverlay> attachedOverlay = new AtomicReference<>();
protected final MinecraftClient client = MinecraftClient.getInstance();
public static SplashOverlay attachedOverlay;
public static ModConfig config;

public void init() {
config = AutoConfig.register(ModConfig.class, Toml4jConfigSerializer::new).get();
}

public void tickReload(MinecraftClient minecraft) {
if (Rrls.attachedOverlay != null) {
SplashOverlay overlay = Rrls.attachedOverlay.get();

if (overlay != null) {
minecraft.setOverlay(null);

if (Rrls.attachedOverlay.reload.isComplete()) {
if (overlay.reload.isComplete()) {
try {
Rrls.attachedOverlay.reload.throwException();
Rrls.attachedOverlay.exceptionHandler.accept(Optional.empty());
overlay.reload.throwException();
overlay.exceptionHandler.accept(Optional.empty());
} catch (Throwable var23) {
Rrls.attachedOverlay.exceptionHandler.accept(Optional.of(var23));
overlay.exceptionHandler.accept(Optional.of(var23));
}

Rrls.attachedOverlay = null;
Rrls.attachedOverlay.set(null);

if (Rrls.config.reInitScreen && minecraft.currentScreen != null) {
minecraft.currentScreen.init(minecraft, minecraft.getWindow().getScaledWidth(), minecraft.getWindow().getScaledHeight());
Expand All @@ -40,13 +43,17 @@ public void tickReload(MinecraftClient minecraft) {
}

public void renderText(MatrixStack stack, boolean isGame) {
if (Rrls.attachedOverlay != null && Rrls.config.showIn.canShow(isGame)) {
if (!Rrls.config.showIn.canShow(isGame)) return;

SplashOverlay overlay = Rrls.attachedOverlay.get();

if (overlay != null) {
int i = this.client.getWindow().getScaledWidth();
int s = (int) ((double) this.client.getWindow().getScaledHeight() * 0.8325);

int r = (int) (Math.min(i * 0.75, this.client.getWindow().getScaledHeight()) * 0.5);

Rrls.attachedOverlay.renderProgressBar(stack, i / 2 - r, s - 5, i / 2 + r, s + 5, 0.8F);
overlay.renderProgressBar(stack, i / 2 - r, s - 5, i / 2 + r, s + 5, 0.8F);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.resource.ResourceReload;
import net.minecraft.util.math.ColorHelper;
import net.minecraft.util.math.MathHelper;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
Expand All @@ -28,28 +27,31 @@ public abstract class SplashOverlayMixin extends Overlay {
public ResourceReload reload;
@Shadow
private float progress;
public boolean rrls_attach;

@Inject(at = @At("TAIL"), method = "<init>")
private void init(MinecraftClient client, ResourceReload monitor, Consumer<Optional<Throwable>> exceptionHandler, boolean reloading, CallbackInfo ci) { // TODO rewrite
if (Rrls.attachedOverlay != null && Rrls.attachedOverlay != (Object) this) {
Rrls.attachedOverlay.reload.whenComplete().cancel(true);
}
private void init(MinecraftClient client, ResourceReload monitor, Consumer<Optional<Throwable>> exceptionHandler, boolean reloading, CallbackInfo ci) {
if ((reloading && Rrls.config.enabled) || (!reloading && Rrls.config.loadingScreenHide)) {
SplashOverlay oldSplash = Rrls.attachedOverlay.getAndSet((SplashOverlay) (Object) this);

this.rrls_attach = true;

if ((reloading && Rrls.config.enabled) || (!reloading && Rrls.config.loadingScreenHide))
Rrls.attachedOverlay = (SplashOverlay) (Object) this;
if (oldSplash != null && oldSplash != (Object) this) {
oldSplash.reload.whenComplete().cancel(true);
}
}
}

@Inject(at = @At("HEAD"), method = "render", cancellable = true)
public void render(MatrixStack matrices, int mouseX, int mouseY, float delta, CallbackInfo ci) {
if (Rrls.attachedOverlay == (Object) this)
if (this.rrls_attach)
ci.cancel();
}

@Inject(at = @At("HEAD"), method = "renderProgressBar")
public void fixProgress(MatrixStack matrices, int minX, int minY, int maxX, int maxY, float opacity, CallbackInfo ci) {
if (Rrls.attachedOverlay == (Object) this) {
float t = this.reload.getProgress();
this.progress = MathHelper.clamp(this.progress * 0.95f + t * 0.050000012f, 0.0f, 1.0f);
if (this.rrls_attach) {
this.progress = this.reload.getProgress();
}
}

Expand All @@ -61,7 +63,7 @@ public void fixProgress(MatrixStack matrices, int minX, int minY, int maxX, int
)
)
public int rainbowProgress(int alpha, int red, int green, int blue) {
if (Rrls.config.rgbProgress && Rrls.attachedOverlay == (Object) this) {
if (Rrls.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
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx4G
forge_version = 1.19.4-45.0.66

# Mod Properties
mod_version = 2.0.0
mod_version = 2.0.2
maven_group = com.github.dima_dencep.mods
archives_base_name = Rrls

Expand Down

0 comments on commit c526521

Please sign in to comment.