Remove the v1 API and start throwing exceptions

This commit is contained in:
filoghost 2021-05-10 22:48:24 +02:00
parent dfb6e0ab33
commit 1906cadf96
9 changed files with 26 additions and 618 deletions

View File

@ -5,64 +5,79 @@
*/
package com.gmail.filoghost.holograms.api;
import com.gmail.filoghost.holograms.api.internal.NewAPIAdapter;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* @deprecated Please use the new API!
*/
@Deprecated
public class HolographicDisplaysAPI {
private static final Set<String> notifiedPlugins = new HashSet<>();
@Deprecated
public static Hologram createHologram(Plugin plugin, Location source, String... lines) {
return NewAPIAdapter.createHologram(plugin, source, lines);
throw removedAPIException(plugin);
}
@Deprecated
public static FloatingItem createFloatingItem(Plugin plugin, Location source, ItemStack itemstack) {
return NewAPIAdapter.createFloatingItem(plugin, source, itemstack);
throw removedAPIException(plugin);
}
@Deprecated
public static Hologram createIndividualHologram(Plugin plugin, Location source, Player whoCanSee, String... lines) {
return NewAPIAdapter.createIndividualHologram(plugin, source, whoCanSee, lines);
throw removedAPIException(plugin);
}
@Deprecated
public static Hologram createIndividualHologram(Plugin plugin, Location source, List<Player> whoCanSee, String... lines) {
return NewAPIAdapter.createIndividualHologram(plugin, source, whoCanSee, lines);
throw removedAPIException(plugin);
}
@Deprecated
public static FloatingItem createIndividualFloatingItem(Plugin plugin, Location source, Player whoCanSee, ItemStack itemstack) {
return NewAPIAdapter.createIndividualFloatingItem(plugin, source, whoCanSee, itemstack);
throw removedAPIException(plugin);
}
@Deprecated
public static FloatingItem createIndividualFloatingItem(Plugin plugin, Location source, List<Player> whoCanSee, ItemStack itemstack) {
return NewAPIAdapter.createIndividualFloatingItem(plugin, source, whoCanSee, itemstack);
throw removedAPIException(plugin);
}
@Deprecated
public static Hologram[] getHolograms(Plugin plugin) {
return NewAPIAdapter.getHolograms(plugin);
throw removedAPIException(plugin);
}
@Deprecated
public static FloatingItem[] getFloatingItems(Plugin plugin) {
return NewAPIAdapter.getFloatingItems(plugin);
throw removedAPIException(plugin);
}
private static RuntimeException removedAPIException(Plugin plugin) {
if (plugin != null && notifiedPlugins.add(plugin.getName())) {
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[Holographic Displays] The plugin \""
+ plugin.getName() + "\" is still using the API v1 of Holographic Displays,"
+ "which has been removed. Please notify its author.");
}
return new IllegalStateException("The legacy Holographic Displays API v1 has been removed");
}
@Deprecated
public static boolean isHologramEntity(Entity bukkitEntity) {
return NewAPIAdapter.isHologramEntity(bukkitEntity);
return me.filoghost.holographicdisplays.api.HolographicDisplaysAPI.isHologramEntity(bukkitEntity);
}
}

View File

