mirror of
https://github.com/Sollace/Unicopia.git
synced 2024-11-27 15:17:59 +01:00
Implement a configurable races whitelist
This commit is contained in:
parent
26fbda8b76
commit
58a41e4eba
6 changed files with 173 additions and 5 deletions
74
src/main/java/com/minelittlepony/unicopia/UConfig.java
Normal file
74
src/main/java/com/minelittlepony/unicopia/UConfig.java
Normal file
|
@ -0,0 +1,74 @@
|
|||
package com.minelittlepony.unicopia;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.util.List;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.annotations.Expose;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
|
||||
public class UConfig {
|
||||
|
||||
private static UConfig instance;
|
||||
|
||||
private static final Gson gson = new GsonBuilder()
|
||||
.excludeFieldsWithoutExposeAnnotation()
|
||||
.setPrettyPrinting()
|
||||
.create();
|
||||
|
||||
public static UConfig getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
static void init(File directory) {
|
||||
File file = new File(directory, "unicopia.json");
|
||||
|
||||
try {
|
||||
if (!file.exists()) {
|
||||
file.createNewFile();
|
||||
}
|
||||
|
||||
try(JsonReader reader = new JsonReader(new InputStreamReader(new FileInputStream(file)));) {
|
||||
instance = gson.fromJson(reader, UConfig.class);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (instance == null) {
|
||||
instance = new UConfig();
|
||||
}
|
||||
}
|
||||
|
||||
instance.file = file;
|
||||
|
||||
}
|
||||
|
||||
private File file;
|
||||
|
||||
@Expose
|
||||
private final List<Race> speciesWhiteList = Lists.newArrayList();
|
||||
|
||||
public List<Race> getSpeciesWhiteList() {
|
||||
return speciesWhiteList;
|
||||
}
|
||||
|
||||
public void save() {
|
||||
if (file.exists()) {
|
||||
file.delete();
|
||||
}
|
||||
|
||||
try (JsonWriter writer = new JsonWriter(new OutputStreamWriter(new FileOutputStream(file)))) {
|
||||
gson.toJson(this, UConfig.class, writer);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -101,6 +101,8 @@ public class Unicopia implements IGuiHandler {
|
|||
|
||||
@EventHandler
|
||||
public void preInit(FMLPreInitializationEvent event) {
|
||||
UConfig.init(event.getModConfigurationDirectory());
|
||||
|
||||
if (UClient.isClientSide()) {
|
||||
UEntities.preInit();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,78 @@
|
|||
package com.minelittlepony.unicopia.command;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.minelittlepony.unicopia.Race;
|
||||
import com.minelittlepony.unicopia.player.PlayerSpeciesList;
|
||||
|
||||
import net.minecraft.command.CommandBase;
|
||||
import net.minecraft.command.CommandException;
|
||||
import net.minecraft.command.ICommandSender;
|
||||
import net.minecraft.command.WrongUsageException;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.text.TextComponentTranslation;
|
||||
|
||||
class CommandRacelist extends CommandBase {
|
||||
|
||||
public String getName() {
|
||||
return "racelist";
|
||||
}
|
||||
|
||||
public int getRequiredPermissionLevel() {
|
||||
return 4;
|
||||
}
|
||||
|
||||
public String getUsage(ICommandSender sender) {
|
||||
return "commands.racelist.usage";
|
||||
}
|
||||
|
||||
public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException {
|
||||
if (args.length < 2) {
|
||||
throw new WrongUsageException(getUsage(sender));
|
||||
}
|
||||
|
||||
EntityPlayer player = getCommandSenderAsPlayer(sender);
|
||||
|
||||
Race race = Race.fromName(args[1], Race.HUMAN);
|
||||
|
||||
TextComponentTranslation formattedName = new TextComponentTranslation(race.name().toLowerCase());
|
||||
|
||||
if (race == Race.HUMAN) {
|
||||
player.sendMessage(new TextComponentTranslation("commands.racelist.illegal", formattedName));
|
||||
} else if (args[0].contentEquals("allow")) {
|
||||
PlayerSpeciesList.instance().whiteListRace(race);
|
||||
|
||||
player.sendMessage(new TextComponentTranslation("commands.racelist.allowed", formattedName));
|
||||
} else if (args[0].contentEquals("disallow")) {
|
||||
PlayerSpeciesList.instance().unwhiteListRace(race);
|
||||
|
||||
player.sendMessage(new TextComponentTranslation("commands.racelist.disallowed", formattedName));
|
||||
} else {
|
||||
throw new WrongUsageException(getUsage(sender));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the strings available in this command to the given list of tab completion options.
|
||||
*/
|
||||
public List<String> getTabCompletions(MinecraftServer server, ICommandSender sender, String[] args, BlockPos pos) {
|
||||
if (args.length == 1) {
|
||||
return getListOfStringsMatchingLastWord(args, "allow", "disallow");
|
||||
}
|
||||
|
||||
if (args.length == 2) {
|
||||
ArrayList<String> names = new ArrayList<String>();
|
||||
|
||||
for (Race i : Race.values()) {
|
||||
names.add(i.name().toLowerCase());
|
||||
}
|
||||
|
||||
return getListOfStringsMatchingLastWord(args, names.stream().toArray(String[]::new));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -158,7 +158,7 @@ class CommandSpecies extends CommandBase {
|
|||
}
|
||||
}
|
||||
|
||||
return getListOfStringsMatchingLastWord(args, names.toArray(new String[names.size()]));
|
||||
return getListOfStringsMatchingLastWord(args, names.stream().toArray(String[]::new));
|
||||
}
|
||||
|
||||
if ((args.length == 3 && args[0].contentEquals("set")) || (args[0].contentEquals("get") && args.length == 2)) {
|
||||
|
|
|
@ -7,6 +7,7 @@ public class Commands {
|
|||
public static void init(FMLServerStartingEvent event) {
|
||||
event.registerServerCommand(new CommandOverrideGameMode());
|
||||
event.registerServerCommand(new CommandSpecies());
|
||||
event.registerServerCommand(new CommandRacelist());
|
||||
|
||||
event.getServer().setAllowFlight(true);
|
||||
}
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
package com.minelittlepony.unicopia.player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import com.minelittlepony.unicopia.Race;
|
||||
import com.minelittlepony.unicopia.UConfig;
|
||||
import com.minelittlepony.unicopia.forgebullshit.FBS;
|
||||
|
||||
import net.minecraft.entity.Entity;
|
||||
|
@ -19,14 +18,28 @@ public class PlayerSpeciesList {
|
|||
return instance;
|
||||
}
|
||||
|
||||
private List<Race> serverPermittedRaces = new ArrayList<>();
|
||||
public boolean whiteListRace(Race race) {
|
||||
boolean result = UConfig.getInstance().getSpeciesWhiteList().add(race);
|
||||
|
||||
UConfig.getInstance().save();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean unwhiteListRace(Race race) {
|
||||
boolean result = UConfig.getInstance().getSpeciesWhiteList().remove(race);
|
||||
|
||||
UConfig.getInstance().save();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean speciesPermitted(Race race, EntityPlayer sender) {
|
||||
if (race == Race.ALICORN && (sender == null || !sender.capabilities.isCreativeMode)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return race.isDefault() || serverPermittedRaces.isEmpty() || serverPermittedRaces.contains(race);
|
||||
return race.isDefault() || UConfig.getInstance().getSpeciesWhiteList().isEmpty() || UConfig.getInstance().getSpeciesWhiteList().contains(race);
|
||||
}
|
||||
|
||||
public IRaceContainer<?> emptyContainer(Entity entity) {
|
||||
|
|
Loading…
Reference in a new issue