Skip to content

Instantly share code, notes, and snippets.

@PaulBGD
Last active Dec 3, 2016
Embed
What would you like to do?
No dependency 1.8+ holograms and item holograms
package net.burngames.decorations.utils;
import lombok.Getter;
import org.bukkit.Location;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
/**
* @author PaulBGD
*/
@Getter
public class Hologram {
private Location location;
private String text;
private ArmorStand armorStand;
public Hologram(Location location, String name) throws Exception {
this(location, name, true);
}
public Hologram(Location location, String name, boolean spawn) throws Exception {
this.location = location.clone().subtract(0, 1.5, 0);
this.text = name;
if(spawn) {
spawn();
}
}
public void spawn() throws Exception {
if(this.armorStand != null) {
this.armorStand.remove();
}
if (this.location.getWorld() == null) {
throw new Exception("Null world!");
}
this.armorStand = (ArmorStand) this.location.getWorld().spawnEntity(location, EntityType.ARMOR_STAND);
armorStand.setSmall(true);
armorStand.setGravity(false);
armorStand.setVisible(false);
if(!this.text.equals("")) {
armorStand.setCustomName(this.text);
armorStand.setCustomNameVisible(true);
}
armorStand.setBasePlate(false);
armorStand.setCanPickupItems(false);
}
public void setText(String text) throws Exception {
if(!this.armorStand.isValid()) {
this.spawn();
}
this.text = text;
this.armorStand.setCustomName(text);
}
public void setLocation(Location location) throws Exception {
if(!this.armorStand.isValid()) {
this.spawn();
}
this.location = location.clone().subtract(0, 1.5, 0);
Entity passenger = this.armorStand.getPassenger();
if(passenger != null) {
this.armorStand.eject();
}
this.armorStand.teleport(this.location);
if(passenger != null) {
this.armorStand.setPassenger(passenger);
}
}
}
package net.burngames.decorations.utils;
import lombok.Getter;
import org.bukkit.Location;
import org.bukkit.entity.Item;
import org.bukkit.inventory.ItemStack;
/**
* @author PaulBGD
*/
@Getter
public class ItemHologram extends Hologram {
private ItemStack itemStack;
private Item item;
public ItemHologram(ItemStack itemStack, Location location) throws Exception {
super(location, "", false);
this.itemStack = itemStack;
spawn();
}
@Override
public void spawn() throws Exception {
super.spawn();
if(this.item != null) {
this.item.remove();
}
if (getLocation().getWorld() == null) {
throw new Exception("Null world");
}
this.item = getLocation().getWorld().dropItem(getLocation().clone().add(0, 1.4, 0), this.itemStack);
item.setPickupDelay(Integer.MAX_VALUE);
getArmorStand().setPassenger(item);
}
public void setItemStack(ItemStack itemStack) throws Exception {
if(!this.item.isValid() || !this.getArmorStand().isValid()) {
spawn();
}
this.itemStack = itemStack;
this.item.setItemStack(itemStack);
}
@Override
public void setLocation(Location location) throws Exception {
if(!this.item.isValid()) {
spawn();
}
try {
super.setLocation(location);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void setText(String text) {
throw new UnsupportedOperationException();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment