Move old API to separate Maven module, fix some methods

This commit is contained in:
filoghost 2020-04-19 17:15:46 +02:00
parent 60e024f4db
commit 2d24b4edac
15 changed files with 354 additions and 164 deletions

43
Legacy/pom.xml Normal file
View File

@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.gmail.filoghost.holographicdisplays</groupId>
<artifactId>holographicdisplays-parent</artifactId>
<version>2.4.2-SNAPSHOT</version>
</parent>
<artifactId>holographicdisplays-legacy</artifactId>
<name>HolographicDisplays Legacy</name>
<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>holographicdisplays-api</artifactId>
<version>2.4.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>holographicdisplays-utils</artifactId>
<version>2.4.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>${spigot-api.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@ -14,7 +14,9 @@
*/
package com.gmail.filoghost.holograms.api;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@ -28,12 +30,10 @@ import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import com.gmail.filoghost.holograms.api.replacements.FakeFloatingItem;
import com.gmail.filoghost.holograms.api.adapter.FloatingItemAdapter;
import com.gmail.filoghost.holograms.api.adapter.HologramAdapter;
import com.gmail.filoghost.holographicdisplays.api.HologramsAPI;
import com.gmail.filoghost.holographicdisplays.api.internal.BackendAPI;
import com.gmail.filoghost.holographicdisplays.object.CraftHologram;
import com.gmail.filoghost.holographicdisplays.object.PluginHologram;
import com.gmail.filoghost.holographicdisplays.object.PluginHologramManager;
import com.gmail.filoghost.holographicdisplays.api.line.ItemLine;
import com.gmail.filoghost.holographicdisplays.util.Validator;
/**
@ -45,6 +45,8 @@ public class HolographicDisplaysAPI {
private static Set<String> notifiedPlugins = new HashSet<>();
private static void notifyOldAPI(Plugin plugin) {
Validator.notNull(plugin, "plugin");
if (notifiedPlugins.add(plugin.getName())) {
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[Holographic Displays] The plugin \"" + plugin.getName() + "\" is still using the old API of Holographic Displays. "
+ "Please notify the author and ask them to update it, the old API will be removed soon.");
@ -54,40 +56,42 @@ public class HolographicDisplaysAPI {
@Deprecated
public static Hologram createHologram(Plugin plugin, Location source, String... lines) {
notifyOldAPI(plugin);
CraftHologram hologram = (CraftHologram) BackendAPI.getImplementation().createHologram(plugin, source);
validateLocation(source);
com.gmail.filoghost.holographicdisplays.api.Hologram hologram = HologramsAPI.createHologram(plugin, source);
for (String line : lines) {
hologram.appendTextLine(line);
}
return hologram;
return new HologramAdapter(plugin, hologram);
}
@Deprecated
public static FloatingItem createFloatingItem(Plugin plugin, Location source, ItemStack itemstack) {
notifyOldAPI(plugin);
Validator.notNull(itemstack, "itemstack");
Validator.isTrue(itemstack.getType() != Material.AIR, "itemstack cannot be AIR");
CraftHologram hologram = (CraftHologram) BackendAPI.getImplementation().createHologram(plugin, source);
hologram.appendItemLine(itemstack);
return new FakeFloatingItem(hologram, itemstack);
validateLocation(source);
validateItem(itemstack);
com.gmail.filoghost.holographicdisplays.api.Hologram hologram = HologramsAPI.createHologram(plugin, source);
ItemLine itemLine = hologram.appendItemLine(itemstack);
return new FloatingItemAdapter(plugin, hologram, itemLine);
}
@Deprecated
public static Hologram createIndividualHologram(Plugin plugin, Location source, Player whoCanSee, String... lines) {
notifyOldAPI(plugin);
List<Player> whoCanSeeList = new ArrayList<>();
whoCanSeeList.add(whoCanSee);
return createIndividualHologram(plugin, source, whoCanSeeList, lines);
return createIndividualHologram(plugin, source, Arrays.asList(whoCanSee), lines);
}
@Deprecated
public static Hologram createIndividualHologram(Plugin plugin, Location source, List<Player> whoCanSee, String... lines) {
notifyOldAPI(plugin);
Validator.notNull(plugin, "plugin");
Validator.notNull(source, "source");
Validator.notNull(source.getWorld(), "source's world");
CraftHologram hologram = (CraftHologram) BackendAPI.getImplementation().createHologram(plugin, source);
validateLocation(source);
com.gmail.filoghost.holographicdisplays.api.Hologram hologram = HologramsAPI.createHologram(plugin, source);
hologram.getVisibilityManager().setVisibleByDefault(false);
if (whoCanSee != null) {
@ -100,28 +104,25 @@ public class HolographicDisplaysAPI {
hologram.appendTextLine(line);
}
return hologram;
return new HologramAdapter(plugin, hologram);
}
@Deprecated
public static FloatingItem createIndividualFloatingItem(Plugin plugin, Location source, Player whoCanSee, ItemStack itemstack) {
notifyOldAPI(plugin);
List<Player> whoCanSeeList = new ArrayList<>();
whoCanSeeList.add(whoCanSee);
return createIndividualFloatingItem(plugin, source, whoCanSeeList, itemstack);
return createIndividualFloatingItem(plugin, source, Arrays.asList(whoCanSee), itemstack);
}
@Deprecated
public static FloatingItem createIndividualFloatingItem(Plugin plugin, Location source, List<Player> whoCanSee, ItemStack itemstack) {
notifyOldAPI(plugin);
Validator.notNull(plugin, "plugin cannot be null");
Validator.notNull(source, "source cannot be null");
Validator.notNull(source.getWorld(), "source's world cannot be null");
Validator.notNull(itemstack, "itemstack cannot be null");
Validator.isTrue(itemstack.getType() != Material.AIR, "itemstack cannot be AIR");
CraftHologram hologram = (CraftHologram) BackendAPI.getImplementation().createHologram(plugin, source);
hologram.appendItemLine(itemstack);
validateLocation(source);
validateItem(itemstack);
com.gmail.filoghost.holographicdisplays.api.Hologram hologram = HologramsAPI.createHologram(plugin, source);
ItemLine itemLine = hologram.appendItemLine(itemstack);
hologram.getVisibilityManager().setVisibleByDefault(false);
if (whoCanSee != null) {
@ -130,34 +131,38 @@ public class HolographicDisplaysAPI {
}
}
return new FakeFloatingItem(hologram, itemstack);
return new FloatingItemAdapter(plugin, hologram, itemLine);
}
@Deprecated
public static Hologram[] getHolograms(Plugin plugin) {
notifyOldAPI(plugin);
Validator.notNull(plugin, "plugin cannot be null");
List<Hologram> pluginHolograms = new ArrayList<>();;
for (PluginHologram pluginHologram : PluginHologramManager.getHolograms()) {
if (pluginHologram.getOwner().equals(plugin)) {
pluginHolograms.add(pluginHologram);
}
}
return pluginHolograms.toArray(new Hologram[0]);
Collection<HologramAdapter> pluginHolograms = HologramAdapter.activeHolograms.getOrDefault(plugin, Collections.emptyList());
return pluginHolograms.toArray(new HologramAdapter[0]);
}
@Deprecated
public static FloatingItem[] getFloatingItems(Plugin plugin) {
notifyOldAPI(plugin);
Validator.notNull(plugin, "plugin cannot be null");
return new FloatingItem[0];
Collection<FloatingItemAdapter> pluginFloatingItems = FloatingItemAdapter.activeFloatingItems.getOrDefault(plugin, Collections.emptyList());
return pluginFloatingItems.toArray(new FloatingItemAdapter[0]);
}
@Deprecated
public static boolean isHologramEntity(Entity bukkitEntity) {
return HologramsAPI.isHologramEntity(bukkitEntity);
}
private static void validateLocation(Location loc) {
Validator.notNull(loc, "location");
Validator.notNull(loc.getWorld(), "location's world");
}
private static void validateItem(ItemStack itemstack) {
Validator.notNull(itemstack, "itemstack");
Validator.isTrue(itemstack.getType() != Material.AIR, "itemstack cannot be AIR");
}
}

View File

@ -12,30 +12,41 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.gmail.filoghost.holograms.api.replacements;
package com.gmail.filoghost.holograms.api.adapter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import com.gmail.filoghost.holograms.api.FloatingItem;
import com.gmail.filoghost.holograms.api.ItemTouchHandler;
import com.gmail.filoghost.holograms.api.PickupHandler;
import com.gmail.filoghost.holographicdisplays.object.CraftHologram;
import com.gmail.filoghost.holographicdisplays.object.line.CraftItemLine;
import com.gmail.filoghost.holographicdisplays.api.Hologram;
import com.gmail.filoghost.holographicdisplays.api.line.ItemLine;
/**
* Do not use this class!
*/
@SuppressWarnings("deprecation")
public class FakeFloatingItem implements FloatingItem {
public class FloatingItemAdapter implements FloatingItem {
public CraftHologram hologram;
private CraftItemLine mainLine;
public static Map<Plugin, Collection<FloatingItemAdapter>> activeFloatingItems = new HashMap<>();
public FakeFloatingItem(CraftHologram hologram, ItemStack item) {
this.hologram = hologram;
mainLine = hologram.appendItemLine(item);
private Plugin plugin;
public Hologram hologram;
private ItemLine itemLine;
private ItemTouchHandler touchHandler;
private PickupHandler pickupHandler;
public FloatingItemAdapter(Plugin plugin, Hologram delegateHologram, ItemLine delegateItemLine) {
this.plugin = plugin;
this.hologram = delegateHologram;
this.itemLine = delegateItemLine;
activeFloatingItems.computeIfAbsent(plugin, __ -> new ArrayList<>()).add(this);
}
@Override
@ -50,12 +61,12 @@ public class FakeFloatingItem implements FloatingItem {
@Override
public void setItemStack(ItemStack itemstack) {
mainLine.setItemStack(itemstack);
itemLine.setItemStack(itemstack);
}
@Override
public ItemStack getItemStack() {
return mainLine.getItemStack();
return itemLine.getItemStack();
}
@Override
@ -90,40 +101,44 @@ public class FakeFloatingItem implements FloatingItem {
@Override
public void setTouchHandler(ItemTouchHandler handler) {
this.touchHandler = handler;
if (handler != null) {
mainLine.setTouchHandler(new OldItemTouchHandlerWrapper(this, handler));
itemLine.setTouchHandler(new ItemTouchHandlerAdapter(this, handler));
} else {
mainLine.setTouchHandler(null);
itemLine.setTouchHandler(null);
}
}
@Override
public ItemTouchHandler getTouchHandler() {
return ((OldItemTouchHandlerWrapper) mainLine.getTouchHandler()).oldHandler;
return touchHandler;
}
@Override
public boolean hasTouchHandler() {
return mainLine.getTouchHandler() != null;
return touchHandler != null;
}
@Override
public void setPickupHandler(PickupHandler handler) {
this.pickupHandler = handler;
if (handler != null) {
mainLine.setPickupHandler(new OldPickupHandlerWrapper(this, handler));
itemLine.setPickupHandler(new PickupHandlerAdapter(this, handler));
} else {
mainLine.setPickupHandler(null);
itemLine.setPickupHandler(null);
}
}
@Override
public PickupHandler getPickupHandler() {
return ((OldPickupHandlerWrapper) mainLine.getPickupHandler()).oldHandler;
return pickupHandler;
}
@Override
public boolean hasPickupHandler() {
return mainLine.getPickupHandler() != null;
return pickupHandler != null;
}
@Override
@ -134,6 +149,8 @@ public class FakeFloatingItem implements FloatingItem {
@Override
public void delete() {
hologram.delete();
activeFloatingItems.get(plugin).remove(this);
}
@Override

View File

@ -0,0 +1,202 @@
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.gmail.filoghost.holograms.api.adapter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.plugin.Plugin;
import com.gmail.filoghost.holograms.api.Hologram;
import com.gmail.filoghost.holograms.api.TouchHandler;
import com.gmail.filoghost.holographicdisplays.api.line.HologramLine;
import com.gmail.filoghost.holographicdisplays.api.line.TextLine;
import com.gmail.filoghost.holographicdisplays.api.line.TouchableLine;
@SuppressWarnings("deprecation")
public class HologramAdapter implements Hologram {
public static Map<Plugin, Collection<HologramAdapter>> activeHolograms = new HashMap<>();
private Plugin plugin;
private com.gmail.filoghost.holographicdisplays.api.Hologram hologram;
private TouchHandler touchHandler;
public HologramAdapter(Plugin plugin, com.gmail.filoghost.holographicdisplays.api.Hologram delegate) {
this.plugin = plugin;
this.hologram = delegate;
activeHolograms.computeIfAbsent(plugin, __ -> new ArrayList<>()).add(this);
}
@Override
@Deprecated
public boolean update() {
return true;
}
@Override
@Deprecated
public void hide() {
}
@Override
@Deprecated
public void addLine(String text) {
hologram.appendTextLine(text);
updateTouchHandler();
}
@Override
@Deprecated
public void setLine(int index, String text) {
hologram.removeLine(index);
hologram.insertTextLine(index, text);
updateTouchHandler();
}
@Override
@Deprecated
public void insertLine(int index, String text) {
hologram.insertTextLine(index, text);
updateTouchHandler();
}
@Override
@Deprecated
public String[] getLines() {
String[] lines = new String[hologram.size()];
for (int i = 0; i < lines.length; i++) {
HologramLine lineObject = hologram.getLine(i);
if (lineObject instanceof TextLine) {
lines[i] = ((TextLine) lineObject).getText();
} else {
lines[i] = "";
}
}
return lines;
}
@Override
@Deprecated
public int getLinesLength() {
return hologram.size();
}
@Override
@Deprecated
public void setLocation(Location location) {
hologram.teleport(location);
}
@Override
@Deprecated
public void setTouchHandler(TouchHandler handler) {
this.touchHandler = handler;
updateTouchHandler();
}
private void updateTouchHandler() {
for (int i = 0; i < hologram.size(); i++) {
HologramLine line = hologram.getLine(i);
if (!(line instanceof TouchableLine)) {
continue;
}
TouchableLine touchableLine = (TouchableLine) line;
if (touchHandler != null) {
touchableLine.setTouchHandler(new HologramTouchHandlerAdapter(this, touchHandler));
} else {
touchableLine.setTouchHandler(null);
}
}
}
@Override
@Deprecated
public TouchHandler getTouchHandler() {
return touchHandler;
}
@Override
@Deprecated
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 void delete() {
hologram.delete();
activeHolograms.get(plugin).remove(this);
}
@Override
public boolean isDeleted() {
return hologram.isDeleted();
}
}

View File

@ -12,20 +12,20 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.gmail.filoghost.holograms.api.replacements;
package com.gmail.filoghost.holograms.api.adapter;
import org.bukkit.entity.Player;
import com.gmail.filoghost.holograms.api.Hologram;
import com.gmail.filoghost.holograms.api.TouchHandler;
import com.gmail.filoghost.holographicdisplays.object.CraftHologram;
@SuppressWarnings("deprecation")
public class OldTouchHandlerWrapper implements com.gmail.filoghost.holographicdisplays.api.handler.TouchHandler {
public class HologramTouchHandlerAdapter implements com.gmail.filoghost.holographicdisplays.api.handler.TouchHandler {
public TouchHandler oldHandler;
private CraftHologram hologram;
protected TouchHandler oldHandler;
private Hologram hologram;
public OldTouchHandlerWrapper(CraftHologram hologram, TouchHandler oldHandler) {
public HologramTouchHandlerAdapter(Hologram hologram, TouchHandler oldHandler) {
this.hologram = hologram;
this.oldHandler = oldHandler;
}

View File

@ -12,7 +12,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.gmail.filoghost.holograms.api.replacements;
package com.gmail.filoghost.holograms.api.adapter;
import org.bukkit.entity.Player;
@ -20,12 +20,12 @@ import com.gmail.filoghost.holograms.api.FloatingItem;
import com.gmail.filoghost.holograms.api.ItemTouchHandler;
@SuppressWarnings("deprecation")
public class OldItemTouchHandlerWrapper implements com.gmail.filoghost.holographicdisplays.api.handler.TouchHandler {
public class ItemTouchHandlerAdapter implements com.gmail.filoghost.holographicdisplays.api.handler.TouchHandler {
public ItemTouchHandler oldHandler;
protected ItemTouchHandler oldHandler;
private FloatingItem item;
public OldItemTouchHandlerWrapper(FloatingItem item, ItemTouchHandler oldHandler) {
public ItemTouchHandlerAdapter(FloatingItem item, ItemTouchHandler oldHandler) {
this.item = item;
this.oldHandler = oldHandler;
}

View File

@ -12,7 +12,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.gmail.filoghost.holograms.api.replacements;
package com.gmail.filoghost.holograms.api.adapter;
import org.bukkit.entity.Player;
@ -20,12 +20,12 @@ import com.gmail.filoghost.holograms.api.FloatingItem;
import com.gmail.filoghost.holograms.api.PickupHandler;
@SuppressWarnings("deprecation")
public class OldPickupHandlerWrapper implements com.gmail.filoghost.holographicdisplays.api.handler.PickupHandler {
public class PickupHandlerAdapter implements com.gmail.filoghost.holographicdisplays.api.handler.PickupHandler {
public PickupHandler oldHandler;
private FloatingItem item;
public OldPickupHandlerWrapper(FloatingItem item, PickupHandler oldPickupHandler) {
public PickupHandlerAdapter(FloatingItem item, PickupHandler oldPickupHandler) {
this.item = item;
this.oldHandler = oldPickupHandler;
}

View File

@ -41,6 +41,12 @@
<artifactId>holographicdisplays-utils</artifactId>
<version>2.4.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>holographicdisplays-legacy</artifactId>
<version>2.4.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>

View File

@ -22,10 +22,7 @@ import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.inventory.ItemStack;
import com.gmail.filoghost.holograms.api.TouchHandler;
import com.gmail.filoghost.holograms.api.replacements.OldTouchHandlerWrapper;
import com.gmail.filoghost.holographicdisplays.api.Hologram;
import com.gmail.filoghost.holographicdisplays.api.line.TouchableLine;
import com.gmail.filoghost.holographicdisplays.disk.Configuration;
import com.gmail.filoghost.holographicdisplays.object.line.CraftHologramLine;
import com.gmail.filoghost.holographicdisplays.object.line.CraftItemLine;
@ -36,10 +33,8 @@ import com.gmail.filoghost.holographicdisplays.util.Validator;
/**
* This class is only used by the plugin itself. Do not attempt to use it.
* It still implements the old API, but it's temporary.
*/
@SuppressWarnings("deprecation")
public class CraftHologram implements Hologram, com.gmail.filoghost.holograms.api.Hologram {
public class CraftHologram implements Hologram {
// Position variables.
private World world;
@ -356,85 +351,6 @@ public class CraftHologram implements Hologram, com.gmail.filoghost.holograms.ap
public String toString() {
return "CraftHologram [world=" + world + ", x=" + x + ", y=" + y + ", z=" + z + ", lines=" + lines + ", deleted=" + deleted + "]";
}
/**
* Old API methods, will be removed soon
*/
@Override
@Deprecated
public boolean update() {
return true;
}
@Override
@Deprecated
public void hide() {
}
@Override
@Deprecated
public void addLine(String text) {
appendTextLine(text);
}
@Override
@Deprecated
public void setLine(int index, String text) {
lines.get(index).despawn();
lines.set(index, new CraftTextLine(this, text));
}
@Override
@Deprecated
public void insertLine(int index, String text) {
insertLine(index, text);
}
@Override
@Deprecated
public String[] getLines() {
return null;
}
@Override
@Deprecated
public int getLinesLength() {
return size();
}
@Override
@Deprecated
public void setLocation(Location location) {
teleport(location);
}
@Override
@Deprecated
public void setTouchHandler(TouchHandler handler) {
if (size() > 0) {
TouchableLine line0 = ((TouchableLine) getLine(0));
if (handler != null) {
line0.setTouchHandler(new OldTouchHandlerWrapper(this, handler));
} else {
line0.setTouchHandler(null);
}
}
}
@Override
@Deprecated
public TouchHandler getTouchHandler() {
return null;
}
@Override
@Deprecated
public boolean hasTouchHandler() {
return false;
}
/**
* About: equals() and hashcode()

View File

@ -32,6 +32,7 @@
<module>API</module>
<module>Utils</module>
<module>Config</module>
<module>Legacy</module>
<module>NMS/Interfaces</module>
<module>NMS/v1_8_R2</module>
<module>NMS/v1_8_R3</module>