Use annotations in config system

This commit is contained in:
Acrobot 2013-04-23 20:09:23 +02:00
parent 0c1e287b8a
commit 688d146732
6 changed files with 83 additions and 67 deletions

View File

@ -1,7 +1,9 @@
package com.Acrobot.Breeze.Configuration;
package com.Acrobot.Breeze.Configuration.Annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Annotation for a configuration value
@ -9,6 +11,7 @@ import java.lang.annotation.RetentionPolicy;
* @author Acrobot
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface ConfigurationComment {
/**
* This option's comment

View File

@ -0,0 +1,15 @@
package com.Acrobot.Breeze.Configuration.Annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Annotation indicating that the value should be followed by a blank space
* @author Acrobot
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface PrecededBySpace {
}

View File

@ -1,15 +1,14 @@
package com.Acrobot.Breeze.Configuration;
import com.Acrobot.Breeze.Configuration.Annotations.PrecededBySpace;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Scanner;
/**
* A class which can be used to make configs easier to load
@ -29,6 +28,10 @@ public class Configuration {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));
if (!endsWithSpace(file)) {
writer.newLine();
}
for (Field field : clazz.getDeclaredFields()) {
if (!Modifier.isStatic(field.getModifiers()) || Modifier.isTransient(field.getModifiers()) || !Modifier.isPublic(field.getModifiers())) {
continue;
@ -37,25 +40,20 @@ public class Configuration {
String path = field.getName();
try {
if (path.toLowerCase().replace("_", "").startsWith("newline")) {
continue;
}
if (config.isSet(path)) {
field.set(null, ValueParser.parseToJava(config.get(path)));
} else {
writer.write('\n' + FieldParser.parse(field));
if (clazz.getDeclaredField("NEWLINE_" + path) != null) {
writer.write('\n');
if (field.isAnnotationPresent(PrecededBySpace.class)) {
writer.newLine();
}
writer.write(FieldParser.parse(field));
writer.newLine();
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
continue;
}
}
@ -66,6 +64,27 @@ public class Configuration {
}
}
/**
* Checks if the file ends with space
* @param file File to check
* @return If the file ends with space
*/
public static boolean endsWithSpace(File file) {
try {
Scanner scanner = new Scanner(file);
String lastLine = "";
while (scanner.hasNextLine()) {
lastLine = scanner.nextLine();
}
return lastLine.isEmpty();
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
}
}
/**
* Converts a java value to config-compatible value
*

View File

@ -1,5 +1,7 @@
package com.Acrobot.Breeze.Configuration;
import com.Acrobot.Breeze.Configuration.Annotations.ConfigurationComment;
import java.lang.reflect.Field;
/**

View File

@ -1,5 +1,6 @@
package com.Acrobot.ChestShop.Configuration;
import com.Acrobot.Breeze.Configuration.Annotations.PrecededBySpace;
import org.bukkit.ChatColor;
/**
@ -9,72 +10,59 @@ public class Messages {
public static String prefix = ChatColor.GREEN + "[Shop] " + ChatColor.RESET;
public static String iteminfo = ChatColor.GREEN + "Item Information: " + ChatColor.RESET;
public static byte NEWLINE_iteminfo; ///////////////////////////////////////////////////
@PrecededBySpace
public static String ACCESS_DENIED = "You don't have permission to do that!";
public static byte NEWLINE_ACCESS_DENIED; ///////////////////////////////////////////////////
@PrecededBySpace
public static String NOT_ENOUGH_MONEY = "You don't have enough money!";
public static String NOT_ENOUGH_MONEY_SHOP = "Shop owner doesn't have enough money!";
public static byte NEWLINE_NOT_ENOUGH_MONEY_SHOP; ///////////////////////////////////////////////////
@PrecededBySpace
public static String CLIENT_DEPOSIT_FAILED = "Money deposit to your account failed!";
public static String SHOP_DEPOSIT_FAILED = "Money deposit to shop owner failed!";
public static byte NEWLINE_SHOP_DEPOSIT_FAILED; ///////////////////////////////////////////////////
@PrecededBySpace
public static String NO_BUYING_HERE = "You can't buy here!";
public static String NO_SELLING_HERE = "You can't sell here!";
public static byte NEWLINE_NO_SELLING_HERE; ///////////////////////////////////////////////////
@PrecededBySpace
public static String NOT_ENOUGH_SPACE_IN_INVENTORY = "You haven't got enough space in inventory!";
public static String NOT_ENOUGH_SPACE_IN_CHEST = "There isn't enough space in chest!";
public static String NOT_ENOUGH_ITEMS_TO_SELL = "You don't have enough items to sell!";
public static byte NEWLINE_NOT_ENOUGH_ITEMS_TO_SELL; ///////////////////////////////////////////////////
@PrecededBySpace
public static String NOT_ENOUGH_STOCK = "This shop is out of stock.";
public static String NOT_ENOUGH_STOCK_IN_YOUR_SHOP = "Your %material shop is out of stock!";
public static byte NEWLINE_ENOUGH_STOCK_IN_YOUR_SHOP; ///////////////////////////////////////////////////
@PrecededBySpace
public static String YOU_BOUGHT_FROM_SHOP = "You bought %item from %owner for %price.";
public static String SOMEBODY_BOUGHT_FROM_YOUR_SHOP = "%buyer bought %item for %price from you.";
public static byte NEWLINE_SOMEBODY_BOUGHT_FROM_YOUR_SHOP; ///////////////////////////////////////////////////
@PrecededBySpace
public static String YOU_SOLD_TO_SHOP = "You sold %item to %buyer for %price.";
public static String SOMEBODY_SOLD_TO_YOUR_SHOP = "%seller sold %item for %price to you.";
public static byte NEWLINE_SOMEBODY_SOLD_TO_YOUR_SHOP; ///////////////////////////////////////////////////
@PrecededBySpace
public static String YOU_CANNOT_CREATE_SHOP = "You can't create this type of shop!";
public static String NO_CHEST_DETECTED = "Couldn't find a chest!";
public static String INVALID_SHOP_DETECTED = "The shop cannot be used!";
public static String CANNOT_ACCESS_THE_CHEST = "You don't have permissions to access this chest!";
public static byte NEWLINE_CANNOT_ACCESS_THE_CHEST; ///////////////////////////////////////////////////
@PrecededBySpace
public static String PROTECTED_SHOP = "Successfully protected the shop with LWC!";
public static String SHOP_CREATED = "Shop successfully created!";
public static String SHOP_FEE_PAID = "You have been charged %amount";
public static String SHOP_REFUNDED = "You have been refunded %amount.";
public static byte NEWLINE_SHOP_REFUNDED; ///////////////////////////////////////////////////
@PrecededBySpace
public static String RESTRICTED_SIGN_CREATED = "Sign successfully created!";
public static byte NEWLINE_RESTRICTED_SIGN_CREATED; ///////////////////////////////////////////////////
@PrecededBySpace
public static String NO_PERMISSION = "You don't have permissions to do that!";
public static String INCORRECT_ITEM_ID = "You have specified invalid item id!";
public static String NOT_ENOUGH_PROTECTIONS = "Could not create a protection!";
public static byte NEWLINE_NOT_ENOUGH_PROTECTIONS; ///////////////////////////////////////////////////
@PrecededBySpace
public static String CANNOT_CREATE_SHOP_HERE = "You can't create shop here!";
public static String prefix(String message) {

View File

@ -1,6 +1,7 @@
package com.Acrobot.ChestShop.Configuration;
import com.Acrobot.Breeze.Configuration.ConfigurationComment;
import com.Acrobot.Breeze.Configuration.Annotations.ConfigurationComment;
import com.Acrobot.Breeze.Configuration.Annotations.PrecededBySpace;
/**
* @author Acrobot
@ -9,8 +10,7 @@ public class Properties {
@ConfigurationComment("(When not using Vault) Preferred economy plugin to use (iConomy/BOSEconomy/Essentials).")
public static String PREFERRED_ECONOMY_PLUGIN = "";
public static byte NEWLINE_PREFERRED_ECONOMY_PLUGIN; ///////////////////////////////////////////////////
@PrecededBySpace
@ConfigurationComment("(In 1/1000th of a second) How often can a player use the shop sign?")
public static int SHOP_INTERACTION_INTERVAL = 250;
@ -32,16 +32,14 @@ public class Properties {
@ConfigurationComment("If true, when you left-click your own shop sign you won't open chest's inventory, but instead you will start destroying the sign.")
public static boolean ALLOW_LEFT_CLICK_DESTROYING = true;
public static byte NEWLINE_ALLOW_LEFT_CLICK_DESTROYING; ///////////////////////////////////////////////////
@PrecededBySpace
@ConfigurationComment("If true, if the shop is empty, the sign is destroyed and put into the chest, so the shop isn't usable anymore.")
public static boolean REMOVE_EMPTY_SHOPS = false;
@ConfigurationComment("If true, if the REMOVE_EMPTY_SHOPS option is turned on, the chest is also destroyed.")
public static boolean REMOVE_EMPTY_CHESTS = false;
public static byte NEWLINE_REMOVE_EMPTY_CHESTS; ///////////////////////////////////////////////////
@PrecededBySpace
@ConfigurationComment("First line of your Admin Shop's sign should look like this:")
public static String ADMIN_SHOP_NAME = "Admin Shop";
@ -54,7 +52,7 @@ public class Properties {
@ConfigurationComment("Percent of the price that should go to the server's account. (100 = 100 percent)")
public static int TAX_AMOUNT = 0;
@ConfigurationComment("Percent of the price that should go to the server's account when buying from a Bank.")
@ConfigurationComment("Percent of the price that should go to the server's account when buying from a bank.")
public static int BANK_TAX_AMOUNT = 0;
@ConfigurationComment("Percent of the price that should go to the server's account when buying from an Admin Shop.")
@ -66,21 +64,18 @@ public class Properties {
@ConfigurationComment("How much money do you get back when destroying a sign?")
public static double SHOP_REFUND_PRICE = 0;
public static byte NEWLINE_SHOP_REFUND_PRICE; ///////////////////////////////////////////////////
@PrecededBySpace
@ConfigurationComment("Should we block shops that sell things for more than they buy? (This prevents newbies from creating shops that would be exploited)")
public static boolean BLOCK_SHOPS_WITH_SELL_PRICE_HIGHER_THAN_BUY_PRICE = true;
public static byte NEWLINE_BLOCK_SHOPS_WITH_SELL_PRICE_HIGHER_THAN_BUY_PRICE; ///////////////////////////////////////////////////
@PrecededBySpace
@ConfigurationComment("Do you want to allow other players to build a shop on a block where there's one already?")
public static boolean ALLOW_MULTIPLE_SHOPS_AT_ONE_BLOCK = false;
@ConfigurationComment("Can shops be used even when the seller doesn't have enough items? (The price will be scaled adequatly to the item amount)")
public static boolean ALLOW_PARTIAL_TRANSACTIONS = true;
public static byte NEWLINE_ALLOW_PARTIAL_TRANSACTIONS; ///////////////////////////////////////////////////
@PrecededBySpace
@ConfigurationComment("Do you want to show \"Out of stock\" messages?")
public static boolean SHOW_MESSAGE_OUT_OF_STOCK = true;
@ -90,8 +85,7 @@ public class Properties {
@ConfigurationComment("Do you want to show \"Somebody bought/sold... \" messages?")
public static boolean SHOW_TRANSACTION_INFORMATION_OWNER = true;
public static byte NEWLINE_SHOW_TRANSACTION_INFORMATION_OWNER; ///////////////////////////////////////////////////
@PrecededBySpace
@ConfigurationComment("If true, plugin will log transactions in its own file")
public static boolean LOG_TO_FILE = false;
@ -104,8 +98,7 @@ public class Properties {
@ConfigurationComment("How long should transaction information be stored in the database (in seconds, -1 means forever)?")
public static int RECORD_TIME_TO_LIVE = 600;
public static byte NEWLINE_RECORD_TIME_TO_LIV; ///////////////////////////////////////////////////
@PrecededBySpace
@ConfigurationComment("Do you want to stack all items up to 64 item stacks?")
public static boolean STACK_TO_64 = false;
@ -127,8 +120,7 @@ public class Properties {
@ConfigurationComment("Do you want to protect shop signs with LWC?")
public static boolean PROTECT_SIGN_WITH_LWC = false;
public static byte NEWLINE_PROTECT_SIGN_WITH_LWC; ///////////////////////////////////////////////////
@PrecededBySpace
@ConfigurationComment("If true, plugin will generate shop statistics webpage.")
public static boolean GENERATE_STATISTICS_PAGE = false;
@ -138,16 +130,14 @@ public class Properties {
@ConfigurationComment("How often should the website be generated?")
public static long STATISTICS_PAGE_GENERATION_INTERVAL = 60;
public static byte NEWLINE_STATISTICS_PAGE_GENERATION_INTERVAL; ///////////////////////////////////////////////////
@PrecededBySpace
@ConfigurationComment("Do you want to only let people build inside shop plots?")
public static boolean TOWNY_INTEGRATION = false;
@ConfigurationComment("If true, only plot owners are able to build inside a shop plot. If false, every town's resident is able to build there.")
public static boolean TOWNY_SHOPS_FOR_OWNERS_ONLY = true;
public static byte NEWLINE_TOWNY_SHOPS_FOR_OWNERS_ONLY; ///////////////////////////////////////////////////
@PrecededBySpace
@ConfigurationComment("Do you want to only let people build inside regions?")
public static boolean WORLDGUARD_INTEGRATION = false;
@ -157,8 +147,7 @@ public class Properties {
@ConfigurationComment("Do you want ChestShop to respect WorldGuard's chest protection?")
public static boolean WORLDGUARD_USE_PROTECTION = false;
public static byte NEWLINE_WORLDGUARD_USE_PROTECTION; ///////////////////////////////////////////////////
@PrecededBySpace
@ConfigurationComment("How much Heroes exp should people get for creating a ChestShop?")
public static double HEROES_EXP = 100;
}