Skip to content

Commit

Permalink
Non-player dependant placeholders, fix library loading
Browse files Browse the repository at this point in the history
  • Loading branch information
iiAhmedYT committed Nov 27, 2024
1 parent d04e633 commit 6501a34
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 32 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ dependencies {
implementation "com.alessiodp.libby:libby-bukkit:2.0.0-SNAPSHOT"

compileOnly "net.kyori:adventure-platform-bukkit:$adventurePlatformVersion"
compileOnly "com.comphenix.protocol:ProtocolLib:5.3.0-SNAPSHOT"
compileOnly "io.papermc.paper:paper-api:1.20.4-R0.1-SNAPSHOT"
compileOnly "com.comphenix.protocol:ProtocolLib:5.3.0"
compileOnly "me.clip:placeholderapi:2.11.6"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,39 @@ public ColoredTextPlaceholder(
section,
filePath,
PlaceholderType.COLORED_TEXT,
PlaceholderDependancy.NONE,
PlaceholderDependancy.PLAYER,
PlaceholderDependancy.OFFLINE_PLAYER
);
this.miniText = section.getString("value", "");
this.registerArguments(this.miniText);
}

@Override
public String getResult(String[] args) {
if (args.length == 0) {
return this.miniText;
}

final String firstArg = args[0].toLowerCase(Locale.ROOT);
return switch (firstArg) {
case "l", "legacy" -> this.replaceArguments(
args,
AMPERSAND_SERIALIZER.serialize(
Utilities.translate(this.miniText)
), 1
);
case "c", "console" -> this.replaceArguments(
args,
SECTION_SERIALIZER.serialize(
Utilities.translate(this.miniText)
), 1
);
case "m", "mini" -> this.replaceArguments(args, this.miniText, 1);
default -> this.replaceArguments(args, this.miniText);
};
}

