From d48dc925282a64e78fd0cf87282e223cd1224224 Mon Sep 17 00:00:00 2001 From: Sollace Date: Tue, 9 Feb 2021 20:43:19 +0200 Subject: [PATCH] Add the ability to change the species whitelist from the settings screen --- .../com/minelittlepony/unicopia/Race.java | 6 + .../unicopia/client/MineLPConnector.java | 2 +- .../unicopia/client/gui/SettingsScreen.java | 115 ++++++++++++++++-- .../entity/effect/RaceChangeStatusEffect.java | 4 +- .../resources/assets/unicopia/lang/en_us.json | 6 + 5 files changed, 123 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/Race.java b/src/main/java/com/minelittlepony/unicopia/Race.java index cc7fc71b..286fe774 100644 --- a/src/main/java/com/minelittlepony/unicopia/Race.java +++ b/src/main/java/com/minelittlepony/unicopia/Race.java @@ -15,6 +15,8 @@ import com.minelittlepony.unicopia.ability.magic.Affine; import com.minelittlepony.unicopia.item.toxin.FoodType; import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.text.Text; +import net.minecraft.text.TranslatableText; import net.minecraft.util.Identifier; public enum Race implements Affine { @@ -96,6 +98,10 @@ public enum Race implements Affine { return canFly() && this != CHANGELING && this != BAT; } + public Text getDisplayName() { + return new TranslatableText(getTranslationKey()); + } + public String getTranslationKey() { return String.format("unicopia.race.%s", name().toLowerCase()); } diff --git a/src/main/java/com/minelittlepony/unicopia/client/MineLPConnector.java b/src/main/java/com/minelittlepony/unicopia/client/MineLPConnector.java index 68e8a0e2..c7084833 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/MineLPConnector.java +++ b/src/main/java/com/minelittlepony/unicopia/client/MineLPConnector.java @@ -8,7 +8,7 @@ import net.minecraft.client.MinecraftClient; public final class MineLPConnector { public static Race getPlayerPonyRace() { - if (!FabricLoader.getInstance().isModLoaded("minelp")) { + if (!FabricLoader.getInstance().isModLoaded("minelp") || MinecraftClient.getInstance().player == null) { return Race.HUMAN; } diff --git a/src/main/java/com/minelittlepony/unicopia/client/gui/SettingsScreen.java b/src/main/java/com/minelittlepony/unicopia/client/gui/SettingsScreen.java index 77c74d49..abfffb5d 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/gui/SettingsScreen.java +++ b/src/main/java/com/minelittlepony/unicopia/client/gui/SettingsScreen.java @@ -1,32 +1,55 @@ package com.minelittlepony.unicopia.client.gui; +import java.util.Set; + +import javax.annotation.Nullable; + import com.minelittlepony.common.client.gui.GameGui; import com.minelittlepony.common.client.gui.ScrollContainer; +import com.minelittlepony.common.client.gui.dimension.Bounds; 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.common.client.gui.packing.GridPacker; +import com.minelittlepony.common.client.gui.sprite.TextureSprite; +import com.minelittlepony.common.client.gui.style.Style; import com.minelittlepony.unicopia.Config; import com.minelittlepony.unicopia.Race; import com.minelittlepony.unicopia.Unicopia; import com.minelittlepony.unicopia.WorldTribeManager; +import com.minelittlepony.unicopia.client.MineLPConnector; +import net.fabricmc.loader.api.FabricLoader; 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.Text; import net.minecraft.text.TranslatableText; +import net.minecraft.util.Formatting; import net.minecraft.util.math.MathHelper; public class SettingsScreen extends GameGui { + private static final GridPacker WHITELIST_GRID_PACKER = new GridPacker() + .setItemSpacing(25) + .setListWidth(200) + .setItemWidth(30) + .setItemHeight(0); + private final Config config = Unicopia.getConfig(); private final ScrollContainer content = new ScrollContainer(); + @Nullable + private Style mineLpStatus; + + private boolean forceShowWhitelist; + private boolean forceHideWhitelist; + public SettingsScreen(Screen parent) { super(new TranslatableText("unicopia.options.title"), parent); @@ -48,8 +71,18 @@ public class SettingsScreen extends GameGui { IntegratedServer server = client.getServer(); - boolean canOpenLan = client.isIntegratedServerRunning() && !client.getServer().isRemote(); - boolean singleColumn = server == null || LEFT < 0 || canOpenLan; + // In game + // Singleplayer server != null && !server.isRemote(); + // Lan (hosting) server != null && server.isRemote(); + // Multiplayer server == null + // Out of game + // server == null + + final boolean showLanOptions = server == null || server.isRemote(); + + final boolean canEditWhitelist = client.world == null || server != null && server.isRemote(); + + boolean singleColumn = LEFT < 0 || !showLanOptions; if (singleColumn) { LEFT = content.width / 2 - 100; @@ -67,13 +100,22 @@ public class SettingsScreen extends GameGui { .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) + .onChange(v -> { + config.ignoreMineLP.set(v); + if (mineLpStatus != null) { + mineLpStatus.setText(getMineLPStatus()); + } + return v; + }) .getStyle().setText("unicopia.options.ignore_mine_lp"); - content.addButton(new EnumSlider<>(LEFT, row += 20, config.preferredRace.get())) + mineLpStatus = content.addButton(new Label(LEFT, row += 10)).getStyle().setText(getMineLPStatus()); + + content.addButton(new EnumSlider<>(LEFT, row += 25, config.preferredRace.get())) .onChange(config.preferredRace::set) - .setFormatter(v -> I18n.translate("unicopia.options.preferred_race", v)); + .setFormatter(v -> new TranslatableText("unicopia.options.preferred_race", v.getDisplayName()).getString()); if (server != null) { row += 20; @@ -83,9 +125,11 @@ public class SettingsScreen extends GameGui { content.addButton(new EnumSlider<>(LEFT, row += 20, tribes.getDefaultRace())) .onChange(tribes::setDefaultRace) - .setFormatter(v -> I18n.translate("unicopia.options.world.default_race", v)) + .setFormatter(v -> new TranslatableText("unicopia.options.world.default_race", v.getDisplayName()).getString()) .setEnabled(client.isInSingleplayer()); + } + if (showLanOptions) { if (RIGHT != LEFT) { row = 0; } else { @@ -93,8 +137,62 @@ public class SettingsScreen extends GameGui { } content.addButton(new Label(RIGHT, row)).getStyle().setText("unicopia.options.lan"); + + Set whitelist = config.speciesWhiteList.get(); + boolean whitelistEnabled = (forceShowWhitelist || !whitelist.isEmpty()) && !forceHideWhitelist; + + content.addButton(new Toggle(RIGHT, row += 20, whitelistEnabled)) + .onChange(v -> { + forceShowWhitelist = v; + forceHideWhitelist = !v; + init(); + return v; + }) + .setEnabled(canEditWhitelist) + .getStyle().setText("unicopia.options.whitelist"); + + if (whitelistEnabled) { + + content.addButton(new Label(RIGHT, row += 20)).getStyle().setText(new TranslatableText("unicopia.options.whitelist.details").formatted(Formatting.DARK_GREEN)); + + row += 20; + WHITELIST_GRID_PACKER.start(); + + for (Race race : Race.values()) { + if (!race.isDefault()) { + Bounds bound = WHITELIST_GRID_PACKER.next(); + + Button button = content.addButton(new Toggle(RIGHT + bound.left + 10, row + bound.top, whitelist.contains(race))) + .onChange(v -> { + if (v) { + whitelist.add(race); + } else { + whitelist.remove(race); + } + return v; + }) + .setEnabled(canEditWhitelist) + .setStyle(race.getStyle()); + + ((TextureSprite)button.getStyle().getIcon()).setPosition(-20, 0); + } + } + } + } + } + + private Text getMineLPStatus() { + final boolean hasMineLP = FabricLoader.getInstance().isModLoaded("minelp"); + + if (hasMineLP) { + if (config.ignoreMineLP.get()) { + return new TranslatableText("unicopia.options.ignore_mine_lp.undetected").formatted(Formatting.DARK_GREEN); + } + + return new TranslatableText("unicopia.options.ignore_mine_lp.detected", MineLPConnector.getPlayerPonyRace().getDisplayName()).formatted(Formatting.GREEN); } + return new TranslatableText("unicopia.options.ignore_mine_lp.missing").formatted(Formatting.RED); } @Override @@ -106,6 +204,9 @@ public class SettingsScreen extends GameGui { @Override public void removed() { + if (forceHideWhitelist) { + config.speciesWhiteList.get().clear(); + } config.save(); } diff --git a/src/main/java/com/minelittlepony/unicopia/entity/effect/RaceChangeStatusEffect.java b/src/main/java/com/minelittlepony/unicopia/entity/effect/RaceChangeStatusEffect.java index 005afa1f..bb0ad5f3 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/effect/RaceChangeStatusEffect.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/effect/RaceChangeStatusEffect.java @@ -140,8 +140,8 @@ public class RaceChangeStatusEffect extends StatusEffect { return String.format("unicopia.effect.tribe.stage.%s", name().toLowerCase()); } - public Text getMessage(Race species) { - return new TranslatableText(getTranslationKey(), new TranslatableText(species.getTranslationKey())); + public Text getMessage(Race race) { + return new TranslatableText(getTranslationKey(), race.getDisplayName()); } } diff --git a/src/main/resources/assets/unicopia/lang/en_us.json b/src/main/resources/assets/unicopia/lang/en_us.json index 197ee2aa..53b5977c 100644 --- a/src/main/resources/assets/unicopia/lang/en_us.json +++ b/src/main/resources/assets/unicopia/lang/en_us.json @@ -221,7 +221,13 @@ "unicopia.options.title": "Unicopia Options", "unicopia.options.ignore_mine_lp": "Ignore Mine Little Pony", + "unicopia.options.ignore_mine_lp.missing": "* Mine Little Pony is not installed", + "unicopia.options.ignore_mine_lp.detected": "* Your detected race is %s", + "unicopia.options.ignore_mine_lp.undetected": "* We will not use your detected race", "unicopia.options.preferred_race": "Preferred Race: %s", + "unicopia.options.whitelist": "Enable Whitelist", + "unicopia.options.whitelist.race": "Allow %ss", + "unicopia.options.whitelist.details": "* Select the races you wish to allow", "unicopia.options.client": "Client Settings", "unicopia.options.world": "World Settings", "unicopia.options.world.default_race": "Default Race: %s",