Add the ability to change the species whitelist from the settings screen

This commit is contained in:
Sollace 2021-02-09 20:43:19 +02:00
parent f27c0af0cb
commit d48dc92528
5 changed files with 123 additions and 10 deletions

View file

@ -15,6 +15,8 @@ import com.minelittlepony.unicopia.ability.magic.Affine;
import com.minelittlepony.unicopia.item.toxin.FoodType; import com.minelittlepony.unicopia.item.toxin.FoodType;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
public enum Race implements Affine { public enum Race implements Affine {
@ -96,6 +98,10 @@ public enum Race implements Affine {
return canFly() && this != CHANGELING && this != BAT; return canFly() && this != CHANGELING && this != BAT;
} }
public Text getDisplayName() {
return new TranslatableText(getTranslationKey());
}
public String getTranslationKey() { public String getTranslationKey() {
return String.format("unicopia.race.%s", name().toLowerCase()); return String.format("unicopia.race.%s", name().toLowerCase());
} }

View file

@ -8,7 +8,7 @@ import net.minecraft.client.MinecraftClient;
public final class MineLPConnector { public final class MineLPConnector {
public static Race getPlayerPonyRace() { public static Race getPlayerPonyRace() {
if (!FabricLoader.getInstance().isModLoaded("minelp")) { if (!FabricLoader.getInstance().isModLoaded("minelp") || MinecraftClient.getInstance().player == null) {
return Race.HUMAN; return Race.HUMAN;
} }

View file

@ -1,32 +1,55 @@
package com.minelittlepony.unicopia.client.gui; 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.GameGui;
import com.minelittlepony.common.client.gui.ScrollContainer; 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.Button;
import com.minelittlepony.common.client.gui.element.Cycler; import com.minelittlepony.common.client.gui.element.Cycler;
import com.minelittlepony.common.client.gui.element.EnumSlider; import com.minelittlepony.common.client.gui.element.EnumSlider;
import com.minelittlepony.common.client.gui.element.Label; import com.minelittlepony.common.client.gui.element.Label;
import com.minelittlepony.common.client.gui.element.Toggle; 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.Config;
import com.minelittlepony.unicopia.Race; import com.minelittlepony.unicopia.Race;
import com.minelittlepony.unicopia.Unicopia; import com.minelittlepony.unicopia.Unicopia;
import com.minelittlepony.unicopia.WorldTribeManager; 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.MinecraftClient;
import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.resource.language.I18n;
import net.minecraft.client.util.math.MatrixStack; import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.server.integrated.IntegratedServer; import net.minecraft.server.integrated.IntegratedServer;
import net.minecraft.server.world.ServerWorld; import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText; import net.minecraft.text.TranslatableText;
import net.minecraft.util.Formatting;
import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.MathHelper;
public class SettingsScreen extends GameGui { 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 Config config = Unicopia.getConfig();
private final ScrollContainer content = new ScrollContainer(); private final ScrollContainer content = new ScrollContainer();
@Nullable
private Style mineLpStatus;
private boolean forceShowWhitelist;
private boolean forceHideWhitelist;
public SettingsScreen(Screen parent) { public SettingsScreen(Screen parent) {
super(new TranslatableText("unicopia.options.title"), parent); super(new TranslatableText("unicopia.options.title"), parent);
@ -48,8 +71,18 @@ public class SettingsScreen extends GameGui {
IntegratedServer server = client.getServer(); IntegratedServer server = client.getServer();
boolean canOpenLan = client.isIntegratedServerRunning() && !client.getServer().isRemote(); // In game
boolean singleColumn = server == null || LEFT < 0 || canOpenLan; // 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) { if (singleColumn) {
LEFT = content.width / 2 - 100; LEFT = content.width / 2 - 100;
@ -67,13 +100,22 @@ public class SettingsScreen extends GameGui {
.setText("gui.done"); .setText("gui.done");
content.addButton(new Label(LEFT, row)).getStyle().setText("unicopia.options.client"); content.addButton(new Label(LEFT, row)).getStyle().setText("unicopia.options.client");
content.addButton(new Toggle(LEFT, row += 20, config.ignoreMineLP.get())) 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"); .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) .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) { if (server != null) {
row += 20; row += 20;
@ -83,9 +125,11 @@ public class SettingsScreen extends GameGui {
content.addButton(new EnumSlider<>(LEFT, row += 20, tribes.getDefaultRace())) content.addButton(new EnumSlider<>(LEFT, row += 20, tribes.getDefaultRace()))
.onChange(tribes::setDefaultRace) .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()); .setEnabled(client.isInSingleplayer());
}
if (showLanOptions) {
if (RIGHT != LEFT) { if (RIGHT != LEFT) {
row = 0; row = 0;
} else { } else {
@ -93,8 +137,62 @@ public class SettingsScreen extends GameGui {
} }
content.addButton(new Label(RIGHT, row)).getStyle().setText("unicopia.options.lan"); content.addButton(new Label(RIGHT, row)).getStyle().setText("unicopia.options.lan");
Set<Race> 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 @Override
@ -106,6 +204,9 @@ public class SettingsScreen extends GameGui {
@Override @Override
public void removed() { public void removed() {
if (forceHideWhitelist) {
config.speciesWhiteList.get().clear();
}
config.save(); config.save();
} }

View file

@ -140,8 +140,8 @@ public class RaceChangeStatusEffect extends StatusEffect {
return String.format("unicopia.effect.tribe.stage.%s", name().toLowerCase()); return String.format("unicopia.effect.tribe.stage.%s", name().toLowerCase());
} }
public Text getMessage(Race species) { public Text getMessage(Race race) {
return new TranslatableText(getTranslationKey(), new TranslatableText(species.getTranslationKey())); return new TranslatableText(getTranslationKey(), race.getDisplayName());
} }
} }

View file

@ -221,7 +221,13 @@
"unicopia.options.title": "Unicopia Options", "unicopia.options.title": "Unicopia Options",
"unicopia.options.ignore_mine_lp": "Ignore Mine Little Pony", "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.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.client": "Client Settings",
"unicopia.options.world": "World Settings", "unicopia.options.world": "World Settings",
"unicopia.options.world.default_race": "Default Race: %s", "unicopia.options.world.default_race": "Default Race: %s",