From e12bdbae5277397bb261354d01cad02cbab5518a Mon Sep 17 00:00:00 2001 From: Oak Date: Fri, 21 Jun 2024 12:00:11 +0100 Subject: [PATCH] Added base implementation for hologram stacks --- .../api/data/HologramReferenceStackData.java | 73 ++++++++++++ .../api/data/HologramStackData.java | 104 ++++++++++++++++ .../fancyholograms/api/hologram/Hologram.java | 2 +- .../api/hologram/HologramStack.java | 111 ++++++++++++++++++ .../api/hologram/HologramType.java | 4 +- 5 files changed, 292 insertions(+), 2 deletions(-) create mode 100644 api/src/main/java/de/oliver/fancyholograms/api/data/HologramReferenceStackData.java create mode 100644 api/src/main/java/de/oliver/fancyholograms/api/data/HologramStackData.java create mode 100644 api/src/main/java/de/oliver/fancyholograms/api/hologram/HologramStack.java diff --git a/api/src/main/java/de/oliver/fancyholograms/api/data/HologramReferenceStackData.java b/api/src/main/java/de/oliver/fancyholograms/api/data/HologramReferenceStackData.java new file mode 100644 index 00000000..189e101c --- /dev/null +++ b/api/src/main/java/de/oliver/fancyholograms/api/data/HologramReferenceStackData.java @@ -0,0 +1,73 @@ +package de.oliver.fancyholograms.api.data; + +import de.oliver.fancyholograms.api.hologram.HologramType; +import org.bukkit.Location; +import org.bukkit.configuration.ConfigurationSection; + +import java.util.ArrayList; +import java.util.List; + +public class HologramReferenceStackData extends HologramData { + private List holograms = new ArrayList<>(); + + /** + * @param name Name of hologram + * @param location Location of hologram + * Default values are already set + */ + public HologramReferenceStackData(String name, Location location) { + super(name, HologramType.HOLOGRAM_STACK, location); + } + + public List getHolograms() { + return holograms; + } + + public HologramReferenceStackData setHolograms(List holograms) { + this.holograms = holograms; + setHasChanges(true); + return this; + } + + public void addLine(String hologramName) { + holograms.add(hologramName); + setHasChanges(true); + } + + public void removeLine(int index) { + holograms.remove(index); + setHasChanges(true); + } + + @Override + public void read(ConfigurationSection section, String name) { + super.read(section, name); + + ConfigurationSection stackSection = section.getConfigurationSection("stack"); + if (stackSection != null) { + + } + } + + @Override + public void write(ConfigurationSection section, String name) { + super.write(section, name); + + for (int i = 0; i < holograms.size(); i++) { + section.set("stack." + i, holograms.get(i)); + } + } + + @Override + public HologramReferenceStackData copy(String name) { + HologramReferenceStackData hologramStackData = new HologramReferenceStackData(name, getLocation()); + hologramStackData + .setHolograms(this.getHolograms()) + .setVisibilityDistance(this.getVisibilityDistance()) + .setVisibility(this.getVisibility()) + .setPersistent(this.isPersistent()) + .setLinkedNpcName(this.getLinkedNpcName()); + + return hologramStackData; + } +} diff --git a/api/src/main/java/de/oliver/fancyholograms/api/data/HologramStackData.java b/api/src/main/java/de/oliver/fancyholograms/api/data/HologramStackData.java new file mode 100644 index 00000000..2b93b25c --- /dev/null +++ b/api/src/main/java/de/oliver/fancyholograms/api/data/HologramStackData.java @@ -0,0 +1,104 @@ +package de.oliver.fancyholograms.api.data; + +import de.oliver.fancyholograms.api.FancyHologramsPlugin; +import de.oliver.fancyholograms.api.hologram.HologramType; +import org.bukkit.Location; +import org.bukkit.configuration.ConfigurationSection; + +import java.util.ArrayList; +import java.util.List; + +public class HologramStackData extends HologramData { + private List content = new ArrayList<>(); + + /** + * @param name Name of hologram + * @param location Location of hologram + * Default values are already set + */ + public HologramStackData(String name, Location location) { + super(name, HologramType.HOLOGRAM_STACK, location); + } + + public List getContent() { + return content; + } + + public HologramStackData setContent(List content) { + this.content = content; + setHasChanges(true); + return this; + } + + public void addLine(HologramData data) { + content.add(data); + setHasChanges(true); + } + + public void removeLine(int index) { + content.remove(index); + setHasChanges(true); + } + + @Override + public void read(ConfigurationSection section, String name) { + super.read(section, name); + + ConfigurationSection stackSection = section.getConfigurationSection("stack"); + if (stackSection != null) { + for (String indexRaw : stackSection.getKeys(false)) { + ConfigurationSection holoSection = stackSection.getConfigurationSection(indexRaw); + if (holoSection == null) { + continue; + } + + String typeName = holoSection.getString("type"); + if (typeName == null) { + FancyHologramsPlugin.get().getPlugin().getLogger().warning("HologramType was not saved"); + continue; + } + + HologramType type = HologramType.getByName(typeName); + if (type == null) { + FancyHologramsPlugin.get().getPlugin().getLogger().warning("Could not parse HologramType"); + continue; + } + + DisplayHologramData displayData = null; + switch (type) { + case TEXT -> displayData = new TextHologramData(name, new Location(null, 0, 0, 0)); + case ITEM -> displayData = new ItemHologramData(name, new Location(null, 0, 0, 0)); + case BLOCK -> displayData = new BlockHologramData(name, new Location(null, 0, 0, 0)); + } + displayData.read(holoSection, name); + + int index = Integer.parseInt(indexRaw); + content.set(index, displayData); + } + } + } + + @Override + public void write(ConfigurationSection section, String name) { + super.write(section, name); + + for (int i = 0; i < content.size(); i++) { + HologramData hologramData = content.get(i); + ConfigurationSection dataSection = section.createSection("stack." + i); + hologramData.write(dataSection, hologramData.getName()); + } + } + + @Override + public HologramStackData copy(String name) { + HologramStackData hologramStackData = new HologramStackData(name, getLocation()); + hologramStackData + .setContent(this.getContent()) + .setVisibilityDistance(this.getVisibilityDistance()) + .setVisibility(this.getVisibility()) + .setPersistent(this.isPersistent()) + .setLinkedNpcName(this.getLinkedNpcName()); + + return hologramStackData; + } +} diff --git a/api/src/main/java/de/oliver/fancyholograms/api/hologram/Hologram.java b/api/src/main/java/de/oliver/fancyholograms/api/hologram/Hologram.java index 666d36d2..9a873258 100644 --- a/api/src/main/java/de/oliver/fancyholograms/api/hologram/Hologram.java +++ b/api/src/main/java/de/oliver/fancyholograms/api/hologram/Hologram.java @@ -48,7 +48,7 @@ public String getName() { return data.getName(); } - public final @NotNull HologramData getData() { + public @NotNull HologramData getData() { return this.data; } diff --git a/api/src/main/java/de/oliver/fancyholograms/api/hologram/HologramStack.java b/api/src/main/java/de/oliver/fancyholograms/api/hologram/HologramStack.java new file mode 100644 index 00000000..c67cc845 --- /dev/null +++ b/api/src/main/java/de/oliver/fancyholograms/api/hologram/HologramStack.java @@ -0,0 +1,111 @@ +package de.oliver.fancyholograms.api.hologram; + +import de.oliver.fancyholograms.api.FancyHologramsPlugin; +import de.oliver.fancyholograms.api.data.HologramData; +import de.oliver.fancyholograms.api.data.HologramStackData; +import org.bukkit.Bukkit; +import org.bukkit.entity.Display; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.*; + +public class HologramStack extends Hologram { + private final List holograms = new ArrayList<>(); + + protected HologramStack(@NotNull HologramStackData data) { + super(data); + } + + @Override + public @NotNull HologramStackData getData() { + return (HologramStackData) this.data; + } + + @Override + public @Nullable Display getDisplayEntity() { + return null; + } + + @Override + protected void create() { + for (HologramData hologramData : this.getData().getContent()) { + Hologram hologram = FancyHologramsPlugin.get().getHologramManager().create(hologramData); + this.holograms.add(hologram); + } + } + + @Override + protected void delete() { + for (Hologram hologram : holograms) { + hologram.delete(); + } + } + + @Override + protected void update() { + HashMap currHolograms = new HashMap<>(); + for (Hologram hologram : this.holograms) { + currHolograms.put(hologram.getData(), hologram); + } + this.holograms.clear(); + + for (HologramData hologramData : this.getData().getContent()) { + Hologram hologram; + if (currHolograms.containsKey(hologramData)) { + hologram = currHolograms.get(hologramData); + currHolograms.remove(hologramData); + } else { + hologram = FancyHologramsPlugin.get().getHologramManager().create(hologramData); + } + + this.holograms.add(hologram); + } + + // Handle old/removed holograms + for (Hologram hologram : currHolograms.values()) { + for (UUID viewer : hologram.getViewers()) { + Player player = Bukkit.getPlayer(viewer); + if (player != null) { + forceHideHologram(player); + } + } + + hologram.delete(); + } + } + + @Override + protected boolean show(@NotNull Player player) { + boolean success = true; + + for (Hologram hologram : holograms) { + if (!hologram.show(player)) { + success = false; + } + } + + return success; + } + + @Override + protected boolean hide(@NotNull Player player) { + boolean success = true; + + for (Hologram hologram : holograms) { + if (!hologram.hide(player)) { + success = false; + } + } + + return success; + } + + @Override + protected void refresh(@NotNull Player player) { + for (Hologram hologram : holograms) { + hologram.refresh(player); + } + } +} diff --git a/api/src/main/java/de/oliver/fancyholograms/api/hologram/HologramType.java b/api/src/main/java/de/oliver/fancyholograms/api/hologram/HologramType.java index 432fcdd8..b18c6bc2 100644 --- a/api/src/main/java/de/oliver/fancyholograms/api/hologram/HologramType.java +++ b/api/src/main/java/de/oliver/fancyholograms/api/hologram/HologramType.java @@ -1,12 +1,14 @@ package de.oliver.fancyholograms.api.hologram; import java.util.Arrays; +import java.util.Collections; import java.util.List; public enum HologramType { TEXT(Arrays.asList("background", "textshadow", "textalignment", "seethrough", "setline", "removeline", "addline", "insertbefore", "insertafter", "updatetextinterval")), ITEM(List.of("item")), - BLOCK(List.of("block")); + BLOCK(List.of("block")), + HOLOGRAM_STACK(Collections.emptyList()); private final List commands;