Skip to content

Commit

Permalink
Fix java-documents, Fix issues related to de/serializing ClickEvent a…
Browse files Browse the repository at this point in the history
…nd HoverEvent
  • Loading branch information
iiAhmedYT committed May 14, 2024
1 parent 3812166 commit d3388f1
Show file tree
Hide file tree
Showing 18 changed files with 332 additions and 129 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = 'to.itsme'
version = '3.0.1-BETA'
version = '3.0.2-BETA'
description = 'ItsMyConfig'
java.sourceCompatibility = JavaVersion.VERSION_1_8

Expand Down Expand Up @@ -40,7 +40,7 @@ repositories {
}

def lampVersion = "3.1.9"
def adventureVersion = "4.16.0"
def adventureVersion = "4.17.0"
dependencies {
api "net.kyori:adventure-text-minimessage:${adventureVersion}"
api "net.kyori:adventure-text-serializer-gson:${adventureVersion}"
Expand Down
18 changes: 11 additions & 7 deletions src/main/java/to/itsme/itsmyconfig/ItsMyConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import to.itsme.itsmyconfig.progress.ProgressBar;
import to.itsme.itsmyconfig.progress.ProgressBarBucket;
import to.itsme.itsmyconfig.requirement.RequirementManager;
import to.itsme.itsmyconfig.util.Utilities;

/**
* ItsMyConfig class represents the main configuration class for the plugin.
Expand All @@ -29,6 +30,7 @@
public final class ItsMyConfig extends JavaPlugin {

private static final boolean ALLOW_ITEM_EDITS = false;
private static final String HYPHEN = "#########################################################";

private static ItsMyConfig instance;
private final PlaceholderManager placeholderManager = new PlaceholderManager();
Expand All @@ -44,6 +46,8 @@ public static ItsMyConfig getInstance() {

@Override
public void onEnable() {
this.getLogger().info("\n\n" + HYPHEN + "\n" + Utilities.getACSIIArt() + "\nLoading ItsMyConfig...");
final long start = System.currentTimeMillis();
instance = this;
new DynamicPlaceHolder(this, progressBarBucket).register();
new CommandManager(this);
Expand All @@ -53,22 +57,22 @@ public void onEnable() {

loadConfig();

if (getConfig().getBoolean("bstats", true)) {
new Metrics(this, 21713);
}
new Metrics(this, 21713);

final ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();
protocolManager.addPacketListener(new PacketChatListener(this));

if (ALLOW_ITEM_EDITS) {
protocolManager.addPacketListener(new PacketItemListener(this));
}

this.getLogger().info("ItsMyConfig loaded in " + (System.currentTimeMillis() - start) + "ms\n" + HYPHEN + "\n\n");
}

/**
* The loadConfig method is responsible for loading the configuration file and initializing various settings and data.
* It performs the following steps:
*
* <p>
* 1. Clears all progress bars in the ProgressBarBucket.
* 2. Saves the default configuration file if it does not exist.
* 3. Reloads the configuration file.
Expand All @@ -77,7 +81,6 @@ public void onEnable() {
* 6. Loads the custom progress bars from the configuration and registers them.
*/
public void loadConfig() {

progressBarBucket.clearAllProgressBars();
this.saveDefaultConfig();
this.reloadConfig();
Expand All @@ -103,9 +106,10 @@ private void loadPlaceholders() {
final ConfigurationSection placeholdersConfigSection =
this.getConfig().getConfigurationSection("custom-placeholder");
for (final String identifier : placeholdersConfigSection.getKeys(false)) {
PlaceholderData data = getPlaceholderData(placeholdersConfigSection, identifier);
final long currentTime = System.currentTimeMillis();
final PlaceholderData data = getPlaceholderData(placeholdersConfigSection, identifier);
registerPlaceholder(placeholdersConfigSection, identifier, data);
this.getLogger().info(String.format("Registered placeholder %s", identifier));
this.getLogger().info(String.format("Registered placeholder %s in %dms", identifier, System.currentTimeMillis() - currentTime));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.TranslatableComponent;
import org.jetbrains.annotations.NotNull;
import to.itsme.itsmyconfig.component.event.ClickEvent;
import to.itsme.itsmyconfig.component.event.HoverEvent;
import to.itsme.itsmyconfig.component.impl.KeybindedComponent;
import to.itsme.itsmyconfig.component.impl.PseudoComponent;
import to.itsme.itsmyconfig.component.impl.TextfulComponent;
Expand All @@ -17,6 +19,8 @@
public abstract class AbstractComponent {

private static final Gson GSON = new GsonBuilder()
.registerTypeAdapter(ClickEvent.class, new ClickEvent.Adapter())
.registerTypeAdapter(HoverEvent.class, new HoverEvent.Adapter())
.registerTypeAdapter(TextfulComponent.class, new TextfulComponent.Adapter())
.registerTypeAdapter(KeybindedComponent.class, new KeybindedComponent.Adapter())
.registerTypeAdapter(TranslatingComponent.class, new TranslatingComponent.Adapter())
Expand Down Expand Up @@ -92,4 +96,12 @@ public static AbstractComponent parse(@NotNull final JsonElement element) {

public abstract String toMiniMessage();

public String toJson() {
return GSON.toJson(this);
}

public JsonElement toJsonElement() {
return GSON.toJsonTree(this);
}

}
57 changes: 57 additions & 0 deletions src/main/java/to/itsme/itsmyconfig/component/event/ClickEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package to.itsme.itsmyconfig.component.event;

import com.google.gson.*;

import java.lang.reflect.Type;

public class ClickEvent {

private String action;
private String value;

/**
* Empty Constructor
*/
public ClickEvent() {
}

/**
* {@link net.kyori.adventure.text.event.ClickEvent} convetrer to a {@link ClickEvent}
*/
public ClickEvent(net.kyori.adventure.text.event.ClickEvent event) {
this.action = event.action().toString();
this.value = event.value();
}

public String toMiniMessage() {
return "<click:" + this.action + ":\"" + this.value + "\">";
}

public static final class Adapter implements JsonSerializer<ClickEvent>, JsonDeserializer<ClickEvent> {

public JsonElement serialize(
final ClickEvent event,
final Type type,
final JsonSerializationContext context
) {
final JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("action", event.action);
jsonObject.addProperty("value", event.value);
return jsonObject;
}

public ClickEvent deserialize(
final JsonElement json,
final Type type,
final JsonDeserializationContext context
) throws JsonParseException {
final ClickEvent event = new ClickEvent();
final JsonObject jsonObject = json.getAsJsonObject();
event.action = jsonObject.has("action") ? jsonObject.get("action").getAsString() : null;
event.value = jsonObject.has("value") ? jsonObject.get("value").getAsString() : null;
return event;
}

}

}
154 changes: 154 additions & 0 deletions src/main/java/to/itsme/itsmyconfig/component/event/HoverEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package to.itsme.itsmyconfig.component.event;

import com.google.gson.*;
import net.kyori.adventure.text.TextComponent;
import to.itsme.itsmyconfig.component.AbstractComponent;
import to.itsme.itsmyconfig.component.impl.TextfulComponent;

import java.lang.reflect.Type;
import java.util.UUID;

@SuppressWarnings({"rawtypes", "unused"})
public class HoverEvent {

private String action;
private Object value;

/**
* Empty Constructor
*/
public HoverEvent() {
}

/**
* {@link net.kyori.adventure.text.event.HoverEvent} convetrer to a {@link HoverEvent}
*/
public HoverEvent(net.kyori.adventure.text.event.HoverEvent event) {
this.action = event.action().toString();
switch (this.action) {
case "show_text":
this.value = new TextfulComponent((TextComponent) event.value());
break;
case "show_achievement":
this.value = new TextfulComponent(event.value().toString());
break;
case "show_item":
this.value = new ShowItem((net.kyori.adventure.text.event.HoverEvent.ShowItem) event.value());
break;
case "show_entity":
this.value = new ShowEntity((net.kyori.adventure.text.event.HoverEvent.ShowEntity) event.value());
break;
}
}

public String toMiniMessage() {
StringBuilder builder = new StringBuilder();
builder.append("<hover:").append(this.action).append(":\"");
if (this.value instanceof String) {
builder.append(this.value);
} else if (this.value instanceof AbstractComponent) {
builder.append(((AbstractComponent) this.value).toMiniMessage());
} else if (this.value instanceof ShowItem) {
builder.append(((ShowItem) this.value).toMMArg());
} else if (this.value instanceof ShowEntity) {
builder.append(((ShowEntity) this.value).toMMArg());
}
return builder.append("\">").toString();
}

public static final class ShowItem {
private String item;

private int count;

public ShowItem() {
}

public ShowItem(net.kyori.adventure.text.event.HoverEvent.ShowItem value) {
this.item = value.item().value();
this.count = value.count();
}

public String toMMArg() {
return this.item + ":" + this.count;
}
}

public static final class ShowEntity {

private String type;
private UUID id;
private AbstractComponent name;

public ShowEntity() {
}

public ShowEntity(net.kyori.adventure.text.event.HoverEvent.ShowEntity value) {
this.type = value.type().value();
this.id = value.id();
if (value.name() != null)
this.name = AbstractComponent.parse(value.name());
}

public String toMMArg() {
return this.type + ":" + this.id + ":\"" + this.name.toMiniMessage() + "\"";
}
}

public static final class Adapter implements JsonSerializer<HoverEvent>, JsonDeserializer<HoverEvent> {

public JsonElement serialize(
final HoverEvent event,
final Type type,
final JsonSerializationContext context
) {
final JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("action", event.action);

final Object value = event.value;
if (value != null) {
if (value instanceof String) {
jsonObject.addProperty("value", (String) event.value);
} else if (value instanceof TextfulComponent) {
jsonObject.add("value", ((TextfulComponent) value).toJsonElement());
} else if (value instanceof HoverEvent.ShowItem || value instanceof HoverEvent.ShowEntity) {
jsonObject.add("value", context.serialize(value));
}
}
return jsonObject;
}

public HoverEvent deserialize(
final JsonElement json,
final Type type,
final JsonDeserializationContext context
) throws JsonParseException {
final HoverEvent event = new HoverEvent();
final JsonObject jsonObject = json.getAsJsonObject();
event.action = jsonObject.get("action").getAsString();
if (jsonObject.has("value")) {
final JsonElement element = jsonObject.get("value");
switch (event.action) {
default:
if (element.isJsonPrimitive()) {
event.value = new TextfulComponent(element.getAsString());
} else if (element.isJsonArray() || element.isJsonObject()) {
event.value = context.deserialize(element, TextfulComponent.class);
}
return event;
case "show_achievement":
event.value = element.getAsString();
return event;
case "show_item":
event.value = context.deserialize(element, HoverEvent.ShowItem.class);
return event;
case "show_entity":
break;
}
event.value = context.deserialize(element, HoverEvent.ShowEntity.class);
}
return event;
}
}

}
Loading

0 comments on commit d3388f1

Please sign in to comment.