Fixed saving of settings and added a configuration screen

This commit is contained in:
Sollace 2021-02-07 19:57:35 +02:00
parent c75a2077a8
commit 9e8d18b8e3
9 changed files with 182 additions and 95 deletions

View file

@ -3,71 +3,31 @@ package com.minelittlepony.unicopia;
import java.util.HashSet;
import java.util.Set;
import com.google.gson.annotations.Expose;
import com.minelittlepony.common.util.GamePaths;
import com.minelittlepony.common.util.settings.JsonConfig;
import com.minelittlepony.common.util.settings.Setting;
public class Config extends JsonConfig {
/*private final String speciesWhiteListComment =
"A whitelist of races permitted on the server. " +
"Races added to this list can be used by anyone, whilst any ones left off are not permitted. " +
"An empty list disables whitelisting entirely.";*/
@Deprecated
public static Config getInstance() {
return Unicopia.getConfig();
}
public final Setting<Set<Race>> speciesWhiteList = value("server", "speciesWhiteList", new HashSet<>());
/*private final String preferredRaceComment =
"The default preferred race. " +
"This is the race a client requests when first joining a game. " +
"It is the default used both when Mine Little Pony is not installed and when they respond with a human race.";*/
public final Setting<Race> preferredRace = value("client", "preferredRace", Race.EARTH);
/*private final String ignoreMineLPComment =
"If true Mine Little Pony will not be considered when determining the race to use. " +
"The result will always be what is set by this config file.";*/
public final Setting<Boolean> ignoreMineLP = value("client", "ignoreMineLP", false);
public Config() {
super(GamePaths.getConfigDirectory().resolve("unicopia.json"));
}
@Expose(deserialize = false)
private final String speciesWhiteListComment =
"A whitelist of races permitted on the server. " +
"Races added to this list can be used by anyone, whilst any ones left off are not permitted. " +
"An empty list disables whitelisting entirely.";
@Expose
private final Set<Race> speciesWhiteList = new HashSet<>();
@Expose(deserialize = false)
private final String preferredRaceComment =
"The default preferred race. " +
"This is the race a client requests when first joining a game. " +
"It is the default used both when Mine Little Pony is not installed and when they respond with a human race.";
@Expose
private Race preferredRace = Race.EARTH;
@Expose(deserialize = false)
private final String ignoreMineLPComment =
"If true Mine Little Pony will not be considered when determining the race to use. " +
"The result will always be what is set by this config file.";
@Expose
private boolean ignoreMineLP = false;
public Set<Race> getSpeciesWhiteList() {
return speciesWhiteList;
}
public boolean ignoresMineLittlePony() {
return ignoreMineLP;
}
public void setIgnoreMineLittlePony(boolean value) {
if (value != ignoreMineLP) {
ignoreMineLP = value;
save();
}
}
public void setPreferredRace(Race race) {
if (preferredRace != race) {
preferredRace = race;
save();
}
}
public Race getPrefferedRace() {
if (preferredRace == null) {
setPreferredRace(Race.EARTH);
}
return preferredRace;
}
}

View file

@ -105,7 +105,7 @@ public enum Race implements Affine {
return false;
}
Set<Race> whitelist = Unicopia.getConfig().getSpeciesWhiteList();
Set<Race> whitelist = Unicopia.getConfig().speciesWhiteList.get();
return isDefault()
|| whitelist.isEmpty()

View file

