Skip to content

Instantly share code, notes, and snippets.

@PaulBGD
Last active December 3, 2016 18:16
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PaulBGD/c5766534ee7316299316 to your computer and use it in GitHub Desktop.
Save PaulBGD/c5766534ee7316299316 to your computer and use it in GitHub Desktop.
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