use of net.minecraft.command.CommandException in project Minestuck by mraof.
the class CommandSburbSession method execute.
@Override
public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException {
if (args.length < 2 || args.length < 3 && args[1].equalsIgnoreCase("add") || args.length != 4 && (args[1].equalsIgnoreCase("landTerrain") || args[1].equalsIgnoreCase("landTitle")) || args.length != 5 && args[1].equalsIgnoreCase("title") || args.length != 6 && args.length != 7 && args[1].equalsIgnoreCase("define"))
throw new WrongUsageException(this.getUsage(sender));
String sessionName = args[0];
String command = args[1];
if (command.equalsIgnoreCase("name")) {
String playerName = args.length < 3 ? getCommandSenderAsPlayer(sender).getName() : args[2];
SburbHandler.sessionName(server, sender, this, playerName, sessionName);
} else if (command.equalsIgnoreCase("add")) /* || command.equalsIgnoreCase("finish")*/
{
String[] params = Arrays.copyOfRange(args, 2, args.length);
// command.equalsIgnoreCase("finish"));
SburbHandler.managePredefinedSession(server, sender, this, sessionName, params, false);
} else if (command.equalsIgnoreCase("title")) {
String playerName = args[2];
String classStr = args[3], aspectStr = args[4];
EnumClass titleClass = null;
EnumAspect titleAspect = null;
try // Parse class
{
for (EnumClass c : EnumClass.values()) if (c.name().equalsIgnoreCase(classStr)) {
titleClass = c;
break;
}
if (titleClass == null) {
int classIndex = Integer.parseInt(classStr);
titleClass = EnumClass.getClassFromInt(classIndex);
}
} catch (Exception e) {
throw new WrongUsageException("commands.sburbSession.notClass", classStr);
}
try // Parse aspect
{
for (EnumAspect aspect : EnumAspect.values()) if (aspect.name().equalsIgnoreCase(aspectStr)) {
titleAspect = aspect;
break;
}
if (titleAspect == null) {
int aspectIndex = Integer.parseInt(aspectStr);
titleAspect = EnumAspect.getAspectFromInt(aspectIndex);
}
} catch (Exception e) {
throw new WrongUsageException("commands.sburbSession.notAspect", aspectStr);
}
SburbHandler.predefineTitle(server, sender, this, playerName, sessionName, new Title(titleClass, titleAspect));
} else if (command.equalsIgnoreCase("landTerrain")) {
String playerName = args[2];
TerrainLandAspect landAspect = LandAspectRegistry.fromNameTerrain(args[3].toLowerCase());
if (landAspect == null)
throw new CommandException("Can't find terrain land aspect by the name %s", args[3]);
SburbHandler.predefineTerrainLandAspect(server, sender, this, playerName, sessionName, landAspect);
} else if (command.equalsIgnoreCase("landTitle")) {
String playerName = args[2];
TitleLandAspect landAspect = LandAspectRegistry.fromNameTitle(args[3].toLowerCase());
if (landAspect == null)
throw new CommandException("Can't find title land aspect by the name %s", args[3]);
SburbHandler.predefineTitleLandAspect(server, sender, this, playerName, sessionName, landAspect);
} else if (command.equalsIgnoreCase("define")) {
String playerName = args[2];
String classStr = args[3], aspectStr = args[4];
EnumClass titleClass = null;
EnumAspect titleAspect = null;
try // Parse class
{
for (EnumClass c : EnumClass.values()) if (c.name().equalsIgnoreCase(classStr)) {
titleClass = c;
break;
}
if (titleClass == null) {
int classIndex = Integer.parseInt(classStr);
titleClass = EnumClass.getClassFromInt(classIndex);
}
} catch (Exception e) {
throw new WrongUsageException("commands.sburbSession.notClass", classStr);
}
try // Parse aspect
{
for (EnumAspect aspect : EnumAspect.values()) if (aspect.name().equalsIgnoreCase(aspectStr)) {
titleAspect = aspect;
break;
}
if (titleAspect == null) {
int aspectIndex = Integer.parseInt(aspectStr);
titleAspect = EnumAspect.getAspectFromInt(aspectIndex);
}
} catch (Exception e) {
throw new WrongUsageException("commands.sburbSession.notAspect", aspectStr);
}
TerrainLandAspect terrainLand = null;
TitleLandAspect titleLand = null;
if (args.length == 7) {
titleLand = LandAspectRegistry.fromNameTitle(args[5].toLowerCase());
if (titleLand == null)
throw new CommandException("Can't find title land aspect by the name %s", args[5]);
terrainLand = LandAspectRegistry.fromNameTerrain(args[6].toLowerCase());
if (terrainLand == null)
throw new CommandException("Can't find terrain land aspect by the name %s", args[6]);
} else {
titleLand = LandAspectRegistry.fromNameTitle(args[5].toLowerCase());
if (titleLand == null) {
terrainLand = LandAspectRegistry.fromNameTerrain(args[5].toLowerCase());
if (terrainLand == null)
throw new CommandException("Can't find any land aspect by the name %s", args[5]);
}
}
SburbHandler.predefineTitle(server, sender, this, playerName, sessionName, new Title(titleClass, titleAspect));
if (titleLand != null)
SburbHandler.predefineTitleLandAspect(server, sender, this, playerName, sessionName, titleLand);
if (terrainLand != null)
SburbHandler.predefineTerrainLandAspect(server, sender, this, playerName, sessionName, terrainLand);
} else
throw new WrongUsageException(this.getUsage(sender));
}
use of net.minecraft.command.CommandException in project Minestuck by mraof.
the class SburbHandler method predefineTitle.
public static void predefineTitle(MinecraftServer server, ICommandSender sender, ICommand command, String playerName, String sessionName, Title title) throws CommandException {
PlayerIdentifier identifier = predefineCheck(server, sender, playerName, sessionName);
Title playerTitle = MinestuckPlayerData.getTitle(identifier);
if (playerTitle != null)
throw new CommandException("You can't change your title after having entered the medium.");
Session session = sessionsByName.get(sessionName);
for (SburbConnection c : session.connections) if (c.isMain && c.enteredGame && title.equals(MinestuckPlayerData.getTitle(c.getClientIdentifier())))
throw new CommandException("This title is already used by %s.", c.getClientIdentifier().getUsername());
for (Map.Entry<PlayerIdentifier, PredefineData> entry : session.predefinedPlayers.entrySet()) if (entry.getValue().title != null && title.equals(entry.getValue().title))
throw new CommandException("This title is already assigned to %s.", entry.getKey().getUsername());
PredefineData data = session.predefinedPlayers.get(identifier);
data.title = title;
TitleLandAspect landAspect = LandAspectRegistry.getSingleLandAspect(title.getHeroAspect());
if (landAspect != null)
// This part could be made more robust for when landTerrain is already defined
data.landTitle = landAspect;
CommandBase.notifyCommandListener(sender, command, "commands.sburbSession.titleSuccess", playerName, title.asTextComponent());
}
use of net.minecraft.command.CommandException in project Minestuck by mraof.
the class SburbHandler method managePredefinedSession.
public static void managePredefinedSession(MinecraftServer server, ICommandSender sender, ICommand command, String sessionName, String[] playerNames, boolean finish) throws CommandException {
Session session = sessionsByName.get(sessionName);
if (session == null) {
if (finish && playerNames.length == 0)
throw new CommandException("Couldn't find session with that name. Aborting the finalizing process.", sessionName);
if (singleSession)
throw new CommandException("Not allowed to create new sessions when global session is active. Use \"%s\" as session name for global session access.", GLOBAL_SESSION_NAME);
if (sender.sendCommandFeedback())
sender.sendMessage(new TextComponentString("Couldn't find session with that name, creating a new session..."));
session = new Session();
session.name = sessionName;
sessions.add(session);
sessionsByName.put(session.name, session);
}
if (session.locked)
throw new CommandException("That session may no longer be modified.");
int handled = 0;
boolean skipFinishing = false;
for (String playerName : playerNames) {
if (playerName.startsWith("!")) {
playerName = playerName.substring(1);
PlayerIdentifier identifier;
try {
identifier = IdentifierHandler.getForCommand(server, sender, playerName);
} catch (CommandException c) {
if (sender.sendCommandFeedback())
sender.sendMessage(new TextComponentString(String.format(c.getMessage(), c.getErrorObjects())));
continue;
}
if (!session.containsPlayer(identifier)) {
if (sender.sendCommandFeedback())
sender.sendMessage(new TextComponentString("Failed to remove player \"" + playerName + "\": Player isn't in session.").setStyle(new Style().setColor(TextFormatting.RED)));
continue;
}
if (session.predefinedPlayers.remove(identifier) == null) {
if (sender.sendCommandFeedback())
sender.sendMessage(new TextComponentString("Failed to remove player \"" + playerName + "\": Player isn't registered with the session.").setStyle(new Style().setColor(TextFormatting.RED)));
continue;
}
handled++;
if (session.containsPlayer(identifier)) {
split(session);
session = sessionsByName.get(sessionName);
if (session.containsPlayer(identifier)) {
if (sender.sendCommandFeedback())
sender.sendMessage(new TextComponentString("Removed player \"" + playerName + "\", but they are still part of a connection in the session and will therefore be part of the session unless the connection is discarded.").setStyle(new Style().setColor(TextFormatting.YELLOW)));
skipFinishing = true;
continue;
}
}
} else {
PlayerIdentifier identifier;
try {
identifier = IdentifierHandler.getForCommand(server, sender, playerName);
} catch (CommandException c) {
if (sender.sendCommandFeedback())
sender.sendMessage(new TextComponentString(String.format(c.getMessage(), c.getErrorObjects())));
continue;
}
if (session.predefinedPlayers.containsKey(identifier)) {
if (sender.sendCommandFeedback())
sender.sendMessage(new TextComponentString("Failed to add player \"" + playerName + "\": Player is already registered with session.").setStyle(new Style().setColor(TextFormatting.RED)));
continue;
}
Session playerSession = getPlayerSession(identifier);
if (playerSession != null) {
if (merge(session, playerSession, null) != null) {
if (sender.sendCommandFeedback())
sender.sendMessage(new TextComponentString("Failed to add player \"" + playerName + "\": Can't merge with the session that the player is already in.").setStyle(new Style().setColor(TextFormatting.RED)));
continue;
}
} else if (MinestuckConfig.forceMaxSize && session.getPlayerList().size() + 1 > maxSize) {
if (sender.sendCommandFeedback())
sender.sendMessage(new TextComponentString("Failed to add player \"" + playerName + "\": The session can't accept more players with the current configurations.").setStyle(new Style().setColor(TextFormatting.RED)));
continue;
}
session.predefinedPlayers.put(identifier, new PredefineData());
handled++;
}
}
if (playerNames.length > 0)
CommandBase.notifyCommandListener(sender, command, "commands.sburbSession.addSuccess", handled, playerNames.length);
/*if(finish)
if(!skipFinishing && handled == playerNames.length)
{
SburbHandler.finishSession(sender, command, session);
} else throw new CommandException("Skipping to finalize the session due to one or more issues while adding players.");*/
}
use of net.minecraft.command.CommandException in project Minestuck by mraof.
the class SburbHandler method sessionName.
public static void sessionName(MinecraftServer server, ICommandSender sender, ICommand command, String playerName, String sessionName) throws CommandException {
PlayerIdentifier identifier = IdentifierHandler.getForCommand(server, sender, playerName);
Session playerSession = getPlayerSession(identifier), session = sessionsByName.get(sessionName);
if (singleSession)
throw new CommandException("Not allowed to change session name when global session is active. Use \"%s\" as session name for global session access.", GLOBAL_SESSION_NAME);
if (playerSession == null)
throw new CommandException("Couldn't find session for player \"%s\"", playerName);
if (session != null)
throw new CommandException("That session name is already taken.");
if (playerSession.name != null)
sessionsByName.remove(playerSession.name);
String prevName = playerSession.name;
playerSession.name = sessionName;
sessionsByName.put(playerSession.name, playerSession);
if (prevName != null)
CommandBase.notifyCommandListener(sender, command, "commands.sburbSession.rename", prevName, sessionName, playerName);
else
CommandBase.notifyCommandListener(sender, command, "commands.sburbSession.name", sessionName, playerName);
}
use of net.minecraft.command.CommandException in project Minestuck by mraof.
the class SburbHandler method predefineTerrainLandAspect.
public static void predefineTerrainLandAspect(MinecraftServer server, ICommandSender sender, ICommand command, String playerName, String sessionName, TerrainLandAspect aspect) throws CommandException {
PlayerIdentifier identifier = predefineCheck(server, sender, playerName, sessionName);
Session session = sessionsByName.get(sessionName);
SburbConnection clientConnection = SkaianetHandler.getClientConnection(identifier);
PredefineData data = session.predefinedPlayers.get(identifier);
if (clientConnection != null && clientConnection.enteredGame())
throw new CommandException("You can't change your land aspects after having entered the medium.");
if (data.landTitle == null)
throw new CommandException("You should define the other land aspect before this one.");
if (!data.landTitle.isAspectCompatible(aspect))
throw new CommandException("That terrain land aspect isn't compatible with the other land aspect.");
data.landTerrain = aspect;
CommandBase.notifyCommandListener(sender, command, "commands.sburbSession.landTerrainSuccess", playerName, aspect.getPrimaryName());
}
Aggregations