Skip to content

Commit 8d5953a

Browse files
committed
apidomains: Added custom domains to WorldGuard
This change allows third party plugins to dynamically add custom domains to WorldGuard.
1 parent 129ae6c commit 8d5953a

File tree

23 files changed

+1077
-128
lines changed

23 files changed

+1077
-128
lines changed

worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/WorldGuardPlugin.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import com.sk89q.worldguard.commands.GeneralCommands;
6666
import com.sk89q.worldguard.commands.ProtectionCommands;
6767
import com.sk89q.worldguard.commands.ToggleCommands;
68+
import com.sk89q.worldguard.domains.registry.SimpleDomainRegistry;
6869
import com.sk89q.worldguard.protection.flags.Flag;
6970
import com.sk89q.worldguard.protection.flags.Flags;
7071
import com.sk89q.worldguard.protection.flags.registry.SimpleFlagRegistry;
@@ -212,6 +213,7 @@ public void onEnable() {
212213
});
213214

214215
((SimpleFlagRegistry) WorldGuard.getInstance().getFlagRegistry()).setInitialized(true);
216+
((SimpleDomainRegistry) WorldGuard.getInstance().getDomainRegistry()).setInitialized(true);
215217

216218
// Enable metrics
217219
final Metrics metrics = new Metrics(this, BSTATS_PLUGIN_ID); // bStats plugin id

worldguard-core/src/main/java/com/sk89q/worldguard/WorldGuard.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import com.google.common.util.concurrent.ListeningExecutorService;
2525
import com.google.common.util.concurrent.MoreExecutors;
2626
import com.sk89q.minecraft.util.commands.CommandException;
27+
import com.sk89q.worldguard.domains.registry.DomainRegistry;
28+
import com.sk89q.worldguard.domains.registry.SimpleDomainRegistry;
2729
import com.sk89q.worldguard.util.profile.cache.HashMapCache;
2830
import com.sk89q.worldguard.util.profile.cache.ProfileCache;
2931
import com.sk89q.worldguard.util.profile.cache.SQLiteCache;
@@ -55,6 +57,7 @@ public final class WorldGuard {
5557

5658
private WorldGuardPlatform platform;
5759
private final SimpleFlagRegistry flagRegistry = new SimpleFlagRegistry();
60+
private final SimpleDomainRegistry domainRegistry = new SimpleDomainRegistry();
5861
private final Supervisor supervisor = new SimpleSupervisor();
5962
private ProfileCache profileCache;
6063
private ProfileService profileService;
@@ -116,6 +119,16 @@ public FlagRegistry getFlagRegistry() {
116119
return this.flagRegistry;
117120
}
118121

122+
123+
/**
124+
* Get the domain registry.
125+
*
126+
* @return the domain registry
127+
*/
128+
public DomainRegistry getDomainRegistry() {
129+
return this.domainRegistry;
130+
}
131+
119132
/**
120133
* Get the supervisor.
121134
*
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* WorldGuard, a suite of tools for Minecraft
3+
* Copyright (C) sk89q <http://www.sk89q.com>
4+
* Copyright (C) WorldGuard team and contributors
5+
*
6+
* This program is free software: you can redistribute it and/or modify it
7+
* under the terms of the GNU Lesser General Public License as published by the
8+
* Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
14+
* for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
package com.sk89q.worldguard.commands;
21+
22+
import com.sk89q.worldedit.extension.platform.Actor;
23+
import com.sk89q.worldguard.LocalPlayer;
24+
import com.sk89q.worldguard.protection.flags.InvalidFlagFormat;
25+
26+
import javax.annotation.Nullable;
27+
import java.util.Map;
28+
29+
public abstract class CommandInputContext<T extends Exception> {
30+
protected final Actor sender;
31+
protected final String input;
32+
33+
protected Map<String, Object> context;
34+
35+
protected CommandInputContext(Actor sender, String input, Map<String, Object> values) {
36+
this.sender = sender;
37+
this.input = input;
38+
this.context = values;
39+
}
40+
41+
public void put(String name, Object value) {
42+
context.put(name, value);
43+
}
44+
45+
public Actor getSender() {
46+
return sender;
47+
}
48+
49+
public String getUserInput() {
50+
return input;
51+
}
52+
53+
/**
54+
* Gets the CommandSender as a player.
55+
*
56+
* @return Player
57+
* @throws T if the sender is not a player
58+
*/
59+
public LocalPlayer getPlayerSender() throws T {
60+
if (sender.isPlayer() && sender instanceof LocalPlayer) {
61+
return (LocalPlayer) sender;
62+
} else {
63+
throw createException("Not a player");
64+
}
65+
}
66+
67+
public Integer getUserInputAsInt() throws T {
68+
try {
69+
return Integer.parseInt(input);
70+
} catch (NumberFormatException e) {
71+
throw createException("Not a number: " + input);
72+
}
73+
}
74+
75+
public Double getUserInputAsDouble() throws T {
76+
try {
77+
return Double.parseDouble(input);
78+
} catch (NumberFormatException e) {
79+
throw createException("Not a number: " + input);
80+
}
81+
}
82+
83+
protected abstract T createException(String str);
84+
85+
/**
86+
* Get an object from the context by key name.
87+
* May return null if the object does not exist in the context.
88+
*
89+
* @param name key name of the object
90+
* @return the object matching the key, or null
91+
*/
92+
@Nullable
93+
public Object get(String name) {
94+
return get(name, null);
95+
}
96+
97+
/**
98+
* Get an object from the context by key name.
99+
* Will only return null if
100+
* a) you provide null as the default
101+
* b) the key has explicity been set to null
102+
*
103+
* @param name key name of the object
104+
* @return the object matching the key
105+
*/
106+
@Nullable
107+
public Object get(String name, Object defaultValue) {
108+
Object obj;
109+
return (((obj = context.get(name)) != null) || context.containsKey(name)
110+
? obj : defaultValue);
111+
}
112+
}

