Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hologram Stacks #110

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> getHolograms() {
return holograms;
}

public HologramReferenceStackData setHolograms(List<String> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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<HologramData> 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<HologramData> getContent() {
return content;
}

public HologramStackData setContent(List<HologramData> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public String getName() {
return data.getName();
}

public final @NotNull HologramData getData() {
public @NotNull HologramData getData() {
return this.data;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Hologram> 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<HologramData, Hologram> 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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<String> commands;

Expand Down