Skip to content

Commit

Permalink
Merge pull request #199 from FlintMC/feature/hover-event-content
Browse files Browse the repository at this point in the history
Add method to get the content of HoverEvents as text
  • Loading branch information
zortax committed May 27, 2021
2 parents 2a06b6f + 41acff5 commit 66e85fe
Show file tree
Hide file tree
Showing 33 changed files with 1,190 additions and 374 deletions.
@@ -0,0 +1,71 @@
/*
* FlintMC
* Copyright (C) 2020-2021 LabyMedia GmbH and contributors
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package net.flintmc.mcapi.internal.chat.event;

import net.flintmc.mcapi.chat.component.ChatComponent;
import net.flintmc.mcapi.chat.component.event.HoverEvent;
import net.flintmc.mcapi.chat.component.event.content.HoverContent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class DefaultHoverEvent implements HoverEvent {

private final HoverContent[] contents;
private List<ChatComponent> cachedText;

protected DefaultHoverEvent(HoverContent... contents) {
if (contents.length != 0) {
Action action = contents[0].getAction();
for (HoverContent content : contents) {
if (action != content.getAction()) {
throw new IllegalArgumentException("The action of every content needs to be the same");
}
}
}
this.contents = contents;
}

/**
* {@inheritDoc}
*/
@Override
public HoverContent[] getContents() {
return this.contents;
}

/**
* {@inheritDoc}
*/
@Override
public List<ChatComponent> getAsText() {
if (this.cachedText != null) {
return this.cachedText;
}

List<ChatComponent> components = new ArrayList<>();

for (HoverContent content : this.contents) {
components.addAll(content.getAsText());
}

return this.cachedText = Collections.unmodifiableList(components);
}
}
@@ -0,0 +1,77 @@
/*
* FlintMC
* Copyright (C) 2020-2021 LabyMedia GmbH and contributors
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package net.flintmc.mcapi.internal.chat.event;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.util.UUID;
import net.flintmc.framework.inject.implement.Implement;
import net.flintmc.mcapi.chat.component.ChatComponent;
import net.flintmc.mcapi.chat.component.event.HoverEvent;
import net.flintmc.mcapi.chat.component.event.content.HoverContent;
import net.flintmc.mcapi.chat.component.event.content.HoverContent.Factory;

@Singleton
@Implement(HoverEvent.Factory.class)
public class DefaultHoverEventFactory implements HoverEvent.Factory {

private final HoverContent.Factory contentFactory;

@Inject
private DefaultHoverEventFactory(Factory contentFactory) {
this.contentFactory = contentFactory;
}

/**
* {@inheritDoc}
*/
@Override
public HoverEvent create(HoverContent... contents) {
return new DefaultHoverEvent(contents);
}

/**
* {@inheritDoc}
*/
@Override
public HoverEvent text(ChatComponent... texts) {
HoverContent[] contents = new HoverContent[texts.length];
for (int i = 0; i < texts.length; i++) {
contents[i] = this.contentFactory.text(texts[i]);
}
return this.create(contents);
}

/**
* {@inheritDoc}
*/
@Override
public HoverEvent entity(UUID entityId, String type) {
return this.create(this.contentFactory.entity(entityId, type));
}

/**
* {@inheritDoc}
*/
@Override
public HoverEvent entity(UUID entityId, String type, ChatComponent displayName) {
return this.create(this.contentFactory.entity(entityId, type, displayName));
}
}
Expand Up @@ -23,6 +23,7 @@
import com.google.inject.Singleton;
import net.flintmc.framework.inject.implement.Implement;
import net.flintmc.mcapi.chat.builder.ComponentBuilder;
import net.flintmc.mcapi.chat.component.event.HoverEvent;
import net.flintmc.mcapi.chat.serializer.ComponentSerializer;
import net.flintmc.mcapi.chat.serializer.GsonComponentSerializer;
import net.flintmc.mcapi.internal.chat.serializer.gson.DefaultGsonComponentSerializer;
Expand All @@ -41,20 +42,14 @@ public class DefaultComponentSerializerFactory implements ComponentSerializer.Fa
private DefaultComponentSerializerFactory(
Logger logger,
ComponentBuilder.Factory componentFactory,
LegacyHoverHolder legacyHolder,
GsonChatComponentSerializer componentSerializer) {
DefaultGsonComponentSerializer gson) {
this.legacy =
new PlainComponentSerializer(
logger, componentFactory, true); // plain serializer with all colors/formatting
this.plain =
new PlainComponentSerializer(
logger, componentFactory, false); // plain serializer without any colors/formatting
this.gson =
new DefaultGsonComponentSerializer(
logger,
componentSerializer,
componentFactory,
legacyHolder.isLegacyHoverEvent()); // in 1.16 the hoverEvent has completely changed
this.gson = gson;
}

/**
Expand Down

This file was deleted.

Expand Up @@ -42,6 +42,9 @@ public PlainComponentSerializer(
this.applyFormat = applyFormat;
}

/**
* {@inheritDoc}
*/
@Override
public String serialize(ChatComponent component) {
StringBuilder builder = new StringBuilder();
Expand All @@ -64,6 +67,9 @@ private void serialize(ChatComponent component, StringBuilder builder) {
}
}

