Fixed bug with help that I introduced.

Added specific convenience showHelp() method to Composite command.
This commit is contained in:
Tastybento 2018-02-06 22:45:21 -08:00
parent 61f0a6f997
commit 1db21c10da
9 changed files with 55 additions and 23 deletions

View File

@ -5,6 +5,7 @@ import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.bukkit.entity.EntityType;
@ -129,8 +130,8 @@ public class Settings implements ISettings<Settings> {
private boolean endIslands = true;
// Entities
private HashMap<EntityType, Integer> entityLimits;
private HashMap<String, Integer> tileEntityLimits;
private Map<EntityType, Integer> entityLimits = new HashMap<>();
private Map<String, Integer> tileEntityLimits = new HashMap<>();
// ---------------------------------------------
@ -188,7 +189,7 @@ public class Settings implements ISettings<Settings> {
// Ranks
@ConfigEntry(path = "island.customranks")
private HashMap<String, Integer> customRanks = new HashMap<>();
private Map<String, Integer> customRanks = new HashMap<>();
// ---------------------------------------------
@ -201,7 +202,7 @@ public class Settings implements ISettings<Settings> {
private int togglePvPCooldown;
private HashMap<Flag, Boolean> defaultFlags;
private Map<Flag, Boolean> defaultFlags = new HashMap<>();
//TODO transform these options below into flags
private boolean allowEndermanGriefing;
@ -252,7 +253,7 @@ public class Settings implements ISettings<Settings> {
private boolean useOwnGenerator;
private HashMap<String,Integer> limitedBlocks;
private Map<String,Integer> limitedBlocks = new HashMap<>();
private boolean teamJoinDeathReset;
private String uniqueId = "config";
@ -316,7 +317,7 @@ public class Settings implements ISettings<Settings> {
/**
* @return the customRanks
*/
public HashMap<String, Integer> getCustomRanks() {
public Map<String, Integer> getCustomRanks() {
return customRanks;
}
/**
@ -370,7 +371,7 @@ public class Settings implements ISettings<Settings> {
/**
* @return the defaultFlags
*/
public HashMap<Flag, Boolean> getDefaultFlags() {
public Map<Flag, Boolean> getDefaultFlags() {
return defaultFlags;
}
/**
@ -382,7 +383,7 @@ public class Settings implements ISettings<Settings> {
/**
* @return the entityLimits
*/
public HashMap<EntityType, Integer> getEntityLimits() {
public Map<EntityType, Integer> getEntityLimits() {
return entityLimits;
}
@Override
@ -452,7 +453,7 @@ public class Settings implements ISettings<Settings> {
/**
* @return the limitedBlocks
*/
public HashMap<String, Integer> getLimitedBlocks() {
public Map<String, Integer> getLimitedBlocks() {
return limitedBlocks;
}
/**
@ -524,7 +525,7 @@ public class Settings implements ISettings<Settings> {
/**
* @return the tileEntityLimits
*/
public HashMap<String, Integer> getTileEntityLimits() {
public Map<String, Integer> getTileEntityLimits() {
return tileEntityLimits;
}
/**
@ -860,7 +861,7 @@ public class Settings implements ISettings<Settings> {
/**
* @param customRanks the customRanks to set
*/
public void setCustomRanks(HashMap<String, Integer> customRanks) {
public void setCustomRanks(Map<String, Integer> customRanks) {
this.customRanks = customRanks;
}
/**
@ -920,7 +921,7 @@ public class Settings implements ISettings<Settings> {
/**
* @param defaultFlags the defaultFlags to set
*/
public void setDefaultFlags(HashMap<Flag, Boolean> defaultFlags) {
public void setDefaultFlags(Map<Flag, Boolean> defaultFlags) {
this.defaultFlags = defaultFlags;
}
/**
@ -950,7 +951,7 @@ public class Settings implements ISettings<Settings> {
/**
* @param entityLimits the entityLimits to set
*/
public void setEntityLimits(HashMap<EntityType, Integer> entityLimits) {
public void setEntityLimits(Map<EntityType, Integer> entityLimits) {
this.entityLimits = entityLimits;
}
/**
@ -1046,7 +1047,7 @@ public class Settings implements ISettings<Settings> {
/**
* @param limitedBlocks the limitedBlocks to set
*/
public void setLimitedBlocks(HashMap<String, Integer> limitedBlocks) {
public void setLimitedBlocks(Map<String, Integer> limitedBlocks) {
this.limitedBlocks = limitedBlocks;
}
/**
@ -1191,7 +1192,7 @@ public class Settings implements ISettings<Settings> {
/**
* @param tileEntityLimits the tileEntityLimits to set
*/
public void setTileEntityLimits(HashMap<String, Integer> tileEntityLimits) {
public void setTileEntityLimits(Map<String, Integer> tileEntityLimits) {
this.tileEntityLimits = tileEntityLimits;
}
/**

View File

@ -477,4 +477,17 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
}
return Util.tabLimit(options, lastArg);
}
/**
* Show help
* @param command
* @param user
* @param args
*/
protected void showHelp(CompositeCommand command, User user, List<String> args) {
Optional<CompositeCommand> helpCommand = command.getSubCommand("help");
if (helpCommand.isPresent()) {
helpCommand.get().execute(user, args);
}
}
}

View File

@ -74,7 +74,10 @@ public class DefaultHelpCommand extends CompositeCommand {
// Ignore the help command
if (!subCommand.getLabel().equals("help")) {
// Every command should have help because every command has a default help
subCommand.getSubCommand("help").ifPresent(sub -> execute(user, Arrays.asList(String.valueOf(newDepth))));
Optional<CompositeCommand> sub = subCommand.getSubCommand("help");
if (sub.isPresent()) {
sub.get().execute(user, Arrays.asList(String.valueOf(newDepth)));
}
}
}
}

View File

@ -28,7 +28,8 @@ public class AdminCommand extends CompositeCommand {
@Override
public boolean execute(User user, List<String> args) {
// By default run the attached help command, if it exists (it should)
return this.getSubCommand("help").map(help -> execute(user, args)).orElse(false);
showHelp(this, user, args);
return false;
}
}

View File

@ -1,6 +1,8 @@
package us.tastybento.bskyblock.commands;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import us.tastybento.bskyblock.Constants;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
@ -45,10 +47,19 @@ public class IslandCommand extends CompositeCommand {
public boolean execute(User user, List<String> args) {
// If this player does not have an island, create one
if (!getPlugin().getIslands().hasIsland(user.getUniqueId())) {
return this.getSubCommand("create").map(command -> execute(user, args)).orElse(false);
Optional<CompositeCommand> subCreate = this.getSubCommand("create");
if (subCreate.isPresent()) {
subCreate.get().execute(user, new ArrayList<>());
}
}
Optional<CompositeCommand> go = this.getSubCommand("go");
// Otherwise, currently, just go home
return this.getSubCommand("go").map(command -> execute(user, args)).orElse(false);
if (go.isPresent()) {
go.get().execute(user, new ArrayList<>());
}
return true;
}
}

View File

@ -51,7 +51,7 @@ public class IslandSetnameCommand extends CompositeCommand {
}
// Explain command
if (args.isEmpty()) {
this.getSubCommand("help").ifPresent(help -> help.execute(user, args));
showHelp(this, user, args);
return false;
}

View File

@ -51,7 +51,8 @@ public class IslandTeamInviteCommand extends AbstractIslandTeamCommand {
return true;
}
// Show help
return this.getSubCommand("help").map(command -> execute(user, args)).orElse(false);
showHelp(this, user, args);
return false;
} else {
// Only online players can be invited
UUID invitedPlayerUUID = getPlayers().getUUID(args.get(0));

View File

@ -40,7 +40,8 @@ public class IslandTeamKickCommand extends AbstractIslandTeamCommand {
}
// If args are not right, show help
if (args.size() != 1) {
return this.getSubCommand("help").map(command -> execute(user, new ArrayList<>())).orElse(false);
showHelp(this, user, args);
return false;
}
// Get target
UUID targetUUID = getPlayers().getUUID(args.get(0));

View File

@ -42,7 +42,8 @@ public class IslandTeamSetownerCommand extends AbstractIslandTeamCommand {
}
// If args are not right, show help
if (args.size() != 1) {
return this.getSubCommand("help").map(command -> execute(user, new ArrayList<>())).orElse(false);
showHelp(this, user, args);
return false;
}
//getPlugin().getLogger().info("DEBUG: arg[0] = " + args.get(0));
UUID targetUUID = getPlayers().getUUID(args.get(0));