Skip to content

Commit

Permalink
Add non-player members
Browse files Browse the repository at this point in the history
  • Loading branch information
stonar96 committed Oct 8, 2022
1 parent c8a6e59 commit 2dff680
Show file tree
Hide file tree
Showing 12 changed files with 217 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ public void loadConfiguration() {
maxClaimVolume = getInt("regions.max-claim-volume", 30000);
claimOnlyInsideExistingRegions = getBoolean("regions.claim-only-inside-existing-regions", false);
setParentOnClaim = getString("regions.set-parent-on-claim", "");
nonplayerBorderBypassOnClaim = getBoolean("regions.nonplayer-border-bypass-on-claim", false);
boundedLocationFlags = getBoolean("regions.location-flags-only-inside-regions", false);

maxRegionCountPerPlayer = getInt("regions.max-region-count-per-player.default", 7);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
import com.sk89q.worldguard.commands.GeneralCommands;
import com.sk89q.worldguard.commands.ProtectionCommands;
import com.sk89q.worldguard.commands.ToggleCommands;
import com.sk89q.worldguard.domains.CustomDomain;
import com.sk89q.worldguard.domains.registry.CustomDomainContext;
import com.sk89q.worldguard.domains.registry.SimpleDomainRegistry;
import com.sk89q.worldguard.protection.flags.Flag;
import com.sk89q.worldguard.protection.flags.Flags;
Expand Down Expand Up @@ -93,8 +95,14 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -132,6 +140,61 @@ public static WorldGuardPlugin inst() {
return inst;
}

@Override
public void onLoad() {
WorldGuard.getInstance().getDomainRegistry().register("nonplayer-protection-domains", n -> new CustomDomain(n) {
private final Set<String> nonplayerProtectionDomains = new HashSet<>();

@Override
public void parseInput(CustomDomainContext context) {
setDirty(true);
nonplayerProtectionDomains.addAll(Arrays.asList(context.getUserInput().split(",")));
}

@Override
public void unmarshal(Object o) {
nonplayerProtectionDomains.clear();
nonplayerProtectionDomains.addAll((Collection<? extends String>) o);
}

@Override
public Object marshal() {
return new ArrayList<>(nonplayerProtectionDomains);
}

@Override
public boolean contains(UUID uniqueId) {
return false;
}

@Override
public boolean contains(String playerName) {
return false;
}

@Override
public boolean containsNonplayer(String nonplayerProtectionDomain) {
return nonplayerProtectionDomains.contains(nonplayerProtectionDomain);
}

@Override
public int size() {
return nonplayerProtectionDomains.size();
}

@Override
public void clear() {
setDirty(true);
nonplayerProtectionDomains.clear();
}

@Override
public String toString() {
return " " + nonplayerProtectionDomains;
}
});
}

/**
* Called on plugin enable.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
import com.sk89q.worldguard.commands.task.RegionRemover;
import com.sk89q.worldguard.config.ConfigurationManager;
import com.sk89q.worldguard.config.WorldConfiguration;
import com.sk89q.worldguard.domains.CustomDomain;
import com.sk89q.worldguard.domains.registry.CustomDomainContext;
import com.sk89q.worldguard.domains.registry.InvalidDomainFormat;
import com.sk89q.worldguard.internal.permission.RegionPermissionModel;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.FlagValueCalculator;
Expand Down Expand Up @@ -83,7 +86,9 @@
import com.sk89q.worldguard.util.logging.LoggerToChatHandler;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.logging.Level;
Expand Down Expand Up @@ -330,6 +335,19 @@ public void claim(CommandContext args, Actor sender) throws CommandException {
}
}

if (wcfg.nonplayerBorderBypassOnClaim) {
CustomDomain customDomain = WorldGuard.getInstance().getDomainRegistry().createDomain("nonplayer-protection-domains");

try {
customDomain.parseInput(CustomDomainContext.create().setInput(player.getUniqueId().toString()).build());
} catch (InvalidDomainFormat e) {
throw new CommandException(e.getMessage());
}

region.getOwners().addCustomDomain(customDomain);
region.setFlag(Flags.NONPLAYER_PROTECTION_DOMAINS, new HashSet<>(Arrays.asList(player.getUniqueId().toString())));
}

region.getOwners().addPlayer(player);
manager.addRegion(region);
player.print(TextComponent.of(String.format("A new region has been claimed named '%s'.", id)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public abstract class WorldConfiguration {
public int maxClaimVolume;
public boolean claimOnlyInsideExistingRegions;
public String setParentOnClaim;
public boolean nonplayerBorderBypassOnClaim;
public int maxRegionCountPerPlayer;
public boolean antiWolfDumbness;
public boolean signChestProtection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,11 @@ public boolean contains(String playerName) {
return playerDomain.contains(playerName);
}

@Override
public boolean containsNonplayer(String nonplayerProtectionDomain) {
return customDomains.stream().anyMatch(d -> d.containsNonplayer(nonplayerProtectionDomain));
}

@Override
public int size() {
return groupDomain.size() + playerDomain.size() + customDomains.size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ public interface Domain {
@Deprecated
boolean contains(String playerName);

/**
* Returns true if a domain contains a non-player.
*
* @param nonplayerProtectionDomain the non-player protection domain to check
* @return whether this domain contains {@code nonplayerProtectionDomain}
*/
boolean containsNonplayer(String nonplayerProtectionDomain);

/**
* Get the number of entries.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ public boolean contains(String playerName) {
return false; // GroupDomains can't contain player names.
}

@Override
public boolean containsNonplayer(String nonplayerProtectionDomain) {
return false; // GroupDomains can't contain non-players.
}

@Override
public int size() {
return groups.size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ public boolean contains(String playerName) {
return names.contains(playerName.trim().toLowerCase());
}

@Override
public boolean containsNonplayer(String nonplayerProtectionDomain) {
return false; // PlayerDomains can't contain non-players.
}

@Override
public int size() {
return names.size() + uniqueIds.size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public boolean contains(String playerName) {
return false;
}

@Override
public boolean containsNonplayer(String nonplayerProtectionDomain) {
return false;
}

@Override
public int size() {
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,9 @@
import static com.google.common.base.Preconditions.checkNotNull;

import com.sk89q.worldguard.domains.Association;
import com.sk89q.worldguard.protection.FlagValueCalculator;
import com.sk89q.worldguard.protection.flags.Flags;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;

import javax.annotation.Nullable;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -64,39 +60,22 @@ protected void calcMaxPriority() {
this.maxPriorityRegions = bestRegions;
}

private boolean checkNonplayerProtectionDomains(Iterable<? extends ProtectedRegion> source, Collection<?> domains) {
if (source == null || domains == null || domains.isEmpty()) {
return false;
}

for (ProtectedRegion region : source) {
// Potential endless recurrence? No, because there is no region group flag.
Set<String> regionDomains = FlagValueCalculator.getEffectiveFlagOf(region, Flags.NONPLAYER_PROTECTION_DOMAINS, this);

if (regionDomains == null || regionDomains.isEmpty()) {
continue;
}

if (!Collections.disjoint(regionDomains, domains)) {
return true;
}
}

return false;
}

@Override
public Association getAssociation(List<ProtectedRegion> regions) {
checkNotNull(source);
boolean member = false;

for (ProtectedRegion region : regions) {
while (region != null) {
if ((region.getId().equals(ProtectedRegion.GLOBAL_REGION) && source.isEmpty())) {
ProtectedRegion current = region;

while (current != null) {
if ((current.getId().equals(ProtectedRegion.GLOBAL_REGION) && source.isEmpty())) {
return Association.OWNER;
}

if (source.contains(region)) {
if (source.contains(current)) {
if (useMaxPriorityAssociation) {
int priority = region.getPriority();
int priority = current.getPriority();
if (priority == maxPriority) {
return Association.OWNER;
}
Expand All @@ -105,23 +84,24 @@ public Association getAssociation(List<ProtectedRegion> regions) {
}
}

Set<ProtectedRegion> source;
current = current.getParent();
}

if (useMaxPriorityAssociation) {
source = maxPriorityRegions;
} else {
source = this.source;
}
Set<ProtectedRegion> source;

// Potential endless recurrence? No, because there is no region group flag.
if (checkNonplayerProtectionDomains(source, FlagValueCalculator.getEffectiveFlagOf(region, Flags.NONPLAYER_PROTECTION_DOMAINS, this))) {
return Association.OWNER;
}
if (useMaxPriorityAssociation) {
source = maxPriorityRegions;
} else {
source = this.source;
}

region = region.getParent();
if (source.stream().anyMatch(region::isOwner)) {
return Association.OWNER;
} else if (!member && source.stream().anyMatch(region::isMember)) {
member = true;
}
}

return Association.NON_MEMBER;
return member ? Association.MEMBER : Association.NON_MEMBER;
}
}
Loading

0 comments on commit 2dff680

Please sign in to comment.