Skip to content

Commit

Permalink
Merge pull request #49 from Dragon-Seeker/1.21-CondensedCreativeCompat
Browse files Browse the repository at this point in the history
[1.21] Condensed Creative Compability, Refactors mixins, and adjust such to be Client Mod
  • Loading branch information
SmajloSlovakian authored Aug 16, 2024
2 parents 5c5085a + bd06f04 commit ae3f3a9
Show file tree
Hide file tree
Showing 20 changed files with 298 additions and 129 deletions.
46 changes: 35 additions & 11 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ base {
}

repositories {
// Add repositories to retrieve artifacts from in here.
// You should only use this when depending on other mods because
// Loom adds the essential maven repositories to download Minecraft and libraries from automatically.
// See https://docs.gradle.org/current/userguide/declaring_repositories.html
// for more information about repositories.
exclusiveContent {
forRepository {
maven {
Expand All @@ -27,7 +22,9 @@ repositories {
includeGroup "maven.modrinth"
}
}
maven { url 'https://maven.wispforest.io' }
maven { url "https://maven.shedaniel.me/" }
maven { url "https://maven.terraformersmc.com/releases/" }
maven { url 'https://maven.wispforest.io/releases' }
}

dependencies {
Expand All @@ -38,15 +35,42 @@ dependencies {

// Fabric API. This is technically optional, but you probably want it anyway.
// modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"

// modImplementation "maven.modrinth:chat-heads:${project.chat_heads_version}" DISABLED due to not being neccessary anymore
// modCompileOnly "maven.modrinth:immediatelyfast:${project.immediatelyfast_version}" DISABLED due to not being neccessary anymore
modCompileOnly "maven.modrinth:item-borders:${project.item_borders_version}"
//modCompileOnly "maven.modrinth:condensed-creative:3.3.0+1.21"
modCompileOnly ("io.wispforest:condensed_creative-fabric:3.3.0+1.21") {
exclude(group: "me.shedaniel.cloth")
}
modCompileOnly ("io.wispforest:condensed_creative-fabric:${project.cc_version}") { exclude(group: "me.shedaniel.cloth") }
//modLocalRuntime ("io.wispforest:condensed_creative-fabric:${project.cc_version}")
}

loom {
runs {
// Loom Run for using Mixin Debugging with export and HotSwapping capability
it.create("clientMixinDebug", {
client()
runDir "run"
name "Minecraft Client [Mixin Debug]"

vmArg "-Dfabric.dli.config=${file(".gradle/loom-cache/launch.cfg").toString()}"
vmArg "-Dfabric.dli.env=client"
vmArg "-Dfabric.dli.main=net.fabricmc.loader.impl.launch.knot.KnotClient"

Object runConfig = it

project.afterEvaluate {
project.configurations.compileClasspath
.getAllDependencies()
.findAll { Dependency test -> test.getName() == "sponge-mixin" }
.iterator().forEachRemaining {runConfig.vmArg "-javaagent:\"${(project.configurations.compileClasspath.files(it).iterator().next().getPath())}\"" }
}

vmArg "-Dlog4j.configurationFile=${file(".gradle/loom-cache/log4j.xml").toString()}"
vmArg "-Dfabric.log.disableAnsi=false"
vmArg "-Dmixin.debug.export=true"

ideConfigGenerated = true
})
}
}

processResources {
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ maven_group=smsk.smoothscroll
archives_base_name=smoothscroll

item_borders_version=1.2.3
cc_version=3.4.0+1.21
43 changes: 43 additions & 0 deletions src/main/java/smsk/smoothscroll/DelegatingInventory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package smsk.smoothscroll;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;

import java.util.function.Function;
import java.util.function.IntFunction;

/**
* An implementation of {@link Inventory} used to allow for wrapping any method of getting a
* stack from a given collection. Used to support Condensed Creative method of entries.
*/
public class DelegatingInventory<S> implements Inventory {

protected final IntFunction<S> getter;
protected final Function<S, ItemStack> mapper;

protected DelegatingInventory(IntFunction<S> getter, Function<S, ItemStack> mapper){
this.getter = getter;
this.mapper = mapper;
}

public static DelegatingInventory<ItemStack> itemStackBased(IntFunction<ItemStack> stackGetter) {
return new DelegatingInventory<>(stackGetter, stack -> stack);
}

@Override
public ItemStack getStack(int slot) {
return mapper.apply(getter.apply(slot));
}

//-- DEFAULT IMPLEMENTATION SECTION BELOW --//

@Override public int size() { return 1; }
@Override public boolean isEmpty() { return false; }
@Override public ItemStack removeStack(int slot, int amount) { return ItemStack.EMPTY; }
@Override public ItemStack removeStack(int slot) { return ItemStack.EMPTY; }
@Override public void setStack(int slot, ItemStack stack) {}
@Override public void markDirty() {}
@Override public boolean canPlayerUse(PlayerEntity player) { return true; }
@Override public void clear() {}
}
14 changes: 12 additions & 2 deletions src/main/java/smsk/smoothscroll/SmoothSc.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package smsk.smoothscroll;

import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.ingame.CreativeInventoryScreen.CreativeScreenHandler;
import net.minecraft.inventory.Inventory;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.ColorHelper;

import org.slf4j.Logger;
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 @@ -30,14 +34,20 @@ 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();
FabricLoader.getInstance().getObjectShare().put("smoothscroll:creative_screen/y_offset", 0);
FabricLoader.getInstance().getObjectShare().put("smoothscroll:creative_screen/item_count", 0);
}

public static Inventory getDelegatingInventory(ScreenHandler handler) {
return isCondensedInventoryLoaded
? CondensedInventoryCompat.of(handler)
: DelegatingInventory.itemStackBased(creativeSH.itemList::get);
}

public static void print(Object s) {
LOGGER.info(s + "");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
package smsk.smoothscroll.compat;

import io.wispforest.condensed_creative.util.CondensedInventory;
import net.minecraft.item.ItemStack;
import smsk.smoothscroll.mixin.CreativeScreen.CreativeScreenAccessor;
import io.wispforest.condensed_creative.ducks.CreativeInventoryScreenHandlerDuck;
import io.wispforest.condensed_creative.entry.Entry;
import io.wispforest.condensed_creative.entry.EntryContainer;
import io.wispforest.condensed_creative.entry.impl.ItemEntry;
import net.minecraft.screen.ScreenHandler;
import smsk.smoothscroll.DelegatingInventory;

public class CondensedInventoryCompat {
public static ItemStack getStack(int i) {
return ((CondensedInventory) CreativeScreenAccessor.INVENTORY()).getEntryStack(i - 9).getEntryStack();

public static DelegatingInventory<Entry> of(ScreenHandler handler) {
return new CondensedEntryDelegatingInventory(handler);
}

public static class CondensedEntryDelegatingInventory extends DelegatingInventory<Entry> implements EntryContainer {
protected CondensedEntryDelegatingInventory(ScreenHandler handler) {
super(value -> {
try {
if(handler instanceof CreativeInventoryScreenHandlerDuck duck) {
return duck.getFilteredEntryList().get(value);
}
} catch (IndexOutOfBoundsException ignored) {}

return ItemEntry.EMPTY;
}, Entry::getDisplayStack);
}

@Override
public Entry getEntryStack(int slot) {
return this.getter.apply(slot);
}

@Override
public void setEntryStack(int slot, Entry entryStack){}
}
}
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);
}
}
Loading

0 comments on commit ae3f3a9

Please sign in to comment.