Saves NPCs immediately after they are changed

Additionally:
Updates Citizens dependency to the newest version
Removes some redundancy in NPC settings' path
This commit is contained in:
Kristian Knarvik 2022-10-07 17:14:58 +02:00
parent fb483a4c2a
commit 907389f978
5 changed files with 43 additions and 39 deletions

20
pom.xml
View File

@ -13,8 +13,6 @@
<!-- Properties --> <!-- Properties -->
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<citizensapi.version>2.0.26-SNAPSHOT</citizensapi.version>
<vault.version>1.7.3</vault.version>
<build.number>Unknown</build.number> <build.number>Unknown</build.number>
</properties> </properties>
@ -38,10 +36,16 @@
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>net.citizensnpcs</groupId> <groupId>net.citizensnpcs</groupId>
<artifactId>citizens</artifactId> <artifactId>citizens-main</artifactId>
<version>${citizensapi.version}</version> <version>2.0.30-SNAPSHOT</version>
<type>jar</type> <type>jar</type>
<scope>compile</scope> <scope>provided</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.spigotmc</groupId> <groupId>org.spigotmc</groupId>
@ -52,7 +56,7 @@
<dependency> <dependency>
<groupId>net.milkbowl.vault</groupId> <groupId>net.milkbowl.vault</groupId>
<artifactId>Vault</artifactId> <artifactId>Vault</artifactId>
<version>${vault.version}</version> <version>1.7.3</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
@ -81,8 +85,8 @@
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version> <version>3.7.0</version>
<configuration> <configuration>
<source>17</source> <source>16</source>
<target>17</target> <target>16</target>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>

View File

