Basic Config API for sponge, need to do comments

This commit is contained in:
Myles 2016-09-26 20:12:34 +01:00
parent 3411c3d144
commit 8b9a1750de
8 changed files with 173 additions and 54 deletions

View File

@ -1,8 +1,7 @@
PORT STUFF TO GUAVA :D (so we dont need to include commons)
Test on SpongeForge, only tested SpongeVanilla
Stop using new Gson() everywhere
Java docs (for platforms etc)
Config implementation for sponge
Add comments to sponge api
Port bukkit listeners to sponge maybe
Fix task ids, methods for sponge
Find all the TODO's and check they're done.

View File

@ -13,7 +13,7 @@ public class ViaConfig implements ViaVersionConfig, ConfigurationProvider {
public ViaConfig(ViaVersionPlugin plugin) {
this.plugin = plugin;
generateConfig();
reloadConfig();
}
public void generateConfig() {
@ -198,6 +198,11 @@ public class ViaConfig implements ViaVersionConfig, ConfigurationProvider {
plugin.saveConfig();
}
@Override
public void reloadConfig() {
generateConfig();
}
@Override
public Map<String, Object> getValues() {
return plugin.getConfig().getValues(false);

View File

@ -17,7 +17,7 @@ import us.myles.ViaVersion.bungee.BungeeViaAPI;
import us.myles.ViaVersion.bungee.BungeeViaInjector;
import us.myles.ViaVersion.bungee.BungeeViaLoader;
import us.myles.ViaVersion.bungee.command.BungeeCommandSender;
import us.myles.ViaVersion.bungee.config.BungeeConfigProvider;
import us.myles.ViaVersion.bungee.config.BungeeConfigAPI;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@ -25,13 +25,13 @@ import java.util.concurrent.TimeUnit;
public class Bungee extends Plugin implements ViaPlatform {
private BungeeViaAPI api;
private BungeeConfigProvider config;
private BungeeConfigAPI config;
private BungeeCommandHandler commandHandler;
@Override
public void onLoad() {
api = new BungeeViaAPI();
config = new BungeeConfigProvider();
config = new BungeeConfigAPI();
commandHandler = new BungeeCommandHandler();
ProxyServer.getInstance().getPluginManager().registerCommand(this, new BungeeCommand(commandHandler));
// Init platform

View File

@ -8,7 +8,7 @@ import java.util.List;
import java.util.Map;
// TODO implement
public class BungeeConfigProvider implements ViaVersionConfig, ConfigurationProvider {
public class BungeeConfigAPI implements ViaVersionConfig, ConfigurationProvider {
@Override
public boolean isCheckForUpdates() {
return false;
@ -169,6 +169,11 @@ public class BungeeConfigProvider implements ViaVersionConfig, ConfigurationProv
}
@Override
public void reloadConfig() {
}
@Override
public Map<String, Object> getValues() {
return null;

View File

@ -17,6 +17,11 @@ public interface ConfigurationProvider {
*/
void saveConfig();
/**
* Reloads the config
*/
void reloadConfig();
/**
* Get all the configuration values
*

View File

@ -17,7 +17,7 @@ public class ReloadSubCmd extends ViaSubCommand {
@Override
public boolean execute(ViaCommandSender sender, String[] args) {
Via.getPlatform().getConfigurationProvider().saveConfig();
Via.getPlatform().getConfigurationProvider().reloadConfig();
sendMessage(sender, "&6Configuration successfully reloaded! Some features may need a restart.");
return true;
}

View File

@ -3,7 +3,11 @@ package us.myles.ViaVersion;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.inject.Inject;
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import ninja.leaping.configurate.loader.ConfigurationLoader;
import ninja.leaping.configurate.yaml.YAMLConfigurationLoader;
import org.spongepowered.api.Game;
import org.spongepowered.api.config.DefaultConfig;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.game.state.GameAboutToStartServerEvent;
@ -21,6 +25,7 @@ import us.myles.ViaVersion.api.platform.ViaPlatform;
import us.myles.ViaVersion.dump.PluginInfo;
import us.myles.ViaVersion.sponge.*;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -37,13 +42,18 @@ import java.util.logging.Logger;
public class SpongePlugin implements ViaPlatform {
@Inject
private Game game;
@Inject
private PluginContainer container;
@Inject
@DefaultConfig(sharedRoot = false)
private File defaultConfig;
private SpongeViaAPI api = new SpongeViaAPI();
private SpongeExecutorService asyncExecutor;
private SpongeExecutorService syncExecutor;
private SpongeConfigAPI conf = new SpongeConfigAPI(this);
private SpongeViaAPI api = new SpongeViaAPI();
private SpongeConfigAPI conf;
private Logger logger;
@Listener
@ -51,6 +61,7 @@ public class SpongePlugin implements ViaPlatform {
// Setup Logger
logger = new LoggerWrapper(container.getLogger());
// Setup Plugin
conf = new SpongeConfigAPI(defaultConfig.getParentFile());
syncExecutor = game.getScheduler().createSyncExecutor(this);
asyncExecutor = game.getScheduler().createAsyncExecutor(this);
SpongeCommandHandler commandHandler = new SpongeCommandHandler();

View File

@ -1,64 +1,70 @@
package us.myles.ViaVersion.sponge;
import us.myles.ViaVersion.SpongePlugin;
import org.yaml.snakeyaml.Yaml;
import us.myles.ViaVersion.api.ViaVersionConfig;
import us.myles.ViaVersion.api.configuration.ConfigurationProvider;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.*;
import java.net.URL;
import java.util.*;
public class SpongeConfigAPI implements ViaVersionConfig, ConfigurationProvider{
private final SpongePlugin spongePlugin;
public class SpongeConfigAPI implements ViaVersionConfig, ConfigurationProvider {
private final File defaultConfig;
private Map<Object, Object> config;
private ThreadLocal<Yaml> yaml = new ThreadLocal<Yaml>() {
@Override
protected Yaml initialValue() {
return new Yaml();
}
};
public SpongeConfigAPI(SpongePlugin spongePlugin) {
this.spongePlugin = spongePlugin;
public SpongeConfigAPI(File defaultConfig) {
this.defaultConfig = defaultConfig;
reloadConfig();
}
@Override
public boolean isCheckForUpdates() {
return false;
return getBoolean("checkforupdates", true);
}
@Override
public boolean isPreventCollision() {
return false;
return getBoolean("prevent-collision", true);
}
@Override
public boolean isNewEffectIndicator() {
return false;
return getBoolean("use-new-effect-indicator", true);
}
@Override
public boolean isShowNewDeathMessages() {
return false;
return getBoolean("use-new-deathmessages", false);
}
@Override
public boolean isSuppressMetadataErrors() {
return false;
return getBoolean("suppress-metadata-errors", false);
}
@Override
public boolean isShieldBlocking() {
return false;
return getBoolean("shield-blocking", true);
}
@Override
public boolean isHologramPatch() {
return false;
return getBoolean("hologram-patch", false);
}
@Override
public boolean isBossbarPatch() {
return false;
return getBoolean("bossbar-patch", true);
}
@Override
public boolean isBossbarAntiflicker() {
return false;
return getBoolean("bossbar-anti-flicker", false);
}
@Override
@ -68,12 +74,7 @@ public class SpongeConfigAPI implements ViaVersionConfig, ConfigurationProvider{
@Override
public double getHologramYOffset() {
return 0;
}
@Override
public boolean isAutoTeam() {
return false;
return getDouble("hologram-y", -0.96D);
}
@Override
@ -83,101 +84,194 @@ public class SpongeConfigAPI implements ViaVersionConfig, ConfigurationProvider{
@Override
public int getMaxPPS() {
return 0;
return getInt("max-pps", 140);
}
@Override
public String getMaxPPSKickMessage() {
return null;
return getString("max-pps-kick-msg", "Sending packets too fast? lag?");
}
@Override
public int getTrackingPeriod() {
return 0;
return getInt("tracking-period", 6);
}
@Override
public int getWarningPPS() {
return 0;
return getInt("tracking-warning-pps", 120);
}
@Override
public int getMaxWarnings() {
return 0;
return getInt("tracking-max-warnings", 3);
}
@Override
public String getMaxWarningsKickMessage() {
return null;
return getString("tracking-max-kick-msg", "You are sending too many packets, :(");
}
@Override
public boolean isAntiXRay() {
return false;
return getBoolean("anti-xray-patch", true);
}
@Override
public boolean isSendSupportedVersions() {
return false;
return getBoolean("send-supported-versions", false);
}
@Override
public boolean isStimulatePlayerTick() {
return false;
return getBoolean("simulate-pt", true);
}
@Override
public boolean isItemCache() {
return false;
return getBoolean("item-cache", true);
}
@Override
public boolean isNMSPlayerTicking() {
return false;
return getBoolean("nms-player-ticking", true);
}
@Override
public boolean isReplacePistons() {
return false;
return getBoolean("replace-pistons", false);
}
@Override
public int getPistonReplacementId() {
return 0;
return getInt("replacement-piston-id", 0);
}
public boolean isAutoTeam() {
// Collision has to be enabled first
return isPreventCollision() && getBoolean("auto-team", true);
}
@Override
public boolean isForceJsonTransform() {
return false;
return getBoolean("force-json-transform", false);
}
@Override
public List<Integer> getBlockedProtocols() {
return Arrays.asList(0);
return getIntegerList("block-protocols");
}
@Override
public String getBlockedDisconnectMsg() {
return "Boop";
return getString("block-disconnect-msg", "You are using an unsupported Minecraft version!");
}
@Override
public String getReloadDisconnectMsg() {
return "Beep";
return getString("reload-disconnect-msg", "Server reload, please rejoin!");
}
@Override
public void set(String path, Object value) {
config.put(path, value);
}
@Override
public void saveConfig() {
if (!defaultConfig.isDirectory()) {
defaultConfig.mkdir();
}
File config = new File(defaultConfig, "config.yml");
try (FileWriter fw = new FileWriter(config)) {
yaml.get().dump(this.config, fw);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void reloadConfig() {
if (!defaultConfig.isDirectory()) {
defaultConfig.mkdir();
}
File config = new File(defaultConfig, "config.yml");
URL jarConfigFile = this.getClass().getClassLoader().getResource("config.yml");
this.config = null;
if (config.exists()) {
try (FileInputStream input = new FileInputStream(config)) {
this.config = (Map<Object, Object>) yaml.get().load(input);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
if (this.config == null) {
this.config = new HashMap<>();
}
Map<Object, Object> defaults;
try (InputStream stream = jarConfigFile.openStream()) {
defaults = (Map<Object, Object>) yaml.get().load(stream);
} catch (IOException e) {
e.printStackTrace();
return;
}
// Merge with defaultLoader
for (Object key : this.config.keySet()) {
// Set option in new conf if exists
if (defaults.containsKey(key)) {
defaults.put(key, this.config.get(key));
}
}
this.config = defaults;
// Save
saveConfig();
}
public boolean getBoolean(String key, boolean def) {
if (this.config.containsKey(key)) {
return (boolean) this.config.get(key);
} else {
return def;
}
}
public String getString(String key, String def) {
if (this.config.containsKey(key)) {
return (String) this.config.get(key);
} else {
return def;
}
}
public int getInt(String key, int def) {
if (this.config.containsKey(key)) {
return (int) this.config.get(key);
} else {
return def;
}
}
public double getDouble(String key, double def) {
if (this.config.containsKey(key)) {
return (double) this.config.get(key);
} else {
return def;
}
}
public List<Integer> getIntegerList(String key) {
if (this.config.containsKey(key)) {
return (List<Integer>) this.config.get(key);
} else {
return new ArrayList<>();
}
}
@Override
public Map<String, Object> getValues() {
return new HashMap<>();
return (Map<String, Object>) ((Map) this.config);
}
}