-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the ability to graphically configure the mod
- Loading branch information
Showing
11 changed files
with
185 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/me/chrr/pegsemotes/config/ClothConfigScreen.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters