Remove async data management

This commit is contained in:
HexedHero 2022-06-21 03:23:21 +01:00
parent f13a6de519
commit 50c4fc9ee1
3 changed files with 242 additions and 265 deletions

View File

@ -114,6 +114,12 @@ public final class CitizensCMD extends JavaPlugin {
console.sendMessage(TAG.append(LEGACY.deserialize("&3Citizens&cCMD &8&o" + getDescription().getVersion()))); console.sendMessage(TAG.append(LEGACY.deserialize("&3Citizens&cCMD &8&o" + getDescription().getVersion())));
console.sendMessage(TAG.append(LEGACY.deserialize("&8by &3Mateus Moreira &c@LichtHund &8& Maintained by &3HexedHero"))); console.sendMessage(TAG.append(LEGACY.deserialize("&8by &3Mateus Moreira &c@LichtHund &8& Maintained by &3HexedHero")));
// Data
dataHandler = new DataHandler(this);
cooldownHandler = new CooldownHandler(this);
dataHandler.initialize();
cooldownHandler.initialize();
// Settings // Settings
settings = SettingsManagerBuilder settings = SettingsManagerBuilder
.withYamlFile(Paths.get(getDataFolder().getPath(), "config.yml")) .withYamlFile(Paths.get(getDataFolder().getPath(), "config.yml"))
@ -157,11 +163,6 @@ public final class CitizensCMD extends JavaPlugin {
console.sendMessage(TAG.append(lang.getMessage(Messages.VAULT_AVAILABLE))); console.sendMessage(TAG.append(lang.getMessage(Messages.VAULT_AVAILABLE)));
} }
// Data
dataHandler = new DataHandler(this);
cooldownHandler = new CooldownHandler(this);
Bukkit.getScheduler().scheduleSyncDelayedTask(this, () -> { dataHandler.initialize(); cooldownHandler.initialize(); }, 1L);
// API // API
api = new CitizensCMDAPI(dataHandler); api = new CitizensCMDAPI(dataHandler);

View File

