Skip to content

Commit b4e8cef

Browse files
committed
alphanumeric names only
1 parent beed7cd commit b4e8cef

5 files changed

Lines changed: 24 additions & 1 deletion

File tree

realty-paper-api/src/main/java/io/github/md5sha256/realty/api/SignTextApplicator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public ApplyResult applySignTextIfLoaded(@NotNull World world,
111111
if (!world.isChunkLoaded(chunkX, chunkZ)) {
112112
return ApplyResult.BLOCK_NOT_LOADED;
113113
}
114-
Block block = world.getBlockAt(blockX, blockY, blockZ);
114+
Block block = world.getBlockAt(blockX, blockY, blockZ);git
115115
if (!(block.getState(false) instanceof Sign sign)) {
116116
return ApplyResult.FAILED;
117117
}

realty-paper/src/main/java/io/github/md5sha256/realty/command/CreateCommand.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.util.Objects;
4141
import java.util.UUID;
4242
import java.util.concurrent.atomic.AtomicReference;
43+
import java.util.regex.Pattern;
4344

4445
/**
4546
* Handles {@code /realty create leasehold <name> <price> <period> <maxextensions>}
@@ -54,6 +55,7 @@ public record CreateCommand(@NotNull RealtyPaperApi api,
5455
@NotNull MessageContainer messages) implements CustomCommandBean {
5556

5657
private static final CloudKey<String> NAME = CloudKey.of("name", String.class);
58+
private static final Pattern VALID_NAME_PATTERN = Pattern.compile("^[A-Za-z0-9]+$");
5759
private static final CloudKey<Double> PRICE = CloudKey.of("price", Double.class);
5860
private static final CloudKey<Duration> PERIOD = CloudKey.of("period", Duration.class);
5961
private static final CloudKey<Integer> MAX_EXTENSIONS = CloudKey.of("maxextensions", Integer.class);
@@ -110,6 +112,11 @@ private void executeLeasehold(@NotNull CommandContext<Source> ctx) {
110112
return;
111113
}
112114
String name = ctx.get(NAME);
115+
if (!VALID_NAME_PATTERN.matcher(name).matches()) {
116+
player.sendMessage(messages.messageFor(MessageKeys.CREATE_INVALID_NAME,
117+
Placeholder.unparsed("region", name)));
118+
return;
119+
}
113120
double price = ctx.get(PRICE);
114121
Duration period = ctx.get(PERIOD);
115122
int maxExtensions = ctx.get(MAX_EXTENSIONS);
@@ -168,6 +175,11 @@ private void executeFreehold(@NotNull CommandContext<Source> ctx) {
168175
return;
169176
}
170177
String name = ctx.get(NAME);
178+
if (!VALID_NAME_PATTERN.matcher(name).matches()) {
179+
player.sendMessage(messages.messageFor(MessageKeys.CREATE_INVALID_NAME,
180+
Placeholder.unparsed("region", name)));
181+
return;
182+
}
171183
Double price = ctx.flags().getValue(PRICE_FLAG, null);
172184
UUID authority = ctx.flags()
173185
.getValue(AUTHORITY_FLAG, settings.get().defaultFreeholdAuthority());

realty-paper/src/main/java/io/github/md5sha256/realty/command/SubregionCommandGroup.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.Objects;
3939
import java.util.UUID;
4040
import java.util.concurrent.atomic.AtomicReference;
41+
import java.util.regex.Pattern;
4142

4243
public record SubregionCommandGroup(
4344
@NotNull RealtyPaperApi api,
@@ -50,6 +51,7 @@ public record SubregionCommandGroup(
5051
private static final CloudKey<String> NAME = CloudKey.of("name", String.class);
5152
private static final CloudKey<Double> PRICE = CloudKey.of("price", Double.class);
5253
private static final CloudKey<Duration> DURATION = CloudKey.of("duration", Duration.class);
54+
private static final Pattern VALID_NAME_PATTERN = Pattern.compile("^[A-Za-z0-9]+$");
5355

5456
@Override
5557
public @NotNull List<Command<? extends Source>> commands(
@@ -87,6 +89,11 @@ private void executeQuickCreate(@NotNull CommandContext<Source> ctx) {
8789
}
8890
WorldGuardRegion parentRegion = ctx.get(PARENT_REGION);
8991
String name = ctx.get(NAME);
92+
if (!VALID_NAME_PATTERN.matcher(name).matches()) {
93+
player.sendMessage(messages.messageFor(MessageKeys.SUBREGION_INVALID_NAME,
94+
Placeholder.unparsed("region", name)));
95+
return;
96+
}
9097
double price = ctx.get(PRICE);
9198
Duration duration = ctx.get(DURATION);
9299
boolean canBypass = player.hasPermission(BYPASS_PERMISSION);

realty-paper/src/main/java/io/github/md5sha256/realty/localisation/MessageKeys.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ private MessageKeys() {}
107107
public static final String CREATE_FREEHOLD_SUCCESS = "create.freehold-success";
108108
public static final String CREATE_ALREADY_REGISTERED = "create.already-registered";
109109
public static final String CREATE_REGION_EXISTS = "create.region-exists";
110+
public static final String CREATE_INVALID_NAME = "create.invalid-name";
110111
public static final String CREATE_INCOMPLETE_SELECTION = "create.incomplete-selection";
111112
public static final String CREATE_ERROR = "create.error";
112113

@@ -369,6 +370,7 @@ private MessageKeys() {}
369370
public static final String SUBREGION_CREATE_SUCCESS = "subregion.create-success";
370371
public static final String SUBREGION_CREATE_ERROR = "subregion.create-error";
371372
public static final String SUBREGION_REGION_EXISTS = "subregion.region-exists";
373+
public static final String SUBREGION_INVALID_NAME = "subregion.invalid-name";
372374
public static final String SUBREGION_NO_FREEHOLD = "subregion.no-freehold";
373375
public static final String SUBREGION_NOT_TITLEHOLDER = "subregion.not-titleholder";
374376
public static final String SUBREGION_WRONG_WORLD = "subregion.wrong-world";

realty-paper/src/main/resources/messages.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ create:
9090
freehold-success: "Created WorldGuard region <region> and registered it as freehold!"
9191
already-registered: "Region <region> is already registered in Realty!"
9292
region-exists: "A WorldGuard region named <region> already exists!"
93+
invalid-name: "Region name <region> is invalid. Names must be alphanumeric only."
9394
incomplete-selection: "You must make a WorldEdit selection first!"
9495
error: "Failed to create region: <error>"
9596

@@ -459,6 +460,7 @@ subregion:
459460
create-success: "Subregion <region> created as a child of <parent>."
460461
create-error: "Failed to create subregion: <error>"
461462
region-exists: "A region named <region> already exists."
463+
invalid-name: "Region name <region> is invalid. Names must be alphanumeric only."
462464
no-freehold: "Region <region> is not a freehold region."
463465
not-titleholder: "You are not the titleholder of region <region>."
464466
wrong-world: "Your WorldEdit selection is in a different world than the parent region."

0 commit comments

Comments
 (0)