Skip to content

Commit

Permalink
Start separating logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tr7zw committed Aug 1, 2024
1 parent 1c62af8 commit fb7cab2
Show file tree
Hide file tree
Showing 14 changed files with 383 additions and 223 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public class Config {
public ComponentSettings crosshairSettings = new ComponentSettings(false, 20);
public ComponentSettings bossbarSettings = new ComponentSettings(true, 5);


@Getter
@Setter
public static class ComponentSettings {
Expand Down
115 changes: 0 additions & 115 deletions src/main/java/dev/tr7zw/exordium/BufferManager.java

This file was deleted.

13 changes: 11 additions & 2 deletions src/main/java/dev/tr7zw/exordium/ExordiumModBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem;

import dev.tr7zw.exordium.components.BufferManager;
import dev.tr7zw.exordium.config.ExordiumConfigScreen;
import dev.tr7zw.exordium.util.CustomShaderManager;
import dev.tr7zw.exordium.util.DelayedRenderCallManager;
Expand All @@ -40,8 +41,8 @@ public abstract class ExordiumModBase {
private final DelayedRenderCallManager delayedRenderCallManager = new DelayedRenderCallManager();
@Getter
private final CustomShaderManager customShaderManager = new CustomShaderManager();
@Getter
private final BufferManager bufferManager = new BufferManager();
private boolean lateInit = true;

void onInitialize() {
instance = this;
Expand All @@ -61,7 +62,6 @@ void onInitialize() {
writeConfig(); // Config got modified
}
}
bufferManager.initialize();
initModloader();
}

Expand Down Expand Up @@ -92,4 +92,13 @@ public static void correctBlendMode() {
GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA);
}

public BufferManager getBufferManager() {
// FIXME
if (lateInit) {
bufferManager.initialize();
lateInit = false;
}
return bufferManager;
}

}
16 changes: 13 additions & 3 deletions src/main/java/dev/tr7zw/exordium/access/ChatAccess.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package dev.tr7zw.exordium.access;

import dev.tr7zw.exordium.access.VanillaBufferAccess.ChatOverlayAccess;
import java.util.List;