@ -88,10 +88,10 @@ public class BlacksmithPlugin extends JavaPlugin {
registerListeners(); registerListeners();
getLogger().log(Level.INFO, " v" + getDescription().getVersion() + " enabled."); getLogger().log(Level.INFO, " v" + getDescription().getVersion() + " enabled.");
//TODO: Default blacksmith trait aren't saved to the config file. One way to fix this would be to display the //TODO: Improve un-setting of values for a given NPC: While setting values works fine, a bit more care should
// default value as the current value if the current value is null, as it's effectively the actual value. // be performed regarding removing a custom value. Basically, using null for strings and -1 for numbers should
// Additionally, this would allow NPC settings to update when the default is changed, as long as no custom // unset a value for an NPC. Unsetting a value would make the NPC use the default value set in the config file
// value has been set. // instead
} }
/** /**

View File

@ -39,7 +39,7 @@ public class BlackSmithEditCommand implements CommandExecutor {
return false; return false;
} }
BlacksmithTrait blacksmithTrait = npc.getTrait(BlacksmithTrait.class); BlacksmithTrait blacksmithTrait = npc.getTraitNullable(BlacksmithTrait.class);
for (NPCSetting npcSetting : NPCSetting.values()) { for (NPCSetting npcSetting : NPCSetting.values()) {
String commandName = npcSetting.getCommandName(); String commandName = npcSetting.getCommandName();
@ -87,6 +87,8 @@ public class BlackSmithEditCommand implements CommandExecutor {
ChatColor.translateAlternateColorCodes('&', newValue); ChatColor.translateAlternateColorCodes('&', newValue);
blacksmithTrait.getSettings().changeSetting(npcSetting, nullCheckedValue); blacksmithTrait.getSettings().changeSetting(npcSetting, nullCheckedValue);
displaySuccessMessage(sender, TranslatableMessage.getValueChangedMessage(npcSetting.getNodeName(), newValue)); displaySuccessMessage(sender, TranslatableMessage.getValueChangedMessage(npcSetting.getNodeName(), newValue));
//Save the changes immediately to prevent data loss on server crash
CitizensAPI.getNPCRegistry().saveToStore();
return true; return true;
} else { } else {
return false; return false;

View File

@ -1,50 +1,48 @@
package net.knarcraft.blacksmith.config; package net.knarcraft.blacksmith.config;
import java.util.Arrays;
/** /**
* An enum representing all of Blacksmith's settings * An enum representing all of Blacksmith's settings
*/ */
public enum NPCSetting { public enum NPCSetting {
DROP_ITEM("defaults.dropItem", SettingValueType.BOOLEAN, true, "dropItem"), DROP_ITEM("dropItem", SettingValueType.BOOLEAN, true, "dropItem"),
DISABLE_COOL_DOWN("defaults.disableCoolDown", SettingValueType.BOOLEAN, false, "disableCoolDown"), DISABLE_COOL_DOWN("disableCoolDown", SettingValueType.BOOLEAN, false, "disableCoolDown"),
DISABLE_DELAY("defaults.disableDelay", SettingValueType.BOOLEAN, false, "disableDelay"), DISABLE_DELAY("disableDelay", SettingValueType.BOOLEAN, false, "disableDelay"),
FAIL_CHANCE("defaults.failReforgeChance", SettingValueType.PERCENTAGE, 10, "failReforgeChance"), FAIL_CHANCE("failReforgeChance", SettingValueType.PERCENTAGE, 10, "failReforgeChance"),
EXTRA_ENCHANTMENT_CHANCE("defaults.extraEnchantmentChance", SettingValueType.PERCENTAGE, 5, EXTRA_ENCHANTMENT_CHANCE("extraEnchantmentChance", SettingValueType.PERCENTAGE, 5,
"extraEnchantmentChance"), "extraEnchantmentChance"),
MAX_ENCHANTMENTS("defaults.maxEnchantments", SettingValueType.POSITIVE_INTEGER, 3, "maxEnchantments"), MAX_ENCHANTMENTS("maxEnchantments", SettingValueType.POSITIVE_INTEGER, 3, "maxEnchantments"),
MAX_REFORGE_DELAY("defaults.delaysInSeconds.maximum", SettingValueType.POSITIVE_INTEGER, 30, "maxReforgeDelay"), MAX_REFORGE_DELAY("delaysInSeconds.maximum", SettingValueType.POSITIVE_INTEGER, 30, "maxReforgeDelay"),
MIN_REFORGE_DELAY("defaults.delaysInSeconds.minimum", SettingValueType.POSITIVE_INTEGER, 5, "minReforgeDelay"), MIN_REFORGE_DELAY("delaysInSeconds.minimum", SettingValueType.POSITIVE_INTEGER, 5, "minReforgeDelay"),
REFORGE_COOL_DOWN("defaults.delaysInSeconds.reforgeCoolDown", SettingValueType.POSITIVE_INTEGER, 60, "reforgeCoolDown"), REFORGE_COOL_DOWN("delaysInSeconds.reforgeCoolDown", SettingValueType.POSITIVE_INTEGER, 60, "reforgeCoolDown"),
REFORGE_ABLE_ITEMS("defaults.reforgeAbleItems", SettingValueType.STRING_LIST, new String[]{}, "reforgeAbleItems"), REFORGE_ABLE_ITEMS("reforgeAbleItems", SettingValueType.STRING_LIST, new String[]{}, "reforgeAbleItems"),
/*----------- /*-----------
| Messages | | Messages |
-----------*/ -----------*/
BUSY_WITH_PLAYER_MESSAGE("defaults.messages.busyPlayerMessage", SettingValueType.STRING, BUSY_WITH_PLAYER_MESSAGE("messages.busyPlayerMessage", SettingValueType.STRING,
"&cI'm busy at the moment. Come back later!", "busyPlayerMessage"), "&cI'm busy at the moment. Come back later!", "busyPlayerMessage"),
//TODO: Add placeholder for remaining time? //TODO: Add placeholder for remaining time?
BUSY_WITH_REFORGE_MESSAGE("defaults.messages.busyReforgeMessage", SettingValueType.STRING, BUSY_WITH_REFORGE_MESSAGE("messages.busyReforgeMessage", SettingValueType.STRING,
"&cI'm working on it. Be patient!", "busyReforgeMessage"), "&cI'm working on it. Be patient!", "busyReforgeMessage"),
//TODO: Add placeholder for remaining time? //TODO: Add placeholder for remaining time?
COOL_DOWN_UNEXPIRED_MESSAGE("defaults.messages.coolDownUnexpiredMessage", SettingValueType.STRING, COOL_DOWN_UNEXPIRED_MESSAGE("messages.coolDownUnexpiredMessage", SettingValueType.STRING,
"&cYou've already had your chance! Give me a break!", "coolDownUnexpiredMessage"), "&cYou've already had your chance! Give me a break!", "coolDownUnexpiredMessage"),
COST_MESSAGE("defaults.messages.costMessage", SettingValueType.STRING, COST_MESSAGE("messages.costMessage", SettingValueType.STRING,
"&eIt will cost &a<price> &eto reforge that &a<item>&e! Click again to reforge!", "costMessage"), "&eIt will cost &a<price> &eto reforge that &a<item>&e! Click again to reforge!", "costMessage"),
FAIL_MESSAGE("defaults.messages.failReforgeMessage", SettingValueType.STRING, FAIL_MESSAGE("messages.failReforgeMessage", SettingValueType.STRING,
"&cWhoops! Didn't mean to do that! Maybe next time?", "failReforgeMessage"), "&cWhoops! Didn't mean to do that! Maybe next time?", "failReforgeMessage"),
INSUFFICIENT_FUNDS_MESSAGE("defaults.messages.insufficientFundsMessage", SettingValueType.STRING, INSUFFICIENT_FUNDS_MESSAGE("messages.insufficientFundsMessage", SettingValueType.STRING,
"&cYou don't have enough money to reforge that item!", "insufficientFundsMessage"), "&cYou don't have enough money to reforge that item!", "insufficientFundsMessage"),
INVALID_ITEM_MESSAGE("defaults.messages.invalidItemMessage", SettingValueType.STRING, INVALID_ITEM_MESSAGE("messages.invalidItemMessage", SettingValueType.STRING,
"&cI'm sorry, but I don't know how to reforge that!", "invalidItemMessage"), "&cI'm sorry, but I don't know how to reforge that!", "invalidItemMessage"),
ITEM_UNEXPECTEDLY_CHANGED_MESSAGE("defaults.messages.itemChangedMessage", SettingValueType.STRING, ITEM_UNEXPECTEDLY_CHANGED_MESSAGE("messages.itemChangedMessage", SettingValueType.STRING,
"&cThat's not the item you wanted to reforge before!", "itemChangedMessage"), "&cThat's not the item you wanted to reforge before!", "itemChangedMessage"),
START_REFORGE_MESSAGE("defaults.messages.startReforgeMessage", SettingValueType.STRING, START_REFORGE_MESSAGE("messages.startReforgeMessage", SettingValueType.STRING,
"&eOk, let's see what I can do...", "startReforgeMessage"), "&eOk, let's see what I can do...", "startReforgeMessage"),
SUCCESS_MESSAGE("defaults.messages.successMessage", SettingValueType.STRING, SUCCESS_MESSAGE("messages.successMessage", SettingValueType.STRING,
"There you go! All better!", "successMessage"), "There you go! All better!", "successMessage"),
NOT_DAMAGED_MESSAGE("defaults.messages.notDamagedMessage", SettingValueType.STRING, NOT_DAMAGED_MESSAGE("messages.notDamagedMessage", SettingValueType.STRING,
"&cThat item is not in need of repair", "notDamagedMessage"); "&cThat item is not in need of repair", "notDamagedMessage");
private final String path; private final String path;
@ -63,11 +61,11 @@ public enum NPCSetting {
* @param commandName <p>The name of the command used to change this setting</p> * @param commandName <p>The name of the command used to change this setting</p>
*/ */
NPCSetting(String path, SettingValueType valueType, Object value, String commandName) { NPCSetting(String path, SettingValueType valueType, Object value, String commandName) {
this.path = path; this.path = "defaults." + path;
this.value = value; this.value = value;
this.valueType = valueType; this.valueType = valueType;
String[] pathParts = path.split("\\."); String[] pathParts = path.split("\\.");
this.childPath = String.join(".", Arrays.copyOfRange(pathParts, 1, pathParts.length)); this.childPath = path;
this.commandName = commandName; this.commandName = commandName;
if (pathParts.length > 0) { if (pathParts.length > 0) {
this.nodeName = pathParts[pathParts.length - 1]; this.nodeName = pathParts[pathParts.length - 1];

View File

@ -19,7 +19,7 @@ public class NPCClickListener implements Listener {
if (!event.getNPC().hasTrait(BlacksmithTrait.class)) { if (!event.getNPC().hasTrait(BlacksmithTrait.class)) {
return; return;
} }
BlacksmithTrait blacksmithTrait = event.getNPC().getTrait(BlacksmithTrait.class); BlacksmithTrait blacksmithTrait = event.getNPC().getTraitNullable(BlacksmithTrait.class);
//Perform any necessary pre-session work //Perform any necessary pre-session work
Player player = event.getClicker(); Player player = event.getClicker();