From b4e5edfa8e6f5a4d7974a888fe1c9ed5f43b31ff Mon Sep 17 00:00:00 2001 From: filoghost Date: Mon, 23 Aug 2021 13:45:41 +0200 Subject: [PATCH] Separate v2 holograms from new API --- .../plugin/HolographicDisplays.java | 10 +- .../plugin/api/v2/V2Hologram.java | 174 ++++++++++++++ .../plugin/api/v2/V2HologramAdapter.java | 162 ------------- .../plugin/api/v2/V2HologramLine.java | 21 ++ .../plugin/api/v2/V2HologramLineAdapter.java | 53 ----- .../plugin/api/v2/V2HologramManager.java | 27 +++ .../plugin/api/v2/V2HologramsAPIProvider.java | 14 +- .../plugin/api/v2/V2ItemLine.java | 69 ++++++ .../plugin/api/v2/V2ItemLineAdapter.java | 79 ------- .../plugin/api/v2/V2PickupHandlerAdapter.java | 53 ----- .../plugin/api/v2/V2TextLine.java | 48 ++++ .../plugin/api/v2/V2TextLineAdapter.java | 31 --- .../plugin/api/v2/V2TouchHandlerAdapter.java | 53 ----- .../plugin/api/v2/V2TouchableLineAdapter.java | 68 ------ ...rAdapter.java => V2VisibilityManager.java} | 29 +-- .../plugin/api/v2/V3ClickListenerAdapter.java | 23 -- .../api/v2/V3PickupListenerAdapter.java | 23 -- .../plugin/hologram/api/APIHologram.java | 21 +- .../plugin/hologram/api/APIHologramLine.java | 3 - .../plugin/hologram/api/APIItemLine.java | 25 -- .../plugin/hologram/api/APITextLine.java | 17 -- .../api/DefaultVisibilitySettings.java | 7 - .../hologram/base/BaseHologramLines.java | 15 ++ .../plugin/listener/ChunkListener.java | 29 ++- .../plugin/api/v2/V2HologramTest.java | 61 +++++ .../api/v2/V2TouchableLineAdapterTest.java | 213 ------------------ ...anager.java => TestV2HologramManager.java} | 8 +- 27 files changed, 457 insertions(+), 879 deletions(-) create mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2Hologram.java delete mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2HologramAdapter.java create mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2HologramLine.java delete mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2HologramLineAdapter.java create mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2HologramManager.java create mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2ItemLine.java delete mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2ItemLineAdapter.java delete mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2PickupHandlerAdapter.java create mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2TextLine.java delete mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2TextLineAdapter.java delete mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2TouchHandlerAdapter.java delete mode 100644 plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2TouchableLineAdapter.java rename plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/{V2VisibilityManagerAdapter.java => V2VisibilityManager.java} (65%) create mode 100644 plugin/src/test/java/me/filoghost/holographicdisplays/plugin/api/v2/V2HologramTest.java delete mode 100644 plugin/src/test/java/me/filoghost/holographicdisplays/plugin/api/v2/V2TouchableLineAdapterTest.java rename plugin/src/test/java/me/filoghost/holographicdisplays/plugin/test/{TestAPIHologramManager.java => TestV2HologramManager.java} (75%) diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/HolographicDisplays.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/HolographicDisplays.java index baf27690..5274bbed 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/HolographicDisplays.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/HolographicDisplays.java @@ -12,6 +12,7 @@ import me.filoghost.fcommons.logging.ErrorCollector; import me.filoghost.holographicdisplays.api.internal.HolographicDisplaysAPIProvider; import me.filoghost.holographicdisplays.common.nms.NMSManager; import me.filoghost.holographicdisplays.plugin.api.current.DefaultHolographicDisplaysAPIProvider; +import me.filoghost.holographicdisplays.plugin.api.v2.V2HologramManager; import me.filoghost.holographicdisplays.plugin.api.v2.V2HologramsAPIProvider; import me.filoghost.holographicdisplays.plugin.bridge.bungeecord.BungeeServerTracker; import me.filoghost.holographicdisplays.plugin.bridge.placeholderapi.PlaceholderAPIHook; @@ -104,6 +105,7 @@ public class HolographicDisplays extends FCommonsPlugin { lineTrackerManager = new LineTrackerManager(nmsManager, placeholderTracker, lineClickListener); internalHologramManager = new InternalHologramManager(lineTrackerManager); APIHologramManager apiHologramManager = new APIHologramManager(lineTrackerManager); + V2HologramManager v2HologramManager = new V2HologramManager(lineTrackerManager); // Run only once at startup, before loading the configuration new SymbolsLegacyUpgrade(configManager, errorCollector).tryRun(); @@ -123,7 +125,7 @@ public class HolographicDisplays extends FCommonsPlugin { // Listeners registerListener(new PlayerListener(nmsManager, lineTrackerManager, lineClickListener)); - registerListener(new ChunkListener(this, internalHologramManager, apiHologramManager)); + registerListener(new ChunkListener(this, internalHologramManager, apiHologramManager, v2HologramManager)); UpdateNotificationListener updateNotificationListener = new UpdateNotificationListener(); registerListener(updateNotificationListener); @@ -135,7 +137,7 @@ public class HolographicDisplays extends FCommonsPlugin { // Enable the APIs HolographicDisplaysAPIProvider.setImplementation( new DefaultHolographicDisplaysAPIProvider(apiHologramManager, placeholderRegistry)); - enableLegacyAPI(apiHologramManager, placeholderRegistry); + enableLegacyAPI(v2HologramManager, placeholderRegistry); // Setup external plugin hooks PlaceholderAPIHook.setup(); @@ -152,8 +154,8 @@ public class HolographicDisplays extends FCommonsPlugin { } @SuppressWarnings("deprecation") - private void enableLegacyAPI(APIHologramManager apiHologramManager, PlaceholderRegistry placeholderRegistry) { - HologramsAPIProvider.setImplementation(new V2HologramsAPIProvider(apiHologramManager, placeholderRegistry)); + private void enableLegacyAPI(V2HologramManager hologramManager, PlaceholderRegistry placeholderRegistry) { + HologramsAPIProvider.setImplementation(new V2HologramsAPIProvider(hologramManager, placeholderRegistry)); } public void load(ErrorCollector errorCollector) { diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2Hologram.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2Hologram.java new file mode 100644 index 00000000..44b8765a --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2Hologram.java @@ -0,0 +1,174 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.plugin.api.v2; + +import com.gmail.filoghost.holographicdisplays.api.Hologram; +import com.gmail.filoghost.holographicdisplays.api.VisibilityManager; +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; +import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; +import com.gmail.filoghost.holographicdisplays.api.line.TextLine; +import me.filoghost.holographicdisplays.plugin.hologram.base.BaseHologram; +import me.filoghost.holographicdisplays.plugin.hologram.base.BaseHologramLines; +import me.filoghost.holographicdisplays.plugin.hologram.base.BaseHologramPosition; +import me.filoghost.holographicdisplays.plugin.hologram.tracking.LineTrackerManager; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.plugin.Plugin; + +@SuppressWarnings("deprecation") +public class V2Hologram extends BaseHologram implements Hologram { + + private final Plugin plugin; + private final V2HologramManager hologramManager; + private final BaseHologramLines lines; + private final V2VisibilityManager visibilityManager; + private final long creationTimestamp; + private boolean allowPlaceholders; + + public V2Hologram( + BaseHologramPosition position, + Plugin plugin, + LineTrackerManager lineTrackerManager, + V2HologramManager hologramManager) { + super(position, lineTrackerManager); + this.plugin = plugin; + this.hologramManager = hologramManager; + this.lines = new BaseHologramLines<>(this); + this.visibilityManager = new V2VisibilityManager(); + this.creationTimestamp = System.currentTimeMillis(); + } + + @Override + protected BaseHologramLines getLines() { + return lines; + } + + @Override + protected boolean isVisibleTo(Player player) { + return visibilityManager.isVisibleTo(player); + } + + @Override + public Plugin getCreatorPlugin() { + return plugin; + } + + @Override + public TextLine appendTextLine(String text) { + V2TextLine textLine = new V2TextLine(this, text); + lines.add(textLine); + return textLine; + } + + @Override + public ItemLine appendItemLine(ItemStack itemStack) { + V2ItemLine itemLine = new V2ItemLine(this, itemStack); + lines.add(itemLine); + return itemLine; + } + + @Override + public TextLine insertTextLine(int index, String text) { + V2TextLine textLine = new V2TextLine(this, text); + lines.insert(index, textLine); + return textLine; + } + + @Override + public ItemLine insertItemLine(int index, ItemStack itemStack) { + V2ItemLine itemLine = new V2ItemLine(this, itemStack); + lines.insert(index, itemLine); + return itemLine; + } + + @Override + public HologramLine getLine(int index) { + return lines.get(index); + } + + @Override + public void removeLine(int index) { + lines.remove(index); + } + + @Override + public void clearLines() { + lines.clear(); + } + + @Override + public int size() { + return lines.size(); + } + + @Override + public double getHeight() { + return lines.getHeight(); + } + + @Override + public void teleport(Location location) { + super.setPosition(location); + } + + @Override + public void teleport(World world, double x, double y, double z) { + super.setPosition(world, x, y, z); + } + + @Override + public Location getLocation() { + return super.getPosition().toLocation(); + } + + @Override + public double getX() { + return super.getPosition().getX(); + } + + @Override + public double getY() { + return super.getPosition().getY(); + } + + @Override + public double getZ() { + return super.getPosition().getZ(); + } + + @Override + public World getWorld() { + return super.getWorldIfLoaded(); + } + + @Override + public VisibilityManager getVisibilityManager() { + return visibilityManager; + } + + @Override + public long getCreationTimestamp() { + return creationTimestamp; + } + + @Override + public boolean isAllowPlaceholders() { + return allowPlaceholders; + } + + @Override + public void setAllowPlaceholders(boolean allowPlaceholders) { + this.allowPlaceholders = allowPlaceholders; + } + + @Override + public void delete() { + hologramManager.deleteHologram(this); + } + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2HologramAdapter.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2HologramAdapter.java deleted file mode 100644 index 29159543..00000000 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2HologramAdapter.java +++ /dev/null @@ -1,162 +0,0 @@ -/* - * Copyright (C) filoghost and contributors - * - * SPDX-License-Identifier: GPL-3.0-or-later - */ -package me.filoghost.holographicdisplays.plugin.api.v2; - -import com.gmail.filoghost.holographicdisplays.api.Hologram; -import com.gmail.filoghost.holographicdisplays.api.VisibilityManager; -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; -import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; -import com.gmail.filoghost.holographicdisplays.api.line.TextLine; -import me.filoghost.holographicdisplays.plugin.hologram.api.APIHologram; -import org.bukkit.Location; -import org.bukkit.World; -import org.bukkit.inventory.ItemStack; - -@SuppressWarnings("deprecation") -public class V2HologramAdapter implements Hologram { - - private final APIHologram v3Hologram; - private final long creationTimestamp; - - public V2HologramAdapter(APIHologram v3Hologram) { - this.v3Hologram = v3Hologram; - this.creationTimestamp = System.currentTimeMillis(); - } - - @Override - public TextLine appendTextLine(String text) { - return v3Hologram.appendTextLine(text).getV2Adapter(); - } - - @Override - public ItemLine appendItemLine(ItemStack itemStack) { - return v3Hologram.appendItemLine(itemStack).getV2Adapter(); - } - - @Override - public TextLine insertTextLine(int index, String text) { - return v3Hologram.insertTextLine(index, text).getV2Adapter(); - } - - @Override - public ItemLine insertItemLine(int index, ItemStack itemStack) { - return v3Hologram.insertItemLine(index, itemStack).getV2Adapter(); - } - - @Override - public HologramLine getLine(int index) { - return v3Hologram.getLine(index).getV2Adapter(); - } - - @Override - public void removeLine(int index) { - v3Hologram.removeLine(index); - } - - @Override - public void clearLines() { - v3Hologram.clearLines(); - } - - @Override - public int size() { - return v3Hologram.getLineCount(); - } - - @Override - public double getHeight() { - return v3Hologram.getHeight(); - } - - @Override - public void teleport(Location location) { - v3Hologram.setPosition(location); - } - - @Override - public void teleport(World world, double x, double y, double z) { - v3Hologram.setPosition(world, x, y, z); - } - - @Override - public Location getLocation() { - return v3Hologram.getPosition().toLocation(); - } - - @Override - public double getX() { - return v3Hologram.getPosition().getX(); - } - - @Override - public double getY() { - return v3Hologram.getPosition().getY(); - } - - @Override - public double getZ() { - return v3Hologram.getPosition().getZ(); - } - - @Override - public World getWorld() { - return v3Hologram.getWorldIfLoaded(); - } - - @Override - public VisibilityManager getVisibilityManager() { - return v3Hologram.getVisibilitySettings().getV2Adapter(); - } - - @Override - public long getCreationTimestamp() { - return creationTimestamp; - } - - @Override - public boolean isAllowPlaceholders() { - return v3Hologram.isAllowPlaceholders(); - } - - @Override - public void setAllowPlaceholders(boolean allowPlaceholders) { - v3Hologram.setAllowPlaceholders(allowPlaceholders); - } - - @Override - public void delete() { - v3Hologram.delete(); - } - - @Override - public boolean isDeleted() { - return v3Hologram.isDeleted(); - } - - @Override - public final boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (!(obj instanceof V2HologramAdapter)) { - return false; - } - - V2HologramAdapter other = (V2HologramAdapter) obj; - return this.v3Hologram.equals(other.v3Hologram); - } - - @Override - public final int hashCode() { - return v3Hologram.hashCode(); - } - - @Override - public final String toString() { - return v3Hologram.toString(); - } - -} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2HologramLine.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2HologramLine.java new file mode 100644 index 00000000..36366f3d --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2HologramLine.java @@ -0,0 +1,21 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.plugin.api.v2; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; +import me.filoghost.holographicdisplays.plugin.hologram.base.EditableHologramLine; + +@SuppressWarnings("deprecation") +public interface V2HologramLine extends HologramLine, EditableHologramLine { + + V2Hologram getParent(); + + @Override + default void removeLine() { + getParent().getLines().remove(this); + } + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2HologramLineAdapter.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2HologramLineAdapter.java deleted file mode 100644 index 04554080..00000000 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2HologramLineAdapter.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (C) filoghost and contributors - * - * SPDX-License-Identifier: GPL-3.0-or-later - */ -package me.filoghost.holographicdisplays.plugin.api.v2; - -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; -import me.filoghost.holographicdisplays.plugin.hologram.api.APIHologramLine; - -@SuppressWarnings("deprecation") -public abstract class V2HologramLineAdapter implements HologramLine { - - private final APIHologramLine v3Line; - - public V2HologramLineAdapter(APIHologramLine v3Line) { - this.v3Line = v3Line; - } - - @Override - public V2HologramAdapter getParent() { - return v3Line.getHologram().getV2Adapter(); - } - - @Override - public void removeLine() { - v3Line.getHologram().removeLine(v3Line); - } - - @Override - public final boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (!(obj instanceof V2HologramLineAdapter)) { - return false; - } - - V2HologramLineAdapter other = (V2HologramLineAdapter) obj; - return this.v3Line.equals(other.v3Line); - } - - @Override - public final int hashCode() { - return v3Line.hashCode(); - } - - @Override - public final String toString() { - return v3Line.toString(); - } - -} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2HologramManager.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2HologramManager.java new file mode 100644 index 00000000..ed638b6b --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2HologramManager.java @@ -0,0 +1,27 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.plugin.api.v2; + +import me.filoghost.holographicdisplays.plugin.hologram.base.BaseHologramManager; +import me.filoghost.holographicdisplays.plugin.hologram.base.BaseHologramPosition; +import me.filoghost.holographicdisplays.plugin.hologram.tracking.LineTrackerManager; +import org.bukkit.plugin.Plugin; + +public class V2HologramManager extends BaseHologramManager { + + private final LineTrackerManager lineTrackerManager; + + public V2HologramManager(LineTrackerManager lineTrackerManager) { + this.lineTrackerManager = lineTrackerManager; + } + + public V2Hologram createHologram(BaseHologramPosition position, Plugin plugin) { + V2Hologram hologram = new V2Hologram(position, plugin, lineTrackerManager, this); + super.addHologram(hologram); + return hologram; + } + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2HologramsAPIProvider.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2HologramsAPIProvider.java index f61089b3..8ecc6bec 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2HologramsAPIProvider.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2HologramsAPIProvider.java @@ -9,8 +9,6 @@ import com.gmail.filoghost.holographicdisplays.api.Hologram; import com.gmail.filoghost.holographicdisplays.api.internal.HologramsAPIProvider; import com.gmail.filoghost.holographicdisplays.api.placeholder.PlaceholderReplacer; import me.filoghost.fcommons.Preconditions; -import me.filoghost.holographicdisplays.plugin.hologram.api.APIHologram; -import me.filoghost.holographicdisplays.plugin.hologram.api.APIHologramManager; import me.filoghost.holographicdisplays.plugin.hologram.base.BaseHologramPosition; import me.filoghost.holographicdisplays.plugin.placeholder.registry.PlaceholderRegistry; import org.bukkit.Location; @@ -25,11 +23,11 @@ import java.util.List; @SuppressWarnings("deprecation") public class V2HologramsAPIProvider extends HologramsAPIProvider { - private final APIHologramManager apiHologramManager; + private final V2HologramManager hologramManager; private final PlaceholderRegistry placeholderRegistry; - public V2HologramsAPIProvider(APIHologramManager apiHologramManager, PlaceholderRegistry placeholderRegistry) { - this.apiHologramManager = apiHologramManager; + public V2HologramsAPIProvider(V2HologramManager hologramManager, PlaceholderRegistry placeholderRegistry) { + this.hologramManager = hologramManager; this.placeholderRegistry = placeholderRegistry; } @@ -40,7 +38,7 @@ public class V2HologramsAPIProvider extends HologramsAPIProvider { Preconditions.notNull(source.getWorld(), "source's world"); Preconditions.checkMainThread("async hologram creation"); - return apiHologramManager.createHologram(new BaseHologramPosition(source), plugin).getV2Adapter(); + return hologramManager.createHologram(new BaseHologramPosition(source), plugin); } @Override @@ -48,9 +46,9 @@ public class V2HologramsAPIProvider extends HologramsAPIProvider { Preconditions.notNull(plugin, "plugin"); List ownedHolograms = new ArrayList<>(); - for (APIHologram hologram : apiHologramManager.getHolograms()) { + for (V2Hologram hologram : hologramManager.getHolograms()) { if (hologram.getCreatorPlugin().equals(plugin)) { - ownedHolograms.add(hologram.getV2Adapter()); + ownedHolograms.add(hologram); } } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2ItemLine.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2ItemLine.java new file mode 100644 index 00000000..c3347212 --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2ItemLine.java @@ -0,0 +1,69 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.plugin.api.v2; + +import com.gmail.filoghost.holographicdisplays.api.handler.PickupHandler; +import com.gmail.filoghost.holographicdisplays.api.handler.TouchHandler; +import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; +import me.filoghost.holographicdisplays.api.hologram.line.ClickListener; +import me.filoghost.holographicdisplays.api.hologram.line.PickupListener; +import me.filoghost.holographicdisplays.plugin.hologram.base.BaseItemLine; +import org.bukkit.inventory.ItemStack; + +@SuppressWarnings("deprecation") +public class V2ItemLine extends BaseItemLine implements ItemLine, V2HologramLine { + + private final V2Hologram parent; + + public V2ItemLine(V2Hologram parent, ItemStack itemStack) { + super(parent, itemStack); + this.parent = parent; + } + + @Override + public V2Hologram getParent() { + return parent; + } + + @Override + public void setTouchHandler(TouchHandler touchHandler) { + if (touchHandler != null) { + super.setClickListener(new V3ClickListenerAdapter(touchHandler)); + } else { + super.setClickListener(null); + } + } + + @Override + public TouchHandler getTouchHandler() { + ClickListener clickListener = super.getClickListener(); + if (clickListener instanceof V3ClickListenerAdapter) { + return ((V3ClickListenerAdapter) clickListener).getV2TouchHandler(); + } else { + return null; + } + } + + @Override + public void setPickupHandler(PickupHandler pickupHandler) { + if (pickupHandler != null) { + super.setPickupListener(new V3PickupListenerAdapter(pickupHandler)); + } else { + super.setPickupListener(null); + } + } + + @Override + public PickupHandler getPickupHandler() { + PickupListener pickupListener = super.getPickupListener(); + if (pickupListener instanceof V3PickupListenerAdapter) { + return ((V3PickupListenerAdapter) pickupListener).getV2PickupHandler(); + } else { + return null; + } + } + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2ItemLineAdapter.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2ItemLineAdapter.java deleted file mode 100644 index 75827efe..00000000 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2ItemLineAdapter.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright (C) filoghost and contributors - * - * SPDX-License-Identifier: GPL-3.0-or-later - */ -package me.filoghost.holographicdisplays.plugin.api.v2; - -import com.gmail.filoghost.holographicdisplays.api.handler.PickupHandler; -import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; -import me.filoghost.holographicdisplays.api.hologram.line.PickupListener; -import me.filoghost.holographicdisplays.plugin.hologram.api.APIItemLine; -import org.bukkit.inventory.ItemStack; - -@SuppressWarnings("deprecation") -public class V2ItemLineAdapter extends V2TouchableLineAdapter implements ItemLine { - - private final APIItemLine v3Line; - private PickupHandler v2PickupHandler; - - public V2ItemLineAdapter(APIItemLine v3Line) { - super(v3Line); - this.v3Line = v3Line; - } - - public void onV3PickupListenerChange(PickupListener previous, PickupListener current) { - if (previous != current) { - v2PickupHandler = null; // Clear the field to force a new lazy initialization - } - } - - @Override - public void setPickupHandler(PickupHandler v2PickupHandler) { - if (v2PickupHandler == null) { - v3Line.setPickupListener(null); - } else if (v2PickupHandler instanceof V2PickupHandlerAdapter) { - // Adapter created from the getter method, simply unwrap it - v3Line.setPickupListener(((V2PickupHandlerAdapter) v2PickupHandler).getV3PickupListener()); - } else { - PickupListener v3PickupListener = v3Line.getPickupListener(); - - // Adapt the old v2 handler to the new API, creating a new instance only if the wrapped handler changed - if (!(v3PickupListener instanceof V3PickupListenerAdapter) - || ((V3PickupListenerAdapter) v3PickupListener).getV2PickupHandler() != v2PickupHandler) { - v3Line.setPickupListener(new V3PickupListenerAdapter(v2PickupHandler)); - } - } - this.v2PickupHandler = v2PickupHandler; - } - - @Override - public PickupHandler getPickupHandler() { - // Lazy initialization - if (v2PickupHandler == null) { - PickupListener v3PickupListener = v3Line.getPickupListener(); - - if (v3PickupListener == null) { - // Keep it null - } else if (v3PickupListener instanceof V3PickupListenerAdapter) { - // Adapter created from the setter method, simply unwrap it - v2PickupHandler = ((V3PickupListenerAdapter) v3PickupListener).getV2PickupHandler(); - } else { - // Adapt the new handler to the old v2 API - v2PickupHandler = new V2PickupHandlerAdapter(v3PickupListener); - } - } - return v2PickupHandler; - } - - @Override - public ItemStack getItemStack() { - return v3Line.getItemStack(); - } - - @Override - public void setItemStack(ItemStack itemStack) { - v3Line.setItemStack(itemStack); - } - -} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2PickupHandlerAdapter.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2PickupHandlerAdapter.java deleted file mode 100644 index 09c16507..00000000 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2PickupHandlerAdapter.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (C) filoghost and contributors - * - * SPDX-License-Identifier: GPL-3.0-or-later - */ -package me.filoghost.holographicdisplays.plugin.api.v2; - -import com.gmail.filoghost.holographicdisplays.api.handler.PickupHandler; -import me.filoghost.holographicdisplays.api.hologram.line.PickupListener; -import org.bukkit.entity.Player; - -@SuppressWarnings("deprecation") -class V2PickupHandlerAdapter implements PickupHandler { - - private final PickupListener v3PickupListener; - - V2PickupHandlerAdapter(PickupListener v3PickupListener) { - this.v3PickupListener = v3PickupListener; - } - - @Override - public void onPickup(Player player) { - v3PickupListener.onPickup(player); - } - - public PickupListener getV3PickupListener() { - return v3PickupListener; - } - - @Override - public final boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (!(obj instanceof V2PickupHandlerAdapter)) { - return false; - } - - V2PickupHandlerAdapter other = (V2PickupHandlerAdapter) obj; - return this.v3PickupListener.equals(other.v3PickupListener); - } - - @Override - public final int hashCode() { - return v3PickupListener.hashCode(); - } - - @Override - public final String toString() { - return v3PickupListener.toString(); - } - -} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2TextLine.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2TextLine.java new file mode 100644 index 00000000..18511f56 --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2TextLine.java @@ -0,0 +1,48 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.plugin.api.v2; + +import com.gmail.filoghost.holographicdisplays.api.handler.TouchHandler; +import com.gmail.filoghost.holographicdisplays.api.line.TextLine; +import me.filoghost.holographicdisplays.api.hologram.line.ClickListener; +import me.filoghost.holographicdisplays.plugin.hologram.base.BaseTextLine; + +@SuppressWarnings("deprecation") +public class V2TextLine extends BaseTextLine implements TextLine, V2HologramLine { + + private final V2Hologram parent; + + public V2TextLine(V2Hologram parent, String text) { + super(parent, text); + this.parent = parent; + } + + @Override + public V2Hologram getParent() { + return parent; + } + + @Override + public boolean isAllowPlaceholders() { + return parent.isAllowPlaceholders(); + } + + @Override + public void setTouchHandler(TouchHandler touchHandler) { + super.setClickListener(new V3ClickListenerAdapter(touchHandler)); + } + + @Override + public TouchHandler getTouchHandler() { + ClickListener clickListener = super.getClickListener(); + if (clickListener instanceof V3ClickListenerAdapter) { + return ((V3ClickListenerAdapter) clickListener).getV2TouchHandler(); + } else { + return null; + } + } + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2TextLineAdapter.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2TextLineAdapter.java deleted file mode 100644 index 845afd23..00000000 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2TextLineAdapter.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (C) filoghost and contributors - * - * SPDX-License-Identifier: GPL-3.0-or-later - */ -package me.filoghost.holographicdisplays.plugin.api.v2; - -import com.gmail.filoghost.holographicdisplays.api.line.TextLine; -import me.filoghost.holographicdisplays.plugin.hologram.api.APITextLine; - -@SuppressWarnings("deprecation") -public class V2TextLineAdapter extends V2TouchableLineAdapter implements TextLine { - - private final APITextLine v3Line; - - public V2TextLineAdapter(APITextLine v3Line) { - super(v3Line); - this.v3Line = v3Line; - } - - @Override - public String getText() { - return v3Line.getText(); - } - - @Override - public void setText(String text) { - v3Line.setText(text); - } - -} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2TouchHandlerAdapter.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2TouchHandlerAdapter.java deleted file mode 100644 index 03c983fe..00000000 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2TouchHandlerAdapter.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (C) filoghost and contributors - * - * SPDX-License-Identifier: GPL-3.0-or-later - */ -package me.filoghost.holographicdisplays.plugin.api.v2; - -import com.gmail.filoghost.holographicdisplays.api.handler.TouchHandler; -import me.filoghost.holographicdisplays.api.hologram.line.ClickListener; -import org.bukkit.entity.Player; - -@SuppressWarnings("deprecation") -class V2TouchHandlerAdapter implements TouchHandler { - - private final ClickListener v3ClickListener; - - V2TouchHandlerAdapter(ClickListener v3ClickListener) { - this.v3ClickListener = v3ClickListener; - } - - @Override - public void onTouch(Player player) { - v3ClickListener.onClick(player); - } - - public ClickListener getV3ClickListener() { - return v3ClickListener; - } - - @Override - public final boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (!(obj instanceof V2TouchHandlerAdapter)) { - return false; - } - - V2TouchHandlerAdapter other = (V2TouchHandlerAdapter) obj; - return this.v3ClickListener.equals(other.v3ClickListener); - } - - @Override - public final int hashCode() { - return v3ClickListener.hashCode(); - } - - @Override - public final String toString() { - return v3ClickListener.toString(); - } - -} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2TouchableLineAdapter.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2TouchableLineAdapter.java deleted file mode 100644 index 6f729fcd..00000000 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2TouchableLineAdapter.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (C) filoghost and contributors - * - * SPDX-License-Identifier: GPL-3.0-or-later - */ -package me.filoghost.holographicdisplays.plugin.api.v2; - -import com.gmail.filoghost.holographicdisplays.api.handler.TouchHandler; -import com.gmail.filoghost.holographicdisplays.api.line.TouchableLine; -import me.filoghost.holographicdisplays.api.hologram.line.ClickListener; -import me.filoghost.holographicdisplays.plugin.hologram.api.APIClickableLine; - -@SuppressWarnings("deprecation") -abstract class V2TouchableLineAdapter extends V2HologramLineAdapter implements TouchableLine { - - private final APIClickableLine v3Line; - private TouchHandler v2TouchHandler; - - V2TouchableLineAdapter(APIClickableLine v3Line) { - super(v3Line); - this.v3Line = v3Line; - } - - public void onV3ClickListenerChange(ClickListener previous, ClickListener current) { - if (previous != current) { - v2TouchHandler = null; // Clear the field to force a new lazy initialization - } - } - - @Override - public void setTouchHandler(TouchHandler v2TouchHandler) { - if (v2TouchHandler == null) { - v3Line.setClickListener(null); - } else if (v2TouchHandler instanceof V2TouchHandlerAdapter) { - // Adapter created from the getter method, simply unwrap it - v3Line.setClickListener(((V2TouchHandlerAdapter) v2TouchHandler).getV3ClickListener()); - } else { - ClickListener v3ClickListener = v3Line.getClickListener(); - - // Adapt the old v2 handler to the new API, creating a new instance only if the wrapped handler changed - if (!(v3ClickListener instanceof V3ClickListenerAdapter) - || ((V3ClickListenerAdapter) v3ClickListener).getV2TouchHandler() != v2TouchHandler) { - v3Line.setClickListener(new V3ClickListenerAdapter(v2TouchHandler)); - } - } - this.v2TouchHandler = v2TouchHandler; - } - - @Override - public TouchHandler getTouchHandler() { - // Lazy initialization - if (v2TouchHandler == null) { - ClickListener v3ClickListener = v3Line.getClickListener(); - - if (v3ClickListener == null) { - // Keep it null - } else if (v3ClickListener instanceof V3ClickListenerAdapter) { - // Adapter created from the setter method, simply unwrap it - v2TouchHandler = ((V3ClickListenerAdapter) v3ClickListener).getV2TouchHandler(); - } else { - // Adapt the new handler to the old v2 API - v2TouchHandler = new V2TouchHandlerAdapter(v3ClickListener); - } - } - return v2TouchHandler; - } - -} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2VisibilityManagerAdapter.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2VisibilityManager.java similarity index 65% rename from plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2VisibilityManagerAdapter.java rename to plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2VisibilityManager.java index 55f1fd1a..4bfab46e 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2VisibilityManagerAdapter.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V2VisibilityManager.java @@ -11,12 +11,12 @@ import me.filoghost.holographicdisplays.plugin.hologram.api.DefaultVisibilitySet import org.bukkit.entity.Player; @SuppressWarnings("deprecation") -public class V2VisibilityManagerAdapter implements VisibilityManager { +public class V2VisibilityManager implements VisibilityManager { private final DefaultVisibilitySettings v3VisibilitySettings; - public V2VisibilityManagerAdapter(DefaultVisibilitySettings v3VisibilitySettings) { - this.v3VisibilitySettings = v3VisibilitySettings; + public V2VisibilityManager() { + this.v3VisibilitySettings = new DefaultVisibilitySettings(); } @Override @@ -54,27 +54,4 @@ public class V2VisibilityManagerAdapter implements VisibilityManager { v3VisibilitySettings.resetIndividualVisibilityAll(); } - @Override - public final boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (!(obj instanceof V2VisibilityManagerAdapter)) { - return false; - } - - V2VisibilityManagerAdapter other = (V2VisibilityManagerAdapter) obj; - return this.v3VisibilitySettings.equals(other.v3VisibilitySettings); - } - - @Override - public final int hashCode() { - return v3VisibilitySettings.hashCode(); - } - - @Override - public final String toString() { - return v3VisibilitySettings.toString(); - } - } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V3ClickListenerAdapter.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V3ClickListenerAdapter.java index dd4384eb..0dab447d 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V3ClickListenerAdapter.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V3ClickListenerAdapter.java @@ -28,27 +28,4 @@ public class V3ClickListenerAdapter implements ClickListener { return v2TouchHandler; } - @Override - public final boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (!(obj instanceof V3ClickListenerAdapter)) { - return false; - } - - V3ClickListenerAdapter other = (V3ClickListenerAdapter) obj; - return this.v2TouchHandler.equals(other.v2TouchHandler); - } - - @Override - public final int hashCode() { - return v2TouchHandler.hashCode(); - } - - @Override - public final String toString() { - return v2TouchHandler.toString(); - } - } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V3PickupListenerAdapter.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V3PickupListenerAdapter.java index 4622f322..464933b6 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V3PickupListenerAdapter.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/api/v2/V3PickupListenerAdapter.java @@ -28,27 +28,4 @@ public class V3PickupListenerAdapter implements PickupListener { return v2PickupHandler; } - @Override - public final boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (!(obj instanceof V3PickupListenerAdapter)) { - return false; - } - - V3PickupListenerAdapter other = (V3PickupListenerAdapter) obj; - return this.v2PickupHandler.equals(other.v2PickupHandler); - } - - @Override - public final int hashCode() { - return v2PickupHandler.hashCode(); - } - - @Override - public final String toString() { - return v2PickupHandler.toString(); - } - } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/api/APIHologram.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/api/APIHologram.java index b38c4dce..5627624d 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/api/APIHologram.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/api/APIHologram.java @@ -8,8 +8,6 @@ package me.filoghost.holographicdisplays.plugin.hologram.api; import me.filoghost.fcommons.Preconditions; import me.filoghost.holographicdisplays.api.hologram.Hologram; import me.filoghost.holographicdisplays.api.hologram.line.HologramLine; -import me.filoghost.holographicdisplays.plugin.api.v2.V2HologramAdapter; -import me.filoghost.holographicdisplays.plugin.config.Settings; import me.filoghost.holographicdisplays.plugin.hologram.base.BaseHologram; import me.filoghost.holographicdisplays.plugin.hologram.base.BaseHologramLines; import me.filoghost.holographicdisplays.plugin.hologram.base.BaseHologramPosition; @@ -26,7 +24,6 @@ public class APIHologram extends BaseHologram implements Hologram { private final Plugin plugin; private final APIHologramManager apiHologramManager; private final DefaultVisibilitySettings visibilitySettings; - private final V2HologramAdapter v2Adapter; private boolean allowPlaceholders; @@ -41,7 +38,6 @@ public class APIHologram extends BaseHologram implements Hologram { this.plugin = plugin; this.apiHologramManager = apiHologramManager; this.visibilitySettings = new DefaultVisibilitySettings(); - this.v2Adapter = new V2HologramAdapter(this); } @Override @@ -153,18 +149,7 @@ public class APIHologram extends BaseHologram implements Hologram { @Override public double getHeight() { - if (lines.isEmpty()) { - return 0; - } - - double height = 0.0; - - for (APIHologramLine line : lines) { - height += line.getHeight(); - } - - height += Settings.spaceBetweenLines * (lines.size() - 1); - return height; + return lines.getHeight(); } @Override @@ -177,8 +162,4 @@ public class APIHologram extends BaseHologram implements Hologram { apiHologramManager.deleteHologram(this); } - public V2HologramAdapter getV2Adapter() { - return v2Adapter; - } - } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/api/APIHologramLine.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/api/APIHologramLine.java index 8b24e82b..b3ddd91f 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/api/APIHologramLine.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/api/APIHologramLine.java @@ -6,7 +6,6 @@ package me.filoghost.holographicdisplays.plugin.hologram.api; import me.filoghost.holographicdisplays.api.hologram.line.HologramLine; -import me.filoghost.holographicdisplays.plugin.api.v2.V2HologramLineAdapter; import me.filoghost.holographicdisplays.plugin.hologram.base.EditableHologramLine; import org.jetbrains.annotations.NotNull; @@ -17,6 +16,4 @@ public interface APIHologramLine extends HologramLine, EditableHologramLine { void setChanged(); - V2HologramLineAdapter getV2Adapter(); - } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/api/APIItemLine.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/api/APIItemLine.java index b6388ffb..0031d8a3 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/api/APIItemLine.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/api/APIItemLine.java @@ -6,23 +6,17 @@ package me.filoghost.holographicdisplays.plugin.hologram.api; import me.filoghost.holographicdisplays.api.hologram.line.ItemHologramLine; -import me.filoghost.holographicdisplays.api.hologram.line.PickupListener; -import me.filoghost.holographicdisplays.api.hologram.line.ClickListener; -import me.filoghost.holographicdisplays.plugin.api.v2.V2ItemLineAdapter; import me.filoghost.holographicdisplays.plugin.hologram.base.BaseItemLine; import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; public class APIItemLine extends BaseItemLine implements ItemHologramLine, APIClickableLine { private final APIHologram parent; - private final V2ItemLineAdapter v2Adapter; public APIItemLine(APIHologram parent, ItemStack itemStack) { super(parent, itemStack); this.parent = parent; - this.v2Adapter = new V2ItemLineAdapter(this); } @Override @@ -30,23 +24,4 @@ public class APIItemLine extends BaseItemLine implements ItemHologramLine, APICl return parent; } - @Override - public void setClickListener(@Nullable ClickListener clickListener) { - ClickListener oldClickListener = getClickListener(); - super.setClickListener(clickListener); - v2Adapter.onV3ClickListenerChange(oldClickListener, clickListener); - } - - @Override - public void setPickupListener(@Nullable PickupListener pickupListener) { - PickupListener oldPickupListener = getPickupListener(); - super.setPickupListener(pickupListener); - v2Adapter.onV3PickupListenerChange(oldPickupListener, pickupListener); - } - - @Override - public V2ItemLineAdapter getV2Adapter() { - return v2Adapter; - } - } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/api/APITextLine.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/api/APITextLine.java index ef6dd499..fd243bfa 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/api/APITextLine.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/api/APITextLine.java @@ -5,22 +5,17 @@ */ package me.filoghost.holographicdisplays.plugin.hologram.api; -import me.filoghost.holographicdisplays.api.hologram.line.ClickListener; import me.filoghost.holographicdisplays.api.hologram.line.TextHologramLine; -import me.filoghost.holographicdisplays.plugin.api.v2.V2TextLineAdapter; import me.filoghost.holographicdisplays.plugin.hologram.base.BaseTextLine; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; public class APITextLine extends BaseTextLine implements TextHologramLine, APIClickableLine { private final APIHologram parent; - private final V2TextLineAdapter v2Adapter; public APITextLine(APIHologram parent, String text) { super(parent, text); this.parent = parent; - this.v2Adapter = new V2TextLineAdapter(this); } @Override @@ -33,16 +28,4 @@ public class APITextLine extends BaseTextLine implements TextHologramLine, APICl return parent.isAllowPlaceholders(); } - @Override - public void setClickListener(@Nullable ClickListener clickListener) { - ClickListener oldClickListener = getClickListener(); - super.setClickListener(clickListener); - v2Adapter.onV3ClickListenerChange(oldClickListener, clickListener); - } - - @Override - public V2TextLineAdapter getV2Adapter() { - return v2Adapter; - } - } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/api/DefaultVisibilitySettings.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/api/DefaultVisibilitySettings.java index 776979a7..79917ae2 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/api/DefaultVisibilitySettings.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/api/DefaultVisibilitySettings.java @@ -7,7 +7,6 @@ package me.filoghost.holographicdisplays.plugin.hologram.api; import me.filoghost.fcommons.Preconditions; import me.filoghost.holographicdisplays.api.hologram.VisibilitySettings; -import me.filoghost.holographicdisplays.plugin.api.v2.V2VisibilityManagerAdapter; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; @@ -17,12 +16,10 @@ import java.util.concurrent.ConcurrentHashMap; public class DefaultVisibilitySettings implements VisibilitySettings { - private final V2VisibilityManagerAdapter v2Adapter; private Map visibilityByPlayer; private Visibility defaultVisibility; public DefaultVisibilitySettings() { - this.v2Adapter = new V2VisibilityManagerAdapter(this); this.defaultVisibility = Visibility.VISIBLE; } @@ -87,10 +84,6 @@ public class DefaultVisibilitySettings implements VisibilitySettings { visibilityByPlayer.clear(); } - public V2VisibilityManagerAdapter getV2Adapter() { - return v2Adapter; - } - @Override public String toString() { return "DefaultVisibilitySettings{" diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/base/BaseHologramLines.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/base/BaseHologramLines.java index faad775f..a207f45d 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/base/BaseHologramLines.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/base/BaseHologramLines.java @@ -130,6 +130,21 @@ public class BaseHologramLines implements Iterab } } + public double getHeight() { + if (isEmpty()) { + return 0; + } + + double height = 0.0; + + for (EditableHologramLine line : lines) { + height += line.getHeight(); + } + + height += Settings.spaceBetweenLines * (lines.size() - 1); + return height; + } + public void setDeleted() { for (T line : lines) { line.setDeleted(); diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/listener/ChunkListener.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/listener/ChunkListener.java index adeaac08..5fc3c82d 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/listener/ChunkListener.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/listener/ChunkListener.java @@ -5,10 +5,12 @@ */ package me.filoghost.holographicdisplays.plugin.listener; +import me.filoghost.holographicdisplays.plugin.api.v2.V2HologramManager; import me.filoghost.holographicdisplays.plugin.hologram.api.APIHologramManager; import me.filoghost.holographicdisplays.plugin.hologram.internal.InternalHologramManager; import org.bukkit.Bukkit; import org.bukkit.Chunk; +import org.bukkit.World; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; @@ -23,29 +25,41 @@ public class ChunkListener implements Listener { private final Plugin plugin; private final InternalHologramManager internalHologramManager; private final APIHologramManager apiHologramManager; + private final V2HologramManager v2HologramManager; - public ChunkListener(Plugin plugin, InternalHologramManager internalHologramManager, APIHologramManager apiHologramManager) { + public ChunkListener( + Plugin plugin, + InternalHologramManager internalHologramManager, + APIHologramManager apiHologramManager, + V2HologramManager v2HologramManager) { this.plugin = plugin; this.internalHologramManager = internalHologramManager; this.apiHologramManager = apiHologramManager; + this.v2HologramManager = v2HologramManager; } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onWorldUnload(WorldUnloadEvent event) { - internalHologramManager.onWorldUnload(event.getWorld()); - apiHologramManager.onWorldUnload(event.getWorld()); + World world = event.getWorld(); + internalHologramManager.onWorldUnload(world); + apiHologramManager.onWorldUnload(world); + v2HologramManager.onWorldUnload(world); } @EventHandler(priority = EventPriority.MONITOR) public void onWorldLoad(WorldLoadEvent event) { - internalHologramManager.onWorldLoad(event.getWorld()); - apiHologramManager.onWorldLoad(event.getWorld()); + World world = event.getWorld(); + internalHologramManager.onWorldLoad(world); + apiHologramManager.onWorldLoad(world); + v2HologramManager.onWorldLoad(world); } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onChunkUnload(ChunkUnloadEvent event) { - internalHologramManager.onChunkUnload(event.getChunk()); - apiHologramManager.onChunkUnload(event.getChunk()); + Chunk chunk = event.getChunk(); + internalHologramManager.onChunkUnload(chunk); + apiHologramManager.onChunkUnload(chunk); + v2HologramManager.onChunkUnload(chunk); } @EventHandler(priority = EventPriority.MONITOR) @@ -68,6 +82,7 @@ public class ChunkListener implements Listener { private void onChunkLoad(Chunk chunk) { internalHologramManager.onChunkLoad(chunk); apiHologramManager.onChunkLoad(chunk); + v2HologramManager.onChunkLoad(chunk); } } diff --git a/plugin/src/test/java/me/filoghost/holographicdisplays/plugin/api/v2/V2HologramTest.java b/plugin/src/test/java/me/filoghost/holographicdisplays/plugin/api/v2/V2HologramTest.java new file mode 100644 index 00000000..490d0dbc --- /dev/null +++ b/plugin/src/test/java/me/filoghost/holographicdisplays/plugin/api/v2/V2HologramTest.java @@ -0,0 +1,61 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.plugin.api.v2; + +import com.gmail.filoghost.holographicdisplays.api.handler.PickupHandler; +import com.gmail.filoghost.holographicdisplays.api.handler.TouchHandler; +import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; +import com.gmail.filoghost.holographicdisplays.api.line.TextLine; +import me.filoghost.holographicdisplays.plugin.hologram.base.BaseHologramPosition; +import me.filoghost.holographicdisplays.plugin.test.Mocks; +import me.filoghost.holographicdisplays.plugin.test.TestV2HologramManager; +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.*; + +@SuppressWarnings("deprecation") +class V2HologramTest { + + V2HologramManager hologramManager = new TestV2HologramManager(); + + V2Hologram hologram = hologramManager.createHologram( + new BaseHologramPosition("world", 0, 0, 0), + Mocks.PLUGIN + ); + + @BeforeAll + static void beforeAll() { + Mocks.prepareEnvironment(); + } + + @Test + void setGetTouchHandler() { + TextLine textLine = hologram.appendTextLine(""); + + TouchHandler touchHandler = player -> {}; + textLine.setTouchHandler(touchHandler); + assertThat(textLine.getTouchHandler()).isSameAs(touchHandler); + + textLine.setTouchHandler(null); + assertThat(textLine.getTouchHandler()).isNull(); + } + + @Test + void setGetPickupHandler() { + ItemLine itemLine = hologram.appendItemLine(new ItemStack(Material.ARROW)); + + PickupHandler pickupHandler = player -> {}; + itemLine.setPickupHandler(pickupHandler); + assertThat(itemLine.getPickupHandler()).isSameAs(pickupHandler); + + itemLine.setPickupHandler(null); + assertThat(itemLine.getPickupHandler()).isNull(); + } + +} diff --git a/plugin/src/test/java/me/filoghost/holographicdisplays/plugin/api/v2/V2TouchableLineAdapterTest.java b/plugin/src/test/java/me/filoghost/holographicdisplays/plugin/api/v2/V2TouchableLineAdapterTest.java deleted file mode 100644 index e45f7c64..00000000 --- a/plugin/src/test/java/me/filoghost/holographicdisplays/plugin/api/v2/V2TouchableLineAdapterTest.java +++ /dev/null @@ -1,213 +0,0 @@ -/* - * Copyright (C) filoghost and contributors - * - * SPDX-License-Identifier: GPL-3.0-or-later - */ -package me.filoghost.holographicdisplays.plugin.api.v2; - -import com.gmail.filoghost.holographicdisplays.api.handler.TouchHandler; -import com.gmail.filoghost.holographicdisplays.api.line.TextLine; -import me.filoghost.holographicdisplays.api.hologram.line.ClickListener; -import me.filoghost.holographicdisplays.plugin.hologram.api.APIHologram; -import me.filoghost.holographicdisplays.plugin.hologram.api.APIHologramManager; -import me.filoghost.holographicdisplays.plugin.hologram.api.APITextLine; -import me.filoghost.holographicdisplays.plugin.hologram.base.BaseHologramPosition; -import me.filoghost.holographicdisplays.plugin.test.Mocks; -import me.filoghost.holographicdisplays.plugin.test.TestAPIHologramManager; -import org.bukkit.entity.Player; -import org.jetbrains.annotations.NotNull; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; - -import static org.assertj.core.api.Assertions.*; - -@SuppressWarnings("deprecation") -class V2TouchableLineAdapterTest { - - APIHologramManager apiHologramManager = new TestAPIHologramManager(); - - APIHologram hologram = apiHologramManager.createHologram( - new BaseHologramPosition("world", 0, 0, 0), - Mocks.PLUGIN - ); - - @BeforeAll - static void beforeAll() { - Mocks.prepareEnvironment(); - } - - @Test - void setNullV2TouchHandler() { - APITextLine v3Line = hologram.appendTextLine(""); - TextLine v2Line = v3Line.getV2Adapter(); - ExampleV3ClickListener v3ClickListener = new ExampleV3ClickListener(); - - v3Line.setClickListener(v3ClickListener); - v2Line.setTouchHandler(null); - - assertThat(v2Line.getTouchHandler()).isNull(); - assertThat(v3Line.getClickListener()).isNull(); - } - - @Test - void setNullV3ClickListener() { - APITextLine v3Line = hologram.appendTextLine(""); - TextLine v2Line = v3Line.getV2Adapter(); - ExampleV2TouchHandler v2TouchHandler = new ExampleV2TouchHandler(); - - v2Line.setTouchHandler(v2TouchHandler); - v3Line.setClickListener(null); - - assertThat(v2Line.getTouchHandler()).isNull(); - assertThat(v3Line.getClickListener()).isNull(); - } - - @Test - void setThenGetV2TouchHandler() { - TextLine v2Line = new APITextLine(hologram, "").getV2Adapter(); - ExampleV2TouchHandler v2TouchHandler = new ExampleV2TouchHandler(); - - v2Line.setTouchHandler(v2TouchHandler); - - assertThat(v2Line.getTouchHandler()).isSameAs(v2TouchHandler); - } - - @Test - void setV3ClickListenerThenGetV2TouchHandler() { - APITextLine v3Line = hologram.appendTextLine(""); - TextLine v2Line = v3Line.getV2Adapter(); - ExampleV3ClickListener v3ClickListener = new ExampleV3ClickListener(); - - v3Line.setClickListener(v3ClickListener); - - assertThat(v2Line.getTouchHandler()).isNotNull(); - } - - @Test - void setV2TouchHandlerThenGetV3ClickListener() { - APITextLine v3Line = hologram.appendTextLine(""); - TextLine v2Line = v3Line.getV2Adapter(); - ExampleV2TouchHandler v2TouchHandler = new ExampleV2TouchHandler(); - - v2Line.setTouchHandler(v2TouchHandler); - - assertThat(v3Line.getClickListener()).isNotNull(); - } - - @Test - void repeatedGetV2TouchHandlerShouldReturnSameInstance() { - APITextLine v3Line = hologram.appendTextLine(""); - TextLine v2Line = v3Line.getV2Adapter(); - - v3Line.setClickListener(new ExampleV3ClickListener()); - assertThat(v2Line.getTouchHandler()).isSameAs(v2Line.getTouchHandler()); - - v2Line.setTouchHandler(new ExampleV2TouchHandler()); - assertThat(v2Line.getTouchHandler()).isSameAs(v2Line.getTouchHandler()); - } - - @Test - void repeatedSetV2TouchHandlerShouldNotChangeV3ClickListenerInstance() { - APITextLine v3Line = hologram.appendTextLine(""); - TextLine v2Line = v3Line.getV2Adapter(); - ExampleV2TouchHandler v2TouchHandler = new ExampleV2TouchHandler(); - - v2Line.setTouchHandler(v2TouchHandler); - ClickListener v3ClickListenerA = v3Line.getClickListener(); - v2Line.setTouchHandler(v2TouchHandler); - ClickListener v3ClickListenerB = v3Line.getClickListener(); - - assertThat(v3ClickListenerA).isSameAs(v3ClickListenerB); - } - - @Test - void repeatedSetV3ClickListenerShouldNotChangeV2TouchHandlerInstance() { - APITextLine v3Line = hologram.appendTextLine(""); - TextLine v2Line = v3Line.getV2Adapter(); - ExampleV3ClickListener v2TouchHandler = new ExampleV3ClickListener(); - - v3Line.setClickListener(v2TouchHandler); - TouchHandler v2TouchHandlerA = v2Line.getTouchHandler(); - v3Line.setClickListener(v2TouchHandler); - TouchHandler v2TouchHandlerB = v2Line.getTouchHandler(); - - assertThat(v2TouchHandlerA).isSameAs(v2TouchHandlerB); - } - - @Test - void v2TouchHandlerChangesWhenV3ClickListenerChanges() { - APITextLine v3Line = hologram.appendTextLine(""); - TextLine v2Line = v3Line.getV2Adapter(); - - v3Line.setClickListener(new ExampleV3ClickListener()); - TouchHandler v2TouchHandlerA = v2Line.getTouchHandler(); - v3Line.setClickListener(new ExampleV3ClickListener()); - TouchHandler v2TouchHandlerB = v2Line.getTouchHandler(); - - assertThat(v2TouchHandlerA).isNotSameAs(v2TouchHandlerB); - } - - @Test - void v3ClickListenerChangesWhenV2TouchHandlerChanges() { - APITextLine v3Line = hologram.appendTextLine(""); - TextLine v2Line = v3Line.getV2Adapter(); - - v2Line.setTouchHandler(new ExampleV2TouchHandler()); - ClickListener v3ClickListenerA = v3Line.getClickListener(); - v2Line.setTouchHandler(new ExampleV2TouchHandler()); - ClickListener v3ClickListenerB = v3Line.getClickListener(); - - assertThat(v3ClickListenerA).isNotSameAs(v3ClickListenerB); - } - - @Test - void preserveInstancesWhenUsingV3API() { - APITextLine v3Line = hologram.appendTextLine(""); - TextLine v2Line = v3Line.getV2Adapter(); - ExampleV2TouchHandler v2TouchHandler = new ExampleV2TouchHandler(); - - v2Line.setTouchHandler(v2TouchHandler); - - ClickListener v3ClickListener = v3Line.getClickListener(); - v2Line.setTouchHandler(null); // This also clears the adapter - v3Line.setClickListener(v3ClickListener); // Return to the initial value - - // Instances should be the same - assertThat(v2Line.getTouchHandler()).isSameAs(v2TouchHandler); - assertThat(v3Line.getClickListener()).isSameAs(v3ClickListener); - } - - @Test - void preserveInstancesWhenUsingV2API() { - APITextLine v3Line = hologram.appendTextLine(""); - TextLine v2Line = v3Line.getV2Adapter(); - ExampleV3ClickListener v3ClickListener = new ExampleV3ClickListener(); - - v3Line.setClickListener(v3ClickListener); - - TouchHandler v2TouchHandler = v2Line.getTouchHandler(); - v2Line.setTouchHandler(null); // This also clears the adapter - v2Line.setTouchHandler(v2TouchHandler); // Return to the initial value - - // Instances should be the same - assertThat(v2Line.getTouchHandler()).isSameAs(v2TouchHandler); - assertThat(v3Line.getClickListener()).isSameAs(v3ClickListener); - } - - - private static class ExampleV3ClickListener implements ClickListener { - - @Override - public void onClick(@NotNull Player player) {} - - } - - - private static class ExampleV2TouchHandler implements TouchHandler { - - @Override - public void onTouch(Player player) {} - - } - -} diff --git a/plugin/src/test/java/me/filoghost/holographicdisplays/plugin/test/TestAPIHologramManager.java b/plugin/src/test/java/me/filoghost/holographicdisplays/plugin/test/TestV2HologramManager.java similarity index 75% rename from plugin/src/test/java/me/filoghost/holographicdisplays/plugin/test/TestAPIHologramManager.java rename to plugin/src/test/java/me/filoghost/holographicdisplays/plugin/test/TestV2HologramManager.java index e576ab5c..0d43f16e 100644 --- a/plugin/src/test/java/me/filoghost/holographicdisplays/plugin/test/TestAPIHologramManager.java +++ b/plugin/src/test/java/me/filoghost/holographicdisplays/plugin/test/TestV2HologramManager.java @@ -5,16 +5,16 @@ */ package me.filoghost.holographicdisplays.plugin.test; -import me.filoghost.holographicdisplays.plugin.hologram.api.APIHologramManager; -import me.filoghost.holographicdisplays.plugin.listener.LineClickListener; +import me.filoghost.holographicdisplays.plugin.api.v2.V2HologramManager; import me.filoghost.holographicdisplays.plugin.hologram.tracking.LineTrackerManager; +import me.filoghost.holographicdisplays.plugin.listener.LineClickListener; import me.filoghost.holographicdisplays.plugin.placeholder.tracking.PlaceholderTracker; import static org.mockito.Mockito.*; -public class TestAPIHologramManager extends APIHologramManager { +public class TestV2HologramManager extends V2HologramManager { - public TestAPIHologramManager() { + public TestV2HologramManager() { super(new LineTrackerManager(new TestNMSManager(), mock(PlaceholderTracker.class), new LineClickListener())); }