@ -36,9 +36,9 @@ import me.mattstudios.citizenscmd.utility.Util;
public class CooldownHandler { public class CooldownHandler {
private final CitizensCMD plugin; private final CitizensCMD plugin;
private File cooldownsFile; private File cooldownsFile;
private File dir; private File dir;
private FileConfiguration cooldownsConfigurator; private FileConfiguration cooldownsConfigurator;
private final Map<String, Long> cooldownData = new ConcurrentHashMap<>(); private final Map<String, Long> cooldownData = new ConcurrentHashMap<>();

View File

@ -30,7 +30,6 @@ import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import org.bukkit.Bukkit;
import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
@ -45,10 +44,10 @@ import net.kyori.adventure.audience.Audience;
public class DataHandler { public class DataHandler {
private final CitizensCMD plugin; private final CitizensCMD plugin;
private static File savesFile;
private static File dir;
private static FileConfiguration dataConfigurator; private File savesFile;
private File dir;
private FileConfiguration dataConfigurator;
private final Map<String, Object> data = new ConcurrentHashMap<>(); private final Map<String, Object> data = new ConcurrentHashMap<>();
@ -90,43 +89,40 @@ public class DataHandler {
* Caches the data from the file * Caches the data from the file
*/ */
private void cacheData() { private void cacheData() {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { try {
try { dataConfigurator.load(savesFile);
dataConfigurator.load(savesFile);
if (!dataConfigurator.contains("npc-data")) { if (!dataConfigurator.contains("npc-data")) {
return; return;
}
for (final String parent : Objects.requireNonNull(dataConfigurator.getConfigurationSection("npc-data")).getKeys(false)) {
for (final String child : Objects.requireNonNull(dataConfigurator.getConfigurationSection("npc-data." + parent)).getKeys(false)) {
switch (child.toLowerCase()) {
case "permission":
data.put("npc-data." + parent + "." + child, dataConfigurator.getString("npc-data." + parent + "." + child));
break;
case "cooldown":
data.put("npc-data." + parent + "." + child, dataConfigurator.getInt("npc-data." + parent + "." + child));
break;
case "right-click-commands":
case "left-click-commands":
data.put("npc-data." + parent + "." + child, dataConfigurator.getStringList("npc-data." + parent + "." + child));
break;
case "price":
data.put("npc-data." + parent + "." + child, dataConfigurator.getDouble("npc-data." + parent + "." + child));
break;
}
}
}
} catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();
} }
}); for (final String parent : Objects.requireNonNull(dataConfigurator.getConfigurationSection("npc-data")).getKeys(false)) {
for (final String child : Objects.requireNonNull(dataConfigurator.getConfigurationSection("npc-data." + parent)).getKeys(false)) {
switch (child.toLowerCase()) {
case "permission":
data.put("npc-data." + parent + "." + child, dataConfigurator.getString("npc-data." + parent + "." + child));
break;
case "cooldown":
data.put("npc-data." + parent + "." + child, dataConfigurator.getInt("npc-data." + parent + "." + child));
break;
case "right-click-commands":
case "left-click-commands":
data.put("npc-data." + parent + "." + child, dataConfigurator.getStringList("npc-data." + parent + "." + child));
break;
case "price":
data.put("npc-data." + parent + "." + child, dataConfigurator.getDouble("npc-data." + parent + "." + child));
break;
}
}
}
} catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();
}
} }
/** /**
@ -139,53 +135,51 @@ public class DataHandler {
* @param left If the command should be added to the left or right click * @param left If the command should be added to the left or right click
*/ */
public void addCommand(int npc, String permission, String command, Audience sender, boolean left) { public void addCommand(int npc, String permission, String command, Audience sender, boolean left) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { try {
try { createBasics();
createBasics(); dataConfigurator.load(savesFile);
dataConfigurator.load(savesFile);
final List<String> commandList = data.containsKey("npc-data.npc-" + npc + ".right-click-commands") ? (List<String>) data.get("npc-data.npc-" + npc + ".right-click-commands") : new ArrayList<>(); final List<String> commandList = data.containsKey("npc-data.npc-" + npc + ".right-click-commands") ? (List<String>) data.get("npc-data.npc-" + npc + ".right-click-commands") : new ArrayList<>();
final List<String> commandListLeft = data.containsKey("npc-data.npc-" + npc + ".left-click-commands") ? (List<String>) data.get("npc-data.npc-" + npc + ".left-click-commands") : new ArrayList<>(); final List<String> commandListLeft = data.containsKey("npc-data.npc-" + npc + ".left-click-commands") ? (List<String>) data.get("npc-data.npc-" + npc + ".left-click-commands") : new ArrayList<>();
if (!data.containsKey("npc-data.npc-" + npc + ".cooldown")) { if (!data.containsKey("npc-data.npc-" + npc + ".cooldown")) {
data.put("npc-data.npc-" + npc + ".cooldown", Util.getDefaultCooldown(plugin)); data.put("npc-data.npc-" + npc + ".cooldown", Util.getDefaultCooldown(plugin));
dataConfigurator.set("npc-data.npc-" + npc + ".cooldown", Util.getDefaultCooldown(plugin)); dataConfigurator.set("npc-data.npc-" + npc + ".cooldown", Util.getDefaultCooldown(plugin));
}
if (left) {
commandListLeft.add("[" + permission + "] " + command);
} else {
commandList.add("[" + permission + "] " + command);
}
if (data.containsKey("npc-data.npc-" + npc + ".right-click-commands")) {
data.replace("npc-data.npc-" + npc + ".right-click-commands", commandList);
} else {
data.put("npc-data.npc-" + npc + ".right-click-commands", commandList);
}
dataConfigurator.set("npc-data.npc-" + npc + ".right-click-commands", commandList);
if (data.containsKey("npc-data.npc-" + npc + ".left-click-commands")) {
data.replace("npc-data.npc-" + npc + ".left-click-commands", commandListLeft);
} else {
data.put("npc-data.npc-" + npc + ".left-click-commands", commandListLeft);
}
dataConfigurator.set("npc-data.npc-" + npc + ".left-click-commands", commandListLeft);
if (!data.containsKey("npc-data.npc-" + npc + ".price")) {
data.put("npc-data.npc-" + npc + ".price", 0);
dataConfigurator.set("npc-data.npc-" + npc + ".price", 0);
}
sender.sendMessage(HEADER);
sender.sendMessage(plugin.getLang().getMessage(Messages.NPC_ADDED));
dataConfigurator.save(savesFile);
} catch (IOException | InvalidConfigurationException e) {
sender.sendMessage(HEADER);
sender.sendMessage(plugin.getLang().getMessage(Messages.NPC_ADD_FAIL));
} }
});
if (left) {
commandListLeft.add("[" + permission + "] " + command);
} else {
commandList.add("[" + permission + "] " + command);
}
if (data.containsKey("npc-data.npc-" + npc + ".right-click-commands")) {
data.replace("npc-data.npc-" + npc + ".right-click-commands", commandList);
} else {
data.put("npc-data.npc-" + npc + ".right-click-commands", commandList);
}
dataConfigurator.set("npc-data.npc-" + npc + ".right-click-commands", commandList);
if (data.containsKey("npc-data.npc-" + npc + ".left-click-commands")) {
data.replace("npc-data.npc-" + npc + ".left-click-commands", commandListLeft);
} else {
data.put("npc-data.npc-" + npc + ".left-click-commands", commandListLeft);
}
dataConfigurator.set("npc-data.npc-" + npc + ".left-click-commands", commandListLeft);
if (!data.containsKey("npc-data.npc-" + npc + ".price")) {
data.put("npc-data.npc-" + npc + ".price", 0);
dataConfigurator.set("npc-data.npc-" + npc + ".price", 0);
}
sender.sendMessage(HEADER);
sender.sendMessage(plugin.getLang().getMessage(Messages.NPC_ADDED));
dataConfigurator.save(savesFile);
} catch (IOException | InvalidConfigurationException e) {
sender.sendMessage(HEADER);
sender.sendMessage(plugin.getLang().getMessage(Messages.NPC_ADD_FAIL));
}
} }
/** /**
@ -197,48 +191,46 @@ public class DataHandler {
* @param left If the command should be added to the left or right click * @param left If the command should be added to the left or right click
*/ */
public void addCommand(int npc, String permission, String command, boolean left) { public void addCommand(int npc, String permission, String command, boolean left) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { try {
try { createBasics();
createBasics(); dataConfigurator.load(savesFile);
dataConfigurator.load(savesFile);
final List<String> commandList = data.containsKey("npc-data.npc-" + npc + ".right-click-commands") ? (List<String>) data.get("npc-data.npc-" + npc + ".right-click-commands") : new ArrayList<>(); final List<String> commandList = data.containsKey("npc-data.npc-" + npc + ".right-click-commands") ? (List<String>) data.get("npc-data.npc-" + npc + ".right-click-commands") : new ArrayList<>();
final List<String> commandListLeft = data.containsKey("npc-data.npc-" + npc + ".left-click-commands") ? (List<String>) data.get("npc-data.npc-" + npc + ".left-click-commands") : new ArrayList<>(); final List<String> commandListLeft = data.containsKey("npc-data.npc-" + npc + ".left-click-commands") ? (List<String>) data.get("npc-data.npc-" + npc + ".left-click-commands") : new ArrayList<>();
if (!data.containsKey("npc-data.npc-" + npc + ".cooldown")) { if (!data.containsKey("npc-data.npc-" + npc + ".cooldown")) {
data.put("npc-data.npc-" + npc + ".cooldown", Util.getDefaultCooldown(plugin)); data.put("npc-data.npc-" + npc + ".cooldown", Util.getDefaultCooldown(plugin));
dataConfigurator.set("npc-data.npc-" + npc + ".cooldown", Util.getDefaultCooldown(plugin)); dataConfigurator.set("npc-data.npc-" + npc + ".cooldown", Util.getDefaultCooldown(plugin));
}
if (left) {
commandListLeft.add("[" + permission + "] " + command);
} else {
commandList.add("[" + permission + "] " + command);
}
if (data.containsKey("npc-data.npc-" + npc + ".right-click-commands")) {
data.replace("npc-data.npc-" + npc + ".right-click-commands", commandList);
} else {
data.put("npc-data.npc-" + npc + ".right-click-commands", commandList);
}
dataConfigurator.set("npc-data.npc-" + npc + ".right-click-commands", commandList);
if (data.containsKey("npc-data.npc-" + npc + ".left-click-commands")) {
data.replace("npc-data.npc-" + npc + ".left-click-commands", commandListLeft);
} else {
data.put("npc-data.npc-" + npc + ".left-click-commands", commandListLeft);
}
dataConfigurator.set("npc-data.npc-" + npc + ".left-click-commands", commandListLeft);
if (!data.containsKey("npc-data.npc-" + npc + ".price")) {
data.put("npc-data.npc-" + npc + ".price", 0);
dataConfigurator.set("npc-data.npc-" + npc + ".price", 0);
}
dataConfigurator.save(savesFile);
} catch (IOException | InvalidConfigurationException ignored) {
} }
});
if (left) {
commandListLeft.add("[" + permission + "] " + command);
} else {
commandList.add("[" + permission + "] " + command);
}
if (data.containsKey("npc-data.npc-" + npc + ".right-click-commands")) {
data.replace("npc-data.npc-" + npc + ".right-click-commands", commandList);
} else {
data.put("npc-data.npc-" + npc + ".right-click-commands", commandList);
}
dataConfigurator.set("npc-data.npc-" + npc + ".right-click-commands", commandList);
if (data.containsKey("npc-data.npc-" + npc + ".left-click-commands")) {
data.replace("npc-data.npc-" + npc + ".left-click-commands", commandListLeft);
} else {
data.put("npc-data.npc-" + npc + ".left-click-commands", commandListLeft);
}
dataConfigurator.set("npc-data.npc-" + npc + ".left-click-commands", commandListLeft);
if (!data.containsKey("npc-data.npc-" + npc + ".price")) {
data.put("npc-data.npc-" + npc + ".price", 0);
dataConfigurator.set("npc-data.npc-" + npc + ".price", 0);
}
dataConfigurator.save(savesFile);
} catch (IOException | InvalidConfigurationException ignored) {
}
} }
/** /**
@ -249,24 +241,22 @@ public class DataHandler {
* @param sender The player who run the command * @param sender The player who run the command
*/ */
public void setCooldown(int npc, int cooldown, Audience sender) { public void setCooldown(int npc, int cooldown, Audience sender) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { try {
try { createBasics();
createBasics(); dataConfigurator.load(savesFile);
dataConfigurator.load(savesFile);
dataConfigurator.set("npc-data.npc-" + npc + ".cooldown", cooldown); dataConfigurator.set("npc-data.npc-" + npc + ".cooldown", cooldown);
data.replace("npc-data.npc-" + npc + ".cooldown", cooldown); data.replace("npc-data.npc-" + npc + ".cooldown", cooldown);
sender.sendMessage(HEADER); sender.sendMessage(HEADER);
sender.sendMessage(plugin.getLang().getMessage(Messages.NPC_COOLDOWN_SET)); sender.sendMessage(plugin.getLang().getMessage(Messages.NPC_COOLDOWN_SET));
dataConfigurator.save(savesFile); dataConfigurator.save(savesFile);
} catch (IOException | InvalidConfigurationException e) { } catch (IOException | InvalidConfigurationException e) {
sender.sendMessage(HEADER); sender.sendMessage(HEADER);
sender.sendMessage(plugin.getLang().getMessage(Messages.NPC_COOLDOWN_SET_ERROR)); sender.sendMessage(plugin.getLang().getMessage(Messages.NPC_COOLDOWN_SET_ERROR));
} }
});
} }
/** /**
@ -277,23 +267,21 @@ public class DataHandler {
* @param sender The player who run the command * @param sender The player who run the command
*/ */
public void setPrice(int npc, double price, Audience sender) { public void setPrice(int npc, double price, Audience sender) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { try {
try { createBasics();
createBasics(); dataConfigurator.load(savesFile);
dataConfigurator.load(savesFile);
dataConfigurator.set("npc-data.npc-" + npc + ".price", price); dataConfigurator.set("npc-data.npc-" + npc + ".price", price);
data.replace("npc-data.npc-" + npc + ".price", price); data.replace("npc-data.npc-" + npc + ".price", price);
sender.sendMessage(HEADER); sender.sendMessage(HEADER);
sender.sendMessage(plugin.getLang().getMessage(Messages.NPC_PRICE_SET)); sender.sendMessage(plugin.getLang().getMessage(Messages.NPC_PRICE_SET));
dataConfigurator.save(savesFile); dataConfigurator.save(savesFile);
} catch (IOException | InvalidConfigurationException e) { } catch (IOException | InvalidConfigurationException e) {
e.printStackTrace(); e.printStackTrace();
} }
});
} }
/** /**
@ -304,23 +292,21 @@ public class DataHandler {
* @param sender The player who run the command * @param sender The player who run the command
*/ */
public void setCustomPermission(int npc, String permission, Audience sender) { public void setCustomPermission(int npc, String permission, Audience sender) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { try {
try { createBasics();
createBasics(); dataConfigurator.load(savesFile);
dataConfigurator.load(savesFile);
dataConfigurator.set("npc-data.npc-" + npc + ".permission", permission); dataConfigurator.set("npc-data.npc-" + npc + ".permission", permission);
data.replace("npc-data.npc-" + npc + ".permission", permission); data.replace("npc-data.npc-" + npc + ".permission", permission);
sender.sendMessage(HEADER); sender.sendMessage(HEADER);
sender.sendMessage(plugin.getLang().getMessage(Messages.PERMISSION_SET)); sender.sendMessage(plugin.getLang().getMessage(Messages.PERMISSION_SET));
dataConfigurator.save(savesFile); dataConfigurator.save(savesFile);
} catch (IOException | InvalidConfigurationException e) { } catch (IOException | InvalidConfigurationException e) {
e.printStackTrace(); e.printStackTrace();
} }
});
} }
/** /**
@ -330,25 +316,23 @@ public class DataHandler {
* @param sender The player who run the command * @param sender The player who run the command
*/ */
public void removeCustomPermission(int npc, Audience sender) { public void removeCustomPermission(int npc, Audience sender) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { try {
try { createBasics();
createBasics(); dataConfigurator.load(savesFile);
dataConfigurator.load(savesFile);
if (dataConfigurator.contains("npc-data.npc-" + npc + ".permission")) { if (dataConfigurator.contains("npc-data.npc-" + npc + ".permission")) {
dataConfigurator.set("npc-data.npc-" + npc + ".permission", null); dataConfigurator.set("npc-data.npc-" + npc + ".permission", null);
}
data.remove("npc-data.npc-" + npc + ".permission");
sender.sendMessage(HEADER);
sender.sendMessage(plugin.getLang().getMessage(Messages.PERMISSION_REMOVED));
dataConfigurator.save(savesFile);
} catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();
} }
});
data.remove("npc-data.npc-" + npc + ".permission");
sender.sendMessage(HEADER);
sender.sendMessage(plugin.getLang().getMessage(Messages.PERMISSION_REMOVED));
dataConfigurator.save(savesFile);
} catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();
}
} }
public String getCustomPermission(int npc) { public String getCustomPermission(int npc) {
@ -445,27 +429,25 @@ public class DataHandler {
* @param sender The player to send the message to * @param sender The player to send the message to
*/ */
public void removeCommand(int npc, int commandID, EnumTypes.ClickType click, Audience sender) { public void removeCommand(int npc, int commandID, EnumTypes.ClickType click, Audience sender) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { try {
try { createBasics();
createBasics(); dataConfigurator.load(savesFile);
dataConfigurator.load(savesFile);
final List<String> commands = getClickCommandsData(npc, click); final List<String> commands = getClickCommandsData(npc, click);
commands.remove(commandID - 1); commands.remove(commandID - 1);
final String key = "npc-data.npc-" + npc + "." + click.toString().toLowerCase() + "-click-commands"; final String key = "npc-data.npc-" + npc + "." + click.toString().toLowerCase() + "-click-commands";
data.replace(key, commands); data.replace(key, commands);
dataConfigurator.set(key, commands); dataConfigurator.set(key, commands);
sender.sendMessage(HEADER); sender.sendMessage(HEADER);
sender.sendMessage(plugin.getLang().getMessage(Messages.REMOVED_COMMAND)); sender.sendMessage(plugin.getLang().getMessage(Messages.REMOVED_COMMAND));
dataConfigurator.save(savesFile); dataConfigurator.save(savesFile);
} catch (IOException | InvalidConfigurationException e) { } catch (IOException | InvalidConfigurationException e) {
e.printStackTrace(); e.printStackTrace();
} }
});
} }
/** /**
@ -479,43 +461,41 @@ public class DataHandler {
* @param sender The player to send messages * @param sender The player to send messages
*/ */
public void edit(int npc, int commandID, EnumTypes.ClickType click, EnumTypes.EditType type, String newValue, Audience sender) { public void edit(int npc, int commandID, EnumTypes.ClickType click, EnumTypes.EditType type, String newValue, Audience sender) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { try {
try { createBasics();
createBasics(); dataConfigurator.load(savesFile);
dataConfigurator.load(savesFile);
final List<String> commandsData = getClickCommandsData(npc, click); final List<String> commandsData = getClickCommandsData(npc, click);
final String typeText; final String typeText;
switch (type) { switch (type) {
case CMD: case CMD:
String tempCommand = commandsData.get(commandID - 1); String tempCommand = commandsData.get(commandID - 1);
tempCommand = tempCommand.replaceAll(" ([^]]*)", " " + newValue); tempCommand = tempCommand.replaceAll(" ([^]]*)", " " + newValue);
commandsData.set(commandID - 1, tempCommand); commandsData.set(commandID - 1, tempCommand);
typeText = "CMD"; typeText = "CMD";
break; break;
case PERM: case PERM:
String tempPerm = commandsData.get(commandID - 1); String tempPerm = commandsData.get(commandID - 1);
tempPerm = tempPerm.replaceAll("\\[([^]]*)]", "[" + newValue + "]"); tempPerm = tempPerm.replaceAll("\\[([^]]*)]", "[" + newValue + "]");
commandsData.set(commandID - 1, tempPerm); commandsData.set(commandID - 1, tempPerm);
typeText = "PERM"; typeText = "PERM";
break; break;
default: default:
typeText = ""; typeText = "";
}
final String key = "npc-data.npc-" + npc + "." + click.toString().toLowerCase() + "-click-commands";
data.replace(key, commandsData);
dataConfigurator.set(key, commandsData);
sender.sendMessage(HEADER);
sender.sendMessage(plugin.getLang().getMessage(Messages.EDITED_COMMAND, "{type}", typeText));
dataConfigurator.save(savesFile);
} catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();
} }
});
final String key = "npc-data.npc-" + npc + "." + click.toString().toLowerCase() + "-click-commands";
data.replace(key, commandsData);
dataConfigurator.set(key, commandsData);
sender.sendMessage(HEADER);
sender.sendMessage(plugin.getLang().getMessage(Messages.EDITED_COMMAND, "{type}", typeText));
dataConfigurator.save(savesFile);
} catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();
}
} }
/** /**
@ -524,22 +504,20 @@ public class DataHandler {
* @param npc the NPC id * @param npc the NPC id
*/ */
public void removeNPCData(int npc) { public void removeNPCData(int npc) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { try {
try { createBasics();
createBasics(); dataConfigurator.load(savesFile);
dataConfigurator.load(savesFile);
if (dataConfigurator.contains("npc-data.npc-" + npc)) { if (dataConfigurator.contains("npc-data.npc-" + npc)) {
dataConfigurator.set("npc-data.npc-" + npc, null); dataConfigurator.set("npc-data.npc-" + npc, null);
}
data.keySet().removeIf(key -> key.contains("npc-data.npc-" + npc));
dataConfigurator.save(savesFile);
} catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();
} }
});
data.keySet().removeIf(key -> key.contains("npc-data.npc-" + npc));
dataConfigurator.save(savesFile);
} catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();
}
} }
/** /**
@ -549,27 +527,25 @@ public class DataHandler {
* @param npcClone The ID of the new NPC. * @param npcClone The ID of the new NPC.
*/ */
public void cloneData(int npc, int npcClone) { public void cloneData(int npc, int npcClone) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { try {
try { createBasics();
createBasics(); dataConfigurator.load(savesFile);
dataConfigurator.load(savesFile);
final Map<String, Object> newNpcData = new HashMap<>(); final Map<String, Object> newNpcData = new HashMap<>();
for (final String key : data.keySet()) { for (final String key : data.keySet()) {
if (key.contains("npc-" + npc)) { if (key.contains("npc-" + npc)) {
final String newKey = key.replace("npc-" + npc, "npc-" + npcClone); final String newKey = key.replace("npc-" + npc, "npc-" + npcClone);
newNpcData.put(newKey, data.get(key)); newNpcData.put(newKey, data.get(key));
dataConfigurator.set(newKey, data.get(key)); dataConfigurator.set(newKey, data.get(key));
}
} }
data.putAll(newNpcData);
dataConfigurator.save(savesFile);
} catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();
} }
});
data.putAll(newNpcData);
dataConfigurator.save(savesFile);
} catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();
}
} }
/** /**