Skip to content

Commit

Permalink
Add the ability to graphically configure the mod
Browse files Browse the repository at this point in the history
  • Loading branch information
chrrs committed Feb 8, 2024
1 parent 266a01a commit 5a228f2
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 60 deletions.
5 changes: 5 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ version = "${property("mod_version")}+mc${property("minecraft_version")}"

repositories {
maven("https://maven.terraformersmc.com")
maven("https://maven.shedaniel.me")
}

dependencies {
Expand All @@ -19,6 +20,10 @@ dependencies {
modImplementation("net.fabricmc.fabric-api:fabric-api:${property("fabric_version")}")
modImplementation("com.terraformersmc:modmenu:${property("mod_menu_version")}")

modApi("me.shedaniel.cloth:cloth-config-fabric:${property("cloth_config_version")}") {
exclude("net.fabricmc.fabric-api")
}

include(implementation(annotationProcessor("io.github.llamalad7:mixinextras-fabric:0.2.2")!!)!!)
}

Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ archives_base_name=pegs-emotes

fabric_version=0.84.0+1.20.1
mod_menu_version=4.0.0
cloth_config_version=11.1.118
43 changes: 0 additions & 43 deletions src/main/java/me/chrr/pegsemotes/Config.java

This file was deleted.

11 changes: 2 additions & 9 deletions src/main/java/me/chrr/pegsemotes/EmoteMod.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package me.chrr.pegsemotes;

import me.chrr.pegsemotes.config.Config;
import me.chrr.pegsemotes.emote.RepositoryManager;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.resource.ResourceManager;
Expand All @@ -20,28 +20,21 @@
import org.lwjgl.glfw.GLFW;

import java.io.IOException;
import java.nio.file.Path;

public class EmoteMod implements ClientModInitializer {
private static final Logger LOGGER = LogManager.getLogger("pegs-emotes");

public static final String USER_AGENT = "PegsEmotes (github.com/chrrs)";
public static final Identifier EMOTE_FONT = new Identifier("pegs-emotes", "font/emotes");

private Config config = new Config();

@Override
public void onInitializeClient() {
Path configPath = FabricLoader.getInstance().getConfigDir().resolve("pegs-emotes.json");

try {
config = Config.load(configPath);
Config.loadInstance();
} catch (IOException e) {
LOGGER.error("failed to load config", e);
}

RepositoryManager.getInstance().setRepositories(config.repositories);

// Reload emotes when client resources get reloaded
ResourceManagerHelper.get(ResourceType.CLIENT_RESOURCES)
.registerReloadListener(new SimpleSynchronousResourceReloadListener() {
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/me/chrr/pegsemotes/config/ClothConfigScreen.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package me.chrr.pegsemotes.config;

import me.chrr.pegsemotes.emote.RepositoryManager;
import me.shedaniel.clothconfig2.api.ConfigBuilder;
import me.shedaniel.clothconfig2.api.ConfigCategory;
import me.shedaniel.clothconfig2.api.ConfigEntryBuilder;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;

public class ClothConfigScreen {
private static final Logger LOGGER = LogManager.getLogger("pegs-emotes.ConfigScreen");

public static Screen create(Screen parent) {
ConfigBuilder builder = ConfigBuilder.create()
.setParentScreen(parent)
.setTitle(Text.translatable("title.pegs-emotes.options"));

builder.setSavingRunnable(() -> {
try {
Config.getInstance().save();
} catch (IOException e) {
LOGGER.error("could not save config", e);
}
});

ConfigCategory category = builder.getOrCreateCategory(Text.of("General"));
ConfigEntryBuilder entryBuilder = builder.entryBuilder();

category.addEntry(entryBuilder.startTextDescription(
Text.translatable("option.pegs-emotes.active", RepositoryManager.getInstance().getEmoteNames().size()).formatted(Formatting.GREEN)
)
.setTooltip(Text.translatable("option.pegs-emotes.active.tooltip"))
.build());

category.addEntry(entryBuilder.startStrList(
Text.translatable("option.pegs-emotes.repositories"),
Config.getInstance().repositories
)
.setDefaultValue(Config.DEFAULT.repositories)
.setSaveConsumer((value) -> {
Config.getInstance().repositories = value;
RepositoryManager.getInstance().reload();
})
.build());

category.addEntry(entryBuilder.startBooleanToggle(
Text.translatable("option.pegs-emotes.emote-shadow"),
Config.getInstance().emoteShadow
)
.setDefaultValue(Config.DEFAULT.emoteShadow)
.setSaveConsumer((value) -> Config.getInstance().emoteShadow = value)
.build());

return builder.build();
}
}
68 changes: 68 additions & 0 deletions src/main/java/me/chrr/pegsemotes/config/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package me.chrr.pegsemotes.config;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import net.fabricmc.loader.api.FabricLoader;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

public class Config {
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
private static final int VERSION = 3;
public static final Config DEFAULT = new Config();

private static Config INSTANCE;

public int version;
public List<String> repositories = List.of("https://emotes.chrr.me/v2/");
public boolean emoteShadow = true;

private void upgrade() throws IOException {
if (this.version == VERSION) {
return;
}

if (this.version <= 1) {
this.repositories = DEFAULT.repositories;
}

if (this.version <= 2) {
this.emoteShadow = DEFAULT.emoteShadow;
}

this.version = VERSION;
this.save();
}

public void save() throws IOException {
String json = GSON.toJson(this);
Files.writeString(getPath(), json);
}

public static Path getPath() {
return FabricLoader.getInstance().getConfigDir().resolve("pegs-emotes.json");
}

public static void loadInstance() throws IOException {
Path path = getPath();
if (path.toFile().isFile()) {
String source = Files.readString(path);
INSTANCE = GSON.fromJson(source, Config.class);
INSTANCE.upgrade();
} else {
INSTANCE = DEFAULT;
INSTANCE.save();
}
}

public static Config getInstance() {
if (INSTANCE == null) {
throw new RuntimeException("Config not loaded yet");
}

return INSTANCE;
}
}
31 changes: 31 additions & 0 deletions src/main/java/me/chrr/pegsemotes/config/EmoteModMenu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package me.chrr.pegsemotes.config;

import com.terraformersmc.modmenu.api.ConfigScreenFactory;
import com.terraformersmc.modmenu.api.ModMenuApi;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.Text;
import net.minecraft.util.Util;

import java.awt.*;
import java.io.IOException;

public class EmoteModMenu implements ModMenuApi {
private static final boolean CLOTH_CONFIG_INSTALLED = FabricLoader.getInstance().isModLoaded("cloth-config2");

@Override
public ConfigScreenFactory<?> getModConfigScreenFactory() {
if (CLOTH_CONFIG_INSTALLED) {
return ClothConfigScreen::create;
} else {
return parent -> new Screen(Text.empty()) {
@Override
protected void init() {
Util.getOperatingSystem().open(Config.getPath().toFile());
assert client != null;
client.setScreen(parent);
}
};
}
}
}
9 changes: 2 additions & 7 deletions src/main/java/me/chrr/pegsemotes/emote/RepositoryManager.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.chrr.pegsemotes.emote;

import me.chrr.pegsemotes.config.Config;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand All @@ -16,8 +17,6 @@ public class RepositoryManager {

private static final RepositoryManager INSTANCE = new RepositoryManager();

private String[] repositories = new String[]{};

private final Map<Integer, Future<Emote>> emotes = new HashMap<>();
private final Map<String, Integer> codePoints = new HashMap<>();
private final Map<String, EmoteFetcher.RemoteEmote> remoteEmotes = new HashMap<>();
Expand All @@ -32,7 +31,7 @@ public void reload() {
remoteEmotes.clear();
emotes.clear();

for (String repository : repositories) {
for (String repository : Config.getInstance().repositories) {
try {
URL base = new URL(repository);
ApiEmotes res = ApiEmotes.fetchFrom(new URL(base, "emotes.json"));
Expand Down Expand Up @@ -91,10 +90,6 @@ public Collection<String> getEmoteNames() {
return remoteEmotes.keySet();
}

public void setRepositories(String[] repositories) {
this.repositories = repositories;
}

public static RepositoryManager getInstance() {
return INSTANCE;
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/me/chrr/pegsemotes/font/ImageGlyph.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.chrr.pegsemotes.font;

import me.chrr.pegsemotes.config.Config;
import net.minecraft.client.font.Glyph;
import net.minecraft.client.font.GlyphRenderer;
import net.minecraft.client.font.RenderableGlyph;
Expand Down Expand Up @@ -53,4 +54,9 @@ public float getOversample() {
public float getBoldOffset() {
return 0f;
}

@Override
public float getShadowOffset() {
return Config.getInstance().emoteShadow ? 1f : 0f;
}
}
7 changes: 6 additions & 1 deletion src/main/resources/assets/pegs-emotes/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"key.pegs-emotes.reload": "Reload emotes",
"category.pegs-emotes": "PEGS Emotes",
"text.pegs-emotes.reload-complete": "Emotes reloaded. (%d active)"
"text.pegs-emotes.reload-complete": "Emotes reloaded. (%d active)",
"title.pegs-emotes.options": "PEGS Emotes Options",
"option.pegs-emotes.active": "%d emotes loaded and active",
"option.pegs-emotes.active.tooltip": "NOTE: This number only updates whenever the config is reloaded.\nIf the repositories have changed, please re-enter this screen.",
"option.pegs-emotes.repositories": "Repository URLs",
"option.pegs-emotes.emote-shadow": "Emote shadow"
}
3 changes: 3 additions & 0 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"entrypoints": {
"client": [
"me.chrr.pegsemotes.EmoteMod"
],
"modmenu": [
"me.chrr.pegsemotes.config.EmoteModMenu"
]
},
"mixins": [
Expand Down

0 comments on commit 5a228f2

Please sign in to comment.