diff --git a/worldguard-core/src/main/java/com/sk89q/worldguard/domains/DefaultDomain.java b/worldguard-core/src/main/java/com/sk89q/worldguard/domains/DefaultDomain.java index 975d1a58..672b22ce 100644 --- a/worldguard-core/src/main/java/com/sk89q/worldguard/domains/DefaultDomain.java +++ b/worldguard-core/src/main/java/com/sk89q/worldguard/domains/DefaultDomain.java @@ -36,12 +36,12 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; import static com.google.common.base.Preconditions.checkNotNull; @@ -53,7 +53,7 @@ public class DefaultDomain implements Domain, ChangeTracked { private PlayerDomain playerDomain = new PlayerDomain(); private GroupDomain groupDomain = new GroupDomain(); - private Set customDomains = new HashSet<>(); + private final Map customDomains = new ConcurrentHashMap<>(); private boolean customDomainsChanged = false; /** @@ -70,7 +70,7 @@ public DefaultDomain() { public DefaultDomain(DefaultDomain existing) { setPlayerDomain(existing.getPlayerDomain()); setGroupDomain(existing.getGroupDomain()); - setCustomDomains(existing.getCustomDomains()); + setCustomDomains(existing.customDomains); } /** @@ -118,9 +118,8 @@ public void setGroupDomain(GroupDomain groupDomain) { */ public void addCustomDomain(CustomDomain customDomain) { checkNotNull(customDomain); - removeCustomDomain(customDomain.getName()); - this.customDomains.add(customDomain); - customDomainsChanged = true; + this.customDomains.put(customDomain.getName(), customDomain); + this.customDomainsChanged = true; } /** @@ -130,8 +129,8 @@ public void addCustomDomain(CustomDomain customDomain) { */ public void removeCustomDomain(String name) { checkNotNull(name); - if (this.customDomains.removeIf(d -> d.getName().equalsIgnoreCase(name))) { - customDomainsChanged = true; + if (this.customDomains.remove(name) != null) { + this.customDomainsChanged = true; } } @@ -142,8 +141,8 @@ public void removeCustomDomain(String name) { */ public void removeCustomDomain(CustomDomain customDomain) { checkNotNull(customDomain); - if (this.customDomains.remove(customDomain)) { - customDomainsChanged = true; + if (this.customDomains.remove(customDomain.getName()) != null) { + this.customDomainsChanged = true; } } @@ -152,10 +151,11 @@ public void removeCustomDomain(CustomDomain customDomain) { * * @param customDomains the domains */ - public void setCustomDomains(Collection customDomains) { + public void setCustomDomains(Map customDomains) { checkNotNull(customDomains); - this.customDomains = new HashSet<>(customDomains); - customDomainsChanged = true; + this.customDomains.clear(); + this.customDomains.putAll(customDomains); + this.customDomainsChanged = true; } /** @@ -163,8 +163,18 @@ public void setCustomDomains(Collection customDomains) { * * @return a unmodifiable copy of the domains */ - public Set getCustomDomains() { - return Collections.unmodifiableSet(this.customDomains); + public Collection getCustomDomains() { + return Collections.unmodifiableCollection(this.customDomains.values()); + } + + /** + * Get the api domain specified by its name + * + * @param name the name of the domain + * @return the custom domain + */ + public @Nullable CustomDomain getCustomDomain(String name) { + return this.customDomains.get(name); } /** @@ -311,12 +321,12 @@ public Set getGroups() { @Override public boolean contains(LocalPlayer player) { - return playerDomain.contains(player) || groupDomain.contains(player) || customDomains.stream().anyMatch(d -> d.contains(player)); + return playerDomain.contains(player) || groupDomain.contains(player) || customDomains.values().stream().anyMatch(d -> d.contains(player)); } @Override public boolean contains(UUID uniqueId) { - return playerDomain.contains(uniqueId) || customDomains.stream().anyMatch(d -> d.contains(uniqueId)); + return playerDomain.contains(uniqueId) || customDomains.values().stream().anyMatch(d -> d.contains(uniqueId)); } @Override @@ -384,7 +394,7 @@ public String toGroupsString() { public String toCustomDomainsString() { List output = new ArrayList<>(); - for (CustomDomain customDomain : customDomains) { + for (CustomDomain customDomain : customDomains.values()) { output.add(customDomain.getName() + ":" + customDomain.toString()); } output.sort(String.CASE_INSENSITIVE_ORDER); @@ -513,7 +523,7 @@ private Component toPlayersComponent(ProfileCache cache) { private Component toCustomDomainsComponent() { final TextComponent.Builder builder = TextComponent.builder(""); - for (Iterator it = customDomains.iterator(); it.hasNext(); ) { + for (Iterator it = customDomains.values().iterator(); it.hasNext(); ) { CustomDomain domain = it.next(); builder.append(TextComponent.of(domain.getName() + ":", TextColor.LIGHT_PURPLE)) .append(TextComponent.of(domain.toString(), TextColor.GOLD)); @@ -528,7 +538,7 @@ private Component toCustomDomainsComponent() { @Override public boolean isDirty() { return playerDomain.isDirty() || groupDomain.isDirty() || - customDomainsChanged || customDomains.stream().anyMatch(ChangeTracked::isDirty); + customDomainsChanged || customDomains.values().stream().anyMatch(ChangeTracked::isDirty); } @Override @@ -536,7 +546,7 @@ public void setDirty(boolean dirty) { playerDomain.setDirty(dirty); groupDomain.setDirty(dirty); customDomainsChanged = dirty; - customDomains.forEach(d -> d.setDirty(dirty)); + customDomains.values().forEach(d -> d.setDirty(dirty)); } @Override diff --git a/worldguard-core/src/main/java/com/sk89q/worldguard/domains/registry/DomainRegistry.java b/worldguard-core/src/main/java/com/sk89q/worldguard/domains/registry/DomainRegistry.java index 5fd8ac46..65e6d9c1 100644 --- a/worldguard-core/src/main/java/com/sk89q/worldguard/domains/registry/DomainRegistry.java +++ b/worldguard-core/src/main/java/com/sk89q/worldguard/domains/registry/DomainRegistry.java @@ -83,7 +83,7 @@ public interface DomainRegistry extends Iterable> { * @param createUnknown Whether "just in time" domains should be created for unknown domains * @return The unmarshalled domain list */ - List unmarshal(Map rawValues, boolean createUnknown); + Map unmarshal(Map rawValues, boolean createUnknown); /** * Get the number of registered domains. diff --git a/worldguard-core/src/main/java/com/sk89q/worldguard/domains/registry/SimpleDomainRegistry.java b/worldguard-core/src/main/java/com/sk89q/worldguard/domains/registry/SimpleDomainRegistry.java index 2d4330a2..488ff322 100644 --- a/worldguard-core/src/main/java/com/sk89q/worldguard/domains/registry/SimpleDomainRegistry.java +++ b/worldguard-core/src/main/java/com/sk89q/worldguard/domains/registry/SimpleDomainRegistry.java @@ -25,9 +25,8 @@ import com.sk89q.worldguard.domains.CustomDomain; import javax.annotation.Nullable; -import java.util.ArrayList; +import java.util.HashMap; import java.util.Iterator; -import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentMap; import java.util.logging.Level; @@ -139,20 +138,20 @@ private CustomDomain getOrCreate(String name, Object value, boolean createUnknow return null; } - public List unmarshal(Map rawValues, boolean createUnknown) { + public Map unmarshal(Map rawValues, boolean createUnknown) { checkNotNull(rawValues, "rawValues"); - List domainList = new ArrayList<>(); + Map domains = new HashMap<>(); for (Map.Entry entry : rawValues.entrySet()) { try { CustomDomain domain = getOrCreate(entry.getKey(), entry.getValue(), createUnknown); - domainList.add(domain); + domains.put(domain.getName(), domain); } catch (Throwable e) { log.log(Level.WARNING, "Failed to unmarshal domain for " + entry.getKey(), e); } } - return domainList; + return domains; } @Override diff --git a/worldguard-core/src/main/java/com/sk89q/worldguard/protection/managers/storage/file/YamlRegionFile.java b/worldguard-core/src/main/java/com/sk89q/worldguard/protection/managers/storage/file/YamlRegionFile.java index cd1d26e7..732bb423 100644 --- a/worldguard-core/src/main/java/com/sk89q/worldguard/protection/managers/storage/file/YamlRegionFile.java +++ b/worldguard-core/src/main/java/com/sk89q/worldguard/protection/managers/storage/file/YamlRegionFile.java @@ -289,7 +289,7 @@ private DefaultDomain parseDomain(YAMLNode node) { YAMLNode apiDomains = node.getNode("custom"); if (apiDomains != null) { - List parsedDomains = WorldGuard.getInstance().getDomainRegistry().unmarshal(apiDomains.getMap(), true); + Map parsedDomains = WorldGuard.getInstance().getDomainRegistry().unmarshal(apiDomains.getMap(), true); domain.setCustomDomains(parsedDomains); }