Page MenuHomedesp's stash

BaseConvert.java
No OneTemporary

BaseConvert.java

package me.despawningbone.discordbot.command.misc;
import java.util.Arrays;
import me.despawningbone.discordbot.command.Command;
import me.despawningbone.discordbot.command.CommandResult;
import me.despawningbone.discordbot.command.CommandResult.CommandResultType;
import me.despawningbone.discordbot.utils.MiscUtils;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.entities.User;
public class BaseConvert extends Command {
public BaseConvert() {
this.alias = Arrays.asList("bc");
this.desc = "convert integers of a base to another base!";
this.usage = "[frombase] <base> <int>";
this.examples = Arrays.asList("2 100", "16 8 FF");
}
@Override
public CommandResult execute(TextChannel channel, User author, Message msg, String[] args) { //base 1 is weird, fix?
String x = "0";
long base = 10;
long frombase = 10;
try {
if (args.length < 3) {
try {
base = Long.parseLong(args[0]);
x = args[1];
} catch (ArrayIndexOutOfBoundsException e) {
return new CommandResult(CommandResultType.INVALIDARGS, "Please enter" + (args.length < 1 ? "something :joy:" : "a value to convert."));
} catch (NumberFormatException e) {
base = MiscUtils.nameToBase(args[0]);
}
} else {
try { frombase = Long.parseLong(args[0]); } catch (NumberFormatException e) { frombase = MiscUtils.nameToBase(args[0]); }
try { base = Long.parseLong(args[1]); } catch (NumberFormatException e) { base = MiscUtils.nameToBase(args[1]); }
x = args[2];
}
if (base > 30 || frombase > 30) {
return new CommandResult(CommandResultType.INVALIDARGS, "Please enter a base smaller than 30.");
}
try {
channel.sendMessage("Base" + MiscUtils.longToSubscript(frombase) + ": `" + x.toUpperCase() + "`\n" + "Base"
+ MiscUtils.longToSubscript(base) + ": `"
+ Long.toString(Long.parseLong(x, (int) frombase), (int) base).toUpperCase() + "`").queue();
return new CommandResult(CommandResultType.SUCCESS);
} catch (NumberFormatException e) {
return new CommandResult(CommandResultType.INVALIDARGS, "Please enter a valid value within the base range specified.");
}
} catch (IllegalArgumentException e) {
return new CommandResult(CommandResultType.INVALIDARGS, e.getMessage());
}
/*DEPRECATED OLD CODE*/
/*try {
if (args.length < 2) {
try {
base = Long.parseLong(msg.getContentDisplay().split(" ")[2]);
} catch (ArrayIndexOutOfBoundsException e) {
channel.sendMessage("Please enter something :joy:").queue();
return new CommandResult(CommandResultType.INVALIDARGS, null);
}
} else {
frombase = Long.parseLong(msg.getContentDisplay().split(" ")[2]);
base = Long.parseLong(msg.getContentDisplay().split(" ")[3]);
}
if (base > 30 || frombase > 30) {
channel.sendMessage("Please enter a base smaller than 30.").queue();
return new CommandResult(CommandResultType.INVALIDARGS, null);
}
} catch (NumberFormatException e) {
String sbase = msg.getContentDisplay().split(" ")[2];
try {
if (msg.getContentDisplay().split(" ").length > 4) {
frombase = MiscUtils.nameToBase(sbase);
base = MiscUtils.nameToBase(msg.getContentDisplay().split(" ")[3]);
} else {
base = MiscUtils.nameToBase(sbase);
}
} catch (NullPointerException | IllegalArgumentException e1) {
if (e1 instanceof IllegalArgumentException) {
channel.sendMessage(e1.getMessage()).queue();
return new CommandResult(CommandResultType.INVALIDARGS, null);
}
return new CommandResult(CommandResultType.ERROR, ExceptionUtils.getStackTrace(e1));
}
}
if (msg.getContentDisplay().split(" ").length < 5) {
try {
x = msg.getContentDisplay().split(" ", 4)[3];
} catch (ArrayIndexOutOfBoundsException e) {
channel.sendMessage("Please enter a value to convert.").queue();
return new CommandResult(CommandResultType.INVALIDARGS, null);
}
} else {
x = msg.getContentDisplay().split(" ", 5)[4];
}
try {
channel.sendMessage("Base" + MiscUtils.longToSubscript(frombase) + ": `" + x + "`\n" + "Base"
+ MiscUtils.longToSubscript(base) + ": `"
+ Long.toString(Long.parseLong(x, (int) frombase), (int) base).toUpperCase() + "`").queue();
return new CommandResult(CommandResultType.SUCCESS, null);
} catch (NumberFormatException e) {
channel.sendMessage("Please enter a valid positive integer within the base range specified.").queue();
return new CommandResult(CommandResultType.INVALIDARGS, null);
}*/
}
}

File Metadata

Mime Type
text/x-java
Expires
Mon, Mar 2, 1:55 AM (21 h, 53 m)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
8d/db/05604f4faa46073bf98c732b1b99

Event Timeline