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;
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())

View file

@ -71,10 +71,7 @@ public class ConfigData<T> {
@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<T> {
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, () -> {

View file

@ -18,8 +18,11 @@ import java.util.stream.Collectors;
*/
public final class IHaveConfig {
private final HashMap<String, ConfigData<?>> 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<T>) data;
}
/**
* Schedules a save operation. Use after changing the ConfigurationSection directly.
*/
public void signalChange() {
ConfigData.signalChange(config, saveAction);
}
/**
* Generates the config YAML.
*