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

added private constructor to util classes and typos fixed #12

Open
wants to merge 2 commits into
base: v5.0.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 8 additions & 8 deletions core/src/main/java/me/koply/kcommando/boot/AnnotationChecks.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ public AnnotationBox check(Method method, Class<? extends Annotation> annotation
*/
private BoxType commandoCheck(Method method) {
Class<?> returnType = method.getReturnType();
boolean isboolean;
boolean isok = (isboolean = returnType == Boolean.TYPE) || returnType == Void.TYPE;
if (!isok) {
boolean isBoolean = returnType.equals(Boolean.TYPE);
boolean isOk = isBoolean || returnType == Void.TYPE;
if (!isOk) {
if (KCommando.verbose) {
Kogger.info("The return type of " + method.getName() + " neither Boolean nor Void.");
}
Expand All @@ -74,7 +74,7 @@ private BoxType commandoCheck(Method method) {
}
}

return isboolean && type != BoxType.UNKNOWN ? BoxType.fromValue(type.value+3) : type;
return isBoolean && type != BoxType.UNKNOWN ? BoxType.fromValue(type.value+3) : type;
}

/**
Expand Down Expand Up @@ -108,13 +108,13 @@ private BoxType similarCallbackCheck(Method method) {

String typename = params[1].getParameterizedType().getTypeName();

boolean islist = typename.equals("java.util.List<java.lang.String>");
boolean isset = typename.equals("java.util.Set<java.lang.String>");
boolean isList = typename.equals("java.util.List<java.lang.String>");
boolean isSet = typename.equals("java.util.Set<java.lang.String>");

if (!(islist || isset))
if (!(isList || isSet))
return null;

int value = islist ? 9 : 10;
int value = isList ? 9 : 10;

if (params.length == 3 && params[2].getType() == String.class)
value += 2;
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/me/koply/kcommando/boot/ClassLooter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

public class ClassLooter {

private ClassLooter() {}

public static Set<Class<?>> getClasses(List<String> paths) {
Set<Class<?>> classes = new HashSet<>();
paths.forEach(path -> classes.addAll(getClasses(path)));
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/me/koply/kcommando/boot/KInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ private void registerCommandBox(Object instance, AnnotationBox box) {
HandleCommand ann = (HandleCommand) box.annotation;

int type = box.type.value;
boolean isboolean = type > 3;
boolean isBoolean = type > 3;
CommandBox commandBox = new CommandBox(instance, box.method, box.clazz,
CommandBox.CommandType.fromBoxType(type),
isboolean ? CommandBox.ReturnType.BOOLEAN : CommandBox.ReturnType.VOID, ann);
isBoolean ? CommandBox.ReturnType.BOOLEAN : CommandBox.ReturnType.VOID, ann);

for (String alias : ann.aliases()) {
commandManager.commands.put(alias, commandBox);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ public boolean process(Parameters p) {
if (methodName == null) return true;

FalseBox fbox = options.falseBoxMap.get(methodName);
System.out.println(methodName);
if (fbox != null) {
if (KCommando.verbose) Kogger.info("Calling false method: " + methodName);
invoke(fbox.type.value, fbox.instance, fbox.method, p.event, cmdArgs, prefix);
Expand Down Expand Up @@ -197,7 +196,7 @@ private Object invoke(int invokeValue, Object obj, Method method, Object...param
Kogger.warn("An impossible situation happened.");
result = null;
}} catch (InvocationTargetException | IllegalAccessException e) {
Kogger.warn("An error occured while handling the command. Stacktrace:");
Kogger.warn("An error occurred while handling the command. Stacktrace:");
e.printStackTrace();
result = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void process(Parameters p) {
try {
box.method.invoke(box.instance, p.event);
} catch (InvocationTargetException | IllegalAccessException e) {
Kogger.warn("An error occured while handling a slash command. Stacktrace:");
Kogger.warn("An error occurred while handling a slash command. Stacktrace:");
e.printStackTrace();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void removeCustomPrefix(long guildID, String prefix) {
temp.remove(prefix);
return temp;
});
if (customGuildPrefixes.get(guildID).size() == 0) customGuildPrefixes.remove(guildID);
if (customGuildPrefixes.get(guildID).isEmpty()) customGuildPrefixes.remove(guildID);
}
/**
* Removes guild from the custom prefix map
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@

public class Kogger {

private Kogger() {}

private static final Logger LOGGER;
static {
LOGGER = Logger.getLogger("KCommando");
LOGGER.setUseParentHandlers(false);
ConsoleHandler consoleHandler = new ConsoleHandler();
consoleHandler.setFormatter(new Formatter() {
private final DateFormat formatter = new SimpleDateFormat("HH:mm:ss.SSS");
public String format(LogRecord record) {
return String.format("[%s %s] %s -> %s\n", formatter.format(new Date(record.getMillis())), record.getLevel(), record.getLoggerName(), record.getMessage());
public String format(LogRecord logRecord) {
return String.format("[%s %s] %s -> %s%n", formatter.format(new Date(logRecord.getMillis())), logRecord.getLevel(), logRecord.getLoggerName(), logRecord.getMessage());
}
});
LOGGER.addHandler(consoleHandler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public abstract class Box {
public final Method method;
public final Class<?> clazz;

public Box(Object instance, Method method, Class<?> clazz) {
protected Box(Object instance, Method method, Class<?> clazz) {
this.instance = instance;
this.method = method;
this.clazz = clazz;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

public class PackageReader {

private PackageReader() {}

// currently unavailable
// experimental package reader method (to be developed)
// inspired from MaeveS2/SnowballNebula
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

public class StringUtil {

private StringUtil() {}

/**
* JaroWinklerDistance
* Copied from https://commons.apache.org/sandbox/commons-text/jacoco/org.apache.commons.text.similarity/JaroWinklerDistance.java.html
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
public class JavacordIntegration extends Integration {

public final DiscordApi api;

public JavacordIntegration(DiscordApi api) {
super(api.getClientId());
this.api = api;
Expand All @@ -56,7 +57,6 @@ public void registerSlashCommand(SlashBox box) {

String name = info.name();
String desc = info.desc();
boolean isglobal = info.global();

Option[] options = info.options();
List<SlashCommandOption> optionList = new ArrayList<>();
Expand All @@ -79,7 +79,7 @@ public void registerSlashCommand(SlashBox box) {

box.getPerm().ifPresent(perm -> builder.setDefaultEnabledForPermissions(Util.getPermissions(perm.value())));

if (isglobal) {
if (info.global()) {
builder.createGlobal(api).join();
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

public class Util {

private Util() {}

public static PermissionType[] getPermissions(String[] values) {
PermissionType[] ret = new PermissionType[values.length];
PermissionType[] perms = PermissionType.values();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void onMessageCreate(MessageCreateEvent e) {
if (result) cooldownList.put(authorID, ms);
});
} catch (Exception ex) {
Kogger.warn("An error occured while processing a MessageCreateEvent. Stacktrace:");
Kogger.warn("An error occurred while processing a MessageCreateEvent. Stacktrace:");
ex.printStackTrace();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void onSlashCommandCreate(SlashCommandCreateEvent event) {
try {
executorService.submit(() -> handler.process(new SlashCommandHandler.Parameters(event, event.getSlashCommandInteraction().getCommandName())));
} catch (Exception ex) {
Kogger.warn("An error occured while processing a SlashCommandCreateEvent. Stacktrace:");
Kogger.warn("An error occurred while processing a SlashCommandCreateEvent. Stacktrace:");
ex.printStackTrace();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,30 +52,30 @@ public void registerSlashCommand(SlashBox box) {

String name = info.name();
String desc = info.desc();
boolean isglobal = info.global();
boolean isGlobal = info.global();

Option[] options = info.options();
OptionData[] optionDatas = new OptionData[options.length];
int filledDatas = 0;
OptionData[] optionData = new OptionData[options.length];
int filledData = 0;
for (Option option : options) {
if (options[filledDatas].type() == me.koply.kcommando.internal.OptionType.UNKNOWN) continue;
if (options[filledData].type() == me.koply.kcommando.internal.OptionType.UNKNOWN) continue;
OptionType type = OptionType.fromKey(option.type().value);
optionDatas[filledDatas] = new OptionData(type, option.name(), option.desc(), option.required());
filledDatas++;
optionData[filledData] = new OptionData(type, option.name(), option.desc(), option.required());
filledData++;
}

OptionData[] rolledOptionDatas = new OptionData[filledDatas];
System.arraycopy(optionDatas, 0, rolledOptionDatas, 0, filledDatas);
OptionData[] rolledOptionData = new OptionData[filledData];
System.arraycopy(optionData, 0, rolledOptionData, 0, filledData);

boolean guildOnly = !info.enabledInDms();

CommandData data = new CommandDataImpl(name, desc)
.setGuildOnly(guildOnly)
.addOptions(rolledOptionDatas);
.addOptions(rolledOptionData);

box.getPerm().ifPresent(perm -> data.setDefaultPermissions(DefaultMemberPermissions.enabledFor(Util.getPermissions(perm.value()))));

if (isglobal) {
if (isGlobal) {
api.upsertCommand(data).queue();
return;
}
Expand All @@ -89,7 +89,7 @@ public void registerSlashCommand(SlashBox box) {
Guild guild = api.getGuildById(guildId);
if (guild != null) {
guild.upsertCommand(data).queue();
} if (KCommando.verbose) {
} else if (KCommando.verbose) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what?

Copy link
Author

@yungcemic yungcemic Jul 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought you forgot to put else. for readable code conditions must start on a new line.

Kogger.warn("Guild not found for Slash Command named as " + name);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

public class Util {

private Util() {}

public static Permission[] getPermissions(String[] values) {
Permission[] ret = new Permission[values.length];
Permission[] perms = Permission.values();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void onMessageReceived(@NotNull MessageReceivedEvent e) {
)) cooldownList.put(authorID, ms);
});
} catch (Exception ex) {
Kogger.warn("An error occured while processing a MessageReceivedEvent. Stacktrace:");
Kogger.warn("An error occurred while processing a MessageReceivedEvent. Stacktrace:");
ex.printStackTrace();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent even
try {
executorService.submit(() -> handler.process(new SlashCommandHandler.Parameters(event, event.getName())));
} catch (Exception ex) {
Kogger.warn("An error occured while processing a SlashCommandEvent. Stacktrace:");
Kogger.warn("An error occurred while processing a SlashCommandEvent. Stacktrace:");
ex.printStackTrace();
}
}
Expand Down
12 changes: 6 additions & 6 deletions plugin/src/main/java/me/koply/kcommando/plugin/LightYML.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@
import java.util.Map;

public class LightYML {
private boolean Ok = true;
private boolean ok = true;

private final Map<String, String> attributes = new HashMap<>();
public LightYML(final InputStream fileInputStream) {
try {
final InputStreamReader isreader = new InputStreamReader(fileInputStream);
final BufferedReader reader = new BufferedReader(isreader);
final InputStreamReader isReader = new InputStreamReader(fileInputStream);
final BufferedReader reader = new BufferedReader(isReader);

String line;
while ((line = reader.readLine()) != null) {
String[] sided = line.split(":");
attributes.put(sided[0].trim(), sided[1].trim());
}

isreader.close();
isReader.close();
reader.close();
} catch (Exception e) {
e.printStackTrace();
Ok = false;
ok = false;
}
}

Expand All @@ -34,7 +34,7 @@ public final Map<String, String> getAttributes() {
}

public final boolean isOk() {
return Ok && attributes.containsKey("main");
return ok && attributes.containsKey("main");
}

/* Example plugin.yml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,17 @@ public void detectPlugins() {
}

public void enablePlugins() {
final ArrayList<PluginFile<E>> toremove = new ArrayList<>();
final ArrayList<PluginFile<E>> toRemove = new ArrayList<>();
for (PluginFile<E> plugin : plugins) {
final String pluginName = plugin.getYml().getAttributes().get("name");

Class<?> firstSuperClass = plugin.getMainClass().getSuperclass(); // like JDAPlugin and JavacordPlugin
Class<?> secondSuperClass = firstSuperClass.getSuperclass(); // for JavaPlugin from JDAPlugin
boolean isOkey = firstSuperClass == JavaPlugin.class || secondSuperClass == JavaPlugin.class;
boolean isOk = firstSuperClass == JavaPlugin.class || secondSuperClass == JavaPlugin.class;

if (!isOkey) {
if (!isOk) {
logger.warning(pluginName + " could not be enabled. Main class is not extends JavaPlugin.");
toremove.add(plugin);
toRemove.add(plugin);
continue;
}

Expand All @@ -106,13 +106,13 @@ public void enablePlugins() {
plugin.setInstance(instance);
} catch (Exception e) {
logger.warning(pluginName + " could not be enabled.");
toremove.add(plugin);
toRemove.add(plugin);
}
}

PluginCargo.setDelivery(null);

for (PluginFile<E> b : toremove) {
for (PluginFile<E> b : toRemove) {
plugins.remove(b);
}
}
Expand Down