generated from JamCoreModding/template-mod
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
69 changed files
with
1,359 additions
and
856 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,10 @@ | ||
Fix dependency logic | ||
- Ensure inherited fields are present in config GUIs (closes #13). | ||
- When both a mod ID and config file name are specified, the config file is now saved under | ||
`config/{mod id}/{config name}.json5` (closes #12). | ||
- This should not be a breaking change as I am not aware of any mods registering multiple configs currently. | ||
- Switch to fabric-api mod ID in dependencies block (closes #10). | ||
- Enable split source sets (closes #14). | ||
- Identify config managers by `(MOD_ID, CONFIG_NAME)` rather than by just `(CONFIG_NAME)` (closes #15). | ||
- Allow `List<?>` config fields (closes #11). | ||
- The reset button next to each config field now resets to the default value, rather than the value the field had when | ||
the config screen was opened. |
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
13 changes: 8 additions & 5 deletions
13
...ithub/jamalam360/jamlib/JamLibClient.java → ...amalam360/jamlib/client/JamLibClient.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
164 changes: 164 additions & 0 deletions
164
common/src/client/java/io/github/jamalam360/jamlib/client/config/gui/ConfigScreen.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,164 @@ | ||
package io.github.jamalam360.jamlib.client.config.gui; | ||
|
||
import dev.architectury.platform.Platform; | ||
import io.github.jamalam360.jamlib.JamLib; | ||
import io.github.jamalam360.jamlib.client.config.gui.entry.ConfigEntry; | ||
import io.github.jamalam360.jamlib.client.gui.WidgetList; | ||
import io.github.jamalam360.jamlib.config.ConfigExtensions; | ||
import io.github.jamalam360.jamlib.config.ConfigManager; | ||
import io.github.jamalam360.jamlib.config.HiddenInGui; | ||
import net.minecraft.Util; | ||
import net.minecraft.client.Minecraft; | ||
import net.minecraft.client.gui.GuiGraphics; | ||
import net.minecraft.client.gui.components.AbstractWidget; | ||
import net.minecraft.client.gui.components.Button; | ||
import net.minecraft.client.gui.components.SpriteIconButton; | ||
import net.minecraft.client.gui.screens.Screen; | ||
import net.minecraft.client.resources.language.I18n; | ||
import net.minecraft.network.chat.CommonComponents; | ||
import net.minecraft.network.chat.Component; | ||
import org.jetbrains.annotations.ApiStatus; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
/** | ||
* A screen for editing a config managed through a {@link ConfigManager}. | ||
*/ | ||
@ApiStatus.Internal | ||
public class ConfigScreen<T> extends Screen { | ||
|
||
protected final ConfigManager<T> manager; | ||
private final Screen parent; | ||
private final List<ConfigEntry<T, ?>> entries; | ||
private WidgetList widgetList; | ||
private Button doneButton; | ||
|
||
public ConfigScreen(ConfigManager<T> manager, Screen parent) { | ||
super(createTitle(manager)); | ||
this.manager = manager; | ||
this.parent = parent; | ||
this.entries = new ArrayList<>(); | ||
} | ||
|
||
@ApiStatus.Internal | ||
public static String createTranslationKey(String modId, String configName, String path) { | ||
if (modId.equals(configName)) { | ||
return "config." + modId + "." + path; | ||
} else { | ||
return "config." + modId + "." + configName + "." + path; | ||
} | ||
} | ||
|
||
protected static Component createTitle(ConfigManager<?> manager) { | ||
String translationKey = createTranslationKey(manager.getModId(), manager.getConfigName(), "title"); | ||
|
||
if (I18n.exists(translationKey)) { | ||
return Component.translatable(translationKey); | ||
} else { | ||
return Component.literal(Platform.getMod(manager.getModId()).getName()); | ||
} | ||
} | ||
|
||
@Override | ||
protected void init() { | ||
super.init(); | ||
|
||
this.addRenderableWidget(Button.builder(CommonComponents.GUI_CANCEL, button -> { | ||
this.manager.reloadFromDisk(); | ||
Objects.requireNonNull(this.minecraft).setScreen(this.parent); | ||
}).pos(this.width / 2 - 154, this.height - 28).size(150, 20).build()); | ||
|
||
this.doneButton = this.addRenderableWidget(Button.builder(CommonComponents.GUI_DONE, button -> { | ||
if (this.hasChanges()) { | ||
this.manager.save(); | ||
} | ||
|
||
Objects.requireNonNull(this.minecraft).setScreen(this.parent); | ||
}).pos(this.width / 2 + 4, this.height - 28).size(150, 20).build()); | ||
|
||
SpriteIconButton editManuallyButton = this.addRenderableWidget( | ||
SpriteIconButton.builder(Component.translatable("config.jamlib.edit_manually"), button -> { | ||
if (this.hasChanges()) { | ||
this.manager.save(); | ||
} | ||
|
||
Util.getPlatform().openFile(Platform.getConfigFolder().resolve(this.manager.getConfigName() + ".json5").toFile()); | ||
Objects.requireNonNull(this.minecraft).setScreen(this.parent); | ||
}, true).sprite(JamLib.id("writable_book"), 16, 16).size(20, 20).build() | ||
); | ||
editManuallyButton.setX(7); | ||
editManuallyButton.setY(7); | ||
this.widgetList = new WidgetList(this.minecraft, this.width, this.height - 64, 32); | ||
|
||
if (this.entries.isEmpty()) { | ||
for (Field field : this.manager.getConfigClass().getFields()) { | ||
if (field.isAnnotationPresent(HiddenInGui.class)) { | ||
continue; | ||
} | ||
|
||
this.entries.add(ConfigEntry.createFromField(this.manager.getModId(), this.manager.getConfigName(), field)); | ||
} | ||
} | ||
|
||
for (ConfigEntry<T, ?> entry : this.entries) { | ||
this.widgetList.addWidgetGroup(entry.createWidgets(this.width)); | ||
} | ||
|
||
this.addRenderableWidget(this.widgetList); | ||
|
||
if (this.manager.get() instanceof ConfigExtensions<?> ext) { | ||
List<ConfigExtensions.Link> links = ext.getLinks(); | ||
|
||
for (int i = 0; i < links.size(); i++) { | ||
ConfigExtensions.Link link = links.get(i); | ||
SpriteIconButton linkButton = this.addRenderableWidget( | ||
SpriteIconButton.builder(link.getTooltip(), button -> { | ||
try { | ||
Util.getPlatform().openUri(link.getUrl().toURI()); | ||
} catch (Exception e) { | ||
JamLib.LOGGER.error("Failed to open link", e); | ||
} | ||
}, true).sprite(link.getTexture(), 16, 16).size(20, 20).build() | ||
|
||
); | ||
linkButton.setX(this.width - 30 - (28 * i)); | ||
linkButton.setY(5); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void render(GuiGraphics graphics, int mouseX, int mouseY, float delta) { | ||
super.render(graphics, mouseX, mouseY, delta); | ||
graphics.drawCenteredString(Minecraft.getInstance().font, this.title, this.width / 2, 12, 0xFFFFFF); | ||
} | ||
|
||
private boolean canExit() { | ||
return this.entries.stream().allMatch(ConfigEntry::isValid); | ||
} | ||
|
||
private boolean hasChanges() { | ||
return this.entries.stream().anyMatch(ConfigEntry::hasChanged); | ||
} | ||
|
||
@Override | ||
public void tick() { | ||
super.tick(); | ||
boolean canExit = this.canExit(); | ||
|
||
if (this.doneButton.active != canExit) { | ||
this.doneButton.active = canExit; | ||
} | ||
|
||
for (int i = 0; i < this.entries.size(); i++) { | ||
ConfigEntry<T, ?> entry = this.entries.get(i); | ||
List<AbstractWidget> widgets = entry.getNewWidgets(this.width); | ||
if (widgets != null) { | ||
this.widgetList.updateWidgetGroup(i, widgets); | ||
} | ||
} | ||
} | ||
} |
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
39 changes: 39 additions & 0 deletions
39
...c/client/java/io/github/jamalam360/jamlib/client/config/gui/entry/BooleanConfigEntry.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,39 @@ | ||
package io.github.jamalam360.jamlib.client.config.gui.entry; | ||
|
||
import net.minecraft.ChatFormatting; | ||
import net.minecraft.client.gui.components.AbstractWidget; | ||
import net.minecraft.client.gui.components.Button; | ||
import net.minecraft.network.chat.Component; | ||
import org.jetbrains.annotations.ApiStatus; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.List; | ||
|
||
public class BooleanConfigEntry<T> extends ConfigEntry<T, Boolean> { | ||
@Nullable | ||
private Button button = null; | ||
|
||
public BooleanConfigEntry(String modId, String configName, ConfigField<T, Boolean> field) { | ||
super(modId, configName, field); | ||
} | ||
|
||
@Override | ||
public List<AbstractWidget> createElementWidgets(int left, int width) { | ||
this.button = Button.builder(this.getComponent(Boolean.TRUE.equals(this.getFieldValue())), button -> this.setFieldValue(!(Boolean.TRUE.equals(this.getFieldValue())))).pos(left, 0).size(width, 20).build(); | ||
|
||
return List.of(this.button); | ||
} | ||
|
||
@Override | ||
public void onChange() { | ||
super.onChange(); | ||
|
||
if (this.button != null) { | ||
this.button.setMessage(getComponent(Boolean.TRUE.equals(this.getFieldValue()))); | ||
} | ||
} | ||
|
||
private Component getComponent(boolean value) { | ||
return Component.literal(value ? "Yes" : "No").withStyle(s -> s.withColor(value ? ChatFormatting.GREEN : ChatFormatting.RED)); | ||
} | ||
} |
Oops, something went wrong.