105 lines
3.3 KiB
Java
105 lines
3.3 KiB
Java
/*
|
|
* Copyright (c) 2017 univento.eu - All rights reserved
|
|
* You are not allowed to use, distribute or modify this code
|
|
*/
|
|
|
|
package eu.univento.core.api.hologram;
|
|
|
|
import eu.univento.core.Core;
|
|
import eu.univento.core.api.events.HologramClickEvent;
|
|
import eu.univento.core.api.player.CustomPlayer;
|
|
import lombok.Getter;
|
|
import net.minecraft.server.v1_12_R1.EntityArmorStand;
|
|
import net.minecraft.server.v1_12_R1.PacketPlayOutEntityDestroy;
|
|
import net.minecraft.server.v1_12_R1.PacketPlayOutSpawnEntityLiving;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.ChatColor;
|
|
import org.bukkit.Location;
|
|
import org.bukkit.craftbukkit.v1_12_R1.CraftWorld;
|
|
import org.bukkit.entity.ArmorStand;
|
|
import org.bukkit.event.EventHandler;
|
|
import org.bukkit.event.Listener;
|
|
import org.bukkit.event.player.PlayerInteractEntityEvent;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* @author joethei
|
|
* @version 1.0
|
|
*/
|
|
public class Hologram implements Listener{
|
|
|
|
private final String name;
|
|
private final List<EntityArmorStand> stands = new ArrayList<>();
|
|
private final String[] text;
|
|
@Getter private final Location location;
|
|
private int count;
|
|
|
|
public Hologram(String name, String[] text, Location location) {
|
|
this.name = name;
|
|
HologramManager.addHologram(name, this);
|
|
this.text = text;
|
|
this.location = location;
|
|
create();
|
|
Bukkit.getPluginManager().registerEvents(this, Core.getInstance());
|
|
}
|
|
|
|
public void show(CustomPlayer p) {
|
|
for(EntityArmorStand stand : stands) {
|
|
p.sendPacket(new PacketPlayOutSpawnEntityLiving(stand));
|
|
}
|
|
}
|
|
|
|
public void showAll() {
|
|
for(CustomPlayer p : Core.getOnlinePlayers()) show(p);
|
|
}
|
|
|
|
public void hide(CustomPlayer p) {
|
|
for(EntityArmorStand stand : stands) {
|
|
p.sendPacket(new PacketPlayOutEntityDestroy(stand.getId()));
|
|
}
|
|
}
|
|
|
|
public void hideAll() {
|
|
for(CustomPlayer p : Core.getOnlinePlayers()) hide(p);
|
|
}
|
|
|
|
public void destroy() {
|
|
hideAll();
|
|
for(EntityArmorStand armorStand : stands) {
|
|
armorStand.die();
|
|
}
|
|
HologramManager.removeHologram(name);
|
|
}
|
|
|
|
private void create() {
|
|
double DISTANCE = 0.25D;
|
|
for (String text : this.text) {
|
|
EntityArmorStand entity = new EntityArmorStand(((CraftWorld) this.location.getWorld()).getHandle(),this.location.getX(), this.location.getY(),this.location.getZ());
|
|
entity.setCustomName(ChatColor.translateAlternateColorCodes('&', text));
|
|
entity.setCustomNameVisible(true);
|
|
entity.setInvisible(true);
|
|
entity.setNoGravity(true);
|
|
stands.add(entity);
|
|
this.location.subtract(0, DISTANCE, 0);
|
|
count++;
|
|
}
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
this.location.add(0, DISTANCE, 0);
|
|
}
|
|
this.count = 0;
|
|
}
|
|
|
|
@EventHandler
|
|
public void onEntityInteract(PlayerInteractEntityEvent e) {
|
|
CustomPlayer p = CustomPlayer.getPlayer(e.getPlayer());
|
|
if(e.getRightClicked() instanceof EntityArmorStand) {
|
|
if(stands.contains(e.getRightClicked())) {
|
|
ArmorStand stand = (ArmorStand) e.getRightClicked();
|
|
Bukkit.getPluginManager().callEvent(new HologramClickEvent(p, stand));
|
|
}
|
|
}
|
|
}
|
|
} |