Skip to content

Commit

Permalink
Add support for QuickText in mod descriptions
Browse files Browse the repository at this point in the history
- Added support for QuickText in mod descriptions
- Newlines in descriptions are no longer forced to be paragraphs, so single linebreaks are now allowd
  • Loading branch information
Prospector committed Jun 15, 2024
1 parent f543352 commit 90221e5
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 88 deletions.
8 changes: 8 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ dependencies {
mappings "net.fabricmc:yarn:$project.yarn_mappings:v2"

mod "fabric-loader", "net.fabricmc:fabric-loader:$project.loader_version"

includeMod "fabric-api", fabricApi.module("fabric-api-base", project.fabric_version)
includeMod "fabric-api", fabricApi.module("fabric-resource-loader-v0", project.fabric_version)
includeMod "fabric-api", fabricApi.module("fabric-screen-api-v1", project.fabric_version)
includeMod "fabric-api", fabricApi.module("fabric-key-binding-api-v1", project.fabric_version)
includeMod "fabric-api", fabricApi.module("fabric-lifecycle-events-v1", project.fabric_version)

includeMod "text-placeholder-api", "eu.pb4:placeholder-api:${project.text_placeholder_api_version}"

compileOnly "org.quiltmc:quilt-loader:$project.quilt_loader_version"
}

Expand All @@ -26,4 +30,8 @@ repositories {
name = 'Quilt'
url = 'https://maven.quiltmc.org/repository/release'
}
maven {
name = 'Nucleoid'
url = 'https://maven.nucleoid.xyz/'
}
}
8 changes: 5 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ org.gradle.jvmargs=-Xmx1G
maven_group=com.terraformersmc
archive_name=modmenu

minecraft_version=1.21-pre2
yarn_mappings=1.21-pre2+build.2
minecraft_version=1.21
yarn_mappings=1.21+build.2
loader_version=0.15.11
fabric_version=0.100.1+1.21
text_placeholder_api_version=2.4.0-pre.2+1.21
quilt_loader_version=0.17.7

# Project Metadata
Expand All @@ -22,12 +23,13 @@ modrinth_slug=modmenu
modrinth_id=mOgUt4GM
modrinth_game_versions=1.21
modrinth_mod_loaders=fabric, quilt
modrinth_required_dependencies=P7dR8mSH,eXts2L7r

# CurseForge Metadata
curseforge_slug=modmenu
curseforge_id=308702
curseforge_game_versions=1.21, Fabric, Quilt
curseforge_required_dependencies=
curseforge_required_dependencies=306612
curseforge_optional_dependencies=

