public class MessageHead {
private final server plugin;
public MessageHead(server plugin) {
this.plugin = plugin;
}
public final Map<Player, Queue<String>> map = new HashMap<>();
public final Map<Player, ArmorStand> playerArmorStand = new HashMap<>();
public final Map<Player, Boolean> armorStandExists = new HashMap<>();
public void messageDisplay(Player p, String m) {
if (armorStandExists.get(p) != null) {
return;
}
// Set variables to be used
Location loc = p.getLocation();
World world = p.getWorld();
ArmorStand chat = world.spawn(loc, ArmorStand.class);
// Associate a specific armor stand with the player
playerArmorStand.put(p, chat);
armorStandExists.put(p, true);
// Set armor stand properties
chat.setVisible(false);
chat.setGravity(false);
// Final shenanigans to make it work in the runnable
final String[] chatMessage = {""};
final int[] finalI = new int[1];
// Repeating task instead of a loop, so I can add delay
new BukkitRunnable() {
@Override
public void run() {
// Display message character by character
chatMessage[0] = chatMessage[0] + m.charAt(finalI[0]);
chat.setCustomName(chatMessage[0]);
chat.setCustomNameVisible(true);
// Play sound as if player has a voice
Location newloc = p.getLocation();
world.playSound(newloc, Sound.BLOCK_STONE_BUTTON_CLICK_ON, 0.5F, 1);
// Teleport armor stand to player
messageTeleport(p);
// When loop is finished
if (finalI[0] + 1 >= m.length()) {
// Get rid of display
messageFade(chat, p);
cancel();
}
finalI[0]++;
}
}.runTaskTimer(plugin, 0, 1);
}
public void messageQueue(Player p, String m){
// Create temp queue
Queue<String> messageQ = new LinkedList<>();
// If map doesn't have anything, add it
if (!map.containsKey(p)){
map.put(p, messageQ);
}
map.get(p).offer(m);
}
public void showNextMessage(Player p){
Queue<String> messageQueue = map.get(p);
if (messageQueue != null && !messageQueue.isEmpty()){
String nextMessage = messageQueue.poll();
messageDisplay(p, nextMessage);
}
}
public void messageTeleport(Player p) {
if (!(map.get(p) == null)){
ArmorStand a = playerArmorStand.get(p);
a.teleport(p);
}
}
public void messageFade(ArmorStand a, Player p){
BukkitTask balls = new BukkitRunnable(){
@Override
public void run(){
messageTeleport(p);
}
}.runTaskTimer(plugin, 1, 1);
new BukkitRunnable(){
@Override
public void run(){
balls.cancel();
a.remove();
playerArmorStand.remove(p);
armorStandExists.replace(p, null);
showNextMessage(p);
}
}.runTaskLater(plugin, 80L);
}
}