improve command input safety
This commit is contained in:
parent
e9e2bfb48b
commit
0069b09131
@ -50,14 +50,14 @@ public class OptVillagersRadius implements VillagerOptimizerCommand, TabComplete
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
|
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
|
||||||
if (!(sender instanceof Player player)) {
|
if (!sender.hasPermission(Commands.OPTIMIZE_RADIUS.get())) {
|
||||||
sender.sendMessage(Component.text("This command can only be executed by a player.")
|
sender.sendMessage(VillagerOptimizer.getLang(sender).no_permission);
|
||||||
.color(NamedTextColor.RED).decorate(TextDecoration.BOLD));
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!sender.hasPermission(Commands.OPTIMIZE_RADIUS.get())) {
|
if (!(sender instanceof Player player)) {
|
||||||
sender.sendMessage(VillagerOptimizer.getLang(sender).no_permission);
|
sender.sendMessage(Component.text("This command can only be executed by a player.")
|
||||||
|
.color(NamedTextColor.RED).decorate(TextDecoration.BOLD));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,9 +67,16 @@ public class OptVillagersRadius implements VillagerOptimizerCommand, TabComplete
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
int specifiedRadius = Integer.parseInt(args[0]);
|
final int specifiedRadius = Integer.parseInt(args[0]);
|
||||||
|
// Turn negative numbers into positive ones
|
||||||
|
final int safeRadius = (int) Math.sqrt(specifiedRadius * specifiedRadius);
|
||||||
|
|
||||||
if (specifiedRadius > max_radius) {
|
if (safeRadius == 0) {
|
||||||
|
VillagerOptimizer.getLang(player.locale()).command_radius_invalid.forEach(player::sendMessage);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (safeRadius > max_radius) {
|
||||||
final TextReplacementConfig limit = TextReplacementConfig.builder()
|
final TextReplacementConfig limit = TextReplacementConfig.builder()
|
||||||
.matchLiteral("%distance%")
|
.matchLiteral("%distance%")
|
||||||
.replacement(Integer.toString(max_radius))
|
.replacement(Integer.toString(max_radius))
|
||||||
@ -83,7 +90,7 @@ public class OptVillagersRadius implements VillagerOptimizerCommand, TabComplete
|
|||||||
int failCount = 0;
|
int failCount = 0;
|
||||||
final boolean player_has_cooldown_bypass = player.hasPermission(Bypass.COMMAND_COOLDOWN.get());
|
final boolean player_has_cooldown_bypass = player.hasPermission(Bypass.COMMAND_COOLDOWN.get());
|
||||||
|
|
||||||
for (Entity entity : player.getNearbyEntities(specifiedRadius, specifiedRadius, specifiedRadius)) {
|
for (Entity entity : player.getNearbyEntities(safeRadius, safeRadius, safeRadius)) {
|
||||||
if (!entity.getType().equals(EntityType.VILLAGER)) continue;
|
if (!entity.getType().equals(EntityType.VILLAGER)) continue;
|
||||||
Villager villager = (Villager) entity;
|
Villager villager = (Villager) entity;
|
||||||
Villager.Profession profession = villager.getProfession();
|
Villager.Profession profession = villager.getProfession();
|
||||||
@ -106,7 +113,7 @@ public class OptVillagersRadius implements VillagerOptimizerCommand, TabComplete
|
|||||||
if (successCount <= 0 && failCount <= 0) {
|
if (successCount <= 0 && failCount <= 0) {
|
||||||
final TextReplacementConfig radius = TextReplacementConfig.builder()
|
final TextReplacementConfig radius = TextReplacementConfig.builder()
|
||||||
.matchLiteral("%radius%")
|
.matchLiteral("%radius%")
|
||||||
.replacement(Integer.toString(specifiedRadius))
|
.replacement(Integer.toString(safeRadius))
|
||||||
.build();
|
.build();
|
||||||
VillagerOptimizer.getLang(player.locale()).command_no_villagers_nearby.forEach(line -> player.sendMessage(line.replaceText(radius)));
|
VillagerOptimizer.getLang(player.locale()).command_no_villagers_nearby.forEach(line -> player.sendMessage(line.replaceText(radius)));
|
||||||
return true;
|
return true;
|
||||||
@ -119,7 +126,7 @@ public class OptVillagersRadius implements VillagerOptimizerCommand, TabComplete
|
|||||||
.build();
|
.build();
|
||||||
final TextReplacementConfig radius = TextReplacementConfig.builder()
|
final TextReplacementConfig radius = TextReplacementConfig.builder()
|
||||||
.matchLiteral("%radius%")
|
.matchLiteral("%radius%")
|
||||||
.replacement(Integer.toString(specifiedRadius))
|
.replacement(Integer.toString(safeRadius))
|
||||||
.build();
|
.build();
|
||||||
VillagerOptimizer.getLang(player.locale()).command_optimize_success.forEach(line -> player.sendMessage(line
|
VillagerOptimizer.getLang(player.locale()).command_optimize_success.forEach(line -> player.sendMessage(line
|
||||||
.replaceText(success_amount)
|
.replaceText(success_amount)
|
||||||
|
@ -43,14 +43,14 @@ public class UnOptVillagersRadius implements VillagerOptimizerCommand, TabComple
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
|
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
|
||||||
if (!(sender instanceof Player player)) {
|
if (!sender.hasPermission(Commands.UNOPTIMIZE_RADIUS.get())) {
|
||||||
sender.sendMessage(Component.text("This command can only be executed by a player.")
|
sender.sendMessage(VillagerOptimizer.getLang(sender).no_permission);
|
||||||
.color(NamedTextColor.RED).decorate(TextDecoration.BOLD));
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!sender.hasPermission(Commands.UNOPTIMIZE_RADIUS.get())) {
|
if (!(sender instanceof Player player)) {
|
||||||
sender.sendMessage(VillagerOptimizer.getLang(sender).no_permission);
|
sender.sendMessage(Component.text("This command can only be executed by a player.")
|
||||||
|
.color(NamedTextColor.RED).decorate(TextDecoration.BOLD));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,9 +60,16 @@ public class UnOptVillagersRadius implements VillagerOptimizerCommand, TabComple
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
int specifiedRadius = Integer.parseInt(args[0]);
|
final int specifiedRadius = Integer.parseInt(args[0]);
|
||||||
|
// Turn negative numbers into positive ones
|
||||||
|
final int safeRadius = (int) Math.sqrt(specifiedRadius * specifiedRadius);
|
||||||
|
|
||||||
if (specifiedRadius > max_radius) {
|
if (safeRadius == 0) {
|
||||||
|
VillagerOptimizer.getLang(player.locale()).command_radius_invalid.forEach(player::sendMessage);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (safeRadius > max_radius) {
|
||||||
final TextReplacementConfig limit = TextReplacementConfig.builder()
|
final TextReplacementConfig limit = TextReplacementConfig.builder()
|
||||||
.matchLiteral("%distance%")
|
.matchLiteral("%distance%")
|
||||||
.replacement(Integer.toString(max_radius))
|
.replacement(Integer.toString(max_radius))
|
||||||
@ -74,7 +81,7 @@ public class UnOptVillagersRadius implements VillagerOptimizerCommand, TabComple
|
|||||||
VillagerCache villagerCache = VillagerOptimizer.getCache();
|
VillagerCache villagerCache = VillagerOptimizer.getCache();
|
||||||
int successCount = 0;
|
int successCount = 0;
|
||||||
|
|
||||||
for (Entity entity : player.getNearbyEntities(specifiedRadius, specifiedRadius, specifiedRadius)) {
|
for (Entity entity : player.getNearbyEntities(safeRadius, safeRadius, safeRadius)) {
|
||||||
if (!entity.getType().equals(EntityType.VILLAGER)) continue;
|
if (!entity.getType().equals(EntityType.VILLAGER)) continue;
|
||||||
Villager villager = (Villager) entity;
|
Villager villager = (Villager) entity;
|
||||||
Villager.Profession profession = villager.getProfession();
|
Villager.Profession profession = villager.getProfession();
|
||||||
@ -94,7 +101,7 @@ public class UnOptVillagersRadius implements VillagerOptimizerCommand, TabComple
|
|||||||
if (successCount <= 0) {
|
if (successCount <= 0) {
|
||||||
final TextReplacementConfig radius = TextReplacementConfig.builder()
|
final TextReplacementConfig radius = TextReplacementConfig.builder()
|
||||||
.matchLiteral("%radius%")
|
.matchLiteral("%radius%")
|
||||||
.replacement(Integer.toString(specifiedRadius))
|
.replacement(Integer.toString(safeRadius))
|
||||||
.build();
|
.build();
|
||||||
VillagerOptimizer.getLang(player.locale()).command_no_villagers_nearby.forEach(line -> player.sendMessage(line.replaceText(radius)));
|
VillagerOptimizer.getLang(player.locale()).command_no_villagers_nearby.forEach(line -> player.sendMessage(line.replaceText(radius)));
|
||||||
} else {
|
} else {
|
||||||
@ -104,7 +111,7 @@ public class UnOptVillagersRadius implements VillagerOptimizerCommand, TabComple
|
|||||||
.build();
|
.build();
|
||||||
final TextReplacementConfig radius = TextReplacementConfig.builder()
|
final TextReplacementConfig radius = TextReplacementConfig.builder()
|
||||||
.matchLiteral("%radius%")
|
.matchLiteral("%radius%")
|
||||||
.replacement(Integer.toString(specifiedRadius))
|
.replacement(Integer.toString(safeRadius))
|
||||||
.build();
|
.build();
|
||||||
VillagerOptimizer.getLang(player.locale()).command_unoptimize_success.forEach(line -> player.sendMessage(line
|
VillagerOptimizer.getLang(player.locale()).command_unoptimize_success.forEach(line -> player.sendMessage(line
|
||||||
.replaceText(success_amount)
|
.replaceText(success_amount)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user