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 allowed
  • Loading branch information
Prospector committed Jun 18, 2024
1 parent 3b48ad5 commit 9ee75aa
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 84 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 @@ -7,6 +7,7 @@ minecraft_version=1.20.5
yarn_mappings=1.20.5+build.1
loader_version=0.15.10
fabric_version=0.97.8+1.20.5
text_placeholder_api_version=2.4.0-pre.1+1.20.5
quilt_loader_version=0.17.7

# Project Metadata
Expand All @@ -20,14 +21,15 @@ default_release_type=stable
# Modrinth Metadata
modrinth_slug=modmenu
modrinth_id=mOgUt4GM
modrinth_game_versions=1.20.5-pre2, 1.20.5-pre3, 1.20.5-pre4, 1.20.5-rc1, 1.20.5-rc2, 1.20.5-rc3, 1.20.5, 1.20.6-rc1
modrinth_game_versions=1.20.5, 1.20.6-rc1
modrinth_mod_loaders=fabric, quilt
modrinth_required_dependencies=P7dR8mSH,eXts2L7r

# CurseForge Metadata
curseforge_slug=modmenu
curseforge_id=308702
curseforge_game_versions=1.20.5-Snapshot, 1.20.5, Fabric, Quilt
curseforge_required_dependencies=
curseforge_game_versions=1.20.5, 1.20.6, Fabric, Quilt
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 @@ -28,28 +28,37 @@
import net.minecraft.util.Util;
import net.minecraft.util.math.MathHelper;

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

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 DOWNLOAD_TEXT = Text.translatable("modmenu.downloadLink").formatted(Formatting.BLUE).formatted(Formatting.UNDERLINE);
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 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 @@ -88,22 +97,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 @@ -113,20 +125,34 @@ public void renderList(DrawContext DrawContext, int mouseX, int mouseY, float de
children().add(new DescriptionEntry(line, 8));
}

Text updateMessage = updateInfo.getUpdateMessage();
String downloadLink = updateInfo.getDownloadLink();
if (updateMessage == null) {
updateMessage = DOWNLOAD_TEXT;
} else {
if (downloadLink != null) {
updateMessage = updateMessage.copy().formatted(Formatting.BLUE).formatted(Formatting.UNDERLINE);
if (updateInfo instanceof ModrinthUpdateInfo modrinthUpdateInfo) {
Text updateText = Text.translatable("modmenu.updateText",
VersionUtil.stripPrefix(modrinthUpdateInfo.getVersionNumber()),
MODRINTH_TEXT)
.formatted(Formatting.BLUE)
.formatted(Formatting.UNDERLINE);

for (OrderedText line : textRenderer.wrapLines(updateText, wrapWidth - 16)) {
children().add(new LinkEntry(line, modrinthUpdateInfo.getDownloadLink(), 8));
}
}
for (OrderedText line : textRenderer.wrapLines(updateMessage, wrapWidth - 16)) {
if (downloadLink != null) {
children().add(new LinkEntry(line, downloadLink, 8));
} else {
Text updateMessage = updateInfo.getUpdateMessage();
String downloadLink = updateInfo.getDownloadLink();
if (updateMessage == null) {
updateMessage = DOWNLOAD_TEXT;
} else {
children().add(new DescriptionEntry(line, 8));
if (downloadLink != null) {
updateMessage = updateMessage.copy()
.formatted(Formatting.BLUE)
.formatted(Formatting.UNDERLINE);
}
}
for (OrderedText line : textRenderer.wrapLines(updateMessage, wrapWidth - 16)) {
if (downloadLink != null) {
children().add(new LinkEntry(line, downloadLink, 8));
} else {
children().add(new DescriptionEntry(line, 8));
}
}
}
}
Expand All @@ -136,7 +162,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 @@ -163,7 +191,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 @@ -212,7 +242,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 @@ -258,7 +289,10 @@ 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.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_COLOR);
Expand Down Expand Up @@ -348,7 +382,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 @@ -371,7 +406,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 @@ -220,7 +220,7 @@ private static UpdateChannel getUpdateChannel(String versionType) {

private static @Nullable Map<String, VersionUpdate> getUpdatedVersions(Collection<String> modHashes) {
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 9ee75aa

Please sign in to comment.