Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ignore non-existent worlds when choosing a free sign #1371

Merged
merged 2 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Predicate;
import lombok.NonNull;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -190,10 +191,18 @@ public void handleInternalSignRemove(@NonNull WorldPosition position) {
.target(ChannelMessageTarget.Type.NODE, this.wrapperConfig.serviceConfiguration().serviceId().nodeUniqueId());
}

public int removeMissingSigns() {
public int removeAllMissingSigns() {
return this.removeMissingSigns(sign -> true);
}

public int removeMissingSigns(@NonNull String world) {
return this.removeMissingSigns(sign -> sign.base().location().world().equalsIgnoreCase(world));
}

public int removeMissingSigns(@NonNull Predicate<PlatformSign<P, C>> filter) {
var removed = 0;
for (var sign : this.platformSigns.values()) {
if (!sign.exists()) {
if (filter.test(sign) && !sign.exists()) {
this.deleteSign(sign.base());
removed++;
}
Expand Down Expand Up @@ -381,6 +390,10 @@ protected void tick(@NonNull Map<SignLayoutsHolder, Set<PlatformSign<P, C>>> sig
try {
PlatformSign<P, C> bestChoice = null;
for (var platformSign : this.platformSigns.values()) {
if (!platformSign.exists()) {
continue;
}

var sign = platformSign.base();
if (snapshot.configuration().groups().contains(sign.targetGroup())
&& (sign.templatePath() == null || this.checkTemplatePath(snapshot, sign))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,18 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
}

return true;
} else if (args.length == 1 && args[0].equalsIgnoreCase("cleanup")) {
} else if (args.length == 1 && args[0].equalsIgnoreCase("cleanupall")) {
// removes all signs on which location is not a sign anymore
var removed = this.signManagement.removeMissingSigns();
var removed = this.signManagement.removeAllMissingSigns();
SignsConfiguration.sendMessage(
"command-cloudsign-cleanup-success",
player::sendMessage,
m -> m.replace("%amount%", Integer.toString(removed)));
return true;
} else if ((args.length == 1 || args.length == 2) && args[0].equalsIgnoreCase("cleanup")) {
var world = args.length == 2 ? args[1] : player.getWorld().getName();
// removes all signs on which location is not a sign anymore
var removed = this.signManagement.removeMissingSigns(world);
SignsConfiguration.sendMessage(
"command-cloudsign-cleanup-success",
player::sendMessage,
Expand Down Expand Up @@ -134,7 +143,8 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
sender.sendMessage("§7/cloudsigns create <targetGroup> [templatePath]");
sender.sendMessage("§7/cloudsigns remove");
sender.sendMessage("§7/cloudsigns removeAll");
sender.sendMessage("§7/cloudsigns cleanup");
sender.sendMessage("§7/cloudsigns cleanup [world]");
sender.sendMessage("§7/cloudsigns cleanupAll");
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class SignsCommand extends Command {
private static final ArgumentLiteral REMOVE_LITERAL = new ArgumentLiteral("remove");
private static final ArgumentLiteral REMOVE_ALL_LITERAL = new ArgumentLiteral("removeAll");
private static final ArgumentLiteral CLEANUP_LITERAL = new ArgumentLiteral("cleanup");
private static final ArgumentLiteral CLEANUP_ALL_LITERAL = new ArgumentLiteral("cleanupAll");

private static final CommandCondition DEFAULT_CONDITION = (sender, $) ->
sender instanceof Player && !(sender instanceof FakePlayer) && sender.hasPermission("cloudnet.command.cloudsign");
Expand All @@ -59,6 +60,7 @@ public class SignsCommand extends Command {
}.setDefaultValue(() -> null);

private final Argument<String> targetGroup;
private final Argument<String> world;
private final MinestomSignManagement signManagement;

@Inject
Expand All @@ -69,14 +71,16 @@ public SignsCommand(
super("cloudsign", "cs", "signs", "cloudsigns");

this.targetGroup = this.createTargetGroupArgument(groupProvider);
this.world = new ArgumentString("world");
this.signManagement = signManagement;

var createLiteral = new ArgumentLiteral("create");

this.addConditionalSyntax(DEFAULT_CONDITION, this::handleCreate, createLiteral, this.targetGroup, this.template);
this.addConditionalSyntax(DEFAULT_CONDITION, this::handleRemove, REMOVE_LITERAL);
this.addConditionalSyntax(DEFAULT_CONDITION, this::handleRemoveAll, REMOVE_ALL_LITERAL);
this.addConditionalSyntax(DEFAULT_CONDITION, this::handleCleanup, CLEANUP_LITERAL);
this.addConditionalSyntax(DEFAULT_CONDITION, this::handleCleanup, CLEANUP_LITERAL, this.world);
this.addConditionalSyntax(DEFAULT_CONDITION, this::handleCleanupAll, CLEANUP_ALL_LITERAL);
}

private @NonNull ArgumentString createTargetGroupArgument(@NonNull GroupConfigurationProvider groupProvider) {
Expand Down Expand Up @@ -178,8 +182,18 @@ private void handleRemoveAll(@NonNull CommandSender sender, @NonNull CommandCont
}

private void handleCleanup(@NonNull CommandSender sender, @NonNull CommandContext context) {
var world = context.getOrDefault(this.world, ((Player) sender).getInstance().getUniqueId().toString());
// removes all signs on which location is not a sign anymore
var removed = this.signManagement.removeMissingSigns();
var removed = this.signManagement.removeMissingSigns(world);
SignsConfiguration.sendMessage(
"command-cloudsign-cleanup-success",
sender::sendMessage,
m -> m.replace("%amount%", Integer.toString(removed)));
}

private void handleCleanupAll(@NonNull CommandSender sender, @NonNull CommandContext context) {
// removes all signs on which location is not a sign anymore
var removed = this.signManagement.removeAllMissingSigns();
SignsConfiguration.sendMessage(
"command-cloudsign-cleanup-success",
sender::sendMessage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,14 @@ public boolean onCommand(CommandSender sender, Command command, String commandLa
}

return true;
} else if (args.length == 1 && args[0].equalsIgnoreCase("cleanup")) {
var removed = this.signManagement.removeMissingSigns();
} else if (args.length == 1 && args[0].equalsIgnoreCase("cleanupall")) {
var removed = this.signManagement.removeAllMissingSigns();
SignsConfiguration.sendMessage("command-cloudsign-cleanup-success", player::sendMessage,
m -> m.replace("%amount%", Integer.toString(removed)));
return true;
} else if ((args.length == 1 || args.length == 2) && args[0].equalsIgnoreCase("cleanup")) {
var world = args.length == 2 ? args[1] : player.getLocation().getLevel().getName();
var removed = this.signManagement.removeMissingSigns(world);
SignsConfiguration.sendMessage("command-cloudsign-cleanup-success", player::sendMessage,
m -> m.replace("%amount%", Integer.toString(removed)));
return true;
Expand Down Expand Up @@ -109,7 +115,8 @@ public boolean onCommand(CommandSender sender, Command command, String commandLa
sender.sendMessage("§7/cloudsigns create <targetGroup> [templatePath]");
sender.sendMessage("§7/cloudsigns remove");
sender.sendMessage("§7/cloudsigns removeAll");
sender.sendMessage("§7/cloudsigns cleanup");
sender.sendMessage("§7/cloudsigns cleanup [world]");
sender.sendMessage("§7/cloudsigns cleanupAll");

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class SignsCommand implements CommandExecutor {
public static final Parameter.Key<String> ACTION = Parameter.key("action", String.class);
public static final Parameter.Key<String> TARGET_GROUP = Parameter.key("target_group", String.class);
public static final Parameter.Key<String> TARGET_TEMPLATE = Parameter.key("target_template_path", String.class);
public static final Parameter.Key<String> WORLD = Parameter.key("world", String.class);

protected final Supplier<SpongeSignManagement> signManagement;

Expand All @@ -66,6 +67,7 @@ public CommandResult execute(@NonNull CommandContext context) {
var type = context.one(ACTION).orElse(null);
var targetGroup = context.one(TARGET_GROUP).orElse(null);
var targetTemplatePath = context.one(TARGET_TEMPLATE).orElse(null);
var world = context.one(WORLD).orElse(player.world().properties().name());

if (type != null) {
if (type.equalsIgnoreCase("create") && targetGroup != null) {
Expand Down Expand Up @@ -93,8 +95,15 @@ public CommandResult execute(@NonNull CommandContext context) {
}

return CommandResult.success();
} else if (type.equalsIgnoreCase("cleanupall")) {
var removed = this.signManagement.get().removeAllMissingSigns();
SignsConfiguration.sendMessage(
"command-cloudsign-cleanup-success",
m -> player.sendMessage(Component.text(m)),
m -> m.replace("%amount%", Integer.toString(removed)));
return CommandResult.success();
} else if (type.equalsIgnoreCase("cleanup")) {
var removed = this.signManagement.get().removeMissingSigns();
var removed = this.signManagement.get().removeMissingSigns(world);
SignsConfiguration.sendMessage(
"command-cloudsign-cleanup-success",
m -> player.sendMessage(Component.text(m)),
Expand Down Expand Up @@ -133,7 +142,8 @@ public CommandResult execute(@NonNull CommandContext context) {
context.sendMessage(Identity.nil(), Component.text("§7/cloudsigns create <targetGroup> [templatePath]"));
context.sendMessage(Identity.nil(), Component.text("§7/cloudsigns remove"));
context.sendMessage(Identity.nil(), Component.text("§7/cloudsigns removeAll"));
context.sendMessage(Identity.nil(), Component.text("§7/cloudsigns cleanup"));
context.sendMessage(Identity.nil(), Component.text("§7/cloudsigns cleanup [world]"));
context.sendMessage(Identity.nil(), Component.text("§7/cloudsigns cleanupAll"));

return CommandResult.success();
}
Expand Down
Loading