diff --git a/api/src/main/java/me/filoghost/holographicdisplays/api/internal/HolographicDisplaysAPIProvider.java b/api/src/main/java/me/filoghost/holographicdisplays/api/internal/HolographicDisplaysAPIProvider.java index 4b06b036..edf9670a 100644 --- a/api/src/main/java/me/filoghost/holographicdisplays/api/internal/HolographicDisplaysAPIProvider.java +++ b/api/src/main/java/me/filoghost/holographicdisplays/api/internal/HolographicDisplaysAPIProvider.java @@ -12,6 +12,8 @@ import org.jetbrains.annotations.ApiStatus.Internal; @Internal public abstract class HolographicDisplaysAPIProvider { + + public static final String ERROR_IMPLEMENTATION_NOT_SET = "Holographic Displays did not load properly (no API implementation was set)"; private static HolographicDisplaysAPIProvider implementation; @@ -21,7 +23,7 @@ public abstract class HolographicDisplaysAPIProvider { public static HolographicDisplaysAPIProvider getImplementation() { if (implementation == null) { - throw new IllegalStateException("Holographic Displays did not load properly"); + throw new IllegalStateException(ERROR_IMPLEMENTATION_NOT_SET); } return implementation; diff --git a/legacy-api/v2/src/main/java/com/gmail/filoghost/holographicdisplays/api/HologramsAPI.java b/legacy-api/v2/src/main/java/com/gmail/filoghost/holographicdisplays/api/HologramsAPI.java index 831780a1..10e83327 100644 --- a/legacy-api/v2/src/main/java/com/gmail/filoghost/holographicdisplays/api/HologramsAPI.java +++ b/legacy-api/v2/src/main/java/com/gmail/filoghost/holographicdisplays/api/HologramsAPI.java @@ -6,7 +6,7 @@ package com.gmail.filoghost.holographicdisplays.api; import com.gmail.filoghost.holographicdisplays.api.placeholder.PlaceholderReplacer; -import com.gmail.filoghost.holographicdisplays.api.internal.BackendAPI; +import com.gmail.filoghost.holographicdisplays.api.internal.HologramsAPIProvider; import org.bukkit.Location; import org.bukkit.entity.Entity; import org.bukkit.plugin.Plugin; @@ -23,37 +23,37 @@ public class HologramsAPI { @Deprecated public static Hologram createHologram(Plugin plugin, Location source) { - return BackendAPI.getImplementation().createHologram(plugin, source); + return HologramsAPIProvider.getImplementation().createHologram(plugin, source); } @Deprecated public static Collection getHolograms(Plugin plugin) { - return BackendAPI.getImplementation().getHolograms(plugin); + return HologramsAPIProvider.getImplementation().getHolograms(plugin); } @Deprecated public static boolean registerPlaceholder(Plugin plugin, String textPlaceholder, double refreshRate, PlaceholderReplacer replacer) { - return BackendAPI.getImplementation().registerPlaceholder(plugin, textPlaceholder, refreshRate, replacer); + return HologramsAPIProvider.getImplementation().registerPlaceholder(plugin, textPlaceholder, refreshRate, replacer); } @Deprecated public static Collection getRegisteredPlaceholders(Plugin plugin) { - return BackendAPI.getImplementation().getRegisteredPlaceholders(plugin); + return HologramsAPIProvider.getImplementation().getRegisteredPlaceholders(plugin); } @Deprecated public static boolean unregisterPlaceholder(Plugin plugin, String textPlaceholder) { - return BackendAPI.getImplementation().unregisterPlaceholder(plugin, textPlaceholder); + return HologramsAPIProvider.getImplementation().unregisterPlaceholder(plugin, textPlaceholder); } @Deprecated public static void unregisterPlaceholders(Plugin plugin) { - BackendAPI.getImplementation().unregisterPlaceholders(plugin); + HologramsAPIProvider.getImplementation().unregisterPlaceholders(plugin); } @Deprecated public static boolean isHologramEntity(Entity bukkitEntity) { - return BackendAPI.getImplementation().isHologramEntity(bukkitEntity); + return HologramsAPIProvider.getImplementation().isHologramEntity(bukkitEntity); } } diff --git a/legacy-api/v2/src/main/java/com/gmail/filoghost/holographicdisplays/api/internal/BackendAPI.java b/legacy-api/v2/src/main/java/com/gmail/filoghost/holographicdisplays/api/internal/HologramsAPIProvider.java similarity index 70% rename from legacy-api/v2/src/main/java/com/gmail/filoghost/holographicdisplays/api/internal/BackendAPI.java rename to legacy-api/v2/src/main/java/com/gmail/filoghost/holographicdisplays/api/internal/HologramsAPIProvider.java index 45a7a374..82241823 100644 --- a/legacy-api/v2/src/main/java/com/gmail/filoghost/holographicdisplays/api/internal/BackendAPI.java +++ b/legacy-api/v2/src/main/java/com/gmail/filoghost/holographicdisplays/api/internal/HologramsAPIProvider.java @@ -5,11 +5,13 @@ */ package com.gmail.filoghost.holographicdisplays.api.internal; -import com.gmail.filoghost.holographicdisplays.api.placeholder.PlaceholderReplacer; import com.gmail.filoghost.holographicdisplays.api.Hologram; +import com.gmail.filoghost.holographicdisplays.api.placeholder.PlaceholderReplacer; +import me.filoghost.holographicdisplays.api.internal.HolographicDisplaysAPIProvider; import org.bukkit.Location; import org.bukkit.entity.Entity; import org.bukkit.plugin.Plugin; +import org.jetbrains.annotations.ApiStatus.Internal; import java.util.Collection; @@ -17,19 +19,20 @@ import java.util.Collection; * @deprecated Please use the new API! */ @Deprecated -public abstract class BackendAPI { +@Internal +public abstract class HologramsAPIProvider { - private static BackendAPI implementation; + private static HologramsAPIProvider implementation; @Deprecated - public static void setImplementation(BackendAPI implementation) { - BackendAPI.implementation = implementation; + public static void setImplementation(HologramsAPIProvider implementation) { + HologramsAPIProvider.implementation = implementation; } @Deprecated - public static BackendAPI getImplementation() { + public static HologramsAPIProvider getImplementation() { if (implementation == null) { - throw new IllegalStateException("No API implementation set. Is Holographic Displays enabled?"); + throw new IllegalStateException(HolographicDisplaysAPIProvider.ERROR_IMPLEMENTATION_NOT_SET); } return implementation; diff --git a/plugin/pom.xml b/plugin/pom.xml index f2720d34..a38e77f9 100644 --- a/plugin/pom.xml +++ b/plugin/pom.xml @@ -32,7 +32,6 @@ ${project.groupId} holographicdisplays-legacy-api-v2 - runtime diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/HolographicDisplays.java b/plugin/src/main/java/me/filoghost/holographicdisplays/HolographicDisplays.java index a9cc7f79..54b7a15a 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/HolographicDisplays.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/HolographicDisplays.java @@ -5,6 +5,7 @@ */ package me.filoghost.holographicdisplays; +import com.gmail.filoghost.holographicdisplays.api.internal.HologramsAPIProvider; import me.filoghost.fcommons.FCommonsPlugin; import me.filoghost.fcommons.FeatureSupport; import me.filoghost.fcommons.config.exception.ConfigException; @@ -19,6 +20,7 @@ import me.filoghost.holographicdisplays.disk.ConfigManager; import me.filoghost.holographicdisplays.disk.Configuration; import me.filoghost.holographicdisplays.disk.HologramDatabase; import me.filoghost.holographicdisplays.disk.upgrade.LegacySymbolsUpgrader; +import me.filoghost.holographicdisplays.legacy.api.v2.V2HologramsAPIProvider; import me.filoghost.holographicdisplays.listener.ChunkListener; import me.filoghost.holographicdisplays.listener.InteractListener; import me.filoghost.holographicdisplays.listener.SpawnListener; @@ -31,6 +33,7 @@ import me.filoghost.holographicdisplays.object.internal.InternalHologramManager; import me.filoghost.holographicdisplays.placeholder.PlaceholderManager; import me.filoghost.holographicdisplays.placeholder.internal.AnimationRegistry; import me.filoghost.holographicdisplays.placeholder.internal.DefaultPlaceholders; +import me.filoghost.holographicdisplays.placeholder.registry.PlaceholderRegistry; import me.filoghost.holographicdisplays.util.NMSVersion; import org.bstats.bukkit.MetricsLite; import org.bukkit.Bukkit; @@ -122,11 +125,15 @@ public class HolographicDisplays extends FCommonsPlugin implements ProtocolPacke UpdateNotificationListener updateNotificationListener = new UpdateNotificationListener(); registerListener(updateNotificationListener); - // Enable the API. + // Enable the APIs HolographicDisplaysAPIProvider.setImplementation(new DefaultHolographicDisplaysAPIProvider( apiHologramManager, nmsManager, placeholderManager.getPlaceholderRegistry())); + enableLegacyAPI( + apiHologramManager, + nmsManager, + placeholderManager.getPlaceholderRegistry()); // Register bStats metrics int pluginID = 3123; @@ -139,7 +146,15 @@ public class HolographicDisplays extends FCommonsPlugin implements ProtocolPacke Bukkit.getScheduler().runTaskLater(this, errorCollector::logErrorCount, 10L); } } - + + @SuppressWarnings("deprecation") + private void enableLegacyAPI(APIHologramManager apiHologramManager, NMSManager nmsManager, PlaceholderRegistry placeholderRegistry) { + HologramsAPIProvider.setImplementation(new V2HologramsAPIProvider( + apiHologramManager, + nmsManager, + placeholderRegistry)); + } + public void load(boolean deferHologramsCreation, ErrorCollector errorCollector) { DefaultPlaceholders.resetAndRegister(placeholderManager.getPlaceholderRegistry(), animationRegistry, bungeeServerTracker); diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/legacy/api/v2/NewPickupHandlerAdapter.java b/plugin/src/main/java/me/filoghost/holographicdisplays/legacy/api/v2/NewPickupHandlerAdapter.java new file mode 100644 index 00000000..67a62e4b --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/legacy/api/v2/NewPickupHandlerAdapter.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.legacy.api.v2; + +import com.gmail.filoghost.holographicdisplays.api.handler.PickupHandler; +import org.bukkit.entity.Player; + +@SuppressWarnings("deprecation") +public class NewPickupHandlerAdapter implements me.filoghost.holographicdisplays.api.handler.PickupHandler { + + private final PickupHandler v2PickupHandler; + + public NewPickupHandlerAdapter(PickupHandler v2PickupHandler) { + this.v2PickupHandler = v2PickupHandler; + } + + @Override + public void onPickup(Player player) { + v2PickupHandler.onPickup(player); + } + + public PickupHandler getV2PickupHandler() { + return v2PickupHandler; + } + + @Override + public final boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!(obj instanceof NewPickupHandlerAdapter)) { + return false; + } + + NewPickupHandlerAdapter other = (NewPickupHandlerAdapter) 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/legacy/api/v2/NewTouchHandlerAdapter.java b/plugin/src/main/java/me/filoghost/holographicdisplays/legacy/api/v2/NewTouchHandlerAdapter.java new file mode 100644 index 00000000..96c48591 --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/legacy/api/v2/NewTouchHandlerAdapter.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.legacy.api.v2; + +import com.gmail.filoghost.holographicdisplays.api.handler.TouchHandler; +import org.bukkit.entity.Player; + +@SuppressWarnings("deprecation") +public class NewTouchHandlerAdapter implements me.filoghost.holographicdisplays.api.handler.TouchHandler { + + private final TouchHandler v2TouchHandler; + + public NewTouchHandlerAdapter(TouchHandler v2TouchHandler) { + this.v2TouchHandler = v2TouchHandler; + } + + @Override + public void onTouch(Player player) { + v2TouchHandler.onTouch(player); + } + + public TouchHandler getV2TouchHandler() { + return v2TouchHandler; + } + + @Override + public final boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!(obj instanceof NewTouchHandlerAdapter)) { + return false; + } + + NewTouchHandlerAdapter other = (NewTouchHandlerAdapter) 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/legacy/api/v2/V2HologramAdapter.java b/plugin/src/main/java/me/filoghost/holographicdisplays/legacy/api/v2/V2HologramAdapter.java new file mode 100644 index 00000000..9679651a --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/legacy/api/v2/V2HologramAdapter.java @@ -0,0 +1,160 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.legacy.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.object.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 newHologram; + + public V2HologramAdapter(APIHologram newHologram) { + this.newHologram = newHologram; + } + + @Override + public TextLine appendTextLine(String text) { + return newHologram.appendTextLine(text).getV2Adapter(); + } + + @Override + public ItemLine appendItemLine(ItemStack itemStack) { + return newHologram.appendItemLine(itemStack).getV2Adapter(); + } + + @Override + public TextLine insertTextLine(int index, String text) { + return newHologram.insertTextLine(index, text).getV2Adapter(); + } + + @Override + public ItemLine insertItemLine(int index, ItemStack itemStack) { + return newHologram.insertItemLine(index, itemStack).getV2Adapter(); + } + + @Override + public HologramLine getLine(int index) { + return newHologram.getLine(index).getV2Adapter(); + } + + @Override + public void removeLine(int index) { + newHologram.removeLine(index); + } + + @Override + public void clearLines() { + newHologram.clearLines(); + } + + @Override + public int size() { + return newHologram.size(); + } + + @Override + public double getHeight() { + return newHologram.getHeight(); + } + + @Override + public void teleport(Location location) { + newHologram.teleport(location); + } + + @Override + public void teleport(World world, double x, double y, double z) { + newHologram.teleport(world, x, y, z); + } + + @Override + public Location getLocation() { + return newHologram.getLocation(); + } + + @Override + public double getX() { + return newHologram.getX(); + } + + @Override + public double getY() { + return newHologram.getY(); + } + + @Override + public double getZ() { + return newHologram.getZ(); + } + + @Override + public World getWorld() { + return newHologram.getWorld(); + } + + @Override + public VisibilityManager getVisibilityManager() { + return newHologram.getVisibilityManager().getV2Adapter(); + } + + @Override + public long getCreationTimestamp() { + return newHologram.getCreationTimestamp(); + } + + @Override + public boolean isAllowPlaceholders() { + return newHologram.isAllowPlaceholders(); + } + + @Override + public void setAllowPlaceholders(boolean allowPlaceholders) { + newHologram.setAllowPlaceholders(allowPlaceholders); + } + + @Override + public void delete() { + newHologram.delete(); + } + + @Override + public boolean isDeleted() { + return newHologram.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.newHologram.equals(other.newHologram); + } + + @Override + public final int hashCode() { + return newHologram.hashCode(); + } + + @Override + public final String toString() { + return newHologram.toString(); + } + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/legacy/api/v2/V2HologramLineAdapter.java b/plugin/src/main/java/me/filoghost/holographicdisplays/legacy/api/v2/V2HologramLineAdapter.java new file mode 100644 index 00000000..54b80b6b --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/legacy/api/v2/V2HologramLineAdapter.java @@ -0,0 +1,53 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.legacy.api.v2; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; +import me.filoghost.holographicdisplays.object.api.APIHologramLine; + +@SuppressWarnings("deprecation") +public abstract class V2HologramLineAdapter implements HologramLine { + + private final APIHologramLine newHologramLine; + + public V2HologramLineAdapter(APIHologramLine newHologramLine) { + this.newHologramLine = newHologramLine; + } + + @Override + public V2HologramAdapter getParent() { + return newHologramLine.getParent().getV2Adapter(); + } + + @Override + public void removeLine() { + newHologramLine.removeLine(); + } + + @Override + public final boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!(obj instanceof V2HologramLineAdapter)) { + return false; + } + + V2HologramLineAdapter other = (V2HologramLineAdapter) obj; + return this.newHologramLine.equals(other.newHologramLine); + } + + @Override + public final int hashCode() { + return newHologramLine.hashCode(); + } + + @Override + public final String toString() { + return newHologramLine.toString(); + } + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/legacy/api/v2/V2HologramsAPIProvider.java b/plugin/src/main/java/me/filoghost/holographicdisplays/legacy/api/v2/V2HologramsAPIProvider.java new file mode 100644 index 00000000..ced4400a --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/legacy/api/v2/V2HologramsAPIProvider.java @@ -0,0 +1,116 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.legacy.api.v2; + +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.core.nms.NMSManager; +import me.filoghost.holographicdisplays.object.api.APIHologram; +import me.filoghost.holographicdisplays.object.api.APIHologramManager; +import me.filoghost.holographicdisplays.placeholder.registry.PlaceholderRegistry; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.entity.Entity; +import org.bukkit.plugin.Plugin; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +@SuppressWarnings("deprecation") +public class V2HologramsAPIProvider extends HologramsAPIProvider { + + private final APIHologramManager apiHologramManager; + private final NMSManager nmsManager; + private final PlaceholderRegistry placeholderRegistry; + + public V2HologramsAPIProvider(APIHologramManager apiHologramManager, NMSManager nmsManager, PlaceholderRegistry placeholderRegistry) { + this.apiHologramManager = apiHologramManager; + this.nmsManager = nmsManager; + this.placeholderRegistry = placeholderRegistry; + } + + @Override + public Hologram createHologram(Plugin plugin, Location source) { + Preconditions.notNull(plugin, "plugin"); + Preconditions.notNull(source, "source"); + Preconditions.notNull(source.getWorld(), "source's world"); + Preconditions.checkState(Bukkit.isPrimaryThread(), "async hologram creation"); + + return apiHologramManager.createHologram(source, plugin).getV2Adapter(); + } + + @Override + public Collection getHolograms(Plugin plugin) { + Preconditions.notNull(plugin, "plugin"); + List ownedHolograms = new ArrayList<>(); + + for (APIHologram hologram : apiHologramManager.getHolograms()) { + if (hologram.getOwnerPlugin().equals(plugin)) { + ownedHolograms.add(hologram.getV2Adapter()); + } + } + + return Collections.unmodifiableList(ownedHolograms); + } + + @Override + public boolean registerPlaceholder(Plugin plugin, String textPlaceholder, double refreshRate, PlaceholderReplacer replacer) { + Preconditions.notNull(plugin, "plugin"); + Preconditions.notNull(textPlaceholder, "textPlaceholder"); + Preconditions.checkArgument(refreshRate >= 0, "refreshRate should be positive"); + Preconditions.notNull(replacer, "replacer"); + + int refreshIntervalTicks = Math.min((int) (refreshRate * 20.0), 1); + boolean alreadyRegistered = placeholderRegistry.isRegisteredIdentifier(plugin, textPlaceholder); + + if (!alreadyRegistered) { + placeholderRegistry.registerReplacer(plugin, textPlaceholder, refreshIntervalTicks, argument -> replacer.update()); + return true; + } else { + return false; + } + } + + @Override + public Collection getRegisteredPlaceholders(Plugin plugin) { + Preconditions.notNull(plugin, "plugin"); + + return placeholderRegistry.getRegisteredIdentifiers(plugin); + } + + @Override + public boolean unregisterPlaceholder(Plugin plugin, String textPlaceholder) { + Preconditions.notNull(plugin, "plugin"); + Preconditions.notNull(textPlaceholder, "textPlaceholder"); + + boolean registered = placeholderRegistry.isRegisteredIdentifier(plugin, textPlaceholder); + + if (registered) { + placeholderRegistry.unregister(plugin, textPlaceholder); + return true; + } else { + return false; + } + } + + @Override + public void unregisterPlaceholders(Plugin plugin) { + Preconditions.notNull(plugin, "plugin"); + + placeholderRegistry.unregisterAll(plugin); + } + + @Override + public boolean isHologramEntity(Entity bukkitEntity) { + Preconditions.notNull(bukkitEntity, "bukkitEntity"); + return nmsManager.isNMSEntityBase(bukkitEntity); + } + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/legacy/api/v2/V2ItemLineAdapter.java b/plugin/src/main/java/me/filoghost/holographicdisplays/legacy/api/v2/V2ItemLineAdapter.java new file mode 100644 index 00000000..ec06a7f2 --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/legacy/api/v2/V2ItemLineAdapter.java @@ -0,0 +1,79 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.legacy.api.v2; + +import com.gmail.filoghost.holographicdisplays.api.handler.PickupHandler; +import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; +import me.filoghost.holographicdisplays.object.api.APIItemLine; +import org.bukkit.inventory.ItemStack; + +@SuppressWarnings("deprecation") +public class V2ItemLineAdapter extends V2TouchableLineAdapter implements ItemLine { + + private final APIItemLine newItemLine; + private PickupHandler v2PickupHandler; + + public V2ItemLineAdapter(APIItemLine newItemLine) { + super(newItemLine); + this.newItemLine = newItemLine; + } + + public void onNewPickupHandlerChange(me.filoghost.holographicdisplays.api.handler.PickupHandler previous, + me.filoghost.holographicdisplays.api.handler.PickupHandler current) { + if (previous != current) { + v2PickupHandler = null; // Clear the field to force a new lazy initialization + } + } + + @Override + public void setPickupHandler(PickupHandler v2PickupHandler) { + if (v2PickupHandler == null) { + newItemLine.setPickupHandler(null); + } else if (v2PickupHandler instanceof V2PickupHandlerAdapter) { + // Adapter created from the getPickupHandler() method, simply unwrap it + newItemLine.setPickupHandler(((V2PickupHandlerAdapter) v2PickupHandler).getNewPickupHandler()); + } else { + me.filoghost.holographicdisplays.api.handler.PickupHandler newPickupHandler = newItemLine.getPickupHandler(); + + // Adapt the old v2 handler to the new API, creating a new instance only if the wrapped handler changed + if (!(newPickupHandler instanceof NewPickupHandlerAdapter) + || ((NewPickupHandlerAdapter) newPickupHandler).getV2PickupHandler() != v2PickupHandler) { + newItemLine.setPickupHandler(new NewPickupHandlerAdapter(v2PickupHandler)); + } + } + this.v2PickupHandler = v2PickupHandler; + } + + @Override + public PickupHandler getPickupHandler() { + // Lazy initialization + if (v2PickupHandler == null) { + me.filoghost.holographicdisplays.api.handler.PickupHandler newPickupHandler = newItemLine.getPickupHandler(); + + if (newPickupHandler == null) { + // Keep it null + } else if (newPickupHandler instanceof NewPickupHandlerAdapter) { + // Adapter created from the setPickupHandler() method, simply unwrap it + v2PickupHandler = ((NewPickupHandlerAdapter) newPickupHandler).getV2PickupHandler(); + } else { + // Adapt the new handler to the old v2 API + v2PickupHandler = new V2PickupHandlerAdapter(newPickupHandler); + } + } + return v2PickupHandler; + } + + @Override + public ItemStack getItemStack() { + return newItemLine.getItemStack(); + } + + @Override + public void setItemStack(ItemStack itemStack) { + newItemLine.setItemStack(itemStack); + } + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/legacy/api/v2/V2PickupHandlerAdapter.java b/plugin/src/main/java/me/filoghost/holographicdisplays/legacy/api/v2/V2PickupHandlerAdapter.java new file mode 100644 index 00000000..b45c3d5f --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/legacy/api/v2/V2PickupHandlerAdapter.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.legacy.api.v2; + +import me.filoghost.holographicdisplays.api.handler.PickupHandler; +import org.bukkit.entity.Player; + +@SuppressWarnings("deprecation") +class V2PickupHandlerAdapter implements com.gmail.filoghost.holographicdisplays.api.handler.PickupHandler { + + private final PickupHandler newPickupHandler; + + V2PickupHandlerAdapter(PickupHandler newPickupHandler) { + this.newPickupHandler = newPickupHandler; + } + + @Override + public void onPickup(Player player) { + newPickupHandler.onPickup(player); + } + + public PickupHandler getNewPickupHandler() { + return newPickupHandler; + } + + @Override + public final boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!(obj instanceof V2PickupHandlerAdapter)) { + return false; + } + + V2PickupHandlerAdapter other = (V2PickupHandlerAdapter) obj; + return this.newPickupHandler.equals(other.newPickupHandler); + } + + @Override + public final int hashCode() { + return newPickupHandler.hashCode(); + } + + @Override + public final String toString() { + return newPickupHandler.toString(); + } + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/legacy/api/v2/V2TextLineAdapter.java b/plugin/src/main/java/me/filoghost/holographicdisplays/legacy/api/v2/V2TextLineAdapter.java new file mode 100644 index 00000000..c198be95 --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/legacy/api/v2/V2TextLineAdapter.java @@ -0,0 +1,31 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.legacy.api.v2; + +import com.gmail.filoghost.holographicdisplays.api.line.TextLine; +import me.filoghost.holographicdisplays.object.api.APITextLine; + +@SuppressWarnings("deprecation") +public class V2TextLineAdapter extends V2TouchableLineAdapter implements TextLine { + + private final APITextLine newTextLine; + + public V2TextLineAdapter(APITextLine newTextLine) { + super(newTextLine); + this.newTextLine = newTextLine; + } + + @Override + public String getText() { + return newTextLine.getText(); + } + + @Override + public void setText(String text) { + newTextLine.setText(text); + } + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/legacy/api/v2/V2TouchHandlerAdapter.java b/plugin/src/main/java/me/filoghost/holographicdisplays/legacy/api/v2/V2TouchHandlerAdapter.java new file mode 100644 index 00000000..de2890f7 --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/legacy/api/v2/V2TouchHandlerAdapter.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.legacy.api.v2; + +import me.filoghost.holographicdisplays.api.handler.TouchHandler; +import org.bukkit.entity.Player; + +@SuppressWarnings("deprecation") +class V2TouchHandlerAdapter implements com.gmail.filoghost.holographicdisplays.api.handler.TouchHandler { + + private final TouchHandler newTouchHandler; + + V2TouchHandlerAdapter(TouchHandler newTouchHandler) { + this.newTouchHandler = newTouchHandler; + } + + @Override + public void onTouch(Player player) { + newTouchHandler.onTouch(player); + } + + public TouchHandler getNewTouchHandler() { + return newTouchHandler; + } + + @Override + public final boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!(obj instanceof V2TouchHandlerAdapter)) { + return false; + } + + V2TouchHandlerAdapter other = (V2TouchHandlerAdapter) obj; + return this.newTouchHandler.equals(other.newTouchHandler); + } + + @Override + public final int hashCode() { + return newTouchHandler.hashCode(); + } + + @Override + public final String toString() { + return newTouchHandler.toString(); + } + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/legacy/api/v2/V2TouchableLineAdapter.java b/plugin/src/main/java/me/filoghost/holographicdisplays/legacy/api/v2/V2TouchableLineAdapter.java new file mode 100644 index 00000000..459d93a7 --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/legacy/api/v2/V2TouchableLineAdapter.java @@ -0,0 +1,68 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.legacy.api.v2; + +import com.gmail.filoghost.holographicdisplays.api.handler.TouchHandler; +import com.gmail.filoghost.holographicdisplays.api.line.TouchableLine; +import me.filoghost.holographicdisplays.object.api.APITouchableLine; + +@SuppressWarnings("deprecation") +abstract class V2TouchableLineAdapter extends V2HologramLineAdapter implements TouchableLine { + + private final APITouchableLine newTouchableLine; + private TouchHandler v2TouchHandler; + + V2TouchableLineAdapter(APITouchableLine newTouchableLine) { + super(newTouchableLine); + this.newTouchableLine = newTouchableLine; + } + + public void onNewTouchHandlerChange(me.filoghost.holographicdisplays.api.handler.TouchHandler previous, + me.filoghost.holographicdisplays.api.handler.TouchHandler current) { + if (previous != current) { + v2TouchHandler = null; // Clear the field to force a new lazy initialization + } + } + + @Override + public void setTouchHandler(TouchHandler v2TouchHandler) { + if (v2TouchHandler == null) { + newTouchableLine.setTouchHandler(null); + } else if (v2TouchHandler instanceof V2TouchHandlerAdapter) { + // Adapter created from the getTouchHandler() method, simply unwrap it + newTouchableLine.setTouchHandler(((V2TouchHandlerAdapter) v2TouchHandler).getNewTouchHandler()); + } else { + me.filoghost.holographicdisplays.api.handler.TouchHandler newTouchHandler = newTouchableLine.getTouchHandler(); + + // Adapt the old v2 handler to the new API, creating a new instance only if the wrapped handler changed + if (!(newTouchHandler instanceof NewTouchHandlerAdapter) + || ((NewTouchHandlerAdapter) newTouchHandler).getV2TouchHandler() != v2TouchHandler) { + newTouchableLine.setTouchHandler(new NewTouchHandlerAdapter(v2TouchHandler)); + } + } + this.v2TouchHandler = v2TouchHandler; + } + + @Override + public TouchHandler getTouchHandler() { + // Lazy initialization + if (v2TouchHandler == null) { + me.filoghost.holographicdisplays.api.handler.TouchHandler newTouchHandler = newTouchableLine.getTouchHandler(); + + if (newTouchHandler == null) { + // Keep it null + } else if (newTouchHandler instanceof NewTouchHandlerAdapter) { + // Adapter created from the setTouchHandler() method, simply unwrap it + v2TouchHandler = ((NewTouchHandlerAdapter) newTouchHandler).getV2TouchHandler(); + } else { + // Adapt the new handler to the old v2 API + v2TouchHandler = new V2TouchHandlerAdapter(newTouchHandler); + } + } + return v2TouchHandler; + } + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/legacy/api/v2/V2VisibilityManagerAdapter.java b/plugin/src/main/java/me/filoghost/holographicdisplays/legacy/api/v2/V2VisibilityManagerAdapter.java new file mode 100644 index 00000000..78cbb346 --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/legacy/api/v2/V2VisibilityManagerAdapter.java @@ -0,0 +1,79 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.legacy.api.v2; + +import com.gmail.filoghost.holographicdisplays.api.VisibilityManager; +import me.filoghost.holographicdisplays.object.api.DefaultVisibilityManager; +import org.bukkit.entity.Player; + +@SuppressWarnings("deprecation") +public class V2VisibilityManagerAdapter implements VisibilityManager { + + private final DefaultVisibilityManager newVisibilityManager; + + public V2VisibilityManagerAdapter(DefaultVisibilityManager newVisibilityManager) { + this.newVisibilityManager = newVisibilityManager; + } + + @Override + public boolean isVisibleByDefault() { + return newVisibilityManager.isVisibleByDefault(); + } + + @Override + public void setVisibleByDefault(boolean visibleByDefault) { + newVisibilityManager.setVisibleByDefault(visibleByDefault); + } + + @Override + public void showTo(Player player) { + newVisibilityManager.showTo(player); + } + + @Override + public void hideTo(Player player) { + newVisibilityManager.hideTo(player); + } + + @Override + public boolean isVisibleTo(Player player) { + return newVisibilityManager.isVisibleTo(player); + } + + @Override + public void resetVisibility(Player player) { + newVisibilityManager.resetVisibility(player); + } + + @Override + public void resetVisibilityAll() { + newVisibilityManager.resetVisibilityAll(); + } + + @Override + public final boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!(obj instanceof V2VisibilityManagerAdapter)) { + return false; + } + + V2VisibilityManagerAdapter other = (V2VisibilityManagerAdapter) obj; + return this.newVisibilityManager.equals(other.newVisibilityManager); + } + + @Override + public final int hashCode() { + return newVisibilityManager.hashCode(); + } + + @Override + public final String toString() { + return newVisibilityManager.toString(); + } + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APIHologram.java b/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APIHologram.java index f2bbd7d2..bec16fa6 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APIHologram.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APIHologram.java @@ -7,12 +7,9 @@ package me.filoghost.holographicdisplays.object.api; import me.filoghost.fcommons.Preconditions; import me.filoghost.holographicdisplays.api.Hologram; -import me.filoghost.holographicdisplays.api.VisibilityManager; -import me.filoghost.holographicdisplays.api.line.HologramLine; -import me.filoghost.holographicdisplays.api.line.ItemLine; -import me.filoghost.holographicdisplays.api.line.TextLine; import me.filoghost.holographicdisplays.core.nms.NMSManager; import me.filoghost.holographicdisplays.disk.Configuration; +import me.filoghost.holographicdisplays.legacy.api.v2.V2HologramAdapter; import me.filoghost.holographicdisplays.object.base.BaseHologram; import me.filoghost.holographicdisplays.placeholder.PlaceholderManager; import org.bukkit.Location; @@ -23,11 +20,12 @@ import org.bukkit.plugin.Plugin; import java.util.List; public class APIHologram extends BaseHologram implements Hologram { - + private final Plugin plugin; private final APIHologramManager apiHologramManager; - private final VisibilityManager visibilityManager; + private final DefaultVisibilityManager visibilityManager; private final long creationTimestamp; + private final V2HologramAdapter v2Adapter; private boolean allowPlaceholders; @@ -38,6 +36,7 @@ public class APIHologram extends BaseHologram implements Hologr this.apiHologramManager = apiHologramManager; this.visibilityManager = new DefaultVisibilityManager(this); this.creationTimestamp = System.currentTimeMillis(); + this.v2Adapter = new V2HologramAdapter(this); } @Override @@ -46,14 +45,14 @@ public class APIHologram extends BaseHologram implements Hologr } @Override - public TextLine appendTextLine(String text) { + public APITextLine appendTextLine(String text) { APITextLine line = createTextLine(text); addLine(line); return line; } @Override - public ItemLine appendItemLine(ItemStack itemStack) { + public APIItemLine appendItemLine(ItemStack itemStack) { Preconditions.notNull(itemStack, "itemStack"); APIItemLine line = createItemLine(itemStack); @@ -62,14 +61,14 @@ public class APIHologram extends BaseHologram implements Hologr } @Override - public TextLine insertTextLine(int index, String text) { + public APITextLine insertTextLine(int index, String text) { APITextLine line = createTextLine(text); addLine(line); return line; } @Override - public ItemLine insertItemLine(int index, ItemStack itemStack) { + public APIItemLine insertItemLine(int index, ItemStack itemStack) { Preconditions.notNull(itemStack, "itemStack"); APIItemLine line = createItemLine(itemStack); @@ -86,7 +85,7 @@ public class APIHologram extends BaseHologram implements Hologr } @Override - public HologramLine getLine(int index) { + public APIHologramLine getLine(int index) { return getLines().get(index); } @@ -138,7 +137,7 @@ public class APIHologram extends BaseHologram implements Hologr } @Override - public VisibilityManager getVisibilityManager() { + public DefaultVisibilityManager getVisibilityManager() { return visibilityManager; } @@ -152,4 +151,8 @@ public class APIHologram extends BaseHologram implements Hologr return plugin.getName() + "@" + Integer.toHexString(hashCode()); } + public V2HologramAdapter getV2Adapter() { + return v2Adapter; + } + } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APIHologramLine.java b/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APIHologramLine.java index 2325f730..f2956e11 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APIHologramLine.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APIHologramLine.java @@ -7,6 +7,7 @@ package me.filoghost.holographicdisplays.object.api; import me.filoghost.holographicdisplays.api.line.HologramLine; import me.filoghost.holographicdisplays.core.hologram.StandardHologramLine; +import me.filoghost.holographicdisplays.legacy.api.v2.V2HologramLineAdapter; public interface APIHologramLine extends HologramLine, StandardHologramLine { @@ -18,4 +19,6 @@ public interface APIHologramLine extends HologramLine, StandardHologramLine { getParent().removeLine(this); } + V2HologramLineAdapter getV2Adapter(); + } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APIHologramManager.java b/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APIHologramManager.java index 8b3bc1f9..2695cdf9 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APIHologramManager.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APIHologramManager.java @@ -27,7 +27,7 @@ public class APIHologramManager extends BaseHologramManager { this.placeholderManager = placeholderManager; } - public Hologram createHologram(Location source, Plugin plugin) { + public APIHologram createHologram(Location source, Plugin plugin) { APIHologram hologram = new APIHologram(source, plugin, nmsManager, this, placeholderManager); super.addHologram(hologram); return hologram; diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APIItemLine.java b/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APIItemLine.java index 2b353494..0cdfb49e 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APIItemLine.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APIItemLine.java @@ -1,16 +1,21 @@ package me.filoghost.holographicdisplays.object.api; +import me.filoghost.holographicdisplays.api.handler.PickupHandler; +import me.filoghost.holographicdisplays.api.handler.TouchHandler; import me.filoghost.holographicdisplays.api.line.ItemLine; +import me.filoghost.holographicdisplays.legacy.api.v2.V2ItemLineAdapter; import me.filoghost.holographicdisplays.object.base.BaseItemLine; import org.bukkit.inventory.ItemStack; -public class APIItemLine extends BaseItemLine implements ItemLine, APIHologramLine { +public class APIItemLine extends BaseItemLine implements ItemLine, APITouchableLine { 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 @@ -18,4 +23,21 @@ public class APIItemLine extends BaseItemLine implements ItemLine, APIHologramLi return parent; } + @Override + public void setTouchHandler(TouchHandler touchHandler) { + v2Adapter.onNewTouchHandlerChange(getTouchHandler(), touchHandler); + super.setTouchHandler(touchHandler); + } + + @Override + public void setPickupHandler(PickupHandler pickupHandler) { + v2Adapter.onNewPickupHandlerChange(getPickupHandler(), pickupHandler); + super.setPickupHandler(pickupHandler); + } + + @Override + public V2ItemLineAdapter getV2Adapter() { + return v2Adapter; + } + } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APITextLine.java b/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APITextLine.java index 805b193b..f0b71d19 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APITextLine.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APITextLine.java @@ -1,15 +1,19 @@ package me.filoghost.holographicdisplays.object.api; +import me.filoghost.holographicdisplays.api.handler.TouchHandler; import me.filoghost.holographicdisplays.api.line.TextLine; +import me.filoghost.holographicdisplays.legacy.api.v2.V2TextLineAdapter; import me.filoghost.holographicdisplays.object.base.BaseTextLine; -public class APITextLine extends BaseTextLine implements TextLine, APIHologramLine { +public class APITextLine extends BaseTextLine implements TextLine, APITouchableLine { 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 @@ -17,14 +21,20 @@ public class APITextLine extends BaseTextLine implements TextLine, APIHologramLi return parent; } - @Override - public void setText(String text) { - super.setText(text); - } - @Override public boolean isAllowPlaceholders() { return parent.isAllowPlaceholders(); } + @Override + public void setTouchHandler(TouchHandler touchHandler) { + v2Adapter.onNewTouchHandlerChange(getTouchHandler(), touchHandler); + super.setTouchHandler(touchHandler); + } + + @Override + public V2TextLineAdapter getV2Adapter() { + return v2Adapter; + } + } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APITouchableLine.java b/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APITouchableLine.java new file mode 100644 index 00000000..94b3ad42 --- /dev/null +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/APITouchableLine.java @@ -0,0 +1,12 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.object.api; + +import me.filoghost.holographicdisplays.api.line.TouchableLine; + +public interface APITouchableLine extends TouchableLine, APIHologramLine { + +} diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/DefaultVisibilityManager.java b/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/DefaultVisibilityManager.java index e64fe44c..f5e72a2b 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/DefaultVisibilityManager.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/object/api/DefaultVisibilityManager.java @@ -9,6 +9,7 @@ import me.filoghost.fcommons.Preconditions; import me.filoghost.holographicdisplays.api.VisibilityManager; import me.filoghost.holographicdisplays.bridge.protocollib.ProtocolLibHook; import me.filoghost.holographicdisplays.core.hologram.StandardHologram; +import me.filoghost.holographicdisplays.legacy.api.v2.V2VisibilityManagerAdapter; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -21,12 +22,14 @@ import java.util.concurrent.ConcurrentHashMap; public class DefaultVisibilityManager implements VisibilityManager { private final StandardHologram hologram; + private final V2VisibilityManagerAdapter v2Adapter; private Map playersVisibilityMap; private boolean visibleByDefault; public DefaultVisibilityManager(StandardHologram hologram) { Preconditions.notNull(hologram, "hologram"); this.hologram = hologram; + this.v2Adapter = new V2VisibilityManagerAdapter(this); this.visibleByDefault = true; } @@ -164,5 +167,9 @@ public class DefaultVisibilityManager implements VisibilityManager { public String toString() { return "VisibilityManager [playersMap=" + playersVisibilityMap + ", visibleByDefault=" + visibleByDefault + "]"; } - + + public V2VisibilityManagerAdapter getV2Adapter() { + return v2Adapter; + } + } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/object/base/BaseTextLine.java b/plugin/src/main/java/me/filoghost/holographicdisplays/object/base/BaseTextLine.java index bbd14b77..7e2bf1d3 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/object/base/BaseTextLine.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/object/base/BaseTextLine.java @@ -33,7 +33,7 @@ public abstract class BaseTextLine extends BaseTouchableLine implements Standard return text; } - protected void setText(String text) { + public void setText(String text) { this.text = text; if (textEntity != null) { diff --git a/plugin/src/test/java/me/filoghost/holographicdisplays/legacy/api/v2/V2TouchableLineAdapterTest.java b/plugin/src/test/java/me/filoghost/holographicdisplays/legacy/api/v2/V2TouchableLineAdapterTest.java new file mode 100644 index 00000000..275e922e --- /dev/null +++ b/plugin/src/test/java/me/filoghost/holographicdisplays/legacy/api/v2/V2TouchableLineAdapterTest.java @@ -0,0 +1,197 @@ +/* + * Copyright (C) filoghost and contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package me.filoghost.holographicdisplays.legacy.api.v2; + +import com.gmail.filoghost.holographicdisplays.api.line.TextLine; +import me.filoghost.holographicdisplays.api.handler.TouchHandler; +import me.filoghost.holographicdisplays.object.api.APIHologram; +import me.filoghost.holographicdisplays.object.api.APITextLine; +import org.bukkit.entity.Player; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.*; +import static org.mockito.Mockito.*; + +@SuppressWarnings("deprecation") +class V2TouchableLineAdapterTest { + + APIHologram hologram = mock(APIHologram.class); + + @Test + void setNullV2TouchHandler() { + APITextLine newLine = new APITextLine(hologram, ""); + TextLine v2Line = newLine.getV2Adapter(); + ExampleNewTouchHandler newTouchHandler = new ExampleNewTouchHandler(); + + newLine.setTouchHandler(newTouchHandler); + v2Line.setTouchHandler(null); + + assertThat(v2Line.getTouchHandler()).isNull(); + assertThat(newLine.getTouchHandler()).isNull(); + } + + @Test + void setNullNewTouchHandler() { + APITextLine newLine = new APITextLine(hologram, ""); + TextLine v2Line = newLine.getV2Adapter(); + ExampleV2TouchHandler v2TouchHandler = new ExampleV2TouchHandler(); + + v2Line.setTouchHandler(v2TouchHandler); + newLine.setTouchHandler(null); + + assertThat(v2Line.getTouchHandler()).isNull(); + assertThat(newLine.getTouchHandler()).isNull(); + } + + @Test + void setV2GetV2TouchHandler() { + TextLine v2Line = new APITextLine(hologram, "").getV2Adapter(); + ExampleV2TouchHandler v2TouchHandler = new ExampleV2TouchHandler(); + + v2Line.setTouchHandler(v2TouchHandler); + + assertThat(v2Line.getTouchHandler()).isSameAs(v2TouchHandler); + } + + @Test + void setNewGetV2TouchHandler() { + APITextLine newLine = new APITextLine(hologram, ""); + TextLine v2Line = newLine.getV2Adapter(); + ExampleNewTouchHandler newTouchHandler = new ExampleNewTouchHandler(); + + newLine.setTouchHandler(newTouchHandler); + + assertThat(v2Line.getTouchHandler()).isNotNull(); + } + + @Test + void setV2GetNewTouchHandler() { + APITextLine newLine = new APITextLine(hologram, ""); + TextLine v2Line = newLine.getV2Adapter(); + ExampleV2TouchHandler v2TouchHandler = new ExampleV2TouchHandler(); + + v2Line.setTouchHandler(v2TouchHandler); + + assertThat(newLine.getTouchHandler()).isNotNull(); + } + + @Test + void repeatedGetV2TouchHandlerShouldReturnSameInstance() { + APITextLine newLine = new APITextLine(hologram, ""); + TextLine v2Line = newLine.getV2Adapter(); + + newLine.setTouchHandler(new ExampleNewTouchHandler()); + assertThat(v2Line.getTouchHandler()).isSameAs(v2Line.getTouchHandler()); + + v2Line.setTouchHandler(new ExampleV2TouchHandler()); + assertThat(v2Line.getTouchHandler()).isSameAs(v2Line.getTouchHandler()); + } + + @Test + void repeatedSetV2TouchHandlerShouldNotChangeNewTouchHandlerInstance() { + APITextLine newLine = new APITextLine(hologram, ""); + TextLine v2Line = newLine.getV2Adapter(); + ExampleV2TouchHandler v2TouchHandler = new ExampleV2TouchHandler(); + + v2Line.setTouchHandler(v2TouchHandler); + TouchHandler newTouchHandlerA = newLine.getTouchHandler(); + v2Line.setTouchHandler(v2TouchHandler); + TouchHandler newTouchHandlerB = newLine.getTouchHandler(); + + assertThat(newTouchHandlerA).isSameAs(newTouchHandlerB); + } + + @Test + void repeatedSetNewTouchHandlerShouldNotChangeV2TouchHandlerInstance() { + APITextLine newLine = new APITextLine(hologram, ""); + TextLine v2Line = newLine.getV2Adapter(); + ExampleNewTouchHandler v2TouchHandler = new ExampleNewTouchHandler(); + + newLine.setTouchHandler(v2TouchHandler); + com.gmail.filoghost.holographicdisplays.api.handler.TouchHandler v2TouchHandlerA = v2Line.getTouchHandler(); + newLine.setTouchHandler(v2TouchHandler); + com.gmail.filoghost.holographicdisplays.api.handler.TouchHandler v2TouchHandlerB = v2Line.getTouchHandler(); + + assertThat(v2TouchHandlerA).isSameAs(v2TouchHandlerB); + } + + @Test + void v2TouchHandlerChangesWhenNewTouchHandlerChanges() { + APITextLine newLine = new APITextLine(hologram, ""); + TextLine v2Line = newLine.getV2Adapter(); + + newLine.setTouchHandler(new ExampleNewTouchHandler()); + com.gmail.filoghost.holographicdisplays.api.handler.TouchHandler v2TouchHandlerA = v2Line.getTouchHandler(); + newLine.setTouchHandler(new ExampleNewTouchHandler()); + com.gmail.filoghost.holographicdisplays.api.handler.TouchHandler v2TouchHandlerB = v2Line.getTouchHandler(); + + assertThat(v2TouchHandlerA).isNotSameAs(v2TouchHandlerB); + } + + @Test + void newTouchHandlerChangesWhenV2TouchHandlerChanges() { + APITextLine newLine = new APITextLine(hologram, ""); + TextLine v2Line = newLine.getV2Adapter(); + + v2Line.setTouchHandler(new ExampleV2TouchHandler()); + TouchHandler newTouchHandlerA = newLine.getTouchHandler(); + v2Line.setTouchHandler(new ExampleV2TouchHandler()); + TouchHandler newTouchHandlerB = newLine.getTouchHandler(); + + assertThat(newTouchHandlerA).isNotSameAs(newTouchHandlerB); + } + + @Test + void preserveTouchHandlerInstancesWhenUsingNewAPI() { + APITextLine newLine = new APITextLine(hologram, ""); + TextLine v2Line = newLine.getV2Adapter(); + ExampleV2TouchHandler v2TouchHandler = new ExampleV2TouchHandler(); + + v2Line.setTouchHandler(v2TouchHandler); + + TouchHandler newTouchHandler = newLine.getTouchHandler(); + v2Line.setTouchHandler(null); // This also clears the adapter + newLine.setTouchHandler(newTouchHandler); // Return to the initial value + + // Instances should be the same + assertThat(v2Line.getTouchHandler()).isSameAs(v2TouchHandler); + assertThat(newLine.getTouchHandler()).isSameAs(newTouchHandler); + } + + @Test + void preserveTouchHandlerInstancesWhenUsingV2API() { + APITextLine newLine = new APITextLine(hologram, ""); + TextLine v2Line = newLine.getV2Adapter(); + ExampleNewTouchHandler newTouchHandler = new ExampleNewTouchHandler(); + + newLine.setTouchHandler(newTouchHandler); + + com.gmail.filoghost.holographicdisplays.api.handler.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(newLine.getTouchHandler()).isSameAs(newTouchHandler); + } + + + private static class ExampleNewTouchHandler implements TouchHandler { + + @Override + public void onTouch(Player player) {} + + } + + + private static class ExampleV2TouchHandler implements com.gmail.filoghost.holographicdisplays.api.handler.TouchHandler { + + @Override + public void onTouch(Player player) {} + + } + +}