Skip to content

Commit

Permalink
3.3.0 BETA, Performance update
Browse files Browse the repository at this point in the history
  • Loading branch information
iiAhmedYT committed Jul 16, 2024
1 parent 843785f commit 6e88093
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 129 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = 'to.itsme'
version = '3.2.2'
version = '3.3.0-BETA'
description = 'ItsMyConfig'
java.sourceCompatibility = JavaVersion.VERSION_1_8
repositories {
Expand Down Expand Up @@ -83,7 +83,7 @@ dependencies {
implementation "com.github.Redempt:Crunch:2.0.3"

compileOnly "org.spigotmc:spigot-api:1.12.2-R0.1-SNAPSHOT"
compileOnly "com.comphenix.protocol:ProtocolLib:5.1.0"
compileOnly "com.comphenix.protocol:ProtocolLib:5.3.0-SNAPSHOT"
compileOnly "me.clip:placeholderapi:2.11.1"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import to.itsme.itsmyconfig.placeholder.Placeholder;
import to.itsme.itsmyconfig.placeholder.PlaceholderType;
import to.itsme.itsmyconfig.util.Message;
import to.itsme.itsmyconfig.util.Strings;
import to.itsme.itsmyconfig.util.Utilities;

import java.io.IOException;
Expand Down Expand Up @@ -62,7 +63,7 @@ private TagResolver pluginInfo() {
.decorate(TextDecoration.BOLD)
.hoverEvent(
Utilities.MM.deserialize(
Utilities.toString(
Strings.toString(
Arrays.asList(
" ",
"<white>Name: <gold>" + description.getName(),
Expand All @@ -83,7 +84,7 @@ private TagResolver authorInfo() {
.decorate(TextDecoration.UNDERLINED)
.hoverEvent(
Utilities.MM.deserialize(
Utilities.toString(
Strings.toString(
Arrays.asList(
" ",
"<white>Discord: <aqua>@iiAhmedYT</aqua>",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import to.itsme.itsmyconfig.ItsMyConfig;
import to.itsme.itsmyconfig.component.AbstractComponent;
import to.itsme.itsmyconfig.listener.PacketListener;
import to.itsme.itsmyconfig.util.Reflections;
import to.itsme.itsmyconfig.util.Utilities;

import java.lang.reflect.Method;
Expand Down Expand Up @@ -45,34 +44,30 @@ public PacketChatListener(

@Override
public void onPacketSending(final PacketEvent event) {
Utilities.debug("################# CHAT PACKET #################");
final PacketContainer container = event.getPacket();
Utilities.debug("Analyzing packet " + container.getType().name());
Utilities.debug(() -> "################# CHAT PACKET #################\nProccessing packet " + container.getType().name());
final PacketResponse response = this.processPacket(container);
if (response == null || response.message.isEmpty()) {
Utilities.debug("###############################################");
Utilities.debug(() -> "Packet is null or empty\n###############################################");
return;
}

final String message = response.message;
Utilities.debug("Checking: " + message);
Utilities.debug(() -> "Checking: " + message);
if (!this.startsWithSymbol(message)) {
Utilities.debug("Message doesn't start w/ the symbol-prefix: " + message);
Utilities.debug("###############################################");
Utilities.debug(() -> "Message doesn't start w/ the symbol-prefix: " + message + "\n###############################################");
return;
}

final Player player = event.getPlayer();
final Component parsed = Utilities.translate(this.processMessage(message), player);

if (parsed.equals(Component.empty())) {
Utilities.debug("Component is empty, cancelling...");
event.setCancelled(true);
Utilities.debug("###############################################");
Utilities.debug(() -> "Component is empty, cancelling...\n###############################################");
return;
}

Utilities.debug("Overriding Message as " + response.type.name());
Utilities.debug(() -> "Overriding Message as " + response.type.name());
switch (response.type) {
case JSON:
container.getStrings().write(0, gsonComponentSerializer.serialize(parsed));
Expand All @@ -84,46 +79,45 @@ public void onPacketSending(final PacketEvent event) {
break;
case BUNGEE_COMPONENT:
container.getModifier().withType(TextComponent.class).write(0, new TextComponent(
BungeeComponentSerializer.get().serialize(parsed)
bungee.serialize(parsed)
));
break;
case SERVER_ADVENTURE:
final StructureModifier<Object> modifier = container.getModifier().withType(AdventureComponentConverter.getComponentClass());
final String json = gsonComponentSerializer.serialize(parsed);
modifier.write(0, Reflections.fromJsonToComponent(json));
modifier.write(0, AdventureComponentConverter.fromJsonAsObject(json));
break;
}

Utilities.debug("###############################################");
Utilities.debug(() -> "###############################################");
}

private PacketResponse processPacket(final PacketContainer container) {
Utilities.debug("Proccessing a packet");
final StructureModifier<TextComponent> textComponentModifier = container.getModifier().withType(TextComponent.class);
if (textComponentModifier.size() == 1) {
Utilities.debug("Using Bungeecord TextComponent..");
Utilities.debug(() -> "Using Bungeecord TextComponent..");
return new PacketResponse(ResponseType.BUNGEE_COMPONENT, processBaseComponents(textComponentModifier.readSafely(0)));
} else {
Utilities.debug("Failed to use Bungeecord TextComponent, trying ProtocolLib's WrappedChatComponent");
Utilities.debug(() -> "Failed to use Bungeecord TextComponent, trying ProtocolLib's WrappedChatComponent");
}

final WrappedChatComponent wrappedComponent = container.getChatComponents().readSafely(0);
if (wrappedComponent != null) {
final String found = wrappedComponent.getJson();
if (!found.isEmpty()) {
Utilities.debug("Found String: " + found);
Utilities.debug(() -> "Found String: " + found);
try {
Utilities.debug("Trying as json");
Utilities.debug(() -> "Trying as json");
return new PacketResponse(ResponseType.WRAPPED_COMPONENT, AbstractComponent.parse(found).toMiniMessage());
} catch (final Exception e) {
Utilities.debug("An error happened while de/serializing " + found + ": ", e);
Utilities.debug(() -> "An error happened while de/serializing " + found + ": ", e);
}
}
}

final String rawMessage = container.getStrings().readSafely(0);
if (rawMessage != null) {
Utilities.debug("Raw-Parsing message: " + rawMessage);
Utilities.debug(() -> "Raw-Parsing message: " + rawMessage);
return new PacketResponse(ResponseType.JSON, AbstractComponent.parse(rawMessage).toMiniMessage());
}

Expand All @@ -133,17 +127,16 @@ private PacketResponse processPacket(final PacketContainer container) {
if (modifier.size() == 1) {
final WrappedChatComponent wrappedAComponent = (WrappedChatComponent) fromComponent.invoke(null, modifier.readSafely(0));
final String json = wrappedAComponent.getJson();
Utilities.debug("Performing Server-Side Adventure for " + json);
Utilities.debug(() -> "Performing Server-Side Adventure for " + json);
return new PacketResponse(ResponseType.SERVER_ADVENTURE, AbstractComponent.parse(json).toMiniMessage());
} else {
Utilities.debug("Failed to use Server-Side Adventure, Trying Bungeecord TextComponent..");
Utilities.debug(() -> "Failed to use Server-Side Adventure");
}
} catch (Throwable ignored) {
Utilities.debug("Failed to use Server-Side Adventure, Trying Bungeecord TextComponent..");
Utilities.debug(() -> "Failed to use Server-Side Adventure");
}
}

Utilities.debug("Found nothing.. returning null.");
return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package to.itsme.itsmyconfig.placeholder.type;

import net.kyori.adventure.text.format.*;
import net.kyori.adventure.text.minimessage.tag.Tag;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
Expand All @@ -18,7 +19,7 @@ public final class ColorPlaceholder extends Placeholder {
/**
* Represents the style of a variable.
*/
private Style style;
private Tag style;
/**
* Represents a legacy color value.
*/
Expand Down Expand Up @@ -116,25 +117,26 @@ public ColorPlaceholder(
* @param configurationSection The ConfigurationSection containing the style properties.
*/
private void initializeStyle(ConfigurationSection configurationSection) {
final Style.Builder builder = Style.style().color(TextColor.fromHexString(hexValue));

final StringBuilder propertiesBuilder = new StringBuilder();
final StringBuilder propertiesPrefixBuilder = new StringBuilder();
final StringBuilder propertiesSuffixBuilder = new StringBuilder();

for (final String decorationType : DECORATIONS_PROPERTIES.keySet()) {
if (configurationSection.getBoolean(decorationType)) {
propertiesBuilder.append(DECORATIONS_PROPERTIES.get(decorationType));
propertiesPrefixBuilder.append("<").append(decorationType).append(">");
propertiesSuffixBuilder.append("</").append(decorationType).append(">");
builder.decorate(TextDecoration.valueOf(decorationType.toUpperCase(Locale.ENGLISH)));
this.style = Tag.styling(builder -> {
builder.color(TextColor.fromHexString(hexValue));

final StringBuilder propertiesBuilder = new StringBuilder();
final StringBuilder propertiesPrefixBuilder = new StringBuilder();
final StringBuilder propertiesSuffixBuilder = new StringBuilder();

for (final String decorationType : DECORATIONS_PROPERTIES.keySet()) {
if (configurationSection.getBoolean(decorationType)) {
propertiesBuilder.append(DECORATIONS_PROPERTIES.get(decorationType));
propertiesPrefixBuilder.append("<").append(decorationType).append(">");
propertiesSuffixBuilder.append("</").append(decorationType).append(">");
builder.decorate(TextDecoration.valueOf(decorationType.toUpperCase(Locale.ENGLISH)));
}
}
}

this.properties = propertiesBuilder.toString();
this.propertiesMiniPrefix = propertiesPrefixBuilder.toString();
this.propertiesMiniSuffix = propertiesSuffixBuilder.toString();
this.style = builder.build();
this.properties = propertiesBuilder.toString();
this.propertiesMiniPrefix = propertiesPrefixBuilder.toString();
this.propertiesMiniSuffix = propertiesSuffixBuilder.toString();
});
}

/**
Expand Down Expand Up @@ -187,7 +189,7 @@ public String getResult(final Player player, final String[] params) {
*
* @return The style of this instance.
*/
public Style getStyle() {
public Tag getStyle() {
return this.style;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/to/itsme/itsmyconfig/util/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public String toString() {

final String result;
if (msg instanceof List<?>) {
result = Utilities.toString((List<?>) msg);
result = Strings.toString((List<?>) msg);
} else if (msg instanceof String) {
result = (String) msg;
} else {
Expand Down
31 changes: 0 additions & 31 deletions src/main/java/to/itsme/itsmyconfig/util/Reflections.java

This file was deleted.

11 changes: 11 additions & 0 deletions src/main/java/to/itsme/itsmyconfig/util/Strings.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package to.itsme.itsmyconfig.util;

import net.kyori.adventure.text.minimessage.tag.standard.StandardTags;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -215,4 +216,14 @@ public static String integerToRoman(int num) {
return roman.toString();
}

/**
* Converts a list of objects to a string, where each object is represented on a new line.
*
* @param list The list of objects to be converted to a string.
* @return The string representation of the list.
*/
public static String toString(final @NotNull List<?> list) {
return String.join(System.lineSeparator(), list.stream().map(Object::toString).toArray(String[]::new));
}

}
Loading

0 comments on commit 6e88093

Please sign in to comment.