Skip to content

Commit

Permalink
Fix adventure checks in some weird platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
iiAhmedYT committed Nov 4, 2024
1 parent fa7358b commit ffc59f1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import dev.velix.imperat.type.ParameterWorld;
import dev.velix.imperat.util.reflection.Reflections;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -92,12 +93,12 @@ public BukkitConfigBuilder applyBrigadier(boolean supportBrigadier) {
}

private static AdventureProvider<CommandSender> loadAdventure(Plugin plugin, @Nullable AdventureProvider<CommandSender> provider) {
if (Reflections.findClass("net.kyori.adventure.audience.Audience")) {
if (Reflections.findClass(() -> Audience.class)) {
if (provider != null) {
return provider;
} else if (Audience.class.isAssignableFrom(CommandSender.class)) {
return new CastingAdventure<>();
} else if (Reflections.findClass("net.kyori.adventure.platform.bukkit.BukkitAudiences")) {
} else if (Reflections.findClass(() -> BukkitAudiences.class)) {
return new BukkitAdventure(plugin);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import dev.velix.imperat.resolvers.BungeePermissionResolver;
import dev.velix.imperat.type.ParameterProxiedPlayer;
import dev.velix.imperat.util.reflection.Reflections;
import net.kyori.adventure.platform.bungeecord.BungeeAudiences;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Plugin;
Expand All @@ -30,7 +31,7 @@ private BungeeConfigBuilder(Plugin plugin, @Nullable AdventureProvider<CommandSe
}

private AdventureProvider<CommandSender> loadAdventureProvider() {
if (Reflections.findClass("net.kyori.adventure.platform.bungeecord.BungeeAudiences")) {
if (Reflections.findClass(() -> BungeeAudiences.class)) {
return new BungeeAdventure(plugin);
}
return new EmptyAdventure<>();
Expand Down
15 changes: 3 additions & 12 deletions bungee/src/main/java/dev/velix/imperat/BungeeImperat.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package dev.velix.imperat;

import dev.velix.imperat.adventure.AdventureProvider;
import dev.velix.imperat.adventure.BungeeAdventure;
import dev.velix.imperat.adventure.EmptyAdventure;
import dev.velix.imperat.command.BaseImperat;
import dev.velix.imperat.command.Command;
import dev.velix.imperat.util.ImperatDebugger;
import dev.velix.imperat.util.StringUtils;
import dev.velix.imperat.util.reflection.Reflections;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.plugin.Plugin;
import org.jetbrains.annotations.NotNull;

import java.util.HashSet;

Expand All @@ -20,20 +18,13 @@ public final class BungeeImperat extends BaseImperat<BungeeSource> {

BungeeImperat(
Plugin plugin,
AdventureProvider<CommandSender> adventureProvider,
@NotNull AdventureProvider<CommandSender> adventureProvider,
ImperatConfig<BungeeSource> config
) {
super(config);
this.plugin = plugin;
this.adventureProvider = adventureProvider;
ImperatDebugger.setLogger(plugin.getLogger());
this.adventureProvider = adventureProvider != null ? adventureProvider : loadAdventureProvider();
}

private AdventureProvider<CommandSender> loadAdventureProvider() {
if (Reflections.findClass("net.kyori.adventure.platform.bungeecord.BungeeAudiences")) {
return new BungeeAdventure(plugin);
}
return new EmptyAdventure<>();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.function.Supplier;

public final class Reflections {

Expand Down Expand Up @@ -117,6 +118,17 @@ public boolean hasField(final Object target) {
throw new IllegalArgumentException("Cannot find field with valueType " + fieldType);
}

/**
* @return false if the {@link Class} was NOT found
*/
public static boolean findClass(final Supplier<Class<?>> classSupplier) {
try {
return classSupplier.get() != null;
} catch (final Exception e) {
return false;
}
}

/**
* Finds any {@link Class} of the provided paths
*
Expand Down

0 comments on commit ffc59f1

Please sign in to comment.