Skip to content

Commit

Permalink
* Completely removed old task scheduling utility for newly optimized …
Browse files Browse the repository at this point in the history
…alternative.

* Optimized task execution handling.
* Added new isModded check to LegacyCheckService
* Added new toBlockFace method to DirectivePoint
* Optimized internal Unity task executions.
* Finalized OrdinalProcedure service.
* Completely removed old PaginatedList.java utility.
* Finalized sub-command injection handling.
* Optimized and fixed remaining issues with Message.java abstraction in tandem with ComponentUtil.
  • Loading branch information
Hempfest committed May 31, 2022
1 parent 3208ab8 commit 8fcf40a
Show file tree
Hide file tree
Showing 58 changed files with 1,216 additions and 1,577 deletions.
2 changes: 1 addition & 1 deletion labyrinth-common/pom.xml
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>labyrinth</artifactId>
<groupId>com.github.the-h-team</groupId>
<version>1.7.7</version>
<version>1.7.8</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
@@ -1,11 +1,17 @@
package com.github.sanctum.labyrinth.api;

import com.github.sanctum.labyrinth.library.StringUtils;
import org.bukkit.Bukkit;

/**
* Detects legacy server environments.
*/
public interface LegacyCheckService extends Service {

default boolean isModded() {
return StringUtils.use(Bukkit.getServer().getName()).containsIgnoreCase("forge", "magma");
}

/**
* Check if the environment of the server is legacy.
*
Expand Down
@@ -1,19 +1,35 @@
package com.github.sanctum.labyrinth.command;

import com.github.sanctum.labyrinth.LabyrinthProvider;
import com.github.sanctum.labyrinth.annotation.Ordinal;
import com.github.sanctum.labyrinth.data.container.LabyrinthCollection;
import com.github.sanctum.labyrinth.data.container.LabyrinthCollectionBase;
import com.github.sanctum.labyrinth.data.container.LabyrinthList;
import com.github.sanctum.labyrinth.interfacing.OrdinalProcedure;
import com.github.sanctum.labyrinth.library.CommandUtils;
import com.github.sanctum.labyrinth.library.StringUtils;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.bukkit.command.Command;
import org.bukkit.command.CommandMap;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* A class designed for easy bukkit sub command flow. Append or remove sub labels to registered commands.
*
* @author Hempfest
*/
public abstract class SubCommandList extends LabyrinthCollectionBase<SubCommand> {
protected final Crossover parent;

Expand All @@ -22,10 +38,71 @@ protected SubCommandList(@NotNull Command parent) {
this.parent = new Crossover(parent);
}

public final String getCommand() {
/**
* Get the main command label this sub command belongs to.
*
* @return The main command this sub command is for.
*/
public final @NotNull String getCommand() {
return parent.getLabel();
}

/**
* Get a sub command from this list by its label.
*
* @param label The label of the sub command.
* @return a sub command match or null if not found.
*/
public final @Nullable SubCommand getSubCommand(@NotNull String label) {
return stream().filter(s -> s.getLabel().equalsIgnoreCase(label)).findFirst().orElse(null);
}

/**
* Register a sub command into this list.
*
* @param subCommand The command to register.
*/
public final void register(@NotNull SubCommand subCommand) {
if (subCommand.getCommand().equalsIgnoreCase(getCommand())) {
final Command parent = CommandUtils.getCommandByLabel(subCommand.getCommand());
if (parent != null) {
final Plugin plugin = Optional.of((Plugin)JavaPlugin.getProvidingPlugin(parent.getClass())).orElseGet(() -> {
if (parent instanceof PluginCommand) {
return ((PluginCommand)parent).getPlugin();
} else return LabyrinthProvider.getInstance().getPluginInstance();
});
CommandUtils.read(entry -> {
Map<String, Command> commandMappings = entry.getValue();
CommandMap map = entry.getKey();
commandMappings.remove(parent.getName());
for (String alias : parent.getAliases()) {
if (commandMappings.containsKey(alias) && commandMappings.get(alias).getAliases().contains(alias)) {
commandMappings.remove(alias);
}
}
parent.unregister(map);
map.register(getCommand(), plugin.getName(), parent);
if (!contains(subCommand)) add(subCommand);
return this;
});
} else throw new IllegalArgumentException("Command " + subCommand.getCommand() + " either not found or not loaded yet.");
}
}

/**
* Unregister a sub command from this list.
*
* @param subCommand The command to unregister.
*/
public final void unregister(@NotNull SubCommand subCommand) {
if (subCommand.getCommand().equalsIgnoreCase(getCommand())) {
final Command parent = CommandUtils.getCommandByLabel(subCommand.getCommand());
if (parent != null) {
if (contains(subCommand)) remove(subCommand);
} else throw new IllegalArgumentException("Command " + subCommand.getCommand() + " either not found or not loaded yet.");
}
}

class Crossover extends Command {

private final Command command;
Expand Down
@@ -1,5 +1,6 @@
package com.github.sanctum.labyrinth.data;

import com.github.sanctum.labyrinth.LabyrinthProvider;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.io.File;
Expand All @@ -13,6 +14,7 @@
import java.util.Optional;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;

/**
Expand Down Expand Up @@ -40,9 +42,10 @@ final class AddonClassLoader extends URLClassLoader {

private AddonClassLoader(URL[] urls) {
super(urls, javaPlugin.getClass().getClassLoader());
Bukkit.getLogger().severe(javaPlugin.getClass().getClassLoader().getClass().getName());
}

final Class<?> resolveClass(String name) throws ClassNotFoundException {
Class<?> resolveClass(String name) throws ClassNotFoundException {
return loadClass(name, true);
}

Expand Down
Expand Up @@ -2,12 +2,8 @@

import com.github.sanctum.labyrinth.LabyrinthProvider;
import com.github.sanctum.labyrinth.api.Service;
import com.github.sanctum.labyrinth.task.Schedule;
import com.github.sanctum.labyrinth.task.TaskScheduler;
import com.google.gson.GsonBuilder;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -69,7 +65,7 @@ public String getString() {

@Override
public int getInt() {
return config.getInt(this.key);
return config.getInt(this.key);
}

@Override
Expand Down Expand Up @@ -237,7 +233,7 @@ public boolean create() {
if (config.getType() == FileType.JSON) {
set(new Object());
} else {
((YamlConfiguration)config).getConfig().createSection(this.key);
((YamlConfiguration) config).getConfig().createSection(this.key);
}
save();
return false;
Expand Down Expand Up @@ -286,17 +282,17 @@ public Set<String> getKeys(boolean deep) {
Set<String> keys = new HashSet<>();
JsonConfiguration json = (JsonConfiguration) config;
if (json.get(this.key) instanceof Map) {
Map<String, Object> map1 = (Map<String, Object>)json.get(this.key);
Map<String, Object> map1 = (Map<String, Object>) json.get(this.key);
if (deep) {
for (Map.Entry<String, Object> entry : map1.entrySet()) {
if (entry.getValue() instanceof Map) {
Map<String, Object> map2 = (Map<String, Object>)entry.getValue();
Map<String, Object> map2 = (Map<String, Object>) entry.getValue();
for (Map.Entry<String, Object> entry2 : map2.entrySet()) {
if (entry2.getValue() instanceof Map) {
Map<String, Object> map3 = (Map<String, Object>)entry2.getValue();
Map<String, Object> map3 = (Map<String, Object>) entry2.getValue();
for (Map.Entry<String, Object> entry3 : map3.entrySet()) {
if (entry3.getValue() instanceof Map) {
Map<String, Object> map4 = (Map<String, Object>)entry2.getValue();
Map<String, Object> map4 = (Map<String, Object>) entry2.getValue();
for (Map.Entry<String, Object> entry4 : map4.entrySet()) {
keys.add(this.key + "." + entry.getKey() + "." + entry2.getKey() + "." + entry3.getKey() + "." + entry4.getKey());
}
Expand Down Expand Up @@ -331,17 +327,17 @@ public Map<String, Object> getValues(boolean deep) {
Map<String, Object> map = new HashMap<>();
JsonConfiguration json = (JsonConfiguration) config;
if (json.get(this.key) instanceof Map) {
Map<String, Object> map1 = (Map<String, Object>)json.get(this.key);
Map<String, Object> map1 = (Map<String, Object>) json.get(this.key);
if (deep) {
for (Map.Entry<String, Object> entry : map1.entrySet()) {
if (entry.getValue() instanceof Map) {
Map<String, Object> map2 = (Map<String, Object>)entry.getValue();
Map<String, Object> map2 = (Map<String, Object>) entry.getValue();
for (Map.Entry<String, Object> entry2 : map2.entrySet()) {
if (entry2.getValue() instanceof Map) {
Map<String, Object> map3 = (Map<String, Object>)entry2.getValue();
Map<String, Object> map3 = (Map<String, Object>) entry2.getValue();
for (Map.Entry<String, Object> entry3 : map3.entrySet()) {
if (entry3.getValue() instanceof Map) {
Map<String, Object> map4 = (Map<String, Object>)entry2.getValue();
Map<String, Object> map4 = (Map<String, Object>) entry2.getValue();
for (Map.Entry<String, Object> entry4 : map4.entrySet()) {
map.put(this.key + "." + entry.getKey() + "." + entry2.getKey() + "." + entry3.getKey() + "." + entry4.getKey(), entry4.getValue());
}
Expand All @@ -359,13 +355,13 @@ public Map<String, Object> getValues(boolean deep) {
} else {
for (Map.Entry<String, Object> entry : map1.entrySet()) {
if (entry.getValue() instanceof Map) {
Map<String, Object> map2 = (Map<String, Object>)entry.getValue();
Map<String, Object> map2 = (Map<String, Object>) entry.getValue();
for (Map.Entry<String, Object> entry2 : map2.entrySet()) {
if (entry2.getValue() instanceof Map) {
Map<String, Object> map3 = (Map<String, Object>)entry2.getValue();
Map<String, Object> map3 = (Map<String, Object>) entry2.getValue();
for (Map.Entry<String, Object> entry3 : map3.entrySet()) {
if (entry3.getValue() instanceof Map) {
Map<String, Object> map4 = (Map<String, Object>)entry2.getValue();
Map<String, Object> map4 = (Map<String, Object>) entry2.getValue();
for (Map.Entry<String, Object> entry4 : map4.entrySet()) {
map.put(this.key + "." + entry.getKey() + "." + entry2.getKey() + "." + entry3.getKey() + "." + entry4.getKey(), entry4.getValue());
}
Expand Down
Expand Up @@ -15,7 +15,6 @@
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;

/**
Expand All @@ -24,7 +23,7 @@
* @param <T> A type of annotation.
* @param <R> A listener to use.
*/
public final class AnnotationDiscovery<T extends Annotation, R> implements Iterable<Method>{
public final class AnnotationDiscovery<T extends Annotation, R> implements Iterable<Method> {

private final int count;
private final Class<T> annotation;
Expand All @@ -41,7 +40,8 @@ public final class AnnotationDiscovery<T extends Annotation, R> implements Itera
for (Method method : rClass.getDeclaredMethods()) {
try {
method.setAccessible(true);
} catch (Exception ignored) {}
} catch (Exception ignored) {
}
if (method.isAnnotationPresent(annotation)) {
annotated++;
}
Expand All @@ -59,7 +59,8 @@ public final class AnnotationDiscovery<T extends Annotation, R> implements Itera
for (Method method : rClass.getDeclaredMethods()) {
try {
method.setAccessible(true);
} catch (Exception ignored) {}
} catch (Exception ignored) {
}
if (method.isAnnotationPresent(annotation)) {
annotated++;
}
Expand Down Expand Up @@ -105,7 +106,7 @@ public AnnotationDiscovery<T, R> filter(Predicate<? super Method> predicate) {
* Filter the methods and only work with ones of interest.
*
* @param predicate The filtration.
* @param hard whether or not to breach accessibility.
* @param hard whether or not to breach accessibility.
* @return The same annotation discovery object.
*/
public AnnotationDiscovery<T, R> filter(Predicate<? super Method> predicate, boolean hard) {
Expand All @@ -118,7 +119,8 @@ public AnnotationDiscovery<T, R> filter(Predicate<? super Method> predicate, boo
methods.addAll(Arrays.stream(this.rClass.getDeclaredMethods()).filter(m -> {
try {
m.setAccessible(true);
} catch (Exception ignored){}
} catch (Exception ignored) {
}
return m.isAnnotationPresent(annotation) && predicate.test(m);
}).collect(Collectors.toList()));
}
Expand All @@ -140,25 +142,25 @@ public boolean isPresent() {
*/
public void ifPresent(WideConsumer<T, Method> function) {
if (isPresent()) {
for (Method m : methods) {
methods.forEach(m -> {
for (Annotation a : m.getAnnotations()) {
if (annotation.isAssignableFrom(a.annotationType())) {
function.accept((T) a, m);
}
}
}
});
}
}

/**
* Get information from the leading source objects located annotation.
*
* <p>
* This method gives you access to an annotation and the source object itself.
*
* @deprecated Use {@link AnnotationDiscovery#mapFromClass(AnnotativeConsumer)} instead!
* @param function The function.
* @param <U> The desired return value.
* @param <U> The desired return value.
* @return A value from an annotation.
* @deprecated Use {@link AnnotationDiscovery#mapFromClass(AnnotativeConsumer)} instead!
*/
@Deprecated
public <U> U map(AnnotativeConsumer<T, R, U> function) {
Expand All @@ -167,11 +169,11 @@ public <U> U map(AnnotativeConsumer<T, R, U> function) {

/**
* Get information from the leading source objects located annotation.
*
* <p>
* This method gives you access to an annotation and the source object itself.
*
* @param function The function.
* @param <U> The desired return value.
* @param <U> The desired return value.
* @return A value from an annotation.
*/
public <U> U mapFromClass(AnnotativeConsumer<T, R, U> function) {
Expand All @@ -183,11 +185,11 @@ public <U> U mapFromClass(AnnotativeConsumer<T, R, U> function) {

/**
* Get information from the leading source objects methods found with the specified annotation.
*
* <p>
* This method gives you access to an annotation and the source object itself.
*
* @param function The function.
* @param <U> The desired return value.
* @param <U> The desired return value.
* @return A value from an annotation.
*/
public <U> List<U> mapFromMethods(AnnotativeConsumer<T, R, U> function) {
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit 8fcf40a

Please sign in to comment.