Skip to content

Commit 75458de

Browse files
committed
Add world-permission flag
1 parent 5af676e commit 75458de

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

src/main/java/com/intellectualsites/hyperverse/configuration/Messages.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ public class Messages {
9898
"&cThe world could not be removed. Reason: %reason%");
9999
public static final Message messageWorldRemoved = createMessage("world.removed",
100100
"&7The world was removed successfully");
101+
public static final Message messageNotPermittedEntry = createMessage("world.not_permitted",
102+
"&cYou are not allowed to enter that world");
101103

102104
// Flag descriptions
103105
public static final Message flagDescriptionGamemode = createMessage("flags.gamemode",
@@ -111,6 +113,8 @@ public class Messages {
111113
"Whether or not player vs. player combat is enabled");
112114
public static final Message flagDescriptionPve = createMessage("flags.pve",
113115
"Whether or not player vs. entity combat is enabled");
116+
public static final Message flagWorldPermission = createMessage("flags.world-permission",
117+
"Permission node required to visit the world");
114118

115119
public static Message createMessage(@NotNull final String key,
116120
@NotNull final String defaultValue) {
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//
2+
// Hyperverse - A minecraft world management plugin
3+
// Copyright © 2020 Alexander Söderberg ([email protected])
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
//
18+
19+
package com.intellectualsites.hyperverse.flags.implementation;
20+
21+
import com.intellectualsites.hyperverse.configuration.Messages;
22+
import com.intellectualsites.hyperverse.flags.FlagParseException;
23+
import com.intellectualsites.hyperverse.flags.WorldFlag;
24+
import org.jetbrains.annotations.NotNull;
25+
26+
import java.util.regex.Pattern;
27+
28+
public class WorldPermissionFlag extends WorldFlag<String, WorldPermissionFlag> {
29+
30+
private static final Pattern permissionPattern = Pattern.compile("[A-Za-z0-9\\-_.]+");
31+
public static final WorldPermissionFlag WORLD_PERMISSION_FLAG_DEFAULT = new WorldPermissionFlag("");
32+
33+
public WorldPermissionFlag(@NotNull final String value) {
34+
super(value, Messages.flagWorldPermission);
35+
}
36+
37+
@Override public WorldPermissionFlag parse(@NotNull final String input) throws FlagParseException {
38+
if (input.isEmpty()) {
39+
return WORLD_PERMISSION_FLAG_DEFAULT;
40+
}
41+
if (permissionPattern.matcher(input).matches()) {
42+
return flagOf(input);
43+
}
44+
throw new FlagParseException(this, input, "A permission node may only contain alphanumerical characters,"
45+
+ " -, . and _");
46+
}
47+
48+
@Override public WorldPermissionFlag merge(@NotNull final String newValue) {
49+
return flagOf(newValue);
50+
}
51+
52+
@Override public String toString() {
53+
return getValue();
54+
}
55+
56+
@Override public String getExample() {
57+
return "your.permission.node";
58+
}
59+
60+
@Override protected WorldPermissionFlag flagOf(@NotNull final String value) {
61+
return new WorldPermissionFlag(value);
62+
}
63+
64+
}

src/main/java/com/intellectualsites/hyperverse/world/SimpleWorld.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.intellectualsites.hyperverse.flags.FlagParseException;
3131
import com.intellectualsites.hyperverse.flags.GlobalWorldFlagContainer;
3232
import com.intellectualsites.hyperverse.flags.WorldFlag;
33+
import com.intellectualsites.hyperverse.flags.implementation.WorldPermissionFlag;
3334
import com.intellectualsites.hyperverse.modules.FlagContainerFactory;
3435
import com.intellectualsites.hyperverse.modules.HyperWorldCreatorFactory;
3536
import com.intellectualsites.hyperverse.util.MessageUtil;
@@ -256,10 +257,19 @@ public class SimpleWorld implements HyperWorld {
256257
throw new IllegalStateException(
257258
"Cannot teleport a player to a world before it has been generated");
258259
}
260+
259261
if (player.getWorld().equals(this.bukkitWorld)) {
260262
return;
261263
}
262264

265+
if (!this.getFlag(WorldPermissionFlag.class).isEmpty()) {
266+
final String permission = getFlag(WorldPermissionFlag.class);
267+
if (!player.hasPermission(permission)) {
268+
MessageUtil.sendMessage(player, Messages.messageNotPermittedEntry);
269+
return;
270+
}
271+
}
272+
263273
final Location location;
264274
if (this.hyperConfiguration.shouldPersistLocations()) {
265275
location = this.hyperDatabase.getLocation(player.getUniqueId(),

0 commit comments

Comments
 (0)