Skip to content

Commit 13a42d1

Browse files
committed
Added config command for dedicated servers
1 parent 5bcaa6b commit 13a42d1

File tree

3 files changed

+226
-1
lines changed

3 files changed

+226
-1
lines changed

enderio-base/src/main/java/crazypants/enderio/base/EnderIO.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
6565
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
6666
import net.minecraftforge.fml.common.event.FMLServerAboutToStartEvent;
67+
import net.minecraftforge.fml.common.event.FMLServerStartingEvent;
6768
import net.minecraftforge.fml.common.event.FMLServerStoppedEvent;
6869
import net.minecraftforge.fml.common.network.NetworkCheckHandler;
6970
import net.minecraftforge.fml.relauncher.Side;
@@ -220,7 +221,7 @@ public void serverStopped(@Nonnull FMLServerStoppedEvent event) {
220221
}
221222

222223
@EventHandler
223-
public static void onServerStart(FMLServerAboutToStartEvent event) {
224+
public static void onServerStart(@Nonnull FMLServerAboutToStartEvent event) {
224225
MinecraftForge.EVENT_BUS.post(new EnderIOLifecycleEvent.ServerAboutToStart.Pre());
225226
if (DiagnosticsConfig.debugProfilerTracer.get()) {
226227
ProfilerDebugger.init(event);
@@ -235,6 +236,11 @@ public static void onServerStart(FMLServerAboutToStartEvent event) {
235236
MinecraftForge.EVENT_BUS.post(new EnderIOLifecycleEvent.ServerAboutToStart.Post());
236237
}
237238

239+
@EventHandler
240+
public static void onServerStarting(@Nonnull FMLServerStartingEvent event) {
241+
MinecraftForge.EVENT_BUS.post(EnderIOLifecycleEvent.ServerStarting.get(event));
242+
}
243+
238244
void processImc(ImmutableList<IMCMessage> messages) {
239245
for (IMCMessage msg : messages) {
240246
String key = msg.key;
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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+
}

enderio-base/src/main/java/crazypants/enderio/base/events/EnderIOLifecycleEvent.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import javax.annotation.Nonnull;
44

5+
import crazypants.enderio.base.EnderIO;
56
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
7+
import net.minecraftforge.fml.common.event.FMLServerStartingEvent;
68
import net.minecraftforge.fml.common.eventhandler.Event;
79

810
public abstract class EnderIOLifecycleEvent extends Event {
@@ -97,4 +99,38 @@ public static class Post extends ServerStopped {
9799

98100
}
99101

102+
public abstract static class ServerStarting extends EnderIOLifecycleEvent {
103+
104+
private final @Nonnull FMLServerStartingEvent event;
105+
106+
public @Nonnull FMLServerStartingEvent getEvent() {
107+
return event;
108+
}
109+
110+
private ServerStarting(@Nonnull FMLServerStartingEvent event) {
111+
this.event = event;
112+
}
113+
114+
public static ServerStarting get(@Nonnull FMLServerStartingEvent event) {
115+
return EnderIO.proxy.isDedicatedServer() ? new Dedicated(event) : new Integrated(event);
116+
}
117+
118+
public static class Dedicated extends ServerStarting {
119+
120+
private Dedicated(@Nonnull FMLServerStartingEvent event) {
121+
super(event);
122+
}
123+
124+
}
125+
126+
public static class Integrated extends ServerStarting {
127+
128+
private Integrated(@Nonnull FMLServerStartingEvent event) {
129+
super(event);
130+
}
131+
132+
}
133+
134+
}
135+
100136
}

0 commit comments

Comments
 (0)