Hologram hook rework. Only tested with DecentHolograms, needs further testing.

This commit is contained in:
Fernando Pettinelli 2021-12-18 23:15:46 -03:00
parent edde5f9963
commit 253883c735
8 changed files with 204 additions and 108 deletions

View File

@ -365,6 +365,13 @@
<version>4.9.0.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.decentsoftware-eu</groupId>
<artifactId>decentholograms</artifactId>
<version>2.2.5</version>
<scope>provided</scope>
</dependency>
<!-- End Plugin Hooks -->
<dependency>

View File

@ -47,21 +47,21 @@ public class HologramManager {
return manager.getCurrentHook();
}
public static void createHologram(Location location, String line) {
public static void createHologram(String id, Location location, String line) {
if (manager.isEnabled()) {
manager.getCurrentHook().createHologram(location, line);
manager.getCurrentHook().createHologram(id, location, line);
}
}
public static void createHologram(Location location, List<String> lines) {
public static void createHologram(String id, Location location, List<String> lines) {
if (manager.isEnabled()) {
manager.getCurrentHook().createHologram(location, lines);
manager.getCurrentHook().createHologram(id, location, lines);
}
}
public static void removeHologram(Location location) {
public static void removeHologram(String id) {
if (manager.isEnabled()) {
manager.getCurrentHook().removeHologram(location);
manager.getCurrentHook().removeHologram(id);
}
}
@ -71,21 +71,29 @@ public class HologramManager {
}
}
public static void updateHologram(Location location, String line) {
public static void updateHologram(String id, String line) {
if (manager.isEnabled()) {
manager.getCurrentHook().updateHologram(location, line);
manager.getCurrentHook().updateHologram(id, line);
}
}
public static void updateHologram(Location location, List<String> lines) {
public static void updateHologram(String id, List<String> lines) {
if (manager.isEnabled()) {
manager.getCurrentHook().updateHologram(location, lines);
manager.getCurrentHook().updateHologram(id, lines);
}
}
public static void bulkUpdateHolograms(Map<Location, List<String>> holograms) {
public static void bulkUpdateHolograms(Map<String, List<String>> holograms) {
if (manager.isEnabled()) {
manager.getCurrentHook().bulkUpdateHolograms(holograms);
}
}
public static boolean isHologramLoaded(String id) {
if (manager.isEnabled()) {
return manager.getCurrentHook().isHologramLoaded(id);
}
return false;
}
}

View File

@ -5,6 +5,7 @@ import com.songoda.core.hooks.economies.PlayerPointsEconomy;
import com.songoda.core.hooks.economies.ReserveEconomy;
import com.songoda.core.hooks.economies.VaultEconomy;
import com.songoda.core.hooks.holograms.CMIHolograms;
import com.songoda.core.hooks.holograms.DecentHologramsHolograms;
import com.songoda.core.hooks.holograms.Holograms;
import com.songoda.core.hooks.holograms.HologramsHolograms;
import com.songoda.core.hooks.holograms.HolographicDisplaysHolograms;
@ -44,6 +45,7 @@ public final class PluginHook<T extends Class> {
public static final PluginHook HOLO_DISPLAYS = new PluginHook(Holograms.class, "HolographicDisplays", HolographicDisplaysHolograms.class);
public static final PluginHook HOLO_HOLOGRAMS = new PluginHook(Holograms.class, "Holograms", HologramsHolograms.class);
public static final PluginHook HOLO_CMI = new PluginHook(Holograms.class, "CMI", CMIHolograms.class);
public static final PluginHook HOLO_DECENTHOLOGRAMS = new PluginHook(Holograms.class, "DecentHolograms", DecentHologramsHolograms.class);
public static final PluginHook LOG_CORE_PROTECT = new PluginHook(Log.class, "CoreProtect", CoreProtectLog.class);
public static final PluginHook PROTECTION_GRIEFPREVENTION = new PluginHook(Protection.class, "GriefPrevention", GriefPreventionProtection.class);
public static final PluginHook PROTECTION_LANDS = new PluginHook(Protection.class, "Lands", LandsProtection.class);

View File

@ -58,15 +58,12 @@ public class CMIHolograms extends Holograms {
}
@Override
public void createHologram(Location location, List<String> lines) {
createAt(fixLocation(location), lines);
public void createHologram(String id, Location location, List<String> lines) {
createAt(id, fixLocation(location), lines);
}
@Override
public void removeHologram(Location location) {
location = fixLocation(location);
final String id = locStr(location);
public void removeHologram(String id) {
CMIHologram holo = cmiHologramManager.getByName(id);
if (holo != null) {
@ -90,9 +87,13 @@ public class CMIHolograms extends Holograms {
}
@Override
public void updateHologram(Location location, List<String> lines) {
location = fixLocation(location);
CMIHologram holo = cmiHologramManager.getByName(locStr(location));
public boolean isHologramLoaded(String id) {
return cmiHologramManager.getByName(id) != null;
}
@Override
public void updateHologram(String id, List<String> lines) {
CMIHologram holo = cmiHologramManager.getByName(id);
if (holo != null) {
// only update if there is a change to the text
@ -123,24 +124,16 @@ public class CMIHolograms extends Holograms {
return;
}
createAt(location, lines);
}
@Override
public void bulkUpdateHolograms(Map<Location, List<String>> hologramData) {
for (Map.Entry<Location, List<String>> entry : hologramData.entrySet()) {
public void bulkUpdateHolograms(Map<String, List<String>> hologramData) {
for (Map.Entry<String, List<String>> entry : hologramData.entrySet()) {
updateHologram(entry.getKey(), entry.getValue());
}
}
private String locStr(Location loc) {
return String.format("%s-%d-%d-%d", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
}
private void createAt(Location location, List<String> lines) {
final String id = locStr(location);
private void createAt(String id, Location location, List<String> lines) {
CMIHologram holo = new CMIHologram(id, location);
holo.setLines(lines);

View File

@ -0,0 +1,98 @@
package com.songoda.core.hooks.holograms;
import eu.decentsoftware.holograms.api.DHAPI;
import eu.decentsoftware.holograms.api.DecentHologramsAPI;
import eu.decentsoftware.holograms.api.holograms.Hologram;
import org.bukkit.Location;
import org.bukkit.plugin.Plugin;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class DecentHologramsHolograms extends Holograms {
private final Set<String> ourHolograms = new HashSet<>();
public DecentHologramsHolograms(Plugin plugin) {
super(plugin);
}
@Override
public String getName() {
return "DecentHolograms";
}
@Override
public boolean isEnabled() {
return true;
}
@Override
protected double defaultHeightOffset() {
return 1;
}
@Override
public void createHologram(String id, Location location, List<String> lines) {
createAt(id, location, lines);
}
@Override
public void removeHologram(String id) {
Hologram hologram = DHAPI.getHologram(id);
if (hologram != null) {
hologram.delete();
DecentHologramsAPI.get().getHologramManager().removeHologram(id);
}
ourHolograms.remove(id);
}
@Override
public void updateHologram(String id, List<String> lines) {
Hologram hologram = DHAPI.getHologram(id);
if (hologram != null) {
DHAPI.setHologramLines(hologram, lines);
}
}
@Override
public void bulkUpdateHolograms(Map<String, List<String>> hologramData) {
for (Map.Entry<String, List<String>> entry : hologramData.entrySet()) {
updateHologram(entry.getKey(), entry.getValue());
}
}
@Override
public void removeAllHolograms() {
for (String id : ourHolograms) {
Hologram hologram = DHAPI.getHologram(id);
if (hologram != null) {
hologram.delete();
DecentHologramsAPI.get().getHologramManager().removeHologram(id);
}
}
ourHolograms.clear();
}
@Override
public boolean isHologramLoaded(String id) {
return DHAPI.getHologram(id) != null;
}
private void createAt(String id, Location location, List<String> lines) {
location = fixLocation(location);
if (DHAPI.getHologram(id) != null) {
return;
}
DHAPI.createHologram(id, location, lines);
ourHolograms.add(id);
}
}

View File

@ -44,21 +44,23 @@ public abstract class Holograms implements Hook {
protected abstract double defaultHeightOffset();
public void createHologram(Location location, String line) {
createHologram(location, Collections.singletonList(line));
public void createHologram(String id, Location location, String line) {
createHologram(id, location, Collections.singletonList(line));
}
public abstract void createHologram(Location location, List<String> lines);
public abstract void createHologram(String id, Location location, List<String> lines);
public abstract void removeHologram(Location location);
public abstract void removeHologram(String id);
public void updateHologram(Location location, String line) {
updateHologram(location, Collections.singletonList(line));
public void updateHologram(String id, String line) {
updateHologram(id, Collections.singletonList(line));
}
public abstract void updateHologram(Location location, List<String> lines);
public abstract void updateHologram(String id, List<String> lines);
public abstract void bulkUpdateHolograms(Map<Location, List<String>> hologramData);
public abstract void bulkUpdateHolograms(Map<String, List<String>> hologramData);
public abstract void removeAllHolograms();
public abstract boolean isHologramLoaded(String id);
}

View File

@ -11,10 +11,11 @@ import org.bukkit.plugin.Plugin;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class HologramsHolograms extends Holograms {
HologramPlugin hologramPlugin;
HashSet<String> ourHolograms = new HashSet<>();
private final HologramPlugin hologramPlugin;
private final Set<String> ourHolograms = new HashSet<>();
public HologramsHolograms(Plugin plugin) {
super(plugin);
@ -38,15 +39,12 @@ public class HologramsHolograms extends Holograms {
}
@Override
public void createHologram(Location location, List<String> lines) {
createAt(fixLocation(location), lines);
public void createHologram(String id, Location location, List<String> lines) {
createAt(id, fixLocation(location), lines);
}
@Override
public void removeHologram(Location location) {
location = fixLocation(location);
final String id = locStr(location);
public void removeHologram(String id) {
Hologram hologram = hologramPlugin.getHologramManager().getHologram(id);
if (hologram != null) {
@ -72,9 +70,13 @@ public class HologramsHolograms extends Holograms {
}
@Override
public void updateHologram(Location location, List<String> lines) {
location = fixLocation(location);
Hologram hologram = hologramPlugin.getHologramManager().getHologram(locStr(location));
public boolean isHologramLoaded(String id) {
return hologramPlugin.getHologramManager().getHologram(id) != null;
}
@Override
public void updateHologram(String id, List<String> lines) {
Hologram hologram = hologramPlugin.getHologramManager().getHologram(id);
if (hologram != null) {
hologram.spawn();
@ -100,24 +102,16 @@ public class HologramsHolograms extends Holograms {
return;
}
createAt(location, lines);
}
@Override
public void bulkUpdateHolograms(Map<Location, List<String>> hologramData) {
for (Map.Entry<Location, List<String>> entry : hologramData.entrySet()) {
public void bulkUpdateHolograms(Map<String, List<String>> hologramData) {
for (Map.Entry<String, List<String>> entry : hologramData.entrySet()) {
updateHologram(entry.getKey(), entry.getValue());
}
}
private String locStr(Location loc) {
return String.format("%s-%d-%d-%d", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
}
private void createAt(Location location, List<String> lines) {
final String id = locStr(location);
private void createAt(String id, Location location, List<String> lines) {
Hologram hologram = new Hologram(id, location);
for (String line : lines) {

View File

@ -7,10 +7,14 @@ import org.bukkit.plugin.Plugin;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class HolographicDisplaysHolograms extends Holograms {
private final Map<String, Hologram> holograms = new HashMap<>();
public HolographicDisplaysHolograms(Plugin plugin) {
super(plugin);
}
@ -31,44 +35,30 @@ public class HolographicDisplaysHolograms extends Holograms {
}
@Override
public void createHologram(Location location, List<String> lines) {
createAt(fixLocation(location), lines);
public void createHologram(String id, Location location, List<String> lines) {
createAt(id, location, lines);
}
@Override
public void removeHologram(Location location) {
location = fixLocation(location);
for (Hologram hologram : HologramsAPI.getHolograms(plugin)) {
if (hologram.getX() != location.getX()
|| hologram.getY() != location.getY()
|| hologram.getZ() != location.getZ()) {
continue;
}
public void removeHologram(String id) {
Hologram hologram = holograms.remove(id);
if (hologram != null) {
hologram.delete();
}
}
@Override
public void updateHologram(Location location, List<String> lines) {
bulkUpdateHolograms(Collections.singletonMap(location, lines));
public void updateHologram(String id, List<String> lines) {
bulkUpdateHolograms(Collections.singletonMap(id, lines));
}
@Override
public void bulkUpdateHolograms(Map<Location, List<String>> hologramData) {
Collection<Hologram> holograms = HologramsAPI.getHolograms(plugin);
for (Map.Entry<Location, List<String>> entry : hologramData.entrySet()) {
Location location = fixLocation(entry.getKey());
public void bulkUpdateHolograms(Map<String, List<String>> hologramData) {
for (Map.Entry<String, List<String>> entry : hologramData.entrySet()) {
String id = entry.getKey();
List<String> lines = entry.getValue();
for (Hologram hologram : holograms) {
if (hologram.getX() != location.getX()
|| hologram.getY() != location.getY()
|| hologram.getZ() != location.getZ()) {
continue;
}
Hologram hologram = holograms.get(id);
// only update if there is a change to the text
boolean isChanged = lines.size() != hologram.size();
@ -87,24 +77,26 @@ public class HolographicDisplaysHolograms extends Holograms {
hologram.appendTextLine(line);
}
}
return;
}
createAt(location, lines);
}
}
private void createAt(Location location, List<String> lines) {
private void createAt(String id, Location location, List<String> lines) {
Hologram hologram = HologramsAPI.createHologram(plugin, location);
for (String line : lines) {
hologram.appendTextLine(line);
}
holograms.put(id, hologram);
}
@Override
public void removeAllHolograms() {
HologramsAPI.getHolograms(plugin).forEach(Hologram::delete);
holograms.values().forEach(Hologram::delete);
}
@Override
public boolean isHologramLoaded(String id) {
return holograms.get(id) != null;
}
}