public interface ChatAccess extends ChatOverlayAccess {
import net.minecraft.client.GuiMessage;

void updateState(int tickCount);
public interface ChatAccess {

List<GuiMessage.Line> getTrimmedMessages();

int getChatScollbarPos();

boolean isChatFocused();

boolean isChatHidden();

int getLinesPerPage();

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ interface DebugOverlayAccess extends VanillaBufferAccess {

}

interface ChatOverlayAccess extends VanillaBufferAccess {

BufferedComponent getChatOverlayBuffer();

}

interface CrosshairOverlayAccess extends VanillaBufferAccess {

BufferedComponent exordium_getCrosshairOverlayBuffer();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dev.tr7zw.exordium.components;

public interface BufferComponent<T> {

public void captureState(T context);

public boolean hasChanged(int tickCount, T context);

}
44 changes: 44 additions & 0 deletions src/main/java/dev/tr7zw/exordium/components/BufferInstance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package dev.tr7zw.exordium.components;

import java.util.function.Supplier;

import dev.tr7zw.exordium.util.BufferedComponent;
import dev.tr7zw.exordium.versionless.config.Config;
import lombok.Getter;
import net.minecraft.resources.ResourceLocation;

public final class BufferInstance<T> {

@Getter
private final ResourceLocation id;
private final BufferComponent<T> component;
private final BufferedComponent buffer;

BufferInstance(ResourceLocation id, BufferComponent<T> component, Supplier<Config.ComponentSettings> settings) {
this.id = id;
this.component = component;
this.buffer = new BufferedComponent(settings);
}

/**
* Tries to render the buffer. Returns false if the buffer was not rendered and
* the normal render logic should be used.
*
* @param context
* @return
*/
public boolean renderBuffer(int ticks, T context) {
return buffer.render(() -> component.hasChanged(ticks, context));
}

/**
* This method should be called after the render logic, no matter if the buffer
* or normal render logic was used.
*
* @param context
*/
public void postRender(T context) {
buffer.renderEnd(() -> component.captureState(context));
}

}
133 changes: 133 additions & 0 deletions src/main/java/dev/tr7zw/exordium/components/BufferManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package dev.tr7zw.exordium.components;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

import dev.tr7zw.exordium.ExordiumModBase;
import dev.tr7zw.exordium.access.ChatAccess;
import dev.tr7zw.exordium.access.GuiAccess;
import dev.tr7zw.exordium.access.TablistAccess;
import dev.tr7zw.exordium.access.VanillaBufferAccess.CrosshairOverlayAccess;
import dev.tr7zw.exordium.access.VanillaBufferAccess.DebugOverlayAccess;
import dev.tr7zw.exordium.access.VanillaBufferAccess.ExperienceBarOverlayAccess;
import dev.tr7zw.exordium.access.VanillaBufferAccess.HotbarOverlayAccess;
import dev.tr7zw.exordium.access.VanillaBufferAccess.ScoreBoardOverlayAccess;
import dev.tr7zw.exordium.access.VanillaBufferAccess.VignetteOverlayAccess;
import dev.tr7zw.exordium.components.vanilla.ChatComponent;
import dev.tr7zw.exordium.util.BufferedComponent;
import dev.tr7zw.exordium.versionless.config.Config;
import lombok.NoArgsConstructor;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.scores.DisplaySlot;
import net.minecraft.world.scores.Scoreboard;

@NoArgsConstructor
public class BufferManager {

// private final Map<ResourceLocation, Function<Gui, BufferedComponent>> vanillaBuffers = new HashMap<>();
// private final Map<ResourceLocation, Consumer<HandlerData>> customHandlers = new HashMap<>();
// private final Map<ResourceLocation, Runnable> customEndHandlers = new HashMap<>();
private final Map<ResourceLocation, BufferInstance<?>> buffers = new HashMap<>();

public void initialize() {
ExordiumModBase inst = ExordiumModBase.instance;
Minecraft minecraft = Minecraft.getInstance();
// vanillaBuffers.put(new ResourceLocation("debug_text"),
// gui -> ((DebugOverlayAccess) gui).getDebugOverlayBuffer());
// vanillaBuffers.put(new ResourceLocation("crosshair"),
// gui -> ((CrosshairOverlayAccess) gui).exordium_getCrosshairOverlayBuffer());
// vanillaBuffers.put(new ResourceLocation("experience_bar"),
// gui -> ((ExperienceBarOverlayAccess) gui).getExperienceBarOverlayBuffer());
// vanillaBuffers.put(new ResourceLocation("scoreboard"),
// gui -> ((ScoreBoardOverlayAccess) gui).getScoreBoardOverlayBuffer());
// vanillaBuffers.put(new ResourceLocation("hotbar"), gui -> ((HotbarOverlayAccess) gui).getHotbarOverlayBuffer());
// registerCustomHandler(new ResourceLocation("vignette"), data -> {
// VignetteOverlayAccess vignette = (VignetteOverlayAccess) minecraft.gui;
// if (ExordiumModBase.instance.config.vignetteSettings.isEnabled()) {
// if (!vignette.getVignetteOverlayBuffer().render()) {
// vignette.renderCustomVignette(data.gui());
// }
// data.cancel().set(true);
// }
// vignette.getVignetteOverlayBuffer().renderEnd();
// });
// registerCustomHandler(new ResourceLocation("chat_panel"), data -> {
// GuiAccess guiAccess = (GuiAccess) minecraft.gui;
// ChatAccess chatAccess = (ChatAccess) guiAccess.getChatComponent();
// chatAccess.updateState(guiAccess.getTickCount());
// BufferedComponent bufferedComponent = chatAccess.getChatOverlayBuffer();
// if (bufferedComponent.render()) {
// data.cancel().set(true);
// }
// });
registerBuffer(ChatComponent.getId(), new ChatComponent(), () -> inst.config.chatSettings);

// registerCustomEndHandler(new ResourceLocation("chat_panel"), () -> {
// GuiAccess guiAccess = (GuiAccess) minecraft.gui;
// ChatAccess chatAccess = (ChatAccess) guiAccess.getChatComponent();
// BufferedComponent bufferedComponent = chatAccess.getChatOverlayBuffer();
// bufferedComponent.renderEnd();
// });
// registerCustomHandler(new ResourceLocation("player_list"), data -> {
// GuiAccess guiAccess = (GuiAccess) minecraft.gui;
// TablistAccess tablistAccess = (TablistAccess) guiAccess.getPlayerTabOverlay();
// Scoreboard scoreboard = minecraft.level.getScoreboard();
// tablistAccess.updateState(scoreboard, scoreboard.getDisplayObjective(DisplaySlot.LIST));
// BufferedComponent bufferedComponent = tablistAccess.getPlayerListOverlayBuffer();
// if (bufferedComponent.render()) {
// data.cancel().set(true);
// }
// });
// registerCustomEndHandler(new ResourceLocation("player_list"), () -> {
// GuiAccess guiAccess = (GuiAccess) minecraft.gui;
// TablistAccess tabAccess = (TablistAccess) guiAccess.getPlayerTabOverlay();
// BufferedComponent bufferedComponent = tabAccess.getPlayerListOverlayBuffer();
// bufferedComponent.renderEnd();
// });
}

@SuppressWarnings("unchecked")
public <T> BufferInstance<T> getBufferInstance(ResourceLocation id, Class<T> context) {
return (BufferInstance<T>) buffers.get(id);
}

public void registerBuffer(ResourceLocation id, BufferComponent<?> component,
Supplier<Config.ComponentSettings> settings) {
this.buffers.put(id, new BufferInstance<>(id, component, settings));
}
//
// public BufferedComponent getBufferedComponent(ResourceLocation resourceLocation, Gui gui) {
// Function<Gui, BufferedComponent> vanFun = vanillaBuffers.get(resourceLocation);
// if (vanFun != null) {
// return vanFun.apply(gui);
// }
// return null;
// }
//
// public Consumer<HandlerData> getCustomHandler(ResourceLocation resourceLocation) {
// return customHandlers.get(resourceLocation);
// }
//
// public Runnable getCustomEndHandler(ResourceLocation resourceLocation) {
// return customEndHandlers.get(resourceLocation);
// }
//
// public void registerCustomHandler(ResourceLocation resourceLocation, Consumer<HandlerData> handler) {
// customHandlers.put(resourceLocation, handler);
// }
//
// public void registerCustomEndHandler(ResourceLocation resourceLocation, Runnable handler) {
// customEndHandlers.put(resourceLocation, handler);
// }
//
// public record HandlerData(GuiGraphics gui, AtomicBoolean cancel) {
// }

}
Loading

0 comments on commit fb7cab2

Please sign in to comment.