Added Config comment capability.

See the challenges addon for examples.
This commit is contained in:
Tastybento 2018-02-25 22:44:22 -08:00
parent 1315dd990c
commit bdca10636e
6 changed files with 107 additions and 21 deletions

View File

@ -0,0 +1,23 @@
/**
*
*/
package us.tastybento.bskyblock.api.configuration;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Retention(RUNTIME)
@Target({ FIELD, METHOD })
/**
* @author tastybento
*
*/
public @interface ConfigComment {
String value();
}

View File

@ -1,6 +1,7 @@
package us.tastybento.bskyblock.database;
import java.sql.Connection;
import java.util.Map;
import org.bukkit.configuration.file.YamlConfiguration;
@ -16,21 +17,21 @@ public interface DatabaseConnecter {
*
* @return A new connection to the database using the settings provided
*/
public Connection createConnection();
Connection createConnection();
/**
* Returns the connection url
*
* @return the connector's URL
*/
public String getConnectionUrl();
String getConnectionUrl();
/**
* Looks through the database (or files) and returns a known unique key
* @param tableName - name of the table
* @return a unique key for this record
*/
public String getUniqueId(String tableName);
String getUniqueId(String tableName);
/**
* Check if a key exists in the database in this table or not
@ -38,7 +39,7 @@ public interface DatabaseConnecter {
* @param key - key to check
* @return true if it exists
*/
public boolean uniqueIdExists(String tableName, String key);
boolean uniqueIdExists(String tableName, String key);
/**
* Loads a YAML file. Used by the flat file database
@ -46,15 +47,16 @@ public interface DatabaseConnecter {
* @param fileName - the filename
* @return Yaml Configuration
*/
public YamlConfiguration loadYamlFile(String tableName, String fileName);
YamlConfiguration loadYamlFile(String tableName, String fileName);
/**
* Save the Yaml Config
* @param yamlFile - the YAML config
* @param tableName - analogous to a table in a database
* @param fileName - the name of the record. Must be unique.
* @param commentMap - map of comments, may be empty
*/
public void saveYamlFile(YamlConfiguration yamlFile, String tableName, String fileName);
void saveYamlFile(YamlConfiguration yamlConfig, String tableName, String fileName, Map<String, String> commentMap);
}

View File

