WorldGuard merge unification

This commit is contained in:
Esophose 2020-10-01 23:20:00 -06:00
parent b20d9582b3
commit 822fe397aa
3 changed files with 38 additions and 55 deletions

View file

@ -46,7 +46,7 @@ public class PlayerParticles extends JavaPlugin {
/*
* The plugin managers
*/
private Map<Class<?>, Manager> managers;
private final Map<Class<?>, Manager> managers;
public PlayerParticles() {
INSTANCE = this;
@ -93,6 +93,7 @@ public class PlayerParticles extends JavaPlugin {
@Override
public void onLoad() {
if (NMSUtil.isSpigot())
WorldGuardHook.initialize();
}

View file

@ -1,8 +1,10 @@
package dev.esophose.playerparticles.hook;
import java.util.*;
import dev.esophose.playerparticles.manager.ConfigurationManager.Setting;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.codemc.worldguardwrapper.WorldGuardWrapper;
@ -20,13 +22,11 @@ public class WorldGuardHook {
* Must be called during onLoad, or else WorldGuard prevents flag registration.
*/
public static void initialize() {
if (Bukkit.getPluginManager().getPlugin("WorldGuard") == null) {
return; // Unsupported
}
if (Bukkit.getPluginManager().getPlugin("WorldGuard") == null)
return;
worldGuardWrapper = WorldGuardWrapper.getInstance();
flagPlayerParticles = worldGuardWrapper.registerFlag("player-particles", WrappedState.class, WrappedState.ALLOW).get();
flagPlayerParticles = worldGuardWrapper.registerFlag("player-particles", WrappedState.class, WrappedState.ALLOW).orElse(null);
}
/**
@ -34,7 +34,6 @@ public class WorldGuardHook {
*/
public static boolean enabled() {
return worldGuardWrapper != null;
}
/**
@ -51,29 +50,28 @@ public class WorldGuardHook {
// Get the "player-particles" flag.
// This will use the region priority to determine which one takes precedence.
Optional<WrappedState> playerParticles = regions
.stream()
if (flagPlayerParticles != null) {
Optional<WrappedState> flagState = regions.stream()
.sorted(Comparator.comparingInt(IWrappedRegion::getPriority))
.map(region -> region.getFlag(flagPlayerParticles))
.filter(Optional::isPresent)
.map(Optional::get)
.findFirst();
if (playerParticles.isPresent())
return playerParticles.get() == WrappedState.ALLOW;
if (flagState.isPresent())
return flagState.get() == WrappedState.ALLOW;
}
// Legacy blocking by region name.
List<String> disallowedRegionIds = Setting.WORLDGUARD_DISALLOWED_REGIONS.getStringList();
if (regions.stream().map(IWrappedRegion::getId).anyMatch(disallowedRegionIds::contains)) {
if (regions.stream().map(IWrappedRegion::getId).anyMatch(disallowedRegionIds::contains))
return false;
}
if (Setting.WORLDGUARD_USE_ALLOWED_REGIONS.getBoolean()) {
if (!Setting.WORLDGUARD_USE_ALLOWED_REGIONS.getBoolean())
return true;
List<String> allowedRegionIds = Setting.WORLDGUARD_ALLOWED_REGIONS.getStringList();
return regions.stream().map(IWrappedRegion::getId).anyMatch(allowedRegionIds::contains);
}
return true;
}
}

View file

@ -5,12 +5,12 @@ import dev.esophose.playerparticles.config.CommentedFileConfiguration;
import dev.esophose.playerparticles.util.ParticleUtils;
import java.io.File;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.bukkit.Material;
@ -225,10 +225,16 @@ public class ConfigurationManager extends Manager {
}
/**
* @return true if this setting is only a section and doesn't contain an actual value
* @return true if a setting is still its default value, otherwise false
*/
public boolean isSection() {
return this.defaultValue == null;
public boolean isDefault() {
this.loadValue();
// Don't care about list ordering
if (this.defaultValue instanceof Collection && this.value instanceof Collection)
return (((Collection<?>) this.defaultValue).containsAll((Collection<?>) this.value));
return Objects.equals(this.defaultValue, this.value);
}
/**
@ -274,37 +280,15 @@ public class ConfigurationManager extends Manager {
this.configuration.save();
// Legacy nag: WorldGuard Regions
if (!isDefault(Setting.WORLDGUARD_ALLOWED_REGIONS, false) || !isDefault(Setting.WORLDGUARD_DISALLOWED_REGIONS, false)) {
if (!(Setting.WORLDGUARD_ALLOWED_REGIONS.isDefault() && Setting.WORLDGUARD_DISALLOWED_REGIONS.isDefault())) {
Arrays.asList(
"It looks like you're using the 'allowed-regions' or 'disallowed-regions' settings.",
"These settings are deprecated from PlayerParticles, and will be removed in a future update.",
"These settings are deprecated and will be removed in a future update.",
"As an alternative, consider using the newer and more flexible 'player-particles' WorldGuard region flag."
).forEach(PlayerParticles.getInstance().getLogger()::warning);
}
}
/**
* Checks if a setting is the default value.
*
* @param setting The setting to check.
* @param strictOrdering If true, compares lists with strict ordering.
* @return True if the setting is default.
*/
private boolean isDefault(Setting setting, boolean strictOrdering) {
if (setting.defaultValue instanceof List) {
if (strictOrdering) return setting.defaultValue.equals(setting.value);
HashSet<String> currentSet = new HashSet<>(setting.getStringList());
Set<String> defaultSet = ((List<?>) setting.defaultValue).stream()
.map(Object::toString)
.collect(Collectors.toSet());
return defaultSet.equals(currentSet);
}
return setting.defaultValue.equals(setting.value);
}
@Override
public void disable() {
for (Setting setting : Setting.values())