worldguard-core/src/main/java/com/sk89q/worldguard/commands/region/MemberCommands.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,27 @@
2525
import com.sk89q.minecraft.util.commands.CommandPermissionsException;
2626
import com.sk89q.worldedit.command.util.AsyncCommandBuilder;
2727
import com.sk89q.worldedit.extension.platform.Actor;
28-
import com.sk89q.worldedit.util.auth.AuthorizationException;
28+
import com.sk89q.worldedit.util.formatting.component.ErrorFormat;
29+
import com.sk89q.worldedit.util.formatting.text.Component;
30+
import com.sk89q.worldedit.util.formatting.text.TextComponent;
31+
import com.sk89q.worldedit.util.formatting.text.event.ClickEvent;
32+
import com.sk89q.worldedit.util.formatting.text.event.HoverEvent;
33+
import com.sk89q.worldedit.util.formatting.text.format.TextColor;
2934
import com.sk89q.worldedit.world.World;
3035
import com.sk89q.worldguard.LocalPlayer;
3136
import com.sk89q.worldguard.WorldGuard;
3237
import com.sk89q.worldguard.domains.DefaultDomain;
38+
import com.sk89q.worldguard.domains.registry.DomainFactory;
39+
import com.sk89q.worldguard.domains.registry.DomainRegistry;
40+
import com.sk89q.worldguard.internal.permission.RegionPermissionModel;
3341
import com.sk89q.worldguard.protection.managers.RegionManager;
3442
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
3543
import com.sk89q.worldguard.protection.util.DomainInputResolver;
3644
import com.sk89q.worldguard.protection.util.DomainInputResolver.UserLocatorPolicy;
3745

46+
import java.util.ArrayList;
47+
import java.util.Collections;
48+
import java.util.Map;
3849
import java.util.concurrent.Callable;
3950

4051
public class MemberCommands extends RegionCommandsBase {
@@ -67,6 +78,8 @@ public void addMember(CommandContext args, Actor sender) throws CommandException
6778
DomainInputResolver resolver = new DomainInputResolver(
6879
WorldGuard.getInstance().getProfileService(), args.getParsedPaddedSlice(1, 0));
6980
resolver.setLocatorPolicy(args.hasFlag('n') ? UserLocatorPolicy.NAME_ONLY : UserLocatorPolicy.UUID_ONLY);
81+
resolver.setActor(sender);
82+
resolver.setRegion(region);
7083

7184

7285
final String description = String.format("Adding members to the region '%s' on '%s'", region.getId(), world.getName());
@@ -101,7 +114,8 @@ public void addOwner(CommandContext args, Actor sender) throws CommandException
101114
DomainInputResolver resolver = new DomainInputResolver(
102115
WorldGuard.getInstance().getProfileService(), args.getParsedPaddedSlice(1, 0));
103116
resolver.setLocatorPolicy(args.hasFlag('n') ? UserLocatorPolicy.NAME_ONLY : UserLocatorPolicy.UUID_ONLY);
104-
117+
resolver.setActor(sender);
118+
resolver.setRegion(region);
105119

106120
final String description = String.format("Adding owners to the region '%s' on '%s'", region.getId(), world.getName());
107121
AsyncCommandBuilder.wrap(checkedAddOwners(sender, manager, region, world, resolver), sender)
@@ -174,6 +188,8 @@ public void removeMember(CommandContext args, Actor sender) throws CommandExcept
174188
DomainInputResolver resolver = new DomainInputResolver(
175189
WorldGuard.getInstance().getProfileService(), args.getParsedPaddedSlice(1, 0));
176190
resolver.setLocatorPolicy(args.hasFlag('n') ? UserLocatorPolicy.NAME_ONLY : UserLocatorPolicy.UUID_AND_NAME);
191+
resolver.setActor(sender);
192+
resolver.setRegion(region);
177193

178194
callable = resolver;
179195
}
@@ -217,6 +233,8 @@ public void removeOwner(CommandContext args, Actor sender) throws CommandExcepti
217233
DomainInputResolver resolver = new DomainInputResolver(
218234
WorldGuard.getInstance().getProfileService(), args.getParsedPaddedSlice(1, 0));
219235
resolver.setLocatorPolicy(args.hasFlag('n') ? UserLocatorPolicy.NAME_ONLY : UserLocatorPolicy.UUID_AND_NAME);
236+
resolver.setActor(sender);
237+
resolver.setRegion(region);
220238