/**
* {@inheritDoc}
*/
@Override
public ChatComponent deserialize(String serialized) {
if (!this.applyFormat) {
Expand Down
Expand Up @@ -23,58 +23,74 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.util.HashMap;
import java.util.Map;
import net.flintmc.framework.inject.logging.InjectLogger;
import net.flintmc.mcapi.chat.builder.ComponentBuilder;
import net.flintmc.mcapi.chat.component.ChatComponent;
import net.flintmc.mcapi.chat.component.event.HoverEvent;
import net.flintmc.mcapi.chat.component.event.HoverEvent.Action;
import net.flintmc.mcapi.chat.component.event.content.HoverContent;
import net.flintmc.mcapi.chat.component.event.content.HoverContentSerializer;
import net.flintmc.mcapi.chat.exception.ComponentDeserializationException;
import net.flintmc.mcapi.chat.serializer.GsonComponentSerializer;
import net.flintmc.mcapi.internal.chat.serializer.gson.hover.LegacyHoverEventSerializer;
import net.flintmc.mcapi.internal.chat.serializer.gson.hover.ModernHoverEventSerializer;
import net.flintmc.mcapi.internal.chat.serializer.gson.hover.content.HoverEntitySerializer;
import net.flintmc.mcapi.internal.chat.serializer.gson.hover.content.HoverItemSerializer;
import net.flintmc.mcapi.internal.chat.serializer.gson.hover.content.HoverTextSerializer;
import net.flintmc.mcapi.version.VersionHelper;
import org.apache.logging.log4j.Logger;
import java.util.HashMap;
import java.util.Map;

@Singleton
public class DefaultGsonComponentSerializer implements GsonComponentSerializer {

private final Gson gson;
private final ComponentBuilder.Factory componentFactory;

private final Map<HoverEvent.Action, HoverContentSerializer> hoverContentSerializers =
new HashMap<>();
private final Map<Action, HoverContentSerializer> hoverContentSerializers = new HashMap<>();

public DefaultGsonComponentSerializer(
Logger logger,
@Inject
private DefaultGsonComponentSerializer(
@InjectLogger Logger logger,
GsonChatComponentSerializer componentSerializer,
ComponentBuilder.Factory componentFactory,
boolean legacyHover) {
HoverEvent.Factory eventFactory,
VersionHelper versionHelper,
HoverTextSerializer textSerializer,
HoverEntitySerializer entitySerializer,
HoverItemSerializer itemSerializer) {
this.componentFactory = componentFactory;

this.gson =
new GsonBuilder()
.registerTypeHierarchyAdapter(ChatComponent.class, componentSerializer)
.registerTypeAdapter(
.registerTypeHierarchyAdapter(
HoverEvent.class,
legacyHover
? new LegacyHoverEventSerializer(logger, componentFactory, this)
: new ModernHoverEventSerializer(logger, this, componentFactory))
versionHelper.getMinor() < 16
? new LegacyHoverEventSerializer(logger, componentFactory, this, eventFactory)
: new ModernHoverEventSerializer(logger, this, componentFactory, eventFactory))
.create();

this.registerHoverContentSerializer(HoverEvent.Action.SHOW_TEXT, new HoverTextSerializer());
this.registerHoverContentSerializer(HoverEvent.Action.SHOW_ENTITY, new HoverEntitySerializer());
this.registerHoverContentSerializer(
HoverEvent.Action.SHOW_ACHIEVEMENT,
this.getHoverContentSerializer(HoverEvent.Action.SHOW_TEXT));
this.registerHoverContentSerializer(Action.SHOW_TEXT, textSerializer);
this.registerHoverContentSerializer(Action.SHOW_ENTITY, entitySerializer);
this.registerHoverContentSerializer(Action.SHOW_ACHIEVEMENT, textSerializer);
this.registerHoverContentSerializer(Action.SHOW_ITEM, itemSerializer);
}

/**
* {@inheritDoc}
*/
@Override
public String serialize(ChatComponent component) {
return this.gson.toJson(component);
}

/**
* {@inheritDoc}
*/
@Override
public ChatComponent deserialize(String serialized) {
try {
Expand All @@ -84,26 +100,37 @@ public ChatComponent deserialize(String serialized) {
}
}

/**
* {@inheritDoc}
*/
@Override
public Gson getGson() {
return this.gson;
}

/**
* {@inheritDoc}
*/
@Override
public HoverContent deserializeHoverContent(ChatComponent component, HoverEvent.Action action) {
public HoverContent deserializeHoverContent(ChatComponent component, Action action) {
return this.getHoverContentSerializer(action)
.deserialize(component, this.componentFactory, this.gson);
}

/**
* {@inheritDoc}
*/
@Override
public ChatComponent serializeHoverContent(HoverContent content) {
return this.getHoverContentSerializer(content.getAction())
.serialize(content, this.componentFactory, this.gson);
}

/**
* {@inheritDoc}
*/
@Override
public void registerHoverContentSerializer(
HoverEvent.Action action, HoverContentSerializer serializer) {
public void registerHoverContentSerializer(Action action, HoverContentSerializer serializer) {
Preconditions.checkArgument(
!this.hoverContentSerializers.containsKey(action),
"A serializer for the action %s is already registered",
Expand All @@ -112,8 +139,11 @@ public void registerHoverContentSerializer(
this.hoverContentSerializers.put(action, serializer);
}

/**
* {@inheritDoc}
*/
@Override
public HoverContentSerializer getHoverContentSerializer(HoverEvent.Action action) {
public HoverContentSerializer getHoverContentSerializer(Action action) {
HoverContentSerializer serializer = this.hoverContentSerializers.get(action);
if (serializer == null) {
throw new UnsupportedOperationException("No serializer for the action " + action + " found");
Expand Down

0 comments on commit 66e85fe

Please sign in to comment.