@ -7,7 +7,7 @@ import net.minecraft.world.dimension.DimensionType;
public class WorldTribeManager extends PersistentState {
private Race defaultRace = Unicopia.getConfig().getPrefferedRace();
private Race defaultRace = Unicopia.getConfig().preferredRace.get();
public WorldTribeManager(ServerWorld world) {
super(nameFor(world.getDimension()));
@ -17,6 +17,10 @@ public class WorldTribeManager extends PersistentState {
return defaultRace;
}
public Race setDefaultRace(Race race) {
return defaultRace = race;
}
@Override
public void fromTag(CompoundTag tag) {
defaultRace = Race.fromName(tag.getString("defaultRace"));

View file

@ -2,12 +2,12 @@ package com.minelittlepony.unicopia.client;
import java.util.Optional;
import com.minelittlepony.common.client.gui.element.Cycler;
import com.minelittlepony.common.event.ScreenInitCallback;
import com.minelittlepony.common.event.ScreenInitCallback.ButtonList;
import com.minelittlepony.unicopia.InteractionManager;
import com.minelittlepony.unicopia.Race;
import com.minelittlepony.unicopia.Unicopia;
import com.minelittlepony.unicopia.client.gui.SettingsScreen;
import com.minelittlepony.unicopia.client.gui.UHud;
import com.minelittlepony.unicopia.entity.player.PlayerCamera;
import com.minelittlepony.unicopia.entity.player.Pony;
@ -17,9 +17,7 @@ import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.world.CreateWorldScreen;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.math.MathHelper;
public class UnicopiaClient implements ClientModInitializer {
@ -34,7 +32,7 @@ public class UnicopiaClient implements ClientModInitializer {
}
public static Race getPreferredRace() {
if (!Unicopia.getConfig().ignoresMineLittlePony()
if (!Unicopia.getConfig().ignoreMineLP.get()
&& MinecraftClient.getInstance().player != null) {
Race race = MineLPConnector.getPlayerPonyRace();
@ -43,7 +41,7 @@ public class UnicopiaClient implements ClientModInitializer {
}
}
return Unicopia.getConfig().getPrefferedRace();
return Unicopia.getConfig().preferredRace.get();
}
public static float getWorldBrightness(float initial) {
@ -68,26 +66,7 @@ public class UnicopiaClient implements ClientModInitializer {
private void onScreenInit(Screen screen, ButtonList buttons) {
if (screen instanceof CreateWorldScreen) {
buttons.add(new Cycler(screen.width / 2 + 110, 60, 20, 20) {
@Override
protected void renderForground(MatrixStack matrices, MinecraftClient mc, int mouseX, int mouseY, int foreColor) {
super.renderForground(matrices, mc, mouseX, mouseY, foreColor);
if (isMouseOver(mouseX, mouseY)) {
renderToolTip(matrices, screen, mouseX, mouseY);
}
}
}).setStyles(
Race.EARTH.getStyle(),
Race.UNICORN.getStyle(),
Race.PEGASUS.getStyle(),
Race.BAT.getStyle(),
Race.ALICORN.getStyle(),
Race.CHANGELING.getStyle()
).onChange(i -> {
Unicopia.getConfig().setPreferredRace(Race.fromId(i + 1));
return i;
}).setValue(MathHelper.clamp(Unicopia.getConfig().getPrefferedRace().ordinal() - 1, 0, 5));
buttons.add(SettingsScreen.createRaceSelector(screen));
}
}
}

View file

@ -1,26 +1,137 @@
package com.minelittlepony.unicopia.client.gui;
import com.minelittlepony.common.client.gui.GameGui;
import com.minelittlepony.common.client.gui.ScrollContainer;
import com.minelittlepony.common.client.gui.element.Button;
import com.minelittlepony.common.client.gui.element.Cycler;
import com.minelittlepony.common.client.gui.element.EnumSlider;
import com.minelittlepony.common.client.gui.element.Label;
import com.minelittlepony.common.client.gui.element.Toggle;
import com.minelittlepony.unicopia.Config;
import com.minelittlepony.unicopia.Race;
import com.minelittlepony.unicopia.Unicopia;
import com.minelittlepony.unicopia.WorldTribeManager;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.resource.language.I18n;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.server.integrated.IntegratedServer;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.math.MathHelper;
public class SettingsScreen extends GameGui {
private final Config config = Unicopia.getConfig();
private final ScrollContainer content = new ScrollContainer();
public SettingsScreen(Screen parent) {
super(new TranslatableText("options.title"), parent);
super(new TranslatableText("unicopia.options.title"), parent);
content.margin.top = 30;
content.margin.bottom = 30;
content.padding.top = 10;
content.padding.right = 10;
content.padding.bottom = 20;
content.padding.left = 10;
}
@Override
public void init() {
addButton(new Label(width / 2, 15))
content.init(this::rebuildContent);
}
private void rebuildContent() {
int LEFT = content.width / 2 - 210;
int RIGHT = content.width / 2 + 10;
IntegratedServer server = client.getServer();
boolean canOpenLan = client.isIntegratedServerRunning() && !client.getServer().isRemote();
boolean singleColumn = server == null || LEFT < 0 || canOpenLan;
if (singleColumn) {
LEFT = content.width / 2 - 100;
RIGHT = LEFT;
}
children().add(content);
int row = 0;
addButton(new Label(width / 2, 5).setCentered()).getStyle().setText(getTitle().getString());
addButton(new Button(width / 2 - 100, height - 25))
.onClick(sender -> finish())
.getStyle()
.setColor(16777215)
.setText(title);
addButton(new Button(width / 2 - 100, height / 6 + 168))
.onClick(s -> finish())
.getStyle().setText("gui.done");
.setText("gui.done");
content.addButton(new Label(LEFT, row)).getStyle().setText("unicopia.options.client");
content.addButton(new Toggle(LEFT, row += 20, config.ignoreMineLP.get()))
.onChange(config.ignoreMineLP::set)
.getStyle().setText("unicopia.options.ignore_mine_lp");
content.addButton(new EnumSlider<>(LEFT, row += 20, config.preferredRace.get()))
.onChange(config.preferredRace::set)
.setFormatter(v -> I18n.translate("unicopia.options.preferred_race", v));
if (server != null) {
row += 20;
content.addButton(new Label(LEFT, row)).getStyle().setText("unicopia.options.world");
WorldTribeManager tribes = WorldTribeManager.forWorld((ServerWorld)server.getPlayerManager().getPlayer(MinecraftClient.getInstance().player.getUuid()).world);
content.addButton(new EnumSlider<>(LEFT, row += 20, tribes.getDefaultRace()))
.onChange(tribes::setDefaultRace)
.setFormatter(v -> I18n.translate("unicopia.options.world.default_race", v))
.setEnabled(client.isInSingleplayer());
if (RIGHT != LEFT) {
row = 0;
} else {
row += 20;
}
content.addButton(new Label(RIGHT, row)).getStyle().setText("unicopia.options.lan");
}
}
@Override
public void render(MatrixStack matrices, int mouseX, int mouseY, float tickDelta) {
renderBackground(matrices);
super.render(matrices, mouseX, mouseY, tickDelta);
content.render(matrices, mouseX, mouseY, tickDelta);
}
@Override
public void removed() {
config.save();
}
public static Cycler createRaceSelector(Screen screen) {
return new Cycler(screen.width / 2 + 110, 60, 20, 20) {
@Override
protected void renderForground(MatrixStack matrices, MinecraftClient mc, int mouseX, int mouseY, int foreColor) {
super.renderForground(matrices, mc, mouseX, mouseY, foreColor);
if (isMouseOver(mouseX, mouseY)) {
renderToolTip(matrices, screen, mouseX, mouseY);
}
}
}.setStyles(
Race.EARTH.getStyle(),
Race.UNICORN.getStyle(),
Race.PEGASUS.getStyle(),
Race.BAT.getStyle(),
Race.ALICORN.getStyle(),
Race.CHANGELING.getStyle()
).onChange(i -> {
Unicopia.getConfig().preferredRace.set(Race.fromId(i + 1));
Unicopia.getConfig().save();
return i;
}).setValue(MathHelper.clamp(Unicopia.getConfig().preferredRace.get().ordinal() - 1, 0, 5));
}
}

View file

@ -22,7 +22,7 @@ class RacelistCommand {
builder.then(CommandManager.literal("allow")
.then(CommandManager.argument("race", new RaceArgument())
.executes(context -> toggle(context.getSource(), context.getSource().getPlayer(), context.getArgument("race", Race.class), "allowed", race -> {
boolean result = Unicopia.getConfig().getSpeciesWhiteList().remove(race);
boolean result = Unicopia.getConfig().speciesWhiteList.get().remove(race);
Unicopia.getConfig().save();
@ -32,7 +32,7 @@ class RacelistCommand {
builder.then(CommandManager.literal("disallow")
.then(CommandManager.argument("race", new RaceArgument())
.executes(context -> toggle(context.getSource(), context.getSource().getPlayer(), context.getArgument("race", Race.class), "disallowed", race -> {
boolean result = Unicopia.getConfig().getSpeciesWhiteList().add(race);
boolean result = Unicopia.getConfig().speciesWhiteList.get().add(race);
Unicopia.getConfig().save();

View file

@ -0,0 +1,13 @@
package com.minelittlepony.unicopia.modmenu;
import com.minelittlepony.unicopia.client.gui.SettingsScreen;
import io.github.prospector.modmenu.api.ConfigScreenFactory;
import io.github.prospector.modmenu.api.ModMenuApi;
public class UMenuFactory implements ModMenuApi {
@Override
public ConfigScreenFactory<?> getModConfigScreenFactory() {
return SettingsScreen::new;
}
}

View file

@ -219,6 +219,14 @@
"commands.gravity.set": "Your gravity was set to %2$f",
"commands.gravity.set.other": "Set %1$s's gravity to %2$f",
"unicopia.options.title": "Unicopia Options",
"unicopia.options.ignore_mine_lp": "Ignore Mine Little Pony",
"unicopia.options.preferred_race": "Preferred Race: %s",
"unicopia.options.client": "Client Settings",
"unicopia.options.world": "World Settings",
"unicopia.options.world.default_race": "Default Race: %s",
"unicopia.options.lan": "Multiplayer (LAN) Settings",
"unicopia.race.human": "Human",
"unicopia.race.human.alt": "Humans",
"unicopia.race.earth": "Earth Pony",

View file

@ -5,6 +5,10 @@
"name": "Unicopia",
"description": "Magical Abilities for Mine Little Pony!",
"authors": [ "Sollace" ],
"contact": {
"sources": "https://github.com/Sollace/Unicopia",
"issues": "https://github.com/Sollace/Unicopia/issues"
},
"mixins": [
"unicopia.mixin.json"
],
@ -16,6 +20,9 @@
],
"client": [
"com.minelittlepony.unicopia.client.UnicopiaClient"
],
"modmenu": [
"com.minelittlepony.unicopia.modmenu.UMenuFactory"
]
},
"depends": {
@ -23,6 +30,11 @@
"fabric-api-base": ">=0.1.0",
"fabric-events-lifecycle-v0": ">=0.1.0",
"fabric-keybindings-v0": ">=0.1.0",
"fabric-rendering-v1": ">=0.1.0"
"fabric-rendering-v1": ">=0.1.0",
"kirin": "*"
},
"suggests": {
"minelp": "*",
"modmenu": "*"
}
}