Add inheritance for all flags (#1787)

This commit is contained in:
stonar96 2021-08-09 05:00:02 +02:00 committed by GitHub
parent 0165175a2e
commit 5e702f80a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 37 additions and 12 deletions

View File

@ -52,6 +52,7 @@
import com.sk89q.worldguard.config.WorldConfiguration; import com.sk89q.worldguard.config.WorldConfiguration;
import com.sk89q.worldguard.internal.permission.RegionPermissionModel; import com.sk89q.worldguard.internal.permission.RegionPermissionModel;
import com.sk89q.worldguard.protection.ApplicableRegionSet; import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.FlagValueCalculator;
import com.sk89q.worldguard.protection.flags.Flag; import com.sk89q.worldguard.protection.flags.Flag;
import com.sk89q.worldguard.protection.flags.FlagContext; import com.sk89q.worldguard.protection.flags.FlagContext;
import com.sk89q.worldguard.protection.flags.Flags; import com.sk89q.worldguard.protection.flags.Flags;
@ -1095,21 +1096,21 @@ public void teleport(CommandContext args, Actor sender) throws CommandException
// -s for spawn location // -s for spawn location
if (args.hasFlag('s')) { if (args.hasFlag('s')) {
teleportLocation = existing.getFlag(Flags.SPAWN_LOC); teleportLocation = FlagValueCalculator.getEffectiveFlagOf(existing, Flags.SPAWN_LOC, player);
if (teleportLocation == null) { if (teleportLocation == null) {
throw new CommandException( throw new CommandException(
"The region has no spawn point associated."); "The region has no spawn point associated.");
} }
} else { } else {
teleportLocation = existing.getFlag(Flags.TELE_LOC); teleportLocation = FlagValueCalculator.getEffectiveFlagOf(existing, Flags.TELE_LOC, player);
if (teleportLocation == null) { if (teleportLocation == null) {
throw new CommandException("The region has no teleport point associated."); throw new CommandException("The region has no teleport point associated.");
} }
} }
String message = existing.getFlag(Flags.TELE_MESSAGE); String message = FlagValueCalculator.getEffectiveFlagOf(existing, Flags.TELE_MESSAGE, player);
// If the flag isn't set, use the default message // If the flag isn't set, use the default message
// If message.isEmpty(), no message is sent by LocalPlayer#teleport(...) // If message.isEmpty(), no message is sent by LocalPlayer#teleport(...)

View File

@ -36,6 +36,8 @@
import com.sk89q.worldguard.WorldGuard; import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.domains.DefaultDomain; import com.sk89q.worldguard.domains.DefaultDomain;
import com.sk89q.worldguard.internal.permission.RegionPermissionModel; import com.sk89q.worldguard.internal.permission.RegionPermissionModel;
import com.sk89q.worldguard.protection.FlagValueCalculator;
import com.sk89q.worldguard.protection.association.RegionAssociable;
import com.sk89q.worldguard.protection.flags.Flag; import com.sk89q.worldguard.protection.flags.Flag;
import com.sk89q.worldguard.protection.flags.Flags; import com.sk89q.worldguard.protection.flags.Flags;
import com.sk89q.worldguard.protection.flags.RegionGroupFlag; import com.sk89q.worldguard.protection.flags.RegionGroupFlag;
@ -315,7 +317,7 @@ public void appendBounds() {
.clickEvent(ClickEvent.of(ClickEvent.Action.RUN_COMMAND, "/rg select " + region.getId())); .clickEvent(ClickEvent.of(ClickEvent.Action.RUN_COMMAND, "/rg select " + region.getId()));
} }
builder.append(bound); builder.append(bound);
final Location teleFlag = region.getFlag(Flags.TELE_LOC); final Location teleFlag = FlagValueCalculator.getEffectiveFlagOf(region, Flags.TELE_LOC, perms.getSender() instanceof RegionAssociable ? (RegionAssociable) perms.getSender() : null);
if (teleFlag != null && perms != null && perms.mayTeleportTo(region)) { if (teleFlag != null && perms != null && perms.mayTeleportTo(region)) {
builder.append(TextComponent.space().append(TextComponent.of("[Teleport]", TextColor.GRAY) builder.append(TextComponent.space().append(TextComponent.of("[Teleport]", TextColor.GRAY)
.hoverEvent(HoverEvent.of(HoverEvent.Action.SHOW_TEXT, .hoverEvent(HoverEvent.of(HoverEvent.Action.SHOW_TEXT,

View File

@ -24,6 +24,7 @@
import com.sk89q.minecraft.util.commands.CommandException; import com.sk89q.minecraft.util.commands.CommandException;
import com.sk89q.worldguard.util.profile.Profile; import com.sk89q.worldguard.util.profile.Profile;
import com.sk89q.worldedit.extension.platform.Actor; import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.util.formatting.component.PaginationBox; import com.sk89q.worldedit.util.formatting.component.PaginationBox;
import com.sk89q.worldedit.util.formatting.text.Component; import com.sk89q.worldedit.util.formatting.text.Component;
import com.sk89q.worldedit.util.formatting.text.TextComponent; import com.sk89q.worldedit.util.formatting.text.TextComponent;
@ -33,6 +34,8 @@
import com.sk89q.worldguard.WorldGuard; import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.domains.DefaultDomain; import com.sk89q.worldguard.domains.DefaultDomain;
import com.sk89q.worldguard.internal.permission.RegionPermissionModel; import com.sk89q.worldguard.internal.permission.RegionPermissionModel;
import com.sk89q.worldguard.protection.FlagValueCalculator;
import com.sk89q.worldguard.protection.association.RegionAssociable;
import com.sk89q.worldguard.protection.flags.Flags; import com.sk89q.worldguard.protection.flags.Flags;
import com.sk89q.worldguard.protection.managers.RegionManager; import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.ProtectedRegion; import com.sk89q.worldguard.protection.regions.ProtectedRegion;
@ -261,7 +264,8 @@ public Component getComponent(int number) {
.clickEvent(ClickEvent.of(ClickEvent.Action.RUN_COMMAND, .clickEvent(ClickEvent.of(ClickEvent.Action.RUN_COMMAND,
"/rg info -w \"" + world + "\" " + entry.region.getId())))); "/rg info -w \"" + world + "\" " + entry.region.getId()))));
} }
if (perms != null && entry.region.getFlag(Flags.TELE_LOC) != null && perms.mayTeleportTo(entry.region)) { final Location teleFlag = FlagValueCalculator.getEffectiveFlagOf(entry.region, Flags.TELE_LOC, perms.getSender() instanceof RegionAssociable ? (RegionAssociable) perms.getSender() : null);
if (perms != null && teleFlag != null && perms.mayTeleportTo(entry.region)) {
builder.append(TextComponent.space().append(TextComponent.of("[TP]", TextColor.GRAY) builder.append(TextComponent.space().append(TextComponent.of("[TP]", TextColor.GRAY)
.hoverEvent(HoverEvent.of(HoverEvent.Action.SHOW_TEXT, TextComponent.of("Click to teleport"))) .hoverEvent(HoverEvent.of(HoverEvent.Action.SHOW_TEXT, TextComponent.of("Click to teleport")))
.clickEvent(ClickEvent.of(ClickEvent.Action.RUN_COMMAND, .clickEvent(ClickEvent.of(ClickEvent.Action.RUN_COMMAND,

View File

@ -297,7 +297,12 @@ public <V, K> V queryMapValue(@Nullable RegionAssociable subject, MapFlag<K, V>
} }
@Nullable @Nullable
private <V, K> V getEffectiveMapValue(ProtectedRegion region, MapFlag<K, V> mapFlag, K key, RegionAssociable subject) { public <V, K> V getEffectiveMapValue(ProtectedRegion region, MapFlag<K, V> mapFlag, K key, RegionAssociable subject) {
return getEffectiveMapValueOf(region, mapFlag, key, subject);
}
@Nullable
public static <V, K> V getEffectiveMapValueOf(ProtectedRegion region, MapFlag<K, V> mapFlag, K key, RegionAssociable subject) {
List<ProtectedRegion> seen = new ArrayList<>(); List<ProtectedRegion> seen = new ArrayList<>();
ProtectedRegion current = region; ProtectedRegion current = region;
@ -450,7 +455,11 @@ && getEffectiveFlag(region, Flags.PASSTHROUGH, subject) != State.ALLOW) {
* @return the priority * @return the priority
*/ */
public int getPriority(final ProtectedRegion region) { public int getPriority(final ProtectedRegion region) {
if (region == globalRegion) { return getPriorityOf(region);
}
public static int getPriorityOf(final ProtectedRegion region) {
if (region.getId().equals(ProtectedRegion.GLOBAL_REGION)) {
return Integer.MIN_VALUE; return Integer.MIN_VALUE;
} else { } else {
return region.getPriority(); return region.getPriority();
@ -466,9 +475,15 @@ public int getPriority(final ProtectedRegion region) {
* @param subject an subject object * @param subject an subject object
* @return the value * @return the value
*/ */
@SuppressWarnings("unchecked") @Nullable
public <V> V getEffectiveFlag(final ProtectedRegion region, Flag<V> flag, @Nullable RegionAssociable subject) { public <V> V getEffectiveFlag(final ProtectedRegion region, Flag<V> flag, @Nullable RegionAssociable subject) {
if (region == globalRegion) { return getEffectiveFlagOf(region, flag, subject);
}
@SuppressWarnings("unchecked")
@Nullable
public static <V> V getEffectiveFlagOf(final ProtectedRegion region, Flag<V> flag, @Nullable RegionAssociable subject) {
if (region.getId().equals(ProtectedRegion.GLOBAL_REGION)) {
if (flag == Flags.PASSTHROUGH) { if (flag == Flags.PASSTHROUGH) {
// Has members/owners -> the global region acts like // Has members/owners -> the global region acts like
// a regular region without PASSTHROUGH // a regular region without PASSTHROUGH

View File

@ -22,6 +22,7 @@
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import com.sk89q.worldguard.domains.Association; import com.sk89q.worldguard.domains.Association;
import com.sk89q.worldguard.protection.FlagValueCalculator;
import com.sk89q.worldguard.protection.flags.Flags; import com.sk89q.worldguard.protection.flags.Flags;
import com.sk89q.worldguard.protection.regions.ProtectedRegion; import com.sk89q.worldguard.protection.regions.ProtectedRegion;
@ -69,7 +70,8 @@ private boolean checkNonplayerProtectionDomains(Iterable<? extends ProtectedRegi
} }
for (ProtectedRegion region : source) { for (ProtectedRegion region : source) {
Set<String> regionDomains = region.getFlag(Flags.NONPLAYER_PROTECTION_DOMAINS); // 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()) { if (regionDomains == null || regionDomains.isEmpty()) {
continue; continue;
@ -111,7 +113,8 @@ public Association getAssociation(List<ProtectedRegion> regions) {
source = this.source; source = this.source;
} }
if (checkNonplayerProtectionDomains(source, region.getFlag(Flags.NONPLAYER_PROTECTION_DOMAINS))) { // 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; return Association.OWNER;
} }

View File

@ -46,7 +46,7 @@ public final class Flags {
// Overrides membership check // Overrides membership check
public static final StateFlag PASSTHROUGH = register(new StateFlag("passthrough", false)); public static final StateFlag PASSTHROUGH = register(new StateFlag("passthrough", false));
public static final SetFlag<String> NONPLAYER_PROTECTION_DOMAINS = register(new SetFlag<>("nonplayer-protection-domains", new StringFlag(null))); public static final SetFlag<String> NONPLAYER_PROTECTION_DOMAINS = register(new SetFlag<>("nonplayer-protection-domains", null, new StringFlag(null)));
// This flag is unlike the others. It forces the checking of region membership // This flag is unlike the others. It forces the checking of region membership
public static final StateFlag BUILD = register(new BuildFlag("build", true)); public static final StateFlag BUILD = register(new BuildFlag("build", true));