Skip to content

Instantly share code, notes, and snippets.

@dumptruckman
Created January 19, 2016 15:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dumptruckman/4754c29ea0c0189d7836 to your computer and use it in GitHub Desktop.
Save dumptruckman/4754c29ea0c0189d7836 to your computer and use it in GitHub Desktop.
A simple utility class for sending action bar messages.
/* Copyright (c) dumptruckman 2016
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package net.dawnofages.util;
import net.minecraft.server.v1_8_R3.IChatBaseComponent;
import net.minecraft.server.v1_8_R3.IChatBaseComponent.ChatSerializer;
import net.minecraft.server.v1_8_R3.PacketPlayOutChat;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.Map;
public class ActionBarUtil {
private static final Map<Player, BukkitTask> PENDING_MESSAGES = new HashMap<>();
/**
* Sends a message to the player's action bar.
* <p/>
* The message will appear above the player's hot bar for 2 seconds and then fade away over 1 second.
*
* @param bukkitPlayer the player to send the message to.
* @param message the message to send.
*/
public static void sendActionBarMessage(@NotNull Player bukkitPlayer, @NotNull String message) {
sendRawActionBarMessage(bukkitPlayer, "{\"text\": \"" + message + "\"}");
}
/**
* Sends a raw message (JSON format) to the player's action bar. Note: while the action bar accepts raw messages
* it is currently only capable of displaying text.
* <p/>
* The message will appear above the player's hot bar for 2 seconds and then fade away over 1 second.
*
* @param bukkitPlayer the player to send the message to.
* @param rawMessage the json format message to send.
*/
public static void sendRawActionBarMessage(@NotNull Player bukkitPlayer, @NotNull String rawMessage) {
CraftPlayer player = (CraftPlayer) bukkitPlayer;
IChatBaseComponent chatBaseComponent = ChatSerializer.a(rawMessage);
PacketPlayOutChat packetPlayOutChat = new PacketPlayOutChat(chatBaseComponent, (byte) 2);
player.getHandle().playerConnection.sendPacket(packetPlayOutChat);
}
/**
* Sends a message to the player's action bar that lasts for an extended duration.
* <p/>
* The message will appear above the player's hot bar for the specified duration and fade away during the last
* second of the duration.
* <p/>
* Only one long duration message can be sent at a time per player. If a new message is sent via this message
* any previous messages still being displayed will be replaced.
*
* @param bukkitPlayer the player to send the message to.
* @param message the message to send.
* @param duration the duration the message should be visible for in seconds.
* @param plugin the plugin sending the message.
*/
public static void sendActionBarMessage(@NotNull final Player bukkitPlayer, @NotNull final String message,
@NotNull final int duration, @NotNull Plugin plugin) {
cancelPendingMessages(bukkitPlayer);
final BukkitTask messageTask = new BukkitRunnable() {
private int count = 0;
@Override
public void run() {
if (count >= (duration - 3)) {
this.cancel();
}
sendActionBarMessage(bukkitPlayer, message);
count++;
}
}.runTaskTimer(plugin, 0L, 20L);
PENDING_MESSAGES.put(bukkitPlayer, messageTask);
}
private static void cancelPendingMessages(@NotNull Player bukkitPlayer) {
if (PENDING_MESSAGES.containsKey(bukkitPlayer)) {
PENDING_MESSAGES.get(bukkitPlayer).cancel();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment