Skip to content

Instantly share code, notes, and snippets.

@jwne
Forked from SilverCory/Properties.java
Created January 6, 2016 15:21
Show Gist options
  • Save jwne/5dc49fd739ca535c1676 to your computer and use it in GitHub Desktop.
Save jwne/5dc49fd739ca535c1676 to your computer and use it in GitHub Desktop.
Simple class for changing Minecraft server properties easily.
import org.bukkit.Bukkit;
import java.lang.reflect.InvocationTargetException;
public class Properties {
public static void savePropertiesFile() throws Exception {
Object propertyManager = getPropertyManager( getMinecraftServer() );
propertyManager.getClass().getDeclaredMethod( "savePropertiesFile" ).invoke( propertyManager );
}
public static void setServerProperty(ServerProperty property, Object value) throws Exception {
Object propertyManager = getPropertyManager( getMinecraftServer() );
propertyManager.getClass().getDeclaredMethod( "setProperty", String.class, Object.class ).invoke( propertyManager, property.getPropertyName(), value );
}
public enum ServerProperty {
SPAWN_PROTECTION("spawn-protection"),
SERVER_NAME("server-name"),
FORCE_GAMEMODE("force-gamemode"),
NETHER("allow-nether"),
DEFAULT_GAMEMODE("gamemode"),
QUERY("enable-query"),
PLAYER_IDLE_TIMEOUT("player-idle-timeout"),
DIFFICULTY("difficulty"),
SPAWN_MONSTERS("spawn-monsters"),
OP_PERMISSION_LEVEL("op-permission-level"),
RESOURCE_PACK_HASH("resource-pack-hash"),
RESOURCE_PACK("resource-pack"),
ANNOUNCE_PLAYER_ACHIEVEMENTS("announce-player-achievements"),
PVP("pvp"),
SNOOPER("snooper-enabled"),
LEVEL_NAME("level-name"),
LEVEL_TYPE("level-type"),
LEVEL_SEED("level-seed"),
HARDCORE("hardcore"),
COMMAND_BLOCKS("enable-command-blocks"),
MAX_PLAYERS("max-players"),
PACKET_COMPRESSION_LIMIT("network-compression-threshold"),
MAX_WORLD_SIZE("max-world-size"),
IP("server-ip"),
PORT("server-port"),
DEBUG_MODE("debug"),
SPAWN_NPCS("spawn-npcs"),
SPAWN_ANIMALS("spawn-animals"),
FLIGHT("allow-flight"),
VIEW_DISTANCE("view-distance"),
WHITE_LIST("white-list"),
GENERATE_STRUCTURES("generate-structures"),
MAX_BUILD_HEIGHT("max-build-height"),
MOTD("motd"),
REMOTE_CONTROL("enable-rcon");
private String propertyName;
ServerProperty(String propertyName) {
this.propertyName = propertyName;
}
public String getPropertyName() {
return propertyName;
}
}
public static String getVersion() {
return Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
}
public static Object getMinecraftServer() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
return Class.forName( "net.minecraft.server." + getVersion() + ".MinecraftServer" ).getDeclaredMethod("getServer").invoke( null );
}
public static Object getPropertyManager( Object minecraftServer ) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
if(!Class.forName( "net.minecraft.server." + getVersion() + ".MinecraftServer" ).isInstance( minecraftServer )) {
throw new IllegalArgumentException( "The supplied object isn't a minecraft server." );
}
return minecraftServer.getClass().getDeclaredMethod( "getPropertyManager" ).invoke( minecraftServer );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment