From d2102c78b61d57ae091dcea6f89bed36884632d2 Mon Sep 17 00:00:00 2001 From: jascotty2 Date: Mon, 16 Sep 2019 10:04:22 -0500 Subject: [PATCH] add CMI hologram support --- Core/pom.xml | 6 + .../com/songoda/core/hooks/PluginHook.java | 2 + .../core/hooks/holograms/CMIHolograms.java | 135 ++++++++++++++++++ .../hooks/holograms/HologramsHolograms.java | 5 +- 4 files changed, 145 insertions(+), 3 deletions(-) create mode 100644 Core/src/main/java/com/songoda/core/hooks/holograms/CMIHolograms.java diff --git a/Core/pom.xml b/Core/pom.xml index 2c07eeb6..5a914de6 100644 --- a/Core/pom.xml +++ b/Core/pom.xml @@ -195,6 +195,12 @@ 2.9.1 provided + + com.zrips + CMI + 8.4.0.2_1 + provided + net.tnemc Reserve diff --git a/Core/src/main/java/com/songoda/core/hooks/PluginHook.java b/Core/src/main/java/com/songoda/core/hooks/PluginHook.java index 7042fb1b..652470a4 100644 --- a/Core/src/main/java/com/songoda/core/hooks/PluginHook.java +++ b/Core/src/main/java/com/songoda/core/hooks/PluginHook.java @@ -4,6 +4,7 @@ import com.songoda.core.hooks.economies.Economy; 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.stackers.StackMob; import com.songoda.core.hooks.stackers.Stacker; import com.songoda.core.hooks.stackers.UltimateStacker; @@ -33,6 +34,7 @@ public final class PluginHook { public static final PluginHook STACKER_STACK_MOB = new PluginHook(Stacker.class, "StackMob", StackMob.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); /******* Start Manager stuff *******/ diff --git a/Core/src/main/java/com/songoda/core/hooks/holograms/CMIHolograms.java b/Core/src/main/java/com/songoda/core/hooks/holograms/CMIHolograms.java new file mode 100644 index 00000000..cc172770 --- /dev/null +++ b/Core/src/main/java/com/songoda/core/hooks/holograms/CMIHolograms.java @@ -0,0 +1,135 @@ +package com.songoda.core.hooks.holograms; + +import com.Zrips.CMI.CMI; +import com.Zrips.CMI.Modules.Holograms.CMIHologram; +import com.Zrips.CMI.Modules.Holograms.HologramManager; +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.plugin.Plugin; + +public class CMIHolograms extends Holograms { + + CMI cmi; + HologramManager cmiHologramManager; + HashSet ourHolograms = new HashSet(); + Method cmi_CMIHologram_getLines; + + { + try { + // test if we need to watch if the lines is an array + if (CMIHologram.class.getDeclaredField("lines").getDeclaringClass() == String[].class) { + cmi_CMIHologram_getLines = CMIHologram.class.getMethod("getLines"); + } + } catch (Exception ex) { + } + } + + public CMIHolograms(Plugin plugin) { + super(plugin); + cmi = (CMI) Bukkit.getPluginManager().getPlugin("CMI"); + if (cmi != null) { + cmiHologramManager = cmi.getHologramManager(); + } + } + + @Override + public String getName() { + return "CMI"; + } + + @Override + public boolean isEnabled() { + return cmi != null && cmi.isEnabled(); + } + + @Override + protected double defaultHeightOffset() { + return 0.5; + } + + @Override + public void createHologram(Location location, List lines) { + createAt(fixLocation(location), lines); + } + + @Override + public void removeHologram(Location location) { + location = fixLocation(location); + final String id = locStr(location); + CMIHologram holo = cmiHologramManager.getByName(id); + if (holo != null) { + cmiHologramManager.removeHolo(holo); + } + ourHolograms.remove(id); + } + + @Override + public void removeAllHolograms() { + for (String id : ourHolograms) { + CMIHologram holo = cmiHologramManager.getByName(id); + if (holo != null) { + cmiHologramManager.removeHolo(holo); + } + } + ourHolograms.clear(); + } + + @Override + public void updateHologram(Location location, List lines) { + location = fixLocation(location); + CMIHologram holo = cmiHologramManager.getByName(locStr(location)); + if (holo != null) { + // only update if there is a change to the text + List holoLines; + if (cmi_CMIHologram_getLines != null) { + try { + holoLines = Arrays.asList((String[]) cmi_CMIHologram_getLines.invoke(holo)); + } catch (Exception ex) { + Logger.getLogger(CMIHolograms.class.getName()).log(Level.SEVERE, "CMI Hologram error!", ex); + holoLines = Collections.EMPTY_LIST; + } + } else { + holoLines = holo.getLines(); + } + boolean isChanged = lines.size() != holoLines.size(); + if (!isChanged) { + // double-check the lines + for (int i = 0; !isChanged && i < lines.size(); ++i) { + isChanged = !holo.getLine(i).equals(lines.get(i)); + } + } + if (isChanged) { + holo.setLines(lines); + holo.update(); + } + return; + } + createAt(location, lines); + } + + 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 lines) { + + final String id = locStr(location); + CMIHologram holo = new CMIHologram(id, location); + holo.setLines(lines); + + cmiHologramManager.addHologram(holo); + holo.update(); + + if (!ourHolograms.contains(id)) { + ourHolograms.add(id); + } + } + +} diff --git a/Core/src/main/java/com/songoda/core/hooks/holograms/HologramsHolograms.java b/Core/src/main/java/com/songoda/core/hooks/holograms/HologramsHolograms.java index 1c7f11e7..2aad4dd2 100644 --- a/Core/src/main/java/com/songoda/core/hooks/holograms/HologramsHolograms.java +++ b/Core/src/main/java/com/songoda/core/hooks/holograms/HologramsHolograms.java @@ -5,11 +5,10 @@ import com.sainttx.holograms.api.HologramPlugin; import com.sainttx.holograms.api.line.HologramLine; import com.sainttx.holograms.api.line.TextLine; import java.util.HashSet; -import org.bukkit.Location; -import org.bukkit.plugin.Plugin; - import java.util.List; import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.plugin.Plugin; public class HologramsHolograms extends Holograms {