Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Adventure #126

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions helper/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@
<include>net.kyori:text-serializer-legacy</include>
<include>net.kyori:text-feature-pagination</include>
<include>me.lucko:textlegacy</include>
<include>net.kyori:adventure-api</include>
<include>net.kyori:adventure-text-serializer-gson</include>
<include>net.kyori:adventure-text-serializer-legacy</include>
<include>net.kyori:adventure-platform-bukkit</include>
<include>net.kyori:examination</include>
<include>org.spongepowered:configurate-core</include>
<include>org.spongepowered:configurate-yaml</include>
<include>org.spongepowered:configurate-gson</include>
Expand All @@ -110,6 +115,14 @@
<pattern>net.kyori.text</pattern>
<shadedPattern>me.lucko.helper.text3</shadedPattern>
</relocation>
<!--<relocation>
<pattern>net.kyori.examination</pattern>
<shadedPattern>me.lucko.helper.adventure.examination</shadedPattern>
</relocation>
<relocation>
<pattern>net.kyori.adventure</pattern>
<shadedPattern>me.lucko.helper.adventure</shadedPattern>
</relocation> -->
<relocation>
<pattern>net.kyori.event</pattern>
<shadedPattern>me.lucko.helper.eventbus</shadedPattern>
Expand Down Expand Up @@ -291,6 +304,41 @@
<scope>compile</scope>
<optional>true</optional>
</dependency>
<!-- Adventure -->
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-api</artifactId>
<version>${adventure.version}</version>
<scope>compile</scope>
<optional>true</optional>
<exclusions>
<exclusion>
<groupId>org.checkerframework</groupId>
<artifactId>checker-qual</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-text-serializer-gson</artifactId>
<version>${adventure.version}</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-text-serializer-legacy</artifactId>
<version>${adventure.version}</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-platform-bukkit</artifactId>
<version>4.</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<!-- event -->
<dependency>
<groupId>net.kyori</groupId>
Expand Down
105 changes: 105 additions & 0 deletions helper/src/main/java/me/lucko/helper/adventure/Text.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* This file is part of helper, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <[email protected]>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package me.lucko.helper.adventure;

import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.bukkit.command.CommandSender;

import me.lucko.helper.internal.LoaderUtils;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;

