-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix java-documents, Fix issues related to de/serializing ClickEvent a…
…nd HoverEvent
- Loading branch information
Showing
18 changed files
with
332 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/main/java/to/itsme/itsmyconfig/component/event/ClickEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
154
src/main/java/to/itsme/itsmyconfig/component/event/HoverEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.