Resident removal fix, signal config change

Use Essentials to get residents to remove (#95)
Add IHaveConfig.signalChange() method (TBMCPlugins/Chroma-Chat#117)
This commit is contained in:
Norbi Peti 2020-08-26 22:14:53 +02:00
parent ef96ec2604
commit 43b7bd442a
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
3 changed files with 34 additions and 9 deletions

View file

@ -1,16 +1,23 @@
package buttondevteam.core.component.towny; package buttondevteam.core.component.towny;
import buttondevteam.core.MainPlugin;
import buttondevteam.lib.chat.Command2; import buttondevteam.lib.chat.Command2;
import buttondevteam.lib.chat.CommandClass; import buttondevteam.lib.chat.CommandClass;
import buttondevteam.lib.chat.CustomTabComplete; import buttondevteam.lib.chat.CustomTabComplete;
import buttondevteam.lib.chat.ICommand2MC; 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.TownySettings;
import com.palmergames.bukkit.towny.TownyUniverse; import com.palmergames.bukkit.towny.TownyUniverse;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.TownyObject; import com.palmergames.bukkit.towny.object.TownyObject;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import java.util.AbstractMap; import java.util.AbstractMap;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -26,10 +33,17 @@ public class RemoveResidentsCommand extends ICommand2MC {
sender.sendMessage("Starting..."); sender.sendMessage("Starting...");
var ds = TownyUniverse.getInstance().getDataSource(); var ds = TownyUniverse.getInstance().getDataSource();
var res = ds.getResidents().stream() 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 .flatMap(r -> {
.map(TownyObject::getName).map(Bukkit::getOfflinePlayer).filter(p -> !p.hasPlayedBefore()) var st = Stream.of(r) //https://stackoverflow.com/questions/37299312/in-java-8-lambdas-how-to-access-original-object-in-the-stream
.map(p -> new AbstractMap.SimpleEntry<>(r, p))) .map(TownyObject::getName);
.collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue)); 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:"); sender.sendMessage("Residents to remove:");
res.values().forEach(op -> sender.sendMessage(op.getName())); res.values().forEach(op -> sender.sendMessage(op.getName()));
if (TownySettings.isDeleteEcoAccount()) if (TownySettings.isDeleteEcoAccount())

View file

@ -71,10 +71,7 @@ public class ConfigData<T> {
@Override @Override
public String toString() { public String toString() {
return "ConfigData{" + return "ConfigData{" + "path='" + path + '\'' + ", value=" + value + '}';
"path='" + path + '\'' +
", value=" + value +
'}';
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -126,6 +123,10 @@ public class ConfigData<T> {
private void setInternal(Object val) { private void setInternal(Object val) {
config.set(path, val); config.set(path, val);
signalChange(config, saveAction);
}
static void signalChange(ConfigurationSection config, Runnable saveAction) {
if (!saveTasks.containsKey(config.getRoot())) { if (!saveTasks.containsKey(config.getRoot())) {
synchronized (saveTasks) { synchronized (saveTasks) {
saveTasks.put(config.getRoot(), new SaveTask(Bukkit.getScheduler().runTaskLaterAsynchronously(MainPlugin.Instance, () -> { saveTasks.put(config.getRoot(), new SaveTask(Bukkit.getScheduler().runTaskLaterAsynchronously(MainPlugin.Instance, () -> {

View file

@ -18,8 +18,11 @@ import java.util.stream.Collectors;
*/ */
public final class IHaveConfig { public final class IHaveConfig {
private final HashMap<String, ConfigData<?>> datamap = new HashMap<>(); private final HashMap<String, ConfigData<?>> datamap = new HashMap<>();
/**
* Returns the Bukkit ConfigurationSection. Use {@link #signalChange()} after changing it.
*/
@Getter @Getter
private ConfigurationSection config; private final ConfigurationSection config;
private final Runnable saveAction; private final Runnable saveAction;
/** /**
@ -154,6 +157,13 @@ public final class IHaveConfig {
return (ListConfigData<T>) data; return (ListConfigData<T>) data;
} }
/**
* Schedules a save operation. Use after changing the ConfigurationSection directly.
*/
public void signalChange() {
ConfigData.signalChange(config, saveAction);
}
/** /**
* Generates the config YAML. * Generates the config YAML.
* *