@ -1,45 +0,0 @@
/*
* Copyright (C) filoghost and contributors
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package com.gmail.filoghost.holograms.api.internal;
import org.bukkit.plugin.Plugin;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
class ActiveObjectsRegistry {
private static final Map<String, List<HologramAdapter>> hologramsByPlugin = new HashMap<>();
private static final Map<String, List<FloatingItemAdapter>> floatingItemsByPlugin = new HashMap<>();
static void addHologram(HologramAdapter hologram) {
hologramsByPlugin.computeIfAbsent(hologram.getOwnerPluginName(), key -> new ArrayList<>()).add(hologram);
}
static void removeHologram(HologramAdapter hologram) {
hologramsByPlugin.get(hologram.getOwnerPluginName()).remove(hologram);
}
static List<HologramAdapter> getHolograms(Plugin plugin) {
return hologramsByPlugin.getOrDefault(plugin.getName(), Collections.emptyList());
}
static void addFloatingItem(FloatingItemAdapter floatingItem) {
floatingItemsByPlugin.computeIfAbsent(floatingItem.getOwnerPluginName(), key -> new ArrayList<>()).add(floatingItem);
}
static void removeFloatingItem(FloatingItemAdapter floatingItem) {
floatingItemsByPlugin.get(floatingItem.getOwnerPluginName()).remove(floatingItem);
}
static List<FloatingItemAdapter> getFloatingItems(Plugin plugin) {
return floatingItemsByPlugin.getOrDefault(plugin.getName(), Collections.emptyList());
}
}

View File

@ -1,37 +0,0 @@
/*
* Copyright (C) filoghost and contributors
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package com.gmail.filoghost.holograms.api.internal;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import java.util.List;
class BaseHologramAdapter {
private final String ownerPluginName;
protected final me.filoghost.holographicdisplays.api.Hologram hologram;
BaseHologramAdapter(Plugin plugin, Location source) {
this.ownerPluginName = plugin.getName();
this.hologram = me.filoghost.holographicdisplays.api.HolographicDisplaysAPI.get(plugin).createHologram(source);
}
protected void restrictVisibityTo(List<Player> whoCanSee) {
hologram.getVisibilityManager().setVisibleByDefault(false);
if (whoCanSee != null) {
for (Player player : whoCanSee) {
hologram.getVisibilityManager().showTo(player);
}
}
}
String getOwnerPluginName() {
return ownerPluginName;
}
}

View File

@ -1,150 +0,0 @@
/*
* Copyright (C) filoghost and contributors
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package com.gmail.filoghost.holograms.api.internal;
import com.gmail.filoghost.holograms.api.FloatingItem;
import com.gmail.filoghost.holograms.api.ItemTouchHandler;
import com.gmail.filoghost.holograms.api.PickupHandler;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import java.util.List;
@SuppressWarnings("deprecation")
class FloatingItemAdapter extends BaseHologramAdapter implements FloatingItem {
private final me.filoghost.holographicdisplays.api.line.ItemLine itemLine;
private ItemTouchHandler touchHandler;
private PickupHandler pickupHandler;
FloatingItemAdapter(Plugin plugin, Location source, ItemStack itemstack) {
super(plugin, source);
this.itemLine = hologram.appendItemLine(itemstack);
ActiveObjectsRegistry.addFloatingItem(this);
}
FloatingItemAdapter(Plugin plugin, Location source, ItemStack itemstack, List<Player> whoCanSee) {
super(plugin, source);
restrictVisibityTo(whoCanSee);
this.itemLine = hologram.appendItemLine(itemstack);
ActiveObjectsRegistry.addFloatingItem(this);
}
@Override
public void delete() {
hologram.delete();
ActiveObjectsRegistry.removeFloatingItem(this);
}
@Override
public boolean update() {
return true;
}
@Override
public void hide() {
}
@Override
public void setItemStack(ItemStack itemstack) {
itemLine.setItemStack(itemstack);
}
@Override
public ItemStack getItemStack() {
return itemLine.getItemStack();
}
@Override
public Location getLocation() {
return hologram.getLocation();
}
@Override
public double getX() {
return hologram.getX();
}
@Override
public double getY() {
return hologram.getY();
}
@Override
public double getZ() {
return hologram.getZ();
}
@Override
public World getWorld() {
return hologram.getWorld();
}
@Override
public void teleport(Location location) {
hologram.teleport(location);
}
@Override
public void setTouchHandler(ItemTouchHandler handler) {
this.touchHandler = handler;
if (handler != null) {
itemLine.setTouchHandler(new ItemTouchHandlerAdapter(this, handler));
} else {
itemLine.setTouchHandler(null);
}
}
@Override
public ItemTouchHandler getTouchHandler() {
return touchHandler;
}
@Override
public boolean hasTouchHandler() {
return touchHandler != null;
}
@Override
public void setPickupHandler(PickupHandler handler) {
this.pickupHandler = handler;
if (handler != null) {
itemLine.setPickupHandler(new PickupHandlerAdapter(this, handler));
} else {
itemLine.setPickupHandler(null);
}
}
@Override
public PickupHandler getPickupHandler() {
return pickupHandler;
}
@Override
public boolean hasPickupHandler() {
return pickupHandler != null;
}
@Override
public long getCreationTimestamp() {
return hologram.getCreationTimestamp();
}
@Override
public boolean isDeleted() {
return hologram.isDeleted();
}
}

View File

@ -1,179 +0,0 @@
/*
* Copyright (C) filoghost and contributors
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package com.gmail.filoghost.holograms.api.internal;
import com.gmail.filoghost.holograms.api.Hologram;
import com.gmail.filoghost.holograms.api.TouchHandler;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import java.util.List;
@SuppressWarnings("deprecation")
class HologramAdapter extends BaseHologramAdapter implements Hologram {
private TouchHandler touchHandler;
public HologramAdapter(Plugin plugin, Location source, String[] lines) {
super(plugin, source);
for (String line : lines) {
hologram.appendTextLine(line);
}
ActiveObjectsRegistry.addHologram(this);
}
public HologramAdapter(Plugin plugin, Location source, String[] lines, List<Player> whoCanSee) {
super(plugin, source);
restrictVisibityTo(whoCanSee);
for (String line : lines) {
hologram.appendTextLine(line);
}
ActiveObjectsRegistry.addHologram(this);
}
@Override
public void delete() {
hologram.delete();
ActiveObjectsRegistry.removeHologram(this);
}
@Override
public boolean update() {
return true;
}
@Override
public void hide() {
}
@Override
public void addLine(String text) {
hologram.appendTextLine(text);
updateTouchHandler();
}
@Override
public void setLine(int index, String text) {
hologram.removeLine(index);
hologram.insertTextLine(index, text);
updateTouchHandler();
}
@Override
public void insertLine(int index, String text) {
hologram.insertTextLine(index, text);
updateTouchHandler();
}
@Override
public String[] getLines() {
String[] lines = new String[hologram.size()];
for (int i = 0; i < lines.length; i++) {
lines[i] = ((me.filoghost.holographicdisplays.api.line.TextLine) hologram.getLine(i)).getText();
}
return lines;
}
@Override
public int getLinesLength() {
return hologram.size();
}
@Override
public void setLocation(Location location) {
hologram.teleport(location);
}
@Override
public void setTouchHandler(TouchHandler handler) {
this.touchHandler = handler;
updateTouchHandler();
}
private void updateTouchHandler() {
for (int i = 0; i < hologram.size(); i++) {
me.filoghost.holographicdisplays.api.line.HologramLine line = hologram.getLine(i);
if (!(line instanceof me.filoghost.holographicdisplays.api.line.TouchableLine)) {
continue;
}
me.filoghost.holographicdisplays.api.line.TouchableLine touchableLine = (me.filoghost.holographicdisplays.api.line.TouchableLine) line;
if (touchHandler != null) {
touchableLine.setTouchHandler(new HologramTouchHandlerAdapter(this, touchHandler));
} else {
touchableLine.setTouchHandler(null);
}
}
}
@Override
public TouchHandler getTouchHandler() {
return touchHandler;
}
@Override
public boolean hasTouchHandler() {
return touchHandler != null;
}
@Override
public void removeLine(int index) {
hologram.removeLine(index);
}
@Override
public void clearLines() {
hologram.clearLines();
}
@Override
public Location getLocation() {
return hologram.getLocation();
}
@Override
public double getX() {
return hologram.getX();
}
@Override
public double getY() {
return hologram.getY();
}
@Override
public double getZ() {
return hologram.getZ();
}
@Override
public World getWorld() {
return hologram.getWorld();
}
@Override
public void teleport(Location location) {
hologram.teleport(location);
}
@Override
public long getCreationTimestamp() {
return hologram.getCreationTimestamp();
}
@Override
public boolean isDeleted() {
return hologram.isDeleted();
}
}

View File

@ -1,28 +0,0 @@
/*
* Copyright (C) filoghost and contributors
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package com.gmail.filoghost.holograms.api.internal;
import com.gmail.filoghost.holograms.api.Hologram;
import com.gmail.filoghost.holograms.api.TouchHandler;
import org.bukkit.entity.Player;
@SuppressWarnings("deprecation")
class HologramTouchHandlerAdapter implements me.filoghost.holographicdisplays.api.handler.TouchHandler {
private final TouchHandler oldHandler;
private final Hologram hologram;
HologramTouchHandlerAdapter(Hologram hologram, TouchHandler oldHandler) {
this.hologram = hologram;
this.oldHandler = oldHandler;
}
@Override
public void onTouch(Player player) {
oldHandler.onTouch(hologram, player);
}
}

View File

@ -1,28 +0,0 @@
/*
* Copyright (C) filoghost and contributors
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package com.gmail.filoghost.holograms.api.internal;
import com.gmail.filoghost.holograms.api.FloatingItem;
import com.gmail.filoghost.holograms.api.ItemTouchHandler;
import org.bukkit.entity.Player;
@SuppressWarnings("deprecation")
class ItemTouchHandlerAdapter implements me.filoghost.holographicdisplays.api.handler.TouchHandler {
private final ItemTouchHandler oldHandler;
private final FloatingItem item;
ItemTouchHandlerAdapter(FloatingItem item, ItemTouchHandler oldHandler) {
this.item = item;
this.oldHandler = oldHandler;
}
@Override
public void onTouch(Player player) {
oldHandler.onTouch(item, player);
}
}

View File

@ -1,112 +0,0 @@
/*
* Copyright (C) filoghost and contributors
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package com.gmail.filoghost.holograms.api.internal;
import com.gmail.filoghost.holograms.api.FloatingItem;
import com.gmail.filoghost.holograms.api.Hologram;
import me.filoghost.fcommons.Preconditions;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@SuppressWarnings("deprecation")
public class NewAPIAdapter {
private static final Set<String> notifiedPlugins = new HashSet<>();
public static Hologram createHologram(Plugin plugin, Location source, String... lines) {
validatePluginAndNotify(plugin);
validateSource(source);
validateLines(lines);
return new HologramAdapter(plugin, source, lines);
}
public static FloatingItem createFloatingItem(Plugin plugin, Location source, ItemStack itemstack) {
validatePluginAndNotify(plugin);
validateSource(source);
validateItem(itemstack);
return new FloatingItemAdapter(plugin, source, itemstack);
}
public static Hologram createIndividualHologram(Plugin plugin, Location source, Player whoCanSee, String... lines) {
return createIndividualHologram(plugin, source, Collections.singletonList(whoCanSee), lines);
}
public static Hologram createIndividualHologram(Plugin plugin, Location source, List<Player> whoCanSee, String... lines) {
validatePluginAndNotify(plugin);
validateSource(source);
validateLines(lines);
return new HologramAdapter(plugin, source, lines, whoCanSee);
}
public static FloatingItem createIndividualFloatingItem(Plugin plugin, Location source, Player whoCanSee, ItemStack itemstack) {
return createIndividualFloatingItem(plugin, source, Collections.singletonList(whoCanSee), itemstack);
}
public static FloatingItem createIndividualFloatingItem(Plugin plugin, Location source, List<Player> whoCanSee, ItemStack itemstack) {
validatePluginAndNotify(plugin);
validateSource(source);
validateItem(itemstack);
return new FloatingItemAdapter(plugin, source, itemstack, whoCanSee);
}
public static Hologram[] getHolograms(Plugin plugin) {
validatePluginAndNotify(plugin);
List<HologramAdapter> pluginHolograms = ActiveObjectsRegistry.getHolograms(plugin);
return pluginHolograms.toArray(new HologramAdapter[0]);
}
public static FloatingItem[] getFloatingItems(Plugin plugin) {
validatePluginAndNotify(plugin);
List<FloatingItemAdapter> pluginFloatingItems = ActiveObjectsRegistry.getFloatingItems(plugin);
return pluginFloatingItems.toArray(new FloatingItemAdapter[0]);
}
public static boolean isHologramEntity(Entity bukkitEntity) {
return me.filoghost.holographicdisplays.api.HolographicDisplaysAPI.isHologramEntity(bukkitEntity);
}
private static void validatePluginAndNotify(Plugin plugin) {
Preconditions.notNull(plugin, "plugin");
if (notifiedPlugins.add(plugin.getName())) {
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[Holographic Displays] The plugin \""
+ plugin.getName() + "\" is still using the old v1 API of Holographic Displays."
+ " Please notify its author, as it's not guaranteed to work in the future.");
}
}
private static void validateSource(Location source) {
Preconditions.notNull(source, "source");
Preconditions.notNull(source.getWorld(), "source's world");
}
private static void validateLines(String[] lines) {
Preconditions.notNull(lines, "lines");
}
private static void validateItem(ItemStack itemstack) {
Preconditions.notNull(itemstack, "itemstack");
Preconditions.checkArgument(itemstack.getType() != Material.AIR, "itemstack cannot be AIR");
}
}

View File

@ -1,28 +0,0 @@
/*
* Copyright (C) filoghost and contributors
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package com.gmail.filoghost.holograms.api.internal;
import com.gmail.filoghost.holograms.api.FloatingItem;
import com.gmail.filoghost.holograms.api.PickupHandler;
import org.bukkit.entity.Player;
@SuppressWarnings("deprecation")
class PickupHandlerAdapter implements me.filoghost.holographicdisplays.api.handler.PickupHandler {
private final PickupHandler oldHandler;
private final FloatingItem item;
PickupHandlerAdapter(FloatingItem item, PickupHandler oldHandler) {
this.item = item;
this.oldHandler = oldHandler;
}
@Override
public void onPickup(Player player) {
oldHandler.onPickup(item, player);
}
}