/**
* Utilities for working with {@link Component}s and formatted text strings.
* @since 5.7.0
*/
public class Text {
public static final char SECTION_CHAR = '\u00A7'; // §
public static final char AMPERSAND_CHAR = '&';


public static String joinNewline(String... strings) {
return joinNewline(Arrays.stream(strings));
}

public static String joinNewline(Stream<String> strings) {
return strings.collect(Collectors.joining("\n"));
}

public static Component fromLegacy(String input, char character) {
return LegacyComponentSerializer.legacy(character).deserialize(input);
}

public static Component fromLegacy(String input) {
return LegacyComponentSerializer.legacy(SECTION_CHAR).deserialize(input);
}

public static String toLegacy(Component component, char character) {
return LegacyComponentSerializer.legacy(character).serialize(component);
}

public static String toLegacy(Component component) {
return LegacyComponentSerializer.legacy(SECTION_CHAR).serialize(component);
}

public static void sendMessage(CommandSender sender, Component message) {
LoaderUtils.getAdventure().sender(sender).sendMessage(message);
}

public static void sendMessage(Iterable<CommandSender> senders, Component message) {
for (CommandSender sender : senders) {
sendMessage(sender, message);
}
}

public static String colorize(String s) {
return s == null ? null : translateAlternateColorCodes(AMPERSAND_CHAR, SECTION_CHAR, s);
}

public static String decolorize(String s) {
return s == null ? null : translateAlternateColorCodes(SECTION_CHAR, AMPERSAND_CHAR, s);
}

public static String translateAlternateColorCodes(char from, char to, String textToTranslate) {
char[] b = textToTranslate.toCharArray();
for (int i = 0; i < b.length - 1; i++) {
if (b[i] == from && "0123456789AaBbCcDdEeFfKkLlMmNnOoRrXx".indexOf(b[i+1]) > -1) {
b[i] = to;
b[i+1] = Character.toLowerCase(b[i+1]);
}
}
return new String(b);
}

private Text() {
throw new UnsupportedOperationException("This class cannot be instantiated");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

package me.lucko.helper.bossbar;

import me.lucko.helper.text.Text;
import me.lucko.helper.adventure.Text;

import org.bukkit.Server;
import org.bukkit.boss.BarColor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

package me.lucko.helper.bossbar;

import me.lucko.helper.text.Text;
import me.lucko.helper.adventure.Text;
import me.lucko.helper.utils.ImmutableCollectors;
import me.lucko.helper.utils.Players;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

package me.lucko.helper.command;

import me.lucko.helper.text.Text;
import me.lucko.helper.adventure.Text;

import org.bukkit.command.CommandSender;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@
import me.lucko.helper.config.typeserializers.JsonTreeTypeSerializer;
import me.lucko.helper.config.typeserializers.Text3TypeSerializer;
import me.lucko.helper.config.typeserializers.TextTypeSerializer;
import me.lucko.helper.config.typeserializers.AdventureTypeSerializer;
import me.lucko.helper.datatree.DataTree;
import me.lucko.helper.gson.GsonSerializable;

import net.kyori.text.Component;
import net.kyori.adventure.text.Component;

import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.yaml.snakeyaml.DumperOptions;
Expand Down Expand Up @@ -118,8 +119,8 @@ public HoconConfigurationLoader loader(@Nonnull Path path) {
helperSerializers.register(TypeToken.of(DataTree.class), JsonTreeTypeSerializer.INSTANCE);
helperSerializers.register(TypeToken.of(String.class), ColoredStringTypeSerializer.INSTANCE);
helperSerializers.register(TypeToken.of(me.lucko.helper.text.Component.class), TextTypeSerializer.INSTANCE);
helperSerializers.register(TypeToken.of(Component.class), Text3TypeSerializer.INSTANCE);

helperSerializers.register(TypeToken.of(net.kyori.text.Component.class), Text3TypeSerializer.INSTANCE);
helperSerializers.register(TypeToken.of(Component.class), AdventureTypeSerializer.INSTANCE);
TYPE_SERIALIZERS = helperSerializers.newChild();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* This file is part of helper, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <[email protected]>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package me.lucko.helper.config.typeserializers;

import com.google.common.reflect.TypeToken;
import com.google.gson.JsonElement;

import me.lucko.helper.gson.GsonProvider;

import net.kyori.adventure.text.Component;

import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer;

public class AdventureTypeSerializer implements TypeSerializer<Component> {
public static final AdventureTypeSerializer INSTANCE = new AdventureTypeSerializer();

private AdventureTypeSerializer() {
}

@Override
public Component deserialize(TypeToken<?> typeToken, ConfigurationNode node) throws ObjectMappingException {
JsonElement json = node.getValue(TypeToken.of(JsonElement.class));
return GsonProvider.standard().fromJson(json, typeToken.getType());
}

@Override
public void serialize(TypeToken<?> typeToken, Component component, ConfigurationNode node) throws ObjectMappingException {
JsonElement element = GsonProvider.standard().toJsonTree(component, typeToken.getType());
node.setValue(TypeToken.of(JsonElement.class), element);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

import com.google.common.reflect.TypeToken;

import me.lucko.helper.text.Text;
import me.lucko.helper.adventure.Text;

import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer;

@Deprecated
public class Text3TypeSerializer implements TypeSerializer<Component> {
public static final Text3TypeSerializer INSTANCE = new Text3TypeSerializer();

Expand Down
21 changes: 11 additions & 10 deletions helper/src/main/java/me/lucko/helper/gson/GsonProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@

package me.lucko.helper.gson;

import java.io.Reader;
import java.util.Objects;

import javax.annotation.Nonnull;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
Expand All @@ -35,28 +40,24 @@
import me.lucko.helper.gson.typeadapters.BukkitSerializableAdapterFactory;
import me.lucko.helper.gson.typeadapters.GsonSerializableAdapterFactory;
import me.lucko.helper.gson.typeadapters.JsonElementTreeSerializer;

import net.kyori.text.serializer.gson.GsonComponentSerializer;

import java.io.Reader;
import java.util.Objects;

import javax.annotation.Nonnull;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;

/**
* Provides static instances of Gson
*/
public final class GsonProvider {

private static final Gson STANDARD_GSON = GsonComponentSerializer.populate(new GsonBuilder())
private static final Gson STANDARD_GSON = GsonComponentSerializer.gson().populator()
.apply(new GsonBuilder())
.registerTypeHierarchyAdapter(DataTree.class, JsonElementTreeSerializer.INSTANCE)
.registerTypeAdapterFactory(GsonSerializableAdapterFactory.INSTANCE)
.registerTypeAdapterFactory(BukkitSerializableAdapterFactory.INSTANCE)
.serializeNulls()
.disableHtmlEscaping()
.create();

private static final Gson PRETTY_PRINT_GSON = GsonComponentSerializer.populate(new GsonBuilder())
private static final Gson PRETTY_PRINT_GSON = GsonComponentSerializer.gson().populator()
.apply(new GsonBuilder())
.registerTypeHierarchyAdapter(DataTree.class, JsonElementTreeSerializer.INSTANCE)
.registerTypeAdapterFactory(GsonSerializableAdapterFactory.INSTANCE)
.registerTypeAdapterFactory(BukkitSerializableAdapterFactory.INSTANCE)
Expand Down Expand Up @@ -134,4 +135,4 @@ public static Gson getPrettyPrinting() {
return prettyPrinting();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import me.lucko.helper.serialize.Position;
import me.lucko.helper.terminable.composite.CompositeTerminable;

import me.lucko.helper.text3.Text;
import me.lucko.helper.adventure.Text;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.entity.ArmorStand;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import me.lucko.helper.reflect.ServerReflection;
import me.lucko.helper.serialize.Position;
import me.lucko.helper.terminable.composite.CompositeTerminable;
import me.lucko.helper.text3.Text;
import me.lucko.helper.adventure.Text;
import me.lucko.helper.utils.entityspawner.EntitySpawner;

import org.bukkit.Chunk;
Expand Down
Loading