Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #199 from FlintMC/feature/hover-event-content
Add method to get the content of HoverEvents as text
- Loading branch information
Showing
33 changed files
with
1,190 additions
and
374 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
mcapi/src/internal/java/net/flintmc/mcapi/internal/chat/event/DefaultHoverEvent.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,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); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
mcapi/src/internal/java/net/flintmc/mcapi/internal/chat/event/DefaultHoverEventFactory.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,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)); | ||
} | ||
} |
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
37 changes: 0 additions & 37 deletions
37
mcapi/src/internal/java/net/flintmc/mcapi/internal/chat/serializer/LegacyHoverHolder.java
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.