Skip to content

Commit

Permalink
Refactors/Cleanup for mixins combined with making such a client mod s…
Browse files Browse the repository at this point in the history
…pecifically
  • Loading branch information
Dragon-Seeker committed Aug 14, 2024
1 parent ac51a65 commit bd06f04
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 84 deletions.
5 changes: 3 additions & 2 deletions src/main/java/smsk/smoothscroll/SmoothSc.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package smsk.smoothscroll;

import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
Expand All @@ -14,7 +15,7 @@
import org.slf4j.LoggerFactory;
import smsk.smoothscroll.compat.CondensedInventoryCompat;

public class SmoothSc implements ModInitializer {
public class SmoothSc implements ClientModInitializer {
public static final Logger LOGGER = LoggerFactory.getLogger("Smooth Scrolling");
public static final MinecraftClient mc = MinecraftClient.getInstance();

Expand All @@ -33,7 +34,7 @@ public class SmoothSc implements ModInitializer {
public static int hotbarRollover = 0;

@Override
public void onInitialize() {
public void onInitializeClient() {
isSmoothScrollingRefurbishedLoaded = FabricLoader.getInstance().isModLoaded("smoothscrollingrefurbished");
isCondensedInventoryLoaded = FabricLoader.getInstance().isModLoaded("condensed_creative");
updateConfig();
Expand Down
55 changes: 30 additions & 25 deletions src/main/java/smsk/smoothscroll/mixin/Chat/ChatHudMixin.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package smsk.smoothscroll.mixin.Chat;

import java.util.List;

import com.llamalad7.mixinextras.sugar.Local;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArgs;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
Expand All @@ -23,21 +27,19 @@
@Mixin(value = ChatHud.class, priority = 1001) // i want mods to modify the chat position before, so i get to know where they put it
public class ChatHudMixin {

@Shadow int scrolledLines;
@Shadow List<ChatHudLine.Visible> visibleMessages;
@Shadow private int scrolledLines;
@Final @Shadow private List<ChatHudLine.Visible> visibleMessages;

float scrollOffset;
float maskHeightBuffer;
boolean refreshing = false;
int scrollValBefore;
DrawContext savedContext;
int savedCurrentTick;
Vec2f mtc = new Vec2f(0, 0); // matrix translate
int shownLineCount;
@Unique private float scrollOffset;
@Unique private float maskHeightBuffer;
@Unique private boolean refreshing = false;
@Unique private int scrollValBefore;
@Unique private int savedCurrentTick;
@Unique private Vec2f mtc = new Vec2f(0, 0); // matrix translate
@Unique private int shownLineCount;

@Inject(method = "render", at = @At("HEAD"))
public void renderH(DrawContext context, int currentTick, int mouseX, int mouseY, boolean focused, CallbackInfo ci) {
savedContext = context;
private void renderH(DrawContext context, int currentTick, int mouseX, int mouseY, boolean focused, CallbackInfo ci) {
if (Config.cfg.chatSpeed == 0) return;
savedCurrentTick = currentTick;

Expand All @@ -60,7 +62,7 @@ private void matrixTranslateCorrector(Args args) {
}

@ModifyVariable(method = "render", at = @At("STORE"), ordinal = 7)
private int mask(int m) { // m - the y position of the chat
private int mask(int m, @Local(argsOnly = true) DrawContext context) { // m - the y position of the chat
if ((Config.cfg.chatSpeed == 0 && Config.cfg.chatOpeningSpeed == 0) || isChatHidden()) return (m);

var shownLineCount = 0;
Expand Down Expand Up @@ -99,7 +101,7 @@ private int mask(int m) { // m - the y position of the chat
maskbottom -= distance;
}

savedContext.enableScissor(0, masktop, savedContext.getScaledWindowWidth(), maskbottom);
context.enableScissor(0, masktop, context.getScaledWindowWidth(), maskbottom);
return (m);
}

Expand All @@ -116,43 +118,43 @@ private int changePosY(int y) {
}

@ModifyVariable(method = "render", at = @At("STORE"))
private long demask(long a) { // after the cycle
private long demask(long a, @Local(argsOnly = true) DrawContext context) { // after the cycle
if ((Config.cfg.chatSpeed == 0 && Config.cfg.chatOpeningSpeed == 0) || this.isChatHidden()) return (a);
if (Config.cfg.enableMaskDebug) savedContext.fill(-100, -100, savedContext.getScaledWindowWidth(), savedContext.getScaledWindowHeight(), ColorHelper.Argb.getArgb(50, 255, 0, 255));
savedContext.disableScissor();
if (Config.cfg.enableMaskDebug) context.fill(-100, -100, context.getScaledWindowWidth(), context.getScaledWindowHeight(), ColorHelper.Argb.getArgb(50, 255, 0, 255));
context.disableScissor();
return (a);
}

@Inject(method = "render", at = @At("TAIL"))
public void renderT(DrawContext context, int currentTick, int mouseX, int mouseY, boolean focused, CallbackInfo ci) {
private void renderT(DrawContext context, int currentTick, int mouseX, int mouseY, boolean focused, CallbackInfo ci) {
if (Config.cfg.chatSpeed == 0) return;
scrolledLines = scrollValBefore;
}

@ModifyVariable(method = "addVisibleMessage", at = @At("STORE"), ordinal = 0)
List<OrderedText> onNewMessage(List<OrderedText> ot) {
private List<OrderedText> onNewMessage(List<OrderedText> ot) {
if (refreshing) return (ot);
scrollOffset -= ot.size() * getLineHeight();
return (ot);
}

@Inject(method = "scroll", at = @At("HEAD"))
public void scrollH(int scroll, CallbackInfo ci) {
private void scrollH(int scroll, CallbackInfo ci) {
scrollValBefore = scrolledLines;
}

@Inject(method = "scroll", at = @At("TAIL"))
public void scrollT(int scroll, CallbackInfo ci) {
private void scrollT(int scroll, CallbackInfo ci) {
scrollOffset += (scrolledLines - scrollValBefore) * getLineHeight();
}

@Inject(method = "resetScroll", at = @At("HEAD"))
public void scrollResetH(CallbackInfo ci) {
private void scrollResetH(CallbackInfo ci) {
scrollValBefore = scrolledLines;
}

@Inject(method = "resetScroll", at = @At("TAIL"))
public void scrollResetT(CallbackInfo ci) {
private void scrollResetT(CallbackInfo ci) {
scrollOffset += (scrolledLines - scrollValBefore) * getLineHeight();
}

Expand Down Expand Up @@ -192,10 +194,13 @@ private int addLinesUnder(int r) {
@Shadow
public boolean isChatFocused() {return (false);}

int getChatDrawOffset() {
@Unique
private int getChatDrawOffset() {
return Math.round(scrollOffset) - (Math.round(scrollOffset) / getLineHeight() * getLineHeight());
}
int getChatScrollOffset() {

@Unique
private int getChatScrollOffset() {
return Math.round(scrollOffset);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import java.util.List;

import com.llamalad7.mixinextras.sugar.Local;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArg;
Expand All @@ -21,18 +24,17 @@

@Mixin(SuggestionWindow.class)
public class SuggestionWindowMixin {
@Shadow int inWindowIndex;
@Shadow List<Suggestion> suggestions;
@Shadow Rect2i area;
DrawContext savedContext;
int indexBefore;
float scrollPixelOffset;
int targetIndex;
@Shadow private int inWindowIndex;
@Final @Shadow private List<Suggestion> suggestions;
@Final @Shadow private Rect2i area;

@Unique private int indexBefore;
@Unique private float scrollPixelOffset;
@Unique private int targetIndex;

@Inject(method = "render", at = @At("HEAD"))
private void renderH(DrawContext context, int mouseX, int mouseY, CallbackInfo ci) {
if(Config.cfg.chatSpeed == 0) return;
savedContext = context;
scrollPixelOffset = (float) (scrollPixelOffset * Math.pow(Config.cfg.chatSpeed, SmoothSc.getLastFrameDuration()));
inWindowIndex = SmoothSc.clamp(targetIndex - getScrollOffset() / 12, 0, suggestions.size() - 10); // the clamp is here as a workaround to a crash
}
Expand All @@ -47,7 +49,7 @@ private boolean mask(boolean a) {
private void mask(DrawContext context, int mouseX, int mouseY, CallbackInfo ci) {
if(Config.cfg.chatSpeed == 0) return;
// savedContext.enableScissor(area.getX() - 1, area.getY(), area.getX() + area.getWidth(), area.getY() + area.getHeight());
savedContext.enableScissor(0, area.getY(), context.getScaledWindowWidth(), area.getY() + area.getHeight());
context.enableScissor(0, area.getY(), context.getScaledWindowWidth(), area.getY() + area.getHeight());
}

@Inject(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/DrawContext;drawTextWithShadow(Lnet/minecraft/client/font/TextRenderer;Ljava/lang/String;III)I", shift = At.Shift.AFTER))
Expand Down Expand Up @@ -78,11 +80,14 @@ private int textPosY(int s) {
private void scrollH(int off, CallbackInfo ci) {commonSH();}
@Inject(method = "scroll", at = @At("TAIL"))
private void scrollT(int off, CallbackInfo ci) {commonST();}


@Unique
private void commonSH(){
if(Config.cfg.chatSpeed == 0) return;
indexBefore = inWindowIndex;
}

@Unique
private void commonST(){
if(Config.cfg.chatSpeed == 0) return;
scrollPixelOffset += (inWindowIndex - indexBefore) * 12;
Expand All @@ -101,11 +106,14 @@ private int addLineUnder(int i) {
if (Config.cfg.chatSpeed == 0 || getScrollOffset() >= 0 || inWindowIndex >= suggestions.size() - 10) return (i);
return (i + 1);
}

int getDrawOffset() {

@Unique
private int getDrawOffset() {
return Math.round(scrollPixelOffset) - (Math.round(scrollPixelOffset) / 12 * 12);
}
int getScrollOffset() {

@Unique
private int getScrollOffset() {
return Math.round(scrollPixelOffset);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
@Mixin(CreativeScreenHandler.class)
public interface CreativeScreenHandlerAccessor {
@Invoker("getScrollPosition")
public float getPos(int row);
float getPos(int row);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ public class CreativeScreenMixin {
private static ItemGroup selectedTab;

@Inject(method = "setSelectedTab", at = @At("TAIL"))
void setSelectedTabT(ItemGroup group, CallbackInfo ci) {
private void setSelectedTabT(ItemGroup group, CallbackInfo ci) {
SmoothSc.creativeScreenScrollOffset = 0;
}

@Inject(method = "drawBackground", at = @At(value = "INVOKE", shift = Shift.AFTER, target = "Lnet/minecraft/client/gui/DrawContext;drawTexture(Lnet/minecraft/util/Identifier;IIIIII)V"))
void drawBackground(DrawContext context, float delta, int mouseX, int mouseY, CallbackInfo ci) {
private void drawBackground(DrawContext context, float delta, int mouseX, int mouseY, CallbackInfo ci) {
if (SmoothSc.getCreativeScrollOffset() == 0 || Config.cfg.creativeScreenSpeed == 0 || SmoothSc.creativeSH == null) return;

int x0 = Math.round(context.getScaledWindowWidth() / 2f) - 90;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ public abstract class HandledScreenMixin<T extends ScreenHandler> {
@Unique private boolean drawingOverdrawnSlot = false;

@Inject(method = "render", at = @At("HEAD"))
void render(DrawContext context, int mx, int my, float d, CallbackInfo ci) {
savedContext = context;
originalCursorY = my;
private void render(DrawContext context, int mx, int my, float d, CallbackInfo ci) {
this.originalCursorY = my;
if (Config.cfg.creativeScreenSpeed == 0 || SmoothSc.creativeSH == null) return;

SmoothSc.creativeScreenScrollOffset = (float) (SmoothSc.creativeScreenScrollOffset * Math.pow(Config.cfg.creativeScreenSpeed, SmoothSc.getLastFrameDuration()));
Expand All @@ -57,13 +56,13 @@ void render(DrawContext context, int mx, int my, float d, CallbackInfo ci) {
}

@Inject(method = "render", at = @At(shift = At.Shift.AFTER, value = "INVOKE", target = "Lnet/minecraft/client/util/math/MatrixStack;translate(FFF)V"))
void renderMid0(DrawContext context, int mx, int my, float d, CallbackInfo ci, @Local(ordinal = 1) LocalIntRef mouseY) {
private void renderMid0(DrawContext context, int mx, int my, float d, CallbackInfo ci, @Local(ordinal = 1, argsOnly = true) LocalIntRef mouseY) {
if (Config.cfg.creativeScreenSpeed == 0 || SmoothSc.creativeScreenItemCount <= 0 || SmoothSc.getCreativeScrollOffset() == 0) return;
context.enableScissor(0, context.getScaledWindowHeight() / 2 - 50, context.getScaledWindowWidth(), context.getScaledWindowHeight() / 2 + 38);
context.getMatrices().push();
context.getMatrices().translate(0, SmoothSc.getCreativeDrawOffset(), 0);
cutEnabled = true;
if(originalCursorY >= savedContext.getScaledWindowHeight() / 2 - 51 && originalCursorY <= savedContext.getScaledWindowHeight() / 2 + 38)
if(originalCursorY >= context.getScaledWindowHeight() / 2 - 51 && originalCursorY <= context.getScaledWindowHeight() / 2 + 38)
mouseY.set(my - SmoothSc.getCreativeDrawOffset());

// the fix for instantly disappearing items on the opposite side of scrolling...
Expand All @@ -87,26 +86,28 @@ private void drawSlotOverridden(DrawContext context, Slot slot) {
}

@ModifyVariable(method = "drawSlot", at = @At(value = "STORE"), ordinal = 1)
int drawItemY(int y) {
private int drawItemY(int y, @Local(argsOnly = true) DrawContext context) {
if(drawingOverdrawnSlot) return y;
SmoothSc.creativeScreenItemCount -= 1;
if (SmoothSc.creativeScreenItemCount < 0) tryDisableMask(savedContext);
if (SmoothSc.creativeScreenItemCount < 0) tryDisableMask(context);
if (Config.cfg.creativeScreenSpeed == 0 || SmoothSc.creativeScreenItemCount < 0) return y;
return y ;//+ SmoothSc.getCreativeDrawOffset();
}

@ModifyVariable(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screen/ingame/HandledScreen;drawSlot(Lnet/minecraft/client/gui/DrawContext;Lnet/minecraft/screen/slot/Slot;)V", shift = At.Shift.AFTER), argsOnly = true, ordinal = 1)
int revertMousePos(int mouseY) {
private int revertMousePos(int mouseY) {
if (Config.cfg.creativeScreenSpeed == 0 || SmoothSc.creativeScreenItemCount < 0) return originalCursorY;
return mouseY;
}

@Inject(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screen/ingame/HandledScreen;drawForeground(Lnet/minecraft/client/gui/DrawContext;II)V"))
void renderMid1(DrawContext context, int mx, int my, float d, CallbackInfo ci, @Local(ordinal = 1) int mouseY) {
private void renderMid1(DrawContext context, int mx, int my, float d, CallbackInfo ci, @Local(ordinal = 1) int mouseY) {
tryDisableMask(context);
mouseY = originalCursorY;
}

void tryDisableMask(DrawContext context){
@Unique
private void tryDisableMask(DrawContext context){
if (drawingOverdrawnSlot) return;
if (!cutEnabled) return;
if (Config.cfg.enableMaskDebug)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ private void setScreenT(@Nullable Screen s, CallbackInfo ci) {
try {
var sh = ((CreativeScreenHandler) ((ScreenHandlerProvider<?>) s).getScreenHandler());
if (sh != null) SmoothSc.creativeSH = sh;
} catch (Exception e) {}
} catch (Exception ignored) {}
SmoothSc.creativeScreenScrollOffset = 0;
}

@Inject(method = "reloadResources", at = @At("HEAD"))
void onResReload(CallbackInfoReturnable<CompletableFuture<Void>> cir) {
private void onResReload(CallbackInfoReturnable<CompletableFuture<Void>> cir) {
SmoothSc.updateConfig();
}
}
14 changes: 8 additions & 6 deletions src/main/java/smsk/smoothscroll/mixin/EntryListWidgetMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
Expand All @@ -13,13 +14,14 @@

@Mixin(EntryListWidget.class)
public class EntryListWidgetMixin {
@Shadow double scrollAmount; // this is the number of pixels
double scrollAmountBuffer;
double targetScroll;
boolean mousescrolling = false;
@Shadow private double scrollAmount; // this is the number of pixels

double scrollValBefore;
boolean updateScActive = false; // this makes the mod know, when things aren't working as expected and lets the user scroll non-smoothly
@Unique private double scrollAmountBuffer;
@Unique private double targetScroll;
@Unique private boolean mousescrolling = false;

@Unique private double scrollValBefore;
@Unique private boolean updateScActive = false; // this makes the mod know, when things aren't working as expected and lets the user scroll non-smoothly

@Inject(method = "setScrollAmount", at = @At("TAIL"))
private void setScrollT(double s, CallbackInfo ci) {
Expand Down
Loading

0 comments on commit bd06f04

Please sign in to comment.