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>
<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>
</properties>
@ -38,10 +36,16 @@
<dependencies>
<dependency>
<groupId>net.citizensnpcs</groupId>
<artifactId>citizens</artifactId>
<version>${citizensapi.version}</version>
<artifactId>citizens-main</artifactId>
<version>2.0.30-SNAPSHOT</version>
<type>jar</type>
<scope>compile</scope>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
@ -52,7 +56,7 @@
<dependency>
<groupId>net.milkbowl.vault</groupId>
<artifactId>Vault</artifactId>
<version>${vault.version}</version>
<version>1.7.3</version>
<scope>provided</scope>
</dependency>
<dependency>
@ -81,8 +85,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>17</source>
<target>17</target>
<source>16</source>
<target>16</target>
</configuration>
</plugin>
</plugins>

View File

@ -88,10 +88,10 @@ public class BlacksmithPlugin extends JavaPlugin {
registerListeners();
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
// default value as the current value if the current value is null, as it's effectively the actual value.
// Additionally, this would allow NPC settings to update when the default is changed, as long as no custom
// value has been set.
//TODO: Improve un-setting of values for a given NPC: While setting values works fine, a bit more care should
// be performed regarding removing a custom value. Basically, using null for strings and -1 for numbers should
// unset a value for an NPC. Unsetting a value would make the NPC use the default value set in the config file
// instead
}
/**

View File

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

View File

@ -1,50 +1,48 @@
package net.knarcraft.blacksmith.config;
import java.util.Arrays;
/**
* An enum representing all of Blacksmith's settings
*/
public enum NPCSetting {
DROP_ITEM("defaults.dropItem", SettingValueType.BOOLEAN, true, "dropItem"),
DISABLE_COOL_DOWN("defaults.disableCoolDown", SettingValueType.BOOLEAN, false, "disableCoolDown"),
DISABLE_DELAY("defaults.disableDelay", SettingValueType.BOOLEAN, false, "disableDelay"),
FAIL_CHANCE("defaults.failReforgeChance", SettingValueType.PERCENTAGE, 10, "failReforgeChance"),
EXTRA_ENCHANTMENT_CHANCE("defaults.extraEnchantmentChance", SettingValueType.PERCENTAGE, 5,
DROP_ITEM("dropItem", SettingValueType.BOOLEAN, true, "dropItem"),
DISABLE_COOL_DOWN("disableCoolDown", SettingValueType.BOOLEAN, false, "disableCoolDown"),
DISABLE_DELAY("disableDelay", SettingValueType.BOOLEAN, false, "disableDelay"),
FAIL_CHANCE("failReforgeChance", SettingValueType.PERCENTAGE, 10, "failReforgeChance"),
EXTRA_ENCHANTMENT_CHANCE("extraEnchantmentChance", SettingValueType.PERCENTAGE, 5,
"extraEnchantmentChance"),
MAX_ENCHANTMENTS("defaults.maxEnchantments", SettingValueType.POSITIVE_INTEGER, 3, "maxEnchantments"),
MAX_REFORGE_DELAY("defaults.delaysInSeconds.maximum", SettingValueType.POSITIVE_INTEGER, 30, "maxReforgeDelay"),
MIN_REFORGE_DELAY("defaults.delaysInSeconds.minimum", SettingValueType.POSITIVE_INTEGER, 5, "minReforgeDelay"),
REFORGE_COOL_DOWN("defaults.delaysInSeconds.reforgeCoolDown", SettingValueType.POSITIVE_INTEGER, 60, "reforgeCoolDown"),
REFORGE_ABLE_ITEMS("defaults.reforgeAbleItems", SettingValueType.STRING_LIST, new String[]{}, "reforgeAbleItems"),
MAX_ENCHANTMENTS("maxEnchantments", SettingValueType.POSITIVE_INTEGER, 3, "maxEnchantments"),
MAX_REFORGE_DELAY("delaysInSeconds.maximum", SettingValueType.POSITIVE_INTEGER, 30, "maxReforgeDelay"),
MIN_REFORGE_DELAY("delaysInSeconds.minimum", SettingValueType.POSITIVE_INTEGER, 5, "minReforgeDelay"),
REFORGE_COOL_DOWN("delaysInSeconds.reforgeCoolDown", SettingValueType.POSITIVE_INTEGER, 60, "reforgeCoolDown"),
REFORGE_ABLE_ITEMS("reforgeAbleItems", SettingValueType.STRING_LIST, new String[]{}, "reforgeAbleItems"),
/*-----------
| 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"),
//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"),
//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"),
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"),
FAIL_MESSAGE("defaults.messages.failReforgeMessage", SettingValueType.STRING,
FAIL_MESSAGE("messages.failReforgeMessage", SettingValueType.STRING,
"&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"),
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"),
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"),
START_REFORGE_MESSAGE("defaults.messages.startReforgeMessage", SettingValueType.STRING,
START_REFORGE_MESSAGE("messages.startReforgeMessage", SettingValueType.STRING,
"&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"),
NOT_DAMAGED_MESSAGE("defaults.messages.notDamagedMessage", SettingValueType.STRING,
NOT_DAMAGED_MESSAGE("messages.notDamagedMessage", SettingValueType.STRING,
"&cThat item is not in need of repair", "notDamagedMessage");
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>
*/
NPCSetting(String path, SettingValueType valueType, Object value, String commandName) {
this.path = path;
this.path = "defaults." + path;
this.value = value;
this.valueType = valueType;
String[] pathParts = path.split("\\.");
this.childPath = String.join(".", Arrays.copyOfRange(pathParts, 1, pathParts.length));
this.childPath = path;
this.commandName = commandName;
if (pathParts.length > 0) {
this.nodeName = pathParts[pathParts.length - 1];

View File

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