diff --git a/src/build.xml b/src/build.xml
index 9d5f366..e9a1600 100644
--- a/src/build.xml
+++ b/src/build.xml
@@ -2,16 +2,32 @@
-
+
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
+
diff --git a/src/locale/UpkeepCosts.properties b/src/locale/UpkeepCosts.properties
new file mode 100644
index 0000000..8e669c7
--- /dev/null
+++ b/src/locale/UpkeepCosts.properties
@@ -0,0 +1,40 @@
+mod_name=Upkeep costs
+fxml_missing=UpkeepCosts.fxml not found.
+
+# 0 - property name
+negative={0} cannot be negative. Value will not be changed.
+invalid=Invalid value for %s. Value will not be changed.
+
+no_upkeep=Upkeep is not enabled on this server, upkeep costs will have no effect.
+free_deeds=Deeds are free on this server, costs will have no effect.
+# 0 - tile cost, 1 - tile upkeep, 2 - perimeter cost, 3 - perimeter upkeep, 4 - guards cost, 5 - guards upkeep, 6 - minimum upkeep
+all_values=Upkeep costs are as follows: Tile {0}, {1} - Perimeter {2}, {3} - Guards {4}, {5} - Minimum {6}
+
+load_properties_error=An error occurred when loading properties:
+save_properties_error=An error occurred when saving properties:
+error=An error occurred, the following may or may not help:
+
+changes_title=Save Changes
+changes_header=Some changes have been made
+changes_message=Would you like to save the changes?
+
+irons_note=All values are in irons. 100i = 1c, 100c = 1s, 100s = 1g
+
+tile_cost=Tile cost
+tile_cost_description=The amount charged per tile when founding a deed.
+tile_upkeep=Tile upkeep
+tile_upkeep_description=The amount charged a month per tile.
+perimeter_cost=Perimeter cost
+perimeter_cost_description=The amount charged per perimeter tile when founding a deed.
+perimeter_upkeep=Perimeter upkeep
+perimeter_upkeep_description=The amount charged a month per perimeter tile.
+normal_guard_cost=Guard cost (Normal)
+normal_guard_cost_description=The amount charged per guard when founding a deed except for Epic or Challenge type servers.
+normal_guard_upkeep=Guard upkeep (Normal)
+normal_guard_upkeep_description=The amount charged a month for each guard except on Epic or Challenge type servers.
+epic_guard_cost=Guard cost (Epic)
+epic_guard_cost_description=The amount charged per guard when founding a deed on Epic and Challenge type server.
+epic_guard_upkeep=Guard upkeep (Epic)
+epic_guard_upkeep_description=The amount charged a month for each guard on Epic or Challenge type servers.
+minimum_upkeep=Minimum upkeep
+minimum_upkeep_description=The minimum charge per month.
diff --git a/src/mod/wurmonline/mods/upkeepcosts/UpkeepCosts.fxml b/src/mod/wurmonline/mods/upkeepcosts/UpkeepCosts.fxml
new file mode 100644
index 0000000..5191fad
--- /dev/null
+++ b/src/mod/wurmonline/mods/upkeepcosts/UpkeepCosts.fxml
@@ -0,0 +1,18 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/mod/wurmonline/mods/upkeepcosts/UpkeepCosts.java b/src/mod/wurmonline/mods/upkeepcosts/UpkeepCosts.java
index 44f477e..e2cc9b7 100644
--- a/src/mod/wurmonline/mods/upkeepcosts/UpkeepCosts.java
+++ b/src/mod/wurmonline/mods/upkeepcosts/UpkeepCosts.java
@@ -13,21 +13,23 @@
import java.util.logging.Logger;
public class UpkeepCosts implements WurmMod, Configurable, ServerStartedListener {
- private static final Logger logger = Logger.getLogger(UpkeepCosts.class.getName());
- Long tile_upkeep;
- Long tile_cost;
- Long perimeter_cost;
- Long perimeter_upkeep;
- Long normal_guard_cost;
- Long normal_guard_upkeep;
- Long epic_guard_cost;
- Long epic_guard_upkeep;
- Long minimum_upkeep;
+ protected static final Logger logger = Logger.getLogger(UpkeepCosts.class.getName());
+ protected String NO_UPKEEP = "Upkeep is not enabled on this server, upkeep costs will have no effect.";
+ protected String FREE_DEEDS = "Deeds are free on this server, costs will have no effect.";
+ public Long tile_upkeep;
+ public Long tile_cost;
+ public Long perimeter_cost;
+ public Long perimeter_upkeep;
+ public Long normal_guard_cost;
+ public Long normal_guard_upkeep;
+ public Long epic_guard_cost;
+ public Long epic_guard_upkeep;
+ public Long minimum_upkeep;
@Override
public void configure(Properties properties) {
- for (Field field : this.getClass().getDeclaredFields()) {
- if (field.getName().equals("logger")) {
+ for (Field field : this.getClass().getFields()) {
+ if (!(field.getType().isAssignableFrom(Long.class))) {
continue;
}
try {
@@ -49,10 +51,10 @@ public void configure(Properties properties) {
public void onServerStarted() {
ServerEntry local = Servers.localServer;
if (!local.isUpkeep()) {
- logger.info("Upkeep is not enabled on this server, upkeep costs will have no effect.");
+ logger.info(NO_UPKEEP);
}
- if (!local.isFreeDeeds()) {
- logger.info("Deeds are free on this server, costs will have no effect.");
+ if (local.isFreeDeeds()) {
+ logger.info(FREE_DEEDS);
}
if (tile_cost != null) {
Villages.TILE_COST = tile_cost;
@@ -98,14 +100,7 @@ public void onServerStarted() {
Villages.MINIMUM_UPKEEP_STRING = (new Change(Villages.MINIMUM_UPKEEP)).getChangeString();
}
- logger.info(String.format("Upkeep costs are as follows: Tile %s, %s - Perimeter %s, %s - Guards %s, %s - Minimum %s",
- Villages.TILE_COST_STRING,
- Villages.TILE_UPKEEP_STRING,
- Villages.PERIMETER_COST_STRING,
- Villages.PERIMETER_UPKEEP_STRING,
- Villages.GUARD_COST_STRING,
- Villages.GUARD_UPKEEP_STRING,
- Villages.MINIMUM_UPKEEP_STRING));
+ logValues();
}
void negative (String property) {
@@ -115,4 +110,15 @@ void negative (String property) {
void invalid (String property) {
logger.warning(String.format("Invalid value for %s. Value will not be changed.", property));
}
+
+ void logValues () {
+ logger.info(String.format("Upkeep costs are as follows: Tile %s, %s - Perimeter %s, %s - Guards %s, %s - Minimum %s",
+ Villages.TILE_COST_STRING,
+ Villages.TILE_UPKEEP_STRING,
+ Villages.PERIMETER_COST_STRING,
+ Villages.PERIMETER_UPKEEP_STRING,
+ Villages.GUARD_COST_STRING,
+ Villages.GUARD_UPKEEP_STRING,
+ Villages.MINIMUM_UPKEEP_STRING));
+ }
}
diff --git a/src/mod/wurmonline/mods/upkeepcosts/UpkeepCostsUI.java b/src/mod/wurmonline/mods/upkeepcosts/UpkeepCostsUI.java
new file mode 100644
index 0000000..76982fc
--- /dev/null
+++ b/src/mod/wurmonline/mods/upkeepcosts/UpkeepCostsUI.java
@@ -0,0 +1,171 @@
+package mod.wurmonline.mods.upkeepcosts;
+
+import com.ibm.icu.text.MessageFormat;
+import com.wurmonline.server.villages.Villages;
+import javafx.fxml.FXML;
+import javafx.fxml.FXMLLoader;
+import javafx.scene.control.ButtonBar;
+import javafx.scene.control.ButtonType;
+import javafx.scene.control.Label;
+import javafx.scene.control.ScrollPane;
+import javafx.scene.layout.Pane;
+import javafx.scene.layout.Region;
+import mod.wurmonline.serverlauncher.LocaleHelper;
+import mod.wurmonline.serverlauncher.gui.ServerGuiController;
+import org.gotti.wurmunlimited.modloader.interfaces.WurmUIMod;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.lang.reflect.Field;
+import java.nio.file.Paths;
+import java.util.Properties;
+import java.util.ResourceBundle;
+import java.util.logging.Level;
+
+public class UpkeepCostsUI extends UpkeepCosts implements WurmUIMod {
+ ServerGuiController controller;
+ UpkeepPropertySheet upkeepPropertySheet;
+ ResourceBundle messages = LocaleHelper.getBundle("UpkeepCosts");
+
+ public UpkeepCostsUI () {
+ NO_UPKEEP = messages.getString("no_upkeep");
+ FREE_DEEDS = messages.getString("free_deeds");
+ }
+
+ @Override
+ public Region getRegion(ServerGuiController guiController) {
+ controller = guiController;
+ try {
+ FXMLLoader fx = new FXMLLoader(UpkeepCostsUI.class.getResource("UpkeepCosts.fxml"), LocaleHelper.getBundle("UIWindow"));
+ fx.setClassLoader(this.getClass().getClassLoader());
+ fx.setControllerFactory(param -> this);
+ return fx.load();
+ } catch (IOException ex) {
+ logger.log(Level.SEVERE, ex.getMessage(), ex);
+ }
+ Label label = new Label(messages.getString("fxml_missing"));
+ Pane pane = new Pane();
+ pane.getChildren().add(label);
+ return pane;
+ }
+
+ @Override
+ public String getName() {
+ return messages.getString("mod_name");
+ }
+
+ void negative (String property) {
+ logger.warning(MessageFormat.format(messages.getString("negative"), property));
+ }
+
+ void invalid (String property) {
+ logger.warning(MessageFormat.format(messages.getString("invalid"), property));
+ }
+
+ void logValues () {
+ logger.info(MessageFormat.format(messages.getString("all_values"),
+ Villages.TILE_COST_STRING,
+ Villages.TILE_UPKEEP_STRING,
+ Villages.PERIMETER_COST_STRING,
+ Villages.PERIMETER_UPKEEP_STRING,
+ Villages.GUARD_COST_STRING,
+ Villages.GUARD_UPKEEP_STRING,
+ Villages.MINIMUM_UPKEEP_STRING));
+ }
+
+ File getFile () {
+ return new File(Paths.get(controller.getCurrentDir(), "mods", "upkeepcosts", "upkeepcosts.properties").toUri());
+ }
+
+ @FXML
+ ScrollPane container;
+
+ @FXML
+ void saveUpkeep () {
+ if (upkeepPropertySheet.haveChanges()) {
+ File file = getFile();
+ Properties properties = new Properties();
+
+ try {
+ boolean created = file.createNewFile();
+ if (!created) {
+ FileInputStream stream = new FileInputStream(file.toString());
+ properties.load(stream);
+ }
+ } catch (IOException ex) {
+ logger.warning(messages.getString("load_properties_error"));
+ ex.printStackTrace();
+ }
+ for (Field field : this.getClass().getFields()) {
+ if (!(field.getType().isAssignableFrom(Long.class))) {
+ continue;
+ }
+ try {
+ field.set(this, upkeepPropertySheet.list.get(UpkeepPropertySheet.UpkeepPropertyType.valueOf(field.getName().toUpperCase()).ordinal()).getValue());
+ properties.setProperty(field.getName(), field.get(this).toString());
+ } catch (IllegalAccessException ex) {
+ logger.warning(messages.getString("error"));
+ ex.printStackTrace();
+ }
+ }
+ try (FileOutputStream stream = new FileOutputStream(file.toString())) {
+ properties.store(stream, "");
+ } catch (IOException ex) {
+ logger.warning(messages.getString("save_properties_error"));
+ ex.printStackTrace();
+ }
+ upkeepPropertySheet.clearChanges();
+ if (controller.serverIsRunning()) {
+ onServerStarted();
+ }
+ }
+ }
+
+ ButtonType saveCheck() {
+ if (upkeepPropertySheet != null && upkeepPropertySheet.haveChanges()) {
+ ButtonType result = controller.showYesNoCancel(messages.getString("changes_title"), messages.getString("changes_header"), messages.getString("changes_message")).get();
+ if (result == ButtonType.YES) {
+ saveUpkeep();
+ }
+ return result;
+ }
+ return new ButtonType("", ButtonBar.ButtonData.NO);
+ }
+
+ @FXML
+ void initialize () {
+ System.out.println("Initializing");
+ ButtonType check = saveCheck();
+ if (check == ButtonType.CANCEL) {
+ return;
+ }
+
+ try {
+ File file = getFile();
+ boolean created = file.getParentFile().mkdirs();
+ if (!created) {
+ created = file.createNewFile();
+ }
+
+ if (created) {
+ upkeepPropertySheet = new UpkeepPropertySheet(this);
+ upkeepPropertySheet.setAllToChanged();
+ saveUpkeep();
+ container.setContent(upkeepPropertySheet);
+ return;
+ }
+
+ FileInputStream stream = new FileInputStream(file.toString());
+ Properties properties = new Properties();
+ properties.load(stream);
+ configure(properties);
+ upkeepPropertySheet = new UpkeepPropertySheet(this);
+ container.setContent(upkeepPropertySheet);
+ } catch (IOException ex) {
+ logger.warning(messages.getString("load_properties_error"));
+ ex.printStackTrace();
+ }
+ }
+}
diff --git a/src/mod/wurmonline/mods/upkeepcosts/UpkeepPropertySheet.java b/src/mod/wurmonline/mods/upkeepcosts/UpkeepPropertySheet.java
new file mode 100644
index 0000000..0fe7e80
--- /dev/null
+++ b/src/mod/wurmonline/mods/upkeepcosts/UpkeepPropertySheet.java
@@ -0,0 +1,113 @@
+package mod.wurmonline.mods.upkeepcosts;
+
+import com.wurmonline.server.MiscConstants;
+import javafx.collections.FXCollections;
+import javafx.collections.ObservableList;
+import javafx.scene.control.Label;
+import javafx.scene.layout.Priority;
+import javafx.scene.layout.VBox;
+import mod.wurmonline.serverlauncher.LocaleHelper;
+import org.controlsfx.control.PropertySheet;
+
+import java.util.*;
+
+public class UpkeepPropertySheet extends VBox implements MiscConstants {
+ ObservableList list;
+ private Set changedProperties = new HashSet<>();
+ private ResourceBundle messages = LocaleHelper.getBundle("UpkeepCosts");
+
+ public UpkeepPropertySheet(UpkeepCostsUI upkeep) {
+ list = FXCollections.observableArrayList();
+ list.add(new UpkeepItem(UpkeepPropertyType.TILE_COST, "", messages.getString("tile_cost"), messages.getString("tile_cost_description"), true, upkeep.tile_cost));
+ list.add(new UpkeepItem(UpkeepPropertyType.TILE_UPKEEP, "", messages.getString("tile_upkeep"), messages.getString("tile_upkeep_description"), true, upkeep.tile_upkeep));
+ list.add(new UpkeepItem(UpkeepPropertyType.PERIMETER_COST, "", messages.getString("perimeter_cost"), messages.getString("perimeter_cost_description"), true, upkeep.perimeter_cost));
+ list.add(new UpkeepItem(UpkeepPropertyType.PERIMETER_UPKEEP, "", messages.getString("perimeter_upkeep"), messages.getString("perimeter_upkeep_description"), true, upkeep.perimeter_upkeep));
+ list.add(new UpkeepItem(UpkeepPropertyType.NORMAL_GUARD_COST, "", messages.getString("normal_guard_cost"), messages.getString("normal_guard_cost_description"), true, upkeep.normal_guard_cost));
+ list.add(new UpkeepItem(UpkeepPropertyType.NORMAL_GUARD_UPKEEP, "", messages.getString("normal_guard_upkeep"), messages.getString("normal_guard_upkeep_description"), true, upkeep.normal_guard_upkeep));
+ list.add(new UpkeepItem(UpkeepPropertyType.EPIC_GUARD_COST, "", messages.getString("epic_guard_cost"), messages.getString("epic_guard_cost_description"), true, upkeep.epic_guard_cost));
+ list.add(new UpkeepItem(UpkeepPropertyType.EPIC_GUARD_UPKEEP, "", messages.getString("epic_guard_upkeep"), messages.getString("epic_guard_upkeep_description"), true, upkeep.epic_guard_upkeep));
+ list.add(new UpkeepItem(UpkeepPropertyType.MINIMUM_UPKEEP, "", messages.getString("minimum_upkeep"), messages.getString("minimum_upkeep_description"), true, upkeep.minimum_upkeep));
+
+ PropertySheet propertySheet = new PropertySheet(list);
+ VBox.setVgrow(propertySheet, Priority.ALWAYS);
+ this.getChildren().add(new Label(messages.getString("irons_note")));
+ this.getChildren().add(propertySheet);
+ }
+
+ public boolean haveChanges () {
+ return changedProperties.size() > 0;
+ }
+
+ public void clearChanges () { changedProperties.clear(); }
+
+ public void setAllToChanged() {
+ for (PropertySheet.Item item : list) {
+ changedProperties.add(((UpkeepItem) item).getPropertyType());
+ }
+ }
+
+ class UpkeepItem implements PropertySheet.Item {
+ private UpkeepPropertyType type;
+ private String category;
+ private String name;
+ private String description;
+ private boolean editable = true;
+ private Long value;
+
+ UpkeepItem(UpkeepPropertyType aType, String aCategory, String aName, String aDescription, boolean aEditable, Long aValue) {
+ type = aType;
+ category = aCategory;
+ name = aName;
+ description = aDescription;
+ editable = aEditable;
+ value = aValue;
+ }
+
+ public UpkeepPropertyType getPropertyType() {
+ return type;
+ }
+
+ public Class> getType() {
+ return value.getClass();
+ }
+
+ public String getCategory() {
+ return category;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public String getDescription() {
+ return description;
+ }
+
+ public boolean isEditable() {
+ return editable;
+ }
+
+ public Object getValue() {
+ return value;
+ }
+
+ public void setValue(Object aValue) {
+ if(!value.equals(aValue)) {
+ changedProperties.add(type);
+ }
+ value = (Long)aValue;
+ }
+ }
+
+ enum UpkeepPropertyType {
+ TILE_COST,
+ TILE_UPKEEP,
+ PERIMETER_COST,
+ PERIMETER_UPKEEP,
+ NORMAL_GUARD_COST,
+ NORMAL_GUARD_UPKEEP,
+ EPIC_GUARD_COST,
+ EPIC_GUARD_UPKEEP,
+ MINIMUM_UPKEEP
+ }
+}