@Override
public String getResult(final Player player, final String[] args) {
if (args.length == 0) {
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/to/itsme/itsmyconfig/util/LibraryLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.alessiodp.libby.LibraryManager;
import com.alessiodp.libby.relocation.Relocation;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer;
import org.bukkit.command.CommandSender;
import to.itsme.itsmyconfig.ItsMyConfig;
import to.itsme.itsmyconfig.util.reflect.Reflections;
Expand All @@ -19,7 +21,7 @@ public enum LibraryLoader {
"net.kyori",
"adventure-text-minimessage",
BuildParameters.ADVENTURE_VERSION,
() -> !Reflections.findClass("net.kyori.adventure.text.Component")
() -> !Reflections.findClass(() -> Component.class)
),
// ========================================================= //
// Adventure Bukkit Platfrom //
Expand All @@ -36,7 +38,7 @@ public enum LibraryLoader {
"net.kyori",
"adventure-text-serializer-bungeecord",
BuildParameters.ADVENTURE_PLATFORM_VERSION,
() -> !Reflections.findClass("net.kyori.adventure.text.serializer.bungeecord")
() -> !Reflections.findClass(() -> BungeeComponentSerializer.class)
)
// ========================================================= //
;
Expand Down Expand Up @@ -83,7 +85,7 @@ public enum LibraryLoader {
}

this.library = builder.build();
this.shouldLoad = load.get();
this.shouldLoad = this.canLoad(load);
}

public static void loadLibraries() {
Expand All @@ -96,4 +98,12 @@ public boolean shouldLoad() {
return shouldLoad;
}

private boolean canLoad(final Supplier<Boolean> load) {
try {
return load.get();
} catch (final Throwable ignored) {
return false;
}
}

}
88 changes: 60 additions & 28 deletions src/main/java/to/itsme/itsmyconfig/util/Utilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import me.clip.placeholderapi.PlaceholderAPI;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.Tag;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
Expand All @@ -19,14 +18,14 @@
import to.itsme.itsmyconfig.font.Font;
import to.itsme.itsmyconfig.font.FontTag;
import to.itsme.itsmyconfig.placeholder.Placeholder;
import to.itsme.itsmyconfig.placeholder.PlaceholderDependancy;
import to.itsme.itsmyconfig.placeholder.type.ColorPlaceholder;
import to.itsme.itsmyconfig.tag.TagManager;

import java.lang.reflect.Field;
import java.util.*;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.stream.Collectors;

/**
* The Utilities class provides various utility methods for performing common tasks.
Expand Down Expand Up @@ -93,22 +92,18 @@ public static void debug(final Supplier<String> supplier, final Throwable except
}

/**
* Translates a String into a component
* Translates a String into a {@link Component}
*
* @param text The text to translate.
* @param player The player translated-for.
* @return The translated component.
* @return The translated {@link Component}.
*/
public static Component translate(
final String text,
final Player player,
final TagResolver... placeholders
) {
final Component translated = EMPTY_MM.deserialize(
TagManager.process(
player, Strings.quote(text)
),
itsMyConfigTag(player), papiTag(player),
Strings.quote(text),
emptyItsMyConfigTag(),
FONT_RESOLVER, StandardTags.defaults(),
TagResolver.resolver(placeholders)
);
Expand All @@ -118,11 +113,11 @@ public static Component translate(
}

/**
* Translates a String into a component
* Translates a String into a {@link Component}
*
* @param text The text to translate.
* @param player The player translated-for.
* @return The translated component.
* @return The translated {@link Component}.
*/
public static Component translate(
final String text,
Expand All @@ -145,26 +140,63 @@ public static Component translate(
}

/**
* Serialized then deserialized components with a click event have their value starting with "&f"
* <br>
* This fixes it.
* Translates a String into a {@link Component}
*
* @return the fixed component
* @deprecated I have NOT seen that happen so far. Hopefully it never does and is already fixed?
* @param text The text to translate.
* @param player The player translated-for.
* @return The translated {@link Component}.
*/
@SuppressWarnings("unused")
@Deprecated(forRemoval = true)
private static Component fixClickEvent(final Component component) {
final ClickEvent event = component.clickEvent();
Component copied = component;
public static Component translate(
final String text,
final Player player,
final TagResolver... placeholders
) {
final Component translated = EMPTY_MM.deserialize(
TagManager.process(
player, Strings.quote(text)
),
itsMyConfigTag(player), papiTag(player),
FONT_RESOLVER, StandardTags.defaults(),
TagResolver.resolver(placeholders)
);

// Serialized then deserialized components with a click event have their value starting with "&f".
if (event != null && event.value().startsWith("&f")) {
copied = component.clickEvent(ClickEvent.clickEvent(event.action(), event.value().substring(2)));
}
applyChatColors(translated);
return translated;
}

/**
* Provides a ItsMyConfig placeholders tag resolver.
*
* @return The ItsMyConfig placeholder tag resolver.
*/
public static TagResolver emptyItsMyConfigTag() {
return TagResolver.resolver("p", (argumentQueue, context) -> {
if (!argumentQueue.hasNext()) {
return Tag.preProcessParsed("Unknown Placeholder");
}

final String name = argumentQueue.popOr("").value();
final Placeholder data = plugin.getPlaceholderManager().get(name);
if (data == null) {
return Tag.preProcessParsed("Unknown Placeholder");
}

if (data instanceof ColorPlaceholder colorPlaceholder) {
return colorPlaceholder.getStyle();
}

final List<String> args = new LinkedList<>();
while (argumentQueue.hasNext()) {
args.add(argumentQueue.pop().value());
}

copied = copied.children(copied.children().stream().map(Utilities::fixClickEvent).collect(Collectors.toList()));
return copied;
if (!data.hasDependency(PlaceholderDependancy.NONE)) {
return Tag.preProcessParsed("");
}

final String parsed = data.asString(args.toArray(new String[0]));
return Tag.preProcessParsed((parsed == null ? "" : parsed).replace("§", "&"));
});
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/to/itsme/itsmyconfig/util/reflect/Reflections.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.Objects;
import java.util.function.Supplier;

@SuppressWarnings("unused")
public final class Reflections {
Expand Down Expand Up @@ -133,6 +134,17 @@ public static boolean findClass(final String... paths) {
return false;
}

/**
* @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;
}
}

/**
* A nullable {@link Class#forName(String)} instead of throwing exceptions
*
Expand Down

0 comments on commit 6501a34

Please sign in to comment.