Skip to content

Commit

Permalink
Add back RandomBackground
Browse files Browse the repository at this point in the history
  • Loading branch information
connorslade committed Jun 29, 2024
1 parent 6d41f71 commit 083c406
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 14 deletions.
40 changes: 40 additions & 0 deletions src/main/java/com/connorcode/sigmautils/mixin/ScreenMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.connorcode.sigmautils.mixin;

import com.connorcode.sigmautils.config.Config;
import com.connorcode.sigmautils.modules._interface.RandomBackground;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.GameMenuScreen;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.advancement.AdvancementsScreen;
import net.minecraft.util.Identifier;
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(Screen.class)
public abstract class ScreenMixin {
@Shadow
public int width;
@Shadow
public int height;

@Shadow
public static void renderBackgroundTexture(DrawContext context, Identifier texture, int x, int y, float u, float v, int width, int height) {
throw new RuntimeException();
}

@Shadow
public abstract void renderInGameBackground(DrawContext context);

@Inject(method = "renderBackground", at = @At("HEAD"), cancellable = true)
void onRenderBackground(DrawContext context, int mouseX, int mouseY, float delta, CallbackInfo ci) {
if (!Config.getEnabled(RandomBackground.class) || (Screen) (Object) this instanceof GameMenuScreen || (Screen) (Object) this instanceof AdvancementsScreen)
return;

renderBackgroundTexture(context, RandomBackground.getTexture(), 0, 0, 0, 0, this.width, this.height);
renderInGameBackground(context);
ci.cancel();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.connorcode.sigmautils.modules._interface;

import com.connorcode.sigmautils.SigmaUtils;
import com.connorcode.sigmautils.config.settings.DynamicListSetting;
import com.connorcode.sigmautils.config.settings.EnumSetting;
import com.connorcode.sigmautils.config.settings.list.SimpleList;
import com.connorcode.sigmautils.misc.Components;
import com.connorcode.sigmautils.misc.util.Util;
import com.connorcode.sigmautils.module.Module;
import com.connorcode.sigmautils.module.ModuleInfo;
import net.minecraft.block.Block;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.registry.Registries;
import net.minecraft.util.Identifier;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Objects;
import java.util.Random;

import static com.connorcode.sigmautils.SigmaUtils.client;

@ModuleInfo(description = "Uses random textures for the background tessellation", documentation = "It will change every time you open a new screen.")
public class RandomBackground extends Module {
static final List<String> validBackgrounds = new BufferedReader(new InputStreamReader(Objects.requireNonNull(SigmaUtils.class.getClassLoader()
.getResourceAsStream("assets/sigma-utils/background_blocks.txt")))).lines().toList();
static EnumSetting<Util.FilterType> filterType = Util.filterSetting(RandomBackground.class)
.description("Whether to blacklist or whitelist background textures.").value(Util.FilterType.Blacklist)
.build();
static DynamicListSetting<Block> textures = new DynamicListSetting<>(RandomBackground.class, "Textures", BlockList::new).description("Possible background textures.")
.build();
static String asset;
static int screenHash = -1;

public static Identifier getTexture() {
int currentScreenHash = Objects.requireNonNull(Objects.requireNonNull(client).currentScreen).hashCode();

if (screenHash != currentScreenHash || asset == null) {
screenHash = currentScreenHash;

List<String> valid = switch (filterType.value()) {
case Whitelist ->
textures.value().stream().map(block -> Registries.BLOCK.getId(block).getPath()).toList();
case Blacklist -> validBackgrounds.stream().filter(s -> !textures.value()
.contains(Registries.BLOCK.get(Identifier.tryParse("minecraft:" + s)))).toList();
};

var assetIndex = new Random().nextInt(valid.size());
asset = valid.get(assetIndex);
}

return Identifier.tryParse("textures/block/" + asset + ".png");
}

static class BlockList extends SimpleList<Block> {

public BlockList(DynamicListSetting<Block> setting) {
super(setting, Registries.BLOCK);
}

@Override
public String getDisplay(Block value) {
return value.getName().getString();
}

@Override
public boolean renderSelector(Block resource, Screen screen, int x, int y) {
var rawId = Registries.BLOCK.getId(resource).getPath();
var id = Identifier.tryParse("textures/block/" + rawId + ".png");
if (!RandomBackground.validBackgrounds.contains(rawId) || this.setting.value().contains(resource))
return false;
Util.addDrawable(screen, new Components.DrawableElement() {
@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
context.drawTexture(id, x - 1, y + 2, 0, 0.0F, 0.0F, 16, 16, 16, 16);
}
});
return super.renderSelector(resource, screen, x + 16, y);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,9 @@ Text getSliderTitle() {
return Text.of(String.format("Tick Speed: %d [%d%%]", mspt.intValue(), Math.round((100 - mspt.value()) / .5)));
}

void setTickSpeed(long mspt) {
// TODO
}

void setTickSpeedFromPercent(double percent) {
mspt.value(50d * (percent * 2d));
mspt.value(Math.max(mspt.intValue(), 1));
if (enabled) setTickSpeed(mspt.intValue());
}

public void drawInterface(MinecraftClient client, Screen screen, int x, int y) {
Expand All @@ -55,12 +50,4 @@ protected void applyValue() {
}
});
}

public void enable(MinecraftClient client) {
setTickSpeed(mspt.intValue());
}

public void disable(MinecraftClient client) {
setTickSpeed(50);
}
}
3 changes: 2 additions & 1 deletion src/main/resources/modules/modules.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"_interface.NoChatFade",
"_interface.NoScoreboardValue",
"_interface.PortalInventory",
"_interface.RandomBackground",
"_interface.SeeThruLoading",
"_interface.SignClickThrough",
"_interface.SplashRefresh",
Expand Down Expand Up @@ -58,7 +59,6 @@
"misc.SoundControl",
"misc.TickSpeed",
"misc.WindowTitle",
"rendering.MapBorder",
"rendering.AllowShowBarrier",
"rendering.Deadmau5Ears",
"rendering.DebugRenderers",
Expand All @@ -68,6 +68,7 @@
"rendering.FlippedEntities",
"rendering.FullBright",
"rendering.LightLevel",
"rendering.MapBorder",
"rendering.NoArmor",
"rendering.NoBreakParticles",
"rendering.NoDarkSky",
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/sigma-utils.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"RecipeBookWidgetMixin",
"RenderTickCounter$DynamicMixin",
"ScreenAccessor",
"ScreenMixin",
"SimpleOptionAccessor",
"SoundSystemMixin",
"SplashTextResourceSupplierMixin",
Expand Down

0 comments on commit 083c406

Please sign in to comment.