Search in sources :

Example 6 with Component

use of net.kyori.text.Component in project LuckPerms by lucko.

the class EditorCommand method execute.

@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, List<String> args, String label) {
    Type type = Type.ALL;
    // parse type
    String typeString = ArgumentParser.parseStringOrElse(0, args, null);
    if (typeString != null) {
        try {
            type = Type.valueOf(typeString.toUpperCase());
        } catch (IllegalArgumentException e) {
        // ignored
        }
    }
    // collect holders
    List<PermissionHolder> holders = new ArrayList<>();
    if (type.includingGroups) {
        plugin.getGroupManager().getAll().values().stream().sorted((o1, o2) -> {
            int i = Integer.compare(o2.getWeight().orElse(0), o1.getWeight().orElse(0));
            return i != 0 ? i : o1.getName().compareToIgnoreCase(o2.getName());
        }).forEach(holders::add);
    }
    if (type.includingUsers) {
        plugin.getUserManager().getAll().values().stream().sorted((o1, o2) -> o1.getFriendlyName().compareToIgnoreCase(o2.getFriendlyName())).forEach(holders::add);
    }
    // remove holders which the sender doesn't have perms to view
    holders.removeIf(holder -> ArgumentPermissions.checkViewPerms(plugin, sender, getPermission().get(), holder));
    // they don't have perms to view any of them
    if (holders.isEmpty()) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return CommandResult.NO_PERMISSION;
    }
    Message.EDITOR_START.send(sender);
    // form the payload data
    JsonObject payload = WebEditor.formPayload(holders, sender, label, plugin);
    // upload the payload data to gist
    String pasteId = StandardPastebin.BYTEBIN.postJson(payload, true).id();
    if (pasteId == null) {
        Message.EDITOR_UPLOAD_FAILURE.send(sender);
        return CommandResult.STATE_ERROR;
    }
    // form a url for the editor
    String url = plugin.getConfiguration().get(ConfigKeys.WEB_EDITOR_URL_PATTERN) + "?" + pasteId;
    Message.EDITOR_URL.send(sender);
    Component message = TextComponent.builder(url).color(TextColor.AQUA).clickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, url)).hoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, TextComponent.of("Click to open the editor.").color(TextColor.GRAY))).build();
    sender.sendMessage(message);
    return CommandResult.SUCCESS;
}
Also used : JsonObject(com.google.gson.JsonObject) PermissionHolder(me.lucko.luckperms.common.model.PermissionHolder) LocaleManager(me.lucko.luckperms.common.locale.LocaleManager) SingleCommand(me.lucko.luckperms.common.command.abstraction.SingleCommand) Sender(me.lucko.luckperms.common.sender.Sender) TextComponent(net.kyori.text.TextComponent) HoverEvent(net.kyori.text.event.HoverEvent) ConfigKeys(me.lucko.luckperms.common.config.ConfigKeys) CommandResult(me.lucko.luckperms.common.command.CommandResult) StandardPastebin(me.lucko.luckperms.common.web.StandardPastebin) ArgumentParser(me.lucko.luckperms.common.command.utils.ArgumentParser) ArrayList(java.util.ArrayList) CommandPermission(me.lucko.luckperms.common.command.access.CommandPermission) CommandSpec(me.lucko.luckperms.common.locale.command.CommandSpec) TextColor(net.kyori.text.format.TextColor) List(java.util.List) Component(net.kyori.text.Component) ClickEvent(net.kyori.text.event.ClickEvent) Message(me.lucko.luckperms.common.locale.message.Message) WebEditor(me.lucko.luckperms.common.web.WebEditor) LuckPermsPlugin(me.lucko.luckperms.common.plugin.LuckPermsPlugin) ArgumentPermissions(me.lucko.luckperms.common.command.access.ArgumentPermissions) Predicates(me.lucko.luckperms.common.utils.Predicates) HoverEvent(net.kyori.text.event.HoverEvent) ClickEvent(net.kyori.text.event.ClickEvent) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) PermissionHolder(me.lucko.luckperms.common.model.PermissionHolder) TextComponent(net.kyori.text.TextComponent) Component(net.kyori.text.Component)

Example 7 with Component

use of net.kyori.text.Component in project VoxelGamesLibv2 by VoxelGamesLib.

the class SignPlaceholders method init.