@ -1,7 +1,15 @@
package us.tastybento.bskyblock.database.flatfile;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.UUID;
import org.bukkit.Bukkit;
@ -76,7 +84,7 @@ public class FlatFileDatabaseConnecter implements DatabaseConnecter {
* @see us.tastybento.bskyblock.database.DatabaseConnecter#saveYamlFile(org.bukkit.configuration.file.YamlConfiguration, java.lang.String, java.lang.String)
*/
@Override
public void saveYamlFile(YamlConfiguration yamlConfig, String tableName, String fileName) {
public void saveYamlFile(YamlConfiguration yamlConfig, String tableName, String fileName, Map<String, String> commentMap) {
if (!fileName.endsWith(".yml")) {
fileName = fileName + ".yml";
}
@ -89,7 +97,47 @@ public class FlatFileDatabaseConnecter implements DatabaseConnecter {
yamlConfig.save(file);
} catch (Exception e) {
Bukkit.getLogger().severe("Could not save yaml file to database " + tableName + " " + fileName + " " + e.getMessage());
return;
}
if (commentMap != null && !commentMap.isEmpty()) {
commentFile(file, commentMap);
}
}
/**
* Adds comments to a YAML file
* @param file
* @param commentMap
*/
private void commentFile(File file, Map<String, String> commentMap) {
// Run through the file and add in the comments
File commentedFile = new File(file.getPath() + ".tmp");
List<String> newFile = new ArrayList<>();
try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
String nextLine = scanner.nextLine();
// See if there are any comments in this line
for (Entry<String, String> e : commentMap.entrySet()) {
if (nextLine.contains(e.getKey())) {
// We want the comment to start at the same level as the entry
StringBuilder commentLine = new StringBuilder();
for (int i = 0; i < nextLine.indexOf(e.getKey()); i++){
commentLine.append(' ');
}
commentLine.append(e.getValue());
nextLine = commentLine.toString();
break;
}
}
newFile.add(nextLine);
}
Files.write(commentedFile.toPath(), (Iterable<String>)newFile.stream()::iterator);
Files.move(commentedFile.toPath(), file.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
/* (non-Javadoc)

View File

@ -28,6 +28,7 @@ import org.bukkit.plugin.Plugin;
import us.tastybento.bskyblock.Constants;
import us.tastybento.bskyblock.Constants.GameType;
import us.tastybento.bskyblock.api.configuration.ConfigComment;
import us.tastybento.bskyblock.api.configuration.ConfigEntry;
import us.tastybento.bskyblock.api.configuration.StoreAt;
import us.tastybento.bskyblock.database.DatabaseConnecter;
@ -301,6 +302,8 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
// The file name of the Yaml file.
String filename = "";
String path = DATABASE_FOLDER_NAME + File.separator + dataObject.getSimpleName();
// Comments for the file
Map<String, String> yamlComments = new HashMap<>();
// Only allow storing in an arbitrary place if it is a config object. Otherwise it is in the database
if (configFlag) {
@ -342,7 +345,19 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
// TODO: add in game-specific saving
}
// Comments
ConfigComment comment = field.getAnnotation(ConfigComment.class);
if (comment != null) {
// Create a random placeholder string
String random = "comment-" + UUID.randomUUID().toString();
// Store placeholder
config.set(random, " ");
// Create comment
yamlComments.put(random, "# " + comment.value());
}
// Adapter
Adapter adapterNotation = field.getAnnotation(Adapter.class);
if (adapterNotation != null && AdapterInterface.class.isAssignableFrom(adapterNotation.value())) {
if (DEBUG) {
@ -357,13 +372,11 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
// We are done here
continue fields;
}
plugin.getLogger().info("DEBUG: property desc = " + propertyDescriptor.getPropertyType().getTypeName());
// Depending on the vale type, it'll need serializing differently
// Check if this field is the mandatory UniqueId field. This is used to identify this instantiation of the class
if (method.getName().equals("getUniqueId")) {
// If the object does not have a unique name assigned to it already, one is created at random
plugin.getLogger().info("DEBUG: flat file db uniqueId = " + value);
String id = (String)value;
if (value == null || id.isEmpty()) {
id = databaseConnecter.getUniqueId(dataObject.getSimpleName());
@ -407,10 +420,11 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
if (filename.isEmpty()) {
throw new IllegalArgumentException("No uniqueId in class");
}
if (DEBUG) {
plugin.getLogger().info("DEBUG: Saving YAML file : " + path + " " + filename);
}
databaseConnecter.saveYamlFile(config, path, filename);
databaseConnecter.saveYamlFile(config, path, filename, yamlComments);
}
/**

View File

@ -3,6 +3,7 @@ package us.tastybento.bskyblock.database.mysql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
@ -59,15 +60,16 @@ public class MySQLDatabaseConnecter implements DatabaseConnecter {
}
@Override
public void saveYamlFile(YamlConfiguration yamlFile, String tableName, String fileName) {
// Not used
}
@Override
public boolean uniqueIdExists(String simpleName, String key) {
public boolean uniqueIdExists(String tableName, String key) {
// Not used
return false;
}
@Override
public void saveYamlFile(YamlConfiguration yamlConfig, String tableName, String fileName,
Map<String, String> commentMap) {
// Not used
}
}

View File

@ -4,7 +4,6 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.bukkit.Bukkit;
import org.bukkit.configuration.MemorySection;
import us.tastybento.bskyblock.BSkyBlock;
@ -30,8 +29,6 @@ public class FlagSerializer implements AdapterInterface<Map<Flag, Integer>, Map<
if (object instanceof MemorySection) {
MemorySection section = (MemorySection) object;
for (String key : section.getKeys(false)) {
Bukkit.getLogger().info("DEBUG: " + key + " = " + section.getInt(key));
result.put(BSkyBlock.getInstance().getFlagsManager().getFlagByID(key), section.getInt(key));
}
} else {