From 43b7bd442a45d133fec03a273c34454c12c1f3af Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Wed, 26 Aug 2020 22:14:53 +0200 Subject: [PATCH] Resident removal fix, signal config change Use Essentials to get residents to remove (#95) Add IHaveConfig.signalChange() method (TBMCPlugins/Chroma-Chat#117) --- .../towny/RemoveResidentsCommand.java | 22 +++++++++++++++---- .../lib/architecture/ConfigData.java | 9 ++++---- .../lib/architecture/IHaveConfig.java | 12 +++++++++- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/Chroma-Core/src/main/java/buttondevteam/core/component/towny/RemoveResidentsCommand.java b/Chroma-Core/src/main/java/buttondevteam/core/component/towny/RemoveResidentsCommand.java index a740b42..f5307a1 100644 --- a/Chroma-Core/src/main/java/buttondevteam/core/component/towny/RemoveResidentsCommand.java +++ b/Chroma-Core/src/main/java/buttondevteam/core/component/towny/RemoveResidentsCommand.java @@ -1,16 +1,23 @@ package buttondevteam.core.component.towny; +import buttondevteam.core.MainPlugin; import buttondevteam.lib.chat.Command2; import buttondevteam.lib.chat.CommandClass; import buttondevteam.lib.chat.CustomTabComplete; import buttondevteam.lib.chat.ICommand2MC; +import buttondevteam.lib.player.TBMCPlayer; +import com.earth2me.essentials.Essentials; +import com.earth2me.essentials.User; import com.palmergames.bukkit.towny.TownySettings; import com.palmergames.bukkit.towny.TownyUniverse; +import com.palmergames.bukkit.towny.object.Resident; import com.palmergames.bukkit.towny.object.TownyObject; import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; import org.bukkit.command.CommandSender; import java.util.AbstractMap; +import java.util.Map; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -26,10 +33,17 @@ public class RemoveResidentsCommand extends ICommand2MC { sender.sendMessage("Starting..."); var ds = TownyUniverse.getInstance().getDataSource(); var res = ds.getResidents().stream() - .flatMap(r -> Stream.of(r) //https://stackoverflow.com/questions/37299312/in-java-8-lambdas-how-to-access-original-object-in-the-stream - .map(TownyObject::getName).map(Bukkit::getOfflinePlayer).filter(p -> !p.hasPlayedBefore()) - .map(p -> new AbstractMap.SimpleEntry<>(r, p))) - .collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue)); + .flatMap(r -> { + var st = Stream.of(r) //https://stackoverflow.com/questions/37299312/in-java-8-lambdas-how-to-access-original-object-in-the-stream + .map(TownyObject::getName); + return (MainPlugin.ess == null + ? st.map(Bukkit::getOfflinePlayer) + : st.map(MainPlugin.ess::getOfflineUser).map(User::getBase)) + .filter(p -> !p.hasPlayedBefore()) + .map(p -> new AbstractMap.SimpleEntry<>(r, p)); + }).collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue)); + if (MainPlugin.ess == null) + sender.sendMessage("§cEssentials not found, players who haven't joined after changing their names are also listed here."); sender.sendMessage("Residents to remove:"); res.values().forEach(op -> sender.sendMessage(op.getName())); if (TownySettings.isDeleteEcoAccount()) diff --git a/Chroma-Core/src/main/java/buttondevteam/lib/architecture/ConfigData.java b/Chroma-Core/src/main/java/buttondevteam/lib/architecture/ConfigData.java index 2725866..47adc92 100644 --- a/Chroma-Core/src/main/java/buttondevteam/lib/architecture/ConfigData.java +++ b/Chroma-Core/src/main/java/buttondevteam/lib/architecture/ConfigData.java @@ -71,10 +71,7 @@ public class ConfigData { @Override public String toString() { - return "ConfigData{" + - "path='" + path + '\'' + - ", value=" + value + - '}'; + return "ConfigData{" + "path='" + path + '\'' + ", value=" + value + '}'; } @SuppressWarnings("unchecked") @@ -126,6 +123,10 @@ public class ConfigData { private void setInternal(Object val) { config.set(path, val); + signalChange(config, saveAction); + } + + static void signalChange(ConfigurationSection config, Runnable saveAction) { if (!saveTasks.containsKey(config.getRoot())) { synchronized (saveTasks) { saveTasks.put(config.getRoot(), new SaveTask(Bukkit.getScheduler().runTaskLaterAsynchronously(MainPlugin.Instance, () -> { diff --git a/Chroma-Core/src/main/java/buttondevteam/lib/architecture/IHaveConfig.java b/Chroma-Core/src/main/java/buttondevteam/lib/architecture/IHaveConfig.java index 18b8097..5d9e1fa 100644 --- a/Chroma-Core/src/main/java/buttondevteam/lib/architecture/IHaveConfig.java +++ b/Chroma-Core/src/main/java/buttondevteam/lib/architecture/IHaveConfig.java @@ -18,8 +18,11 @@ import java.util.stream.Collectors; */ public final class IHaveConfig { private final HashMap> datamap = new HashMap<>(); + /** + * Returns the Bukkit ConfigurationSection. Use {@link #signalChange()} after changing it. + */ @Getter - private ConfigurationSection config; + private final ConfigurationSection config; private final Runnable saveAction; /** @@ -154,6 +157,13 @@ public final class IHaveConfig { return (ListConfigData) data; } + /** + * Schedules a save operation. Use after changing the ConfigurationSection directly. + */ + public void signalChange() { + ConfigData.signalChange(config, saveAction); + } + /** * Generates the config YAML. *