diff --git a/DisplayManager.java b/DisplayManager.java new file mode 100644 index 0000000..5d92da6 --- /dev/null +++ b/DisplayManager.java @@ -0,0 +1,35 @@ +package com.badbones69.crazycrates.managers; + +import com.badbones69.crazycrates.api.holograms.TextHologram; +import org.bukkit.entity.TextDisplay; +import java.util.HashMap; +import java.util.Map; + +public class DisplayManager { + + private static final Map displays = new HashMap<>(); + + public static void addDisplay(final String hologram, final TextHologram display) { + displays.put(hologram, display); + } + + public static void removeDisplay(final String name) { + final TextHologram textHologram = displays.remove(name); + + if (textHologram == null) return; + + final TextDisplay display = textHologram.getTextDisplay(); + + if (display == null) return; + + display.remove(); + } + + public static TextHologram getDisplay(final String hologram) { + return displays.get(hologram); + } + + public static Map getDisplays() { + return displays; + } +} \ No newline at end of file diff --git a/holograms/ItemHologram.java b/holograms/ItemHologram.java new file mode 100644 index 0000000..b83567d --- /dev/null +++ b/holograms/ItemHologram.java @@ -0,0 +1,50 @@ +package com.badbones69.crazycrates.api.holograms; + +import com.ryderbelserion.vital.paper.util.ItemUtil; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.World; +import org.bukkit.entity.ItemDisplay; +import org.bukkit.inventory.ItemStack; + +public class ItemHologram extends TextHologram { + + private final ItemStack itemStack; + + public ItemHologram(final String value) { + final Material material = ItemUtil.getMaterial(value); + + if (material == null) { + this.itemStack = ItemStack.of(Material.STONE); + + return; + } + + this.itemStack = ItemStack.of(material); + } + + private ItemDisplay itemDisplay; + + @Override + public ItemHologram create() { + final Location location = getLocation(); + + final World world = location.getWorld(); + + this.itemDisplay = world.spawn(location.clone().add(0.0, getHeight(), 0.0), ItemDisplay.class, item -> { + item.setBillboard(org.bukkit.entity.Display.Billboard.CENTER); + + item.setItemStack(this.itemStack); + + item.setPersistent(true); + }); + + //todo() cache + + return this; + } + + public ItemDisplay getItemDisplay() { + return this.itemDisplay; + } +} \ No newline at end of file diff --git a/holograms/TextHologram.java b/holograms/TextHologram.java new file mode 100644 index 0000000..3e733d3 --- /dev/null +++ b/holograms/TextHologram.java @@ -0,0 +1,200 @@ +package com.badbones69.crazycrates.api.holograms; + +import com.ryderbelserion.vital.common.util.AdvUtil; +import com.ryderbelserion.vital.common.util.StringUtil; +import org.bukkit.Color; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.entity.Display; +import org.bukkit.entity.TextDisplay; +import org.jetbrains.annotations.NotNull; +import java.util.List; + +public class TextHologram { + + private Display.Billboard billboard; + private List text; + private Location location; + private Color color; + private int height; + + /** + * Builds a display object with a location + * + * @param location {@link Location} + */ + public TextHologram(final Location location) { + this.location = location; + } + + /** + * Builds a blanket display object + */ + public TextHologram() { + this.location = null; + } + + private TextDisplay textDisplay; + + /** + * Creates the text display + * + * @return {@link TextHologram} + */ + public TextHologram create() { + final World world = location.getWorld(); + + this.textDisplay = world.spawn(this.location.clone().add(0.0, this.height, 0.0), TextDisplay.class, display -> { + display.text(AdvUtil.parse(StringUtil.convertList(this.text))); + + display.setBillboard(org.bukkit.entity.Display.Billboard.CENTER); + + display.setBackgroundColor(this.color); + + display.setPersistent(true); + }); + + //todo() add to cache + + return this; + } + + /** + * Gets the text display + * + * @return {@link TextDisplay} + */ + public TextDisplay getTextDisplay() { + return this.textDisplay; + } + + /** + * Sets the billboard + * + * @param billboard the billboard + */ + public void setBillboard(Display.Billboard billboard) { + this.billboard = billboard; + } + + /** + * Gets the billboard + * + * @return the billboard + */ + public Display.Billboard getBillboard() { + return this.billboard; + } + + /** + * Sets the y-level height of the hologram + * + * @param height the y-level + * + * @return {@link TextHologram} + */ + public TextHologram setHeight(int height) { + this.height = height; + + return this; + } + + /** + * Gets the y-level height of the hologram + * + * @return the y-level + */ + public int getHeight() { + return this.height; + } + + /** + * Sets the hologram text + * + * @param text the text to show + * + * @return {@link TextHologram} + */ + public TextHologram setTexts(@NotNull final List text) { + this.text = text; + + return this; + } + + /** + * Adds a text to existing list of text + * + * @param line the new text + * + * @return {@link TextHologram} + */ + public TextHologram addText(@NotNull final String line) { + this.text.add(line); + + return this; + } + + /** + * Gets a list of text for the hologram + * + * @return list of text + */ + public List getText() { + return this.text; + } + + /** + * Sets the location + * + * @param location {@link Location} + * + * @return {@link TextHologram} + */ + public TextHologram setLocation(Location location) { + this.location = location; + + return this; + } + + /** + * Gets the location + * + * @return {@link Location} + */ + public Location getLocation() { + return this.location; + } + + /** + * Sets the background as transparent. + * + * @return {@link TextHologram} + */ + public TextHologram setTransparent() { + this.color = Color.fromARGB(0); + + return this; + } + + /** + * Sets the background color + * + * @param color {@link Color} + * + * @return {@link TextHologram} + */ + public TextHologram setColor(Color color) { + this.color = color; + + return this; + } + + /** + * Gets the background color + * + * @return {@link Color} + */ + public Color getColor() { + return this.color; + } +} \ No newline at end of file