Skip to content

Commit

Permalink
Added Server Launcher support with per server settings and internatio…
Browse files Browse the repository at this point in the history
…nalisation.
  • Loading branch information
Mthec committed Dec 4, 2015
1 parent cb4803f commit 6bf1ebb
Show file tree
Hide file tree
Showing 6 changed files with 389 additions and 25 deletions.
20 changes: 18 additions & 2 deletions src/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,32 @@
<project default="jar" name="Upkeep Cost Settings">
<target name="jar">
<jar destfile="../out/jar/mods/upkeepcosts/upkeepcosts.jar">
<fileset dir="../out/production/NoMinimumUpkeep" includes="mod/" />
<fileset dir="../out/production/NoMinimumUpkeep" includes="**/UpkeepCosts.class" />
</jar>
<copy file="upkeepcosts.properties" tofile="../out/jar/mods/upkeepcosts.properties" />

<jar destfile="../out/jarui/mods/upkeepcosts/upkeepcosts.jar">
<fileset dir="../out/production/NoMinimumUpkeep" includes="**/Upkeep*" excludes="locale/**" />
</jar>
<copy file="upkeepcosts.properties" tofile="../out/jarui/mods/upkeepcosts.properties" />
<propertyfile file="../out/jarui/mods/upkeepcosts.properties">
<entry key="classname" value="mod.wurmonline.mods.upkeepcosts.UpkeepCostsUI" />
</propertyfile>

<copy todir="C:/Steam/steamapps/common/Wurm Unlimited/WurmServerLauncher">
<fileset dir="../out/jar" />
<fileset dir="../out/jarui" />
</copy>
<copy todir="C:/Steam/steamapps/common/Wurm Unlimited/WurmServerLauncher/locales">
<fileset dir="locale" />
</copy>
</target>
<target name="zip" depends="jar">
<zip destfile="../upkeepcosts.zip">
<zipfileset dir="../out/jar" />
</zip>
<zip destfile="../upkeepcostsui.zip">
<zipfileset dir="../out/jarui" />
<zipfileset file="locale/UpkeepCosts.properties" fullpath="locale/UpkeepCosts.properties" />
</zip>
</target>
</project>
40 changes: 40 additions & 0 deletions src/locale/UpkeepCosts.properties
Original file line number Diff line number Diff line change
@@ -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.
18 changes: 18 additions & 0 deletions src/mod/wurmonline/mods/upkeepcosts/UpkeepCosts.fxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>


<VBox prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="mod.wurmonline.mods.upkeepcosts.UpkeepCostsUI">
<ScrollPane fx:id="container" prefHeight="200.0" prefWidth="200.0" VBox.vgrow="ALWAYS" />
<HBox minHeight="-Infinity" minWidth="-Infinity" VBox.vgrow="NEVER">
<children>
<Button mnemonicParsing="false" onAction="#initialize" text="%button.refresh" />
<Button mnemonicParsing="false" onAction="#saveUpkeep" text="%button.save" />
</children>
</HBox>
</VBox>
52 changes: 29 additions & 23 deletions src/mod/wurmonline/mods/upkeepcosts/UpkeepCosts.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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));
}
}
171 changes: 171 additions & 0 deletions src/mod/wurmonline/mods/upkeepcosts/UpkeepCostsUI.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Loading

0 comments on commit 6bf1ebb

Please sign in to comment.