|
| 1 | +package crazypants.enderio.base.config.command; |
| 2 | + |
| 3 | +import java.util.Collections; |
| 4 | +import java.util.List; |
| 5 | +import java.util.stream.Collectors; |
| 6 | + |
| 7 | +import javax.annotation.Nonnull; |
| 8 | +import javax.annotation.Nullable; |
| 9 | + |
| 10 | +import crazypants.enderio.base.EnderIO; |
| 11 | +import crazypants.enderio.base.events.EnderIOLifecycleEvent; |
| 12 | +import net.minecraft.command.CommandBase; |
| 13 | +import net.minecraft.command.CommandException; |
| 14 | +import net.minecraft.command.ICommandSender; |
| 15 | +import net.minecraft.command.WrongUsageException; |
| 16 | +import net.minecraft.server.MinecraftServer; |
| 17 | +import net.minecraft.util.math.BlockPos; |
| 18 | +import net.minecraft.util.text.TextComponentString; |
| 19 | +import net.minecraftforge.common.MinecraftForge; |
| 20 | +import net.minecraftforge.common.config.Configuration; |
| 21 | +import net.minecraftforge.common.config.Property; |
| 22 | +import net.minecraftforge.fml.client.event.ConfigChangedEvent.OnConfigChangedEvent; |
| 23 | +import net.minecraftforge.fml.common.Mod.EventBusSubscriber; |
| 24 | +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; |
| 25 | +import net.minecraftforge.fml.relauncher.Side; |
| 26 | + |
| 27 | +@EventBusSubscriber(modid = EnderIO.MODID, value = Side.SERVER) |
| 28 | +public class CommandConfig extends CommandBase { |
| 29 | + |
| 30 | + @SubscribeEvent |
| 31 | + public static void onStarting(EnderIOLifecycleEvent.ServerStarting.Dedicated event) { |
| 32 | + event.getEvent().registerServerCommand(new CommandConfig()); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public @Nonnull String getName() { |
| 37 | + return "enderio"; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public @Nonnull String getUsage(@Nonnull ICommandSender sender) { |
| 42 | + return "/" + getName() + " config set <section> <key> <value>|config get <section> <key>|config list [<section>]|config save"; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public void execute(@Nonnull MinecraftServer server, @Nonnull ICommandSender sender, @Nonnull String[] args) throws CommandException { |
| 47 | + if (match(args, "config", "set", null, null, null)) { |
| 48 | + doSet(server, sender, args[2], args[3], args[4]); |
| 49 | + } else if (match(args, "config", "get", null, null)) { |
| 50 | + doGet(server, sender, args[2], args[3]); |
| 51 | + } else if (match(args, "config", "list", null)) { |
| 52 | + doList(server, sender, args[2]); |
| 53 | + } else if (match(args, "config", "list")) { |
| 54 | + doList(server, sender); |
| 55 | + } else if (match(args, "config", "save")) { |
| 56 | + doSave(server, sender); |
| 57 | + } else |
| 58 | + throw new WrongUsageException(getUsage(sender)); |
| 59 | + } |
| 60 | + |
| 61 | + @SuppressWarnings("null") |
| 62 | + private void doList(MinecraftServer server, ICommandSender sender) { |
| 63 | + sender.sendMessage(new TextComponentString(EnderIO.getInstance().getConfiguration().getCategoryNames().stream().collect(Collectors.joining(", ")))); |
| 64 | + } |
| 65 | + |
| 66 | + private void doSave(MinecraftServer server, ICommandSender sender) { |
| 67 | + MinecraftForge.EVENT_BUS.post(new OnConfigChangedEvent(EnderIO.MODID, null, true, false)); |
| 68 | + sender.sendMessage(new TextComponentString("Ender IO Configuration saved")); |
| 69 | + } |
| 70 | + |
| 71 | + @SuppressWarnings("null") |
| 72 | + private void doList(MinecraftServer server, ICommandSender sender, String section) { |
| 73 | + Configuration configuration = EnderIO.getInstance().getConfiguration(); |
| 74 | + if (configuration.hasCategory(section) && !configuration.getCategory(section).isEmpty()) { |
| 75 | + sender.sendMessage(new TextComponentString( |
| 76 | + configuration.getCategory(section).getOrderedValues().stream().map(property -> property.getName()).collect(Collectors.joining(", ")))); |
| 77 | + } else { |
| 78 | + sender.sendMessage(new TextComponentString("No such section")); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private void doGet(MinecraftServer server, ICommandSender sender, String section, String key) { |
| 83 | + Configuration configuration = EnderIO.getInstance().getConfiguration(); |
| 84 | + if (configuration.hasCategory(section) && !configuration.getCategory(section).isEmpty()) { |
| 85 | + Property property = configuration.getCategory(section).get(key); |
| 86 | + if (property != null) { |
| 87 | + String comment = property.getComment(); |
| 88 | + if (comment != null && !comment.isEmpty()) { |
| 89 | + sender.sendMessage(new TextComponentString(comment)); |
| 90 | + } |
| 91 | + sender.sendMessage(new TextComponentString(section + " " + key + " = " + property.getString())); |
| 92 | + } else { |
| 93 | + sender.sendMessage(new TextComponentString("No such key")); |
| 94 | + } |
| 95 | + } else { |
| 96 | + sender.sendMessage(new TextComponentString("No such section")); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private void doSet(MinecraftServer server, ICommandSender sender, String section, String key, String value) { |
| 101 | + Configuration configuration = EnderIO.getInstance().getConfiguration(); |
| 102 | + if (configuration.hasCategory(section) && !configuration.getCategory(section).isEmpty()) { |
| 103 | + Property property = configuration.getCategory(section).get(key); |
| 104 | + if (property != null) { |
| 105 | + |
| 106 | + String[] validValues = property.getValidValues(); |
| 107 | + if (validValues != null && validValues.length > 0) { |
| 108 | + for (String validValue : validValues) { |
| 109 | + if (value.equals(validValue)) { |
| 110 | + property.set(value); |
| 111 | + sender.sendMessage(new TextComponentString(section + " " + key + " = " + property.getString())); |
| 112 | + sender.sendMessage(new TextComponentString("Players may need to reconnect to experience the change")); |
| 113 | + sender.sendMessage(new TextComponentString("You need to save the config for this change to persist")); |
| 114 | + return; |
| 115 | + } |
| 116 | + } |
| 117 | + sender.sendMessage(new TextComponentString("Invalid value. Possible: " + joinNiceString(validValues))); |
| 118 | + } else { |
| 119 | + property.set(value); |
| 120 | + sender.sendMessage(new TextComponentString(section + " " + key + " = " + property.getString())); |
| 121 | + sender.sendMessage(new TextComponentString("Restrictions on possible values may apply")); |
| 122 | + sender.sendMessage(new TextComponentString("Players may need to reconnect to experience the change")); |
| 123 | + sender.sendMessage(new TextComponentString("You need to save the config for this change to persist")); |
| 124 | + } |
| 125 | + } else { |
| 126 | + sender.sendMessage(new TextComponentString("No such key")); |
| 127 | + } |
| 128 | + } else { |
| 129 | + sender.sendMessage(new TextComponentString("No such section")); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private boolean match(@Nonnull String[] args, String... pattern) { |
| 134 | + if (args.length != pattern.length) { |
| 135 | + return false; |
| 136 | + } |
| 137 | + for (int i = 0; i < pattern.length; i++) { |
| 138 | + if (pattern[i] != null && !pattern[i].equals(args[i])) { |
| 139 | + return false; |
| 140 | + } |
| 141 | + } |
| 142 | + return true; |
| 143 | + } |
| 144 | + |
| 145 | + @SuppressWarnings("null") |
| 146 | + @Override |
| 147 | + public @Nonnull List<String> getTabCompletions(@Nonnull MinecraftServer server, @Nonnull ICommandSender sender, @Nonnull String[] args, |
| 148 | + @Nullable BlockPos targetPos) { |
| 149 | + if (args.length <= 1) { |
| 150 | + return getListOfStringsMatchingLastWord(args, "config"); |
| 151 | + } |
| 152 | + if ("config".equals(args[0])) { |
| 153 | + if (args.length == 2) { |
| 154 | + return getListOfStringsMatchingLastWord(args, "set", "get", "list", "save"); |
| 155 | + } |
| 156 | + if ("set".equals(args[1]) || "get".equals(args[1]) || "list".equals(args[1])) { |
| 157 | + if (args.length == 3) { |
| 158 | + return getListOfStringsMatchingLastWord(args, EnderIO.getInstance().getConfiguration().getCategoryNames().toArray(new String[0])); |
| 159 | + } |
| 160 | + if ("set".equals(args[1]) || "get".equals(args[1])) { |
| 161 | + if (args.length == 4) { |
| 162 | + return getListOfStringsMatchingLastWord(args, EnderIO.getInstance().getConfiguration().getCategory(args[2]).getOrderedValues().stream() |
| 163 | + .map(property -> property.getName()).collect(Collectors.toList()).toArray(new String[0])); |
| 164 | + } |
| 165 | + if ("set".equals(args[1])) { |
| 166 | + Configuration configuration = EnderIO.getInstance().getConfiguration(); |
| 167 | + if (configuration.hasCategory(args[2]) && !configuration.getCategory(args[2]).isEmpty()) { |
| 168 | + Property property = configuration.getCategory(args[2]).get(args[3]); |
| 169 | + if (property != null) { |
| 170 | + String[] validValues = property.getValidValues(); |
| 171 | + if (validValues != null && validValues.length > 0) { |
| 172 | + return getListOfStringsMatchingLastWord(args, validValues); |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + return Collections.emptyList(); |
| 181 | + } |
| 182 | + |
| 183 | +} |
0 commit comments