Core/src/main/java/eu/univento/core/api/hologram/Hologram.java

90 lines
2.8 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 net.minecraft.server.v1_11_R1.EntityArmorStand;
import net.minecraft.server.v1_11_R1.PacketPlayOutEntityDestroy;
import net.minecraft.server.v1_11_R1.PacketPlayOutSpawnEntityLiving;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_11_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 List<EntityArmorStand> stands = new ArrayList<>();
private final String[] text;
private final Location location;
private int count;
public Hologram(String[] text, Location location) {
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 hide(CustomPlayer p) {
for(EntityArmorStand stand : stands) {
p.sendPacket(new PacketPlayOutEntityDestroy(stand.getId()));
}
}
public void destroy() {
for(EntityArmorStand armorStand : stands) {
armorStand.die();
}
}
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(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));
}
}
}
}