221239
callable = resolver;
222240
}

worldguard-core/src/main/java/com/sk89q/worldguard/commands/region/RegionCommands.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public void define(CommandContext args, Actor sender) throws CommandException {
160160
region = checkRegionFromSelection(sender, id);
161161
}
162162

163-
RegionAdder task = new RegionAdder(manager, region);
163+
RegionAdder task = new RegionAdder(manager, region, sender);
164164
task.addOwnersFromCommand(args, 2);
165165

166166
final String description = String.format("Adding region '%s'", region.getId());
@@ -214,7 +214,7 @@ public void redefine(CommandContext args, Actor sender) throws CommandException
214214

215215
region.copyFrom(existing);
216216

217-
RegionAdder task = new RegionAdder(manager, region);
217+
RegionAdder task = new RegionAdder(manager, region, sender);
218218

219219
final String description = String.format("Updating region '%s'", region.getId());
220220
AsyncCommandBuilder.wrap(task, sender)

worldguard-core/src/main/java/com/sk89q/worldguard/commands/region/RegionCommandsBase.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,19 @@
3232
import com.sk89q.worldedit.regions.Polygonal2DRegion;
3333
import com.sk89q.worldedit.regions.Region;
3434
import com.sk89q.worldedit.regions.RegionSelector;
35-
import com.sk89q.worldedit.regions.selector.CuboidRegionSelector;
36-
import com.sk89q.worldedit.regions.selector.Polygonal2DRegionSelector;
3735
import com.sk89q.worldedit.util.formatting.component.ErrorFormat;
3836
import com.sk89q.worldedit.util.formatting.component.SubtleFormat;
3937
import com.sk89q.worldedit.util.formatting.text.TextComponent;
4038
import com.sk89q.worldedit.util.formatting.text.event.ClickEvent;
4139
import com.sk89q.worldedit.util.formatting.text.event.HoverEvent;
4240
import com.sk89q.worldedit.util.formatting.text.format.TextColor;
43-
import com.sk89q.worldedit.util.formatting.text.format.TextDecoration;
4441
import com.sk89q.worldedit.world.World;
4542
import com.sk89q.worldguard.LocalPlayer;
4643
import com.sk89q.worldguard.WorldGuard;
44+
import com.sk89q.worldguard.domains.CustomDomain;
45+
import com.sk89q.worldguard.domains.DefaultDomain;
46+
import com.sk89q.worldguard.domains.registry.CustomDomainContext;
47+
import com.sk89q.worldguard.domains.registry.InvalidDomainFormat;
4748
import com.sk89q.worldguard.internal.permission.RegionPermissionModel;
4849
import com.sk89q.worldguard.protection.ApplicableRegionSet;
4950
import com.sk89q.worldguard.protection.flags.Flag;

worldguard-core/src/main/java/com/sk89q/worldguard/commands/task/RegionAdder.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package com.sk89q.worldguard.commands.task;
2121

2222
import com.sk89q.minecraft.util.commands.CommandContext;
23+
import com.sk89q.worldedit.extension.platform.Actor;
2324
import com.sk89q.worldguard.WorldGuard;
2425
import com.sk89q.worldguard.domains.DefaultDomain;
2526
import com.sk89q.worldguard.protection.managers.RegionManager;
@@ -39,22 +40,34 @@ public class RegionAdder implements Callable<ProtectedRegion> {
3940

4041
private final RegionManager manager;
4142
private final ProtectedRegion region;
43+
private final Actor actor;
4244
@Nullable
4345
private String[] ownersInput;
4446
private UserLocatorPolicy locatorPolicy = UserLocatorPolicy.UUID_ONLY;
4547

4648
/**
4749
* Create a new instance.
4850
*
49-
* @param manager the manage
51+
* @param manager the manager
5052
* @param region the region
5153
*/
5254
public RegionAdder(RegionManager manager, ProtectedRegion region) {
55+
this(manager, region, null);
56+
}
57+
58+
/**
59+
* Create a new instance.
60+
* @param manager the manager
61+
* @param region the region
62+
* @param actor the actor
63+
*/
64+
public RegionAdder(RegionManager manager, ProtectedRegion region, Actor actor) {
5365
checkNotNull(manager);
5466
checkNotNull(region);
5567

5668
this.manager = manager;
5769
this.region = region;
70+
this.actor = actor;
5871
}
5972

6073
/**
@@ -75,6 +88,9 @@ public ProtectedRegion call() throws Exception {
7588
if (ownersInput != null) {
7689
DomainInputResolver resolver = new DomainInputResolver(WorldGuard.getInstance().getProfileService(), ownersInput);
7790
resolver.setLocatorPolicy(locatorPolicy);
91+
resolver.setActor(actor);
92+
resolver.setRegion(region);
93+
7894
DefaultDomain domain = resolver.call();
7995
region.getOwners().addAll(domain);
8096
}

0 commit comments

Comments
 (0)