public void init() {
    registerPlaceholders();
    Bukkit.getWorlds().stream().flatMap(w -> Arrays.stream(w.getLoadedChunks())).flatMap(s -> Arrays.stream(s.getTileEntities())).filter(s -> s instanceof Sign).map(s -> (Sign) s).forEach(s -> lastSeenSigns.put(s.getLocation(), s));
    // modify update packets
    protocolManager.addPacketListener(new PacketAdapter(voxelGamesLib, PacketType.Play.Server.TILE_ENTITY_DATA) {

        @Override
        public void onPacketSending(@Nonnull PacketEvent event) {
            int action = event.getPacket().getIntegers().read(0);
            // 9 = set sign text action
            if (action != 9) {
                return;
            }
            NbtCompound data = (NbtCompound) event.getPacket().getNbtModifier().read(0);
            // read data
            Component[] lines = new Component[4];
            String[] rawLines = new String[4];
            for (int i = 0; i < lines.length; i++) {
                lines[i] = ComponentSerializers.JSON.deserialize(data.getString("Text" + (i + 1)));
                rawLines[i] = ChatUtil.toPlainText(lines[i]);
            }
            // sometimes its a double, sometimes its an int...
            double x;
            double y;
            double z;
            try {
                x = data.getDouble("x");
                y = data.getDouble("y");
                z = data.getDouble("z");
            } catch (ClassCastException ex) {
                x = data.getInteger("x");
                y = data.getInteger("y");
                z = data.getInteger("z");
            }
            Location loc = new Location(event.getPlayer().getWorld(), x, y, z);
            if (event.getPlayer().getLocation().distanceSquared(loc) > 200 * 200) {
                return;
            }
            Block b = loc.getBlock();
            if (!(b.getState() instanceof Sign)) {
                return;
            }
            Sign sign = (Sign) b.getState();
            lastSeenSigns.put(loc, sign);
            Optional<User> user = userHandler.getUser(event.getPlayer().getUniqueId());
            if (!user.isPresent()) {
                return;
            }
            // call sign placeholders
            modifySign(user.get(), loc, rawLines, lines);
            // modify packet
            for (int i = 0; i < lines.length; i++) {
                data.put("Text" + (i + 1), ComponentSerializers.JSON.serialize(lines[i]));
            }
        }
    });
    // update task
    new BukkitRunnable() {

        @Override
        public void run() {
            lastSeenSigns.forEach((loc, sign) -> sign.update());
        }
    }.runTaskTimer(voxelGamesLib, 20, 20);
}
Also used : Arrays(java.util.Arrays) ProtocolLibrary(com.comphenix.protocol.ProtocolLibrary) ChunkUnloadEvent(org.bukkit.event.world.ChunkUnloadEvent) UserHandler(com.voxelgameslib.voxelgameslib.user.UserHandler) HashMap(java.util.HashMap) FixedMetadataValue(org.bukkit.metadata.FixedMetadataValue) Singleton(javax.inject.Singleton) Inject(javax.inject.Inject) EventHandler(org.bukkit.event.EventHandler) Block(org.bukkit.block.Block) Location(org.bukkit.Location) PacketAdapter(com.comphenix.protocol.events.PacketAdapter) VoxelGamesLib(com.voxelgameslib.voxelgameslib.VoxelGamesLib) Map(java.util.Map) ChunkLoadEvent(org.bukkit.event.world.ChunkLoadEvent) LocalTime(java.time.LocalTime) PlayerInteractEvent(org.bukkit.event.player.PlayerInteractEvent) Nonnull(javax.annotation.Nonnull) ChatUtil(com.voxelgameslib.voxelgameslib.utils.ChatUtil) Bukkit(org.bukkit.Bukkit) Listener(org.bukkit.event.Listener) ComponentSerializers(net.kyori.text.serializer.ComponentSerializers) Sign(org.bukkit.block.Sign) PacketEvent(com.comphenix.protocol.events.PacketEvent) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PacketType(com.comphenix.protocol.PacketType) TextComponent(net.kyori.text.TextComponent) Logger(java.util.logging.Logger) Component(net.kyori.text.Component) NbtCompound(com.comphenix.protocol.wrappers.nbt.NbtCompound) DateTimeFormatter(java.time.format.DateTimeFormatter) Optional(java.util.Optional) User(com.voxelgameslib.voxelgameslib.user.User) ProtocolManager(com.comphenix.protocol.ProtocolManager) NbtCompound(com.comphenix.protocol.wrappers.nbt.NbtCompound) Optional(java.util.Optional) PacketAdapter(com.comphenix.protocol.events.PacketAdapter) PacketEvent(com.comphenix.protocol.events.PacketEvent) Block(org.bukkit.block.Block) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) Sign(org.bukkit.block.Sign) Location(org.bukkit.Location)

Aggregations

Component (net.kyori.text.Component)7 TextComponent (net.kyori.text.TextComponent)6 ClickEvent (net.kyori.text.event.ClickEvent)5 HoverEvent (net.kyori.text.event.HoverEvent)5 ArrayList (java.util.ArrayList)3 List (java.util.List)3 JsonObject (com.google.gson.JsonObject)2 Map (java.util.Map)2 UUID (java.util.UUID)2 PermissionCache (me.lucko.luckperms.common.caching.type.PermissionCache)2 CommandResult (me.lucko.luckperms.common.command.CommandResult)2 SingleCommand (me.lucko.luckperms.common.command.abstraction.SingleCommand)2 CommandPermission (me.lucko.luckperms.common.command.access.CommandPermission)2 LocaleManager (me.lucko.luckperms.common.locale.LocaleManager)2 CommandSpec (me.lucko.luckperms.common.locale.command.CommandSpec)2 Message (me.lucko.luckperms.common.locale.message.Message)2 User (me.lucko.luckperms.common.model.User)2 LuckPermsPlugin (me.lucko.luckperms.common.plugin.LuckPermsPlugin)2 Sender (me.lucko.luckperms.common.sender.Sender)2 PacketType (com.comphenix.protocol.PacketType)1