ca.loganspeck.cortex.util.CortexUtils.java Source code

Java tutorial

Introduction

Here is the source code for ca.loganspeck.cortex.util.CortexUtils.java

Source

/*
 * This file is part of Psynapse, licensed under the MIT License (MIT).
 *
 * Copyright (c) Logan Speck <https://www.loganspeck.ca>
 * 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 ca.loganspeck.cortex.util;

import com.google.common.base.Optional;
import com.google.inject.Inject;
import org.spongepowered.api.Game;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.TextBuilder;
import org.spongepowered.api.text.Texts;
import org.spongepowered.api.text.format.TextColor;
import org.spongepowered.api.text.format.TextColors;
import org.spongepowered.api.util.TextMessageException;
import org.spongepowered.api.util.command.CommandMessageFormatting;

public final class CortexUtils {

    @Inject
    private static Game game;

    /**
     * Get an empty chat box.
     *
     * @return A Text representing an empty chat box.
     * @author mmonkey (https://github.com/mmonkey)
     * <p>
     * Source: https://github.com/mmonkey/Destinations/blob/master/src/main/java/com/github/mmonkey/Destinations/Utilities/FormatUtil.java
     * </p>
     */
    public static Text empty() {

        TextBuilder text = Texts.builder();
        for (int i = 0; i < 20; i++) {
            text.append(CommandMessageFormatting.NEWLINE_TEXT);
        }

        return text.build();
    }

    @Deprecated
    public static Optional<TextColor> getColorFromName(String name) {
        return Optional.of((TextColor) game.getRegistry().getType(TextColor.class, name.toUpperCase()));
    }

    public static Text translateColorCodesFor(String string) {
        try {
            return Texts.legacy('&').from(string);
        } catch (TextMessageException e) {
            e.printStackTrace();
            return null;
        }
    }

}