# Mod Loader Metadata
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/com/terraformersmc/modmenu/ModMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ public class ModMenu implements ClientModInitializer {
private static final List<ModMenuApi> apiImplementations = new ArrayList<>();

private static int cachedDisplayedModCount = -1;
public static boolean runningQuilt = FabricLoader.getInstance().isModLoaded("quilt_loader");
public static boolean devEnvironment = FabricLoader.getInstance().isDevelopmentEnvironment();
public static final boolean RUNNING_QUILT = FabricLoader.getInstance().isModLoaded("quilt_loader");
public static final boolean DEV_ENVIRONMENT = FabricLoader.getInstance().isDevelopmentEnvironment();
public static final boolean TEXT_PLACEHOLDER_COMPAT = FabricLoader.getInstance().isModLoaded("placeholder-api");

public static Screen getConfigScreen(String modid, Screen menuScreen) {
for (ModMenuApi api : apiImplementations) {
Expand All @@ -72,7 +73,10 @@ public void onInitializeClient() {
Map<String, UpdateChecker> updateCheckers = new HashMap<>();
Map<String, UpdateChecker> providedUpdateCheckers = new HashMap<>();

// Ignore deprecations, they're from Quilt Loader being in the dev env
//noinspection deprecation
FabricLoader.getInstance().getEntrypointContainers("modmenu", ModMenuApi.class).forEach(entrypoint -> {
//noinspection deprecation
ModMetadata metadata = entrypoint.getProvider().getMetadata();
String modId = metadata.getId();
try {
Expand All @@ -88,10 +92,11 @@ public void onInitializeClient() {
});

// Fill mods map
//noinspection deprecation
for (ModContainer modContainer : FabricLoader.getInstance().getAllMods()) {
Mod mod;

if (runningQuilt) {
if (RUNNING_QUILT) {
mod = new QuiltMod(modContainer, modpackMods);
} else {
mod = new FabricMod(modContainer, modpackMods);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public Map<String, ConfigScreenFactory<?>> getProvidedConfigScreenFactories() {

@Override
public Map<String, UpdateChecker> getProvidedUpdateCheckers() {
if (ModMenu.runningQuilt) {
if (ModMenu.RUNNING_QUILT) {
return Map.of("quilt_loader", new QuiltLoaderUpdateChecker());
} else {
return Map.of("fabricloader", new FabricLoaderUpdateChecker());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.terraformersmc.modmenu;

import eu.pb4.placeholders.api.parsers.NodeParser;

public class TextPlaceholderApiCompat {
public static final NodeParser PARSER = NodeParser.builder().quickText().build();
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,40 @@
import net.minecraft.util.Util;
import net.minecraft.util.math.MathHelper;

import java.util.*;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class DescriptionListWidget extends EntryListWidget<DescriptionListWidget.DescriptionEntry> {

private static final Text HAS_UPDATE_TEXT = Text.translatable("modmenu.hasUpdate");
private static final Text EXPERIMENTAL_TEXT = Text.translatable("modmenu.experimental").formatted(Formatting.GOLD);
private static final Text MODRINTH_TEXT = Text.translatable("modmenu.modrinth");
private static final Text DOWNLOAD_TEXT = Text.translatable("modmenu.downloadLink").formatted(Formatting.BLUE).formatted(Formatting.UNDERLINE);
private static final Text DOWNLOAD_TEXT = Text.translatable("modmenu.downloadLink")
.formatted(Formatting.BLUE)
.formatted(Formatting.UNDERLINE);
private static final Text CHILD_HAS_UPDATE_TEXT = Text.translatable("modmenu.childHasUpdate");
private static final Text LINKS_TEXT = Text.translatable("modmenu.links");
private static final Text SOURCE_TEXT = Text.translatable("modmenu.source").formatted(Formatting.BLUE).formatted(Formatting.UNDERLINE);
private static final Text SOURCE_TEXT = Text.translatable("modmenu.source")
.formatted(Formatting.BLUE)
.formatted(Formatting.UNDERLINE);
private static final Text LICENSE_TEXT = Text.translatable("modmenu.license");
private static final Text VIEW_CREDITS_TEXT = Text.translatable("modmenu.viewCredits").formatted(Formatting.BLUE).formatted(Formatting.UNDERLINE);
private static final Text VIEW_CREDITS_TEXT = Text.translatable("modmenu.viewCredits")
.formatted(Formatting.BLUE)
.formatted(Formatting.UNDERLINE);
private static final Text CREDITS_TEXT = Text.translatable("modmenu.credits");

private final ModsScreen parent;
private final TextRenderer textRenderer;
private ModListEntry lastSelected = null;

public DescriptionListWidget(MinecraftClient client, int width, int height, int y, int itemHeight, ModsScreen parent) {
public DescriptionListWidget(MinecraftClient client,
int width,
int height,
int y,
int itemHeight,
ModsScreen parent) {
super(client, width, height, y, itemHeight);
this.parent = parent;
this.textRenderer = client.textRenderer;
Expand Down Expand Up @@ -85,22 +99,25 @@ public void renderList(DrawContext DrawContext, int mouseX, int mouseY, float de
int wrapWidth = getRowWidth() - 5;

Mod mod = lastSelected.getMod();
String description = mod.getTranslatedDescription();
if (!description.isEmpty()) {
for (OrderedText line : textRenderer.wrapLines(Text.literal(description.replaceAll("\n", "\n\n")), wrapWidth)) {
Text description = mod.getFormattedDescription();
if (!description.getString().isEmpty()) {
for (OrderedText line : textRenderer.wrapLines(description, wrapWidth)) {
children().add(new DescriptionEntry(line));
}
}

if (ModMenuConfig.UPDATE_CHECKER.getValue() && !ModMenuConfig.DISABLE_UPDATE_CHECKER.getValue().contains(mod.getId())) {
if (ModMenuConfig.UPDATE_CHECKER.getValue() && !ModMenuConfig.DISABLE_UPDATE_CHECKER.getValue()
.contains(mod.getId())) {
UpdateInfo updateInfo = mod.getUpdateInfo();
if (updateInfo != null && updateInfo.isUpdateAvailable()) {
children().add(emptyEntry);

int index = 0;
for (OrderedText line : textRenderer.wrapLines(HAS_UPDATE_TEXT, wrapWidth - 11)) {
DescriptionEntry entry = new DescriptionEntry(line);
if (index == 0) entry.setUpdateTextEntry();
if (index == 0) {
entry.setUpdateTextEntry();
}

children().add(entry);
index += 1;
Expand All @@ -111,7 +128,9 @@ public void renderList(DrawContext DrawContext, int mouseX, int mouseY, float de
}

if (updateInfo instanceof ModrinthUpdateInfo modrinthUpdateInfo) {
Text updateText = Text.translatable("modmenu.updateText", VersionUtil.stripPrefix(modrinthUpdateInfo.getVersionNumber()), MODRINTH_TEXT)
Text updateText = Text.translatable("modmenu.updateText",
VersionUtil.stripPrefix(modrinthUpdateInfo.getVersionNumber()),
MODRINTH_TEXT)
.formatted(Formatting.BLUE)
.formatted(Formatting.UNDERLINE);

Expand All @@ -125,7 +144,9 @@ public void renderList(DrawContext DrawContext, int mouseX, int mouseY, float de
updateMessage = DOWNLOAD_TEXT;
} else {
if (downloadLink != null) {
updateMessage = updateMessage.copy().formatted(Formatting.BLUE).formatted(Formatting.UNDERLINE);
updateMessage = updateMessage.copy()
.formatted(Formatting.BLUE)
.formatted(Formatting.UNDERLINE);
}
}
for (OrderedText line : textRenderer.wrapLines(updateMessage, wrapWidth - 16)) {
Expand All @@ -143,7 +164,9 @@ public void renderList(DrawContext DrawContext, int mouseX, int mouseY, float de
int index = 0;
for (OrderedText line : textRenderer.wrapLines(CHILD_HAS_UPDATE_TEXT, wrapWidth - 11)) {
DescriptionEntry entry = new DescriptionEntry(line);
if (index == 0) entry.setUpdateTextEntry();
if (index == 0) {
entry.setUpdateTextEntry();
}

children().add(entry);
index += 1;
Expand All @@ -170,7 +193,9 @@ public void renderList(DrawContext DrawContext, int mouseX, int mouseY, float de

links.forEach((key, value) -> {
int indent = 8;
for (OrderedText line : textRenderer.wrapLines(Text.translatable(key).formatted(Formatting.BLUE).formatted(Formatting.UNDERLINE), wrapWidth - 16)) {
for (OrderedText line : textRenderer.wrapLines(Text.translatable(key)
.formatted(Formatting.BLUE)
.formatted(Formatting.UNDERLINE), wrapWidth - 16)) {
children().add(new LinkEntry(line, value, indent));
indent = 16;
}
Expand Down Expand Up @@ -219,7 +244,8 @@ public void renderList(DrawContext DrawContext, int mouseX, int mouseY, float de
var role = iterator.next();
var roleName = role.getKey();

for (var line : textRenderer.wrapLines(this.creditsRoleText(roleName), wrapWidth - 16)) {
for (var line : textRenderer.wrapLines(this.creditsRoleText(roleName),
wrapWidth - 16)) {
children().add(new DescriptionEntry(line, indent));
indent = 16;
}
Expand Down Expand Up @@ -266,42 +292,45 @@ public void renderList(DrawContext DrawContext, int mouseX, int mouseY, float de
RenderSystem.depthFunc(515);
RenderSystem.disableDepthTest();
RenderSystem.enableBlend();
RenderSystem.blendFuncSeparate(GlStateManager.SrcFactor.SRC_ALPHA, GlStateManager.DstFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SrcFactor.ZERO, GlStateManager.DstFactor.ONE);
RenderSystem.blendFuncSeparate(GlStateManager.SrcFactor.SRC_ALPHA,
GlStateManager.DstFactor.ONE_MINUS_SRC_ALPHA,
GlStateManager.SrcFactor.ZERO,
GlStateManager.DstFactor.ONE);
RenderSystem.setShader(GameRenderer::getPositionColorProgram);

bufferBuilder = tessellator.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_COLOR);
bufferBuilder.vertex(this.getX(), (this.getY() + 4), 0.0F).

color(0, 0, 0, 0);
color(0, 0, 0, 0);

bufferBuilder.vertex(this.getRight(), (this.getY() + 4), 0.0F).

color(0, 0, 0, 0);
color(0, 0, 0, 0);

bufferBuilder.vertex(this.getRight(), this.getY(), 0.0F).

color(0, 0, 0, 255);
color(0, 0, 0, 255);

bufferBuilder.vertex(this.getX(), this.getY(), 0.0F).

color(0, 0, 0, 255);
color(0, 0, 0, 255);

bufferBuilder.vertex(this.getX(), this.getBottom(), 0.0F).

color(0, 0, 0, 255);
color(0, 0, 0, 255);

bufferBuilder.vertex(this.getRight(), this.getBottom(), 0.0F).
color(0, 0, 0, 255);

color(0, 0, 0, 255);

bufferBuilder.vertex(this.getRight(), (this.getBottom() - 4), 0.0F).

color(0, 0, 0, 0);
color(0, 0, 0, 0);

bufferBuilder.vertex(this.getX(), (this.getBottom() - 4), 0.0F).

color(0, 0, 0, 0);
color(0, 0, 0, 0);

try {
builtBuffer = bufferBuilder.end();
BufferRenderer.drawWithGlobalProgram(builtBuffer);
Expand Down Expand Up @@ -360,7 +389,8 @@ private Text creditsRoleText(String roleName) {
// Fixes common role names people use in English (e.g. Author -> Authors)
var fallback = roleName.endsWith("r") ? roleName + "s" : roleName;

return Text.translatableWithFallback("modmenu.credits.role." + translationKey, fallback).append(Text.literal(":"));
return Text.translatableWithFallback("modmenu.credits.role." + translationKey, fallback)
.append(Text.literal(":"));
}

protected class DescriptionEntry extends ElementListWidget.Entry<DescriptionEntry> {
Expand All @@ -383,7 +413,16 @@ public DescriptionEntry setUpdateTextEntry() {
}

@Override
public void render(DrawContext DrawContext, int index, int y, int x, int itemWidth, int itemHeight, int mouseX, int mouseY, boolean isSelected, float delta) {
public void render(DrawContext DrawContext,
int index,
int y,
int x,
int itemWidth,
int itemHeight,
int mouseX,
int mouseY,
boolean isSelected,
float delta) {
if (updateTextEntry) {
UpdateAvailableBadge.renderBadge(DrawContext, x + indent, y);
x += 11;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/terraformersmc/modmenu/util/HttpUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public static <T> HttpResponse<T> request(HttpRequest.Builder builder, HttpRespo
}

private static String buildUserAgent() {
String env = ModMenu.devEnvironment ? "/development" : "";
String loader = ModMenu.runningQuilt ? "quilt" : "fabric";
String env = ModMenu.DEV_ENVIRONMENT ? "/development" : "";
String loader = ModMenu.RUNNING_QUILT ? "quilt" : "fabric";

var modMenuVersion = getModMenuVersion();
var minecraftVersion = SharedConstants.getGameVersion().getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.terraformersmc.modmenu.config.ModMenuConfig;
import com.terraformersmc.modmenu.util.mod.Mod;
import com.terraformersmc.modmenu.util.mod.ModrinthUpdateInfo;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.SharedConstants;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.toast.SystemToast;
Expand Down Expand Up @@ -88,7 +87,7 @@ public static void checkForModrinthUpdates() {
});

String mcVer = SharedConstants.getGameVersion().getName();
List<String> loaders = ModMenu.runningQuilt ? List.of("fabric", "quilt") : List.of("fabric");
List<String> loaders = ModMenu.RUNNING_QUILT ? List.of("fabric", "quilt") : List.of("fabric");

List<UpdateChannel> updateChannels;
UpdateChannel preferredChannel = UpdateChannel.getUserPreference();
Expand Down
Loading

0 comments on commit 90221e5

Please sign in to comment.