Skip to content

Commit

Permalink
feat: Improve context and completion for comma separated shares
Browse files Browse the repository at this point in the history
  • Loading branch information
benwoo1110 committed Feb 16, 2023
1 parent ac32819 commit 917bcb8
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.onarandombox.multiverseinventories.MultiverseInventories;
import com.onarandombox.multiverseinventories.WorldGroup;
import com.onarandombox.multiverseinventories.locale.Message;
import com.onarandombox.multiverseinventories.share.Sharables;
import com.onarandombox.multiverseinventories.share.Shares;
import com.onarandombox.multiverseinventories.util.Perm;
import org.jetbrains.annotations.NotNull;
Expand All @@ -22,58 +21,20 @@ public AddSharesCommand(@NotNull MultiverseInventories plugin) {
}

@Subcommand("addshares")
@CommandCompletion("@shares|@sharables @worldGroups")
@CommandCompletion("@shares @worldGroups")
@Syntax("<share[,extra]> <group>")
@Description("Add one or more shares to a group.")
public void onAddSharesCommand(BukkitCommandIssuer issuer,

@Syntax("<share[,extra]>")
@Description("One or more sharables to add.")
@NotNull String sharesString,
Shares shares,

@Syntax("<group>")
@Description("Group you want to add the shares to.")
@NotNull WorldGroup group
WorldGroup group
) {
//TODO: Remove need for negative shares.
Shares newShares;
Shares negativeShares;
if (sharesString.contains("all") || sharesString.contains("everything") || sharesString.contains("*")) {
newShares = Sharables.allOf();
negativeShares = Sharables.noneOf();
}
else if (sharesString.contains("-all") || sharesString.contains("-everything") || sharesString.contains("-*")) {
negativeShares = Sharables.allOf();
newShares = Sharables.noneOf();
}
else {
negativeShares = Sharables.noneOf();
newShares = Sharables.noneOf();
String[] shareList = sharesString.split(",");
for (String share : shareList) {
if (share.startsWith("-") && share.length() > 1) {
Shares shares = Sharables.lookup(share.substring(1));
if (shares == null) {
continue;
}
negativeShares.setSharing(shares, true);
} else {
Shares shares = Sharables.lookup(share);
if (shares == null) {
continue;
}
newShares.setSharing(shares, true);
}
}
}

if (newShares.isEmpty() && negativeShares.isEmpty()) {
this.messager.normal(Message.ERROR_NO_SHARES_SPECIFIED, issuer.getIssuer(), sharesString);
return;
}

group.getShares().mergeShares(newShares);
group.getShares().removeAll(negativeShares);
group.getShares().mergeShares(shares);
this.plugin.getGroupManager().updateGroup(group);
this.plugin.getMVIConfig().save();
this.messager.normal(Message.NOW_SHARING, issuer.getIssuer(), group.getName(), group.getShares().toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public void onAddWorldCommand(BukkitCommandIssuer issuer,

@Syntax("<group>")
@Description("Group you want to add the world to.")
WorldGroup group) {

WorldGroup group
) {
if (group.containsWorld(world.getName())) {
this.messager.normal(Message.WORLD_ALREADY_EXISTS, issuer.getIssuer(), world.getName(), group.getName());
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.onarandombox.acf.annotation.Subcommand;
import com.onarandombox.acf.annotation.Syntax;
import com.onarandombox.multiverseinventories.MultiverseInventories;
import com.onarandombox.multiverseinventories.locale.Message;
import com.onarandombox.multiverseinventories.migration.DataImporter;
import com.onarandombox.multiverseinventories.migration.MigrationException;
import com.onarandombox.multiverseinventories.util.Perm;
Expand All @@ -33,8 +34,9 @@ public void onImportCommand(BukkitCommandIssuer issuer,
try {
importer.importData();
} catch (MigrationException e) {
messager.bad(Message.IMPORT_FAILED, issuer.getIssuer(), importer.getPlugin().getName());
Logging.severe(e.getMessage());
Logging.severe("Cause: " + e.getCauseException().getMessage());
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import com.onarandombox.multiverseinventories.MultiverseInventories;
import com.onarandombox.multiverseinventories.locale.Message;
import com.onarandombox.multiverseinventories.util.Perm;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.jetbrains.annotations.NotNull;

@CommandAlias("mvinv")
Expand All @@ -31,33 +31,35 @@ public MigrateCommand(@NotNull MultiverseInventories plugin) {
@Description("Migrate player data from one name to another.")
public void onMigrateCommand(BukkitCommandIssuer issuer,

@Flags("type= old name")
@Syntax("<oldname>")
@Description("The old name of the player to migrate data from.")
String oldName,
OfflinePlayer oldPlayer,

@NotNull
@Flags("type= new name")
@Syntax("<newname>")
@Description("The new name of the player to migrate data to.")
String newName,
OfflinePlayer newPlayer,

@Optional
@Values("--save-old")
String saveOld
) {
if (oldPlayer.equals(newPlayer)) {
messager.bad(Message.MIGRATE_SAME_PLAYER, issuer.getIssuer());
return;
}

boolean deleteOld = saveOld == null || saveOld.equalsIgnoreCase("--save-old");

try {
this.plugin.getData().migratePlayerData(oldName, newName, Bukkit.getOfflinePlayer(newName).getUniqueId(), deleteOld);
this.plugin.getData().migratePlayerData(oldPlayer.getName(), newPlayer.getName(), newPlayer.getUniqueId(), deleteOld);
}
catch (IOException e) {
messager.bad(Message.MIGRATE_FAILED, issuer.getIssuer(), oldName, newName);
Logging.severe("Could not migrate data from name " + oldName + " to " + newName);
messager.bad(Message.MIGRATE_FAILED, issuer.getIssuer(), oldPlayer.getName(), newPlayer.getName());
Logging.severe("Could not migrate data from name " + oldPlayer.getName() + " to " + newPlayer.getName());
e.printStackTrace();
return;
}

messager.good(Message.MIGRATE_SUCCESSFUL, issuer.getIssuer(), oldName, newName);
messager.good(Message.MIGRATE_SUCCESSFUL, issuer.getIssuer(), oldPlayer.getName(), newPlayer.getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import com.onarandombox.multiverseinventories.MultiverseInventories;
import com.onarandombox.multiverseinventories.WorldGroup;
import com.onarandombox.multiverseinventories.locale.Message;
import com.onarandombox.multiverseinventories.share.Sharable;
import com.onarandombox.multiverseinventories.share.Sharables;
import com.onarandombox.multiverseinventories.share.Shares;
import com.onarandombox.multiverseinventories.util.Perm;
import org.jetbrains.annotations.NotNull;
Expand All @@ -23,41 +21,19 @@ public RemoveSharesCommand(@NotNull MultiverseInventories plugin) {
}

@Subcommand("removeshares")
@CommandCompletion("@shares|@sharables @worldGroups")
@CommandCompletion("@shares @worldGroups")
@Syntax("<share[,extra]> <group>")
public void onRemoveSharesCommand(BukkitCommandIssuer issuer,

@Syntax("<share[,extra]>")
@Description("One or more sharables to remove.")
String sharesString,
Shares shares,

@Syntax("<group>")
@Description("Group you want to remove the shares from.")
WorldGroup group
) {
//TODO: Clean this up with context resolver.
Shares newShares;
if (sharesString.contains("all") || sharesString.contains("everything") || sharesString.contains("*")) {
newShares = Sharables.allOf();
} else {
newShares = Sharables.noneOf();
String[] shareList = sharesString.split(",");
for (String shareString : shareList) {
Shares shares = Sharables.lookup(shareString);
if (shares == null) {
continue;
}
newShares.setSharing(shares, true);
}
}
if (newShares.isEmpty()) {
this.messager.normal(Message.ERROR_NO_SHARES_SPECIFIED, issuer.getIssuer(), sharesString);
return;
}

for (Sharable<?> sharable : newShares) {
group.getShares().setSharing(sharable, false);
}
group.getShares().setSharing(shares, false);
this.plugin.getGroupManager().updateGroup(group);
this.plugin.getMVIConfig().save();
this.messager.normal(Message.NOW_SHARING, issuer.getIssuer(), group.getName(), group.getShares().toString());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.onarandombox.multiverseinventories.command.tools;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

import com.onarandombox.acf.BukkitCommandCompletionContext;
import com.onarandombox.acf.CommandCompletions;
import com.onarandombox.multiverseinventories.MultiverseInventories;
import com.onarandombox.multiverseinventories.WorldGroup;
import com.onarandombox.multiverseinventories.share.Sharable;
import com.onarandombox.multiverseinventories.share.Sharables;

public class MVInvCommandCompletions {
Expand Down Expand Up @@ -43,9 +44,34 @@ private Collection<String> suggestSharables(BukkitCommandCompletionContext conte
}

private Collection<String> suggestShares(BukkitCommandCompletionContext context) {
return Sharables.allShares().stream()
.filter(share -> share.getNames().length > 0)
.map(share -> share.getNames()[0])
String input = context.getInput();

if (input.isEmpty()) {
// No input, so we're suggesting the first share
return Sharables.registeredNames();
}

int lastComma = input.lastIndexOf(",");
if (lastComma == -1) {
// No comma, so we're suggesting the first share
if (input.startsWith("-")) {
return Sharables.registeredNames().stream()
.map(name -> "-" + name)
.collect(Collectors.toList());
}
return Sharables.registeredNames();
}

// We're suggesting a share after a comma
String lastShare = input.substring(lastComma + 1);
String currentSharesString = input.substring(0, lastComma + (lastShare.startsWith("-") ? 2 : 1));
Set<String> currentShares = Arrays.stream(input.split(","))
.map(share -> share.startsWith("-") ? share.substring(1) : share)
.collect(Collectors.toSet());

return Sharables.registeredNames().stream()
.filter(name -> !currentShares.contains(name))
.map(name -> currentSharesString + name)
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.onarandombox.multiverseinventories.command.tools;

import java.util.Arrays;
import java.util.Optional;

import com.google.common.base.Strings;
import com.onarandombox.acf.BukkitCommandExecutionContext;
import com.onarandombox.acf.CommandContexts;
import com.onarandombox.acf.InvalidCommandArgument;
Expand Down Expand Up @@ -69,14 +70,31 @@ private Sharable<?> parseSharable(BukkitCommandExecutionContext context) {
}

private Shares parseShares(BukkitCommandExecutionContext context) {
Shares shares = Sharables.lookup(context.popFirstArg());
if (shares != null) {
return shares;
String shareStrings = context.popFirstArg();
if (Strings.isNullOrEmpty(shareStrings)) {
throw new InvalidCommandArgument(messager.getMessage(Message.ERROR_NO_SHARES_SPECIFIED, shareStrings));
}
if (context.isOptional()) {
return null;

String[] shareNames = shareStrings.split(",");
Shares newShares = Sharables.noneOf();
Shares negativeShares = Sharables.noneOf();
for (String shareName : shareNames) {
if (shareName.startsWith("-")) {
shareName = shareName.substring(1);
Optional.ofNullable(Sharables.lookup(shareName))
.ifPresent(shares -> negativeShares.setSharing(shares, true));
continue;
}
Optional.ofNullable(Sharables.lookup(shareName))
.ifPresent(shares -> newShares.setSharing(shares, true));
}
throw new InvalidCommandArgument(messager.getMessage(Message.ERROR_NO_SHARES_SPECIFIED));

newShares.setSharing(negativeShares, false);
if (newShares.isEmpty()) {
throw new InvalidCommandArgument(messager.getMessage(Message.ERROR_NO_SHARES_SPECIFIED, shareStrings));
}

return newShares;
}

private WorldGroup parseWorldGroup(BukkitCommandExecutionContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public enum Message {
NOW_NOT_USING_OPTIONAL("&f%1 &6will no longer be considered when player's change world."),
NO_OPTIONAL_SHARES("&f%1 &6is not an optional share!"),
// Migrate Command
MIGRATE_SAME_PLAYER("You cannot migrate data from a player to themselves!"),
MIGRATE_FAILED("Failed to migrate data from %1 to %2! Check logs for error details."),
MIGRATE_SUCCESSFUL("Migrated data from %1 to %2!"),
// Import Command
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.bukkit.potion.PotionEffect;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -692,8 +691,8 @@ public static Shares lookup(String name) {
/**
* @return A collection of all registered {@link Shares}. This is NOT to be modified and serves only as a reference.
*/
public static Collection<Shares> allShares() {
return LOOKUP_MAP.values();
public static Collection<String> registeredNames() {
return LOOKUP_MAP.keySet();
}

/**
Expand Down

0 comments on commit 917bcb8

Please sign in to comment.