diff --git a/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/Hologram.java b/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/Hologram.java index a5c3adb7..019bcc6d 100644 --- a/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/Hologram.java +++ b/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/Hologram.java @@ -1,206 +1,220 @@ -package com.gmail.filoghost.holographicdisplays.api; - -import org.bukkit.Location; -import org.bukkit.World; -import org.bukkit.inventory.ItemStack; - -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; -import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; -import com.gmail.filoghost.holographicdisplays.api.line.TextLine; - -/** - * An object made of various lines, that can be items or holograms. - * Holographic lines appear as a nametag without any entity below. - * To create one, please see {@link HologramsAPI#createHologram(org.bukkit.plugin.Plugin, Location)}. - */ -public interface Hologram { - - /** - * Appends a text line to end of this hologram. - * - * @param text the content of the line, can be null for an empty line - * @return the new TextLine appended - */ - public TextLine appendTextLine(String text); - - - /** - * Appends an item line to end of this hologram. - * - * @param itemStack the content of the line - * @return the new ItemLine appended - */ - public ItemLine appendItemLine(ItemStack itemStack); - - - /** - * Inserts a text line in this hologram. - * - * @param index the line is inserted before this index. If 0, the new line will - * be inserted before the first line. - * @param text the content of the line, can be null for an empty line - * @return the new TextLine inserted - * @throws IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size()) - */ - public TextLine insertTextLine(int index, String text); - - - /** - * Inserts an item line in this hologram. - * - * @param index the line is inserted before this index. If 0, the new line will - * be inserted before the first line. - * @param itemStack the content of the line - * @return the new ItemLine inserted - * @throws IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size()) - */ - public ItemLine insertItemLine(int index, ItemStack itemStack); - - - /** - * Finds the element at a given index in the lines. - * - * @param index the index of the line to retrieve. - * @return the hologram line at the given index, can be an {@link ItemLine} or a {@link TextLine}. - * @throws IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size()) - */ - public HologramLine getLine(int index); - - /** - * Removes a line at a given index. Since: v2.0.1 - * - * @param index the index of the line, that should be between 0 and size() - 1. - * @throws IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size()) - */ - public void removeLine(int index); - - - /** - * Removes all the lines from this hologram. - */ - public void clearLines(); - - - /** - * Checks the amount of lines of the hologram. - * - * @return the amount of lines - */ - public int size(); - - - /** - * The physical height of the hologram, counting all the lines. Since: v2.1.4 - * - * @return the height of the hologram, counting all the lines and the gaps between them - */ - public double getHeight(); - - - /** - * Teleports a hologram to the given location. - * - * @param location the new location - */ - public void teleport(Location location); - - - /** - * Teleports a hologram to the given location. - * - * @param world the world where the hologram should be teleported, - * use {@link #getWorld()} to teleport it in the same world. - * @param x the X coordinate - * @param y the Y coordinate - * @param z the Z coordinate - */ - public void teleport(World world, double x, double y, double z); - - /** - * Returns the position of the hologram. - * - * @return the Location of the hologram - */ - public Location getLocation(); - - /** - * Returns the X coordinate. - * - * @return the X coordinate of the hologram - */ - public double getX(); - - - /** - * Returns the Y coordinate. - * - * @return the Y coordinate of the hologram - */ - public double getY(); - - - /** - * Returns the Z coordinate. - * - * @return the Z coordinate of the hologram - */ - public double getZ(); - - - /** - * Returns the world. - * - * @return the world of the hologram - */ - public World getWorld(); - - - /** - * Returns the {@link VisibilityManager} of this hologram. - *
Note: the usage of the VisibilityManager requires ProtocolLib. - * Without the plugin, holograms will be always visible. - * - * @return the VisibilityManager of this hologram - */ - public VisibilityManager getVisibilityManager(); - - - /** - * Returns when the hologram was created. Useful for removing old holograms. - * - * @return the timestamp of when the hologram was created, in milliseconds - */ - public long getCreationTimestamp(); - - /** - * Checks if the hologram will track and replace placeholders. - * This is false by default. - * - * @return if the hologram allows placeholders - */ - public boolean isAllowPlaceholders(); - - /** - * Sets if the hologram should track and replace placeholders. - * By default if will not track them. - * - * @param allowPlaceholders if the hologram should track placeholders - */ - public void setAllowPlaceholders(boolean allowPlaceholders); - - /** - * Deletes this hologram. Editing or teleporting the hologram when deleted - * will throw an exception. Lines will be automatically cleared. - * You should remove all the references of the hologram after deletion. - */ - public void delete(); - - - /** - * Checks if a hologram was deleted. - * - * @return true if this hologram was deleted - */ - public boolean isDeleted(); - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.api; + +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.inventory.ItemStack; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; +import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; +import com.gmail.filoghost.holographicdisplays.api.line.TextLine; + +/** + * An object made of various lines, that can be items or holograms. + * Holographic lines appear as a nametag without any entity below. + * To create one, please see {@link HologramsAPI#createHologram(org.bukkit.plugin.Plugin, Location)}. + */ +public interface Hologram { + + /** + * Appends a text line to end of this hologram. + * + * @param text the content of the line, can be null for an empty line + * @return the new TextLine appended + */ + public TextLine appendTextLine(String text); + + + /** + * Appends an item line to end of this hologram. + * + * @param itemStack the content of the line + * @return the new ItemLine appended + */ + public ItemLine appendItemLine(ItemStack itemStack); + + + /** + * Inserts a text line in this hologram. + * + * @param index the line is inserted before this index. If 0, the new line will + * be inserted before the first line. + * @param text the content of the line, can be null for an empty line + * @return the new TextLine inserted + * @throws IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size()) + */ + public TextLine insertTextLine(int index, String text); + + + /** + * Inserts an item line in this hologram. + * + * @param index the line is inserted before this index. If 0, the new line will + * be inserted before the first line. + * @param itemStack the content of the line + * @return the new ItemLine inserted + * @throws IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size()) + */ + public ItemLine insertItemLine(int index, ItemStack itemStack); + + + /** + * Finds the element at a given index in the lines. + * + * @param index the index of the line to retrieve. + * @return the hologram line at the given index, can be an {@link ItemLine} or a {@link TextLine}. + * @throws IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size()) + */ + public HologramLine getLine(int index); + + /** + * Removes a line at a given index. Since: v2.0.1 + * + * @param index the index of the line, that should be between 0 and size() - 1. + * @throws IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size()) + */ + public void removeLine(int index); + + + /** + * Removes all the lines from this hologram. + */ + public void clearLines(); + + + /** + * Checks the amount of lines of the hologram. + * + * @return the amount of lines + */ + public int size(); + + + /** + * The physical height of the hologram, counting all the lines. Since: v2.1.4 + * + * @return the height of the hologram, counting all the lines and the gaps between them + */ + public double getHeight(); + + + /** + * Teleports a hologram to the given location. + * + * @param location the new location + */ + public void teleport(Location location); + + + /** + * Teleports a hologram to the given location. + * + * @param world the world where the hologram should be teleported, + * use {@link #getWorld()} to teleport it in the same world. + * @param x the X coordinate + * @param y the Y coordinate + * @param z the Z coordinate + */ + public void teleport(World world, double x, double y, double z); + + /** + * Returns the position of the hologram. + * + * @return the Location of the hologram + */ + public Location getLocation(); + + /** + * Returns the X coordinate. + * + * @return the X coordinate of the hologram + */ + public double getX(); + + + /** + * Returns the Y coordinate. + * + * @return the Y coordinate of the hologram + */ + public double getY(); + + + /** + * Returns the Z coordinate. + * + * @return the Z coordinate of the hologram + */ + public double getZ(); + + + /** + * Returns the world. + * + * @return the world of the hologram + */ + public World getWorld(); + + + /** + * Returns the {@link VisibilityManager} of this hologram. + *
Note: the usage of the VisibilityManager requires ProtocolLib. + * Without the plugin, holograms will be always visible. + * + * @return the VisibilityManager of this hologram + */ + public VisibilityManager getVisibilityManager(); + + + /** + * Returns when the hologram was created. Useful for removing old holograms. + * + * @return the timestamp of when the hologram was created, in milliseconds + */ + public long getCreationTimestamp(); + + /** + * Checks if the hologram will track and replace placeholders. + * This is false by default. + * + * @return if the hologram allows placeholders + */ + public boolean isAllowPlaceholders(); + + /** + * Sets if the hologram should track and replace placeholders. + * By default if will not track them. + * + * @param allowPlaceholders if the hologram should track placeholders + */ + public void setAllowPlaceholders(boolean allowPlaceholders); + + /** + * Deletes this hologram. Editing or teleporting the hologram when deleted + * will throw an exception. Lines will be automatically cleared. + * You should remove all the references of the hologram after deletion. + */ + public void delete(); + + + /** + * Checks if a hologram was deleted. + * + * @return true if this hologram was deleted + */ + public boolean isDeleted(); + +} diff --git a/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/HologramsAPI.java b/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/HologramsAPI.java index 7e60027d..4cc6669f 100644 --- a/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/HologramsAPI.java +++ b/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/HologramsAPI.java @@ -1,107 +1,121 @@ -package com.gmail.filoghost.holographicdisplays.api; - -import java.util.Collection; - -import org.bukkit.Location; -import org.bukkit.entity.Entity; -import org.bukkit.plugin.Plugin; - -import com.gmail.filoghost.holographicdisplays.api.internal.BackendAPI; -import com.gmail.filoghost.holographicdisplays.api.placeholder.PlaceholderReplacer; - -/** - * This the main class of the Holographic Displays API. - * It provides methods to create holograms and to register custom placeholders. - */ -public class HologramsAPI { - - - private HologramsAPI() { - // No constructor needed. - } - - - /** - * Creates a hologram at given location. - * - * @param plugin the plugin that creates it - * @param source the location where it will appear - * @return the new hologram created - */ - public static Hologram createHologram(Plugin plugin, Location source) { - return BackendAPI.getImplementation().createHologram(plugin, source); - } - - - /** - * Finds all the holograms created by a given plugin. - * - * @param plugin the plugin to search for in holograms - * @return the holograms created by a plugin. the Collection is a copy - * and modifying it has no effect on the holograms. - */ - public static Collection getHolograms(Plugin plugin) { - return BackendAPI.getImplementation().getHolograms(plugin); - } - - - /** - * Registers a new placeholder that can be used in holograms created with commands. - * With this method, you can basically expand the core of HolographicDisplays. - * - * @param plugin the owner plugin of the placeholder - * @param textPlaceholder the text that the placeholder will be associated to (e.g.: "{onlinePlayers}") - * @param refreshRate the refresh rate of the placeholder, in seconds. Keep in mind that the minimum is 0.1 seconds, and that will be rounded to tenths of seconds - * @param replacer the implementation that will return the text to replace the placeholder, where the update() method is called every refreshRate seconds - * @return true if the registration was successfull, false if it was already registered - */ - public static boolean registerPlaceholder(Plugin plugin, String textPlaceholder, double refreshRate, PlaceholderReplacer replacer) { - return BackendAPI.getImplementation().registerPlaceholder(plugin, textPlaceholder, refreshRate, replacer); - } - - - /** - * Finds all the placeholders registered by a given plugin. - * - * @param plugin the plugin to search for - * @return a collection of placeholders registered by the plugin - */ - public static Collection getRegisteredPlaceholders(Plugin plugin) { - return BackendAPI.getImplementation().getRegisteredPlaceholders(plugin); - } - - - /** - * Unregister a placeholder created by a plugin. - * - * @param plugin the plugin that owns the placeholder - * @param textPlaceholder the placeholder to remove - * @return true if found and removed, false otherwise - */ - public static boolean unregisterPlaceholder(Plugin plugin, String textPlaceholder) { - return BackendAPI.getImplementation().unregisterPlaceholder(plugin, textPlaceholder); - } - - - /** - * Resets and removes all the placeholders registered by a plugin. This is useful - * when you have configurable placeholders and you want to remove all of them. - * - * @param plugin the plugin that owns the placeholders - */ - public static void unregisterPlaceholders(Plugin plugin) { - BackendAPI.getImplementation().unregisterPlaceholders(plugin); - } - - - /** - * Checks if an entity is part of a hologram. - * - * @param bukkitEntity the entity to check - * @return true if the entity is a part of a hologram - */ - public static boolean isHologramEntity(Entity bukkitEntity) { - return BackendAPI.getImplementation().isHologramEntity(bukkitEntity); - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.api; + +import java.util.Collection; + +import org.bukkit.Location; +import org.bukkit.entity.Entity; +import org.bukkit.plugin.Plugin; + +import com.gmail.filoghost.holographicdisplays.api.internal.BackendAPI; +import com.gmail.filoghost.holographicdisplays.api.placeholder.PlaceholderReplacer; + +/** + * This the main class of the Holographic Displays API. + * It provides methods to create holograms and to register custom placeholders. + */ +public class HologramsAPI { + + + private HologramsAPI() { + // No constructor needed. + } + + + /** + * Creates a hologram at given location. + * + * @param plugin the plugin that creates it + * @param source the location where it will appear + * @return the new hologram created + */ + public static Hologram createHologram(Plugin plugin, Location source) { + return BackendAPI.getImplementation().createHologram(plugin, source); + } + + + /** + * Finds all the holograms created by a given plugin. + * + * @param plugin the plugin to search for in holograms + * @return the holograms created by a plugin. the Collection is a copy + * and modifying it has no effect on the holograms. + */ + public static Collection getHolograms(Plugin plugin) { + return BackendAPI.getImplementation().getHolograms(plugin); + } + + + /** + * Registers a new placeholder that can be used in holograms created with commands. + * With this method, you can basically expand the core of HolographicDisplays. + * + * @param plugin the owner plugin of the placeholder + * @param textPlaceholder the text that the placeholder will be associated to (e.g.: "{onlinePlayers}") + * @param refreshRate the refresh rate of the placeholder, in seconds. Keep in mind that the minimum is 0.1 seconds, and that will be rounded to tenths of seconds + * @param replacer the implementation that will return the text to replace the placeholder, where the update() method is called every refreshRate seconds + * @return true if the registration was successfull, false if it was already registered + */ + public static boolean registerPlaceholder(Plugin plugin, String textPlaceholder, double refreshRate, PlaceholderReplacer replacer) { + return BackendAPI.getImplementation().registerPlaceholder(plugin, textPlaceholder, refreshRate, replacer); + } + + + /** + * Finds all the placeholders registered by a given plugin. + * + * @param plugin the plugin to search for + * @return a collection of placeholders registered by the plugin + */ + public static Collection getRegisteredPlaceholders(Plugin plugin) { + return BackendAPI.getImplementation().getRegisteredPlaceholders(plugin); + } + + + /** + * Unregister a placeholder created by a plugin. + * + * @param plugin the plugin that owns the placeholder + * @param textPlaceholder the placeholder to remove + * @return true if found and removed, false otherwise + */ + public static boolean unregisterPlaceholder(Plugin plugin, String textPlaceholder) { + return BackendAPI.getImplementation().unregisterPlaceholder(plugin, textPlaceholder); + } + + + /** + * Resets and removes all the placeholders registered by a plugin. This is useful + * when you have configurable placeholders and you want to remove all of them. + * + * @param plugin the plugin that owns the placeholders + */ + public static void unregisterPlaceholders(Plugin plugin) { + BackendAPI.getImplementation().unregisterPlaceholders(plugin); + } + + + /** + * Checks if an entity is part of a hologram. + * + * @param bukkitEntity the entity to check + * @return true if the entity is a part of a hologram + */ + public static boolean isHologramEntity(Entity bukkitEntity) { + return BackendAPI.getImplementation().isHologramEntity(bukkitEntity); + } + +} diff --git a/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/VisibilityManager.java b/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/VisibilityManager.java index 10a6c133..e667f7d3 100644 --- a/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/VisibilityManager.java +++ b/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/VisibilityManager.java @@ -1,66 +1,80 @@ -package com.gmail.filoghost.holographicdisplays.api; - -import org.bukkit.entity.Player; - -/** - * This object is used to manage the visibility of a hologram. - * It allows to hide/show the hologram to certain players, and the default behaviour - * (when a hologram is not specifically being hidden/shown to a player) can be customized. - */ -public interface VisibilityManager { - - /** - * Returns if the hologram is visible by default. If not changed, this value - * is true by default so the hologram is visible to everyone. - * - * @return if the hologram hologram is visible by default - */ - public boolean isVisibleByDefault(); - - /** - * Sets if the hologram is visible by default. If not changed, this value - * is true by default so the hologram is visible to everyone. - * - * @param visibleByDefault the new behaviour - */ - public void setVisibleByDefault(boolean visibleByDefault); - - /** - * Shows the hologram to a player, overriding the value of {@link #isVisibleByDefault()}. - * This is persistent if the players goes offline. - * - * @param player the involved player - */ - public void showTo(Player player); - - /** - * Hides the hologram to a player, overriding the value of {@link #isVisibleByDefault()}. - * This is persistent if the players goes offline. - * - * @param player the involved player - */ - public void hideTo(Player player); - - /** - * Checks if a hologram is visible to a player. - * - * @param player the involved player - * @return if the player can see the hologram - */ - public boolean isVisibleTo(Player player); - - /** - * Resets the visibility to the default value. If you previously called {@link #showTo(Player)} - * or {@link #hideTo(Player)} to override the default visibility, this method will reset it - * to reflect the value of {@link #isVisibleByDefault()}. - * - * @param player the involved player - */ - public void resetVisibility(Player player); - - /** - * Resets the visibility for all the players. See {@link #resetVisibility(Player)} for more details. - */ - public void resetVisibilityAll(); - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.api; + +import org.bukkit.entity.Player; + +/** + * This object is used to manage the visibility of a hologram. + * It allows to hide/show the hologram to certain players, and the default behaviour + * (when a hologram is not specifically being hidden/shown to a player) can be customized. + */ +public interface VisibilityManager { + + /** + * Returns if the hologram is visible by default. If not changed, this value + * is true by default so the hologram is visible to everyone. + * + * @return if the hologram hologram is visible by default + */ + public boolean isVisibleByDefault(); + + /** + * Sets if the hologram is visible by default. If not changed, this value + * is true by default so the hologram is visible to everyone. + * + * @param visibleByDefault the new behaviour + */ + public void setVisibleByDefault(boolean visibleByDefault); + + /** + * Shows the hologram to a player, overriding the value of {@link #isVisibleByDefault()}. + * This is persistent if the players goes offline. + * + * @param player the involved player + */ + public void showTo(Player player); + + /** + * Hides the hologram to a player, overriding the value of {@link #isVisibleByDefault()}. + * This is persistent if the players goes offline. + * + * @param player the involved player + */ + public void hideTo(Player player); + + /** + * Checks if a hologram is visible to a player. + * + * @param player the involved player + * @return if the player can see the hologram + */ + public boolean isVisibleTo(Player player); + + /** + * Resets the visibility to the default value. If you previously called {@link #showTo(Player)} + * or {@link #hideTo(Player)} to override the default visibility, this method will reset it + * to reflect the value of {@link #isVisibleByDefault()}. + * + * @param player the involved player + */ + public void resetVisibility(Player player); + + /** + * Resets the visibility for all the players. See {@link #resetVisibility(Player)} for more details. + */ + public void resetVisibilityAll(); + +} diff --git a/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/handler/PickupHandler.java b/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/handler/PickupHandler.java index d11aa8f2..60a6bdc9 100644 --- a/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/handler/PickupHandler.java +++ b/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/handler/PickupHandler.java @@ -1,16 +1,30 @@ -package com.gmail.filoghost.holographicdisplays.api.handler; - -import org.bukkit.entity.Player; - -/** - * Interface to handle items being picked up by players. - */ -public interface PickupHandler { - - /** - * Called when a player picks up the item. - * @param player the player who picked up the item - */ - public void onPickup(Player player); - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.api.handler; + +import org.bukkit.entity.Player; + +/** + * Interface to handle items being picked up by players. + */ +public interface PickupHandler { + + /** + * Called when a player picks up the item. + * @param player the player who picked up the item + */ + public void onPickup(Player player); + +} diff --git a/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/handler/TouchHandler.java b/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/handler/TouchHandler.java index ed0164bb..8f843a47 100644 --- a/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/handler/TouchHandler.java +++ b/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/handler/TouchHandler.java @@ -1,16 +1,30 @@ -package com.gmail.filoghost.holographicdisplays.api.handler; - -import org.bukkit.entity.Player; - -/** - * Interface to handle touch holograms. - */ -public interface TouchHandler { - - /** - * Called when a player interacts with the hologram (right click). - * @param player the player who interacts - */ - public void onTouch(Player player); - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.api.handler; + +import org.bukkit.entity.Player; + +/** + * Interface to handle touch holograms. + */ +public interface TouchHandler { + + /** + * Called when a player interacts with the hologram (right click). + * @param player the player who interacts + */ + public void onTouch(Player player); + +} diff --git a/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/internal/BackendAPI.java b/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/internal/BackendAPI.java index 575d6b66..e6bf5866 100644 --- a/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/internal/BackendAPI.java +++ b/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/internal/BackendAPI.java @@ -1,43 +1,57 @@ -package com.gmail.filoghost.holographicdisplays.api.internal; - -import java.util.Collection; - -import org.bukkit.Location; -import org.bukkit.entity.Entity; -import org.bukkit.plugin.Plugin; - -import com.gmail.filoghost.holographicdisplays.api.Hologram; -import com.gmail.filoghost.holographicdisplays.api.placeholder.PlaceholderReplacer; - -public abstract class BackendAPI { - - private static BackendAPI implementation; - - public static void setImplementation(BackendAPI implementation) { - BackendAPI.implementation = implementation; - } - - public static BackendAPI getImplementation() { - if (implementation == null) { - throw new IllegalStateException("No API implementation set. Is Holographic Displays enabled?"); - } - - return implementation; - } - - public abstract Hologram createHologram(Plugin plugin, Location source); - - public abstract Collection getHolograms(Plugin plugin); - - public abstract boolean registerPlaceholder(Plugin plugin, String textPlaceholder, double refreshRate, PlaceholderReplacer replacer); - - public abstract Collection getRegisteredPlaceholders(Plugin plugin); - - public abstract boolean unregisterPlaceholder(Plugin plugin, String textPlaceholder); - - public abstract void unregisterPlaceholders(Plugin plugin); - - public abstract boolean isHologramEntity(Entity bukkitEntity); - - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.api.internal; + +import java.util.Collection; + +import org.bukkit.Location; +import org.bukkit.entity.Entity; +import org.bukkit.plugin.Plugin; + +import com.gmail.filoghost.holographicdisplays.api.Hologram; +import com.gmail.filoghost.holographicdisplays.api.placeholder.PlaceholderReplacer; + +public abstract class BackendAPI { + + private static BackendAPI implementation; + + public static void setImplementation(BackendAPI implementation) { + BackendAPI.implementation = implementation; + } + + public static BackendAPI getImplementation() { + if (implementation == null) { + throw new IllegalStateException("No API implementation set. Is Holographic Displays enabled?"); + } + + return implementation; + } + + public abstract Hologram createHologram(Plugin plugin, Location source); + + public abstract Collection getHolograms(Plugin plugin); + + public abstract boolean registerPlaceholder(Plugin plugin, String textPlaceholder, double refreshRate, PlaceholderReplacer replacer); + + public abstract Collection getRegisteredPlaceholders(Plugin plugin); + + public abstract boolean unregisterPlaceholder(Plugin plugin, String textPlaceholder); + + public abstract void unregisterPlaceholders(Plugin plugin); + + public abstract boolean isHologramEntity(Entity bukkitEntity); + + +} diff --git a/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/line/CollectableLine.java b/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/line/CollectableLine.java index a7f62562..4ab639ab 100644 --- a/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/line/CollectableLine.java +++ b/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/line/CollectableLine.java @@ -1,24 +1,38 @@ -package com.gmail.filoghost.holographicdisplays.api.line; - -import com.gmail.filoghost.holographicdisplays.api.handler.PickupHandler; - -/** - * A line of a Hologram that can be picked up. - */ -public interface CollectableLine extends HologramLine { - - /** - * Sets the PickupHandler for this line. - * - * @param pickupHandler the new PickupHandler, can be null. - */ - public void setPickupHandler(PickupHandler pickupHandler); - - /** - * Returns the current PickupHandler of this line. - * - * @return the current PickupHandler, can be null. - */ - public PickupHandler getPickupHandler(); - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.api.line; + +import com.gmail.filoghost.holographicdisplays.api.handler.PickupHandler; + +/** + * A line of a Hologram that can be picked up. + */ +public interface CollectableLine extends HologramLine { + + /** + * Sets the PickupHandler for this line. + * + * @param pickupHandler the new PickupHandler, can be null. + */ + public void setPickupHandler(PickupHandler pickupHandler); + + /** + * Returns the current PickupHandler of this line. + * + * @return the current PickupHandler, can be null. + */ + public PickupHandler getPickupHandler(); + +} diff --git a/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/line/HologramLine.java b/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/line/HologramLine.java index 0ab818d6..15f36098 100644 --- a/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/line/HologramLine.java +++ b/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/line/HologramLine.java @@ -1,23 +1,37 @@ -package com.gmail.filoghost.holographicdisplays.api.line; - -import com.gmail.filoghost.holographicdisplays.api.Hologram; - -/** - * Interface to represent a line in a Hologram. - */ -public interface HologramLine { - - /** - * Returns the parent Hologram of this line. - * - * @return the parent Hologram. - */ - public Hologram getParent(); - - /** - * Removes this line from the parent Hologram. Since: v2.0.1 - * Do not call if the Hologram has been deleted, an exception will be thrown. - */ - public void removeLine(); - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.api.line; + +import com.gmail.filoghost.holographicdisplays.api.Hologram; + +/** + * Interface to represent a line in a Hologram. + */ +public interface HologramLine { + + /** + * Returns the parent Hologram of this line. + * + * @return the parent Hologram. + */ + public Hologram getParent(); + + /** + * Removes this line from the parent Hologram. Since: v2.0.1 + * Do not call if the Hologram has been deleted, an exception will be thrown. + */ + public void removeLine(); + +} diff --git a/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/line/ItemLine.java b/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/line/ItemLine.java index b1e4ff5c..60534c2a 100644 --- a/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/line/ItemLine.java +++ b/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/line/ItemLine.java @@ -1,21 +1,35 @@ -package com.gmail.filoghost.holographicdisplays.api.line; - -import org.bukkit.inventory.ItemStack; - -public interface ItemLine extends CollectableLine, TouchableLine { - - /** - * Returns the ItemStack of this ItemLine. - * - * @return the ItemStack if this ItemLine. - */ - public ItemStack getItemStack(); - - /** - * Sets the ItemStack for this ItemLine. - * - * @param itemStack the new item, should not be null. - */ - public void setItemStack(ItemStack itemStack); - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.api.line; + +import org.bukkit.inventory.ItemStack; + +public interface ItemLine extends CollectableLine, TouchableLine { + + /** + * Returns the ItemStack of this ItemLine. + * + * @return the ItemStack if this ItemLine. + */ + public ItemStack getItemStack(); + + /** + * Sets the ItemStack for this ItemLine. + * + * @param itemStack the new item, should not be null. + */ + public void setItemStack(ItemStack itemStack); + +} diff --git a/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/line/TextLine.java b/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/line/TextLine.java index 848a0af1..68d05216 100644 --- a/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/line/TextLine.java +++ b/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/line/TextLine.java @@ -1,19 +1,33 @@ -package com.gmail.filoghost.holographicdisplays.api.line; - -public interface TextLine extends TouchableLine { - - /** - * Returns the current text of this TextLine. - * - * @return the current text of this line. - */ - public String getText(); - - /** - * Sets the text of this TextLine. - * - * @param text the new text of this line. - */ - public void setText(String text); - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.api.line; + +public interface TextLine extends TouchableLine { + + /** + * Returns the current text of this TextLine. + * + * @return the current text of this line. + */ + public String getText(); + + /** + * Sets the text of this TextLine. + * + * @param text the new text of this line. + */ + public void setText(String text); + +} diff --git a/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/line/TouchableLine.java b/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/line/TouchableLine.java index c2bae49b..07f1a716 100644 --- a/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/line/TouchableLine.java +++ b/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/line/TouchableLine.java @@ -1,24 +1,38 @@ -package com.gmail.filoghost.holographicdisplays.api.line; - -import com.gmail.filoghost.holographicdisplays.api.handler.TouchHandler; - -/** - * A line of a Hologram that can be touched (right click). - */ -public interface TouchableLine extends HologramLine { - - /** - * Sets the TouchHandler for this line. - * - * @param touchHandler the new TouchHandler, can be null. - */ - public void setTouchHandler(TouchHandler touchHandler); - - /** - * Returns the current TouchHandler of this line. - * - * @return the current TouchHandler, can be null. - */ - public TouchHandler getTouchHandler(); - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.api.line; + +import com.gmail.filoghost.holographicdisplays.api.handler.TouchHandler; + +/** + * A line of a Hologram that can be touched (right click). + */ +public interface TouchableLine extends HologramLine { + + /** + * Sets the TouchHandler for this line. + * + * @param touchHandler the new TouchHandler, can be null. + */ + public void setTouchHandler(TouchHandler touchHandler); + + /** + * Returns the current TouchHandler of this line. + * + * @return the current TouchHandler, can be null. + */ + public TouchHandler getTouchHandler(); + +} diff --git a/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/placeholder/PlaceholderReplacer.java b/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/placeholder/PlaceholderReplacer.java index 9362b72d..f2e9942f 100644 --- a/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/placeholder/PlaceholderReplacer.java +++ b/API/src/main/java/com/gmail/filoghost/holographicdisplays/api/placeholder/PlaceholderReplacer.java @@ -1,11 +1,25 @@ -package com.gmail.filoghost.holographicdisplays.api.placeholder; - -public interface PlaceholderReplacer { - - /** - * Called to update a placeholder's replacement. - * @return the replacement - */ - public String update(); - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.api.placeholder; + +public interface PlaceholderReplacer { + + /** + * Called to update a placeholder's replacement. + * @return the replacement + */ + public String update(); + +} diff --git a/Config/src/main/java/com/gmail/filoghost/holographicdisplays/disk/ConfigNode.java b/Config/src/main/java/com/gmail/filoghost/holographicdisplays/disk/ConfigNode.java index c1ef47b7..d22b6986 100644 --- a/Config/src/main/java/com/gmail/filoghost/holographicdisplays/disk/ConfigNode.java +++ b/Config/src/main/java/com/gmail/filoghost/holographicdisplays/disk/ConfigNode.java @@ -1,42 +1,56 @@ -package com.gmail.filoghost.holographicdisplays.disk; - -import java.util.Arrays; - -public enum ConfigNode { - - SPACE_BETWEEN_LINES("space-between-lines", 0.02), - PRECISE_HOLOGRAM_MOVEMENT("precise-hologram-movement", true), - IMAGES_SYMBOL("images.symbol", "[x]"), - TRANSPARENCY_SPACE("images.transparency.space", " [|] "), - TRANSPARENCY_COLOR("images.transparency.color", "&7"), - UPDATE_NOTIFICATION("update-notification", true), - BUNGEE_REFRESH_SECONDS("bungee.refresh-seconds", 3), - BUNGEE_USE_REDIS_BUNGEE("bungee.use-RedisBungee", false), - BUNGEE_USE_FULL_PINGER("bungee.pinger.enable", false), - BUNGEE_PINGER_TIMEOUT("bungee.pinger.timeout", 500), - BUNGEE_PINGER_OFFLINE_MOTD("bungee.pinger.offline-motd", "&cOffline, couldn't get the MOTD."), - BUNGEE_PINGER_ONLINE_FORMAT("bungee.pinger.status.online", "&aOnline"), - BUNGEE_PINGER_OFFLINE_FORMAT("bungee.pinger.status.offline", "&cOffline"), - BUNGEE_PINGER_TRIM_MOTD("bungee.pinger.motd-remove-leading-trailing-spaces", true), - BUNGEE_PINGER_SERVERS("bungee.pinger.servers", Arrays.asList("hub: 127.0.0.1:25565", "survival: 127.0.0.1:25566", "minigames: 127.0.0.1:25567")), - TIME_FORMAT("time.format", "H:mm"), - TIME_ZONE("time.zone", "GMT+1"), - DEBUG("debug", false); - - private final String path; - private final Object value; - - private ConfigNode(String path, Object defaultValue) { - this.path = path; - value = defaultValue; - } - - public String getPath() { - return path; - } - - public Object getDefaultValue() { - return value; - } - -} \ No newline at end of file +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.disk; + +import java.util.Arrays; + +public enum ConfigNode { + + SPACE_BETWEEN_LINES("space-between-lines", 0.02), + PRECISE_HOLOGRAM_MOVEMENT("precise-hologram-movement", true), + IMAGES_SYMBOL("images.symbol", "[x]"), + TRANSPARENCY_SPACE("images.transparency.space", " [|] "), + TRANSPARENCY_COLOR("images.transparency.color", "&7"), + UPDATE_NOTIFICATION("update-notification", true), + BUNGEE_REFRESH_SECONDS("bungee.refresh-seconds", 3), + BUNGEE_USE_REDIS_BUNGEE("bungee.use-RedisBungee", false), + BUNGEE_USE_FULL_PINGER("bungee.pinger.enable", false), + BUNGEE_PINGER_TIMEOUT("bungee.pinger.timeout", 500), + BUNGEE_PINGER_OFFLINE_MOTD("bungee.pinger.offline-motd", "&cOffline, couldn't get the MOTD."), + BUNGEE_PINGER_ONLINE_FORMAT("bungee.pinger.status.online", "&aOnline"), + BUNGEE_PINGER_OFFLINE_FORMAT("bungee.pinger.status.offline", "&cOffline"), + BUNGEE_PINGER_TRIM_MOTD("bungee.pinger.motd-remove-leading-trailing-spaces", true), + BUNGEE_PINGER_SERVERS("bungee.pinger.servers", Arrays.asList("hub: 127.0.0.1:25565", "survival: 127.0.0.1:25566", "minigames: 127.0.0.1:25567")), + TIME_FORMAT("time.format", "H:mm"), + TIME_ZONE("time.zone", "GMT+1"), + DEBUG("debug", false); + + private final String path; + private final Object value; + + private ConfigNode(String path, Object defaultValue) { + this.path = path; + value = defaultValue; + } + + public String getPath() { + return path; + } + + public Object getDefaultValue() { + return value; + } + +} diff --git a/Config/src/main/java/com/gmail/filoghost/holographicdisplays/disk/Configuration.java b/Config/src/main/java/com/gmail/filoghost/holographicdisplays/disk/Configuration.java index 70ae5c1e..3acc96c7 100644 --- a/Config/src/main/java/com/gmail/filoghost/holographicdisplays/disk/Configuration.java +++ b/Config/src/main/java/com/gmail/filoghost/holographicdisplays/disk/Configuration.java @@ -1,207 +1,221 @@ -package com.gmail.filoghost.holographicdisplays.disk; - -import java.io.File; -import java.io.IOException; -import java.text.SimpleDateFormat; -import java.util.Arrays; -import java.util.List; -import java.util.Map; -import java.util.TimeZone; -import java.util.logging.Level; - -import org.bukkit.ChatColor; -import org.bukkit.configuration.InvalidConfigurationException; -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.plugin.Plugin; - -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.Utils; - -/** - * Just a bunch of static varibles to hold the settings. - * Useful for fast access. - */ -public class Configuration { - - public static double spaceBetweenLines; - public static boolean preciseHologramMovement; - public static String imageSymbol; - public static String transparencySymbol; - public static boolean updateNotification; - public static ChatColor transparencyColor; - - public static SimpleDateFormat timeFormat; - - public static int bungeeRefreshSeconds; - public static boolean useRedisBungee; - - public static boolean pingerEnable; - public static int pingerTimeout; - public static String pingerOfflineMotd; - public static String pingerStatusOnline; - public static String pingerStatusOffline; - public static boolean pingerTrimMotd; - public static Map pingerServers; - - - public static void load(Plugin plugin) { - File configFile = new File(plugin.getDataFolder(), "config.yml"); - if (!configFile.exists()) { - plugin.getDataFolder().mkdirs(); - plugin.saveResource("config.yml", true); - } - - YamlConfiguration config = new YamlConfiguration(); - try { - config.load(configFile); - } catch (InvalidConfigurationException e) { - e.printStackTrace(); - ConsoleLogger.log(Level.WARNING, "The configuration is not a valid YAML file! Please check it with a tool like http://yaml-online-parser.appspot.com/"); - return; - } catch (IOException e) { - e.printStackTrace(); - ConsoleLogger.log(Level.WARNING, "I/O error while reading the configuration. Was the file in use?"); - return; - } catch (Exception e) { - e.printStackTrace(); - ConsoleLogger.log(Level.WARNING, "Unhandled exception while reading the configuration!"); - return; - } - - boolean needsSave = false; - - for (ConfigNode node : ConfigNode.values()) { - if (!config.isSet(node.getPath())) { - needsSave = true; - config.set(node.getPath(), node.getDefaultValue()); - } - } - - // Check the old values. - List nodesToRemove = Arrays.asList( - "vertical-spacing", - "time-format", - "bungee-refresh-seconds", - "using-RedisBungee", - "bungee-online-format", - "bungee-offline-format" - ); - - for (String oldNode : nodesToRemove) { - if (config.isSet(oldNode)) { - config.set(oldNode, null); - needsSave = true; - } - } - - - - if (needsSave) { - config.options().header(Utils.join(new String[] { - ".", - ". Read the tutorial at: http://dev.bukkit.org/bukkit-plugins/holographic-displays/", - ".", - ". Plugin created by filoghost.", - "."}, - "\n")); - config.options().copyHeader(true); - try { - config.save(configFile); - } catch (IOException e) { - e.printStackTrace(); - ConsoleLogger.log(Level.WARNING, "I/O error while saving the configuration. Was the file in use?"); - } - } - - spaceBetweenLines = config.getDouble(ConfigNode.SPACE_BETWEEN_LINES.getPath()); - preciseHologramMovement = config.getBoolean(ConfigNode.PRECISE_HOLOGRAM_MOVEMENT.getPath()); - - updateNotification = config.getBoolean(ConfigNode.UPDATE_NOTIFICATION.getPath()); - - imageSymbol = StringConverter.toReadableFormat(config.getString(ConfigNode.IMAGES_SYMBOL.getPath())); - transparencySymbol = StringConverter.toReadableFormat(config.getString(ConfigNode.TRANSPARENCY_SPACE.getPath())); - bungeeRefreshSeconds = config.getInt(ConfigNode.BUNGEE_REFRESH_SECONDS.getPath()); - useRedisBungee = config.getBoolean(ConfigNode.BUNGEE_USE_REDIS_BUNGEE.getPath()); - - pingerEnable = config.getBoolean(ConfigNode.BUNGEE_USE_FULL_PINGER.getPath()); - pingerTimeout = config.getInt(ConfigNode.BUNGEE_PINGER_TIMEOUT.getPath()); - pingerTrimMotd = config.getBoolean(ConfigNode.BUNGEE_PINGER_TRIM_MOTD.getPath()); - - pingerOfflineMotd = StringConverter.toReadableFormat(config.getString(ConfigNode.BUNGEE_PINGER_OFFLINE_MOTD.getPath())); - pingerStatusOnline = StringConverter.toReadableFormat(config.getString(ConfigNode.BUNGEE_PINGER_ONLINE_FORMAT.getPath())); - pingerStatusOffline = StringConverter.toReadableFormat(config.getString(ConfigNode.BUNGEE_PINGER_OFFLINE_FORMAT.getPath())); - - if (pingerTimeout <= 0) { - pingerTimeout = 100; - } else if (pingerTimeout >= 10000) { - pingerTimeout = 10000; - } - - pingerServers = Utils.newMap(); - - if (pingerEnable) { - for (String singleServer : config.getStringList(ConfigNode.BUNGEE_PINGER_SERVERS.getPath())) { - String[] nameAndAddress = singleServer.split(":", 2); - if (nameAndAddress.length < 2) { - ConsoleLogger.log(Level.WARNING, "The server info \"" + singleServer + "\" is not valid. There should be a name and an address, separated by a colon."); - continue; - } - - String name = nameAndAddress[0].trim(); - String address = nameAndAddress[1].replace(" ", ""); - - String ip; - int port; - - if (address.contains(":")) { - String[] ipAndPort = address.split(":", 2); - ip = ipAndPort[0]; - try { - port = Integer.parseInt(ipAndPort[1]); - } catch (NumberFormatException e) { - ConsoleLogger.log(Level.WARNING, "Invalid port number in the server info \"" + singleServer + "\"."); - continue; - } - } else { - ip = address; - port = 25565; // The default Minecraft port. - } - - pingerServers.put(name, new ServerAddress(ip, port)); - } - } - - ConsoleLogger.setDebugEnabled(config.getBoolean(ConfigNode.DEBUG.getPath())); - - String tempColor = config.getString(ConfigNode.TRANSPARENCY_COLOR.getPath()).replace('&', ChatColor.COLOR_CHAR); - boolean foundColor = false; - for (ChatColor chatColor : ChatColor.values()) { - if (chatColor.toString().equals(tempColor)) { - Configuration.transparencyColor = chatColor; - foundColor = true; - } - } - if (!foundColor) { - Configuration.transparencyColor = ChatColor.GRAY; - ConsoleLogger.log(Level.WARNING, "You didn't set a valid chat color for transparency in the configuration, light gray (&7) will be used."); - } - - try { - timeFormat = new SimpleDateFormat(config.getString(ConfigNode.TIME_FORMAT.getPath())); - timeFormat.setTimeZone(TimeZone.getTimeZone(config.getString(ConfigNode.TIME_ZONE.getPath()))); - } catch (IllegalArgumentException ex) { - timeFormat = new SimpleDateFormat("H:mm"); - ConsoleLogger.log(Level.WARNING, "Time format not valid in the configuration, using the default."); - } - - if (bungeeRefreshSeconds < 1) { - ConsoleLogger.log(Level.WARNING, "The minimum interval for pinging BungeeCord's servers is 1 second. It has been automatically set."); - bungeeRefreshSeconds = 1; - } - - if (bungeeRefreshSeconds > 60) { - ConsoleLogger.log(Level.WARNING, "The maximum interval for pinging BungeeCord's servers is 60 seconds. It has been automatically set."); - bungeeRefreshSeconds = 60; - } - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.disk; + +import java.io.File; +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.TimeZone; +import java.util.logging.Level; + +import org.bukkit.ChatColor; +import org.bukkit.configuration.InvalidConfigurationException; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.plugin.Plugin; + +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.Utils; + +/** + * Just a bunch of static varibles to hold the settings. + * Useful for fast access. + */ +public class Configuration { + + public static double spaceBetweenLines; + public static boolean preciseHologramMovement; + public static String imageSymbol; + public static String transparencySymbol; + public static boolean updateNotification; + public static ChatColor transparencyColor; + + public static SimpleDateFormat timeFormat; + + public static int bungeeRefreshSeconds; + public static boolean useRedisBungee; + + public static boolean pingerEnable; + public static int pingerTimeout; + public static String pingerOfflineMotd; + public static String pingerStatusOnline; + public static String pingerStatusOffline; + public static boolean pingerTrimMotd; + public static Map pingerServers; + + + public static void load(Plugin plugin) { + File configFile = new File(plugin.getDataFolder(), "config.yml"); + if (!configFile.exists()) { + plugin.getDataFolder().mkdirs(); + plugin.saveResource("config.yml", true); + } + + YamlConfiguration config = new YamlConfiguration(); + try { + config.load(configFile); + } catch (InvalidConfigurationException e) { + e.printStackTrace(); + ConsoleLogger.log(Level.WARNING, "The configuration is not a valid YAML file! Please check it with a tool like http://yaml-online-parser.appspot.com/"); + return; + } catch (IOException e) { + e.printStackTrace(); + ConsoleLogger.log(Level.WARNING, "I/O error while reading the configuration. Was the file in use?"); + return; + } catch (Exception e) { + e.printStackTrace(); + ConsoleLogger.log(Level.WARNING, "Unhandled exception while reading the configuration!"); + return; + } + + boolean needsSave = false; + + for (ConfigNode node : ConfigNode.values()) { + if (!config.isSet(node.getPath())) { + needsSave = true; + config.set(node.getPath(), node.getDefaultValue()); + } + } + + // Check the old values. + List nodesToRemove = Arrays.asList( + "vertical-spacing", + "time-format", + "bungee-refresh-seconds", + "using-RedisBungee", + "bungee-online-format", + "bungee-offline-format" + ); + + for (String oldNode : nodesToRemove) { + if (config.isSet(oldNode)) { + config.set(oldNode, null); + needsSave = true; + } + } + + + + if (needsSave) { + config.options().header(Utils.join(new String[] { + ".", + ". Read the tutorial at: http://dev.bukkit.org/bukkit-plugins/holographic-displays/", + ".", + ". Plugin created by filoghost.", + "."}, + "\n")); + config.options().copyHeader(true); + try { + config.save(configFile); + } catch (IOException e) { + e.printStackTrace(); + ConsoleLogger.log(Level.WARNING, "I/O error while saving the configuration. Was the file in use?"); + } + } + + spaceBetweenLines = config.getDouble(ConfigNode.SPACE_BETWEEN_LINES.getPath()); + preciseHologramMovement = config.getBoolean(ConfigNode.PRECISE_HOLOGRAM_MOVEMENT.getPath()); + + updateNotification = config.getBoolean(ConfigNode.UPDATE_NOTIFICATION.getPath()); + + imageSymbol = StringConverter.toReadableFormat(config.getString(ConfigNode.IMAGES_SYMBOL.getPath())); + transparencySymbol = StringConverter.toReadableFormat(config.getString(ConfigNode.TRANSPARENCY_SPACE.getPath())); + bungeeRefreshSeconds = config.getInt(ConfigNode.BUNGEE_REFRESH_SECONDS.getPath()); + useRedisBungee = config.getBoolean(ConfigNode.BUNGEE_USE_REDIS_BUNGEE.getPath()); + + pingerEnable = config.getBoolean(ConfigNode.BUNGEE_USE_FULL_PINGER.getPath()); + pingerTimeout = config.getInt(ConfigNode.BUNGEE_PINGER_TIMEOUT.getPath()); + pingerTrimMotd = config.getBoolean(ConfigNode.BUNGEE_PINGER_TRIM_MOTD.getPath()); + + pingerOfflineMotd = StringConverter.toReadableFormat(config.getString(ConfigNode.BUNGEE_PINGER_OFFLINE_MOTD.getPath())); + pingerStatusOnline = StringConverter.toReadableFormat(config.getString(ConfigNode.BUNGEE_PINGER_ONLINE_FORMAT.getPath())); + pingerStatusOffline = StringConverter.toReadableFormat(config.getString(ConfigNode.BUNGEE_PINGER_OFFLINE_FORMAT.getPath())); + + if (pingerTimeout <= 0) { + pingerTimeout = 100; + } else if (pingerTimeout >= 10000) { + pingerTimeout = 10000; + } + + pingerServers = Utils.newMap(); + + if (pingerEnable) { + for (String singleServer : config.getStringList(ConfigNode.BUNGEE_PINGER_SERVERS.getPath())) { + String[] nameAndAddress = singleServer.split(":", 2); + if (nameAndAddress.length < 2) { + ConsoleLogger.log(Level.WARNING, "The server info \"" + singleServer + "\" is not valid. There should be a name and an address, separated by a colon."); + continue; + } + + String name = nameAndAddress[0].trim(); + String address = nameAndAddress[1].replace(" ", ""); + + String ip; + int port; + + if (address.contains(":")) { + String[] ipAndPort = address.split(":", 2); + ip = ipAndPort[0]; + try { + port = Integer.parseInt(ipAndPort[1]); + } catch (NumberFormatException e) { + ConsoleLogger.log(Level.WARNING, "Invalid port number in the server info \"" + singleServer + "\"."); + continue; + } + } else { + ip = address; + port = 25565; // The default Minecraft port. + } + + pingerServers.put(name, new ServerAddress(ip, port)); + } + } + + ConsoleLogger.setDebugEnabled(config.getBoolean(ConfigNode.DEBUG.getPath())); + + String tempColor = config.getString(ConfigNode.TRANSPARENCY_COLOR.getPath()).replace('&', ChatColor.COLOR_CHAR); + boolean foundColor = false; + for (ChatColor chatColor : ChatColor.values()) { + if (chatColor.toString().equals(tempColor)) { + Configuration.transparencyColor = chatColor; + foundColor = true; + } + } + if (!foundColor) { + Configuration.transparencyColor = ChatColor.GRAY; + ConsoleLogger.log(Level.WARNING, "You didn't set a valid chat color for transparency in the configuration, light gray (&7) will be used."); + } + + try { + timeFormat = new SimpleDateFormat(config.getString(ConfigNode.TIME_FORMAT.getPath())); + timeFormat.setTimeZone(TimeZone.getTimeZone(config.getString(ConfigNode.TIME_ZONE.getPath()))); + } catch (IllegalArgumentException ex) { + timeFormat = new SimpleDateFormat("H:mm"); + ConsoleLogger.log(Level.WARNING, "Time format not valid in the configuration, using the default."); + } + + if (bungeeRefreshSeconds < 1) { + ConsoleLogger.log(Level.WARNING, "The minimum interval for pinging BungeeCord's servers is 1 second. It has been automatically set."); + bungeeRefreshSeconds = 1; + } + + if (bungeeRefreshSeconds > 60) { + ConsoleLogger.log(Level.WARNING, "The maximum interval for pinging BungeeCord's servers is 60 seconds. It has been automatically set."); + bungeeRefreshSeconds = 60; + } + } +} diff --git a/Config/src/main/java/com/gmail/filoghost/holographicdisplays/disk/LocationSerializer.java b/Config/src/main/java/com/gmail/filoghost/holographicdisplays/disk/LocationSerializer.java index 0a03bfb4..2c3fb357 100644 --- a/Config/src/main/java/com/gmail/filoghost/holographicdisplays/disk/LocationSerializer.java +++ b/Config/src/main/java/com/gmail/filoghost/holographicdisplays/disk/LocationSerializer.java @@ -1,55 +1,69 @@ -package com.gmail.filoghost.holographicdisplays.disk; - -import java.text.DecimalFormat; -import java.text.DecimalFormatSymbols; - -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.World; - -import com.gmail.filoghost.holographicdisplays.exception.InvalidFormatException; -import com.gmail.filoghost.holographicdisplays.exception.WorldNotFoundException; - -public class LocationSerializer { - - private static DecimalFormat decimalFormat; - static { - // More precision is not needed at all. - decimalFormat = new DecimalFormat("0.000"); - DecimalFormatSymbols formatSymbols = decimalFormat.getDecimalFormatSymbols(); - formatSymbols.setDecimalSeparator('.'); - decimalFormat.setDecimalFormatSymbols(formatSymbols); - } - - public static Location locationFromString(String input) throws WorldNotFoundException, InvalidFormatException { - if (input == null) { - throw new InvalidFormatException(); - } - - String[] parts = input.split(","); - - if (parts.length != 4) { - throw new InvalidFormatException(); - } - - try { - double x = Double.parseDouble(parts[1].replace(" ", "")); - double y = Double.parseDouble(parts[2].replace(" ", "")); - double z = Double.parseDouble(parts[3].replace(" ", "")); - - World world = Bukkit.getWorld(parts[0].trim()); - if (world == null) { - throw new WorldNotFoundException(parts[0].trim()); - } - - return new Location(world, x, y, z); - - } catch (NumberFormatException ex) { - throw new InvalidFormatException(); - } - } - - public static String locationToString(Location loc) { - return (loc.getWorld().getName() + ", " + decimalFormat.format(loc.getX()) + ", " + decimalFormat.format(loc.getY()) + ", " + decimalFormat.format(loc.getZ())); - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.disk; + +import java.text.DecimalFormat; +import java.text.DecimalFormatSymbols; + +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.World; + +import com.gmail.filoghost.holographicdisplays.exception.InvalidFormatException; +import com.gmail.filoghost.holographicdisplays.exception.WorldNotFoundException; + +public class LocationSerializer { + + private static DecimalFormat decimalFormat; + static { + // More precision is not needed at all. + decimalFormat = new DecimalFormat("0.000"); + DecimalFormatSymbols formatSymbols = decimalFormat.getDecimalFormatSymbols(); + formatSymbols.setDecimalSeparator('.'); + decimalFormat.setDecimalFormatSymbols(formatSymbols); + } + + public static Location locationFromString(String input) throws WorldNotFoundException, InvalidFormatException { + if (input == null) { + throw new InvalidFormatException(); + } + + String[] parts = input.split(","); + + if (parts.length != 4) { + throw new InvalidFormatException(); + } + + try { + double x = Double.parseDouble(parts[1].replace(" ", "")); + double y = Double.parseDouble(parts[2].replace(" ", "")); + double z = Double.parseDouble(parts[3].replace(" ", "")); + + World world = Bukkit.getWorld(parts[0].trim()); + if (world == null) { + throw new WorldNotFoundException(parts[0].trim()); + } + + return new Location(world, x, y, z); + + } catch (NumberFormatException ex) { + throw new InvalidFormatException(); + } + } + + public static String locationToString(Location loc) { + return (loc.getWorld().getName() + ", " + decimalFormat.format(loc.getX()) + ", " + decimalFormat.format(loc.getY()) + ", " + decimalFormat.format(loc.getZ())); + } +} diff --git a/Config/src/main/java/com/gmail/filoghost/holographicdisplays/disk/ServerAddress.java b/Config/src/main/java/com/gmail/filoghost/holographicdisplays/disk/ServerAddress.java index eb951620..cab83dc2 100644 --- a/Config/src/main/java/com/gmail/filoghost/holographicdisplays/disk/ServerAddress.java +++ b/Config/src/main/java/com/gmail/filoghost/holographicdisplays/disk/ServerAddress.java @@ -1,26 +1,40 @@ -package com.gmail.filoghost.holographicdisplays.disk; - -public class ServerAddress { - - private String ip; - private int port; - - public ServerAddress(String ip, int port) { - this.ip = ip; - this.port = port; - } - - public String getAddress() { - return ip; - } - - public int getPort() { - return port; - } - - @Override - public String toString() { - return ip + ":" + port; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.disk; + +public class ServerAddress { + + private String ip; + private int port; + + public ServerAddress(String ip, int port) { + this.ip = ip; + this.port = port; + } + + public String getAddress() { + return ip; + } + + public int getPort() { + return port; + } + + @Override + public String toString() { + return ip + ":" + port; + } + +} diff --git a/Config/src/main/java/com/gmail/filoghost/holographicdisplays/disk/StringConverter.java b/Config/src/main/java/com/gmail/filoghost/holographicdisplays/disk/StringConverter.java index 12644387..9ee828d3 100644 --- a/Config/src/main/java/com/gmail/filoghost/holographicdisplays/disk/StringConverter.java +++ b/Config/src/main/java/com/gmail/filoghost/holographicdisplays/disk/StringConverter.java @@ -1,28 +1,42 @@ -package com.gmail.filoghost.holographicdisplays.disk; - -import org.bukkit.ChatColor; - -public class StringConverter { - - public static String toReadableFormat(String input) { - if (input == null) { - return null; - } - - input = UnicodeSymbols.placeholdersToSymbols(input); - input = ChatColor.translateAlternateColorCodes('&', input); - return input; - } - - - public static String toSaveableFormat(String input) { - if (input == null) { - return null; - } - - input = UnicodeSymbols.symbolsToPlaceholders(input); - input = input.replace("§", "&"); - return input; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.disk; + +import org.bukkit.ChatColor; + +public class StringConverter { + + public static String toReadableFormat(String input) { + if (input == null) { + return null; + } + + input = UnicodeSymbols.placeholdersToSymbols(input); + input = ChatColor.translateAlternateColorCodes('&', input); + return input; + } + + + public static String toSaveableFormat(String input) { + if (input == null) { + return null; + } + + input = UnicodeSymbols.symbolsToPlaceholders(input); + input = input.replace("§", "&"); + return input; + } + +} diff --git a/Config/src/main/java/com/gmail/filoghost/holographicdisplays/disk/UnicodeSymbols.java b/Config/src/main/java/com/gmail/filoghost/holographicdisplays/disk/UnicodeSymbols.java index e28fc010..d299175f 100644 --- a/Config/src/main/java/com/gmail/filoghost/holographicdisplays/disk/UnicodeSymbols.java +++ b/Config/src/main/java/com/gmail/filoghost/holographicdisplays/disk/UnicodeSymbols.java @@ -1,102 +1,116 @@ -package com.gmail.filoghost.holographicdisplays.disk; - -import java.io.File; -import java.io.IOException; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.logging.Level; - -import org.apache.commons.lang.StringEscapeUtils; -import org.bukkit.plugin.Plugin; - -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.FileUtils; - -public class UnicodeSymbols { - - private static Map placeholders = new HashMap(); - - public static void load(Plugin plugin) { - placeholders.clear(); - - File file = new File(plugin.getDataFolder(), "symbols.yml"); - - if (!file.exists()) { - plugin.getDataFolder().mkdirs(); - plugin.saveResource("symbols.yml", true); - } - - List lines; - try { - lines = FileUtils.readLines(file); - } catch (IOException e) { - ConsoleLogger.log(Level.WARNING, "I/O error while reading symbols.yml. Was the file in use?", e); - return; - } catch (Exception e) { - ConsoleLogger.log(Level.WARNING, "Unhandled exception while reading symbols.yml!", e); - return; - } - - for (String line : lines) { - - // Comment or empty line. - if (line.length() == 0 || line.startsWith("#")) { - continue; - } - - if (!line.contains(":")) { - ConsoleLogger.log(Level.WARNING, "Unable to parse a line(" + line + ") from symbols.yml: it must contain ':' to separate the placeholder and the replacement."); - continue; - } - - int indexOf = line.indexOf(':'); - String placeholder = unquote(line.substring(0, indexOf).trim()); - String replacement = StringEscapeUtils.unescapeJava(unquote(line.substring(indexOf + 1, line.length()).trim())); - - if (placeholder.isEmpty() || replacement.isEmpty()) { - ConsoleLogger.log(Level.WARNING, "Unable to parse a line(" + line + ") from symbols.yml: the placeholder and the replacement must have both at least 1 character."); - continue; - } - - if (placeholder.length() > 30) { - ConsoleLogger.log(Level.WARNING, "Unable to parse a line(" + line + ") from symbols.yml: the placeholder cannot be longer than 30 characters."); - continue; - } - - placeholders.put(placeholder, replacement); - } - } - - - protected static String placeholdersToSymbols(String input) { - for (Entry entry : placeholders.entrySet()) { - input = input.replace(entry.getKey(), entry.getValue()); - } - return input; - } - - - protected static String symbolsToPlaceholders(String input) { - for (Entry entry : placeholders.entrySet()) { - input = input.replace(entry.getValue(), entry.getKey()); - } - return input; - } - - - private static String unquote(String input) { - if (input.length() < 2) { - // Cannot be quoted. - return input; - } - if (input.startsWith("'") && input.endsWith("'")) { - return input.substring(1, input.length() - 1); - } else if (input.startsWith("\"") && input.endsWith("\"")) { - return input.substring(1, input.length() - 1); - } - - return input; - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.disk; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.logging.Level; + +import org.apache.commons.lang.StringEscapeUtils; +import org.bukkit.plugin.Plugin; + +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.FileUtils; + +public class UnicodeSymbols { + + private static Map placeholders = new HashMap(); + + public static void load(Plugin plugin) { + placeholders.clear(); + + File file = new File(plugin.getDataFolder(), "symbols.yml"); + + if (!file.exists()) { + plugin.getDataFolder().mkdirs(); + plugin.saveResource("symbols.yml", true); + } + + List lines; + try { + lines = FileUtils.readLines(file); + } catch (IOException e) { + ConsoleLogger.log(Level.WARNING, "I/O error while reading symbols.yml. Was the file in use?", e); + return; + } catch (Exception e) { + ConsoleLogger.log(Level.WARNING, "Unhandled exception while reading symbols.yml!", e); + return; + } + + for (String line : lines) { + + // Comment or empty line. + if (line.length() == 0 || line.startsWith("#")) { + continue; + } + + if (!line.contains(":")) { + ConsoleLogger.log(Level.WARNING, "Unable to parse a line(" + line + ") from symbols.yml: it must contain ':' to separate the placeholder and the replacement."); + continue; + } + + int indexOf = line.indexOf(':'); + String placeholder = unquote(line.substring(0, indexOf).trim()); + String replacement = StringEscapeUtils.unescapeJava(unquote(line.substring(indexOf + 1, line.length()).trim())); + + if (placeholder.isEmpty() || replacement.isEmpty()) { + ConsoleLogger.log(Level.WARNING, "Unable to parse a line(" + line + ") from symbols.yml: the placeholder and the replacement must have both at least 1 character."); + continue; + } + + if (placeholder.length() > 30) { + ConsoleLogger.log(Level.WARNING, "Unable to parse a line(" + line + ") from symbols.yml: the placeholder cannot be longer than 30 characters."); + continue; + } + + placeholders.put(placeholder, replacement); + } + } + + + protected static String placeholdersToSymbols(String input) { + for (Entry entry : placeholders.entrySet()) { + input = input.replace(entry.getKey(), entry.getValue()); + } + return input; + } + + + protected static String symbolsToPlaceholders(String input) { + for (Entry entry : placeholders.entrySet()) { + input = input.replace(entry.getValue(), entry.getKey()); + } + return input; + } + + + private static String unquote(String input) { + if (input.length() < 2) { + // Cannot be quoted. + return input; + } + if (input.startsWith("'") && input.endsWith("'")) { + return input.substring(1, input.length() - 1); + } else if (input.startsWith("\"") && input.endsWith("\"")) { + return input.substring(1, input.length() - 1); + } + + return input; + } +} diff --git a/Config/src/main/java/com/gmail/filoghost/holographicdisplays/exception/InvalidFormatException.java b/Config/src/main/java/com/gmail/filoghost/holographicdisplays/exception/InvalidFormatException.java index 0cb2db30..25752172 100644 --- a/Config/src/main/java/com/gmail/filoghost/holographicdisplays/exception/InvalidFormatException.java +++ b/Config/src/main/java/com/gmail/filoghost/holographicdisplays/exception/InvalidFormatException.java @@ -1,7 +1,21 @@ -package com.gmail.filoghost.holographicdisplays.exception; - -public class InvalidFormatException extends Exception { - - private static final long serialVersionUID = 1L; - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.exception; + +public class InvalidFormatException extends Exception { + + private static final long serialVersionUID = 1L; + +} diff --git a/Config/src/main/java/com/gmail/filoghost/holographicdisplays/exception/WorldNotFoundException.java b/Config/src/main/java/com/gmail/filoghost/holographicdisplays/exception/WorldNotFoundException.java index b8d96694..569a6d43 100644 --- a/Config/src/main/java/com/gmail/filoghost/holographicdisplays/exception/WorldNotFoundException.java +++ b/Config/src/main/java/com/gmail/filoghost/holographicdisplays/exception/WorldNotFoundException.java @@ -1,11 +1,25 @@ -package com.gmail.filoghost.holographicdisplays.exception; - -public class WorldNotFoundException extends Exception { - - private static final long serialVersionUID = 1L; - - public WorldNotFoundException(String message) { - super(message); - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.exception; + +public class WorldNotFoundException extends Exception { + + private static final long serialVersionUID = 1L; + + public WorldNotFoundException(String message) { + super(message); + } + +} diff --git a/Examples/DeathHolograms/src/main/java/com/gmail/filoghost/test/DeathHolograms.java b/Examples/DeathHolograms/src/main/java/com/gmail/filoghost/test/DeathHolograms.java index 7b37aa76..14708d42 100644 --- a/Examples/DeathHolograms/src/main/java/com/gmail/filoghost/test/DeathHolograms.java +++ b/Examples/DeathHolograms/src/main/java/com/gmail/filoghost/test/DeathHolograms.java @@ -1,3 +1,17 @@ +/* + * 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 . + */ package com.gmail.filoghost.test; import java.text.SimpleDateFormat; diff --git a/Examples/PowerUps/src/main/java/com/gmail/filoghost/test/PowerUps.java b/Examples/PowerUps/src/main/java/com/gmail/filoghost/test/PowerUps.java index 06b696db..52a5471e 100644 --- a/Examples/PowerUps/src/main/java/com/gmail/filoghost/test/PowerUps.java +++ b/Examples/PowerUps/src/main/java/com/gmail/filoghost/test/PowerUps.java @@ -1,3 +1,17 @@ +/* + * 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 . + */ package com.gmail.filoghost.test; import org.bukkit.Bukkit; diff --git a/JavaCompat/src/main/java/java/lang/StackWalker.java b/JavaCompat/src/main/java/java/lang/StackWalker.java index 858de4d1..e31e41a1 100644 --- a/JavaCompat/src/main/java/java/lang/StackWalker.java +++ b/JavaCompat/src/main/java/java/lang/StackWalker.java @@ -1,3 +1,17 @@ +/* + * 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 . + */ package java.lang; import java.util.function.Function; diff --git a/JavaCompat/src/main/java/java/util/Optional.java b/JavaCompat/src/main/java/java/util/Optional.java index 8ea90908..b585a1a1 100644 --- a/JavaCompat/src/main/java/java/util/Optional.java +++ b/JavaCompat/src/main/java/java/util/Optional.java @@ -1,3 +1,17 @@ +/* + * 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 . + */ package java.util; public class Optional { diff --git a/JavaCompat/src/main/java/java/util/function/Function.java b/JavaCompat/src/main/java/java/util/function/Function.java index 5bc7a22e..2bb19d1b 100644 --- a/JavaCompat/src/main/java/java/util/function/Function.java +++ b/JavaCompat/src/main/java/java/util/function/Function.java @@ -1,3 +1,17 @@ +/* + * 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 . + */ package java.util.function; public interface Function { diff --git a/JavaCompat/src/main/java/java/util/stream/Stream.java b/JavaCompat/src/main/java/java/util/stream/Stream.java index 5989a99b..fdbb4245 100644 --- a/JavaCompat/src/main/java/java/util/stream/Stream.java +++ b/JavaCompat/src/main/java/java/util/stream/Stream.java @@ -1,3 +1,17 @@ +/* + * 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 . + */ package java.util.stream; import java.util.Optional; @@ -10,4 +24,4 @@ public interface Stream { public Optional findFirst(); -} \ No newline at end of file +} diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 00000000..871ce8e6 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + 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 . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. \ No newline at end of file diff --git a/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/ItemPickupManager.java b/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/ItemPickupManager.java index 4ebe55cc..f16574db 100644 --- a/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/ItemPickupManager.java +++ b/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/ItemPickupManager.java @@ -1,12 +1,26 @@ -package com.gmail.filoghost.holographicdisplays.nms.interfaces; - -import org.bukkit.entity.Player; - -import com.gmail.filoghost.holographicdisplays.api.Hologram; -import com.gmail.filoghost.holographicdisplays.api.handler.PickupHandler; - -public interface ItemPickupManager { - - public void handleItemLinePickup(Player player, PickupHandler pickupHandler, Hologram hologram); - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.interfaces; + +import org.bukkit.entity.Player; + +import com.gmail.filoghost.holographicdisplays.api.Hologram; +import com.gmail.filoghost.holographicdisplays.api.handler.PickupHandler; + +public interface ItemPickupManager { + + public void handleItemLinePickup(Player player, PickupHandler pickupHandler, Hologram hologram); + +} diff --git a/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/NMSManager.java b/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/NMSManager.java index bbc1aa59..d48ec5f6 100644 --- a/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/NMSManager.java +++ b/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/NMSManager.java @@ -1,27 +1,41 @@ -package com.gmail.filoghost.holographicdisplays.nms.interfaces; - -import org.bukkit.inventory.ItemStack; - -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; -import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSSlime; - -public interface NMSManager { - - // A method to register all the custom entities of the plugin, it may fail. - public void setup() throws Exception; - - public NMSArmorStand spawnNMSArmorStand(org.bukkit.World world, double x, double y, double z, HologramLine parentPiece); - - public NMSItem spawnNMSItem(org.bukkit.World bukkitWorld, double x, double y, double z, ItemLine parentPiece, ItemStack stack, ItemPickupManager itemPickupManager); - - public NMSSlime spawnNMSSlime(org.bukkit.World bukkitWorld, double x, double y, double z, HologramLine parentPiece); - - public boolean isNMSEntityBase(org.bukkit.entity.Entity bukkitEntity); - - public NMSEntityBase getNMSEntityBase(org.bukkit.entity.Entity bukkitEntity); - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.interfaces; + +import org.bukkit.inventory.ItemStack; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; +import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSSlime; + +public interface NMSManager { + + // A method to register all the custom entities of the plugin, it may fail. + public void setup() throws Exception; + + public NMSArmorStand spawnNMSArmorStand(org.bukkit.World world, double x, double y, double z, HologramLine parentPiece); + + public NMSItem spawnNMSItem(org.bukkit.World bukkitWorld, double x, double y, double z, ItemLine parentPiece, ItemStack stack, ItemPickupManager itemPickupManager); + + public NMSSlime spawnNMSSlime(org.bukkit.World bukkitWorld, double x, double y, double z, HologramLine parentPiece); + + public boolean isNMSEntityBase(org.bukkit.entity.Entity bukkitEntity); + + public NMSEntityBase getNMSEntityBase(org.bukkit.entity.Entity bukkitEntity); + +} diff --git a/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/entity/NMSArmorStand.java b/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/entity/NMSArmorStand.java index 9b887a4d..981a412e 100644 --- a/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/entity/NMSArmorStand.java +++ b/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/entity/NMSArmorStand.java @@ -1,5 +1,19 @@ -package com.gmail.filoghost.holographicdisplays.nms.interfaces.entity; - -public interface NMSArmorStand extends NMSNameable { - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.interfaces.entity; + +public interface NMSArmorStand extends NMSNameable { + +} diff --git a/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/entity/NMSCanMount.java b/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/entity/NMSCanMount.java index 74341696..0814bd20 100644 --- a/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/entity/NMSCanMount.java +++ b/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/entity/NMSCanMount.java @@ -1,8 +1,22 @@ -package com.gmail.filoghost.holographicdisplays.nms.interfaces.entity; - -public interface NMSCanMount extends NMSEntityBase { - - // Sets the passenger of this entity through NMS. - public void setPassengerOfNMS(NMSEntityBase vehicleBase); - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.interfaces.entity; + +public interface NMSCanMount extends NMSEntityBase { + + // Sets the passenger of this entity through NMS. + public void setPassengerOfNMS(NMSEntityBase vehicleBase); + +} diff --git a/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/entity/NMSEntityBase.java b/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/entity/NMSEntityBase.java index 38577e98..8a136a52 100644 --- a/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/entity/NMSEntityBase.java +++ b/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/entity/NMSEntityBase.java @@ -1,31 +1,45 @@ -package com.gmail.filoghost.holographicdisplays.nms.interfaces.entity; - -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; - -/** - * An interface to represent a custom NMS entity being part of a hologram. - */ -public interface NMSEntityBase { - - // Returns the linked CraftHologramLine, all the entities are part of a piece. Should never be null. - public HologramLine getHologramLine(); - - // Sets if the entity should tick or not. - public void setLockTick(boolean lock); - - // Sets the location through NMS. - public void setLocationNMS(double x, double y, double z); - - // Returns if the entity is dead through NMS. - public boolean isDeadNMS(); - - // Kills the entity through NMS. - public void killEntityNMS(); - - // The entity ID. - public int getIdNMS(); - - // Returns the bukkit entity. - public org.bukkit.entity.Entity getBukkitEntityNMS(); - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.interfaces.entity; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; + +/** + * An interface to represent a custom NMS entity being part of a hologram. + */ +public interface NMSEntityBase { + + // Returns the linked CraftHologramLine, all the entities are part of a piece. Should never be null. + public HologramLine getHologramLine(); + + // Sets if the entity should tick or not. + public void setLockTick(boolean lock); + + // Sets the location through NMS. + public void setLocationNMS(double x, double y, double z); + + // Returns if the entity is dead through NMS. + public boolean isDeadNMS(); + + // Kills the entity through NMS. + public void killEntityNMS(); + + // The entity ID. + public int getIdNMS(); + + // Returns the bukkit entity. + public org.bukkit.entity.Entity getBukkitEntityNMS(); + +} diff --git a/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/entity/NMSItem.java b/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/entity/NMSItem.java index 59657b43..b9a06328 100644 --- a/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/entity/NMSItem.java +++ b/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/entity/NMSItem.java @@ -1,16 +1,30 @@ -package com.gmail.filoghost.holographicdisplays.nms.interfaces.entity; - -import org.bukkit.inventory.ItemStack; - -public interface NMSItem extends NMSEntityBase, NMSCanMount { - - // Sets the bukkit ItemStack for this item. - public void setItemStackNMS(ItemStack stack); - - // Sets if this item can be picked up by players. - public void allowPickup(boolean pickup); - - // The raw NMS ItemStack object. - public Object getRawItemStack(); - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.interfaces.entity; + +import org.bukkit.inventory.ItemStack; + +public interface NMSItem extends NMSEntityBase, NMSCanMount { + + // Sets the bukkit ItemStack for this item. + public void setItemStackNMS(ItemStack stack); + + // Sets if this item can be picked up by players. + public void allowPickup(boolean pickup); + + // The raw NMS ItemStack object. + public Object getRawItemStack(); + +} diff --git a/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/entity/NMSNameable.java b/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/entity/NMSNameable.java index 2ad365e9..6e4e3877 100644 --- a/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/entity/NMSNameable.java +++ b/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/entity/NMSNameable.java @@ -1,11 +1,25 @@ -package com.gmail.filoghost.holographicdisplays.nms.interfaces.entity; - -public interface NMSNameable extends NMSEntityBase { - - // Sets a custom name for this entity. - public void setCustomNameNMS(String name); - - // Returns the custom name of this entity. - public String getCustomNameNMS(); - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.interfaces.entity; + +public interface NMSNameable extends NMSEntityBase { + + // Sets a custom name for this entity. + public void setCustomNameNMS(String name); + + // Returns the custom name of this entity. + public String getCustomNameNMS(); + +} diff --git a/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/entity/NMSSlime.java b/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/entity/NMSSlime.java index 0ed43192..10d31cc2 100644 --- a/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/entity/NMSSlime.java +++ b/NMS/Interfaces/src/main/java/com/gmail/filoghost/holographicdisplays/nms/interfaces/entity/NMSSlime.java @@ -1,5 +1,19 @@ -package com.gmail.filoghost.holographicdisplays.nms.interfaces.entity; - -public interface NMSSlime extends NMSEntityBase, NMSCanMount { - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.interfaces.entity; + +public interface NMSSlime extends NMSEntityBase, NMSCanMount { + +} diff --git a/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/CraftNMSArmorStand.java b/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/CraftNMSArmorStand.java index 9c3a447b..22c41ca1 100644 --- a/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/CraftNMSArmorStand.java +++ b/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/CraftNMSArmorStand.java @@ -1,78 +1,92 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_10_R1; - -import java.util.Collection; - -import org.bukkit.EntityEffect; -import org.bukkit.Location; -import org.bukkit.craftbukkit.v1_10_R1.CraftServer; -import org.bukkit.craftbukkit.v1_10_R1.entity.CraftArmorStand; -import org.bukkit.entity.Entity; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -import org.bukkit.inventory.ItemStack; -import org.bukkit.potion.PotionEffect; -import org.bukkit.util.EulerAngle; -import org.bukkit.util.Vector; - -public class CraftNMSArmorStand extends CraftArmorStand { - - public CraftNMSArmorStand(CraftServer server, EntityNMSArmorStand entity) { - super(server, entity); - } - - // Disallow all the bukkit methods. - - @Override - public void remove() { - // Cannot be removed, this is the most important to override. - } - - // Methods from Armor stand class - @Override public void setArms(boolean arms) { } - @Override public void setBasePlate(boolean basePlate) { } - @Override public void setBodyPose(EulerAngle pose) { } - @Override public void setBoots(ItemStack item) { } - @Override public void setChestplate(ItemStack item) { } - @Override public void setHeadPose(EulerAngle pose) { } - @Override public void setHelmet(ItemStack item) { } - @Override public void setItemInHand(ItemStack item) { } - @Override public void setLeftArmPose(EulerAngle pose) { } - @Override public void setLeftLegPose(EulerAngle pose) { } - @Override public void setLeggings(ItemStack item) { } - @Override public void setRightArmPose(EulerAngle pose) { } - @Override public void setRightLegPose(EulerAngle pose) { } - @Override public void setSmall(boolean small) { } - @Override public void setVisible(boolean visible) { } - @Override public void setMarker(boolean marker) { } - - // Methods from LivingEntity class - @Override public boolean addPotionEffect(PotionEffect effect) { return false; } - @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } - @Override public boolean addPotionEffects(Collection effects) { return false; } - @Override public void setRemoveWhenFarAway(boolean remove) { } - @Override public void setAI(boolean ai) { } - @Override public void setCanPickupItems(boolean pickup) { } - @Override public void setCollidable(boolean collidable) { } - @Override public void setGliding(boolean gliding) { } - @Override public boolean setLeashHolder(Entity holder) { return false; } - - // Methods from Entity - @Override public void setVelocity(Vector vel) { } - @Override public boolean teleport(Location loc) { return false; } - @Override public boolean teleport(Entity entity) { return false; } - @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } - @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } - @Override public void setFireTicks(int ticks) { } - @Override public boolean setPassenger(Entity entity) { return false; } - @Override public boolean eject() { return false; } - @Override public boolean leaveVehicle() { return false; } - @Override public void playEffect(EntityEffect effect) { } - @Override public void setCustomName(String name) { } - @Override public void setCustomNameVisible(boolean flag) { } - @Override public void setGlowing(boolean flag) { } - @Override public void setGravity(boolean gravity) { } - @Override public void setInvulnerable(boolean flag) { } - @Override public void setMomentum(Vector value) { } - @Override public void setSilent(boolean flag) { } - @Override public void setTicksLived(int value) { } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_10_R1; + +import java.util.Collection; + +import org.bukkit.EntityEffect; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_10_R1.CraftServer; +import org.bukkit.craftbukkit.v1_10_R1.entity.CraftArmorStand; +import org.bukkit.entity.Entity; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import org.bukkit.inventory.ItemStack; +import org.bukkit.potion.PotionEffect; +import org.bukkit.util.EulerAngle; +import org.bukkit.util.Vector; + +public class CraftNMSArmorStand extends CraftArmorStand { + + public CraftNMSArmorStand(CraftServer server, EntityNMSArmorStand entity) { + super(server, entity); + } + + // Disallow all the bukkit methods. + + @Override + public void remove() { + // Cannot be removed, this is the most important to override. + } + + // Methods from Armor stand class + @Override public void setArms(boolean arms) { } + @Override public void setBasePlate(boolean basePlate) { } + @Override public void setBodyPose(EulerAngle pose) { } + @Override public void setBoots(ItemStack item) { } + @Override public void setChestplate(ItemStack item) { } + @Override public void setHeadPose(EulerAngle pose) { } + @Override public void setHelmet(ItemStack item) { } + @Override public void setItemInHand(ItemStack item) { } + @Override public void setLeftArmPose(EulerAngle pose) { } + @Override public void setLeftLegPose(EulerAngle pose) { } + @Override public void setLeggings(ItemStack item) { } + @Override public void setRightArmPose(EulerAngle pose) { } + @Override public void setRightLegPose(EulerAngle pose) { } + @Override public void setSmall(boolean small) { } + @Override public void setVisible(boolean visible) { } + @Override public void setMarker(boolean marker) { } + + // Methods from LivingEntity class + @Override public boolean addPotionEffect(PotionEffect effect) { return false; } + @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } + @Override public boolean addPotionEffects(Collection effects) { return false; } + @Override public void setRemoveWhenFarAway(boolean remove) { } + @Override public void setAI(boolean ai) { } + @Override public void setCanPickupItems(boolean pickup) { } + @Override public void setCollidable(boolean collidable) { } + @Override public void setGliding(boolean gliding) { } + @Override public boolean setLeashHolder(Entity holder) { return false; } + + // Methods from Entity + @Override public void setVelocity(Vector vel) { } + @Override public boolean teleport(Location loc) { return false; } + @Override public boolean teleport(Entity entity) { return false; } + @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } + @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } + @Override public void setFireTicks(int ticks) { } + @Override public boolean setPassenger(Entity entity) { return false; } + @Override public boolean eject() { return false; } + @Override public boolean leaveVehicle() { return false; } + @Override public void playEffect(EntityEffect effect) { } + @Override public void setCustomName(String name) { } + @Override public void setCustomNameVisible(boolean flag) { } + @Override public void setGlowing(boolean flag) { } + @Override public void setGravity(boolean gravity) { } + @Override public void setInvulnerable(boolean flag) { } + @Override public void setMomentum(Vector value) { } + @Override public void setSilent(boolean flag) { } + @Override public void setTicksLived(int value) { } + +} diff --git a/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/CraftNMSItem.java b/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/CraftNMSItem.java index 6511c48f..fba07c57 100644 --- a/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/CraftNMSItem.java +++ b/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/CraftNMSItem.java @@ -1,49 +1,63 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_10_R1; - -import org.bukkit.EntityEffect; -import org.bukkit.Location; -import org.bukkit.craftbukkit.v1_10_R1.CraftServer; -import org.bukkit.craftbukkit.v1_10_R1.entity.CraftItem; -import org.bukkit.entity.Entity; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -import org.bukkit.inventory.ItemStack; -import org.bukkit.util.Vector; - -public class CraftNMSItem extends CraftItem { - - public CraftNMSItem(CraftServer server, EntityNMSItem entity) { - super(server, entity); - } - - // Disallow all the bukkit methods. - - @Override - public void remove() { - // Cannot be removed, this is the most important to override. - } - - // Methods from Entity - @Override public void setVelocity(Vector vel) { } - @Override public boolean teleport(Location loc) { return false; } - @Override public boolean teleport(Entity entity) { return false; } - @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } - @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } - @Override public void setFireTicks(int ticks) { } - @Override public boolean setPassenger(Entity entity) { return false; } - @Override public boolean eject() { return false; } - @Override public boolean leaveVehicle() { return false; } - @Override public void playEffect(EntityEffect effect) { } - @Override public void setCustomName(String name) { } - @Override public void setCustomNameVisible(boolean flag) { } - @Override public void setGlowing(boolean flag) { } - @Override public void setGravity(boolean gravity) { } - @Override public void setInvulnerable(boolean flag) { } - @Override public void setMomentum(Vector value) { } - @Override public void setSilent(boolean flag) { } - @Override public void setTicksLived(int value) { } - - // Methods from Item - @Override public void setItemStack(ItemStack stack) { } - @Override public void setPickupDelay(int delay) { } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_10_R1; + +import org.bukkit.EntityEffect; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_10_R1.CraftServer; +import org.bukkit.craftbukkit.v1_10_R1.entity.CraftItem; +import org.bukkit.entity.Entity; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import org.bukkit.inventory.ItemStack; +import org.bukkit.util.Vector; + +public class CraftNMSItem extends CraftItem { + + public CraftNMSItem(CraftServer server, EntityNMSItem entity) { + super(server, entity); + } + + // Disallow all the bukkit methods. + + @Override + public void remove() { + // Cannot be removed, this is the most important to override. + } + + // Methods from Entity + @Override public void setVelocity(Vector vel) { } + @Override public boolean teleport(Location loc) { return false; } + @Override public boolean teleport(Entity entity) { return false; } + @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } + @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } + @Override public void setFireTicks(int ticks) { } + @Override public boolean setPassenger(Entity entity) { return false; } + @Override public boolean eject() { return false; } + @Override public boolean leaveVehicle() { return false; } + @Override public void playEffect(EntityEffect effect) { } + @Override public void setCustomName(String name) { } + @Override public void setCustomNameVisible(boolean flag) { } + @Override public void setGlowing(boolean flag) { } + @Override public void setGravity(boolean gravity) { } + @Override public void setInvulnerable(boolean flag) { } + @Override public void setMomentum(Vector value) { } + @Override public void setSilent(boolean flag) { } + @Override public void setTicksLived(int value) { } + + // Methods from Item + @Override public void setItemStack(ItemStack stack) { } + @Override public void setPickupDelay(int delay) { } + +} diff --git a/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/CraftNMSSlime.java b/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/CraftNMSSlime.java index b141c6bb..e06c6252 100644 --- a/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/CraftNMSSlime.java +++ b/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/CraftNMSSlime.java @@ -1,61 +1,75 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_10_R1; - -import java.util.Collection; - -import org.bukkit.EntityEffect; -import org.bukkit.Location; -import org.bukkit.craftbukkit.v1_10_R1.CraftServer; -import org.bukkit.craftbukkit.v1_10_R1.entity.CraftSlime; -import org.bukkit.entity.Entity; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -import org.bukkit.potion.PotionEffect; -import org.bukkit.util.Vector; - -public class CraftNMSSlime extends CraftSlime { - - public CraftNMSSlime(CraftServer server, EntityNMSSlime entity) { - super(server, entity); - } - - // Disallow all the bukkit methods. - - @Override - public void remove() { - // Cannot be removed, this is the most important to override. - } - - // Methods from LivingEntity class - @Override public boolean addPotionEffect(PotionEffect effect) { return false; } - @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } - @Override public boolean addPotionEffects(Collection effects) { return false; } - @Override public void setRemoveWhenFarAway(boolean remove) { } - @Override public void setAI(boolean ai) { } - @Override public void setCanPickupItems(boolean pickup) { } - @Override public void setCollidable(boolean collidable) { } - @Override public void setGliding(boolean gliding) { } - @Override public boolean setLeashHolder(Entity holder) { return false; } - - // Methods from Entity - @Override public void setVelocity(Vector vel) { } - @Override public boolean teleport(Location loc) { return false; } - @Override public boolean teleport(Entity entity) { return false; } - @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } - @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } - @Override public void setFireTicks(int ticks) { } - @Override public boolean setPassenger(Entity entity) { return false; } - @Override public boolean eject() { return false; } - @Override public boolean leaveVehicle() { return false; } - @Override public void playEffect(EntityEffect effect) { } - @Override public void setCustomName(String name) { } - @Override public void setCustomNameVisible(boolean flag) { } - @Override public void setGlowing(boolean flag) { } - @Override public void setGravity(boolean gravity) { } - @Override public void setInvulnerable(boolean flag) { } - @Override public void setMomentum(Vector value) { } - @Override public void setSilent(boolean flag) { } - @Override public void setTicksLived(int value) { } - - // Methods from Slime - @Override public void setSize(int size) { } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_10_R1; + +import java.util.Collection; + +import org.bukkit.EntityEffect; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_10_R1.CraftServer; +import org.bukkit.craftbukkit.v1_10_R1.entity.CraftSlime; +import org.bukkit.entity.Entity; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import org.bukkit.potion.PotionEffect; +import org.bukkit.util.Vector; + +public class CraftNMSSlime extends CraftSlime { + + public CraftNMSSlime(CraftServer server, EntityNMSSlime entity) { + super(server, entity); + } + + // Disallow all the bukkit methods. + + @Override + public void remove() { + // Cannot be removed, this is the most important to override. + } + + // Methods from LivingEntity class + @Override public boolean addPotionEffect(PotionEffect effect) { return false; } + @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } + @Override public boolean addPotionEffects(Collection effects) { return false; } + @Override public void setRemoveWhenFarAway(boolean remove) { } + @Override public void setAI(boolean ai) { } + @Override public void setCanPickupItems(boolean pickup) { } + @Override public void setCollidable(boolean collidable) { } + @Override public void setGliding(boolean gliding) { } + @Override public boolean setLeashHolder(Entity holder) { return false; } + + // Methods from Entity + @Override public void setVelocity(Vector vel) { } + @Override public boolean teleport(Location loc) { return false; } + @Override public boolean teleport(Entity entity) { return false; } + @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } + @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } + @Override public void setFireTicks(int ticks) { } + @Override public boolean setPassenger(Entity entity) { return false; } + @Override public boolean eject() { return false; } + @Override public boolean leaveVehicle() { return false; } + @Override public void playEffect(EntityEffect effect) { } + @Override public void setCustomName(String name) { } + @Override public void setCustomNameVisible(boolean flag) { } + @Override public void setGlowing(boolean flag) { } + @Override public void setGravity(boolean gravity) { } + @Override public void setInvulnerable(boolean flag) { } + @Override public void setMomentum(Vector value) { } + @Override public void setSilent(boolean flag) { } + @Override public void setTicksLived(int value) { } + + // Methods from Slime + @Override public void setSize(int size) { } + +} diff --git a/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/EntityNMSArmorStand.java b/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/EntityNMSArmorStand.java index 6b5aab3a..71e07bae 100644 --- a/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/EntityNMSArmorStand.java +++ b/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/EntityNMSArmorStand.java @@ -1,231 +1,245 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_10_R1; - -import org.bukkit.craftbukkit.v1_10_R1.entity.CraftEntity; - -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; -import com.gmail.filoghost.holographicdisplays.disk.Configuration; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; -import com.gmail.filoghost.holographicdisplays.util.Utils; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectionUtils; - -import net.minecraft.server.v1_10_R1.AxisAlignedBB; -import net.minecraft.server.v1_10_R1.DamageSource; -import net.minecraft.server.v1_10_R1.EntityArmorStand; -import net.minecraft.server.v1_10_R1.EntityHuman; -import net.minecraft.server.v1_10_R1.EntityPlayer; -import net.minecraft.server.v1_10_R1.EnumHand; -import net.minecraft.server.v1_10_R1.EnumInteractionResult; -import net.minecraft.server.v1_10_R1.EnumItemSlot; -import net.minecraft.server.v1_10_R1.ItemStack; -import net.minecraft.server.v1_10_R1.NBTTagCompound; -import net.minecraft.server.v1_10_R1.PacketPlayOutEntityTeleport; -import net.minecraft.server.v1_10_R1.SoundEffect; -import net.minecraft.server.v1_10_R1.Vec3D; -import net.minecraft.server.v1_10_R1.World; - -public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorStand { - - private boolean lockTick; - private HologramLine parentPiece; - - public EntityNMSArmorStand(World world, HologramLine parentPiece) { - super(world); - super.setInvisible(true); - super.setSmall(true); - super.setArms(false); - super.setNoGravity(true); - super.setBasePlate(true); - super.setMarker(true); - super.collides = false; - this.parentPiece = parentPiece; - forceSetBoundingBox(new NullBoundingBox()); - } - - - @Override - public void b(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public boolean c(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public boolean d(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public NBTTagCompound e(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return nbttagcompound; - } - - @Override - public void f(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public void a(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public boolean isInvulnerable(DamageSource source) { - /* - * The field Entity.invulnerable is private. - * It's only used while saving NBTTags, but since the entity would be killed - * on chunk unload, we prefer to override isInvulnerable(). - */ - return true; - } - - @Override - public boolean isCollidable() { - return false; - } - - @Override - public void setCustomName(String customName) { - // Locks the custom name. - } - - @Override - public void setCustomNameVisible(boolean visible) { - // Locks the custom name. - } - - @Override - public EnumInteractionResult a(EntityHuman human, Vec3D vec3d, ItemStack itemstack, EnumHand enumhand) { - // Prevent stand being equipped - return EnumInteractionResult.PASS; - } - - @Override - public boolean c(int i, ItemStack item) { - // Prevent stand being equipped - return false; - } - - @Override - public void setSlot(EnumItemSlot enumitemslot, ItemStack itemstack) { - // Prevent stand being equipped - } - - @Override - public void a(AxisAlignedBB boundingBox) { - // Do not change it! - } - - public void forceSetBoundingBox(AxisAlignedBB boundingBox) { - super.a(boundingBox); - } - - @Override - public int getId() { - if (Configuration.preciseHologramMovement) { - StackTraceElement element = ReflectionUtils.getStackTraceElement(2); - if (element != null && element.getFileName() != null && element.getFileName().equals("EntityTrackerEntry.java") && 158 < element.getLineNumber() && element.getLineNumber() < 168) { - // Then this method is being called when creating a new movement packet, we return a fake ID! - return -1; - } - } - - return super.getId(); - } - - @Override - public void m() { - if (!lockTick) { - super.m(); - } - } - - @Override - public void a(SoundEffect soundeffect, float f, float f1) { - // Remove sounds. - } - - @Override - public void setCustomNameNMS(String name) { - if (name != null && name.length() > 300) { - name = name.substring(0, 300); - } - super.setCustomName(name); - super.setCustomNameVisible(name != null && !name.isEmpty()); - } - - @Override - public String getCustomNameNMS() { - return super.getCustomName(); - } - - @Override - public void setLockTick(boolean lock) { - lockTick = lock; - } - - @Override - public void die() { - // Prevent being killed. - } - - @Override - public CraftEntity getBukkitEntity() { - if (super.bukkitEntity == null) { - super.bukkitEntity = new CraftNMSArmorStand(super.world.getServer(), this); - } - return super.bukkitEntity; - } - - @Override - public void killEntityNMS() { - super.dead = true; - } - - @Override - public void setLocationNMS(double x, double y, double z) { - super.setPosition(x, y, z); - - if (Configuration.preciseHologramMovement) { - // Send a packet near to update the position. - PacketPlayOutEntityTeleport teleportPacket = new PacketPlayOutEntityTeleport(this); - - for (Object obj : super.world.players) { - if (obj instanceof EntityPlayer) { - EntityPlayer nmsPlayer = (EntityPlayer) obj; - - double distanceSquared = Utils.square(nmsPlayer.locX - super.locX) + Utils.square(nmsPlayer.locZ - super.locZ); - if (distanceSquared < 8192 && nmsPlayer.playerConnection != null) { - nmsPlayer.playerConnection.sendPacket(teleportPacket); - } - } - } - } - } - - @Override - public boolean isDeadNMS() { - return super.dead; - } - - @Override - public int getIdNMS() { - return super.getId(); // Return the real ID without checking the stack trace. - } - - @Override - public HologramLine getHologramLine() { - return parentPiece; - } - - @Override - public org.bukkit.entity.Entity getBukkitEntityNMS() { - return getBukkitEntity(); - } -} \ No newline at end of file +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_10_R1; + +import org.bukkit.craftbukkit.v1_10_R1.entity.CraftEntity; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; +import com.gmail.filoghost.holographicdisplays.disk.Configuration; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; +import com.gmail.filoghost.holographicdisplays.util.Utils; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectionUtils; + +import net.minecraft.server.v1_10_R1.AxisAlignedBB; +import net.minecraft.server.v1_10_R1.DamageSource; +import net.minecraft.server.v1_10_R1.EntityArmorStand; +import net.minecraft.server.v1_10_R1.EntityHuman; +import net.minecraft.server.v1_10_R1.EntityPlayer; +import net.minecraft.server.v1_10_R1.EnumHand; +import net.minecraft.server.v1_10_R1.EnumInteractionResult; +import net.minecraft.server.v1_10_R1.EnumItemSlot; +import net.minecraft.server.v1_10_R1.ItemStack; +import net.minecraft.server.v1_10_R1.NBTTagCompound; +import net.minecraft.server.v1_10_R1.PacketPlayOutEntityTeleport; +import net.minecraft.server.v1_10_R1.SoundEffect; +import net.minecraft.server.v1_10_R1.Vec3D; +import net.minecraft.server.v1_10_R1.World; + +public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorStand { + + private boolean lockTick; + private HologramLine parentPiece; + + public EntityNMSArmorStand(World world, HologramLine parentPiece) { + super(world); + super.setInvisible(true); + super.setSmall(true); + super.setArms(false); + super.setNoGravity(true); + super.setBasePlate(true); + super.setMarker(true); + super.collides = false; + this.parentPiece = parentPiece; + forceSetBoundingBox(new NullBoundingBox()); + } + + + @Override + public void b(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public boolean c(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public boolean d(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public NBTTagCompound e(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return nbttagcompound; + } + + @Override + public void f(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public void a(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public boolean isInvulnerable(DamageSource source) { + /* + * The field Entity.invulnerable is private. + * It's only used while saving NBTTags, but since the entity would be killed + * on chunk unload, we prefer to override isInvulnerable(). + */ + return true; + } + + @Override + public boolean isCollidable() { + return false; + } + + @Override + public void setCustomName(String customName) { + // Locks the custom name. + } + + @Override + public void setCustomNameVisible(boolean visible) { + // Locks the custom name. + } + + @Override + public EnumInteractionResult a(EntityHuman human, Vec3D vec3d, ItemStack itemstack, EnumHand enumhand) { + // Prevent stand being equipped + return EnumInteractionResult.PASS; + } + + @Override + public boolean c(int i, ItemStack item) { + // Prevent stand being equipped + return false; + } + + @Override + public void setSlot(EnumItemSlot enumitemslot, ItemStack itemstack) { + // Prevent stand being equipped + } + + @Override + public void a(AxisAlignedBB boundingBox) { + // Do not change it! + } + + public void forceSetBoundingBox(AxisAlignedBB boundingBox) { + super.a(boundingBox); + } + + @Override + public int getId() { + if (Configuration.preciseHologramMovement) { + StackTraceElement element = ReflectionUtils.getStackTraceElement(2); + if (element != null && element.getFileName() != null && element.getFileName().equals("EntityTrackerEntry.java") && 158 < element.getLineNumber() && element.getLineNumber() < 168) { + // Then this method is being called when creating a new movement packet, we return a fake ID! + return -1; + } + } + + return super.getId(); + } + + @Override + public void m() { + if (!lockTick) { + super.m(); + } + } + + @Override + public void a(SoundEffect soundeffect, float f, float f1) { + // Remove sounds. + } + + @Override + public void setCustomNameNMS(String name) { + if (name != null && name.length() > 300) { + name = name.substring(0, 300); + } + super.setCustomName(name); + super.setCustomNameVisible(name != null && !name.isEmpty()); + } + + @Override + public String getCustomNameNMS() { + return super.getCustomName(); + } + + @Override + public void setLockTick(boolean lock) { + lockTick = lock; + } + + @Override + public void die() { + // Prevent being killed. + } + + @Override + public CraftEntity getBukkitEntity() { + if (super.bukkitEntity == null) { + super.bukkitEntity = new CraftNMSArmorStand(super.world.getServer(), this); + } + return super.bukkitEntity; + } + + @Override + public void killEntityNMS() { + super.dead = true; + } + + @Override + public void setLocationNMS(double x, double y, double z) { + super.setPosition(x, y, z); + + if (Configuration.preciseHologramMovement) { + // Send a packet near to update the position. + PacketPlayOutEntityTeleport teleportPacket = new PacketPlayOutEntityTeleport(this); + + for (Object obj : super.world.players) { + if (obj instanceof EntityPlayer) { + EntityPlayer nmsPlayer = (EntityPlayer) obj; + + double distanceSquared = Utils.square(nmsPlayer.locX - super.locX) + Utils.square(nmsPlayer.locZ - super.locZ); + if (distanceSquared < 8192 && nmsPlayer.playerConnection != null) { + nmsPlayer.playerConnection.sendPacket(teleportPacket); + } + } + } + } + } + + @Override + public boolean isDeadNMS() { + return super.dead; + } + + @Override + public int getIdNMS() { + return super.getId(); // Return the real ID without checking the stack trace. + } + + @Override + public HologramLine getHologramLine() { + return parentPiece; + } + + @Override + public org.bukkit.entity.Entity getBukkitEntityNMS() { + return getBukkitEntity(); + } +} diff --git a/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/EntityNMSItem.java b/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/EntityNMSItem.java index 82cc582c..35cbabde 100644 --- a/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/EntityNMSItem.java +++ b/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/EntityNMSItem.java @@ -1,271 +1,285 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_10_R1; - -import org.bukkit.craftbukkit.v1_10_R1.entity.CraftEntity; -import org.bukkit.craftbukkit.v1_10_R1.inventory.CraftItemStack; -import org.bukkit.entity.Player; - -import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.ItemUtils; -import com.gmail.filoghost.holographicdisplays.util.Utils; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; - -import net.minecraft.server.v1_10_R1.Blocks; -import net.minecraft.server.v1_10_R1.DamageSource; -import net.minecraft.server.v1_10_R1.Entity; -import net.minecraft.server.v1_10_R1.EntityHuman; -import net.minecraft.server.v1_10_R1.EntityItem; -import net.minecraft.server.v1_10_R1.EntityPlayer; -import net.minecraft.server.v1_10_R1.ItemStack; -import net.minecraft.server.v1_10_R1.NBTTagCompound; -import net.minecraft.server.v1_10_R1.NBTTagList; -import net.minecraft.server.v1_10_R1.NBTTagString; -import net.minecraft.server.v1_10_R1.PacketPlayOutMount; -import net.minecraft.server.v1_10_R1.World; -import net.minecraft.server.v1_10_R1.AxisAlignedBB; - -public class EntityNMSItem extends EntityItem implements NMSItem { - - private static final ReflectField VEHICLE_FIELD = new ReflectField(Entity.class, "au"); - - private boolean lockTick; - private ItemLine parentPiece; - private ItemPickupManager itemPickupManager; - - private int resendMountPacketTicks; - - public EntityNMSItem(World world, ItemLine piece, ItemPickupManager itemPickupManager) { - super(world); - super.pickupDelay = Integer.MAX_VALUE; - this.parentPiece = piece; - this.itemPickupManager = itemPickupManager; - forceSetBoundingBox(new NullBoundingBox()); - } - - @Override - public void m() { - - // So it won't get removed. - ticksLived = 0; - - if (resendMountPacketTicks++ > 20) { - resendMountPacketTicks = 0; - - if (bB() != null) { - // Send a packet near to "remind" players that the item is riding the armor stand (Spigot bug or client bug) - PacketPlayOutMount mountPacket = new PacketPlayOutMount(bB()); - - for (Object obj : super.world.players) { - if (obj instanceof EntityPlayer) { - EntityPlayer nmsPlayer = (EntityPlayer) obj; - - double distanceSquared = Utils.square(nmsPlayer.locX - super.locX) + Utils.square(nmsPlayer.locZ - super.locZ); - if (distanceSquared < 1024 && nmsPlayer.playerConnection != null) { - nmsPlayer.playerConnection.sendPacket(mountPacket); - } - } - } - } - } - - if (!lockTick) { - super.m(); - } - } - - // Method called when a player is near. - @Override - public void d(EntityHuman human) { - - if (human.locY < super.locY - 1.5 || human.locY > super.locY + 1.0) { - // Too low or too high, it's a bit weird. - return; - } - - if (parentPiece.getPickupHandler() != null && human instanceof EntityPlayer) { - itemPickupManager.handleItemLinePickup((Player) human.getBukkitEntity(), parentPiece.getPickupHandler(), parentPiece.getParent()); - // It is never added to the inventory. - } - } - - @Override - public void b(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public boolean c(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public boolean d(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public NBTTagCompound e(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return nbttagcompound; - } - - @Override - public void f(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public void a(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public boolean isInvulnerable(DamageSource source) { - /* - * The field Entity.invulnerable is private. - * It's only used while saving NBTTags, but since the entity would be killed - * on chunk unload, we prefer to override isInvulnerable(). - */ - return true; - } - - @Override - public boolean isCollidable() { - return false; - } - - @Override - public void a(AxisAlignedBB boundingBox) { - // Do not change it! - } - - public void forceSetBoundingBox(AxisAlignedBB boundingBox) { - super.a(boundingBox); - } - - @Override - public void inactiveTick() { - // Check inactive ticks. - - if (!lockTick) { - super.inactiveTick(); - } - } - - @Override - public void setLockTick(boolean lock) { - lockTick = lock; - } - - @Override - public void die() { - // Prevent being killed. - } - - @Override - public CraftEntity getBukkitEntity() { - if (super.bukkitEntity == null) { - super.bukkitEntity = new CraftNMSItem(super.world.getServer(), this); - } - return super.bukkitEntity; - } - - @Override - public boolean isDeadNMS() { - return super.dead; - } - - @Override - public void killEntityNMS() { - super.dead = true; - } - - @Override - public void setLocationNMS(double x, double y, double z) { - super.setPosition(x, y, z); - } - - @Override - public void setItemStackNMS(org.bukkit.inventory.ItemStack stack) { - ItemStack newItem = CraftItemStack.asNMSCopy(stack); - - if (newItem == null) { - newItem = new ItemStack(Blocks.BEDROCK); - } - - if (newItem.getTag() == null) { - newItem.setTag(new NBTTagCompound()); - } - NBTTagCompound display = newItem.getTag().getCompound("display"); - - if (!newItem.getTag().hasKey("display")) { - newItem.getTag().set("display", display); - } - - NBTTagList tagList = new NBTTagList(); - tagList.add(new NBTTagString(ItemUtils.ANTISTACK_LORE)); // Antistack lore - - display.set("Lore", tagList); - newItem.count = 0; - setItemStack(newItem); - } - - @Override - public int getIdNMS() { - return super.getId(); - } - - @Override - public ItemLine getHologramLine() { - return parentPiece; - } - - @Override - public void allowPickup(boolean pickup) { - if (pickup) { - super.pickupDelay = 0; - } else { - super.pickupDelay = Integer.MAX_VALUE; - } - } - - @Override - public org.bukkit.entity.Entity getBukkitEntityNMS() { - return getBukkitEntity(); - } - - @Override - public void setPassengerOfNMS(NMSEntityBase vehicleBase) { - if (vehicleBase == null || !(vehicleBase instanceof Entity)) { - // It should never dismount - return; - } - - Entity entity = (Entity) vehicleBase; - - try { - if (super.bB() != null) { - Entity oldVehicle = super.bB(); - VEHICLE_FIELD.set(this, null); - oldVehicle.passengers.remove(this); - } - - VEHICLE_FIELD.set(this, entity); - entity.passengers.clear(); - entity.passengers.add(this); - - } catch (Exception ex) { - ConsoleLogger.logDebugException(ex); - } - } - - @Override - public Object getRawItemStack() { - return super.getItemStack(); - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_10_R1; + +import org.bukkit.craftbukkit.v1_10_R1.entity.CraftEntity; +import org.bukkit.craftbukkit.v1_10_R1.inventory.CraftItemStack; +import org.bukkit.entity.Player; + +import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.ItemUtils; +import com.gmail.filoghost.holographicdisplays.util.Utils; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; + +import net.minecraft.server.v1_10_R1.Blocks; +import net.minecraft.server.v1_10_R1.DamageSource; +import net.minecraft.server.v1_10_R1.Entity; +import net.minecraft.server.v1_10_R1.EntityHuman; +import net.minecraft.server.v1_10_R1.EntityItem; +import net.minecraft.server.v1_10_R1.EntityPlayer; +import net.minecraft.server.v1_10_R1.ItemStack; +import net.minecraft.server.v1_10_R1.NBTTagCompound; +import net.minecraft.server.v1_10_R1.NBTTagList; +import net.minecraft.server.v1_10_R1.NBTTagString; +import net.minecraft.server.v1_10_R1.PacketPlayOutMount; +import net.minecraft.server.v1_10_R1.World; +import net.minecraft.server.v1_10_R1.AxisAlignedBB; + +public class EntityNMSItem extends EntityItem implements NMSItem { + + private static final ReflectField VEHICLE_FIELD = new ReflectField(Entity.class, "au"); + + private boolean lockTick; + private ItemLine parentPiece; + private ItemPickupManager itemPickupManager; + + private int resendMountPacketTicks; + + public EntityNMSItem(World world, ItemLine piece, ItemPickupManager itemPickupManager) { + super(world); + super.pickupDelay = Integer.MAX_VALUE; + this.parentPiece = piece; + this.itemPickupManager = itemPickupManager; + forceSetBoundingBox(new NullBoundingBox()); + } + + @Override + public void m() { + + // So it won't get removed. + ticksLived = 0; + + if (resendMountPacketTicks++ > 20) { + resendMountPacketTicks = 0; + + if (bB() != null) { + // Send a packet near to "remind" players that the item is riding the armor stand (Spigot bug or client bug) + PacketPlayOutMount mountPacket = new PacketPlayOutMount(bB()); + + for (Object obj : super.world.players) { + if (obj instanceof EntityPlayer) { + EntityPlayer nmsPlayer = (EntityPlayer) obj; + + double distanceSquared = Utils.square(nmsPlayer.locX - super.locX) + Utils.square(nmsPlayer.locZ - super.locZ); + if (distanceSquared < 1024 && nmsPlayer.playerConnection != null) { + nmsPlayer.playerConnection.sendPacket(mountPacket); + } + } + } + } + } + + if (!lockTick) { + super.m(); + } + } + + // Method called when a player is near. + @Override + public void d(EntityHuman human) { + + if (human.locY < super.locY - 1.5 || human.locY > super.locY + 1.0) { + // Too low or too high, it's a bit weird. + return; + } + + if (parentPiece.getPickupHandler() != null && human instanceof EntityPlayer) { + itemPickupManager.handleItemLinePickup((Player) human.getBukkitEntity(), parentPiece.getPickupHandler(), parentPiece.getParent()); + // It is never added to the inventory. + } + } + + @Override + public void b(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public boolean c(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public boolean d(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public NBTTagCompound e(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return nbttagcompound; + } + + @Override + public void f(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public void a(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public boolean isInvulnerable(DamageSource source) { + /* + * The field Entity.invulnerable is private. + * It's only used while saving NBTTags, but since the entity would be killed + * on chunk unload, we prefer to override isInvulnerable(). + */ + return true; + } + + @Override + public boolean isCollidable() { + return false; + } + + @Override + public void a(AxisAlignedBB boundingBox) { + // Do not change it! + } + + public void forceSetBoundingBox(AxisAlignedBB boundingBox) { + super.a(boundingBox); + } + + @Override + public void inactiveTick() { + // Check inactive ticks. + + if (!lockTick) { + super.inactiveTick(); + } + } + + @Override + public void setLockTick(boolean lock) { + lockTick = lock; + } + + @Override + public void die() { + // Prevent being killed. + } + + @Override + public CraftEntity getBukkitEntity() { + if (super.bukkitEntity == null) { + super.bukkitEntity = new CraftNMSItem(super.world.getServer(), this); + } + return super.bukkitEntity; + } + + @Override + public boolean isDeadNMS() { + return super.dead; + } + + @Override + public void killEntityNMS() { + super.dead = true; + } + + @Override + public void setLocationNMS(double x, double y, double z) { + super.setPosition(x, y, z); + } + + @Override + public void setItemStackNMS(org.bukkit.inventory.ItemStack stack) { + ItemStack newItem = CraftItemStack.asNMSCopy(stack); + + if (newItem == null) { + newItem = new ItemStack(Blocks.BEDROCK); + } + + if (newItem.getTag() == null) { + newItem.setTag(new NBTTagCompound()); + } + NBTTagCompound display = newItem.getTag().getCompound("display"); + + if (!newItem.getTag().hasKey("display")) { + newItem.getTag().set("display", display); + } + + NBTTagList tagList = new NBTTagList(); + tagList.add(new NBTTagString(ItemUtils.ANTISTACK_LORE)); // Antistack lore + + display.set("Lore", tagList); + newItem.count = 0; + setItemStack(newItem); + } + + @Override + public int getIdNMS() { + return super.getId(); + } + + @Override + public ItemLine getHologramLine() { + return parentPiece; + } + + @Override + public void allowPickup(boolean pickup) { + if (pickup) { + super.pickupDelay = 0; + } else { + super.pickupDelay = Integer.MAX_VALUE; + } + } + + @Override + public org.bukkit.entity.Entity getBukkitEntityNMS() { + return getBukkitEntity(); + } + + @Override + public void setPassengerOfNMS(NMSEntityBase vehicleBase) { + if (vehicleBase == null || !(vehicleBase instanceof Entity)) { + // It should never dismount + return; + } + + Entity entity = (Entity) vehicleBase; + + try { + if (super.bB() != null) { + Entity oldVehicle = super.bB(); + VEHICLE_FIELD.set(this, null); + oldVehicle.passengers.remove(this); + } + + VEHICLE_FIELD.set(this, entity); + entity.passengers.clear(); + entity.passengers.add(this); + + } catch (Exception ex) { + ConsoleLogger.logDebugException(ex); + } + } + + @Override + public Object getRawItemStack() { + return super.getItemStack(); + } +} diff --git a/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/EntityNMSSlime.java b/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/EntityNMSSlime.java index c860b042..c71ff208 100644 --- a/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/EntityNMSSlime.java +++ b/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/EntityNMSSlime.java @@ -1,231 +1,245 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_10_R1; - -import org.bukkit.Bukkit; -import org.bukkit.craftbukkit.v1_10_R1.entity.CraftEntity; -import org.bukkit.event.player.PlayerInteractEntityEvent; - -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSSlime; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.Utils; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; - -import net.minecraft.server.v1_10_R1.AxisAlignedBB; -import net.minecraft.server.v1_10_R1.DamageSource; -import net.minecraft.server.v1_10_R1.Entity; -import net.minecraft.server.v1_10_R1.EntityDamageSource; -import net.minecraft.server.v1_10_R1.EntityPlayer; -import net.minecraft.server.v1_10_R1.EntitySlime; -import net.minecraft.server.v1_10_R1.NBTTagCompound; -import net.minecraft.server.v1_10_R1.PacketPlayOutMount; -import net.minecraft.server.v1_10_R1.SoundEffect; -import net.minecraft.server.v1_10_R1.World; - -public class EntityNMSSlime extends EntitySlime implements NMSSlime { - - private static final ReflectField VEHICLE_FIELD = new ReflectField(Entity.class, "au"); - - private boolean lockTick; - private HologramLine parentPiece; - - private int resendMountPacketTicks; - - public EntityNMSSlime(World world, HologramLine parentPiece) { - super(world); - super.persistent = true; - super.collides = false; - a(0.0F, 0.0F); - setSize(1); - setInvisible(true); - this.parentPiece = parentPiece; - forceSetBoundingBox(new NullBoundingBox()); - } - - @Override - public void a(AxisAlignedBB boundingBox) { - // Do not change it! - } - - public void forceSetBoundingBox(AxisAlignedBB boundingBox) { - super.a(boundingBox); - } - - @Override - public void m() { - - // So it won't get removed. - ticksLived = 0; - - if (resendMountPacketTicks++ > 20) { - resendMountPacketTicks = 0; - - if (bB() != null) { - // Send a packet near to "remind" players that the slime is riding the armor stand (Spigot bug or client bug) - PacketPlayOutMount mountPacket = new PacketPlayOutMount(bB()); - - for (Object obj : super.world.players) { - if (obj instanceof EntityPlayer) { - EntityPlayer nmsPlayer = (EntityPlayer) obj; - - double distanceSquared = Utils.square(nmsPlayer.locX - super.locX) + Utils.square(nmsPlayer.locZ - super.locZ); - if (distanceSquared < 1024 && nmsPlayer.playerConnection != null) { - nmsPlayer.playerConnection.sendPacket(mountPacket); - } - } - } - } - } - - if (!lockTick) { - super.m(); - } - } - - @Override - public void b(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public boolean c(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public boolean d(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public NBTTagCompound e(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return nbttagcompound; - } - - @Override - public void f(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public void a(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public boolean damageEntity(DamageSource damageSource, float amount) { - if (damageSource instanceof EntityDamageSource) { - EntityDamageSource entityDamageSource = (EntityDamageSource) damageSource; - if (entityDamageSource.getEntity() instanceof EntityPlayer) { - Bukkit.getPluginManager().callEvent(new PlayerInteractEntityEvent(((EntityPlayer) entityDamageSource.getEntity()).getBukkitEntity(), getBukkitEntity())); // Bukkit takes care of the exceptions - } - } - return false; - } - - @Override - public boolean isInvulnerable(DamageSource source) { - /* - * The field Entity.invulnerable is private. - * It's only used while saving NBTTags, but since the entity would be killed - * on chunk unload, we prefer to override isInvulnerable(). - */ - return true; - } - - @Override - public boolean isCollidable() { - return false; - } - - @Override - public void setCustomName(String customName) { - // Locks the custom name. - } - - @Override - public void setCustomNameVisible(boolean visible) { - // Locks the custom name. - } - - @Override - public void a(SoundEffect soundeffect, float f, float f1) { - // Remove sounds. - } - - @Override - public void setLockTick(boolean lock) { - lockTick = lock; - } - - @Override - public void die() { - // Prevent being killed. - } - - @Override - public CraftEntity getBukkitEntity() { - if (super.bukkitEntity == null) { - super.bukkitEntity = new CraftNMSSlime(super.world.getServer(), this); - } - return super.bukkitEntity; - } - - @Override - public boolean isDeadNMS() { - return super.dead; - } - - @Override - public void killEntityNMS() { - super.dead = true; - } - - @Override - public void setLocationNMS(double x, double y, double z) { - super.setPosition(x, y, z); - } - - @Override - public int getIdNMS() { - return super.getId(); - } - - @Override - public HologramLine getHologramLine() { - return parentPiece; - } - - @Override - public org.bukkit.entity.Entity getBukkitEntityNMS() { - return getBukkitEntity(); - } - - @Override - public void setPassengerOfNMS(NMSEntityBase vehicleBase) { - if (vehicleBase == null || !(vehicleBase instanceof Entity)) { - // It should never dismount - return; - } - - Entity entity = (Entity) vehicleBase; - - try { - if (super.bB() != null) { - Entity oldVehicle = super.bB(); - VEHICLE_FIELD.set(this, null); - oldVehicle.passengers.remove(this); - } - - VEHICLE_FIELD.set(this, entity); - entity.passengers.clear(); - entity.passengers.add(this); - - } catch (Exception ex) { - ConsoleLogger.logDebugException(ex); - } - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_10_R1; + +import org.bukkit.Bukkit; +import org.bukkit.craftbukkit.v1_10_R1.entity.CraftEntity; +import org.bukkit.event.player.PlayerInteractEntityEvent; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSSlime; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.Utils; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; + +import net.minecraft.server.v1_10_R1.AxisAlignedBB; +import net.minecraft.server.v1_10_R1.DamageSource; +import net.minecraft.server.v1_10_R1.Entity; +import net.minecraft.server.v1_10_R1.EntityDamageSource; +import net.minecraft.server.v1_10_R1.EntityPlayer; +import net.minecraft.server.v1_10_R1.EntitySlime; +import net.minecraft.server.v1_10_R1.NBTTagCompound; +import net.minecraft.server.v1_10_R1.PacketPlayOutMount; +import net.minecraft.server.v1_10_R1.SoundEffect; +import net.minecraft.server.v1_10_R1.World; + +public class EntityNMSSlime extends EntitySlime implements NMSSlime { + + private static final ReflectField VEHICLE_FIELD = new ReflectField(Entity.class, "au"); + + private boolean lockTick; + private HologramLine parentPiece; + + private int resendMountPacketTicks; + + public EntityNMSSlime(World world, HologramLine parentPiece) { + super(world); + super.persistent = true; + super.collides = false; + a(0.0F, 0.0F); + setSize(1); + setInvisible(true); + this.parentPiece = parentPiece; + forceSetBoundingBox(new NullBoundingBox()); + } + + @Override + public void a(AxisAlignedBB boundingBox) { + // Do not change it! + } + + public void forceSetBoundingBox(AxisAlignedBB boundingBox) { + super.a(boundingBox); + } + + @Override + public void m() { + + // So it won't get removed. + ticksLived = 0; + + if (resendMountPacketTicks++ > 20) { + resendMountPacketTicks = 0; + + if (bB() != null) { + // Send a packet near to "remind" players that the slime is riding the armor stand (Spigot bug or client bug) + PacketPlayOutMount mountPacket = new PacketPlayOutMount(bB()); + + for (Object obj : super.world.players) { + if (obj instanceof EntityPlayer) { + EntityPlayer nmsPlayer = (EntityPlayer) obj; + + double distanceSquared = Utils.square(nmsPlayer.locX - super.locX) + Utils.square(nmsPlayer.locZ - super.locZ); + if (distanceSquared < 1024 && nmsPlayer.playerConnection != null) { + nmsPlayer.playerConnection.sendPacket(mountPacket); + } + } + } + } + } + + if (!lockTick) { + super.m(); + } + } + + @Override + public void b(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public boolean c(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public boolean d(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public NBTTagCompound e(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return nbttagcompound; + } + + @Override + public void f(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public void a(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public boolean damageEntity(DamageSource damageSource, float amount) { + if (damageSource instanceof EntityDamageSource) { + EntityDamageSource entityDamageSource = (EntityDamageSource) damageSource; + if (entityDamageSource.getEntity() instanceof EntityPlayer) { + Bukkit.getPluginManager().callEvent(new PlayerInteractEntityEvent(((EntityPlayer) entityDamageSource.getEntity()).getBukkitEntity(), getBukkitEntity())); // Bukkit takes care of the exceptions + } + } + return false; + } + + @Override + public boolean isInvulnerable(DamageSource source) { + /* + * The field Entity.invulnerable is private. + * It's only used while saving NBTTags, but since the entity would be killed + * on chunk unload, we prefer to override isInvulnerable(). + */ + return true; + } + + @Override + public boolean isCollidable() { + return false; + } + + @Override + public void setCustomName(String customName) { + // Locks the custom name. + } + + @Override + public void setCustomNameVisible(boolean visible) { + // Locks the custom name. + } + + @Override + public void a(SoundEffect soundeffect, float f, float f1) { + // Remove sounds. + } + + @Override + public void setLockTick(boolean lock) { + lockTick = lock; + } + + @Override + public void die() { + // Prevent being killed. + } + + @Override + public CraftEntity getBukkitEntity() { + if (super.bukkitEntity == null) { + super.bukkitEntity = new CraftNMSSlime(super.world.getServer(), this); + } + return super.bukkitEntity; + } + + @Override + public boolean isDeadNMS() { + return super.dead; + } + + @Override + public void killEntityNMS() { + super.dead = true; + } + + @Override + public void setLocationNMS(double x, double y, double z) { + super.setPosition(x, y, z); + } + + @Override + public int getIdNMS() { + return super.getId(); + } + + @Override + public HologramLine getHologramLine() { + return parentPiece; + } + + @Override + public org.bukkit.entity.Entity getBukkitEntityNMS() { + return getBukkitEntity(); + } + + @Override + public void setPassengerOfNMS(NMSEntityBase vehicleBase) { + if (vehicleBase == null || !(vehicleBase instanceof Entity)) { + // It should never dismount + return; + } + + Entity entity = (Entity) vehicleBase; + + try { + if (super.bB() != null) { + Entity oldVehicle = super.bB(); + VEHICLE_FIELD.set(this, null); + oldVehicle.passengers.remove(this); + } + + VEHICLE_FIELD.set(this, entity); + entity.passengers.clear(); + entity.passengers.add(this); + + } catch (Exception ex) { + ConsoleLogger.logDebugException(ex); + } + } +} diff --git a/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/NmsManagerImpl.java b/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/NmsManagerImpl.java index 6ca07528..bf41ff18 100644 --- a/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/NmsManagerImpl.java +++ b/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/NmsManagerImpl.java @@ -1,124 +1,138 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_10_R1; - -import java.lang.reflect.Method; -import java.util.Map; - -import org.bukkit.Bukkit; -import org.bukkit.craftbukkit.v1_10_R1.CraftWorld; -import org.bukkit.craftbukkit.v1_10_R1.entity.CraftEntity; -import org.bukkit.inventory.ItemStack; - -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; -import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.Validator; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; - -import net.minecraft.server.v1_10_R1.Entity; -import net.minecraft.server.v1_10_R1.EntityTypes; -import net.minecraft.server.v1_10_R1.MathHelper; -import net.minecraft.server.v1_10_R1.World; -import net.minecraft.server.v1_10_R1.WorldServer; - -public class NmsManagerImpl implements NMSManager { - - private static final ReflectField, String>> ENTITY_NAMES_BY_CLASS_FIELD = new ReflectField, String>>(EntityTypes.class, "d"); - private static final ReflectField, Integer>> ENTITY_IDS_BY_CLASS_FIELD = new ReflectField, Integer>>(EntityTypes.class, "f"); - - private Method validateEntityMethod; - - @Override - public void setup() throws Exception { - registerCustomEntity(EntityNMSArmorStand.class, "ArmorStand", 30); - registerCustomEntity(EntityNMSItem.class, "Item", 1); - registerCustomEntity(EntityNMSSlime.class, "Slime", 55); - - validateEntityMethod = World.class.getDeclaredMethod("b", Entity.class); - validateEntityMethod.setAccessible(true); - } - - public void registerCustomEntity(Class entityClass, String name, int id) throws Exception { - ENTITY_NAMES_BY_CLASS_FIELD.getStatic().put(entityClass, name); - ENTITY_IDS_BY_CLASS_FIELD.getStatic().put(entityClass, Integer.valueOf(id)); - } - - @Override - public NMSItem spawnNMSItem(org.bukkit.World bukkitWorld, double x, double y, double z, ItemLine parentPiece, ItemStack stack, ItemPickupManager itemPickupManager) { - WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); - EntityNMSItem customItem = new EntityNMSItem(nmsWorld, parentPiece, itemPickupManager); - customItem.setLocationNMS(x, y, z); - customItem.setItemStackNMS(stack); - if (!addEntityToWorld(nmsWorld, customItem)) { - ConsoleLogger.handleSpawnFail(parentPiece); - } - return customItem; - } - - @Override - public EntityNMSSlime spawnNMSSlime(org.bukkit.World bukkitWorld, double x, double y, double z, HologramLine parentPiece) { - WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); - EntityNMSSlime touchSlime = new EntityNMSSlime(nmsWorld, parentPiece); - touchSlime.setLocationNMS(x, y, z); - if (!addEntityToWorld(nmsWorld, touchSlime)) { - ConsoleLogger.handleSpawnFail(parentPiece); - } - return touchSlime; - } - - @Override - public NMSArmorStand spawnNMSArmorStand(org.bukkit.World world, double x, double y, double z, HologramLine parentPiece) { - WorldServer nmsWorld = ((CraftWorld) world).getHandle(); - EntityNMSArmorStand invisibleArmorStand = new EntityNMSArmorStand(nmsWorld, parentPiece); - invisibleArmorStand.setLocationNMS(x, y, z); - if (!addEntityToWorld(nmsWorld, invisibleArmorStand)) { - ConsoleLogger.handleSpawnFail(parentPiece); - } - return invisibleArmorStand; - } - - private boolean addEntityToWorld(WorldServer nmsWorld, Entity nmsEntity) { - Validator.isTrue(Bukkit.isPrimaryThread(), "Async entity add"); - - final int chunkX = MathHelper.floor(nmsEntity.locX / 16.0); - final int chunkZ = MathHelper.floor(nmsEntity.locZ / 16.0); - - if (!nmsWorld.getChunkProviderServer().isLoaded(chunkX, chunkZ)) { - // This should never happen - nmsEntity.dead = true; - return false; - } - - nmsWorld.getChunkAt(chunkX, chunkZ).a(nmsEntity); - nmsWorld.entityList.add(nmsEntity); - - try { - validateEntityMethod.invoke(nmsWorld, nmsEntity); - } catch (Exception e) { - e.printStackTrace(); - return false; - } - return true; - } - - @Override - public boolean isNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { - return ((CraftEntity) bukkitEntity).getHandle() instanceof NMSEntityBase; - } - - @Override - public NMSEntityBase getNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { - - Entity nmsEntity = ((CraftEntity) bukkitEntity).getHandle(); - if (nmsEntity instanceof NMSEntityBase) { - return ((NMSEntityBase) nmsEntity); - } - - return null; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_10_R1; + +import java.lang.reflect.Method; +import java.util.Map; + +import org.bukkit.Bukkit; +import org.bukkit.craftbukkit.v1_10_R1.CraftWorld; +import org.bukkit.craftbukkit.v1_10_R1.entity.CraftEntity; +import org.bukkit.inventory.ItemStack; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; +import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.Validator; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; + +import net.minecraft.server.v1_10_R1.Entity; +import net.minecraft.server.v1_10_R1.EntityTypes; +import net.minecraft.server.v1_10_R1.MathHelper; +import net.minecraft.server.v1_10_R1.World; +import net.minecraft.server.v1_10_R1.WorldServer; + +public class NmsManagerImpl implements NMSManager { + + private static final ReflectField, String>> ENTITY_NAMES_BY_CLASS_FIELD = new ReflectField, String>>(EntityTypes.class, "d"); + private static final ReflectField, Integer>> ENTITY_IDS_BY_CLASS_FIELD = new ReflectField, Integer>>(EntityTypes.class, "f"); + + private Method validateEntityMethod; + + @Override + public void setup() throws Exception { + registerCustomEntity(EntityNMSArmorStand.class, "ArmorStand", 30); + registerCustomEntity(EntityNMSItem.class, "Item", 1); + registerCustomEntity(EntityNMSSlime.class, "Slime", 55); + + validateEntityMethod = World.class.getDeclaredMethod("b", Entity.class); + validateEntityMethod.setAccessible(true); + } + + public void registerCustomEntity(Class entityClass, String name, int id) throws Exception { + ENTITY_NAMES_BY_CLASS_FIELD.getStatic().put(entityClass, name); + ENTITY_IDS_BY_CLASS_FIELD.getStatic().put(entityClass, Integer.valueOf(id)); + } + + @Override + public NMSItem spawnNMSItem(org.bukkit.World bukkitWorld, double x, double y, double z, ItemLine parentPiece, ItemStack stack, ItemPickupManager itemPickupManager) { + WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); + EntityNMSItem customItem = new EntityNMSItem(nmsWorld, parentPiece, itemPickupManager); + customItem.setLocationNMS(x, y, z); + customItem.setItemStackNMS(stack); + if (!addEntityToWorld(nmsWorld, customItem)) { + ConsoleLogger.handleSpawnFail(parentPiece); + } + return customItem; + } + + @Override + public EntityNMSSlime spawnNMSSlime(org.bukkit.World bukkitWorld, double x, double y, double z, HologramLine parentPiece) { + WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); + EntityNMSSlime touchSlime = new EntityNMSSlime(nmsWorld, parentPiece); + touchSlime.setLocationNMS(x, y, z); + if (!addEntityToWorld(nmsWorld, touchSlime)) { + ConsoleLogger.handleSpawnFail(parentPiece); + } + return touchSlime; + } + + @Override + public NMSArmorStand spawnNMSArmorStand(org.bukkit.World world, double x, double y, double z, HologramLine parentPiece) { + WorldServer nmsWorld = ((CraftWorld) world).getHandle(); + EntityNMSArmorStand invisibleArmorStand = new EntityNMSArmorStand(nmsWorld, parentPiece); + invisibleArmorStand.setLocationNMS(x, y, z); + if (!addEntityToWorld(nmsWorld, invisibleArmorStand)) { + ConsoleLogger.handleSpawnFail(parentPiece); + } + return invisibleArmorStand; + } + + private boolean addEntityToWorld(WorldServer nmsWorld, Entity nmsEntity) { + Validator.isTrue(Bukkit.isPrimaryThread(), "Async entity add"); + + final int chunkX = MathHelper.floor(nmsEntity.locX / 16.0); + final int chunkZ = MathHelper.floor(nmsEntity.locZ / 16.0); + + if (!nmsWorld.getChunkProviderServer().isLoaded(chunkX, chunkZ)) { + // This should never happen + nmsEntity.dead = true; + return false; + } + + nmsWorld.getChunkAt(chunkX, chunkZ).a(nmsEntity); + nmsWorld.entityList.add(nmsEntity); + + try { + validateEntityMethod.invoke(nmsWorld, nmsEntity); + } catch (Exception e) { + e.printStackTrace(); + return false; + } + return true; + } + + @Override + public boolean isNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { + return ((CraftEntity) bukkitEntity).getHandle() instanceof NMSEntityBase; + } + + @Override + public NMSEntityBase getNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { + + Entity nmsEntity = ((CraftEntity) bukkitEntity).getHandle(); + if (nmsEntity instanceof NMSEntityBase) { + return ((NMSEntityBase) nmsEntity); + } + + return null; + } + +} diff --git a/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/NullBoundingBox.java b/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/NullBoundingBox.java index f8382c58..562a7851 100644 --- a/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/NullBoundingBox.java +++ b/NMS/v1_10_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_10_R1/NullBoundingBox.java @@ -1,109 +1,123 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_10_R1; - -import net.minecraft.server.v1_10_R1.AxisAlignedBB; -import net.minecraft.server.v1_10_R1.BlockPosition; -import net.minecraft.server.v1_10_R1.MovingObjectPosition; -import net.minecraft.server.v1_10_R1.Vec3D; - -public class NullBoundingBox extends AxisAlignedBB { - - public NullBoundingBox() { - super(0, 0, 0, 0, 0, 0); - } - - @Override - public double a() { - return 0.0; - } - - @Override - public double a(AxisAlignedBB arg0, double arg1) { - return 0.0; - } - - @Override - public AxisAlignedBB a(AxisAlignedBB arg0) { - return this; - } - - @Override - public AxisAlignedBB a(double arg0, double arg1, double arg2) { - return this; - } - - @Override - public MovingObjectPosition b(Vec3D arg0, Vec3D arg1) { - return null; - } - - @Override - public boolean a(Vec3D arg0) { - return false; - } - - @Override - public double b(AxisAlignedBB arg0, double arg1) { - return 0.0; - } - - @Override - public boolean b(AxisAlignedBB arg0) { - return false; - } - - @Override - public double c(AxisAlignedBB arg0, double arg1) { - return 0.0; - } - - @Override - public AxisAlignedBB c(double arg0, double arg1, double arg2) { - return this; - } - - @Override - public AxisAlignedBB grow(double arg0, double arg1, double arg2) { - return this; - } - - @Override - public AxisAlignedBB shrink(double arg0) { - return this; - } - - @Override - public AxisAlignedBB a(BlockPosition arg0) { - return this; - } - - @Override - public boolean a(double arg0, double arg1, double arg2, double arg3, double arg4, double arg5) { - return false; - } - - @Override - public boolean b(Vec3D arg0) { - return false; - } - - @Override - public boolean c(Vec3D arg0) { - return false; - } - - @Override - public boolean d(Vec3D arg0) { - return false; - } - - @Override - public AxisAlignedBB e(double arg0) { - return this; - } - - @Override - public AxisAlignedBB g(double arg0) { - return this; - } - -} \ No newline at end of file +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_10_R1; + +import net.minecraft.server.v1_10_R1.AxisAlignedBB; +import net.minecraft.server.v1_10_R1.BlockPosition; +import net.minecraft.server.v1_10_R1.MovingObjectPosition; +import net.minecraft.server.v1_10_R1.Vec3D; + +public class NullBoundingBox extends AxisAlignedBB { + + public NullBoundingBox() { + super(0, 0, 0, 0, 0, 0); + } + + @Override + public double a() { + return 0.0; + } + + @Override + public double a(AxisAlignedBB arg0, double arg1) { + return 0.0; + } + + @Override + public AxisAlignedBB a(AxisAlignedBB arg0) { + return this; + } + + @Override + public AxisAlignedBB a(double arg0, double arg1, double arg2) { + return this; + } + + @Override + public MovingObjectPosition b(Vec3D arg0, Vec3D arg1) { + return null; + } + + @Override + public boolean a(Vec3D arg0) { + return false; + } + + @Override + public double b(AxisAlignedBB arg0, double arg1) { + return 0.0; + } + + @Override + public boolean b(AxisAlignedBB arg0) { + return false; + } + + @Override + public double c(AxisAlignedBB arg0, double arg1) { + return 0.0; + } + + @Override + public AxisAlignedBB c(double arg0, double arg1, double arg2) { + return this; + } + + @Override + public AxisAlignedBB grow(double arg0, double arg1, double arg2) { + return this; + } + + @Override + public AxisAlignedBB shrink(double arg0) { + return this; + } + + @Override + public AxisAlignedBB a(BlockPosition arg0) { + return this; + } + + @Override + public boolean a(double arg0, double arg1, double arg2, double arg3, double arg4, double arg5) { + return false; + } + + @Override + public boolean b(Vec3D arg0) { + return false; + } + + @Override + public boolean c(Vec3D arg0) { + return false; + } + + @Override + public boolean d(Vec3D arg0) { + return false; + } + + @Override + public AxisAlignedBB e(double arg0) { + return this; + } + + @Override + public AxisAlignedBB g(double arg0) { + return this; + } + +} diff --git a/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/CraftNMSArmorStand.java b/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/CraftNMSArmorStand.java index 9e865913..3fbc09c5 100644 --- a/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/CraftNMSArmorStand.java +++ b/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/CraftNMSArmorStand.java @@ -1,78 +1,92 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_11_R1; - -import java.util.Collection; - -import org.bukkit.EntityEffect; -import org.bukkit.Location; -import org.bukkit.craftbukkit.v1_11_R1.CraftServer; -import org.bukkit.craftbukkit.v1_11_R1.entity.CraftArmorStand; -import org.bukkit.entity.Entity; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -import org.bukkit.inventory.ItemStack; -import org.bukkit.potion.PotionEffect; -import org.bukkit.util.EulerAngle; -import org.bukkit.util.Vector; - -public class CraftNMSArmorStand extends CraftArmorStand { - - public CraftNMSArmorStand(CraftServer server, EntityNMSArmorStand entity) { - super(server, entity); - } - - // Disallow all the bukkit methods. - - @Override - public void remove() { - // Cannot be removed, this is the most important to override. - } - - // Methods from Armor stand class - @Override public void setArms(boolean arms) { } - @Override public void setBasePlate(boolean basePlate) { } - @Override public void setBodyPose(EulerAngle pose) { } - @Override public void setBoots(ItemStack item) { } - @Override public void setChestplate(ItemStack item) { } - @Override public void setHeadPose(EulerAngle pose) { } - @Override public void setHelmet(ItemStack item) { } - @Override public void setItemInHand(ItemStack item) { } - @Override public void setLeftArmPose(EulerAngle pose) { } - @Override public void setLeftLegPose(EulerAngle pose) { } - @Override public void setLeggings(ItemStack item) { } - @Override public void setRightArmPose(EulerAngle pose) { } - @Override public void setRightLegPose(EulerAngle pose) { } - @Override public void setSmall(boolean small) { } - @Override public void setVisible(boolean visible) { } - @Override public void setMarker(boolean marker) { } - - // Methods from LivingEntity class - @Override public boolean addPotionEffect(PotionEffect effect) { return false; } - @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } - @Override public boolean addPotionEffects(Collection effects) { return false; } - @Override public void setRemoveWhenFarAway(boolean remove) { } - @Override public void setAI(boolean ai) { } - @Override public void setCanPickupItems(boolean pickup) { } - @Override public void setCollidable(boolean collidable) { } - @Override public void setGliding(boolean gliding) { } - @Override public boolean setLeashHolder(Entity holder) { return false; } - - // Methods from Entity - @Override public void setVelocity(Vector vel) { } - @Override public boolean teleport(Location loc) { return false; } - @Override public boolean teleport(Entity entity) { return false; } - @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } - @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } - @Override public void setFireTicks(int ticks) { } - @Override public boolean setPassenger(Entity entity) { return false; } - @Override public boolean eject() { return false; } - @Override public boolean leaveVehicle() { return false; } - @Override public void playEffect(EntityEffect effect) { } - @Override public void setCustomName(String name) { } - @Override public void setCustomNameVisible(boolean flag) { } - @Override public void setGlowing(boolean flag) { } - @Override public void setGravity(boolean gravity) { } - @Override public void setInvulnerable(boolean flag) { } - @Override public void setMomentum(Vector value) { } - @Override public void setSilent(boolean flag) { } - @Override public void setTicksLived(int value) { } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_11_R1; + +import java.util.Collection; + +import org.bukkit.EntityEffect; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_11_R1.CraftServer; +import org.bukkit.craftbukkit.v1_11_R1.entity.CraftArmorStand; +import org.bukkit.entity.Entity; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import org.bukkit.inventory.ItemStack; +import org.bukkit.potion.PotionEffect; +import org.bukkit.util.EulerAngle; +import org.bukkit.util.Vector; + +public class CraftNMSArmorStand extends CraftArmorStand { + + public CraftNMSArmorStand(CraftServer server, EntityNMSArmorStand entity) { + super(server, entity); + } + + // Disallow all the bukkit methods. + + @Override + public void remove() { + // Cannot be removed, this is the most important to override. + } + + // Methods from Armor stand class + @Override public void setArms(boolean arms) { } + @Override public void setBasePlate(boolean basePlate) { } + @Override public void setBodyPose(EulerAngle pose) { } + @Override public void setBoots(ItemStack item) { } + @Override public void setChestplate(ItemStack item) { } + @Override public void setHeadPose(EulerAngle pose) { } + @Override public void setHelmet(ItemStack item) { } + @Override public void setItemInHand(ItemStack item) { } + @Override public void setLeftArmPose(EulerAngle pose) { } + @Override public void setLeftLegPose(EulerAngle pose) { } + @Override public void setLeggings(ItemStack item) { } + @Override public void setRightArmPose(EulerAngle pose) { } + @Override public void setRightLegPose(EulerAngle pose) { } + @Override public void setSmall(boolean small) { } + @Override public void setVisible(boolean visible) { } + @Override public void setMarker(boolean marker) { } + + // Methods from LivingEntity class + @Override public boolean addPotionEffect(PotionEffect effect) { return false; } + @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } + @Override public boolean addPotionEffects(Collection effects) { return false; } + @Override public void setRemoveWhenFarAway(boolean remove) { } + @Override public void setAI(boolean ai) { } + @Override public void setCanPickupItems(boolean pickup) { } + @Override public void setCollidable(boolean collidable) { } + @Override public void setGliding(boolean gliding) { } + @Override public boolean setLeashHolder(Entity holder) { return false; } + + // Methods from Entity + @Override public void setVelocity(Vector vel) { } + @Override public boolean teleport(Location loc) { return false; } + @Override public boolean teleport(Entity entity) { return false; } + @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } + @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } + @Override public void setFireTicks(int ticks) { } + @Override public boolean setPassenger(Entity entity) { return false; } + @Override public boolean eject() { return false; } + @Override public boolean leaveVehicle() { return false; } + @Override public void playEffect(EntityEffect effect) { } + @Override public void setCustomName(String name) { } + @Override public void setCustomNameVisible(boolean flag) { } + @Override public void setGlowing(boolean flag) { } + @Override public void setGravity(boolean gravity) { } + @Override public void setInvulnerable(boolean flag) { } + @Override public void setMomentum(Vector value) { } + @Override public void setSilent(boolean flag) { } + @Override public void setTicksLived(int value) { } + +} diff --git a/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/CraftNMSItem.java b/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/CraftNMSItem.java index 7a3234dc..7d3eac65 100644 --- a/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/CraftNMSItem.java +++ b/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/CraftNMSItem.java @@ -1,49 +1,63 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_11_R1; - -import org.bukkit.EntityEffect; -import org.bukkit.Location; -import org.bukkit.craftbukkit.v1_11_R1.CraftServer; -import org.bukkit.craftbukkit.v1_11_R1.entity.CraftItem; -import org.bukkit.entity.Entity; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -import org.bukkit.inventory.ItemStack; -import org.bukkit.util.Vector; - -public class CraftNMSItem extends CraftItem { - - public CraftNMSItem(CraftServer server, EntityNMSItem entity) { - super(server, entity); - } - - // Disallow all the bukkit methods. - - @Override - public void remove() { - // Cannot be removed, this is the most important to override. - } - - // Methods from Entity - @Override public void setVelocity(Vector vel) { } - @Override public boolean teleport(Location loc) { return false; } - @Override public boolean teleport(Entity entity) { return false; } - @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } - @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } - @Override public void setFireTicks(int ticks) { } - @Override public boolean setPassenger(Entity entity) { return false; } - @Override public boolean eject() { return false; } - @Override public boolean leaveVehicle() { return false; } - @Override public void playEffect(EntityEffect effect) { } - @Override public void setCustomName(String name) { } - @Override public void setCustomNameVisible(boolean flag) { } - @Override public void setGlowing(boolean flag) { } - @Override public void setGravity(boolean gravity) { } - @Override public void setInvulnerable(boolean flag) { } - @Override public void setMomentum(Vector value) { } - @Override public void setSilent(boolean flag) { } - @Override public void setTicksLived(int value) { } - - // Methods from Item - @Override public void setItemStack(ItemStack stack) { } - @Override public void setPickupDelay(int delay) { } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_11_R1; + +import org.bukkit.EntityEffect; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_11_R1.CraftServer; +import org.bukkit.craftbukkit.v1_11_R1.entity.CraftItem; +import org.bukkit.entity.Entity; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import org.bukkit.inventory.ItemStack; +import org.bukkit.util.Vector; + +public class CraftNMSItem extends CraftItem { + + public CraftNMSItem(CraftServer server, EntityNMSItem entity) { + super(server, entity); + } + + // Disallow all the bukkit methods. + + @Override + public void remove() { + // Cannot be removed, this is the most important to override. + } + + // Methods from Entity + @Override public void setVelocity(Vector vel) { } + @Override public boolean teleport(Location loc) { return false; } + @Override public boolean teleport(Entity entity) { return false; } + @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } + @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } + @Override public void setFireTicks(int ticks) { } + @Override public boolean setPassenger(Entity entity) { return false; } + @Override public boolean eject() { return false; } + @Override public boolean leaveVehicle() { return false; } + @Override public void playEffect(EntityEffect effect) { } + @Override public void setCustomName(String name) { } + @Override public void setCustomNameVisible(boolean flag) { } + @Override public void setGlowing(boolean flag) { } + @Override public void setGravity(boolean gravity) { } + @Override public void setInvulnerable(boolean flag) { } + @Override public void setMomentum(Vector value) { } + @Override public void setSilent(boolean flag) { } + @Override public void setTicksLived(int value) { } + + // Methods from Item + @Override public void setItemStack(ItemStack stack) { } + @Override public void setPickupDelay(int delay) { } + +} diff --git a/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/CraftNMSSlime.java b/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/CraftNMSSlime.java index 70ff929c..559a300d 100644 --- a/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/CraftNMSSlime.java +++ b/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/CraftNMSSlime.java @@ -1,61 +1,75 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_11_R1; - -import java.util.Collection; - -import org.bukkit.EntityEffect; -import org.bukkit.Location; -import org.bukkit.craftbukkit.v1_11_R1.CraftServer; -import org.bukkit.craftbukkit.v1_11_R1.entity.CraftSlime; -import org.bukkit.entity.Entity; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -import org.bukkit.potion.PotionEffect; -import org.bukkit.util.Vector; - -public class CraftNMSSlime extends CraftSlime { - - public CraftNMSSlime(CraftServer server, EntityNMSSlime entity) { - super(server, entity); - } - - // Disallow all the bukkit methods. - - @Override - public void remove() { - // Cannot be removed, this is the most important to override. - } - - // Methods from LivingEntity class - @Override public boolean addPotionEffect(PotionEffect effect) { return false; } - @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } - @Override public boolean addPotionEffects(Collection effects) { return false; } - @Override public void setRemoveWhenFarAway(boolean remove) { } - @Override public void setAI(boolean ai) { } - @Override public void setCanPickupItems(boolean pickup) { } - @Override public void setCollidable(boolean collidable) { } - @Override public void setGliding(boolean gliding) { } - @Override public boolean setLeashHolder(Entity holder) { return false; } - - // Methods from Entity - @Override public void setVelocity(Vector vel) { } - @Override public boolean teleport(Location loc) { return false; } - @Override public boolean teleport(Entity entity) { return false; } - @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } - @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } - @Override public void setFireTicks(int ticks) { } - @Override public boolean setPassenger(Entity entity) { return false; } - @Override public boolean eject() { return false; } - @Override public boolean leaveVehicle() { return false; } - @Override public void playEffect(EntityEffect effect) { } - @Override public void setCustomName(String name) { } - @Override public void setCustomNameVisible(boolean flag) { } - @Override public void setGlowing(boolean flag) { } - @Override public void setGravity(boolean gravity) { } - @Override public void setInvulnerable(boolean flag) { } - @Override public void setMomentum(Vector value) { } - @Override public void setSilent(boolean flag) { } - @Override public void setTicksLived(int value) { } - - // Methods from Slime - @Override public void setSize(int size) { } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_11_R1; + +import java.util.Collection; + +import org.bukkit.EntityEffect; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_11_R1.CraftServer; +import org.bukkit.craftbukkit.v1_11_R1.entity.CraftSlime; +import org.bukkit.entity.Entity; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import org.bukkit.potion.PotionEffect; +import org.bukkit.util.Vector; + +public class CraftNMSSlime extends CraftSlime { + + public CraftNMSSlime(CraftServer server, EntityNMSSlime entity) { + super(server, entity); + } + + // Disallow all the bukkit methods. + + @Override + public void remove() { + // Cannot be removed, this is the most important to override. + } + + // Methods from LivingEntity class + @Override public boolean addPotionEffect(PotionEffect effect) { return false; } + @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } + @Override public boolean addPotionEffects(Collection effects) { return false; } + @Override public void setRemoveWhenFarAway(boolean remove) { } + @Override public void setAI(boolean ai) { } + @Override public void setCanPickupItems(boolean pickup) { } + @Override public void setCollidable(boolean collidable) { } + @Override public void setGliding(boolean gliding) { } + @Override public boolean setLeashHolder(Entity holder) { return false; } + + // Methods from Entity + @Override public void setVelocity(Vector vel) { } + @Override public boolean teleport(Location loc) { return false; } + @Override public boolean teleport(Entity entity) { return false; } + @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } + @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } + @Override public void setFireTicks(int ticks) { } + @Override public boolean setPassenger(Entity entity) { return false; } + @Override public boolean eject() { return false; } + @Override public boolean leaveVehicle() { return false; } + @Override public void playEffect(EntityEffect effect) { } + @Override public void setCustomName(String name) { } + @Override public void setCustomNameVisible(boolean flag) { } + @Override public void setGlowing(boolean flag) { } + @Override public void setGravity(boolean gravity) { } + @Override public void setInvulnerable(boolean flag) { } + @Override public void setMomentum(Vector value) { } + @Override public void setSilent(boolean flag) { } + @Override public void setTicksLived(int value) { } + + // Methods from Slime + @Override public void setSize(int size) { } + +} diff --git a/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/EntityNMSArmorStand.java b/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/EntityNMSArmorStand.java index 6a0cf533..7599b52e 100644 --- a/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/EntityNMSArmorStand.java +++ b/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/EntityNMSArmorStand.java @@ -1,240 +1,254 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_11_R1; - -import org.bukkit.craftbukkit.v1_11_R1.entity.CraftEntity; - -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; -import com.gmail.filoghost.holographicdisplays.disk.Configuration; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; -import com.gmail.filoghost.holographicdisplays.util.Utils; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectionUtils; - -import net.minecraft.server.v1_11_R1.AxisAlignedBB; -import net.minecraft.server.v1_11_R1.DamageSource; -import net.minecraft.server.v1_11_R1.EntityArmorStand; -import net.minecraft.server.v1_11_R1.EntityHuman; -import net.minecraft.server.v1_11_R1.EntityPlayer; -import net.minecraft.server.v1_11_R1.EnumHand; -import net.minecraft.server.v1_11_R1.EnumInteractionResult; -import net.minecraft.server.v1_11_R1.EnumItemSlot; -import net.minecraft.server.v1_11_R1.ItemStack; -import net.minecraft.server.v1_11_R1.NBTTagCompound; -import net.minecraft.server.v1_11_R1.PacketPlayOutEntityTeleport; -import net.minecraft.server.v1_11_R1.SoundEffect; -import net.minecraft.server.v1_11_R1.Vec3D; -import net.minecraft.server.v1_11_R1.World; - -public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorStand { - - private boolean lockTick; - private HologramLine parentPiece; - - public EntityNMSArmorStand(World world, HologramLine parentPiece) { - super(world); - super.setInvisible(true); - super.setSmall(true); - super.setArms(false); - super.setNoGravity(true); - super.setBasePlate(true); - super.setMarker(true); - super.collides = false; - this.parentPiece = parentPiece; - forceSetBoundingBox(new NullBoundingBox()); - } - - - @Override - public void b(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public boolean c(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public boolean d(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public NBTTagCompound e(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return nbttagcompound; - } - - @Override - public void f(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public void a(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public boolean isInvulnerable(DamageSource source) { - /* - * The field Entity.invulnerable is private. - * It's only used while saving NBTTags, but since the entity would be killed - * on chunk unload, we prefer to override isInvulnerable(). - */ - return true; - } - - @Override - public boolean isCollidable() { - return false; - } - - @Override - public void setCustomName(String customName) { - // Locks the custom name. - } - - @Override - public void inactiveTick() { - // Check inactive ticks. - - if (!lockTick) { - super.inactiveTick(); - } - } - - @Override - public void setCustomNameVisible(boolean visible) { - // Locks the custom name. - } - - @Override - public EnumInteractionResult a(EntityHuman human, Vec3D vec3d, EnumHand enumhand) { - // Prevent stand being equipped - return EnumInteractionResult.PASS; - } - - @Override - public boolean c(int i, ItemStack item) { - // Prevent stand being equipped - return false; - } - - @Override - public void setSlot(EnumItemSlot enumitemslot, ItemStack itemstack) { - // Prevent stand being equipped - } - - @Override - public void a(AxisAlignedBB boundingBox) { - // Do not change it! - } - - public void forceSetBoundingBox(AxisAlignedBB boundingBox) { - super.a(boundingBox); - } - - @Override - public int getId() { - if (Configuration.preciseHologramMovement) { - StackTraceElement element = ReflectionUtils.getStackTraceElement(2); - if (element != null && element.getFileName() != null && element.getFileName().equals("EntityTrackerEntry.java") && 158 < element.getLineNumber() && element.getLineNumber() < 168) { - // Then this method is being called when creating a new movement packet, we return a fake ID! - return -1; - } - } - - return super.getId(); - } - - @Override - public void A_() { - if (!lockTick) { - super.A_(); - } - } - - @Override - public void a(SoundEffect soundeffect, float f, float f1) { - // Remove sounds. - } - - @Override - public void setCustomNameNMS(String name) { - if (name != null && name.length() > 300) { - name = name.substring(0, 300); - } - super.setCustomName(name); - super.setCustomNameVisible(name != null && !name.isEmpty()); - } - - @Override - public String getCustomNameNMS() { - return super.getCustomName(); - } - - @Override - public void setLockTick(boolean lock) { - lockTick = lock; - } - - @Override - public void die() { - // Prevent being killed. - } - - @Override - public CraftEntity getBukkitEntity() { - if (super.bukkitEntity == null) { - super.bukkitEntity = new CraftNMSArmorStand(super.world.getServer(), this); - } - return super.bukkitEntity; - } - - @Override - public void killEntityNMS() { - super.dead = true; - } - - @Override - public void setLocationNMS(double x, double y, double z) { - super.setPosition(x, y, z); - - if (Configuration.preciseHologramMovement) { - // Send a packet near to update the position. - PacketPlayOutEntityTeleport teleportPacket = new PacketPlayOutEntityTeleport(this); - - for (Object obj : super.world.players) { - if (obj instanceof EntityPlayer) { - EntityPlayer nmsPlayer = (EntityPlayer) obj; - - double distanceSquared = Utils.square(nmsPlayer.locX - super.locX) + Utils.square(nmsPlayer.locZ - super.locZ); - if (distanceSquared < 8192 && nmsPlayer.playerConnection != null) { - nmsPlayer.playerConnection.sendPacket(teleportPacket); - } - } - } - } - } - - @Override - public boolean isDeadNMS() { - return super.dead; - } - - @Override - public int getIdNMS() { - return super.getId(); // Return the real ID without checking the stack trace. - } - - @Override - public HologramLine getHologramLine() { - return parentPiece; - } - - @Override - public org.bukkit.entity.Entity getBukkitEntityNMS() { - return getBukkitEntity(); - } -} \ No newline at end of file +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_11_R1; + +import org.bukkit.craftbukkit.v1_11_R1.entity.CraftEntity; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; +import com.gmail.filoghost.holographicdisplays.disk.Configuration; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; +import com.gmail.filoghost.holographicdisplays.util.Utils; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectionUtils; + +import net.minecraft.server.v1_11_R1.AxisAlignedBB; +import net.minecraft.server.v1_11_R1.DamageSource; +import net.minecraft.server.v1_11_R1.EntityArmorStand; +import net.minecraft.server.v1_11_R1.EntityHuman; +import net.minecraft.server.v1_11_R1.EntityPlayer; +import net.minecraft.server.v1_11_R1.EnumHand; +import net.minecraft.server.v1_11_R1.EnumInteractionResult; +import net.minecraft.server.v1_11_R1.EnumItemSlot; +import net.minecraft.server.v1_11_R1.ItemStack; +import net.minecraft.server.v1_11_R1.NBTTagCompound; +import net.minecraft.server.v1_11_R1.PacketPlayOutEntityTeleport; +import net.minecraft.server.v1_11_R1.SoundEffect; +import net.minecraft.server.v1_11_R1.Vec3D; +import net.minecraft.server.v1_11_R1.World; + +public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorStand { + + private boolean lockTick; + private HologramLine parentPiece; + + public EntityNMSArmorStand(World world, HologramLine parentPiece) { + super(world); + super.setInvisible(true); + super.setSmall(true); + super.setArms(false); + super.setNoGravity(true); + super.setBasePlate(true); + super.setMarker(true); + super.collides = false; + this.parentPiece = parentPiece; + forceSetBoundingBox(new NullBoundingBox()); + } + + + @Override + public void b(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public boolean c(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public boolean d(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public NBTTagCompound e(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return nbttagcompound; + } + + @Override + public void f(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public void a(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public boolean isInvulnerable(DamageSource source) { + /* + * The field Entity.invulnerable is private. + * It's only used while saving NBTTags, but since the entity would be killed + * on chunk unload, we prefer to override isInvulnerable(). + */ + return true; + } + + @Override + public boolean isCollidable() { + return false; + } + + @Override + public void setCustomName(String customName) { + // Locks the custom name. + } + + @Override + public void inactiveTick() { + // Check inactive ticks. + + if (!lockTick) { + super.inactiveTick(); + } + } + + @Override + public void setCustomNameVisible(boolean visible) { + // Locks the custom name. + } + + @Override + public EnumInteractionResult a(EntityHuman human, Vec3D vec3d, EnumHand enumhand) { + // Prevent stand being equipped + return EnumInteractionResult.PASS; + } + + @Override + public boolean c(int i, ItemStack item) { + // Prevent stand being equipped + return false; + } + + @Override + public void setSlot(EnumItemSlot enumitemslot, ItemStack itemstack) { + // Prevent stand being equipped + } + + @Override + public void a(AxisAlignedBB boundingBox) { + // Do not change it! + } + + public void forceSetBoundingBox(AxisAlignedBB boundingBox) { + super.a(boundingBox); + } + + @Override + public int getId() { + if (Configuration.preciseHologramMovement) { + StackTraceElement element = ReflectionUtils.getStackTraceElement(2); + if (element != null && element.getFileName() != null && element.getFileName().equals("EntityTrackerEntry.java") && 158 < element.getLineNumber() && element.getLineNumber() < 168) { + // Then this method is being called when creating a new movement packet, we return a fake ID! + return -1; + } + } + + return super.getId(); + } + + @Override + public void A_() { + if (!lockTick) { + super.A_(); + } + } + + @Override + public void a(SoundEffect soundeffect, float f, float f1) { + // Remove sounds. + } + + @Override + public void setCustomNameNMS(String name) { + if (name != null && name.length() > 300) { + name = name.substring(0, 300); + } + super.setCustomName(name); + super.setCustomNameVisible(name != null && !name.isEmpty()); + } + + @Override + public String getCustomNameNMS() { + return super.getCustomName(); + } + + @Override + public void setLockTick(boolean lock) { + lockTick = lock; + } + + @Override + public void die() { + // Prevent being killed. + } + + @Override + public CraftEntity getBukkitEntity() { + if (super.bukkitEntity == null) { + super.bukkitEntity = new CraftNMSArmorStand(super.world.getServer(), this); + } + return super.bukkitEntity; + } + + @Override + public void killEntityNMS() { + super.dead = true; + } + + @Override + public void setLocationNMS(double x, double y, double z) { + super.setPosition(x, y, z); + + if (Configuration.preciseHologramMovement) { + // Send a packet near to update the position. + PacketPlayOutEntityTeleport teleportPacket = new PacketPlayOutEntityTeleport(this); + + for (Object obj : super.world.players) { + if (obj instanceof EntityPlayer) { + EntityPlayer nmsPlayer = (EntityPlayer) obj; + + double distanceSquared = Utils.square(nmsPlayer.locX - super.locX) + Utils.square(nmsPlayer.locZ - super.locZ); + if (distanceSquared < 8192 && nmsPlayer.playerConnection != null) { + nmsPlayer.playerConnection.sendPacket(teleportPacket); + } + } + } + } + } + + @Override + public boolean isDeadNMS() { + return super.dead; + } + + @Override + public int getIdNMS() { + return super.getId(); // Return the real ID without checking the stack trace. + } + + @Override + public HologramLine getHologramLine() { + return parentPiece; + } + + @Override + public org.bukkit.entity.Entity getBukkitEntityNMS() { + return getBukkitEntity(); + } +} diff --git a/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/EntityNMSItem.java b/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/EntityNMSItem.java index afb00008..01ef39b5 100644 --- a/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/EntityNMSItem.java +++ b/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/EntityNMSItem.java @@ -1,247 +1,261 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_11_R1; - -import org.bukkit.craftbukkit.v1_11_R1.entity.CraftEntity; -import org.bukkit.craftbukkit.v1_11_R1.inventory.CraftItemStack; -import org.bukkit.entity.Player; - -import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.ItemUtils; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; - -import net.minecraft.server.v1_11_R1.Blocks; -import net.minecraft.server.v1_11_R1.DamageSource; -import net.minecraft.server.v1_11_R1.Entity; -import net.minecraft.server.v1_11_R1.EntityHuman; -import net.minecraft.server.v1_11_R1.EntityItem; -import net.minecraft.server.v1_11_R1.EntityPlayer; -import net.minecraft.server.v1_11_R1.ItemStack; -import net.minecraft.server.v1_11_R1.NBTTagCompound; -import net.minecraft.server.v1_11_R1.NBTTagList; -import net.minecraft.server.v1_11_R1.NBTTagString; -import net.minecraft.server.v1_11_R1.World; -import net.minecraft.server.v1_11_R1.AxisAlignedBB; - -public class EntityNMSItem extends EntityItem implements NMSItem { - - private static final ReflectField VEHICLE_FIELD = new ReflectField(Entity.class, "au"); - - private boolean lockTick; - private ItemLine parentPiece; - private ItemPickupManager itemPickupManager; - - public EntityNMSItem(World world, ItemLine piece, ItemPickupManager itemPickupManager) { - super(world); - super.pickupDelay = 32767; // Lock the item pickup delay, also prevents entities from picking up the item - this.parentPiece = piece; - this.itemPickupManager = itemPickupManager; - forceSetBoundingBox(new NullBoundingBox()); - } - - @Override - public void A_() { - - // So it won't get removed. - ticksLived = 0; - - if (!lockTick) { - super.A_(); - } - } - - // Method called when a player is near. - @Override - public void d(EntityHuman human) { - - if (human.locY < super.locY - 1.5 || human.locY > super.locY + 1.0) { - // Too low or too high, it's a bit weird. - return; - } - - if (parentPiece.getPickupHandler() != null && human instanceof EntityPlayer) { - itemPickupManager.handleItemLinePickup((Player) human.getBukkitEntity(), parentPiece.getPickupHandler(), parentPiece.getParent()); - // It is never added to the inventory. - } - } - - @Override - public void b(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public boolean c(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public boolean d(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public NBTTagCompound e(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return nbttagcompound; - } - - @Override - public void f(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public void a(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public boolean isInvulnerable(DamageSource source) { - /* - * The field Entity.invulnerable is private. - * It's only used while saving NBTTags, but since the entity would be killed - * on chunk unload, we prefer to override isInvulnerable(). - */ - return true; - } - - @Override - public boolean isCollidable() { - return false; - } - - @Override - public void a(AxisAlignedBB boundingBox) { - // Do not change it! - } - - public void forceSetBoundingBox(AxisAlignedBB boundingBox) { - super.a(boundingBox); - } - - @Override - public void inactiveTick() { - // Check inactive ticks. - - if (!lockTick) { - super.inactiveTick(); - } - } - - @Override - public void setLockTick(boolean lock) { - lockTick = lock; - } - - @Override - public void die() { - // Prevent being killed. - } - - @Override - public CraftEntity getBukkitEntity() { - if (super.bukkitEntity == null) { - super.bukkitEntity = new CraftNMSItem(super.world.getServer(), this); - } - return super.bukkitEntity; - } - - @Override - public boolean isDeadNMS() { - return super.dead; - } - - @Override - public void killEntityNMS() { - super.dead = true; - } - - @Override - public void setLocationNMS(double x, double y, double z) { - super.setPosition(x, y, z); - } - - @Override - public void setItemStackNMS(org.bukkit.inventory.ItemStack stack) { - ItemStack newItem = CraftItemStack.asNMSCopy(stack); // ItemStack.a is returned if the stack is null, invalid or the material is not an Item - - if (newItem == null || newItem == ItemStack.a) { - newItem = new ItemStack(Blocks.BEDROCK); - } - - if (newItem.getTag() == null) { - newItem.setTag(new NBTTagCompound()); - } - NBTTagCompound display = newItem.getTag().getCompound("display"); // Returns a new NBTTagCompound if not existing - if (!newItem.getTag().hasKey("display")) { - newItem.getTag().set("display", display); - } - - NBTTagList tagList = new NBTTagList(); - tagList.add(new NBTTagString(ItemUtils.ANTISTACK_LORE)); // Antistack lore - display.set("Lore", tagList); - - newItem.setCount(1); - - setItemStack(newItem); - } - - @Override - public int getIdNMS() { - return super.getId(); - } - - @Override - public ItemLine getHologramLine() { - return parentPiece; - } - - @Override - public void allowPickup(boolean pickup) { - if (pickup) { - super.pickupDelay = 0; - } else { - super.pickupDelay = 32767; - } - } - - @Override - public org.bukkit.entity.Entity getBukkitEntityNMS() { - return getBukkitEntity(); - } - - @Override - public void setPassengerOfNMS(NMSEntityBase vehicleBase) { - if (vehicleBase == null || !(vehicleBase instanceof Entity)) { - // It should never dismount - return; - } - - Entity entity = (Entity) vehicleBase; - - try { - if (super.bB() != null) { - Entity oldVehicle = super.bB(); - VEHICLE_FIELD.set(this, null); - oldVehicle.passengers.remove(this); - } - - VEHICLE_FIELD.set(this, entity); - entity.passengers.clear(); - entity.passengers.add(this); - - } catch (Exception ex) { - ConsoleLogger.logDebugException(ex); - } - } - - @Override - public Object getRawItemStack() { - return super.getItemStack(); - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_11_R1; + +import org.bukkit.craftbukkit.v1_11_R1.entity.CraftEntity; +import org.bukkit.craftbukkit.v1_11_R1.inventory.CraftItemStack; +import org.bukkit.entity.Player; + +import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.ItemUtils; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; + +import net.minecraft.server.v1_11_R1.Blocks; +import net.minecraft.server.v1_11_R1.DamageSource; +import net.minecraft.server.v1_11_R1.Entity; +import net.minecraft.server.v1_11_R1.EntityHuman; +import net.minecraft.server.v1_11_R1.EntityItem; +import net.minecraft.server.v1_11_R1.EntityPlayer; +import net.minecraft.server.v1_11_R1.ItemStack; +import net.minecraft.server.v1_11_R1.NBTTagCompound; +import net.minecraft.server.v1_11_R1.NBTTagList; +import net.minecraft.server.v1_11_R1.NBTTagString; +import net.minecraft.server.v1_11_R1.World; +import net.minecraft.server.v1_11_R1.AxisAlignedBB; + +public class EntityNMSItem extends EntityItem implements NMSItem { + + private static final ReflectField VEHICLE_FIELD = new ReflectField(Entity.class, "au"); + + private boolean lockTick; + private ItemLine parentPiece; + private ItemPickupManager itemPickupManager; + + public EntityNMSItem(World world, ItemLine piece, ItemPickupManager itemPickupManager) { + super(world); + super.pickupDelay = 32767; // Lock the item pickup delay, also prevents entities from picking up the item + this.parentPiece = piece; + this.itemPickupManager = itemPickupManager; + forceSetBoundingBox(new NullBoundingBox()); + } + + @Override + public void A_() { + + // So it won't get removed. + ticksLived = 0; + + if (!lockTick) { + super.A_(); + } + } + + // Method called when a player is near. + @Override + public void d(EntityHuman human) { + + if (human.locY < super.locY - 1.5 || human.locY > super.locY + 1.0) { + // Too low or too high, it's a bit weird. + return; + } + + if (parentPiece.getPickupHandler() != null && human instanceof EntityPlayer) { + itemPickupManager.handleItemLinePickup((Player) human.getBukkitEntity(), parentPiece.getPickupHandler(), parentPiece.getParent()); + // It is never added to the inventory. + } + } + + @Override + public void b(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public boolean c(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public boolean d(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public NBTTagCompound e(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return nbttagcompound; + } + + @Override + public void f(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public void a(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public boolean isInvulnerable(DamageSource source) { + /* + * The field Entity.invulnerable is private. + * It's only used while saving NBTTags, but since the entity would be killed + * on chunk unload, we prefer to override isInvulnerable(). + */ + return true; + } + + @Override + public boolean isCollidable() { + return false; + } + + @Override + public void a(AxisAlignedBB boundingBox) { + // Do not change it! + } + + public void forceSetBoundingBox(AxisAlignedBB boundingBox) { + super.a(boundingBox); + } + + @Override + public void inactiveTick() { + // Check inactive ticks. + + if (!lockTick) { + super.inactiveTick(); + } + } + + @Override + public void setLockTick(boolean lock) { + lockTick = lock; + } + + @Override + public void die() { + // Prevent being killed. + } + + @Override + public CraftEntity getBukkitEntity() { + if (super.bukkitEntity == null) { + super.bukkitEntity = new CraftNMSItem(super.world.getServer(), this); + } + return super.bukkitEntity; + } + + @Override + public boolean isDeadNMS() { + return super.dead; + } + + @Override + public void killEntityNMS() { + super.dead = true; + } + + @Override + public void setLocationNMS(double x, double y, double z) { + super.setPosition(x, y, z); + } + + @Override + public void setItemStackNMS(org.bukkit.inventory.ItemStack stack) { + ItemStack newItem = CraftItemStack.asNMSCopy(stack); // ItemStack.a is returned if the stack is null, invalid or the material is not an Item + + if (newItem == null || newItem == ItemStack.a) { + newItem = new ItemStack(Blocks.BEDROCK); + } + + if (newItem.getTag() == null) { + newItem.setTag(new NBTTagCompound()); + } + NBTTagCompound display = newItem.getTag().getCompound("display"); // Returns a new NBTTagCompound if not existing + if (!newItem.getTag().hasKey("display")) { + newItem.getTag().set("display", display); + } + + NBTTagList tagList = new NBTTagList(); + tagList.add(new NBTTagString(ItemUtils.ANTISTACK_LORE)); // Antistack lore + display.set("Lore", tagList); + + newItem.setCount(1); + + setItemStack(newItem); + } + + @Override + public int getIdNMS() { + return super.getId(); + } + + @Override + public ItemLine getHologramLine() { + return parentPiece; + } + + @Override + public void allowPickup(boolean pickup) { + if (pickup) { + super.pickupDelay = 0; + } else { + super.pickupDelay = 32767; + } + } + + @Override + public org.bukkit.entity.Entity getBukkitEntityNMS() { + return getBukkitEntity(); + } + + @Override + public void setPassengerOfNMS(NMSEntityBase vehicleBase) { + if (vehicleBase == null || !(vehicleBase instanceof Entity)) { + // It should never dismount + return; + } + + Entity entity = (Entity) vehicleBase; + + try { + if (super.bB() != null) { + Entity oldVehicle = super.bB(); + VEHICLE_FIELD.set(this, null); + oldVehicle.passengers.remove(this); + } + + VEHICLE_FIELD.set(this, entity); + entity.passengers.clear(); + entity.passengers.add(this); + + } catch (Exception ex) { + ConsoleLogger.logDebugException(ex); + } + } + + @Override + public Object getRawItemStack() { + return super.getItemStack(); + } +} diff --git a/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/EntityNMSSlime.java b/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/EntityNMSSlime.java index 54071ee5..cef52723 100644 --- a/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/EntityNMSSlime.java +++ b/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/EntityNMSSlime.java @@ -1,216 +1,230 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_11_R1; - -import org.bukkit.Bukkit; -import org.bukkit.craftbukkit.v1_11_R1.entity.CraftEntity; -import org.bukkit.event.player.PlayerInteractEntityEvent; - -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSSlime; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; - -import net.minecraft.server.v1_11_R1.AxisAlignedBB; -import net.minecraft.server.v1_11_R1.DamageSource; -import net.minecraft.server.v1_11_R1.Entity; -import net.minecraft.server.v1_11_R1.EntityDamageSource; -import net.minecraft.server.v1_11_R1.EntityPlayer; -import net.minecraft.server.v1_11_R1.EntitySlime; -import net.minecraft.server.v1_11_R1.NBTTagCompound; -import net.minecraft.server.v1_11_R1.SoundEffect; -import net.minecraft.server.v1_11_R1.World; - -public class EntityNMSSlime extends EntitySlime implements NMSSlime { - - private static final ReflectField VEHICLE_FIELD = new ReflectField(Entity.class, "au"); - - private boolean lockTick; - private HologramLine parentPiece; - - public EntityNMSSlime(World world, HologramLine parentPiece) { - super(world); - super.persistent = true; - super.collides = false; - a(0.0F, 0.0F); - setSize(1, false); - setInvisible(true); - this.parentPiece = parentPiece; - forceSetBoundingBox(new NullBoundingBox()); - } - - @Override - public void a(AxisAlignedBB boundingBox) { - // Do not change it! - } - - public void forceSetBoundingBox(AxisAlignedBB boundingBox) { - super.a(boundingBox); - } - - @Override - public void A_() { - - // So it won't get removed. - ticksLived = 0; - - if (!lockTick) { - super.A_(); - } - } - - @Override - public void b(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public boolean c(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public boolean d(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public NBTTagCompound e(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return nbttagcompound; - } - - @Override - public void f(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public void a(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public boolean damageEntity(DamageSource damageSource, float amount) { - if (damageSource instanceof EntityDamageSource) { - EntityDamageSource entityDamageSource = (EntityDamageSource) damageSource; - if (entityDamageSource.getEntity() instanceof EntityPlayer) { - Bukkit.getPluginManager().callEvent(new PlayerInteractEntityEvent(((EntityPlayer) entityDamageSource.getEntity()).getBukkitEntity(), getBukkitEntity())); // Bukkit takes care of the exceptions - } - } - return false; - } - - @Override - public boolean isInvulnerable(DamageSource source) { - /* - * The field Entity.invulnerable is private. - * It's only used while saving NBTTags, but since the entity would be killed - * on chunk unload, we prefer to override isInvulnerable(). - */ - return true; - } - - @Override - public boolean isCollidable() { - return false; - } - - @Override - public void inactiveTick() { - // Check inactive ticks. - - if (!lockTick) { - super.inactiveTick(); - } - } - - @Override - public void setCustomName(String customName) { - // Locks the custom name. - } - - @Override - public void setCustomNameVisible(boolean visible) { - // Locks the custom name. - } - - @Override - public void a(SoundEffect soundeffect, float f, float f1) { - // Remove sounds. - } - - @Override - public void setLockTick(boolean lock) { - lockTick = lock; - } - - @Override - public void die() { - // Prevent being killed. - } - - @Override - public CraftEntity getBukkitEntity() { - if (super.bukkitEntity == null) { - super.bukkitEntity = new CraftNMSSlime(super.world.getServer(), this); - } - return super.bukkitEntity; - } - - @Override - public boolean isDeadNMS() { - return super.dead; - } - - @Override - public void killEntityNMS() { - super.dead = true; - } - - @Override - public void setLocationNMS(double x, double y, double z) { - super.setPosition(x, y, z); - } - - @Override - public int getIdNMS() { - return super.getId(); - } - - @Override - public HologramLine getHologramLine() { - return parentPiece; - } - - @Override - public org.bukkit.entity.Entity getBukkitEntityNMS() { - return getBukkitEntity(); - } - - @Override - public void setPassengerOfNMS(NMSEntityBase vehicleBase) { - if (vehicleBase == null || !(vehicleBase instanceof Entity)) { - // It should never dismount - return; - } - - Entity entity = (Entity) vehicleBase; - - try { - if (super.bB() != null) { - Entity oldVehicle = super.bB(); - VEHICLE_FIELD.set(this, null); - oldVehicle.passengers.remove(this); - } - - VEHICLE_FIELD.set(this, entity); - entity.passengers.clear(); - entity.passengers.add(this); - - } catch (Exception ex) { - ConsoleLogger.logDebugException(ex); - } - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_11_R1; + +import org.bukkit.Bukkit; +import org.bukkit.craftbukkit.v1_11_R1.entity.CraftEntity; +import org.bukkit.event.player.PlayerInteractEntityEvent; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSSlime; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; + +import net.minecraft.server.v1_11_R1.AxisAlignedBB; +import net.minecraft.server.v1_11_R1.DamageSource; +import net.minecraft.server.v1_11_R1.Entity; +import net.minecraft.server.v1_11_R1.EntityDamageSource; +import net.minecraft.server.v1_11_R1.EntityPlayer; +import net.minecraft.server.v1_11_R1.EntitySlime; +import net.minecraft.server.v1_11_R1.NBTTagCompound; +import net.minecraft.server.v1_11_R1.SoundEffect; +import net.minecraft.server.v1_11_R1.World; + +public class EntityNMSSlime extends EntitySlime implements NMSSlime { + + private static final ReflectField VEHICLE_FIELD = new ReflectField(Entity.class, "au"); + + private boolean lockTick; + private HologramLine parentPiece; + + public EntityNMSSlime(World world, HologramLine parentPiece) { + super(world); + super.persistent = true; + super.collides = false; + a(0.0F, 0.0F); + setSize(1, false); + setInvisible(true); + this.parentPiece = parentPiece; + forceSetBoundingBox(new NullBoundingBox()); + } + + @Override + public void a(AxisAlignedBB boundingBox) { + // Do not change it! + } + + public void forceSetBoundingBox(AxisAlignedBB boundingBox) { + super.a(boundingBox); + } + + @Override + public void A_() { + + // So it won't get removed. + ticksLived = 0; + + if (!lockTick) { + super.A_(); + } + } + + @Override + public void b(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public boolean c(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public boolean d(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public NBTTagCompound e(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return nbttagcompound; + } + + @Override + public void f(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public void a(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public boolean damageEntity(DamageSource damageSource, float amount) { + if (damageSource instanceof EntityDamageSource) { + EntityDamageSource entityDamageSource = (EntityDamageSource) damageSource; + if (entityDamageSource.getEntity() instanceof EntityPlayer) { + Bukkit.getPluginManager().callEvent(new PlayerInteractEntityEvent(((EntityPlayer) entityDamageSource.getEntity()).getBukkitEntity(), getBukkitEntity())); // Bukkit takes care of the exceptions + } + } + return false; + } + + @Override + public boolean isInvulnerable(DamageSource source) { + /* + * The field Entity.invulnerable is private. + * It's only used while saving NBTTags, but since the entity would be killed + * on chunk unload, we prefer to override isInvulnerable(). + */ + return true; + } + + @Override + public boolean isCollidable() { + return false; + } + + @Override + public void inactiveTick() { + // Check inactive ticks. + + if (!lockTick) { + super.inactiveTick(); + } + } + + @Override + public void setCustomName(String customName) { + // Locks the custom name. + } + + @Override + public void setCustomNameVisible(boolean visible) { + // Locks the custom name. + } + + @Override + public void a(SoundEffect soundeffect, float f, float f1) { + // Remove sounds. + } + + @Override + public void setLockTick(boolean lock) { + lockTick = lock; + } + + @Override + public void die() { + // Prevent being killed. + } + + @Override + public CraftEntity getBukkitEntity() { + if (super.bukkitEntity == null) { + super.bukkitEntity = new CraftNMSSlime(super.world.getServer(), this); + } + return super.bukkitEntity; + } + + @Override + public boolean isDeadNMS() { + return super.dead; + } + + @Override + public void killEntityNMS() { + super.dead = true; + } + + @Override + public void setLocationNMS(double x, double y, double z) { + super.setPosition(x, y, z); + } + + @Override + public int getIdNMS() { + return super.getId(); + } + + @Override + public HologramLine getHologramLine() { + return parentPiece; + } + + @Override + public org.bukkit.entity.Entity getBukkitEntityNMS() { + return getBukkitEntity(); + } + + @Override + public void setPassengerOfNMS(NMSEntityBase vehicleBase) { + if (vehicleBase == null || !(vehicleBase instanceof Entity)) { + // It should never dismount + return; + } + + Entity entity = (Entity) vehicleBase; + + try { + if (super.bB() != null) { + Entity oldVehicle = super.bB(); + VEHICLE_FIELD.set(this, null); + oldVehicle.passengers.remove(this); + } + + VEHICLE_FIELD.set(this, entity); + entity.passengers.clear(); + entity.passengers.add(this); + + } catch (Exception ex) { + ConsoleLogger.logDebugException(ex); + } + } +} diff --git a/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/NmsManagerImpl.java b/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/NmsManagerImpl.java index bc7ebfc3..00f36a22 100644 --- a/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/NmsManagerImpl.java +++ b/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/NmsManagerImpl.java @@ -1,133 +1,147 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_11_R1; - -import java.lang.reflect.Method; - -import org.bukkit.Bukkit; -import org.bukkit.craftbukkit.v1_11_R1.CraftWorld; -import org.bukkit.craftbukkit.v1_11_R1.entity.CraftEntity; -import org.bukkit.inventory.ItemStack; - -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; -import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.Validator; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; - -import net.minecraft.server.v1_11_R1.Entity; -import net.minecraft.server.v1_11_R1.EntityTypes; -import net.minecraft.server.v1_11_R1.MathHelper; -import net.minecraft.server.v1_11_R1.RegistryID; -import net.minecraft.server.v1_11_R1.RegistryMaterials; -import net.minecraft.server.v1_11_R1.World; -import net.minecraft.server.v1_11_R1.WorldServer; - -public class NmsManagerImpl implements NMSManager { - - private static final ReflectField>> REGISTRY_ID_FIELD = new ReflectField>>(RegistryMaterials.class, "a"); - private static final ReflectField ID_TO_CLASS_MAP_FIELD = new ReflectField(RegistryID.class, "d"); - - private Method validateEntityMethod; - - @Override - public void setup() throws Exception { - validateEntityMethod = World.class.getDeclaredMethod("b", Entity.class); - validateEntityMethod.setAccessible(true); - - registerCustomEntity(EntityNMSSlime.class, 55); - } - - public void registerCustomEntity(Class entityClass, int id) throws Exception { - // Use reflection to get the RegistryID of entities. - RegistryID> registryID = REGISTRY_ID_FIELD.get(EntityTypes.b); - Object[] idToClassMap = ID_TO_CLASS_MAP_FIELD.get(registryID); - - // Save the the ID -> entity class mapping before the registration. - Object oldValue = idToClassMap[id]; - - // Register the entity class. - registryID.a(entityClass, id); - - // Restore the ID -> entity class mapping. - idToClassMap[id] = oldValue; - } - - @Override - public NMSItem spawnNMSItem(org.bukkit.World bukkitWorld, double x, double y, double z, ItemLine parentPiece, ItemStack stack, ItemPickupManager itemPickupManager) { - WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); - EntityNMSItem customItem = new EntityNMSItem(nmsWorld, parentPiece, itemPickupManager); - customItem.setLocationNMS(x, y, z); - customItem.setItemStackNMS(stack); - if (!addEntityToWorld(nmsWorld, customItem)) { - ConsoleLogger.handleSpawnFail(parentPiece); - } - return customItem; - } - - @Override - public EntityNMSSlime spawnNMSSlime(org.bukkit.World bukkitWorld, double x, double y, double z, HologramLine parentPiece) { - WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); - EntityNMSSlime touchSlime = new EntityNMSSlime(nmsWorld, parentPiece); - touchSlime.setLocationNMS(x, y, z); - if (!addEntityToWorld(nmsWorld, touchSlime)) { - ConsoleLogger.handleSpawnFail(parentPiece); - } - return touchSlime; - } - - @Override - public NMSArmorStand spawnNMSArmorStand(org.bukkit.World world, double x, double y, double z, HologramLine parentPiece) { - WorldServer nmsWorld = ((CraftWorld) world).getHandle(); - EntityNMSArmorStand invisibleArmorStand = new EntityNMSArmorStand(nmsWorld, parentPiece); - invisibleArmorStand.setLocationNMS(x, y, z); - if (!addEntityToWorld(nmsWorld, invisibleArmorStand)) { - ConsoleLogger.handleSpawnFail(parentPiece); - } - return invisibleArmorStand; - } - - private boolean addEntityToWorld(WorldServer nmsWorld, Entity nmsEntity) { - Validator.isTrue(Bukkit.isPrimaryThread(), "Async entity add"); - - final int chunkX = MathHelper.floor(nmsEntity.locX / 16.0); - final int chunkZ = MathHelper.floor(nmsEntity.locZ / 16.0); - - if (!nmsWorld.getChunkProviderServer().isLoaded(chunkX, chunkZ)) { - // This should never happen - nmsEntity.dead = true; - return false; - } - - nmsWorld.getChunkAt(chunkX, chunkZ).a(nmsEntity); - nmsWorld.entityList.add(nmsEntity); - - try { - validateEntityMethod.invoke(nmsWorld, nmsEntity); - } catch (Exception e) { - e.printStackTrace(); - return false; - } - return true; - } - - @Override - public boolean isNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { - return ((CraftEntity) bukkitEntity).getHandle() instanceof NMSEntityBase; - } - - @Override - public NMSEntityBase getNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { - - Entity nmsEntity = ((CraftEntity) bukkitEntity).getHandle(); - if (nmsEntity instanceof NMSEntityBase) { - return ((NMSEntityBase) nmsEntity); - } - - return null; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_11_R1; + +import java.lang.reflect.Method; + +import org.bukkit.Bukkit; +import org.bukkit.craftbukkit.v1_11_R1.CraftWorld; +import org.bukkit.craftbukkit.v1_11_R1.entity.CraftEntity; +import org.bukkit.inventory.ItemStack; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; +import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.Validator; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; + +import net.minecraft.server.v1_11_R1.Entity; +import net.minecraft.server.v1_11_R1.EntityTypes; +import net.minecraft.server.v1_11_R1.MathHelper; +import net.minecraft.server.v1_11_R1.RegistryID; +import net.minecraft.server.v1_11_R1.RegistryMaterials; +import net.minecraft.server.v1_11_R1.World; +import net.minecraft.server.v1_11_R1.WorldServer; + +public class NmsManagerImpl implements NMSManager { + + private static final ReflectField>> REGISTRY_ID_FIELD = new ReflectField>>(RegistryMaterials.class, "a"); + private static final ReflectField ID_TO_CLASS_MAP_FIELD = new ReflectField(RegistryID.class, "d"); + + private Method validateEntityMethod; + + @Override + public void setup() throws Exception { + validateEntityMethod = World.class.getDeclaredMethod("b", Entity.class); + validateEntityMethod.setAccessible(true); + + registerCustomEntity(EntityNMSSlime.class, 55); + } + + public void registerCustomEntity(Class entityClass, int id) throws Exception { + // Use reflection to get the RegistryID of entities. + RegistryID> registryID = REGISTRY_ID_FIELD.get(EntityTypes.b); + Object[] idToClassMap = ID_TO_CLASS_MAP_FIELD.get(registryID); + + // Save the the ID -> entity class mapping before the registration. + Object oldValue = idToClassMap[id]; + + // Register the entity class. + registryID.a(entityClass, id); + + // Restore the ID -> entity class mapping. + idToClassMap[id] = oldValue; + } + + @Override + public NMSItem spawnNMSItem(org.bukkit.World bukkitWorld, double x, double y, double z, ItemLine parentPiece, ItemStack stack, ItemPickupManager itemPickupManager) { + WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); + EntityNMSItem customItem = new EntityNMSItem(nmsWorld, parentPiece, itemPickupManager); + customItem.setLocationNMS(x, y, z); + customItem.setItemStackNMS(stack); + if (!addEntityToWorld(nmsWorld, customItem)) { + ConsoleLogger.handleSpawnFail(parentPiece); + } + return customItem; + } + + @Override + public EntityNMSSlime spawnNMSSlime(org.bukkit.World bukkitWorld, double x, double y, double z, HologramLine parentPiece) { + WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); + EntityNMSSlime touchSlime = new EntityNMSSlime(nmsWorld, parentPiece); + touchSlime.setLocationNMS(x, y, z); + if (!addEntityToWorld(nmsWorld, touchSlime)) { + ConsoleLogger.handleSpawnFail(parentPiece); + } + return touchSlime; + } + + @Override + public NMSArmorStand spawnNMSArmorStand(org.bukkit.World world, double x, double y, double z, HologramLine parentPiece) { + WorldServer nmsWorld = ((CraftWorld) world).getHandle(); + EntityNMSArmorStand invisibleArmorStand = new EntityNMSArmorStand(nmsWorld, parentPiece); + invisibleArmorStand.setLocationNMS(x, y, z); + if (!addEntityToWorld(nmsWorld, invisibleArmorStand)) { + ConsoleLogger.handleSpawnFail(parentPiece); + } + return invisibleArmorStand; + } + + private boolean addEntityToWorld(WorldServer nmsWorld, Entity nmsEntity) { + Validator.isTrue(Bukkit.isPrimaryThread(), "Async entity add"); + + final int chunkX = MathHelper.floor(nmsEntity.locX / 16.0); + final int chunkZ = MathHelper.floor(nmsEntity.locZ / 16.0); + + if (!nmsWorld.getChunkProviderServer().isLoaded(chunkX, chunkZ)) { + // This should never happen + nmsEntity.dead = true; + return false; + } + + nmsWorld.getChunkAt(chunkX, chunkZ).a(nmsEntity); + nmsWorld.entityList.add(nmsEntity); + + try { + validateEntityMethod.invoke(nmsWorld, nmsEntity); + } catch (Exception e) { + e.printStackTrace(); + return false; + } + return true; + } + + @Override + public boolean isNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { + return ((CraftEntity) bukkitEntity).getHandle() instanceof NMSEntityBase; + } + + @Override + public NMSEntityBase getNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { + + Entity nmsEntity = ((CraftEntity) bukkitEntity).getHandle(); + if (nmsEntity instanceof NMSEntityBase) { + return ((NMSEntityBase) nmsEntity); + } + + return null; + } + +} diff --git a/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/NullBoundingBox.java b/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/NullBoundingBox.java index 35d41869..0fc22026 100644 --- a/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/NullBoundingBox.java +++ b/NMS/v1_11_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_11_R1/NullBoundingBox.java @@ -1,127 +1,141 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_11_R1; - -import net.minecraft.server.v1_11_R1.AxisAlignedBB; -import net.minecraft.server.v1_11_R1.BlockPosition; -import net.minecraft.server.v1_11_R1.MovingObjectPosition; -import net.minecraft.server.v1_11_R1.Vec3D; - -public class NullBoundingBox extends AxisAlignedBB { - - public NullBoundingBox() { - super(0, 0, 0, 0, 0, 0); - } - - @Override - public double a() { - return 0.0; - } - - @Override - public double a(AxisAlignedBB arg0, double arg1) { - return 0.0; - } - - @Override - public AxisAlignedBB a(AxisAlignedBB arg0) { - return this; - } - - @Override - public AxisAlignedBB a(double arg0, double arg1, double arg2) { - return this; - } - - @Override - public MovingObjectPosition b(Vec3D arg0, Vec3D arg1) { - return null; - } - - @Override - public double b(AxisAlignedBB arg0, double arg1) { - return 0.0; - } - - @Override - public double c(AxisAlignedBB arg0, double arg1) { - return 0.0; - } - - @Override - public AxisAlignedBB grow(double arg0, double arg1, double arg2) { - return this; - } - - @Override - public AxisAlignedBB shrink(double arg0) { - return this; - } - - @Override - public AxisAlignedBB a(BlockPosition arg0) { - return this; - } - - @Override - public boolean a(double arg0, double arg1, double arg2, double arg3, double arg4, double arg5) { - return false; - } - - @Override - public boolean b(Vec3D arg0) { - return false; - } - - @Override - public boolean c(Vec3D arg0) { - return false; - } - - @Override - public boolean d(Vec3D arg0) { - return false; - } - - @Override - public AxisAlignedBB e(double arg0) { - return this; - } - - @Override - public AxisAlignedBB g(double arg0) { - return this; - } - - @Override - public AxisAlignedBB a(Vec3D arg0) { - return this; - } - - @Override - public AxisAlignedBB b(AxisAlignedBB arg0) { - return this; - } - - @Override - public AxisAlignedBB b(double arg0, double arg1, double arg2) { - return this; - } - - @Override - public boolean c(AxisAlignedBB arg0) { - return false; - } - - @Override - public AxisAlignedBB d(double arg0, double arg1, double arg2) { - return this; - } - - @Override - public boolean e(Vec3D arg0) { - return false; - } - - - - -} \ No newline at end of file +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_11_R1; + +import net.minecraft.server.v1_11_R1.AxisAlignedBB; +import net.minecraft.server.v1_11_R1.BlockPosition; +import net.minecraft.server.v1_11_R1.MovingObjectPosition; +import net.minecraft.server.v1_11_R1.Vec3D; + +public class NullBoundingBox extends AxisAlignedBB { + + public NullBoundingBox() { + super(0, 0, 0, 0, 0, 0); + } + + @Override + public double a() { + return 0.0; + } + + @Override + public double a(AxisAlignedBB arg0, double arg1) { + return 0.0; + } + + @Override + public AxisAlignedBB a(AxisAlignedBB arg0) { + return this; + } + + @Override + public AxisAlignedBB a(double arg0, double arg1, double arg2) { + return this; + } + + @Override + public MovingObjectPosition b(Vec3D arg0, Vec3D arg1) { + return null; + } + + @Override + public double b(AxisAlignedBB arg0, double arg1) { + return 0.0; + } + + @Override + public double c(AxisAlignedBB arg0, double arg1) { + return 0.0; + } + + @Override + public AxisAlignedBB grow(double arg0, double arg1, double arg2) { + return this; + } + + @Override + public AxisAlignedBB shrink(double arg0) { + return this; + } + + @Override + public AxisAlignedBB a(BlockPosition arg0) { + return this; + } + + @Override + public boolean a(double arg0, double arg1, double arg2, double arg3, double arg4, double arg5) { + return false; + } + + @Override + public boolean b(Vec3D arg0) { + return false; + } + + @Override + public boolean c(Vec3D arg0) { + return false; + } + + @Override + public boolean d(Vec3D arg0) { + return false; + } + + @Override + public AxisAlignedBB e(double arg0) { + return this; + } + + @Override + public AxisAlignedBB g(double arg0) { + return this; + } + + @Override + public AxisAlignedBB a(Vec3D arg0) { + return this; + } + + @Override + public AxisAlignedBB b(AxisAlignedBB arg0) { + return this; + } + + @Override + public AxisAlignedBB b(double arg0, double arg1, double arg2) { + return this; + } + + @Override + public boolean c(AxisAlignedBB arg0) { + return false; + } + + @Override + public AxisAlignedBB d(double arg0, double arg1, double arg2) { + return this; + } + + @Override + public boolean e(Vec3D arg0) { + return false; + } + + + + +} diff --git a/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/CraftNMSArmorStand.java b/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/CraftNMSArmorStand.java index 058db005..7f9c0c51 100644 --- a/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/CraftNMSArmorStand.java +++ b/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/CraftNMSArmorStand.java @@ -1,79 +1,93 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_12_R1; - -import java.util.Collection; - -import org.bukkit.EntityEffect; -import org.bukkit.Location; -import org.bukkit.craftbukkit.v1_12_R1.CraftServer; -import org.bukkit.craftbukkit.v1_12_R1.entity.CraftArmorStand; -import org.bukkit.entity.Entity; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -import org.bukkit.inventory.ItemStack; -import org.bukkit.potion.PotionEffect; -import org.bukkit.util.EulerAngle; -import org.bukkit.util.Vector; - -public class CraftNMSArmorStand extends CraftArmorStand { - - public CraftNMSArmorStand(CraftServer server, EntityNMSArmorStand entity) { - super(server, entity); - } - - // Disallow all the bukkit methods. - - @Override - public void remove() { - // Cannot be removed, this is the most important to override. - } - - // Methods from Armor stand class - - @Override public void setArms(boolean arms) { } - @Override public void setBasePlate(boolean basePlate) { } - @Override public void setBodyPose(EulerAngle pose) { } - @Override public void setBoots(ItemStack item) { } - @Override public void setChestplate(ItemStack item) { } - @Override public void setHeadPose(EulerAngle pose) { } - @Override public void setHelmet(ItemStack item) { } - @Override public void setItemInHand(ItemStack item) { } - @Override public void setLeftArmPose(EulerAngle pose) { } - @Override public void setLeftLegPose(EulerAngle pose) { } - @Override public void setLeggings(ItemStack item) { } - @Override public void setRightArmPose(EulerAngle pose) { } - @Override public void setRightLegPose(EulerAngle pose) { } - @Override public void setSmall(boolean small) { } - @Override public void setVisible(boolean visible) { } - @Override public void setMarker(boolean marker) { } - - // Methods from LivingEntity class - @Override public boolean addPotionEffect(PotionEffect effect) { return false; } - @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } - @Override public boolean addPotionEffects(Collection effects) { return false; } - @Override public void setRemoveWhenFarAway(boolean remove) { } - @Override public void setAI(boolean ai) { } - @Override public void setCanPickupItems(boolean pickup) { } - @Override public void setCollidable(boolean collidable) { } - @Override public void setGliding(boolean gliding) { } - @Override public boolean setLeashHolder(Entity holder) { return false; } - - // Methods from Entity - @Override public void setVelocity(Vector vel) { } - @Override public boolean teleport(Location loc) { return false; } - @Override public boolean teleport(Entity entity) { return false; } - @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } - @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } - @Override public void setFireTicks(int ticks) { } - @Override public boolean setPassenger(Entity entity) { return false; } - @Override public boolean eject() { return false; } - @Override public boolean leaveVehicle() { return false; } - @Override public void playEffect(EntityEffect effect) { } - @Override public void setCustomName(String name) { } - @Override public void setCustomNameVisible(boolean flag) { } - @Override public void setGlowing(boolean flag) { } - @Override public void setGravity(boolean gravity) { } - @Override public void setInvulnerable(boolean flag) { } - @Override public void setMomentum(Vector value) { } - @Override public void setSilent(boolean flag) { } - @Override public void setTicksLived(int value) { } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_12_R1; + +import java.util.Collection; + +import org.bukkit.EntityEffect; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_12_R1.CraftServer; +import org.bukkit.craftbukkit.v1_12_R1.entity.CraftArmorStand; +import org.bukkit.entity.Entity; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import org.bukkit.inventory.ItemStack; +import org.bukkit.potion.PotionEffect; +import org.bukkit.util.EulerAngle; +import org.bukkit.util.Vector; + +public class CraftNMSArmorStand extends CraftArmorStand { + + public CraftNMSArmorStand(CraftServer server, EntityNMSArmorStand entity) { + super(server, entity); + } + + // Disallow all the bukkit methods. + + @Override + public void remove() { + // Cannot be removed, this is the most important to override. + } + + // Methods from Armor stand class + + @Override public void setArms(boolean arms) { } + @Override public void setBasePlate(boolean basePlate) { } + @Override public void setBodyPose(EulerAngle pose) { } + @Override public void setBoots(ItemStack item) { } + @Override public void setChestplate(ItemStack item) { } + @Override public void setHeadPose(EulerAngle pose) { } + @Override public void setHelmet(ItemStack item) { } + @Override public void setItemInHand(ItemStack item) { } + @Override public void setLeftArmPose(EulerAngle pose) { } + @Override public void setLeftLegPose(EulerAngle pose) { } + @Override public void setLeggings(ItemStack item) { } + @Override public void setRightArmPose(EulerAngle pose) { } + @Override public void setRightLegPose(EulerAngle pose) { } + @Override public void setSmall(boolean small) { } + @Override public void setVisible(boolean visible) { } + @Override public void setMarker(boolean marker) { } + + // Methods from LivingEntity class + @Override public boolean addPotionEffect(PotionEffect effect) { return false; } + @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } + @Override public boolean addPotionEffects(Collection effects) { return false; } + @Override public void setRemoveWhenFarAway(boolean remove) { } + @Override public void setAI(boolean ai) { } + @Override public void setCanPickupItems(boolean pickup) { } + @Override public void setCollidable(boolean collidable) { } + @Override public void setGliding(boolean gliding) { } + @Override public boolean setLeashHolder(Entity holder) { return false; } + + // Methods from Entity + @Override public void setVelocity(Vector vel) { } + @Override public boolean teleport(Location loc) { return false; } + @Override public boolean teleport(Entity entity) { return false; } + @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } + @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } + @Override public void setFireTicks(int ticks) { } + @Override public boolean setPassenger(Entity entity) { return false; } + @Override public boolean eject() { return false; } + @Override public boolean leaveVehicle() { return false; } + @Override public void playEffect(EntityEffect effect) { } + @Override public void setCustomName(String name) { } + @Override public void setCustomNameVisible(boolean flag) { } + @Override public void setGlowing(boolean flag) { } + @Override public void setGravity(boolean gravity) { } + @Override public void setInvulnerable(boolean flag) { } + @Override public void setMomentum(Vector value) { } + @Override public void setSilent(boolean flag) { } + @Override public void setTicksLived(int value) { } + +} diff --git a/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/CraftNMSItem.java b/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/CraftNMSItem.java index ed62d8c2..383109c4 100644 --- a/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/CraftNMSItem.java +++ b/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/CraftNMSItem.java @@ -1,49 +1,63 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_12_R1; - -import org.bukkit.EntityEffect; -import org.bukkit.Location; -import org.bukkit.craftbukkit.v1_12_R1.CraftServer; -import org.bukkit.craftbukkit.v1_12_R1.entity.CraftItem; -import org.bukkit.entity.Entity; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -import org.bukkit.inventory.ItemStack; -import org.bukkit.util.Vector; - -public class CraftNMSItem extends CraftItem { - - public CraftNMSItem(CraftServer server, EntityNMSItem entity) { - super(server, entity); - } - - // Disallow all the bukkit methods. - - @Override - public void remove() { - // Cannot be removed, this is the most important to override. - } - - // Methods from Entity - @Override public void setVelocity(Vector vel) { } - @Override public boolean teleport(Location loc) { return false; } - @Override public boolean teleport(Entity entity) { return false; } - @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } - @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } - @Override public void setFireTicks(int ticks) { } - @Override public boolean setPassenger(Entity entity) { return false; } - @Override public boolean eject() { return false; } - @Override public boolean leaveVehicle() { return false; } - @Override public void playEffect(EntityEffect effect) { } - @Override public void setCustomName(String name) { } - @Override public void setCustomNameVisible(boolean flag) { } - @Override public void setGlowing(boolean flag) { } - @Override public void setGravity(boolean gravity) { } - @Override public void setInvulnerable(boolean flag) { } - @Override public void setMomentum(Vector value) { } - @Override public void setSilent(boolean flag) { } - @Override public void setTicksLived(int value) { } - - // Methods from Item - @Override public void setItemStack(ItemStack stack) { } - @Override public void setPickupDelay(int delay) { } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_12_R1; + +import org.bukkit.EntityEffect; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_12_R1.CraftServer; +import org.bukkit.craftbukkit.v1_12_R1.entity.CraftItem; +import org.bukkit.entity.Entity; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import org.bukkit.inventory.ItemStack; +import org.bukkit.util.Vector; + +public class CraftNMSItem extends CraftItem { + + public CraftNMSItem(CraftServer server, EntityNMSItem entity) { + super(server, entity); + } + + // Disallow all the bukkit methods. + + @Override + public void remove() { + // Cannot be removed, this is the most important to override. + } + + // Methods from Entity + @Override public void setVelocity(Vector vel) { } + @Override public boolean teleport(Location loc) { return false; } + @Override public boolean teleport(Entity entity) { return false; } + @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } + @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } + @Override public void setFireTicks(int ticks) { } + @Override public boolean setPassenger(Entity entity) { return false; } + @Override public boolean eject() { return false; } + @Override public boolean leaveVehicle() { return false; } + @Override public void playEffect(EntityEffect effect) { } + @Override public void setCustomName(String name) { } + @Override public void setCustomNameVisible(boolean flag) { } + @Override public void setGlowing(boolean flag) { } + @Override public void setGravity(boolean gravity) { } + @Override public void setInvulnerable(boolean flag) { } + @Override public void setMomentum(Vector value) { } + @Override public void setSilent(boolean flag) { } + @Override public void setTicksLived(int value) { } + + // Methods from Item + @Override public void setItemStack(ItemStack stack) { } + @Override public void setPickupDelay(int delay) { } + +} diff --git a/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/CraftNMSSlime.java b/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/CraftNMSSlime.java index 783b57f5..cba4cd00 100644 --- a/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/CraftNMSSlime.java +++ b/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/CraftNMSSlime.java @@ -1,61 +1,75 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_12_R1; - -import java.util.Collection; - -import org.bukkit.EntityEffect; -import org.bukkit.Location; -import org.bukkit.craftbukkit.v1_12_R1.CraftServer; -import org.bukkit.craftbukkit.v1_12_R1.entity.CraftSlime; -import org.bukkit.entity.Entity; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -import org.bukkit.potion.PotionEffect; -import org.bukkit.util.Vector; - -public class CraftNMSSlime extends CraftSlime { - - public CraftNMSSlime(CraftServer server, EntityNMSSlime entity) { - super(server, entity); - } - - // Disallow all the bukkit methods. - - @Override - public void remove() { - // Cannot be removed, this is the most important to override. - } - - // Methods from LivingEntity class - @Override public boolean addPotionEffect(PotionEffect effect) { return false; } - @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } - @Override public boolean addPotionEffects(Collection effects) { return false; } - @Override public void setRemoveWhenFarAway(boolean remove) { } - @Override public void setAI(boolean ai) { } - @Override public void setCanPickupItems(boolean pickup) { } - @Override public void setCollidable(boolean collidable) { } - @Override public void setGliding(boolean gliding) { } - @Override public boolean setLeashHolder(Entity holder) { return false; } - - // Methods from Entity - @Override public void setVelocity(Vector vel) { } - @Override public boolean teleport(Location loc) { return false; } - @Override public boolean teleport(Entity entity) { return false; } - @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } - @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } - @Override public void setFireTicks(int ticks) { } - @Override public boolean setPassenger(Entity entity) { return false; } - @Override public boolean eject() { return false; } - @Override public boolean leaveVehicle() { return false; } - @Override public void playEffect(EntityEffect effect) { } - @Override public void setCustomName(String name) { } - @Override public void setCustomNameVisible(boolean flag) { } - @Override public void setGlowing(boolean flag) { } - @Override public void setGravity(boolean gravity) { } - @Override public void setInvulnerable(boolean flag) { } - @Override public void setMomentum(Vector value) { } - @Override public void setSilent(boolean flag) { } - @Override public void setTicksLived(int value) { } - - // Methods from Slime - @Override public void setSize(int size) { } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_12_R1; + +import java.util.Collection; + +import org.bukkit.EntityEffect; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_12_R1.CraftServer; +import org.bukkit.craftbukkit.v1_12_R1.entity.CraftSlime; +import org.bukkit.entity.Entity; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import org.bukkit.potion.PotionEffect; +import org.bukkit.util.Vector; + +public class CraftNMSSlime extends CraftSlime { + + public CraftNMSSlime(CraftServer server, EntityNMSSlime entity) { + super(server, entity); + } + + // Disallow all the bukkit methods. + + @Override + public void remove() { + // Cannot be removed, this is the most important to override. + } + + // Methods from LivingEntity class + @Override public boolean addPotionEffect(PotionEffect effect) { return false; } + @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } + @Override public boolean addPotionEffects(Collection effects) { return false; } + @Override public void setRemoveWhenFarAway(boolean remove) { } + @Override public void setAI(boolean ai) { } + @Override public void setCanPickupItems(boolean pickup) { } + @Override public void setCollidable(boolean collidable) { } + @Override public void setGliding(boolean gliding) { } + @Override public boolean setLeashHolder(Entity holder) { return false; } + + // Methods from Entity + @Override public void setVelocity(Vector vel) { } + @Override public boolean teleport(Location loc) { return false; } + @Override public boolean teleport(Entity entity) { return false; } + @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } + @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } + @Override public void setFireTicks(int ticks) { } + @Override public boolean setPassenger(Entity entity) { return false; } + @Override public boolean eject() { return false; } + @Override public boolean leaveVehicle() { return false; } + @Override public void playEffect(EntityEffect effect) { } + @Override public void setCustomName(String name) { } + @Override public void setCustomNameVisible(boolean flag) { } + @Override public void setGlowing(boolean flag) { } + @Override public void setGravity(boolean gravity) { } + @Override public void setInvulnerable(boolean flag) { } + @Override public void setMomentum(Vector value) { } + @Override public void setSilent(boolean flag) { } + @Override public void setTicksLived(int value) { } + + // Methods from Slime + @Override public void setSize(int size) { } + +} diff --git a/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/EntityNMSArmorStand.java b/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/EntityNMSArmorStand.java index d9cf3785..e60123d0 100644 --- a/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/EntityNMSArmorStand.java +++ b/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/EntityNMSArmorStand.java @@ -1,240 +1,254 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_12_R1; - -import org.bukkit.craftbukkit.v1_12_R1.entity.CraftEntity; - -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; -import com.gmail.filoghost.holographicdisplays.disk.Configuration; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; -import com.gmail.filoghost.holographicdisplays.util.Utils; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectionUtils; - -import net.minecraft.server.v1_12_R1.AxisAlignedBB; -import net.minecraft.server.v1_12_R1.DamageSource; -import net.minecraft.server.v1_12_R1.EntityArmorStand; -import net.minecraft.server.v1_12_R1.EntityHuman; -import net.minecraft.server.v1_12_R1.EntityPlayer; -import net.minecraft.server.v1_12_R1.EnumHand; -import net.minecraft.server.v1_12_R1.EnumInteractionResult; -import net.minecraft.server.v1_12_R1.EnumItemSlot; -import net.minecraft.server.v1_12_R1.ItemStack; -import net.minecraft.server.v1_12_R1.NBTTagCompound; -import net.minecraft.server.v1_12_R1.PacketPlayOutEntityTeleport; -import net.minecraft.server.v1_12_R1.SoundEffect; -import net.minecraft.server.v1_12_R1.Vec3D; -import net.minecraft.server.v1_12_R1.World; - -public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorStand { - - private boolean lockTick; - private HologramLine parentPiece; - - public EntityNMSArmorStand(World world, HologramLine parentPiece) { - super(world); - super.setInvisible(true); - super.setSmall(true); - super.setArms(false); - super.setNoGravity(true); - super.setBasePlate(true); - super.setMarker(true); - super.collides = false; - this.parentPiece = parentPiece; - forceSetBoundingBox(new NullBoundingBox()); - } - - - @Override - public void b(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public boolean c(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public boolean d(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public NBTTagCompound save(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return nbttagcompound; - } - - @Override - public void f(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public void a(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public boolean isInvulnerable(DamageSource source) { - /* - * The field Entity.invulnerable is private. - * It's only used while saving NBTTags, but since the entity would be killed - * on chunk unload, we prefer to override isInvulnerable(). - */ - return true; - } - - @Override - public boolean isCollidable() { - return false; - } - - @Override - public void setCustomName(String customName) { - // Locks the custom name. - } - - @Override - public void inactiveTick() { - // Check inactive ticks. - - if (!lockTick) { - super.inactiveTick(); - } - } - - @Override - public void setCustomNameVisible(boolean visible) { - // Locks the custom name. - } - - @Override - public EnumInteractionResult a(EntityHuman human, Vec3D vec3d, EnumHand enumhand) { - // Prevent stand being equipped - return EnumInteractionResult.PASS; - } - - @Override - public boolean c(int i, ItemStack item) { - // Prevent stand being equipped - return false; - } - - @Override - public void setSlot(EnumItemSlot enumitemslot, ItemStack itemstack) { - // Prevent stand being equipped - } - - @Override - public void a(AxisAlignedBB boundingBox) { - // Do not change it! - } - - public void forceSetBoundingBox(AxisAlignedBB boundingBox) { - super.a(boundingBox); - } - - @Override - public int getId() { - if (Configuration.preciseHologramMovement) { - StackTraceElement element = ReflectionUtils.getStackTraceElement(2); - if (element != null && element.getFileName() != null && element.getFileName().equals("EntityTrackerEntry.java") && 158 < element.getLineNumber() && element.getLineNumber() < 168) { - // Then this method is being called when creating a new movement packet, we return a fake ID! - return -1; - } - } - - return super.getId(); - } - - @Override - public void B_() { - if (!lockTick) { - super.B_(); - } - } - - @Override - public void a(SoundEffect soundeffect, float f, float f1) { - // Remove sounds. - } - - @Override - public void setCustomNameNMS(String name) { - if (name != null && name.length() > 300) { - name = name.substring(0, 300); - } - super.setCustomName(name); - super.setCustomNameVisible(name != null && !name.isEmpty()); - } - - @Override - public String getCustomNameNMS() { - return super.getCustomName(); - } - - @Override - public void setLockTick(boolean lock) { - lockTick = lock; - } - - @Override - public void die() { - // Prevent being killed. - } - - @Override - public CraftEntity getBukkitEntity() { - if (super.bukkitEntity == null) { - super.bukkitEntity = new CraftNMSArmorStand(super.world.getServer(), this); - } - return super.bukkitEntity; - } - - @Override - public void killEntityNMS() { - super.dead = true; - } - - @Override - public void setLocationNMS(double x, double y, double z) { - super.setPosition(x, y, z); - - if (Configuration.preciseHologramMovement) { - // Send a packet near to update the position. - PacketPlayOutEntityTeleport teleportPacket = new PacketPlayOutEntityTeleport(this); - - for (Object obj : super.world.players) { - if (obj instanceof EntityPlayer) { - EntityPlayer nmsPlayer = (EntityPlayer) obj; - - double distanceSquared = Utils.square(nmsPlayer.locX - super.locX) + Utils.square(nmsPlayer.locZ - super.locZ); - if (distanceSquared < 8192 && nmsPlayer.playerConnection != null) { - nmsPlayer.playerConnection.sendPacket(teleportPacket); - } - } - } - } - } - - @Override - public boolean isDeadNMS() { - return super.dead; - } - - @Override - public int getIdNMS() { - return super.getId(); // Return the real ID without checking the stack trace. - } - - @Override - public HologramLine getHologramLine() { - return parentPiece; - } - - @Override - public org.bukkit.entity.Entity getBukkitEntityNMS() { - return getBukkitEntity(); - } -} \ No newline at end of file +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_12_R1; + +import org.bukkit.craftbukkit.v1_12_R1.entity.CraftEntity; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; +import com.gmail.filoghost.holographicdisplays.disk.Configuration; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; +import com.gmail.filoghost.holographicdisplays.util.Utils; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectionUtils; + +import net.minecraft.server.v1_12_R1.AxisAlignedBB; +import net.minecraft.server.v1_12_R1.DamageSource; +import net.minecraft.server.v1_12_R1.EntityArmorStand; +import net.minecraft.server.v1_12_R1.EntityHuman; +import net.minecraft.server.v1_12_R1.EntityPlayer; +import net.minecraft.server.v1_12_R1.EnumHand; +import net.minecraft.server.v1_12_R1.EnumInteractionResult; +import net.minecraft.server.v1_12_R1.EnumItemSlot; +import net.minecraft.server.v1_12_R1.ItemStack; +import net.minecraft.server.v1_12_R1.NBTTagCompound; +import net.minecraft.server.v1_12_R1.PacketPlayOutEntityTeleport; +import net.minecraft.server.v1_12_R1.SoundEffect; +import net.minecraft.server.v1_12_R1.Vec3D; +import net.minecraft.server.v1_12_R1.World; + +public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorStand { + + private boolean lockTick; + private HologramLine parentPiece; + + public EntityNMSArmorStand(World world, HologramLine parentPiece) { + super(world); + super.setInvisible(true); + super.setSmall(true); + super.setArms(false); + super.setNoGravity(true); + super.setBasePlate(true); + super.setMarker(true); + super.collides = false; + this.parentPiece = parentPiece; + forceSetBoundingBox(new NullBoundingBox()); + } + + + @Override + public void b(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public boolean c(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public boolean d(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public NBTTagCompound save(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return nbttagcompound; + } + + @Override + public void f(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public void a(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public boolean isInvulnerable(DamageSource source) { + /* + * The field Entity.invulnerable is private. + * It's only used while saving NBTTags, but since the entity would be killed + * on chunk unload, we prefer to override isInvulnerable(). + */ + return true; + } + + @Override + public boolean isCollidable() { + return false; + } + + @Override + public void setCustomName(String customName) { + // Locks the custom name. + } + + @Override + public void inactiveTick() { + // Check inactive ticks. + + if (!lockTick) { + super.inactiveTick(); + } + } + + @Override + public void setCustomNameVisible(boolean visible) { + // Locks the custom name. + } + + @Override + public EnumInteractionResult a(EntityHuman human, Vec3D vec3d, EnumHand enumhand) { + // Prevent stand being equipped + return EnumInteractionResult.PASS; + } + + @Override + public boolean c(int i, ItemStack item) { + // Prevent stand being equipped + return false; + } + + @Override + public void setSlot(EnumItemSlot enumitemslot, ItemStack itemstack) { + // Prevent stand being equipped + } + + @Override + public void a(AxisAlignedBB boundingBox) { + // Do not change it! + } + + public void forceSetBoundingBox(AxisAlignedBB boundingBox) { + super.a(boundingBox); + } + + @Override + public int getId() { + if (Configuration.preciseHologramMovement) { + StackTraceElement element = ReflectionUtils.getStackTraceElement(2); + if (element != null && element.getFileName() != null && element.getFileName().equals("EntityTrackerEntry.java") && 158 < element.getLineNumber() && element.getLineNumber() < 168) { + // Then this method is being called when creating a new movement packet, we return a fake ID! + return -1; + } + } + + return super.getId(); + } + + @Override + public void B_() { + if (!lockTick) { + super.B_(); + } + } + + @Override + public void a(SoundEffect soundeffect, float f, float f1) { + // Remove sounds. + } + + @Override + public void setCustomNameNMS(String name) { + if (name != null && name.length() > 300) { + name = name.substring(0, 300); + } + super.setCustomName(name); + super.setCustomNameVisible(name != null && !name.isEmpty()); + } + + @Override + public String getCustomNameNMS() { + return super.getCustomName(); + } + + @Override + public void setLockTick(boolean lock) { + lockTick = lock; + } + + @Override + public void die() { + // Prevent being killed. + } + + @Override + public CraftEntity getBukkitEntity() { + if (super.bukkitEntity == null) { + super.bukkitEntity = new CraftNMSArmorStand(super.world.getServer(), this); + } + return super.bukkitEntity; + } + + @Override + public void killEntityNMS() { + super.dead = true; + } + + @Override + public void setLocationNMS(double x, double y, double z) { + super.setPosition(x, y, z); + + if (Configuration.preciseHologramMovement) { + // Send a packet near to update the position. + PacketPlayOutEntityTeleport teleportPacket = new PacketPlayOutEntityTeleport(this); + + for (Object obj : super.world.players) { + if (obj instanceof EntityPlayer) { + EntityPlayer nmsPlayer = (EntityPlayer) obj; + + double distanceSquared = Utils.square(nmsPlayer.locX - super.locX) + Utils.square(nmsPlayer.locZ - super.locZ); + if (distanceSquared < 8192 && nmsPlayer.playerConnection != null) { + nmsPlayer.playerConnection.sendPacket(teleportPacket); + } + } + } + } + } + + @Override + public boolean isDeadNMS() { + return super.dead; + } + + @Override + public int getIdNMS() { + return super.getId(); // Return the real ID without checking the stack trace. + } + + @Override + public HologramLine getHologramLine() { + return parentPiece; + } + + @Override + public org.bukkit.entity.Entity getBukkitEntityNMS() { + return getBukkitEntity(); + } +} diff --git a/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/EntityNMSItem.java b/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/EntityNMSItem.java index cd3df350..cf1ddc4f 100644 --- a/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/EntityNMSItem.java +++ b/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/EntityNMSItem.java @@ -1,247 +1,261 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_12_R1; - -import org.bukkit.craftbukkit.v1_12_R1.entity.CraftEntity; -import org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack; -import org.bukkit.entity.Player; - -import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.ItemUtils; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; - -import net.minecraft.server.v1_12_R1.Blocks; -import net.minecraft.server.v1_12_R1.DamageSource; -import net.minecraft.server.v1_12_R1.Entity; -import net.minecraft.server.v1_12_R1.EntityHuman; -import net.minecraft.server.v1_12_R1.EntityItem; -import net.minecraft.server.v1_12_R1.EntityPlayer; -import net.minecraft.server.v1_12_R1.ItemStack; -import net.minecraft.server.v1_12_R1.NBTTagCompound; -import net.minecraft.server.v1_12_R1.NBTTagList; -import net.minecraft.server.v1_12_R1.NBTTagString; -import net.minecraft.server.v1_12_R1.World; -import net.minecraft.server.v1_12_R1.AxisAlignedBB; - -public class EntityNMSItem extends EntityItem implements NMSItem { - - private static final ReflectField VEHICLE_FIELD = new ReflectField(Entity.class, "au"); - - private boolean lockTick; - private ItemLine parentPiece; - private ItemPickupManager itemPickupManager; - - public EntityNMSItem(World world, ItemLine piece, ItemPickupManager itemPickupManager) { - super(world); - super.pickupDelay = 32767; // Lock the item pickup delay, also prevents entities from picking up the item - this.parentPiece = piece; - this.itemPickupManager = itemPickupManager; - forceSetBoundingBox(new NullBoundingBox()); - } - - @Override - public void B_() { - - // So it won't get removed. - ticksLived = 0; - - if (!lockTick) { - super.B_(); - } - } - - // Method called when a player is near. - @Override - public void d(EntityHuman human) { - - if (human.locY < super.locY - 1.5 || human.locY > super.locY + 1.0) { - // Too low or too high, it's a bit weird. - return; - } - - if (parentPiece.getPickupHandler() != null && human instanceof EntityPlayer) { - itemPickupManager.handleItemLinePickup((Player) human.getBukkitEntity(), parentPiece.getPickupHandler(), parentPiece.getParent()); - // It is never added to the inventory. - } - } - - @Override - public void b(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public boolean c(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public boolean d(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public NBTTagCompound save(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return nbttagcompound; - } - - @Override - public void f(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public void a(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public boolean isInvulnerable(DamageSource source) { - /* - * The field Entity.invulnerable is private. - * It's only used while saving NBTTags, but since the entity would be killed - * on chunk unload, we prefer to override isInvulnerable(). - */ - return true; - } - - @Override - public boolean isCollidable() { - return false; - } - - @Override - public void a(AxisAlignedBB boundingBox) { - // Do not change it! - } - - public void forceSetBoundingBox(AxisAlignedBB boundingBox) { - super.a(boundingBox); - } - - @Override - public void inactiveTick() { - // Check inactive ticks. - - if (!lockTick) { - super.inactiveTick(); - } - } - - @Override - public void setLockTick(boolean lock) { - lockTick = lock; - } - - @Override - public void die() { - // Prevent being killed. - } - - @Override - public CraftEntity getBukkitEntity() { - if (super.bukkitEntity == null) { - super.bukkitEntity = new CraftNMSItem(super.world.getServer(), this); - } - return super.bukkitEntity; - } - - @Override - public boolean isDeadNMS() { - return super.dead; - } - - @Override - public void killEntityNMS() { - super.dead = true; - } - - @Override - public void setLocationNMS(double x, double y, double z) { - super.setPosition(x, y, z); - } - - @Override - public void setItemStackNMS(org.bukkit.inventory.ItemStack stack) { - ItemStack newItem = CraftItemStack.asNMSCopy(stack); // ItemStack.a is returned if the stack is null, invalid or the material is not an Item - - if (newItem == null || newItem == ItemStack.a) { - newItem = new ItemStack(Blocks.BEDROCK); - } - - if (newItem.getTag() == null) { - newItem.setTag(new NBTTagCompound()); - } - NBTTagCompound display = newItem.getTag().getCompound("display"); // Returns a new NBTTagCompound if not existing - if (!newItem.getTag().hasKey("display")) { - newItem.getTag().set("display", display); - } - - NBTTagList tagList = new NBTTagList(); - tagList.add(new NBTTagString(ItemUtils.ANTISTACK_LORE)); // Antistack lore - display.set("Lore", tagList); - - newItem.setCount(1); - - setItemStack(newItem); - } - - @Override - public int getIdNMS() { - return super.getId(); - } - - @Override - public ItemLine getHologramLine() { - return parentPiece; - } - - @Override - public void allowPickup(boolean pickup) { - if (pickup) { - super.pickupDelay = 0; - } else { - super.pickupDelay = 32767; - } - } - - @Override - public org.bukkit.entity.Entity getBukkitEntityNMS() { - return getBukkitEntity(); - } - - @Override - public void setPassengerOfNMS(NMSEntityBase vehicleBase) { - if (vehicleBase == null || !(vehicleBase instanceof Entity)) { - // It should never dismount - return; - } - - Entity entity = (Entity) vehicleBase; - - try { - if (super.bJ() != null) { - Entity oldVehicle = super.bJ(); - VEHICLE_FIELD.set(this, null); - oldVehicle.passengers.remove(this); - } - - VEHICLE_FIELD.set(this, entity); - entity.passengers.clear(); - entity.passengers.add(this); - - } catch (Exception ex) { - ConsoleLogger.logDebugException(ex); - } - } - - @Override - public Object getRawItemStack() { - return super.getItemStack(); - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_12_R1; + +import org.bukkit.craftbukkit.v1_12_R1.entity.CraftEntity; +import org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack; +import org.bukkit.entity.Player; + +import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.ItemUtils; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; + +import net.minecraft.server.v1_12_R1.Blocks; +import net.minecraft.server.v1_12_R1.DamageSource; +import net.minecraft.server.v1_12_R1.Entity; +import net.minecraft.server.v1_12_R1.EntityHuman; +import net.minecraft.server.v1_12_R1.EntityItem; +import net.minecraft.server.v1_12_R1.EntityPlayer; +import net.minecraft.server.v1_12_R1.ItemStack; +import net.minecraft.server.v1_12_R1.NBTTagCompound; +import net.minecraft.server.v1_12_R1.NBTTagList; +import net.minecraft.server.v1_12_R1.NBTTagString; +import net.minecraft.server.v1_12_R1.World; +import net.minecraft.server.v1_12_R1.AxisAlignedBB; + +public class EntityNMSItem extends EntityItem implements NMSItem { + + private static final ReflectField VEHICLE_FIELD = new ReflectField(Entity.class, "au"); + + private boolean lockTick; + private ItemLine parentPiece; + private ItemPickupManager itemPickupManager; + + public EntityNMSItem(World world, ItemLine piece, ItemPickupManager itemPickupManager) { + super(world); + super.pickupDelay = 32767; // Lock the item pickup delay, also prevents entities from picking up the item + this.parentPiece = piece; + this.itemPickupManager = itemPickupManager; + forceSetBoundingBox(new NullBoundingBox()); + } + + @Override + public void B_() { + + // So it won't get removed. + ticksLived = 0; + + if (!lockTick) { + super.B_(); + } + } + + // Method called when a player is near. + @Override + public void d(EntityHuman human) { + + if (human.locY < super.locY - 1.5 || human.locY > super.locY + 1.0) { + // Too low or too high, it's a bit weird. + return; + } + + if (parentPiece.getPickupHandler() != null && human instanceof EntityPlayer) { + itemPickupManager.handleItemLinePickup((Player) human.getBukkitEntity(), parentPiece.getPickupHandler(), parentPiece.getParent()); + // It is never added to the inventory. + } + } + + @Override + public void b(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public boolean c(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public boolean d(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public NBTTagCompound save(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return nbttagcompound; + } + + @Override + public void f(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public void a(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public boolean isInvulnerable(DamageSource source) { + /* + * The field Entity.invulnerable is private. + * It's only used while saving NBTTags, but since the entity would be killed + * on chunk unload, we prefer to override isInvulnerable(). + */ + return true; + } + + @Override + public boolean isCollidable() { + return false; + } + + @Override + public void a(AxisAlignedBB boundingBox) { + // Do not change it! + } + + public void forceSetBoundingBox(AxisAlignedBB boundingBox) { + super.a(boundingBox); + } + + @Override + public void inactiveTick() { + // Check inactive ticks. + + if (!lockTick) { + super.inactiveTick(); + } + } + + @Override + public void setLockTick(boolean lock) { + lockTick = lock; + } + + @Override + public void die() { + // Prevent being killed. + } + + @Override + public CraftEntity getBukkitEntity() { + if (super.bukkitEntity == null) { + super.bukkitEntity = new CraftNMSItem(super.world.getServer(), this); + } + return super.bukkitEntity; + } + + @Override + public boolean isDeadNMS() { + return super.dead; + } + + @Override + public void killEntityNMS() { + super.dead = true; + } + + @Override + public void setLocationNMS(double x, double y, double z) { + super.setPosition(x, y, z); + } + + @Override + public void setItemStackNMS(org.bukkit.inventory.ItemStack stack) { + ItemStack newItem = CraftItemStack.asNMSCopy(stack); // ItemStack.a is returned if the stack is null, invalid or the material is not an Item + + if (newItem == null || newItem == ItemStack.a) { + newItem = new ItemStack(Blocks.BEDROCK); + } + + if (newItem.getTag() == null) { + newItem.setTag(new NBTTagCompound()); + } + NBTTagCompound display = newItem.getTag().getCompound("display"); // Returns a new NBTTagCompound if not existing + if (!newItem.getTag().hasKey("display")) { + newItem.getTag().set("display", display); + } + + NBTTagList tagList = new NBTTagList(); + tagList.add(new NBTTagString(ItemUtils.ANTISTACK_LORE)); // Antistack lore + display.set("Lore", tagList); + + newItem.setCount(1); + + setItemStack(newItem); + } + + @Override + public int getIdNMS() { + return super.getId(); + } + + @Override + public ItemLine getHologramLine() { + return parentPiece; + } + + @Override + public void allowPickup(boolean pickup) { + if (pickup) { + super.pickupDelay = 0; + } else { + super.pickupDelay = 32767; + } + } + + @Override + public org.bukkit.entity.Entity getBukkitEntityNMS() { + return getBukkitEntity(); + } + + @Override + public void setPassengerOfNMS(NMSEntityBase vehicleBase) { + if (vehicleBase == null || !(vehicleBase instanceof Entity)) { + // It should never dismount + return; + } + + Entity entity = (Entity) vehicleBase; + + try { + if (super.bJ() != null) { + Entity oldVehicle = super.bJ(); + VEHICLE_FIELD.set(this, null); + oldVehicle.passengers.remove(this); + } + + VEHICLE_FIELD.set(this, entity); + entity.passengers.clear(); + entity.passengers.add(this); + + } catch (Exception ex) { + ConsoleLogger.logDebugException(ex); + } + } + + @Override + public Object getRawItemStack() { + return super.getItemStack(); + } +} diff --git a/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/EntityNMSSlime.java b/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/EntityNMSSlime.java index 335faab3..338b07eb 100644 --- a/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/EntityNMSSlime.java +++ b/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/EntityNMSSlime.java @@ -1,216 +1,230 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_12_R1; - -import org.bukkit.Bukkit; -import org.bukkit.craftbukkit.v1_12_R1.entity.CraftEntity; -import org.bukkit.event.player.PlayerInteractEntityEvent; - -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSSlime; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; - -import net.minecraft.server.v1_12_R1.AxisAlignedBB; -import net.minecraft.server.v1_12_R1.DamageSource; -import net.minecraft.server.v1_12_R1.Entity; -import net.minecraft.server.v1_12_R1.EntityDamageSource; -import net.minecraft.server.v1_12_R1.EntityPlayer; -import net.minecraft.server.v1_12_R1.EntitySlime; -import net.minecraft.server.v1_12_R1.NBTTagCompound; -import net.minecraft.server.v1_12_R1.SoundEffect; -import net.minecraft.server.v1_12_R1.World; - -public class EntityNMSSlime extends EntitySlime implements NMSSlime { - - private static final ReflectField VEHICLE_FIELD = new ReflectField(Entity.class, "au"); - - private boolean lockTick; - private HologramLine parentPiece; - - public EntityNMSSlime(World world, HologramLine parentPiece) { - super(world); - super.persistent = true; - super.collides = false; - a(0.0F, 0.0F); - setSize(1, false); - setInvisible(true); - this.parentPiece = parentPiece; - forceSetBoundingBox(new NullBoundingBox()); - } - - @Override - public void a(AxisAlignedBB boundingBox) { - // Do not change it! - } - - public void forceSetBoundingBox(AxisAlignedBB boundingBox) { - super.a(boundingBox); - } - - @Override - public void B_() { - - // So it won't get removed. - ticksLived = 0; - - if (!lockTick) { - super.B_(); - } - } - - @Override - public void b(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public boolean c(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public boolean d(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public NBTTagCompound save(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return nbttagcompound; - } - - @Override - public void f(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public void a(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public boolean damageEntity(DamageSource damageSource, float amount) { - if (damageSource instanceof EntityDamageSource) { - EntityDamageSource entityDamageSource = (EntityDamageSource) damageSource; - if (entityDamageSource.getEntity() instanceof EntityPlayer) { - Bukkit.getPluginManager().callEvent(new PlayerInteractEntityEvent(((EntityPlayer) entityDamageSource.getEntity()).getBukkitEntity(), getBukkitEntity())); // Bukkit takes care of the exceptions - } - } - return false; - } - - @Override - public boolean isInvulnerable(DamageSource source) { - /* - * The field Entity.invulnerable is private. - * It's only used while saving NBTTags, but since the entity would be killed - * on chunk unload, we prefer to override isInvulnerable(). - */ - return true; - } - - @Override - public boolean isCollidable() { - return false; - } - - @Override - public void inactiveTick() { - // Check inactive ticks. - - if (!lockTick) { - super.inactiveTick(); - } - } - - @Override - public void setCustomName(String customName) { - // Locks the custom name. - } - - @Override - public void setCustomNameVisible(boolean visible) { - // Locks the custom name. - } - - @Override - public void a(SoundEffect soundeffect, float f, float f1) { - // Remove sounds. - } - - @Override - public void setLockTick(boolean lock) { - lockTick = lock; - } - - @Override - public void die() { - // Prevent being killed. - } - - @Override - public CraftEntity getBukkitEntity() { - if (super.bukkitEntity == null) { - super.bukkitEntity = new CraftNMSSlime(super.world.getServer(), this); - } - return super.bukkitEntity; - } - - @Override - public boolean isDeadNMS() { - return super.dead; - } - - @Override - public void killEntityNMS() { - super.dead = true; - } - - @Override - public void setLocationNMS(double x, double y, double z) { - super.setPosition(x, y, z); - } - - @Override - public int getIdNMS() { - return super.getId(); - } - - @Override - public HologramLine getHologramLine() { - return parentPiece; - } - - @Override - public org.bukkit.entity.Entity getBukkitEntityNMS() { - return getBukkitEntity(); - } - - @Override - public void setPassengerOfNMS(NMSEntityBase vehicleBase) { - if (vehicleBase == null || !(vehicleBase instanceof Entity)) { - // It should never dismount - return; - } - - Entity entity = (Entity) vehicleBase; - - try { - if (super.bJ() != null) { - Entity oldVehicle = super.bJ(); - VEHICLE_FIELD.set(this, null); - oldVehicle.passengers.remove(this); - } - - VEHICLE_FIELD.set(this, entity); - entity.passengers.clear(); - entity.passengers.add(this); - - } catch (Exception ex) { - ConsoleLogger.logDebugException(ex); - } - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_12_R1; + +import org.bukkit.Bukkit; +import org.bukkit.craftbukkit.v1_12_R1.entity.CraftEntity; +import org.bukkit.event.player.PlayerInteractEntityEvent; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSSlime; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; + +import net.minecraft.server.v1_12_R1.AxisAlignedBB; +import net.minecraft.server.v1_12_R1.DamageSource; +import net.minecraft.server.v1_12_R1.Entity; +import net.minecraft.server.v1_12_R1.EntityDamageSource; +import net.minecraft.server.v1_12_R1.EntityPlayer; +import net.minecraft.server.v1_12_R1.EntitySlime; +import net.minecraft.server.v1_12_R1.NBTTagCompound; +import net.minecraft.server.v1_12_R1.SoundEffect; +import net.minecraft.server.v1_12_R1.World; + +public class EntityNMSSlime extends EntitySlime implements NMSSlime { + + private static final ReflectField VEHICLE_FIELD = new ReflectField(Entity.class, "au"); + + private boolean lockTick; + private HologramLine parentPiece; + + public EntityNMSSlime(World world, HologramLine parentPiece) { + super(world); + super.persistent = true; + super.collides = false; + a(0.0F, 0.0F); + setSize(1, false); + setInvisible(true); + this.parentPiece = parentPiece; + forceSetBoundingBox(new NullBoundingBox()); + } + + @Override + public void a(AxisAlignedBB boundingBox) { + // Do not change it! + } + + public void forceSetBoundingBox(AxisAlignedBB boundingBox) { + super.a(boundingBox); + } + + @Override + public void B_() { + + // So it won't get removed. + ticksLived = 0; + + if (!lockTick) { + super.B_(); + } + } + + @Override + public void b(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public boolean c(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public boolean d(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public NBTTagCompound save(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return nbttagcompound; + } + + @Override + public void f(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public void a(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public boolean damageEntity(DamageSource damageSource, float amount) { + if (damageSource instanceof EntityDamageSource) { + EntityDamageSource entityDamageSource = (EntityDamageSource) damageSource; + if (entityDamageSource.getEntity() instanceof EntityPlayer) { + Bukkit.getPluginManager().callEvent(new PlayerInteractEntityEvent(((EntityPlayer) entityDamageSource.getEntity()).getBukkitEntity(), getBukkitEntity())); // Bukkit takes care of the exceptions + } + } + return false; + } + + @Override + public boolean isInvulnerable(DamageSource source) { + /* + * The field Entity.invulnerable is private. + * It's only used while saving NBTTags, but since the entity would be killed + * on chunk unload, we prefer to override isInvulnerable(). + */ + return true; + } + + @Override + public boolean isCollidable() { + return false; + } + + @Override + public void inactiveTick() { + // Check inactive ticks. + + if (!lockTick) { + super.inactiveTick(); + } + } + + @Override + public void setCustomName(String customName) { + // Locks the custom name. + } + + @Override + public void setCustomNameVisible(boolean visible) { + // Locks the custom name. + } + + @Override + public void a(SoundEffect soundeffect, float f, float f1) { + // Remove sounds. + } + + @Override + public void setLockTick(boolean lock) { + lockTick = lock; + } + + @Override + public void die() { + // Prevent being killed. + } + + @Override + public CraftEntity getBukkitEntity() { + if (super.bukkitEntity == null) { + super.bukkitEntity = new CraftNMSSlime(super.world.getServer(), this); + } + return super.bukkitEntity; + } + + @Override + public boolean isDeadNMS() { + return super.dead; + } + + @Override + public void killEntityNMS() { + super.dead = true; + } + + @Override + public void setLocationNMS(double x, double y, double z) { + super.setPosition(x, y, z); + } + + @Override + public int getIdNMS() { + return super.getId(); + } + + @Override + public HologramLine getHologramLine() { + return parentPiece; + } + + @Override + public org.bukkit.entity.Entity getBukkitEntityNMS() { + return getBukkitEntity(); + } + + @Override + public void setPassengerOfNMS(NMSEntityBase vehicleBase) { + if (vehicleBase == null || !(vehicleBase instanceof Entity)) { + // It should never dismount + return; + } + + Entity entity = (Entity) vehicleBase; + + try { + if (super.bJ() != null) { + Entity oldVehicle = super.bJ(); + VEHICLE_FIELD.set(this, null); + oldVehicle.passengers.remove(this); + } + + VEHICLE_FIELD.set(this, entity); + entity.passengers.clear(); + entity.passengers.add(this); + + } catch (Exception ex) { + ConsoleLogger.logDebugException(ex); + } + } +} diff --git a/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/NmsManagerImpl.java b/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/NmsManagerImpl.java index 3f1dff3e..5eeaa72f 100644 --- a/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/NmsManagerImpl.java +++ b/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/NmsManagerImpl.java @@ -1,133 +1,147 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_12_R1; - -import java.lang.reflect.Method; - -import org.bukkit.Bukkit; -import org.bukkit.craftbukkit.v1_12_R1.CraftWorld; -import org.bukkit.craftbukkit.v1_12_R1.entity.CraftEntity; -import org.bukkit.inventory.ItemStack; - -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; -import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.Validator; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; - -import net.minecraft.server.v1_12_R1.Entity; -import net.minecraft.server.v1_12_R1.EntityTypes; -import net.minecraft.server.v1_12_R1.MathHelper; -import net.minecraft.server.v1_12_R1.RegistryID; -import net.minecraft.server.v1_12_R1.RegistryMaterials; -import net.minecraft.server.v1_12_R1.World; -import net.minecraft.server.v1_12_R1.WorldServer; - -public class NmsManagerImpl implements NMSManager { - - private static final ReflectField>> REGISTRY_ID_FIELD = new ReflectField>>(RegistryMaterials.class, "a"); - private static final ReflectField ID_TO_CLASS_MAP_FIELD = new ReflectField(RegistryID.class, "d"); - - private Method validateEntityMethod; - - @Override - public void setup() throws Exception { - validateEntityMethod = World.class.getDeclaredMethod("b", Entity.class); - validateEntityMethod.setAccessible(true); - - registerCustomEntity(EntityNMSSlime.class, 55); - } - - public void registerCustomEntity(Class entityClass, int id) throws Exception { - // Use reflection to get the RegistryID of entities. - RegistryID> registryID = REGISTRY_ID_FIELD.get(EntityTypes.b); - Object[] idToClassMap = ID_TO_CLASS_MAP_FIELD.get(registryID); - - // Save the the ID -> entity class mapping before the registration. - Object oldValue = idToClassMap[id]; - - // Register the entity class. - registryID.a(entityClass, id); - - // Restore the ID -> entity class mapping. - idToClassMap[id] = oldValue; - } - - @Override - public NMSItem spawnNMSItem(org.bukkit.World bukkitWorld, double x, double y, double z, ItemLine parentPiece, ItemStack stack, ItemPickupManager itemPickupManager) { - WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); - EntityNMSItem customItem = new EntityNMSItem(nmsWorld, parentPiece, itemPickupManager); - customItem.setLocationNMS(x, y, z); - customItem.setItemStackNMS(stack); - if (!addEntityToWorld(nmsWorld, customItem)) { - ConsoleLogger.handleSpawnFail(parentPiece); - } - return customItem; - } - - @Override - public EntityNMSSlime spawnNMSSlime(org.bukkit.World bukkitWorld, double x, double y, double z, HologramLine parentPiece) { - WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); - EntityNMSSlime touchSlime = new EntityNMSSlime(nmsWorld, parentPiece); - touchSlime.setLocationNMS(x, y, z); - if (!addEntityToWorld(nmsWorld, touchSlime)) { - ConsoleLogger.handleSpawnFail(parentPiece); - } - return touchSlime; - } - - @Override - public NMSArmorStand spawnNMSArmorStand(org.bukkit.World world, double x, double y, double z, HologramLine parentPiece) { - WorldServer nmsWorld = ((CraftWorld) world).getHandle(); - EntityNMSArmorStand invisibleArmorStand = new EntityNMSArmorStand(nmsWorld, parentPiece); - invisibleArmorStand.setLocationNMS(x, y, z); - if (!addEntityToWorld(nmsWorld, invisibleArmorStand)) { - ConsoleLogger.handleSpawnFail(parentPiece); - } - return invisibleArmorStand; - } - - private boolean addEntityToWorld(WorldServer nmsWorld, Entity nmsEntity) { - Validator.isTrue(Bukkit.isPrimaryThread(), "Async entity add"); - - final int chunkX = MathHelper.floor(nmsEntity.locX / 16.0); - final int chunkZ = MathHelper.floor(nmsEntity.locZ / 16.0); - - if (!nmsWorld.getChunkProviderServer().isLoaded(chunkX, chunkZ)) { - // This should never happen - nmsEntity.dead = true; - return false; - } - - nmsWorld.getChunkAt(chunkX, chunkZ).a(nmsEntity); - nmsWorld.entityList.add(nmsEntity); - - try { - validateEntityMethod.invoke(nmsWorld, nmsEntity); - } catch (Exception e) { - e.printStackTrace(); - return false; - } - return true; - } - - @Override - public boolean isNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { - return ((CraftEntity) bukkitEntity).getHandle() instanceof NMSEntityBase; - } - - @Override - public NMSEntityBase getNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { - - Entity nmsEntity = ((CraftEntity) bukkitEntity).getHandle(); - if (nmsEntity instanceof NMSEntityBase) { - return ((NMSEntityBase) nmsEntity); - } - - return null; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_12_R1; + +import java.lang.reflect.Method; + +import org.bukkit.Bukkit; +import org.bukkit.craftbukkit.v1_12_R1.CraftWorld; +import org.bukkit.craftbukkit.v1_12_R1.entity.CraftEntity; +import org.bukkit.inventory.ItemStack; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; +import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.Validator; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; + +import net.minecraft.server.v1_12_R1.Entity; +import net.minecraft.server.v1_12_R1.EntityTypes; +import net.minecraft.server.v1_12_R1.MathHelper; +import net.minecraft.server.v1_12_R1.RegistryID; +import net.minecraft.server.v1_12_R1.RegistryMaterials; +import net.minecraft.server.v1_12_R1.World; +import net.minecraft.server.v1_12_R1.WorldServer; + +public class NmsManagerImpl implements NMSManager { + + private static final ReflectField>> REGISTRY_ID_FIELD = new ReflectField>>(RegistryMaterials.class, "a"); + private static final ReflectField ID_TO_CLASS_MAP_FIELD = new ReflectField(RegistryID.class, "d"); + + private Method validateEntityMethod; + + @Override + public void setup() throws Exception { + validateEntityMethod = World.class.getDeclaredMethod("b", Entity.class); + validateEntityMethod.setAccessible(true); + + registerCustomEntity(EntityNMSSlime.class, 55); + } + + public void registerCustomEntity(Class entityClass, int id) throws Exception { + // Use reflection to get the RegistryID of entities. + RegistryID> registryID = REGISTRY_ID_FIELD.get(EntityTypes.b); + Object[] idToClassMap = ID_TO_CLASS_MAP_FIELD.get(registryID); + + // Save the the ID -> entity class mapping before the registration. + Object oldValue = idToClassMap[id]; + + // Register the entity class. + registryID.a(entityClass, id); + + // Restore the ID -> entity class mapping. + idToClassMap[id] = oldValue; + } + + @Override + public NMSItem spawnNMSItem(org.bukkit.World bukkitWorld, double x, double y, double z, ItemLine parentPiece, ItemStack stack, ItemPickupManager itemPickupManager) { + WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); + EntityNMSItem customItem = new EntityNMSItem(nmsWorld, parentPiece, itemPickupManager); + customItem.setLocationNMS(x, y, z); + customItem.setItemStackNMS(stack); + if (!addEntityToWorld(nmsWorld, customItem)) { + ConsoleLogger.handleSpawnFail(parentPiece); + } + return customItem; + } + + @Override + public EntityNMSSlime spawnNMSSlime(org.bukkit.World bukkitWorld, double x, double y, double z, HologramLine parentPiece) { + WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); + EntityNMSSlime touchSlime = new EntityNMSSlime(nmsWorld, parentPiece); + touchSlime.setLocationNMS(x, y, z); + if (!addEntityToWorld(nmsWorld, touchSlime)) { + ConsoleLogger.handleSpawnFail(parentPiece); + } + return touchSlime; + } + + @Override + public NMSArmorStand spawnNMSArmorStand(org.bukkit.World world, double x, double y, double z, HologramLine parentPiece) { + WorldServer nmsWorld = ((CraftWorld) world).getHandle(); + EntityNMSArmorStand invisibleArmorStand = new EntityNMSArmorStand(nmsWorld, parentPiece); + invisibleArmorStand.setLocationNMS(x, y, z); + if (!addEntityToWorld(nmsWorld, invisibleArmorStand)) { + ConsoleLogger.handleSpawnFail(parentPiece); + } + return invisibleArmorStand; + } + + private boolean addEntityToWorld(WorldServer nmsWorld, Entity nmsEntity) { + Validator.isTrue(Bukkit.isPrimaryThread(), "Async entity add"); + + final int chunkX = MathHelper.floor(nmsEntity.locX / 16.0); + final int chunkZ = MathHelper.floor(nmsEntity.locZ / 16.0); + + if (!nmsWorld.getChunkProviderServer().isLoaded(chunkX, chunkZ)) { + // This should never happen + nmsEntity.dead = true; + return false; + } + + nmsWorld.getChunkAt(chunkX, chunkZ).a(nmsEntity); + nmsWorld.entityList.add(nmsEntity); + + try { + validateEntityMethod.invoke(nmsWorld, nmsEntity); + } catch (Exception e) { + e.printStackTrace(); + return false; + } + return true; + } + + @Override + public boolean isNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { + return ((CraftEntity) bukkitEntity).getHandle() instanceof NMSEntityBase; + } + + @Override + public NMSEntityBase getNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { + + Entity nmsEntity = ((CraftEntity) bukkitEntity).getHandle(); + if (nmsEntity instanceof NMSEntityBase) { + return ((NMSEntityBase) nmsEntity); + } + + return null; + } + +} diff --git a/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/NullBoundingBox.java b/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/NullBoundingBox.java index 1ad74c98..03ebc531 100644 --- a/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/NullBoundingBox.java +++ b/NMS/v1_12_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_12_R1/NullBoundingBox.java @@ -1,127 +1,141 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_12_R1; - -import net.minecraft.server.v1_12_R1.AxisAlignedBB; -import net.minecraft.server.v1_12_R1.BlockPosition; -import net.minecraft.server.v1_12_R1.MovingObjectPosition; -import net.minecraft.server.v1_12_R1.Vec3D; - -public class NullBoundingBox extends AxisAlignedBB { - - public NullBoundingBox() { - super(0, 0, 0, 0, 0, 0); - } - - @Override - public double a() { - return 0.0; - } - - @Override - public double a(AxisAlignedBB arg0, double arg1) { - return 0.0; - } - - @Override - public AxisAlignedBB a(AxisAlignedBB arg0) { - return this; - } - - @Override - public AxisAlignedBB a(double arg0, double arg1, double arg2) { - return this; - } - - @Override - public MovingObjectPosition b(Vec3D arg0, Vec3D arg1) { - return null; - } - - @Override - public double b(AxisAlignedBB arg0, double arg1) { - return 0.0; - } - - @Override - public double c(AxisAlignedBB arg0, double arg1) { - return 0.0; - } - - @Override - public AxisAlignedBB grow(double arg0, double arg1, double arg2) { - return this; - } - - @Override - public AxisAlignedBB shrink(double arg0) { - return this; - } - - @Override - public AxisAlignedBB a(BlockPosition arg0) { - return this; - } - - @Override - public boolean a(double arg0, double arg1, double arg2, double arg3, double arg4, double arg5) { - return false; - } - - @Override - public boolean b(Vec3D arg0) { - return false; - } - - @Override - public boolean c(Vec3D arg0) { - return false; - } - - @Override - public boolean d(Vec3D arg0) { - return false; - } - - @Override - public AxisAlignedBB e(double arg0) { - return this; - } - - @Override - public AxisAlignedBB g(double arg0) { - return this; - } - - @Override - public AxisAlignedBB a(Vec3D arg0) { - return this; - } - - @Override - public AxisAlignedBB b(AxisAlignedBB arg0) { - return this; - } - - @Override - public AxisAlignedBB b(double arg0, double arg1, double arg2) { - return this; - } - - @Override - public boolean c(AxisAlignedBB arg0) { - return false; - } - - @Override - public AxisAlignedBB d(double arg0, double arg1, double arg2) { - return this; - } - - @Override - public boolean e(Vec3D arg0) { - return false; - } - - - - -} \ No newline at end of file +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_12_R1; + +import net.minecraft.server.v1_12_R1.AxisAlignedBB; +import net.minecraft.server.v1_12_R1.BlockPosition; +import net.minecraft.server.v1_12_R1.MovingObjectPosition; +import net.minecraft.server.v1_12_R1.Vec3D; + +public class NullBoundingBox extends AxisAlignedBB { + + public NullBoundingBox() { + super(0, 0, 0, 0, 0, 0); + } + + @Override + public double a() { + return 0.0; + } + + @Override + public double a(AxisAlignedBB arg0, double arg1) { + return 0.0; + } + + @Override + public AxisAlignedBB a(AxisAlignedBB arg0) { + return this; + } + + @Override + public AxisAlignedBB a(double arg0, double arg1, double arg2) { + return this; + } + + @Override + public MovingObjectPosition b(Vec3D arg0, Vec3D arg1) { + return null; + } + + @Override + public double b(AxisAlignedBB arg0, double arg1) { + return 0.0; + } + + @Override + public double c(AxisAlignedBB arg0, double arg1) { + return 0.0; + } + + @Override + public AxisAlignedBB grow(double arg0, double arg1, double arg2) { + return this; + } + + @Override + public AxisAlignedBB shrink(double arg0) { + return this; + } + + @Override + public AxisAlignedBB a(BlockPosition arg0) { + return this; + } + + @Override + public boolean a(double arg0, double arg1, double arg2, double arg3, double arg4, double arg5) { + return false; + } + + @Override + public boolean b(Vec3D arg0) { + return false; + } + + @Override + public boolean c(Vec3D arg0) { + return false; + } + + @Override + public boolean d(Vec3D arg0) { + return false; + } + + @Override + public AxisAlignedBB e(double arg0) { + return this; + } + + @Override + public AxisAlignedBB g(double arg0) { + return this; + } + + @Override + public AxisAlignedBB a(Vec3D arg0) { + return this; + } + + @Override + public AxisAlignedBB b(AxisAlignedBB arg0) { + return this; + } + + @Override + public AxisAlignedBB b(double arg0, double arg1, double arg2) { + return this; + } + + @Override + public boolean c(AxisAlignedBB arg0) { + return false; + } + + @Override + public AxisAlignedBB d(double arg0, double arg1, double arg2) { + return this; + } + + @Override + public boolean e(Vec3D arg0) { + return false; + } + + + + +} diff --git a/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/CraftNMSArmorStand.java b/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/CraftNMSArmorStand.java index 44bd3f3d..b65ece06 100644 --- a/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/CraftNMSArmorStand.java +++ b/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/CraftNMSArmorStand.java @@ -1,81 +1,95 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_13_R1; - -import java.util.Collection; - -import org.bukkit.EntityEffect; -import org.bukkit.Location; -import org.bukkit.craftbukkit.v1_13_R1.CraftServer; -import org.bukkit.craftbukkit.v1_13_R1.entity.CraftArmorStand; -import org.bukkit.entity.Entity; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -import org.bukkit.inventory.ItemStack; -import org.bukkit.potion.PotionEffect; -import org.bukkit.util.EulerAngle; -import org.bukkit.util.Vector; - -public class CraftNMSArmorStand extends CraftArmorStand { - - public CraftNMSArmorStand(CraftServer server, EntityNMSArmorStand entity) { - super(server, entity); - } - - // Disallow all the bukkit methods. - - @Override - public void remove() { - // Cannot be removed, this is the most important to override. - } - - // Methods from Armor stand class - - @Override public void setArms(boolean arms) { } - @Override public void setBasePlate(boolean basePlate) { } - @Override public void setBodyPose(EulerAngle pose) { } - @Override public void setBoots(ItemStack item) { } - @Override public void setChestplate(ItemStack item) { } - @Override public void setHeadPose(EulerAngle pose) { } - @Override public void setHelmet(ItemStack item) { } - @Override public void setItemInHand(ItemStack item) { } - @Override public void setLeftArmPose(EulerAngle pose) { } - @Override public void setLeftLegPose(EulerAngle pose) { } - @Override public void setLeggings(ItemStack item) { } - @Override public void setRightArmPose(EulerAngle pose) { } - @Override public void setRightLegPose(EulerAngle pose) { } - @Override public void setSmall(boolean small) { } - @Override public void setVisible(boolean visible) { } - @Override public void setMarker(boolean marker) { } - - // Methods from LivingEntity class - @Override public boolean addPotionEffect(PotionEffect effect) { return false; } - @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } - @Override public boolean addPotionEffects(Collection effects) { return false; } - @Override public void setRemoveWhenFarAway(boolean remove) { } - @Override public void setAI(boolean ai) { } - @Override public void setCanPickupItems(boolean pickup) { } - @Override public void setCollidable(boolean collidable) { } - @Override public void setGliding(boolean gliding) { } - @Override public boolean setLeashHolder(Entity holder) { return false; } - @Override public void setSwimming(boolean swimming) { } - - // Methods from Entity - @Override public void setVelocity(Vector vel) { } - @Override public boolean teleport(Location loc) { return false; } - @Override public boolean teleport(Entity entity) { return false; } - @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } - @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } - @Override public void setFireTicks(int ticks) { } - @Override public boolean setPassenger(Entity entity) { return false; } - @Override public boolean eject() { return false; } - @Override public boolean leaveVehicle() { return false; } - @Override public void playEffect(EntityEffect effect) { } - @Override public void setCustomName(String name) { } - @Override public void setCustomNameVisible(boolean flag) { } - @Override public void setGlowing(boolean flag) { } - @Override public void setGravity(boolean gravity) { } - @Override public void setInvulnerable(boolean flag) { } - @Override public void setMomentum(Vector value) { } - @Override public void setSilent(boolean flag) { } - @Override public void setTicksLived(int value) { } - - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_13_R1; + +import java.util.Collection; + +import org.bukkit.EntityEffect; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_13_R1.CraftServer; +import org.bukkit.craftbukkit.v1_13_R1.entity.CraftArmorStand; +import org.bukkit.entity.Entity; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import org.bukkit.inventory.ItemStack; +import org.bukkit.potion.PotionEffect; +import org.bukkit.util.EulerAngle; +import org.bukkit.util.Vector; + +public class CraftNMSArmorStand extends CraftArmorStand { + + public CraftNMSArmorStand(CraftServer server, EntityNMSArmorStand entity) { + super(server, entity); + } + + // Disallow all the bukkit methods. + + @Override + public void remove() { + // Cannot be removed, this is the most important to override. + } + + // Methods from Armor stand class + + @Override public void setArms(boolean arms) { } + @Override public void setBasePlate(boolean basePlate) { } + @Override public void setBodyPose(EulerAngle pose) { } + @Override public void setBoots(ItemStack item) { } + @Override public void setChestplate(ItemStack item) { } + @Override public void setHeadPose(EulerAngle pose) { } + @Override public void setHelmet(ItemStack item) { } + @Override public void setItemInHand(ItemStack item) { } + @Override public void setLeftArmPose(EulerAngle pose) { } + @Override public void setLeftLegPose(EulerAngle pose) { } + @Override public void setLeggings(ItemStack item) { } + @Override public void setRightArmPose(EulerAngle pose) { } + @Override public void setRightLegPose(EulerAngle pose) { } + @Override public void setSmall(boolean small) { } + @Override public void setVisible(boolean visible) { } + @Override public void setMarker(boolean marker) { } + + // Methods from LivingEntity class + @Override public boolean addPotionEffect(PotionEffect effect) { return false; } + @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } + @Override public boolean addPotionEffects(Collection effects) { return false; } + @Override public void setRemoveWhenFarAway(boolean remove) { } + @Override public void setAI(boolean ai) { } + @Override public void setCanPickupItems(boolean pickup) { } + @Override public void setCollidable(boolean collidable) { } + @Override public void setGliding(boolean gliding) { } + @Override public boolean setLeashHolder(Entity holder) { return false; } + @Override public void setSwimming(boolean swimming) { } + + // Methods from Entity + @Override public void setVelocity(Vector vel) { } + @Override public boolean teleport(Location loc) { return false; } + @Override public boolean teleport(Entity entity) { return false; } + @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } + @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } + @Override public void setFireTicks(int ticks) { } + @Override public boolean setPassenger(Entity entity) { return false; } + @Override public boolean eject() { return false; } + @Override public boolean leaveVehicle() { return false; } + @Override public void playEffect(EntityEffect effect) { } + @Override public void setCustomName(String name) { } + @Override public void setCustomNameVisible(boolean flag) { } + @Override public void setGlowing(boolean flag) { } + @Override public void setGravity(boolean gravity) { } + @Override public void setInvulnerable(boolean flag) { } + @Override public void setMomentum(Vector value) { } + @Override public void setSilent(boolean flag) { } + @Override public void setTicksLived(int value) { } + + +} diff --git a/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/CraftNMSItem.java b/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/CraftNMSItem.java index 7b434b77..54b05213 100644 --- a/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/CraftNMSItem.java +++ b/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/CraftNMSItem.java @@ -1,49 +1,63 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_13_R1; - -import org.bukkit.EntityEffect; -import org.bukkit.Location; -import org.bukkit.craftbukkit.v1_13_R1.CraftServer; -import org.bukkit.craftbukkit.v1_13_R1.entity.CraftItem; -import org.bukkit.entity.Entity; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -import org.bukkit.inventory.ItemStack; -import org.bukkit.util.Vector; - -public class CraftNMSItem extends CraftItem { - - public CraftNMSItem(CraftServer server, EntityNMSItem entity) { - super(server, entity); - } - - // Disallow all the bukkit methods. - - @Override - public void remove() { - // Cannot be removed, this is the most important to override. - } - - // Methods from Entity - @Override public void setVelocity(Vector vel) { } - @Override public boolean teleport(Location loc) { return false; } - @Override public boolean teleport(Entity entity) { return false; } - @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } - @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } - @Override public void setFireTicks(int ticks) { } - @Override public boolean setPassenger(Entity entity) { return false; } - @Override public boolean eject() { return false; } - @Override public boolean leaveVehicle() { return false; } - @Override public void playEffect(EntityEffect effect) { } - @Override public void setCustomName(String name) { } - @Override public void setCustomNameVisible(boolean flag) { } - @Override public void setGlowing(boolean flag) { } - @Override public void setGravity(boolean gravity) { } - @Override public void setInvulnerable(boolean flag) { } - @Override public void setMomentum(Vector value) { } - @Override public void setSilent(boolean flag) { } - @Override public void setTicksLived(int value) { } - - // Methods from Item - @Override public void setItemStack(ItemStack stack) { } - @Override public void setPickupDelay(int delay) { } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_13_R1; + +import org.bukkit.EntityEffect; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_13_R1.CraftServer; +import org.bukkit.craftbukkit.v1_13_R1.entity.CraftItem; +import org.bukkit.entity.Entity; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import org.bukkit.inventory.ItemStack; +import org.bukkit.util.Vector; + +public class CraftNMSItem extends CraftItem { + + public CraftNMSItem(CraftServer server, EntityNMSItem entity) { + super(server, entity); + } + + // Disallow all the bukkit methods. + + @Override + public void remove() { + // Cannot be removed, this is the most important to override. + } + + // Methods from Entity + @Override public void setVelocity(Vector vel) { } + @Override public boolean teleport(Location loc) { return false; } + @Override public boolean teleport(Entity entity) { return false; } + @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } + @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } + @Override public void setFireTicks(int ticks) { } + @Override public boolean setPassenger(Entity entity) { return false; } + @Override public boolean eject() { return false; } + @Override public boolean leaveVehicle() { return false; } + @Override public void playEffect(EntityEffect effect) { } + @Override public void setCustomName(String name) { } + @Override public void setCustomNameVisible(boolean flag) { } + @Override public void setGlowing(boolean flag) { } + @Override public void setGravity(boolean gravity) { } + @Override public void setInvulnerable(boolean flag) { } + @Override public void setMomentum(Vector value) { } + @Override public void setSilent(boolean flag) { } + @Override public void setTicksLived(int value) { } + + // Methods from Item + @Override public void setItemStack(ItemStack stack) { } + @Override public void setPickupDelay(int delay) { } + +} diff --git a/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/CraftNMSSlime.java b/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/CraftNMSSlime.java index de16dd59..469e4333 100644 --- a/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/CraftNMSSlime.java +++ b/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/CraftNMSSlime.java @@ -1,65 +1,79 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_13_R1; - -import java.util.Collection; - -import org.bukkit.EntityEffect; -import org.bukkit.Location; -import org.bukkit.craftbukkit.v1_13_R1.CraftServer; -import org.bukkit.craftbukkit.v1_13_R1.entity.CraftSlime; -import org.bukkit.entity.Entity; -import org.bukkit.entity.LivingEntity; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -import org.bukkit.potion.PotionEffect; -import org.bukkit.util.Vector; - -public class CraftNMSSlime extends CraftSlime { - - public CraftNMSSlime(CraftServer server, EntityNMSSlime entity) { - super(server, entity); - } - - // Disallow all the bukkit methods. - - @Override - public void remove() { - // Cannot be removed, this is the most important to override. - } - - // Methods from LivingEntity class - @Override public boolean addPotionEffect(PotionEffect effect) { return false; } - @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } - @Override public boolean addPotionEffects(Collection effects) { return false; } - @Override public void setRemoveWhenFarAway(boolean remove) { } - @Override public void setAI(boolean ai) { } - @Override public void setCanPickupItems(boolean pickup) { } - @Override public void setCollidable(boolean collidable) { } - @Override public void setGliding(boolean gliding) { } - @Override public boolean setLeashHolder(Entity holder) { return false; } - @Override public void setSwimming(boolean swimming) { } - - // Methods from Entity - @Override public void setVelocity(Vector vel) { } - @Override public boolean teleport(Location loc) { return false; } - @Override public boolean teleport(Entity entity) { return false; } - @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } - @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } - @Override public void setFireTicks(int ticks) { } - @Override public boolean setPassenger(Entity entity) { return false; } - @Override public boolean eject() { return false; } - @Override public boolean leaveVehicle() { return false; } - @Override public void playEffect(EntityEffect effect) { } - @Override public void setCustomName(String name) { } - @Override public void setCustomNameVisible(boolean flag) { } - @Override public void setGlowing(boolean flag) { } - @Override public void setGravity(boolean gravity) { } - @Override public void setInvulnerable(boolean flag) { } - @Override public void setMomentum(Vector value) { } - @Override public void setSilent(boolean flag) { } - @Override public void setTicksLived(int value) { } - - - // Methods from Slime - @Override public void setSize(int size) { } - @Override public void setTarget(LivingEntity target) { } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_13_R1; + +import java.util.Collection; + +import org.bukkit.EntityEffect; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_13_R1.CraftServer; +import org.bukkit.craftbukkit.v1_13_R1.entity.CraftSlime; +import org.bukkit.entity.Entity; +import org.bukkit.entity.LivingEntity; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import org.bukkit.potion.PotionEffect; +import org.bukkit.util.Vector; + +public class CraftNMSSlime extends CraftSlime { + + public CraftNMSSlime(CraftServer server, EntityNMSSlime entity) { + super(server, entity); + } + + // Disallow all the bukkit methods. + + @Override + public void remove() { + // Cannot be removed, this is the most important to override. + } + + // Methods from LivingEntity class + @Override public boolean addPotionEffect(PotionEffect effect) { return false; } + @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } + @Override public boolean addPotionEffects(Collection effects) { return false; } + @Override public void setRemoveWhenFarAway(boolean remove) { } + @Override public void setAI(boolean ai) { } + @Override public void setCanPickupItems(boolean pickup) { } + @Override public void setCollidable(boolean collidable) { } + @Override public void setGliding(boolean gliding) { } + @Override public boolean setLeashHolder(Entity holder) { return false; } + @Override public void setSwimming(boolean swimming) { } + + // Methods from Entity + @Override public void setVelocity(Vector vel) { } + @Override public boolean teleport(Location loc) { return false; } + @Override public boolean teleport(Entity entity) { return false; } + @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } + @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } + @Override public void setFireTicks(int ticks) { } + @Override public boolean setPassenger(Entity entity) { return false; } + @Override public boolean eject() { return false; } + @Override public boolean leaveVehicle() { return false; } + @Override public void playEffect(EntityEffect effect) { } + @Override public void setCustomName(String name) { } + @Override public void setCustomNameVisible(boolean flag) { } + @Override public void setGlowing(boolean flag) { } + @Override public void setGravity(boolean gravity) { } + @Override public void setInvulnerable(boolean flag) { } + @Override public void setMomentum(Vector value) { } + @Override public void setSilent(boolean flag) { } + @Override public void setTicksLived(int value) { } + + + // Methods from Slime + @Override public void setSize(int size) { } + @Override public void setTarget(LivingEntity target) { } + +} diff --git a/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/EntityNMSArmorStand.java b/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/EntityNMSArmorStand.java index 22ea85f8..4b938cd4 100644 --- a/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/EntityNMSArmorStand.java +++ b/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/EntityNMSArmorStand.java @@ -1,242 +1,256 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_13_R1; - -import org.bukkit.craftbukkit.v1_13_R1.entity.CraftEntity; -import org.bukkit.craftbukkit.v1_13_R1.util.CraftChatMessage; - -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; -import com.gmail.filoghost.holographicdisplays.disk.Configuration; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; -import com.gmail.filoghost.holographicdisplays.util.Utils; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectionUtils; - -import net.minecraft.server.v1_13_R1.AxisAlignedBB; -import net.minecraft.server.v1_13_R1.DamageSource; -import net.minecraft.server.v1_13_R1.EntityArmorStand; -import net.minecraft.server.v1_13_R1.EntityHuman; -import net.minecraft.server.v1_13_R1.EntityPlayer; -import net.minecraft.server.v1_13_R1.EnumHand; -import net.minecraft.server.v1_13_R1.EnumInteractionResult; -import net.minecraft.server.v1_13_R1.EnumItemSlot; -import net.minecraft.server.v1_13_R1.IChatBaseComponent; -import net.minecraft.server.v1_13_R1.ItemStack; -import net.minecraft.server.v1_13_R1.NBTTagCompound; -import net.minecraft.server.v1_13_R1.PacketPlayOutEntityTeleport; -import net.minecraft.server.v1_13_R1.SoundEffect; -import net.minecraft.server.v1_13_R1.Vec3D; -import net.minecraft.server.v1_13_R1.World; - -public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorStand { - - private boolean lockTick; - private HologramLine parentPiece; - - public EntityNMSArmorStand(World world, HologramLine parentPiece) { - super(world); - super.setInvisible(true); - super.setSmall(true); - super.setArms(false); - super.setNoGravity(true); - super.setBasePlate(true); - super.setMarker(true); - super.collides = false; - this.parentPiece = parentPiece; - forceSetBoundingBox(new NullBoundingBox()); - } - - - @Override - public void b(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public boolean c(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public boolean d(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public NBTTagCompound save(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return nbttagcompound; - } - - @Override - public void f(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public void a(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public boolean isInvulnerable(DamageSource source) { - /* - * The field Entity.invulnerable is private. - * It's only used while saving NBTTags, but since the entity would be killed - * on chunk unload, we prefer to override isInvulnerable(). - */ - return true; - } - - @Override - public boolean isCollidable() { - return false; - } - - @Override - public void setCustomName(IChatBaseComponent ichatbasecomponent) { - // Locks the custom name. - } - - @Override - public void inactiveTick() { - // Check inactive ticks. - - if (!lockTick) { - super.inactiveTick(); - } - } - - @Override - public void setCustomNameVisible(boolean visible) { - // Locks the custom name. - } - - @Override - public EnumInteractionResult a(EntityHuman human, Vec3D vec3d, EnumHand enumhand) { - // Prevent stand being equipped - return EnumInteractionResult.PASS; - } - - @Override - public boolean c(int i, ItemStack item) { - // Prevent stand being equipped - return false; - } - - @Override - public void setSlot(EnumItemSlot enumitemslot, ItemStack itemstack) { - // Prevent stand being equipped - } - - @Override - public void a(AxisAlignedBB boundingBox) { - // Do not change it! - } - - public void forceSetBoundingBox(AxisAlignedBB boundingBox) { - super.a(boundingBox); - } - - @Override - public int getId() { - if (Configuration.preciseHologramMovement) { - StackTraceElement element = ReflectionUtils.getStackTraceElement(2); - if (element != null && element.getFileName() != null && element.getFileName().equals("EntityTrackerEntry.java") && 158 < element.getLineNumber() && element.getLineNumber() < 168) { - // Then this method is being called when creating a new movement packet, we return a fake ID! - return -1; - } - } - - return super.getId(); - } - - @Override - public void tick() { - if (!lockTick) { - super.tick(); - } - } - - @Override - public void a(SoundEffect soundeffect, float f, float f1) { - // Remove sounds. - } - - @Override - public void setCustomNameNMS(String name) { - if (name != null && name.length() > 300) { - name = name.substring(0, 300); - } - super.setCustomName(CraftChatMessage.fromStringOrNull(name)); - super.setCustomNameVisible(name != null && !name.isEmpty()); - } - - @Override - public String getCustomNameNMS() { - return CraftChatMessage.fromComponent(super.getCustomName()); - } - - @Override - public void setLockTick(boolean lock) { - lockTick = lock; - } - - @Override - public void die() { - // Prevent being killed. - } - - @Override - public CraftEntity getBukkitEntity() { - if (super.bukkitEntity == null) { - super.bukkitEntity = new CraftNMSArmorStand(super.world.getServer(), this); - } - return super.bukkitEntity; - } - - @Override - public void killEntityNMS() { - super.dead = true; - } - - @Override - public void setLocationNMS(double x, double y, double z) { - super.setPosition(x, y, z); - - if (Configuration.preciseHologramMovement) { - // Send a packet near to update the position. - PacketPlayOutEntityTeleport teleportPacket = new PacketPlayOutEntityTeleport(this); - - for (Object obj : super.world.players) { - if (obj instanceof EntityPlayer) { - EntityPlayer nmsPlayer = (EntityPlayer) obj; - - double distanceSquared = Utils.square(nmsPlayer.locX - super.locX) + Utils.square(nmsPlayer.locZ - super.locZ); - if (distanceSquared < 8192 && nmsPlayer.playerConnection != null) { - nmsPlayer.playerConnection.sendPacket(teleportPacket); - } - } - } - } - } - - @Override - public boolean isDeadNMS() { - return super.dead; - } - - @Override - public int getIdNMS() { - return super.getId(); // Return the real ID without checking the stack trace. - } - - @Override - public HologramLine getHologramLine() { - return parentPiece; - } - - @Override - public org.bukkit.entity.Entity getBukkitEntityNMS() { - return getBukkitEntity(); - } -} \ No newline at end of file +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_13_R1; + +import org.bukkit.craftbukkit.v1_13_R1.entity.CraftEntity; +import org.bukkit.craftbukkit.v1_13_R1.util.CraftChatMessage; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; +import com.gmail.filoghost.holographicdisplays.disk.Configuration; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; +import com.gmail.filoghost.holographicdisplays.util.Utils; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectionUtils; + +import net.minecraft.server.v1_13_R1.AxisAlignedBB; +import net.minecraft.server.v1_13_R1.DamageSource; +import net.minecraft.server.v1_13_R1.EntityArmorStand; +import net.minecraft.server.v1_13_R1.EntityHuman; +import net.minecraft.server.v1_13_R1.EntityPlayer; +import net.minecraft.server.v1_13_R1.EnumHand; +import net.minecraft.server.v1_13_R1.EnumInteractionResult; +import net.minecraft.server.v1_13_R1.EnumItemSlot; +import net.minecraft.server.v1_13_R1.IChatBaseComponent; +import net.minecraft.server.v1_13_R1.ItemStack; +import net.minecraft.server.v1_13_R1.NBTTagCompound; +import net.minecraft.server.v1_13_R1.PacketPlayOutEntityTeleport; +import net.minecraft.server.v1_13_R1.SoundEffect; +import net.minecraft.server.v1_13_R1.Vec3D; +import net.minecraft.server.v1_13_R1.World; + +public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorStand { + + private boolean lockTick; + private HologramLine parentPiece; + + public EntityNMSArmorStand(World world, HologramLine parentPiece) { + super(world); + super.setInvisible(true); + super.setSmall(true); + super.setArms(false); + super.setNoGravity(true); + super.setBasePlate(true); + super.setMarker(true); + super.collides = false; + this.parentPiece = parentPiece; + forceSetBoundingBox(new NullBoundingBox()); + } + + + @Override + public void b(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public boolean c(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public boolean d(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public NBTTagCompound save(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return nbttagcompound; + } + + @Override + public void f(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public void a(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public boolean isInvulnerable(DamageSource source) { + /* + * The field Entity.invulnerable is private. + * It's only used while saving NBTTags, but since the entity would be killed + * on chunk unload, we prefer to override isInvulnerable(). + */ + return true; + } + + @Override + public boolean isCollidable() { + return false; + } + + @Override + public void setCustomName(IChatBaseComponent ichatbasecomponent) { + // Locks the custom name. + } + + @Override + public void inactiveTick() { + // Check inactive ticks. + + if (!lockTick) { + super.inactiveTick(); + } + } + + @Override + public void setCustomNameVisible(boolean visible) { + // Locks the custom name. + } + + @Override + public EnumInteractionResult a(EntityHuman human, Vec3D vec3d, EnumHand enumhand) { + // Prevent stand being equipped + return EnumInteractionResult.PASS; + } + + @Override + public boolean c(int i, ItemStack item) { + // Prevent stand being equipped + return false; + } + + @Override + public void setSlot(EnumItemSlot enumitemslot, ItemStack itemstack) { + // Prevent stand being equipped + } + + @Override + public void a(AxisAlignedBB boundingBox) { + // Do not change it! + } + + public void forceSetBoundingBox(AxisAlignedBB boundingBox) { + super.a(boundingBox); + } + + @Override + public int getId() { + if (Configuration.preciseHologramMovement) { + StackTraceElement element = ReflectionUtils.getStackTraceElement(2); + if (element != null && element.getFileName() != null && element.getFileName().equals("EntityTrackerEntry.java") && 158 < element.getLineNumber() && element.getLineNumber() < 168) { + // Then this method is being called when creating a new movement packet, we return a fake ID! + return -1; + } + } + + return super.getId(); + } + + @Override + public void tick() { + if (!lockTick) { + super.tick(); + } + } + + @Override + public void a(SoundEffect soundeffect, float f, float f1) { + // Remove sounds. + } + + @Override + public void setCustomNameNMS(String name) { + if (name != null && name.length() > 300) { + name = name.substring(0, 300); + } + super.setCustomName(CraftChatMessage.fromStringOrNull(name)); + super.setCustomNameVisible(name != null && !name.isEmpty()); + } + + @Override + public String getCustomNameNMS() { + return CraftChatMessage.fromComponent(super.getCustomName()); + } + + @Override + public void setLockTick(boolean lock) { + lockTick = lock; + } + + @Override + public void die() { + // Prevent being killed. + } + + @Override + public CraftEntity getBukkitEntity() { + if (super.bukkitEntity == null) { + super.bukkitEntity = new CraftNMSArmorStand(super.world.getServer(), this); + } + return super.bukkitEntity; + } + + @Override + public void killEntityNMS() { + super.dead = true; + } + + @Override + public void setLocationNMS(double x, double y, double z) { + super.setPosition(x, y, z); + + if (Configuration.preciseHologramMovement) { + // Send a packet near to update the position. + PacketPlayOutEntityTeleport teleportPacket = new PacketPlayOutEntityTeleport(this); + + for (Object obj : super.world.players) { + if (obj instanceof EntityPlayer) { + EntityPlayer nmsPlayer = (EntityPlayer) obj; + + double distanceSquared = Utils.square(nmsPlayer.locX - super.locX) + Utils.square(nmsPlayer.locZ - super.locZ); + if (distanceSquared < 8192 && nmsPlayer.playerConnection != null) { + nmsPlayer.playerConnection.sendPacket(teleportPacket); + } + } + } + } + } + + @Override + public boolean isDeadNMS() { + return super.dead; + } + + @Override + public int getIdNMS() { + return super.getId(); // Return the real ID without checking the stack trace. + } + + @Override + public HologramLine getHologramLine() { + return parentPiece; + } + + @Override + public org.bukkit.entity.Entity getBukkitEntityNMS() { + return getBukkitEntity(); + } +} diff --git a/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/EntityNMSItem.java b/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/EntityNMSItem.java index 865056fc..497bdc1f 100644 --- a/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/EntityNMSItem.java +++ b/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/EntityNMSItem.java @@ -1,247 +1,261 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_13_R1; - -import org.bukkit.craftbukkit.v1_13_R1.entity.CraftEntity; -import org.bukkit.craftbukkit.v1_13_R1.inventory.CraftItemStack; -import org.bukkit.entity.Player; - -import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.ItemUtils; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; - -import net.minecraft.server.v1_13_R1.AxisAlignedBB; -import net.minecraft.server.v1_13_R1.Blocks; -import net.minecraft.server.v1_13_R1.DamageSource; -import net.minecraft.server.v1_13_R1.Entity; -import net.minecraft.server.v1_13_R1.EntityHuman; -import net.minecraft.server.v1_13_R1.EntityItem; -import net.minecraft.server.v1_13_R1.EntityPlayer; -import net.minecraft.server.v1_13_R1.ItemStack; -import net.minecraft.server.v1_13_R1.NBTTagCompound; -import net.minecraft.server.v1_13_R1.NBTTagList; -import net.minecraft.server.v1_13_R1.NBTTagString; -import net.minecraft.server.v1_13_R1.World; - -public class EntityNMSItem extends EntityItem implements NMSItem { - - private static final ReflectField VEHICLE_FIELD = new ReflectField(Entity.class, "ax"); - - private boolean lockTick; - private ItemLine parentPiece; - private ItemPickupManager itemPickupManager; - - public EntityNMSItem(World world, ItemLine piece, ItemPickupManager itemPickupManager) { - super(world); - super.pickupDelay = 32767; // Lock the item pickup delay, also prevents entities from picking up the item - this.parentPiece = piece; - this.itemPickupManager = itemPickupManager; - forceSetBoundingBox(new NullBoundingBox()); - } - - @Override - public void tick() { - - // So it won't get removed. - ticksLived = 0; - - if (!lockTick) { - super.tick(); - } - } - - // Method called when a player is near. - @Override - public void d(EntityHuman human) { - - if (human.locY < super.locY - 1.5 || human.locY > super.locY + 1.0) { - // Too low or too high, it's a bit weird. - return; - } - - if (parentPiece.getPickupHandler() != null && human instanceof EntityPlayer) { - itemPickupManager.handleItemLinePickup((Player) human.getBukkitEntity(), parentPiece.getPickupHandler(), parentPiece.getParent()); - // It is never added to the inventory. - } - } - - @Override - public void b(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public boolean c(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public boolean d(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public NBTTagCompound save(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return nbttagcompound; - } - - @Override - public void f(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public void a(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public boolean isInvulnerable(DamageSource source) { - /* - * The field Entity.invulnerable is private. - * It's only used while saving NBTTags, but since the entity would be killed - * on chunk unload, we prefer to override isInvulnerable(). - */ - return true; - } - - @Override - public boolean isCollidable() { - return false; - } - - @Override - public void a(AxisAlignedBB boundingBox) { - // Do not change it! - } - - public void forceSetBoundingBox(AxisAlignedBB boundingBox) { - super.a(boundingBox); - } - - @Override - public void inactiveTick() { - // Check inactive ticks. - - if (!lockTick) { - super.inactiveTick(); - } - } - - @Override - public void setLockTick(boolean lock) { - lockTick = lock; - } - - @Override - public void die() { - // Prevent being killed. - } - - @Override - public CraftEntity getBukkitEntity() { - if (super.bukkitEntity == null) { - super.bukkitEntity = new CraftNMSItem(super.world.getServer(), this); - } - return super.bukkitEntity; - } - - @Override - public boolean isDeadNMS() { - return super.dead; - } - - @Override - public void killEntityNMS() { - super.dead = true; - } - - @Override - public void setLocationNMS(double x, double y, double z) { - super.setPosition(x, y, z); - } - - @Override - public void setItemStackNMS(org.bukkit.inventory.ItemStack stack) { - ItemStack newItem = CraftItemStack.asNMSCopy(stack); // ItemStack.a is returned if the stack is null, invalid or the material is not an Item - - if (newItem == null || newItem == ItemStack.a) { - newItem = new ItemStack(Blocks.BEDROCK); - } - - if (newItem.getTag() == null) { - newItem.setTag(new NBTTagCompound()); - } - NBTTagCompound display = newItem.getTag().getCompound("display"); // Returns a new NBTTagCompound if not existing - if (!newItem.getTag().hasKey("display")) { - newItem.getTag().set("display", display); - } - - NBTTagList tagList = new NBTTagList(); - tagList.add(new NBTTagString(ItemUtils.ANTISTACK_LORE)); // Antistack lore - display.set("Lore", tagList); - - newItem.setCount(1); - - setItemStack(newItem); - } - - @Override - public int getIdNMS() { - return super.getId(); - } - - @Override - public ItemLine getHologramLine() { - return parentPiece; - } - - @Override - public void allowPickup(boolean pickup) { - if (pickup) { - super.pickupDelay = 0; - } else { - super.pickupDelay = 32767; - } - } - - @Override - public org.bukkit.entity.Entity getBukkitEntityNMS() { - return getBukkitEntity(); - } - - @Override - public void setPassengerOfNMS(NMSEntityBase vehicleBase) { - if (vehicleBase == null || !(vehicleBase instanceof Entity)) { - // It should never dismount - return; - } - - Entity entity = (Entity) vehicleBase; - - try { - if (super.getVehicle() != null) { - Entity oldVehicle = super.getVehicle(); - VEHICLE_FIELD.set(this, null); - oldVehicle.passengers.remove(this); - } - - VEHICLE_FIELD.set(this, entity); - entity.passengers.clear(); - entity.passengers.add(this); - - } catch (Exception ex) { - ConsoleLogger.logDebugException(ex); - } - } - - @Override - public Object getRawItemStack() { - return super.getItemStack(); - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_13_R1; + +import org.bukkit.craftbukkit.v1_13_R1.entity.CraftEntity; +import org.bukkit.craftbukkit.v1_13_R1.inventory.CraftItemStack; +import org.bukkit.entity.Player; + +import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.ItemUtils; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; + +import net.minecraft.server.v1_13_R1.AxisAlignedBB; +import net.minecraft.server.v1_13_R1.Blocks; +import net.minecraft.server.v1_13_R1.DamageSource; +import net.minecraft.server.v1_13_R1.Entity; +import net.minecraft.server.v1_13_R1.EntityHuman; +import net.minecraft.server.v1_13_R1.EntityItem; +import net.minecraft.server.v1_13_R1.EntityPlayer; +import net.minecraft.server.v1_13_R1.ItemStack; +import net.minecraft.server.v1_13_R1.NBTTagCompound; +import net.minecraft.server.v1_13_R1.NBTTagList; +import net.minecraft.server.v1_13_R1.NBTTagString; +import net.minecraft.server.v1_13_R1.World; + +public class EntityNMSItem extends EntityItem implements NMSItem { + + private static final ReflectField VEHICLE_FIELD = new ReflectField(Entity.class, "ax"); + + private boolean lockTick; + private ItemLine parentPiece; + private ItemPickupManager itemPickupManager; + + public EntityNMSItem(World world, ItemLine piece, ItemPickupManager itemPickupManager) { + super(world); + super.pickupDelay = 32767; // Lock the item pickup delay, also prevents entities from picking up the item + this.parentPiece = piece; + this.itemPickupManager = itemPickupManager; + forceSetBoundingBox(new NullBoundingBox()); + } + + @Override + public void tick() { + + // So it won't get removed. + ticksLived = 0; + + if (!lockTick) { + super.tick(); + } + } + + // Method called when a player is near. + @Override + public void d(EntityHuman human) { + + if (human.locY < super.locY - 1.5 || human.locY > super.locY + 1.0) { + // Too low or too high, it's a bit weird. + return; + } + + if (parentPiece.getPickupHandler() != null && human instanceof EntityPlayer) { + itemPickupManager.handleItemLinePickup((Player) human.getBukkitEntity(), parentPiece.getPickupHandler(), parentPiece.getParent()); + // It is never added to the inventory. + } + } + + @Override + public void b(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public boolean c(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public boolean d(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public NBTTagCompound save(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return nbttagcompound; + } + + @Override + public void f(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public void a(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public boolean isInvulnerable(DamageSource source) { + /* + * The field Entity.invulnerable is private. + * It's only used while saving NBTTags, but since the entity would be killed + * on chunk unload, we prefer to override isInvulnerable(). + */ + return true; + } + + @Override + public boolean isCollidable() { + return false; + } + + @Override + public void a(AxisAlignedBB boundingBox) { + // Do not change it! + } + + public void forceSetBoundingBox(AxisAlignedBB boundingBox) { + super.a(boundingBox); + } + + @Override + public void inactiveTick() { + // Check inactive ticks. + + if (!lockTick) { + super.inactiveTick(); + } + } + + @Override + public void setLockTick(boolean lock) { + lockTick = lock; + } + + @Override + public void die() { + // Prevent being killed. + } + + @Override + public CraftEntity getBukkitEntity() { + if (super.bukkitEntity == null) { + super.bukkitEntity = new CraftNMSItem(super.world.getServer(), this); + } + return super.bukkitEntity; + } + + @Override + public boolean isDeadNMS() { + return super.dead; + } + + @Override + public void killEntityNMS() { + super.dead = true; + } + + @Override + public void setLocationNMS(double x, double y, double z) { + super.setPosition(x, y, z); + } + + @Override + public void setItemStackNMS(org.bukkit.inventory.ItemStack stack) { + ItemStack newItem = CraftItemStack.asNMSCopy(stack); // ItemStack.a is returned if the stack is null, invalid or the material is not an Item + + if (newItem == null || newItem == ItemStack.a) { + newItem = new ItemStack(Blocks.BEDROCK); + } + + if (newItem.getTag() == null) { + newItem.setTag(new NBTTagCompound()); + } + NBTTagCompound display = newItem.getTag().getCompound("display"); // Returns a new NBTTagCompound if not existing + if (!newItem.getTag().hasKey("display")) { + newItem.getTag().set("display", display); + } + + NBTTagList tagList = new NBTTagList(); + tagList.add(new NBTTagString(ItemUtils.ANTISTACK_LORE)); // Antistack lore + display.set("Lore", tagList); + + newItem.setCount(1); + + setItemStack(newItem); + } + + @Override + public int getIdNMS() { + return super.getId(); + } + + @Override + public ItemLine getHologramLine() { + return parentPiece; + } + + @Override + public void allowPickup(boolean pickup) { + if (pickup) { + super.pickupDelay = 0; + } else { + super.pickupDelay = 32767; + } + } + + @Override + public org.bukkit.entity.Entity getBukkitEntityNMS() { + return getBukkitEntity(); + } + + @Override + public void setPassengerOfNMS(NMSEntityBase vehicleBase) { + if (vehicleBase == null || !(vehicleBase instanceof Entity)) { + // It should never dismount + return; + } + + Entity entity = (Entity) vehicleBase; + + try { + if (super.getVehicle() != null) { + Entity oldVehicle = super.getVehicle(); + VEHICLE_FIELD.set(this, null); + oldVehicle.passengers.remove(this); + } + + VEHICLE_FIELD.set(this, entity); + entity.passengers.clear(); + entity.passengers.add(this); + + } catch (Exception ex) { + ConsoleLogger.logDebugException(ex); + } + } + + @Override + public Object getRawItemStack() { + return super.getItemStack(); + } +} diff --git a/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/EntityNMSSlime.java b/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/EntityNMSSlime.java index e6378e5c..4d50898a 100644 --- a/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/EntityNMSSlime.java +++ b/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/EntityNMSSlime.java @@ -1,217 +1,231 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_13_R1; - -import org.bukkit.Bukkit; -import org.bukkit.craftbukkit.v1_13_R1.entity.CraftEntity; -import org.bukkit.event.player.PlayerInteractEntityEvent; - -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSSlime; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; - -import net.minecraft.server.v1_13_R1.AxisAlignedBB; -import net.minecraft.server.v1_13_R1.DamageSource; -import net.minecraft.server.v1_13_R1.Entity; -import net.minecraft.server.v1_13_R1.EntityDamageSource; -import net.minecraft.server.v1_13_R1.EntityPlayer; -import net.minecraft.server.v1_13_R1.EntitySlime; -import net.minecraft.server.v1_13_R1.IChatBaseComponent; -import net.minecraft.server.v1_13_R1.NBTTagCompound; -import net.minecraft.server.v1_13_R1.SoundEffect; -import net.minecraft.server.v1_13_R1.World; - -public class EntityNMSSlime extends EntitySlime implements NMSSlime { - - private static final ReflectField VEHICLE_FIELD = new ReflectField(Entity.class, "ax"); - - private boolean lockTick; - private HologramLine parentPiece; - - public EntityNMSSlime(World world, HologramLine parentPiece) { - super(world); - super.persistent = true; - super.collides = false; - a(0.0F, 0.0F); - setSize(1, false); - setInvisible(true); - this.parentPiece = parentPiece; - forceSetBoundingBox(new NullBoundingBox()); - } - - @Override - public void a(AxisAlignedBB boundingBox) { - // Do not change it! - } - - public void forceSetBoundingBox(AxisAlignedBB boundingBox) { - super.a(boundingBox); - } - - @Override - public void tick() { - - // So it won't get removed. - ticksLived = 0; - - if (!lockTick) { - super.tick(); - } - } - - @Override - public void b(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public boolean c(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public boolean d(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public NBTTagCompound save(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return nbttagcompound; - } - - @Override - public void f(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public void a(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public boolean damageEntity(DamageSource damageSource, float amount) { - if (damageSource instanceof EntityDamageSource) { - EntityDamageSource entityDamageSource = (EntityDamageSource) damageSource; - if (entityDamageSource.getEntity() instanceof EntityPlayer) { - Bukkit.getPluginManager().callEvent(new PlayerInteractEntityEvent(((EntityPlayer) entityDamageSource.getEntity()).getBukkitEntity(), getBukkitEntity())); // Bukkit takes care of the exceptions - } - } - return false; - } - - @Override - public boolean isInvulnerable(DamageSource source) { - /* - * The field Entity.invulnerable is private. - * It's only used while saving NBTTags, but since the entity would be killed - * on chunk unload, we prefer to override isInvulnerable(). - */ - return true; - } - - @Override - public boolean isCollidable() { - return false; - } - - @Override - public void inactiveTick() { - // Check inactive ticks. - - if (!lockTick) { - super.inactiveTick(); - } - } - - @Override - public void setCustomName(IChatBaseComponent ichatbasecomponent) { - // Locks the custom name. - } - - @Override - public void setCustomNameVisible(boolean visible) { - // Locks the custom name. - } - - @Override - public void a(SoundEffect soundeffect, float f, float f1) { - // Remove sounds. - } - - @Override - public void setLockTick(boolean lock) { - lockTick = lock; - } - - @Override - public void die() { - // Prevent being killed. - } - - @Override - public CraftEntity getBukkitEntity() { - if (super.bukkitEntity == null) { - super.bukkitEntity = new CraftNMSSlime(super.world.getServer(), this); - } - return super.bukkitEntity; - } - - @Override - public boolean isDeadNMS() { - return super.dead; - } - - @Override - public void killEntityNMS() { - super.dead = true; - } - - @Override - public void setLocationNMS(double x, double y, double z) { - super.setPosition(x, y, z); - } - - @Override - public int getIdNMS() { - return super.getId(); - } - - @Override - public HologramLine getHologramLine() { - return parentPiece; - } - - @Override - public org.bukkit.entity.Entity getBukkitEntityNMS() { - return getBukkitEntity(); - } - - @Override - public void setPassengerOfNMS(NMSEntityBase vehicleBase) { - if (vehicleBase == null || !(vehicleBase instanceof Entity)) { - // It should never dismount - return; - } - - Entity entity = (Entity) vehicleBase; - - try { - if (super.getVehicle() != null) { - Entity oldVehicle = super.getVehicle(); - VEHICLE_FIELD.set(this, null); - oldVehicle.passengers.remove(this); - } - - VEHICLE_FIELD.set(this, entity); - entity.passengers.clear(); - entity.passengers.add(this); - - } catch (Exception ex) { - ConsoleLogger.logDebugException(ex); - } - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_13_R1; + +import org.bukkit.Bukkit; +import org.bukkit.craftbukkit.v1_13_R1.entity.CraftEntity; +import org.bukkit.event.player.PlayerInteractEntityEvent; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSSlime; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; + +import net.minecraft.server.v1_13_R1.AxisAlignedBB; +import net.minecraft.server.v1_13_R1.DamageSource; +import net.minecraft.server.v1_13_R1.Entity; +import net.minecraft.server.v1_13_R1.EntityDamageSource; +import net.minecraft.server.v1_13_R1.EntityPlayer; +import net.minecraft.server.v1_13_R1.EntitySlime; +import net.minecraft.server.v1_13_R1.IChatBaseComponent; +import net.minecraft.server.v1_13_R1.NBTTagCompound; +import net.minecraft.server.v1_13_R1.SoundEffect; +import net.minecraft.server.v1_13_R1.World; + +public class EntityNMSSlime extends EntitySlime implements NMSSlime { + + private static final ReflectField VEHICLE_FIELD = new ReflectField(Entity.class, "ax"); + + private boolean lockTick; + private HologramLine parentPiece; + + public EntityNMSSlime(World world, HologramLine parentPiece) { + super(world); + super.persistent = true; + super.collides = false; + a(0.0F, 0.0F); + setSize(1, false); + setInvisible(true); + this.parentPiece = parentPiece; + forceSetBoundingBox(new NullBoundingBox()); + } + + @Override + public void a(AxisAlignedBB boundingBox) { + // Do not change it! + } + + public void forceSetBoundingBox(AxisAlignedBB boundingBox) { + super.a(boundingBox); + } + + @Override + public void tick() { + + // So it won't get removed. + ticksLived = 0; + + if (!lockTick) { + super.tick(); + } + } + + @Override + public void b(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public boolean c(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public boolean d(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public NBTTagCompound save(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return nbttagcompound; + } + + @Override + public void f(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public void a(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public boolean damageEntity(DamageSource damageSource, float amount) { + if (damageSource instanceof EntityDamageSource) { + EntityDamageSource entityDamageSource = (EntityDamageSource) damageSource; + if (entityDamageSource.getEntity() instanceof EntityPlayer) { + Bukkit.getPluginManager().callEvent(new PlayerInteractEntityEvent(((EntityPlayer) entityDamageSource.getEntity()).getBukkitEntity(), getBukkitEntity())); // Bukkit takes care of the exceptions + } + } + return false; + } + + @Override + public boolean isInvulnerable(DamageSource source) { + /* + * The field Entity.invulnerable is private. + * It's only used while saving NBTTags, but since the entity would be killed + * on chunk unload, we prefer to override isInvulnerable(). + */ + return true; + } + + @Override + public boolean isCollidable() { + return false; + } + + @Override + public void inactiveTick() { + // Check inactive ticks. + + if (!lockTick) { + super.inactiveTick(); + } + } + + @Override + public void setCustomName(IChatBaseComponent ichatbasecomponent) { + // Locks the custom name. + } + + @Override + public void setCustomNameVisible(boolean visible) { + // Locks the custom name. + } + + @Override + public void a(SoundEffect soundeffect, float f, float f1) { + // Remove sounds. + } + + @Override + public void setLockTick(boolean lock) { + lockTick = lock; + } + + @Override + public void die() { + // Prevent being killed. + } + + @Override + public CraftEntity getBukkitEntity() { + if (super.bukkitEntity == null) { + super.bukkitEntity = new CraftNMSSlime(super.world.getServer(), this); + } + return super.bukkitEntity; + } + + @Override + public boolean isDeadNMS() { + return super.dead; + } + + @Override + public void killEntityNMS() { + super.dead = true; + } + + @Override + public void setLocationNMS(double x, double y, double z) { + super.setPosition(x, y, z); + } + + @Override + public int getIdNMS() { + return super.getId(); + } + + @Override + public HologramLine getHologramLine() { + return parentPiece; + } + + @Override + public org.bukkit.entity.Entity getBukkitEntityNMS() { + return getBukkitEntity(); + } + + @Override + public void setPassengerOfNMS(NMSEntityBase vehicleBase) { + if (vehicleBase == null || !(vehicleBase instanceof Entity)) { + // It should never dismount + return; + } + + Entity entity = (Entity) vehicleBase; + + try { + if (super.getVehicle() != null) { + Entity oldVehicle = super.getVehicle(); + VEHICLE_FIELD.set(this, null); + oldVehicle.passengers.remove(this); + } + + VEHICLE_FIELD.set(this, entity); + entity.passengers.clear(); + entity.passengers.add(this); + + } catch (Exception ex) { + ConsoleLogger.logDebugException(ex); + } + } +} diff --git a/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/NmsManagerImpl.java b/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/NmsManagerImpl.java index 0b5b531d..742cdb6a 100644 --- a/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/NmsManagerImpl.java +++ b/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/NmsManagerImpl.java @@ -1,154 +1,168 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_13_R1; - -import java.lang.reflect.Method; -import java.util.List; -import java.util.function.Function; - -import org.bukkit.Bukkit; -import org.bukkit.craftbukkit.v1_13_R1.CraftWorld; -import org.bukkit.craftbukkit.v1_13_R1.entity.CraftEntity; -import org.bukkit.inventory.ItemStack; - -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; -import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.Validator; -import com.gmail.filoghost.holographicdisplays.util.VersionUtils; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; - -import net.minecraft.server.v1_13_R1.Entity; -import net.minecraft.server.v1_13_R1.EntityTypes; -import net.minecraft.server.v1_13_R1.MathHelper; -import net.minecraft.server.v1_13_R1.RegistryID; -import net.minecraft.server.v1_13_R1.RegistryMaterials; -import net.minecraft.server.v1_13_R1.World; -import net.minecraft.server.v1_13_R1.WorldServer; - -public class NmsManagerImpl implements NMSManager { - - private static final ReflectField>> REGISTRY_ID_FIELD = new ReflectField>>(RegistryMaterials.class, "a"); - private static final ReflectField ID_TO_CLASS_MAP_FIELD = new ReflectField(RegistryID.class, "d"); - private static final ReflectField> ENTITY_LIST_FIELD = new ReflectField>(World.class, "entityList"); - - private Method validateEntityMethod; - - @Override - public void setup() throws Exception { - validateEntityMethod = World.class.getDeclaredMethod("b", Entity.class); - validateEntityMethod.setAccessible(true); - - registerCustomEntity(EntityNMSSlime.class, 55); - } - - public void registerCustomEntity(Class entityClass, int id) throws Exception { - // Use reflection to get the RegistryID of entities. - RegistryID> registryID = REGISTRY_ID_FIELD.get(EntityTypes.REGISTRY); - Object[] idToClassMap = ID_TO_CLASS_MAP_FIELD.get(registryID); - - // Save the the ID -> EntityTypes mapping before the registration. - Object oldValue = idToClassMap[id]; - - // Register the EntityTypes object. - registryID.a(new EntityTypes(entityClass, new Function() { - - @Override - public Entity apply(World world) { - return null; - } - - }, true, true, null), id); - - // Restore the ID -> EntityTypes mapping. - idToClassMap[id] = oldValue; - } - - @Override - public NMSItem spawnNMSItem(org.bukkit.World bukkitWorld, double x, double y, double z, ItemLine parentPiece, ItemStack stack, ItemPickupManager itemPickupManager) { - WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); - EntityNMSItem customItem = new EntityNMSItem(nmsWorld, parentPiece, itemPickupManager); - customItem.setLocationNMS(x, y, z); - customItem.setItemStackNMS(stack); - if (!addEntityToWorld(nmsWorld, customItem)) { - ConsoleLogger.handleSpawnFail(parentPiece); - } - return customItem; - } - - @Override - public EntityNMSSlime spawnNMSSlime(org.bukkit.World bukkitWorld, double x, double y, double z, HologramLine parentPiece) { - WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); - EntityNMSSlime touchSlime = new EntityNMSSlime(nmsWorld, parentPiece); - touchSlime.setLocationNMS(x, y, z); - if (!addEntityToWorld(nmsWorld, touchSlime)) { - ConsoleLogger.handleSpawnFail(parentPiece); - } - return touchSlime; - } - - @Override - public NMSArmorStand spawnNMSArmorStand(org.bukkit.World world, double x, double y, double z, HologramLine parentPiece) { - WorldServer nmsWorld = ((CraftWorld) world).getHandle(); - EntityNMSArmorStand invisibleArmorStand = new EntityNMSArmorStand(nmsWorld, parentPiece); - invisibleArmorStand.setLocationNMS(x, y, z); - if (!addEntityToWorld(nmsWorld, invisibleArmorStand)) { - ConsoleLogger.handleSpawnFail(parentPiece); - } - return invisibleArmorStand; - } - - private boolean addEntityToWorld(WorldServer nmsWorld, Entity nmsEntity) { - Validator.isTrue(Bukkit.isPrimaryThread(), "Async entity add"); - - final int chunkX = MathHelper.floor(nmsEntity.locX / 16.0); - final int chunkZ = MathHelper.floor(nmsEntity.locZ / 16.0); - - if (!nmsWorld.getChunkProviderServer().isLoaded(chunkX, chunkZ)) { - // This should never happen - nmsEntity.dead = true; - return false; - } - - nmsWorld.getChunkAt(chunkX, chunkZ).a(nmsEntity); - if (VersionUtils.isPaperServer()) { - try { - // Workaround because nmsWorld.entityList is a different class in Paper, if used without reflection it throws NoSuchFieldError. - ENTITY_LIST_FIELD.get(nmsWorld).add(nmsEntity); - } catch (Exception e) { - e.printStackTrace(); - return false; - } - } else { - nmsWorld.entityList.add(nmsEntity); - } - - try { - validateEntityMethod.invoke(nmsWorld, nmsEntity); - } catch (Exception e) { - e.printStackTrace(); - return false; - } - return true; - } - - @Override - public boolean isNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { - return ((CraftEntity) bukkitEntity).getHandle() instanceof NMSEntityBase; - } - - @Override - public NMSEntityBase getNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { - - Entity nmsEntity = ((CraftEntity) bukkitEntity).getHandle(); - if (nmsEntity instanceof NMSEntityBase) { - return ((NMSEntityBase) nmsEntity); - } - - return null; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_13_R1; + +import java.lang.reflect.Method; +import java.util.List; +import java.util.function.Function; + +import org.bukkit.Bukkit; +import org.bukkit.craftbukkit.v1_13_R1.CraftWorld; +import org.bukkit.craftbukkit.v1_13_R1.entity.CraftEntity; +import org.bukkit.inventory.ItemStack; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; +import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.Validator; +import com.gmail.filoghost.holographicdisplays.util.VersionUtils; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; + +import net.minecraft.server.v1_13_R1.Entity; +import net.minecraft.server.v1_13_R1.EntityTypes; +import net.minecraft.server.v1_13_R1.MathHelper; +import net.minecraft.server.v1_13_R1.RegistryID; +import net.minecraft.server.v1_13_R1.RegistryMaterials; +import net.minecraft.server.v1_13_R1.World; +import net.minecraft.server.v1_13_R1.WorldServer; + +public class NmsManagerImpl implements NMSManager { + + private static final ReflectField>> REGISTRY_ID_FIELD = new ReflectField>>(RegistryMaterials.class, "a"); + private static final ReflectField ID_TO_CLASS_MAP_FIELD = new ReflectField(RegistryID.class, "d"); + private static final ReflectField> ENTITY_LIST_FIELD = new ReflectField>(World.class, "entityList"); + + private Method validateEntityMethod; + + @Override + public void setup() throws Exception { + validateEntityMethod = World.class.getDeclaredMethod("b", Entity.class); + validateEntityMethod.setAccessible(true); + + registerCustomEntity(EntityNMSSlime.class, 55); + } + + public void registerCustomEntity(Class entityClass, int id) throws Exception { + // Use reflection to get the RegistryID of entities. + RegistryID> registryID = REGISTRY_ID_FIELD.get(EntityTypes.REGISTRY); + Object[] idToClassMap = ID_TO_CLASS_MAP_FIELD.get(registryID); + + // Save the the ID -> EntityTypes mapping before the registration. + Object oldValue = idToClassMap[id]; + + // Register the EntityTypes object. + registryID.a(new EntityTypes(entityClass, new Function() { + + @Override + public Entity apply(World world) { + return null; + } + + }, true, true, null), id); + + // Restore the ID -> EntityTypes mapping. + idToClassMap[id] = oldValue; + } + + @Override + public NMSItem spawnNMSItem(org.bukkit.World bukkitWorld, double x, double y, double z, ItemLine parentPiece, ItemStack stack, ItemPickupManager itemPickupManager) { + WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); + EntityNMSItem customItem = new EntityNMSItem(nmsWorld, parentPiece, itemPickupManager); + customItem.setLocationNMS(x, y, z); + customItem.setItemStackNMS(stack); + if (!addEntityToWorld(nmsWorld, customItem)) { + ConsoleLogger.handleSpawnFail(parentPiece); + } + return customItem; + } + + @Override + public EntityNMSSlime spawnNMSSlime(org.bukkit.World bukkitWorld, double x, double y, double z, HologramLine parentPiece) { + WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); + EntityNMSSlime touchSlime = new EntityNMSSlime(nmsWorld, parentPiece); + touchSlime.setLocationNMS(x, y, z); + if (!addEntityToWorld(nmsWorld, touchSlime)) { + ConsoleLogger.handleSpawnFail(parentPiece); + } + return touchSlime; + } + + @Override + public NMSArmorStand spawnNMSArmorStand(org.bukkit.World world, double x, double y, double z, HologramLine parentPiece) { + WorldServer nmsWorld = ((CraftWorld) world).getHandle(); + EntityNMSArmorStand invisibleArmorStand = new EntityNMSArmorStand(nmsWorld, parentPiece); + invisibleArmorStand.setLocationNMS(x, y, z); + if (!addEntityToWorld(nmsWorld, invisibleArmorStand)) { + ConsoleLogger.handleSpawnFail(parentPiece); + } + return invisibleArmorStand; + } + + private boolean addEntityToWorld(WorldServer nmsWorld, Entity nmsEntity) { + Validator.isTrue(Bukkit.isPrimaryThread(), "Async entity add"); + + final int chunkX = MathHelper.floor(nmsEntity.locX / 16.0); + final int chunkZ = MathHelper.floor(nmsEntity.locZ / 16.0); + + if (!nmsWorld.getChunkProviderServer().isLoaded(chunkX, chunkZ)) { + // This should never happen + nmsEntity.dead = true; + return false; + } + + nmsWorld.getChunkAt(chunkX, chunkZ).a(nmsEntity); + if (VersionUtils.isPaperServer()) { + try { + // Workaround because nmsWorld.entityList is a different class in Paper, if used without reflection it throws NoSuchFieldError. + ENTITY_LIST_FIELD.get(nmsWorld).add(nmsEntity); + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } else { + nmsWorld.entityList.add(nmsEntity); + } + + try { + validateEntityMethod.invoke(nmsWorld, nmsEntity); + } catch (Exception e) { + e.printStackTrace(); + return false; + } + return true; + } + + @Override + public boolean isNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { + return ((CraftEntity) bukkitEntity).getHandle() instanceof NMSEntityBase; + } + + @Override + public NMSEntityBase getNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { + + Entity nmsEntity = ((CraftEntity) bukkitEntity).getHandle(); + if (nmsEntity instanceof NMSEntityBase) { + return ((NMSEntityBase) nmsEntity); + } + + return null; + } + +} diff --git a/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/NullBoundingBox.java b/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/NullBoundingBox.java index 72abb649..1321bd23 100644 --- a/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/NullBoundingBox.java +++ b/NMS/v1_13_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R1/NullBoundingBox.java @@ -1,118 +1,132 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_13_R1; - -import net.minecraft.server.v1_13_R1.AxisAlignedBB; -import net.minecraft.server.v1_13_R1.BlockPosition; -import net.minecraft.server.v1_13_R1.EnumDirection.EnumAxis; -import net.minecraft.server.v1_13_R1.MovingObjectPosition; -import net.minecraft.server.v1_13_R1.Vec3D; - -public class NullBoundingBox extends AxisAlignedBB { - - public NullBoundingBox() { - super(0, 0, 0, 0, 0, 0); - } - - @Override - public double a() { - return 0.0; - } - - @Override - public AxisAlignedBB a(AxisAlignedBB arg0) { - return this; - } - - @Override - public AxisAlignedBB a(double arg0, double arg1, double arg2) { - return this; - } - - @Override - public MovingObjectPosition b(Vec3D arg0, Vec3D arg1) { - return null; - } - - @Override - public AxisAlignedBB grow(double arg0, double arg1, double arg2) { - return this; - } - - @Override - public AxisAlignedBB shrink(double arg0) { - return this; - } - - @Override - public AxisAlignedBB a(BlockPosition arg0) { - return this; - } - - @Override - public boolean a(double arg0, double arg1, double arg2, double arg3, double arg4, double arg5) { - return false; - } - - @Override - public boolean b(Vec3D arg0) { - return false; - } - - @Override - public AxisAlignedBB g(double arg0) { - return this; - } - - @Override - public AxisAlignedBB a(Vec3D arg0) { - return this; - } - - @Override - public AxisAlignedBB b(AxisAlignedBB arg0) { - return this; - } - - @Override - public AxisAlignedBB b(double arg0, double arg1, double arg2) { - return this; - } - - @Override - public boolean c(AxisAlignedBB arg0) { - return false; - } - - @Override - public AxisAlignedBB d(double arg0, double arg1, double arg2) { - return this; - } - - @Override - public double a(EnumAxis arg0) { - return 0.0; - } - - @Override - public MovingObjectPosition a(Vec3D arg0, Vec3D arg1, BlockPosition arg2) { - return null; - } - - @Override - public double b(EnumAxis arg0) { - return 0.0; - } - - @Override - public boolean e(double arg0, double arg1, double arg2) { - return false; - } - - @Override - public AxisAlignedBB f(double arg0, double arg1, double arg2) { - return this; - } - - - - -} \ No newline at end of file +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_13_R1; + +import net.minecraft.server.v1_13_R1.AxisAlignedBB; +import net.minecraft.server.v1_13_R1.BlockPosition; +import net.minecraft.server.v1_13_R1.EnumDirection.EnumAxis; +import net.minecraft.server.v1_13_R1.MovingObjectPosition; +import net.minecraft.server.v1_13_R1.Vec3D; + +public class NullBoundingBox extends AxisAlignedBB { + + public NullBoundingBox() { + super(0, 0, 0, 0, 0, 0); + } + + @Override + public double a() { + return 0.0; + } + + @Override + public AxisAlignedBB a(AxisAlignedBB arg0) { + return this; + } + + @Override + public AxisAlignedBB a(double arg0, double arg1, double arg2) { + return this; + } + + @Override + public MovingObjectPosition b(Vec3D arg0, Vec3D arg1) { + return null; + } + + @Override + public AxisAlignedBB grow(double arg0, double arg1, double arg2) { + return this; + } + + @Override + public AxisAlignedBB shrink(double arg0) { + return this; + } + + @Override + public AxisAlignedBB a(BlockPosition arg0) { + return this; + } + + @Override + public boolean a(double arg0, double arg1, double arg2, double arg3, double arg4, double arg5) { + return false; + } + + @Override + public boolean b(Vec3D arg0) { + return false; + } + + @Override + public AxisAlignedBB g(double arg0) { + return this; + } + + @Override + public AxisAlignedBB a(Vec3D arg0) { + return this; + } + + @Override + public AxisAlignedBB b(AxisAlignedBB arg0) { + return this; + } + + @Override + public AxisAlignedBB b(double arg0, double arg1, double arg2) { + return this; + } + + @Override + public boolean c(AxisAlignedBB arg0) { + return false; + } + + @Override + public AxisAlignedBB d(double arg0, double arg1, double arg2) { + return this; + } + + @Override + public double a(EnumAxis arg0) { + return 0.0; + } + + @Override + public MovingObjectPosition a(Vec3D arg0, Vec3D arg1, BlockPosition arg2) { + return null; + } + + @Override + public double b(EnumAxis arg0) { + return 0.0; + } + + @Override + public boolean e(double arg0, double arg1, double arg2) { + return false; + } + + @Override + public AxisAlignedBB f(double arg0, double arg1, double arg2) { + return this; + } + + + + +} diff --git a/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/CraftNMSArmorStand.java b/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/CraftNMSArmorStand.java index 4c5fa7c3..309a73ea 100644 --- a/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/CraftNMSArmorStand.java +++ b/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/CraftNMSArmorStand.java @@ -1,82 +1,96 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_13_R2; - -import java.util.Collection; - -import org.bukkit.EntityEffect; -import org.bukkit.Location; -import org.bukkit.craftbukkit.v1_13_R2.CraftServer; -import org.bukkit.craftbukkit.v1_13_R2.entity.CraftArmorStand; -import org.bukkit.entity.Entity; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -import org.bukkit.inventory.ItemStack; -import org.bukkit.potion.PotionEffect; -import org.bukkit.util.EulerAngle; -import org.bukkit.util.Vector; - -public class CraftNMSArmorStand extends CraftArmorStand { - - public CraftNMSArmorStand(CraftServer server, EntityNMSArmorStand entity) { - super(server, entity); - } - - // Disallow all the bukkit methods. - - @Override - public void remove() { - // Cannot be removed, this is the most important to override. - } - - // Methods from Armor stand class - - @Override public void setArms(boolean arms) { } - @Override public void setBasePlate(boolean basePlate) { } - @Override public void setBodyPose(EulerAngle pose) { } - @Override public void setBoots(ItemStack item) { } - @Override public void setChestplate(ItemStack item) { } - @Override public void setHeadPose(EulerAngle pose) { } - @Override public void setHelmet(ItemStack item) { } - @Override public void setItemInHand(ItemStack item) { } - @Override public void setLeftArmPose(EulerAngle pose) { } - @Override public void setLeftLegPose(EulerAngle pose) { } - @Override public void setLeggings(ItemStack item) { } - @Override public void setRightArmPose(EulerAngle pose) { } - @Override public void setRightLegPose(EulerAngle pose) { } - @Override public void setSmall(boolean small) { } - @Override public void setVisible(boolean visible) { } - @Override public void setMarker(boolean marker) { } - - // Methods from LivingEntity class - @Override public boolean addPotionEffect(PotionEffect effect) { return false; } - @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } - @Override public boolean addPotionEffects(Collection effects) { return false; } - @Override public void setRemoveWhenFarAway(boolean remove) { } - @Override public void setAI(boolean ai) { } - @Override public void setCanPickupItems(boolean pickup) { } - @Override public void setCollidable(boolean collidable) { } - @Override public void setGliding(boolean gliding) { } - @Override public boolean setLeashHolder(Entity holder) { return false; } - @Override public void setSwimming(boolean swimming) { } - - // Methods from Entity - @Override public void setVelocity(Vector vel) { } - @Override public boolean teleport(Location loc) { return false; } - @Override public boolean teleport(Entity entity) { return false; } - @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } - @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } - @Override public void setFireTicks(int ticks) { } - @Override public boolean setPassenger(Entity entity) { return false; } - @Override public boolean eject() { return false; } - @Override public boolean leaveVehicle() { return false; } - @Override public void playEffect(EntityEffect effect) { } - @Override public void setCustomName(String name) { } - @Override public void setCustomNameVisible(boolean flag) { } - @Override public void setGlowing(boolean flag) { } - @Override public void setGravity(boolean gravity) { } - @Override public void setInvulnerable(boolean flag) { } - @Override public void setMomentum(Vector value) { } - @Override public void setSilent(boolean flag) { } - @Override public void setTicksLived(int value) { } - @Override public void setPersistent(boolean flag) { } - - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_13_R2; + +import java.util.Collection; + +import org.bukkit.EntityEffect; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_13_R2.CraftServer; +import org.bukkit.craftbukkit.v1_13_R2.entity.CraftArmorStand; +import org.bukkit.entity.Entity; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import org.bukkit.inventory.ItemStack; +import org.bukkit.potion.PotionEffect; +import org.bukkit.util.EulerAngle; +import org.bukkit.util.Vector; + +public class CraftNMSArmorStand extends CraftArmorStand { + + public CraftNMSArmorStand(CraftServer server, EntityNMSArmorStand entity) { + super(server, entity); + } + + // Disallow all the bukkit methods. + + @Override + public void remove() { + // Cannot be removed, this is the most important to override. + } + + // Methods from Armor stand class + + @Override public void setArms(boolean arms) { } + @Override public void setBasePlate(boolean basePlate) { } + @Override public void setBodyPose(EulerAngle pose) { } + @Override public void setBoots(ItemStack item) { } + @Override public void setChestplate(ItemStack item) { } + @Override public void setHeadPose(EulerAngle pose) { } + @Override public void setHelmet(ItemStack item) { } + @Override public void setItemInHand(ItemStack item) { } + @Override public void setLeftArmPose(EulerAngle pose) { } + @Override public void setLeftLegPose(EulerAngle pose) { } + @Override public void setLeggings(ItemStack item) { } + @Override public void setRightArmPose(EulerAngle pose) { } + @Override public void setRightLegPose(EulerAngle pose) { } + @Override public void setSmall(boolean small) { } + @Override public void setVisible(boolean visible) { } + @Override public void setMarker(boolean marker) { } + + // Methods from LivingEntity class + @Override public boolean addPotionEffect(PotionEffect effect) { return false; } + @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } + @Override public boolean addPotionEffects(Collection effects) { return false; } + @Override public void setRemoveWhenFarAway(boolean remove) { } + @Override public void setAI(boolean ai) { } + @Override public void setCanPickupItems(boolean pickup) { } + @Override public void setCollidable(boolean collidable) { } + @Override public void setGliding(boolean gliding) { } + @Override public boolean setLeashHolder(Entity holder) { return false; } + @Override public void setSwimming(boolean swimming) { } + + // Methods from Entity + @Override public void setVelocity(Vector vel) { } + @Override public boolean teleport(Location loc) { return false; } + @Override public boolean teleport(Entity entity) { return false; } + @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } + @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } + @Override public void setFireTicks(int ticks) { } + @Override public boolean setPassenger(Entity entity) { return false; } + @Override public boolean eject() { return false; } + @Override public boolean leaveVehicle() { return false; } + @Override public void playEffect(EntityEffect effect) { } + @Override public void setCustomName(String name) { } + @Override public void setCustomNameVisible(boolean flag) { } + @Override public void setGlowing(boolean flag) { } + @Override public void setGravity(boolean gravity) { } + @Override public void setInvulnerable(boolean flag) { } + @Override public void setMomentum(Vector value) { } + @Override public void setSilent(boolean flag) { } + @Override public void setTicksLived(int value) { } + @Override public void setPersistent(boolean flag) { } + + +} diff --git a/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/CraftNMSItem.java b/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/CraftNMSItem.java index f02aaa58..076f4800 100644 --- a/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/CraftNMSItem.java +++ b/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/CraftNMSItem.java @@ -1,50 +1,64 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_13_R2; - -import org.bukkit.EntityEffect; -import org.bukkit.Location; -import org.bukkit.craftbukkit.v1_13_R2.CraftServer; -import org.bukkit.craftbukkit.v1_13_R2.entity.CraftItem; -import org.bukkit.entity.Entity; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -import org.bukkit.inventory.ItemStack; -import org.bukkit.util.Vector; - -public class CraftNMSItem extends CraftItem { - - public CraftNMSItem(CraftServer server, EntityNMSItem entity) { - super(server, entity); - } - - // Disallow all the bukkit methods. - - @Override - public void remove() { - // Cannot be removed, this is the most important to override. - } - - // Methods from Entity - @Override public void setVelocity(Vector vel) { } - @Override public boolean teleport(Location loc) { return false; } - @Override public boolean teleport(Entity entity) { return false; } - @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } - @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } - @Override public void setFireTicks(int ticks) { } - @Override public boolean setPassenger(Entity entity) { return false; } - @Override public boolean eject() { return false; } - @Override public boolean leaveVehicle() { return false; } - @Override public void playEffect(EntityEffect effect) { } - @Override public void setCustomName(String name) { } - @Override public void setCustomNameVisible(boolean flag) { } - @Override public void setGlowing(boolean flag) { } - @Override public void setGravity(boolean gravity) { } - @Override public void setInvulnerable(boolean flag) { } - @Override public void setMomentum(Vector value) { } - @Override public void setSilent(boolean flag) { } - @Override public void setTicksLived(int value) { } - @Override public void setPersistent(boolean flag) { } - - // Methods from Item - @Override public void setItemStack(ItemStack stack) { } - @Override public void setPickupDelay(int delay) { } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_13_R2; + +import org.bukkit.EntityEffect; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_13_R2.CraftServer; +import org.bukkit.craftbukkit.v1_13_R2.entity.CraftItem; +import org.bukkit.entity.Entity; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import org.bukkit.inventory.ItemStack; +import org.bukkit.util.Vector; + +public class CraftNMSItem extends CraftItem { + + public CraftNMSItem(CraftServer server, EntityNMSItem entity) { + super(server, entity); + } + + // Disallow all the bukkit methods. + + @Override + public void remove() { + // Cannot be removed, this is the most important to override. + } + + // Methods from Entity + @Override public void setVelocity(Vector vel) { } + @Override public boolean teleport(Location loc) { return false; } + @Override public boolean teleport(Entity entity) { return false; } + @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } + @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } + @Override public void setFireTicks(int ticks) { } + @Override public boolean setPassenger(Entity entity) { return false; } + @Override public boolean eject() { return false; } + @Override public boolean leaveVehicle() { return false; } + @Override public void playEffect(EntityEffect effect) { } + @Override public void setCustomName(String name) { } + @Override public void setCustomNameVisible(boolean flag) { } + @Override public void setGlowing(boolean flag) { } + @Override public void setGravity(boolean gravity) { } + @Override public void setInvulnerable(boolean flag) { } + @Override public void setMomentum(Vector value) { } + @Override public void setSilent(boolean flag) { } + @Override public void setTicksLived(int value) { } + @Override public void setPersistent(boolean flag) { } + + // Methods from Item + @Override public void setItemStack(ItemStack stack) { } + @Override public void setPickupDelay(int delay) { } + +} diff --git a/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/CraftNMSSlime.java b/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/CraftNMSSlime.java index 72be5e37..ca4505ee 100644 --- a/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/CraftNMSSlime.java +++ b/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/CraftNMSSlime.java @@ -1,70 +1,84 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_13_R2; - -import java.util.Collection; - -import org.bukkit.EntityEffect; -import org.bukkit.Location; -import org.bukkit.craftbukkit.v1_13_R2.CraftServer; -import org.bukkit.craftbukkit.v1_13_R2.entity.CraftSlime; -import org.bukkit.entity.Entity; -import org.bukkit.entity.LivingEntity; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -import org.bukkit.loot.LootTable; -import org.bukkit.potion.PotionEffect; -import org.bukkit.util.Vector; - -public class CraftNMSSlime extends CraftSlime { - - public CraftNMSSlime(CraftServer server, EntityNMSSlime entity) { - super(server, entity); - } - - // Disallow all the bukkit methods. - - @Override - public void remove() { - // Cannot be removed, this is the most important to override. - } - - // Methods from LivingEntity class - @Override public boolean addPotionEffect(PotionEffect effect) { return false; } - @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } - @Override public boolean addPotionEffects(Collection effects) { return false; } - @Override public void setRemoveWhenFarAway(boolean remove) { } - @Override public void setAI(boolean ai) { } - @Override public void setCanPickupItems(boolean pickup) { } - @Override public void setCollidable(boolean collidable) { } - @Override public void setGliding(boolean gliding) { } - @Override public boolean setLeashHolder(Entity holder) { return false; } - @Override public void setSwimming(boolean swimming) { } - - // Methods from Entity - @Override public void setVelocity(Vector vel) { } - @Override public boolean teleport(Location loc) { return false; } - @Override public boolean teleport(Entity entity) { return false; } - @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } - @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } - @Override public void setFireTicks(int ticks) { } - @Override public boolean setPassenger(Entity entity) { return false; } - @Override public boolean eject() { return false; } - @Override public boolean leaveVehicle() { return false; } - @Override public void playEffect(EntityEffect effect) { } - @Override public void setCustomName(String name) { } - @Override public void setCustomNameVisible(boolean flag) { } - @Override public void setGlowing(boolean flag) { } - @Override public void setGravity(boolean gravity) { } - @Override public void setInvulnerable(boolean flag) { } - @Override public void setMomentum(Vector value) { } - @Override public void setSilent(boolean flag) { } - @Override public void setTicksLived(int value) { } - @Override public void setPersistent(boolean flag) { } - - // Methods from Mob - @Override public void setLootTable(LootTable table) { } - @Override public void setSeed(long seed) { } - - // Methods from Slime - @Override public void setSize(int size) { } - @Override public void setTarget(LivingEntity target) { } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_13_R2; + +import java.util.Collection; + +import org.bukkit.EntityEffect; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_13_R2.CraftServer; +import org.bukkit.craftbukkit.v1_13_R2.entity.CraftSlime; +import org.bukkit.entity.Entity; +import org.bukkit.entity.LivingEntity; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import org.bukkit.loot.LootTable; +import org.bukkit.potion.PotionEffect; +import org.bukkit.util.Vector; + +public class CraftNMSSlime extends CraftSlime { + + public CraftNMSSlime(CraftServer server, EntityNMSSlime entity) { + super(server, entity); + } + + // Disallow all the bukkit methods. + + @Override + public void remove() { + // Cannot be removed, this is the most important to override. + } + + // Methods from LivingEntity class + @Override public boolean addPotionEffect(PotionEffect effect) { return false; } + @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } + @Override public boolean addPotionEffects(Collection effects) { return false; } + @Override public void setRemoveWhenFarAway(boolean remove) { } + @Override public void setAI(boolean ai) { } + @Override public void setCanPickupItems(boolean pickup) { } + @Override public void setCollidable(boolean collidable) { } + @Override public void setGliding(boolean gliding) { } + @Override public boolean setLeashHolder(Entity holder) { return false; } + @Override public void setSwimming(boolean swimming) { } + + // Methods from Entity + @Override public void setVelocity(Vector vel) { } + @Override public boolean teleport(Location loc) { return false; } + @Override public boolean teleport(Entity entity) { return false; } + @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } + @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } + @Override public void setFireTicks(int ticks) { } + @Override public boolean setPassenger(Entity entity) { return false; } + @Override public boolean eject() { return false; } + @Override public boolean leaveVehicle() { return false; } + @Override public void playEffect(EntityEffect effect) { } + @Override public void setCustomName(String name) { } + @Override public void setCustomNameVisible(boolean flag) { } + @Override public void setGlowing(boolean flag) { } + @Override public void setGravity(boolean gravity) { } + @Override public void setInvulnerable(boolean flag) { } + @Override public void setMomentum(Vector value) { } + @Override public void setSilent(boolean flag) { } + @Override public void setTicksLived(int value) { } + @Override public void setPersistent(boolean flag) { } + + // Methods from Mob + @Override public void setLootTable(LootTable table) { } + @Override public void setSeed(long seed) { } + + // Methods from Slime + @Override public void setSize(int size) { } + @Override public void setTarget(LivingEntity target) { } + +} diff --git a/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/EntityNMSArmorStand.java b/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/EntityNMSArmorStand.java index b85de6d2..0a83f665 100644 --- a/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/EntityNMSArmorStand.java +++ b/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/EntityNMSArmorStand.java @@ -1,242 +1,256 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_13_R2; - -import org.bukkit.craftbukkit.v1_13_R2.entity.CraftEntity; -import org.bukkit.craftbukkit.v1_13_R2.util.CraftChatMessage; - -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; -import com.gmail.filoghost.holographicdisplays.disk.Configuration; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; -import com.gmail.filoghost.holographicdisplays.util.Utils; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectionUtils; - -import net.minecraft.server.v1_13_R2.AxisAlignedBB; -import net.minecraft.server.v1_13_R2.DamageSource; -import net.minecraft.server.v1_13_R2.EntityArmorStand; -import net.minecraft.server.v1_13_R2.EntityHuman; -import net.minecraft.server.v1_13_R2.EntityPlayer; -import net.minecraft.server.v1_13_R2.EnumHand; -import net.minecraft.server.v1_13_R2.EnumInteractionResult; -import net.minecraft.server.v1_13_R2.EnumItemSlot; -import net.minecraft.server.v1_13_R2.IChatBaseComponent; -import net.minecraft.server.v1_13_R2.ItemStack; -import net.minecraft.server.v1_13_R2.NBTTagCompound; -import net.minecraft.server.v1_13_R2.PacketPlayOutEntityTeleport; -import net.minecraft.server.v1_13_R2.SoundEffect; -import net.minecraft.server.v1_13_R2.Vec3D; -import net.minecraft.server.v1_13_R2.World; - -public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorStand { - - private boolean lockTick; - private HologramLine parentPiece; - - public EntityNMSArmorStand(World world, HologramLine parentPiece) { - super(world); - super.setInvisible(true); - super.setSmall(true); - super.setArms(false); - super.setNoGravity(true); - super.setBasePlate(true); - super.setMarker(true); - super.collides = false; - this.parentPiece = parentPiece; - forceSetBoundingBox(new NullBoundingBox()); - } - - - @Override - public void b(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public boolean c(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public boolean d(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public NBTTagCompound save(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return nbttagcompound; - } - - @Override - public void f(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public void a(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public boolean isInvulnerable(DamageSource source) { - /* - * The field Entity.invulnerable is private. - * It's only used while saving NBTTags, but since the entity would be killed - * on chunk unload, we prefer to override isInvulnerable(). - */ - return true; - } - - @Override - public boolean isCollidable() { - return false; - } - - @Override - public void setCustomName(IChatBaseComponent ichatbasecomponent) { - // Locks the custom name. - } - - @Override - public void inactiveTick() { - // Check inactive ticks. - - if (!lockTick) { - super.inactiveTick(); - } - } - - @Override - public void setCustomNameVisible(boolean visible) { - // Locks the custom name. - } - - @Override - public EnumInteractionResult a(EntityHuman human, Vec3D vec3d, EnumHand enumhand) { - // Prevent stand being equipped - return EnumInteractionResult.PASS; - } - - @Override - public boolean c(int i, ItemStack item) { - // Prevent stand being equipped - return false; - } - - @Override - public void setSlot(EnumItemSlot enumitemslot, ItemStack itemstack) { - // Prevent stand being equipped - } - - @Override - public void a(AxisAlignedBB boundingBox) { - // Do not change it! - } - - public void forceSetBoundingBox(AxisAlignedBB boundingBox) { - super.a(boundingBox); - } - - @Override - public int getId() { - if (Configuration.preciseHologramMovement) { - StackTraceElement element = ReflectionUtils.getStackTraceElement(2); - if (element != null && element.getFileName() != null && element.getFileName().equals("EntityTrackerEntry.java") && 158 < element.getLineNumber() && element.getLineNumber() < 168) { - // Then this method is being called when creating a new movement packet, we return a fake ID! - return -1; - } - } - - return super.getId(); - } - - @Override - public void tick() { - if (!lockTick) { - super.tick(); - } - } - - @Override - public void a(SoundEffect soundeffect, float f, float f1) { - // Remove sounds. - } - - @Override - public void setCustomNameNMS(String name) { - if (name != null && name.length() > 300) { - name = name.substring(0, 300); - } - super.setCustomName(CraftChatMessage.fromStringOrNull(name)); - super.setCustomNameVisible(name != null && !name.isEmpty()); - } - - @Override - public String getCustomNameNMS() { - return CraftChatMessage.fromComponent(super.getCustomName()); - } - - @Override - public void setLockTick(boolean lock) { - lockTick = lock; - } - - @Override - public void die() { - // Prevent being killed. - } - - @Override - public CraftEntity getBukkitEntity() { - if (super.bukkitEntity == null) { - super.bukkitEntity = new CraftNMSArmorStand(super.world.getServer(), this); - } - return super.bukkitEntity; - } - - @Override - public void killEntityNMS() { - super.dead = true; - } - - @Override - public void setLocationNMS(double x, double y, double z) { - super.setPosition(x, y, z); - - if (Configuration.preciseHologramMovement) { - // Send a packet near to update the position. - PacketPlayOutEntityTeleport teleportPacket = new PacketPlayOutEntityTeleport(this); - - for (Object obj : super.world.players) { - if (obj instanceof EntityPlayer) { - EntityPlayer nmsPlayer = (EntityPlayer) obj; - - double distanceSquared = Utils.square(nmsPlayer.locX - super.locX) + Utils.square(nmsPlayer.locZ - super.locZ); - if (distanceSquared < 8192 && nmsPlayer.playerConnection != null) { - nmsPlayer.playerConnection.sendPacket(teleportPacket); - } - } - } - } - } - - @Override - public boolean isDeadNMS() { - return super.dead; - } - - @Override - public int getIdNMS() { - return super.getId(); // Return the real ID without checking the stack trace. - } - - @Override - public HologramLine getHologramLine() { - return parentPiece; - } - - @Override - public org.bukkit.entity.Entity getBukkitEntityNMS() { - return getBukkitEntity(); - } -} \ No newline at end of file +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_13_R2; + +import org.bukkit.craftbukkit.v1_13_R2.entity.CraftEntity; +import org.bukkit.craftbukkit.v1_13_R2.util.CraftChatMessage; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; +import com.gmail.filoghost.holographicdisplays.disk.Configuration; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; +import com.gmail.filoghost.holographicdisplays.util.Utils; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectionUtils; + +import net.minecraft.server.v1_13_R2.AxisAlignedBB; +import net.minecraft.server.v1_13_R2.DamageSource; +import net.minecraft.server.v1_13_R2.EntityArmorStand; +import net.minecraft.server.v1_13_R2.EntityHuman; +import net.minecraft.server.v1_13_R2.EntityPlayer; +import net.minecraft.server.v1_13_R2.EnumHand; +import net.minecraft.server.v1_13_R2.EnumInteractionResult; +import net.minecraft.server.v1_13_R2.EnumItemSlot; +import net.minecraft.server.v1_13_R2.IChatBaseComponent; +import net.minecraft.server.v1_13_R2.ItemStack; +import net.minecraft.server.v1_13_R2.NBTTagCompound; +import net.minecraft.server.v1_13_R2.PacketPlayOutEntityTeleport; +import net.minecraft.server.v1_13_R2.SoundEffect; +import net.minecraft.server.v1_13_R2.Vec3D; +import net.minecraft.server.v1_13_R2.World; + +public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorStand { + + private boolean lockTick; + private HologramLine parentPiece; + + public EntityNMSArmorStand(World world, HologramLine parentPiece) { + super(world); + super.setInvisible(true); + super.setSmall(true); + super.setArms(false); + super.setNoGravity(true); + super.setBasePlate(true); + super.setMarker(true); + super.collides = false; + this.parentPiece = parentPiece; + forceSetBoundingBox(new NullBoundingBox()); + } + + + @Override + public void b(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public boolean c(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public boolean d(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public NBTTagCompound save(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return nbttagcompound; + } + + @Override + public void f(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public void a(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public boolean isInvulnerable(DamageSource source) { + /* + * The field Entity.invulnerable is private. + * It's only used while saving NBTTags, but since the entity would be killed + * on chunk unload, we prefer to override isInvulnerable(). + */ + return true; + } + + @Override + public boolean isCollidable() { + return false; + } + + @Override + public void setCustomName(IChatBaseComponent ichatbasecomponent) { + // Locks the custom name. + } + + @Override + public void inactiveTick() { + // Check inactive ticks. + + if (!lockTick) { + super.inactiveTick(); + } + } + + @Override + public void setCustomNameVisible(boolean visible) { + // Locks the custom name. + } + + @Override + public EnumInteractionResult a(EntityHuman human, Vec3D vec3d, EnumHand enumhand) { + // Prevent stand being equipped + return EnumInteractionResult.PASS; + } + + @Override + public boolean c(int i, ItemStack item) { + // Prevent stand being equipped + return false; + } + + @Override + public void setSlot(EnumItemSlot enumitemslot, ItemStack itemstack) { + // Prevent stand being equipped + } + + @Override + public void a(AxisAlignedBB boundingBox) { + // Do not change it! + } + + public void forceSetBoundingBox(AxisAlignedBB boundingBox) { + super.a(boundingBox); + } + + @Override + public int getId() { + if (Configuration.preciseHologramMovement) { + StackTraceElement element = ReflectionUtils.getStackTraceElement(2); + if (element != null && element.getFileName() != null && element.getFileName().equals("EntityTrackerEntry.java") && 158 < element.getLineNumber() && element.getLineNumber() < 168) { + // Then this method is being called when creating a new movement packet, we return a fake ID! + return -1; + } + } + + return super.getId(); + } + + @Override + public void tick() { + if (!lockTick) { + super.tick(); + } + } + + @Override + public void a(SoundEffect soundeffect, float f, float f1) { + // Remove sounds. + } + + @Override + public void setCustomNameNMS(String name) { + if (name != null && name.length() > 300) { + name = name.substring(0, 300); + } + super.setCustomName(CraftChatMessage.fromStringOrNull(name)); + super.setCustomNameVisible(name != null && !name.isEmpty()); + } + + @Override + public String getCustomNameNMS() { + return CraftChatMessage.fromComponent(super.getCustomName()); + } + + @Override + public void setLockTick(boolean lock) { + lockTick = lock; + } + + @Override + public void die() { + // Prevent being killed. + } + + @Override + public CraftEntity getBukkitEntity() { + if (super.bukkitEntity == null) { + super.bukkitEntity = new CraftNMSArmorStand(super.world.getServer(), this); + } + return super.bukkitEntity; + } + + @Override + public void killEntityNMS() { + super.dead = true; + } + + @Override + public void setLocationNMS(double x, double y, double z) { + super.setPosition(x, y, z); + + if (Configuration.preciseHologramMovement) { + // Send a packet near to update the position. + PacketPlayOutEntityTeleport teleportPacket = new PacketPlayOutEntityTeleport(this); + + for (Object obj : super.world.players) { + if (obj instanceof EntityPlayer) { + EntityPlayer nmsPlayer = (EntityPlayer) obj; + + double distanceSquared = Utils.square(nmsPlayer.locX - super.locX) + Utils.square(nmsPlayer.locZ - super.locZ); + if (distanceSquared < 8192 && nmsPlayer.playerConnection != null) { + nmsPlayer.playerConnection.sendPacket(teleportPacket); + } + } + } + } + } + + @Override + public boolean isDeadNMS() { + return super.dead; + } + + @Override + public int getIdNMS() { + return super.getId(); // Return the real ID without checking the stack trace. + } + + @Override + public HologramLine getHologramLine() { + return parentPiece; + } + + @Override + public org.bukkit.entity.Entity getBukkitEntityNMS() { + return getBukkitEntity(); + } +} diff --git a/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/EntityNMSItem.java b/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/EntityNMSItem.java index bf6a738c..1336b614 100644 --- a/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/EntityNMSItem.java +++ b/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/EntityNMSItem.java @@ -1,247 +1,261 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_13_R2; - -import org.bukkit.craftbukkit.v1_13_R2.entity.CraftEntity; -import org.bukkit.craftbukkit.v1_13_R2.inventory.CraftItemStack; -import org.bukkit.entity.Player; - -import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.ItemUtils; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; - -import net.minecraft.server.v1_13_R2.AxisAlignedBB; -import net.minecraft.server.v1_13_R2.Blocks; -import net.minecraft.server.v1_13_R2.DamageSource; -import net.minecraft.server.v1_13_R2.Entity; -import net.minecraft.server.v1_13_R2.EntityHuman; -import net.minecraft.server.v1_13_R2.EntityItem; -import net.minecraft.server.v1_13_R2.EntityPlayer; -import net.minecraft.server.v1_13_R2.ItemStack; -import net.minecraft.server.v1_13_R2.NBTTagCompound; -import net.minecraft.server.v1_13_R2.NBTTagList; -import net.minecraft.server.v1_13_R2.NBTTagString; -import net.minecraft.server.v1_13_R2.World; - -public class EntityNMSItem extends EntityItem implements NMSItem { - - private static final ReflectField VEHICLE_FIELD = new ReflectField(Entity.class, "vehicle"); - - private boolean lockTick; - private ItemLine parentPiece; - private ItemPickupManager itemPickupManager; - - public EntityNMSItem(World world, ItemLine piece, ItemPickupManager itemPickupManager) { - super(world); - super.pickupDelay = 32767; // Lock the item pickup delay, also prevents entities from picking up the item - this.parentPiece = piece; - this.itemPickupManager = itemPickupManager; - forceSetBoundingBox(new NullBoundingBox()); - } - - @Override - public void tick() { - - // So it won't get removed. - ticksLived = 0; - - if (!lockTick) { - super.tick(); - } - } - - // Method called when a player is near. - @Override - public void d(EntityHuman human) { - - if (human.locY < super.locY - 1.5 || human.locY > super.locY + 1.0) { - // Too low or too high, it's a bit weird. - return; - } - - if (parentPiece.getPickupHandler() != null && human instanceof EntityPlayer) { - itemPickupManager.handleItemLinePickup((Player) human.getBukkitEntity(), parentPiece.getPickupHandler(), parentPiece.getParent()); - // It is never added to the inventory. - } - } - - @Override - public void b(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public boolean c(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public boolean d(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public NBTTagCompound save(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return nbttagcompound; - } - - @Override - public void f(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public void a(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public boolean isInvulnerable(DamageSource source) { - /* - * The field Entity.invulnerable is private. - * It's only used while saving NBTTags, but since the entity would be killed - * on chunk unload, we prefer to override isInvulnerable(). - */ - return true; - } - - @Override - public boolean isCollidable() { - return false; - } - - @Override - public void a(AxisAlignedBB boundingBox) { - // Do not change it! - } - - public void forceSetBoundingBox(AxisAlignedBB boundingBox) { - super.a(boundingBox); - } - - @Override - public void inactiveTick() { - // Check inactive ticks. - - if (!lockTick) { - super.inactiveTick(); - } - } - - @Override - public void setLockTick(boolean lock) { - lockTick = lock; - } - - @Override - public void die() { - // Prevent being killed. - } - - @Override - public CraftEntity getBukkitEntity() { - if (super.bukkitEntity == null) { - super.bukkitEntity = new CraftNMSItem(super.world.getServer(), this); - } - return super.bukkitEntity; - } - - @Override - public boolean isDeadNMS() { - return super.dead; - } - - @Override - public void killEntityNMS() { - super.dead = true; - } - - @Override - public void setLocationNMS(double x, double y, double z) { - super.setPosition(x, y, z); - } - - @Override - public void setItemStackNMS(org.bukkit.inventory.ItemStack stack) { - ItemStack newItem = CraftItemStack.asNMSCopy(stack); // ItemStack.a is returned if the stack is null, invalid or the material is not an Item - - if (newItem == null || newItem == ItemStack.a) { - newItem = new ItemStack(Blocks.BEDROCK); - } - - if (newItem.getTag() == null) { - newItem.setTag(new NBTTagCompound()); - } - NBTTagCompound display = newItem.getTag().getCompound("display"); // Returns a new NBTTagCompound if not existing - if (!newItem.getTag().hasKey("display")) { - newItem.getTag().set("display", display); - } - - NBTTagList tagList = new NBTTagList(); - tagList.add(new NBTTagString(ItemUtils.ANTISTACK_LORE)); // Antistack lore - display.set("Lore", tagList); - - newItem.setCount(1); - - setItemStack(newItem); - } - - @Override - public int getIdNMS() { - return super.getId(); - } - - @Override - public ItemLine getHologramLine() { - return parentPiece; - } - - @Override - public void allowPickup(boolean pickup) { - if (pickup) { - super.pickupDelay = 0; - } else { - super.pickupDelay = 32767; - } - } - - @Override - public org.bukkit.entity.Entity getBukkitEntityNMS() { - return getBukkitEntity(); - } - - @Override - public void setPassengerOfNMS(NMSEntityBase vehicleBase) { - if (vehicleBase == null || !(vehicleBase instanceof Entity)) { - // It should never dismount - return; - } - - Entity entity = (Entity) vehicleBase; - - try { - if (super.getVehicle() != null) { - Entity oldVehicle = super.getVehicle(); - VEHICLE_FIELD.set(this, null); - oldVehicle.passengers.remove(this); - } - - VEHICLE_FIELD.set(this, entity); - entity.passengers.clear(); - entity.passengers.add(this); - - } catch (Exception ex) { - ConsoleLogger.logDebugException(ex); - } - } - - @Override - public Object getRawItemStack() { - return super.getItemStack(); - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_13_R2; + +import org.bukkit.craftbukkit.v1_13_R2.entity.CraftEntity; +import org.bukkit.craftbukkit.v1_13_R2.inventory.CraftItemStack; +import org.bukkit.entity.Player; + +import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.ItemUtils; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; + +import net.minecraft.server.v1_13_R2.AxisAlignedBB; +import net.minecraft.server.v1_13_R2.Blocks; +import net.minecraft.server.v1_13_R2.DamageSource; +import net.minecraft.server.v1_13_R2.Entity; +import net.minecraft.server.v1_13_R2.EntityHuman; +import net.minecraft.server.v1_13_R2.EntityItem; +import net.minecraft.server.v1_13_R2.EntityPlayer; +import net.minecraft.server.v1_13_R2.ItemStack; +import net.minecraft.server.v1_13_R2.NBTTagCompound; +import net.minecraft.server.v1_13_R2.NBTTagList; +import net.minecraft.server.v1_13_R2.NBTTagString; +import net.minecraft.server.v1_13_R2.World; + +public class EntityNMSItem extends EntityItem implements NMSItem { + + private static final ReflectField VEHICLE_FIELD = new ReflectField(Entity.class, "vehicle"); + + private boolean lockTick; + private ItemLine parentPiece; + private ItemPickupManager itemPickupManager; + + public EntityNMSItem(World world, ItemLine piece, ItemPickupManager itemPickupManager) { + super(world); + super.pickupDelay = 32767; // Lock the item pickup delay, also prevents entities from picking up the item + this.parentPiece = piece; + this.itemPickupManager = itemPickupManager; + forceSetBoundingBox(new NullBoundingBox()); + } + + @Override + public void tick() { + + // So it won't get removed. + ticksLived = 0; + + if (!lockTick) { + super.tick(); + } + } + + // Method called when a player is near. + @Override + public void d(EntityHuman human) { + + if (human.locY < super.locY - 1.5 || human.locY > super.locY + 1.0) { + // Too low or too high, it's a bit weird. + return; + } + + if (parentPiece.getPickupHandler() != null && human instanceof EntityPlayer) { + itemPickupManager.handleItemLinePickup((Player) human.getBukkitEntity(), parentPiece.getPickupHandler(), parentPiece.getParent()); + // It is never added to the inventory. + } + } + + @Override + public void b(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public boolean c(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public boolean d(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public NBTTagCompound save(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return nbttagcompound; + } + + @Override + public void f(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public void a(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public boolean isInvulnerable(DamageSource source) { + /* + * The field Entity.invulnerable is private. + * It's only used while saving NBTTags, but since the entity would be killed + * on chunk unload, we prefer to override isInvulnerable(). + */ + return true; + } + + @Override + public boolean isCollidable() { + return false; + } + + @Override + public void a(AxisAlignedBB boundingBox) { + // Do not change it! + } + + public void forceSetBoundingBox(AxisAlignedBB boundingBox) { + super.a(boundingBox); + } + + @Override + public void inactiveTick() { + // Check inactive ticks. + + if (!lockTick) { + super.inactiveTick(); + } + } + + @Override + public void setLockTick(boolean lock) { + lockTick = lock; + } + + @Override + public void die() { + // Prevent being killed. + } + + @Override + public CraftEntity getBukkitEntity() { + if (super.bukkitEntity == null) { + super.bukkitEntity = new CraftNMSItem(super.world.getServer(), this); + } + return super.bukkitEntity; + } + + @Override + public boolean isDeadNMS() { + return super.dead; + } + + @Override + public void killEntityNMS() { + super.dead = true; + } + + @Override + public void setLocationNMS(double x, double y, double z) { + super.setPosition(x, y, z); + } + + @Override + public void setItemStackNMS(org.bukkit.inventory.ItemStack stack) { + ItemStack newItem = CraftItemStack.asNMSCopy(stack); // ItemStack.a is returned if the stack is null, invalid or the material is not an Item + + if (newItem == null || newItem == ItemStack.a) { + newItem = new ItemStack(Blocks.BEDROCK); + } + + if (newItem.getTag() == null) { + newItem.setTag(new NBTTagCompound()); + } + NBTTagCompound display = newItem.getTag().getCompound("display"); // Returns a new NBTTagCompound if not existing + if (!newItem.getTag().hasKey("display")) { + newItem.getTag().set("display", display); + } + + NBTTagList tagList = new NBTTagList(); + tagList.add(new NBTTagString(ItemUtils.ANTISTACK_LORE)); // Antistack lore + display.set("Lore", tagList); + + newItem.setCount(1); + + setItemStack(newItem); + } + + @Override + public int getIdNMS() { + return super.getId(); + } + + @Override + public ItemLine getHologramLine() { + return parentPiece; + } + + @Override + public void allowPickup(boolean pickup) { + if (pickup) { + super.pickupDelay = 0; + } else { + super.pickupDelay = 32767; + } + } + + @Override + public org.bukkit.entity.Entity getBukkitEntityNMS() { + return getBukkitEntity(); + } + + @Override + public void setPassengerOfNMS(NMSEntityBase vehicleBase) { + if (vehicleBase == null || !(vehicleBase instanceof Entity)) { + // It should never dismount + return; + } + + Entity entity = (Entity) vehicleBase; + + try { + if (super.getVehicle() != null) { + Entity oldVehicle = super.getVehicle(); + VEHICLE_FIELD.set(this, null); + oldVehicle.passengers.remove(this); + } + + VEHICLE_FIELD.set(this, entity); + entity.passengers.clear(); + entity.passengers.add(this); + + } catch (Exception ex) { + ConsoleLogger.logDebugException(ex); + } + } + + @Override + public Object getRawItemStack() { + return super.getItemStack(); + } +} diff --git a/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/EntityNMSSlime.java b/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/EntityNMSSlime.java index d48ffd7c..d907adf7 100644 --- a/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/EntityNMSSlime.java +++ b/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/EntityNMSSlime.java @@ -1,217 +1,231 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_13_R2; - -import org.bukkit.Bukkit; -import org.bukkit.craftbukkit.v1_13_R2.entity.CraftEntity; -import org.bukkit.event.player.PlayerInteractEntityEvent; - -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSSlime; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; - -import net.minecraft.server.v1_13_R2.AxisAlignedBB; -import net.minecraft.server.v1_13_R2.DamageSource; -import net.minecraft.server.v1_13_R2.Entity; -import net.minecraft.server.v1_13_R2.EntityDamageSource; -import net.minecraft.server.v1_13_R2.EntityPlayer; -import net.minecraft.server.v1_13_R2.EntitySlime; -import net.minecraft.server.v1_13_R2.IChatBaseComponent; -import net.minecraft.server.v1_13_R2.NBTTagCompound; -import net.minecraft.server.v1_13_R2.SoundEffect; -import net.minecraft.server.v1_13_R2.World; - -public class EntityNMSSlime extends EntitySlime implements NMSSlime { - - private static final ReflectField VEHICLE_FIELD = new ReflectField(Entity.class, "vehicle"); - - private boolean lockTick; - private HologramLine parentPiece; - - public EntityNMSSlime(World world, HologramLine parentPiece) { - super(world); - super.persistent = true; - super.collides = false; - a(0.0F, 0.0F); - setSize(1, false); - setInvisible(true); - this.parentPiece = parentPiece; - forceSetBoundingBox(new NullBoundingBox()); - } - - @Override - public void a(AxisAlignedBB boundingBox) { - // Do not change it! - } - - public void forceSetBoundingBox(AxisAlignedBB boundingBox) { - super.a(boundingBox); - } - - @Override - public void tick() { - - // So it won't get removed. - ticksLived = 0; - - if (!lockTick) { - super.tick(); - } - } - - @Override - public void b(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public boolean c(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public boolean d(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public NBTTagCompound save(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return nbttagcompound; - } - - @Override - public void f(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public void a(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public boolean damageEntity(DamageSource damageSource, float amount) { - if (damageSource instanceof EntityDamageSource) { - EntityDamageSource entityDamageSource = (EntityDamageSource) damageSource; - if (entityDamageSource.getEntity() instanceof EntityPlayer) { - Bukkit.getPluginManager().callEvent(new PlayerInteractEntityEvent(((EntityPlayer) entityDamageSource.getEntity()).getBukkitEntity(), getBukkitEntity())); // Bukkit takes care of the exceptions - } - } - return false; - } - - @Override - public boolean isInvulnerable(DamageSource source) { - /* - * The field Entity.invulnerable is private. - * It's only used while saving NBTTags, but since the entity would be killed - * on chunk unload, we prefer to override isInvulnerable(). - */ - return true; - } - - @Override - public boolean isCollidable() { - return false; - } - - @Override - public void inactiveTick() { - // Check inactive ticks. - - if (!lockTick) { - super.inactiveTick(); - } - } - - @Override - public void setCustomName(IChatBaseComponent ichatbasecomponent) { - // Locks the custom name. - } - - @Override - public void setCustomNameVisible(boolean visible) { - // Locks the custom name. - } - - @Override - public void a(SoundEffect soundeffect, float f, float f1) { - // Remove sounds. - } - - @Override - public void setLockTick(boolean lock) { - lockTick = lock; - } - - @Override - public void die() { - // Prevent being killed. - } - - @Override - public CraftEntity getBukkitEntity() { - if (super.bukkitEntity == null) { - super.bukkitEntity = new CraftNMSSlime(super.world.getServer(), this); - } - return super.bukkitEntity; - } - - @Override - public boolean isDeadNMS() { - return super.dead; - } - - @Override - public void killEntityNMS() { - super.dead = true; - } - - @Override - public void setLocationNMS(double x, double y, double z) { - super.setPosition(x, y, z); - } - - @Override - public int getIdNMS() { - return super.getId(); - } - - @Override - public HologramLine getHologramLine() { - return parentPiece; - } - - @Override - public org.bukkit.entity.Entity getBukkitEntityNMS() { - return getBukkitEntity(); - } - - @Override - public void setPassengerOfNMS(NMSEntityBase vehicleBase) { - if (vehicleBase == null || !(vehicleBase instanceof Entity)) { - // It should never dismount - return; - } - - Entity entity = (Entity) vehicleBase; - - try { - if (super.getVehicle() != null) { - Entity oldVehicle = super.getVehicle(); - VEHICLE_FIELD.set(this, null); - oldVehicle.passengers.remove(this); - } - - VEHICLE_FIELD.set(this, entity); - entity.passengers.clear(); - entity.passengers.add(this); - - } catch (Exception ex) { - ConsoleLogger.logDebugException(ex); - } - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_13_R2; + +import org.bukkit.Bukkit; +import org.bukkit.craftbukkit.v1_13_R2.entity.CraftEntity; +import org.bukkit.event.player.PlayerInteractEntityEvent; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSSlime; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; + +import net.minecraft.server.v1_13_R2.AxisAlignedBB; +import net.minecraft.server.v1_13_R2.DamageSource; +import net.minecraft.server.v1_13_R2.Entity; +import net.minecraft.server.v1_13_R2.EntityDamageSource; +import net.minecraft.server.v1_13_R2.EntityPlayer; +import net.minecraft.server.v1_13_R2.EntitySlime; +import net.minecraft.server.v1_13_R2.IChatBaseComponent; +import net.minecraft.server.v1_13_R2.NBTTagCompound; +import net.minecraft.server.v1_13_R2.SoundEffect; +import net.minecraft.server.v1_13_R2.World; + +public class EntityNMSSlime extends EntitySlime implements NMSSlime { + + private static final ReflectField VEHICLE_FIELD = new ReflectField(Entity.class, "vehicle"); + + private boolean lockTick; + private HologramLine parentPiece; + + public EntityNMSSlime(World world, HologramLine parentPiece) { + super(world); + super.persistent = true; + super.collides = false; + a(0.0F, 0.0F); + setSize(1, false); + setInvisible(true); + this.parentPiece = parentPiece; + forceSetBoundingBox(new NullBoundingBox()); + } + + @Override + public void a(AxisAlignedBB boundingBox) { + // Do not change it! + } + + public void forceSetBoundingBox(AxisAlignedBB boundingBox) { + super.a(boundingBox); + } + + @Override + public void tick() { + + // So it won't get removed. + ticksLived = 0; + + if (!lockTick) { + super.tick(); + } + } + + @Override + public void b(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public boolean c(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public boolean d(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public NBTTagCompound save(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return nbttagcompound; + } + + @Override + public void f(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public void a(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public boolean damageEntity(DamageSource damageSource, float amount) { + if (damageSource instanceof EntityDamageSource) { + EntityDamageSource entityDamageSource = (EntityDamageSource) damageSource; + if (entityDamageSource.getEntity() instanceof EntityPlayer) { + Bukkit.getPluginManager().callEvent(new PlayerInteractEntityEvent(((EntityPlayer) entityDamageSource.getEntity()).getBukkitEntity(), getBukkitEntity())); // Bukkit takes care of the exceptions + } + } + return false; + } + + @Override + public boolean isInvulnerable(DamageSource source) { + /* + * The field Entity.invulnerable is private. + * It's only used while saving NBTTags, but since the entity would be killed + * on chunk unload, we prefer to override isInvulnerable(). + */ + return true; + } + + @Override + public boolean isCollidable() { + return false; + } + + @Override + public void inactiveTick() { + // Check inactive ticks. + + if (!lockTick) { + super.inactiveTick(); + } + } + + @Override + public void setCustomName(IChatBaseComponent ichatbasecomponent) { + // Locks the custom name. + } + + @Override + public void setCustomNameVisible(boolean visible) { + // Locks the custom name. + } + + @Override + public void a(SoundEffect soundeffect, float f, float f1) { + // Remove sounds. + } + + @Override + public void setLockTick(boolean lock) { + lockTick = lock; + } + + @Override + public void die() { + // Prevent being killed. + } + + @Override + public CraftEntity getBukkitEntity() { + if (super.bukkitEntity == null) { + super.bukkitEntity = new CraftNMSSlime(super.world.getServer(), this); + } + return super.bukkitEntity; + } + + @Override + public boolean isDeadNMS() { + return super.dead; + } + + @Override + public void killEntityNMS() { + super.dead = true; + } + + @Override + public void setLocationNMS(double x, double y, double z) { + super.setPosition(x, y, z); + } + + @Override + public int getIdNMS() { + return super.getId(); + } + + @Override + public HologramLine getHologramLine() { + return parentPiece; + } + + @Override + public org.bukkit.entity.Entity getBukkitEntityNMS() { + return getBukkitEntity(); + } + + @Override + public void setPassengerOfNMS(NMSEntityBase vehicleBase) { + if (vehicleBase == null || !(vehicleBase instanceof Entity)) { + // It should never dismount + return; + } + + Entity entity = (Entity) vehicleBase; + + try { + if (super.getVehicle() != null) { + Entity oldVehicle = super.getVehicle(); + VEHICLE_FIELD.set(this, null); + oldVehicle.passengers.remove(this); + } + + VEHICLE_FIELD.set(this, entity); + entity.passengers.clear(); + entity.passengers.add(this); + + } catch (Exception ex) { + ConsoleLogger.logDebugException(ex); + } + } +} diff --git a/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/NmsManagerImpl.java b/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/NmsManagerImpl.java index 0ed46c3b..c88b2c1b 100644 --- a/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/NmsManagerImpl.java +++ b/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/NmsManagerImpl.java @@ -1,155 +1,169 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_13_R2; - -import java.lang.reflect.Method; -import java.util.List; -import java.util.function.Function; - -import org.bukkit.Bukkit; -import org.bukkit.craftbukkit.v1_13_R2.CraftWorld; -import org.bukkit.craftbukkit.v1_13_R2.entity.CraftEntity; -import org.bukkit.inventory.ItemStack; - -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; -import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.Validator; -import com.gmail.filoghost.holographicdisplays.util.VersionUtils; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; - -import net.minecraft.server.v1_13_R2.Entity; -import net.minecraft.server.v1_13_R2.EntityTypes; -import net.minecraft.server.v1_13_R2.IRegistry; -import net.minecraft.server.v1_13_R2.MathHelper; -import net.minecraft.server.v1_13_R2.RegistryID; -import net.minecraft.server.v1_13_R2.RegistryMaterials; -import net.minecraft.server.v1_13_R2.World; -import net.minecraft.server.v1_13_R2.WorldServer; - -public class NmsManagerImpl implements NMSManager { - - private static final ReflectField>> REGISTRY_ID_FIELD = new ReflectField>>(RegistryMaterials.class, "b"); - private static final ReflectField ID_TO_CLASS_MAP_FIELD = new ReflectField(RegistryID.class, "d"); - private static final ReflectField> ENTITY_LIST_FIELD = new ReflectField>(World.class, "entityList"); - - private Method validateEntityMethod; - - @Override - public void setup() throws Exception { - validateEntityMethod = World.class.getDeclaredMethod("b", Entity.class); - validateEntityMethod.setAccessible(true); - - registerCustomEntity(EntityNMSSlime.class, 55); - } - - public void registerCustomEntity(Class entityClass, int id) throws Exception { - // Use reflection to get the RegistryID of entities. - RegistryID> registryID = REGISTRY_ID_FIELD.get(IRegistry.ENTITY_TYPE); - Object[] idToClassMap = ID_TO_CLASS_MAP_FIELD.get(registryID); - - // Save the the ID -> EntityTypes mapping before the registration. - Object oldValue = idToClassMap[id]; - - // Register the EntityTypes object. - registryID.a(new EntityTypes(entityClass, new Function() { - - @Override - public Entity apply(World world) { - return null; - } - - }, true, true, null), id); - - // Restore the ID -> EntityTypes mapping. - idToClassMap[id] = oldValue; - } - - @Override - public NMSItem spawnNMSItem(org.bukkit.World bukkitWorld, double x, double y, double z, ItemLine parentPiece, ItemStack stack, ItemPickupManager itemPickupManager) { - WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); - EntityNMSItem customItem = new EntityNMSItem(nmsWorld, parentPiece, itemPickupManager); - customItem.setLocationNMS(x, y, z); - customItem.setItemStackNMS(stack); - if (!addEntityToWorld(nmsWorld, customItem)) { - ConsoleLogger.handleSpawnFail(parentPiece); - } - return customItem; - } - - @Override - public EntityNMSSlime spawnNMSSlime(org.bukkit.World bukkitWorld, double x, double y, double z, HologramLine parentPiece) { - WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); - EntityNMSSlime touchSlime = new EntityNMSSlime(nmsWorld, parentPiece); - touchSlime.setLocationNMS(x, y, z); - if (!addEntityToWorld(nmsWorld, touchSlime)) { - ConsoleLogger.handleSpawnFail(parentPiece); - } - return touchSlime; - } - - @Override - public NMSArmorStand spawnNMSArmorStand(org.bukkit.World world, double x, double y, double z, HologramLine parentPiece) { - WorldServer nmsWorld = ((CraftWorld) world).getHandle(); - EntityNMSArmorStand invisibleArmorStand = new EntityNMSArmorStand(nmsWorld, parentPiece); - invisibleArmorStand.setLocationNMS(x, y, z); - if (!addEntityToWorld(nmsWorld, invisibleArmorStand)) { - ConsoleLogger.handleSpawnFail(parentPiece); - } - return invisibleArmorStand; - } - - private boolean addEntityToWorld(WorldServer nmsWorld, Entity nmsEntity) { - Validator.isTrue(Bukkit.isPrimaryThread(), "Async entity add"); - - final int chunkX = MathHelper.floor(nmsEntity.locX / 16.0); - final int chunkZ = MathHelper.floor(nmsEntity.locZ / 16.0); - - if (!nmsWorld.getChunkProviderServer().isLoaded(chunkX, chunkZ)) { - // This should never happen - nmsEntity.dead = true; - return false; - } - - nmsWorld.getChunkAt(chunkX, chunkZ).a(nmsEntity); - if (VersionUtils.isPaperServer()) { - try { - // Workaround because nmsWorld.entityList is a different class in Paper, if used without reflection it throws NoSuchFieldError. - ENTITY_LIST_FIELD.get(nmsWorld).add(nmsEntity); - } catch (Exception e) { - e.printStackTrace(); - return false; - } - } else { - nmsWorld.entityList.add(nmsEntity); - } - - try { - validateEntityMethod.invoke(nmsWorld, nmsEntity); - } catch (Exception e) { - e.printStackTrace(); - return false; - } - return true; - } - - @Override - public boolean isNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { - return ((CraftEntity) bukkitEntity).getHandle() instanceof NMSEntityBase; - } - - @Override - public NMSEntityBase getNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { - - Entity nmsEntity = ((CraftEntity) bukkitEntity).getHandle(); - if (nmsEntity instanceof NMSEntityBase) { - return ((NMSEntityBase) nmsEntity); - } - - return null; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_13_R2; + +import java.lang.reflect.Method; +import java.util.List; +import java.util.function.Function; + +import org.bukkit.Bukkit; +import org.bukkit.craftbukkit.v1_13_R2.CraftWorld; +import org.bukkit.craftbukkit.v1_13_R2.entity.CraftEntity; +import org.bukkit.inventory.ItemStack; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; +import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.Validator; +import com.gmail.filoghost.holographicdisplays.util.VersionUtils; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; + +import net.minecraft.server.v1_13_R2.Entity; +import net.minecraft.server.v1_13_R2.EntityTypes; +import net.minecraft.server.v1_13_R2.IRegistry; +import net.minecraft.server.v1_13_R2.MathHelper; +import net.minecraft.server.v1_13_R2.RegistryID; +import net.minecraft.server.v1_13_R2.RegistryMaterials; +import net.minecraft.server.v1_13_R2.World; +import net.minecraft.server.v1_13_R2.WorldServer; + +public class NmsManagerImpl implements NMSManager { + + private static final ReflectField>> REGISTRY_ID_FIELD = new ReflectField>>(RegistryMaterials.class, "b"); + private static final ReflectField ID_TO_CLASS_MAP_FIELD = new ReflectField(RegistryID.class, "d"); + private static final ReflectField> ENTITY_LIST_FIELD = new ReflectField>(World.class, "entityList"); + + private Method validateEntityMethod; + + @Override + public void setup() throws Exception { + validateEntityMethod = World.class.getDeclaredMethod("b", Entity.class); + validateEntityMethod.setAccessible(true); + + registerCustomEntity(EntityNMSSlime.class, 55); + } + + public void registerCustomEntity(Class entityClass, int id) throws Exception { + // Use reflection to get the RegistryID of entities. + RegistryID> registryID = REGISTRY_ID_FIELD.get(IRegistry.ENTITY_TYPE); + Object[] idToClassMap = ID_TO_CLASS_MAP_FIELD.get(registryID); + + // Save the the ID -> EntityTypes mapping before the registration. + Object oldValue = idToClassMap[id]; + + // Register the EntityTypes object. + registryID.a(new EntityTypes(entityClass, new Function() { + + @Override + public Entity apply(World world) { + return null; + } + + }, true, true, null), id); + + // Restore the ID -> EntityTypes mapping. + idToClassMap[id] = oldValue; + } + + @Override + public NMSItem spawnNMSItem(org.bukkit.World bukkitWorld, double x, double y, double z, ItemLine parentPiece, ItemStack stack, ItemPickupManager itemPickupManager) { + WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); + EntityNMSItem customItem = new EntityNMSItem(nmsWorld, parentPiece, itemPickupManager); + customItem.setLocationNMS(x, y, z); + customItem.setItemStackNMS(stack); + if (!addEntityToWorld(nmsWorld, customItem)) { + ConsoleLogger.handleSpawnFail(parentPiece); + } + return customItem; + } + + @Override + public EntityNMSSlime spawnNMSSlime(org.bukkit.World bukkitWorld, double x, double y, double z, HologramLine parentPiece) { + WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); + EntityNMSSlime touchSlime = new EntityNMSSlime(nmsWorld, parentPiece); + touchSlime.setLocationNMS(x, y, z); + if (!addEntityToWorld(nmsWorld, touchSlime)) { + ConsoleLogger.handleSpawnFail(parentPiece); + } + return touchSlime; + } + + @Override + public NMSArmorStand spawnNMSArmorStand(org.bukkit.World world, double x, double y, double z, HologramLine parentPiece) { + WorldServer nmsWorld = ((CraftWorld) world).getHandle(); + EntityNMSArmorStand invisibleArmorStand = new EntityNMSArmorStand(nmsWorld, parentPiece); + invisibleArmorStand.setLocationNMS(x, y, z); + if (!addEntityToWorld(nmsWorld, invisibleArmorStand)) { + ConsoleLogger.handleSpawnFail(parentPiece); + } + return invisibleArmorStand; + } + + private boolean addEntityToWorld(WorldServer nmsWorld, Entity nmsEntity) { + Validator.isTrue(Bukkit.isPrimaryThread(), "Async entity add"); + + final int chunkX = MathHelper.floor(nmsEntity.locX / 16.0); + final int chunkZ = MathHelper.floor(nmsEntity.locZ / 16.0); + + if (!nmsWorld.getChunkProviderServer().isLoaded(chunkX, chunkZ)) { + // This should never happen + nmsEntity.dead = true; + return false; + } + + nmsWorld.getChunkAt(chunkX, chunkZ).a(nmsEntity); + if (VersionUtils.isPaperServer()) { + try { + // Workaround because nmsWorld.entityList is a different class in Paper, if used without reflection it throws NoSuchFieldError. + ENTITY_LIST_FIELD.get(nmsWorld).add(nmsEntity); + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } else { + nmsWorld.entityList.add(nmsEntity); + } + + try { + validateEntityMethod.invoke(nmsWorld, nmsEntity); + } catch (Exception e) { + e.printStackTrace(); + return false; + } + return true; + } + + @Override + public boolean isNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { + return ((CraftEntity) bukkitEntity).getHandle() instanceof NMSEntityBase; + } + + @Override + public NMSEntityBase getNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { + + Entity nmsEntity = ((CraftEntity) bukkitEntity).getHandle(); + if (nmsEntity instanceof NMSEntityBase) { + return ((NMSEntityBase) nmsEntity); + } + + return null; + } + +} diff --git a/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/NullBoundingBox.java b/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/NullBoundingBox.java index 625bf508..5c5c8003 100644 --- a/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/NullBoundingBox.java +++ b/NMS/v1_13_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_13_R2/NullBoundingBox.java @@ -1,118 +1,132 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_13_R2; - -import net.minecraft.server.v1_13_R2.AxisAlignedBB; -import net.minecraft.server.v1_13_R2.BlockPosition; -import net.minecraft.server.v1_13_R2.EnumDirection.EnumAxis; -import net.minecraft.server.v1_13_R2.MovingObjectPosition; -import net.minecraft.server.v1_13_R2.Vec3D; - -public class NullBoundingBox extends AxisAlignedBB { - - public NullBoundingBox() { - super(0, 0, 0, 0, 0, 0); - } - - @Override - public double a() { - return 0.0; - } - - @Override - public AxisAlignedBB a(AxisAlignedBB arg0) { - return this; - } - - @Override - public AxisAlignedBB a(double arg0, double arg1, double arg2) { - return this; - } - - @Override - public MovingObjectPosition b(Vec3D arg0, Vec3D arg1) { - return null; - } - - @Override - public AxisAlignedBB grow(double arg0, double arg1, double arg2) { - return this; - } - - @Override - public AxisAlignedBB shrink(double arg0) { - return this; - } - - @Override - public AxisAlignedBB a(BlockPosition arg0) { - return this; - } - - @Override - public boolean a(double arg0, double arg1, double arg2, double arg3, double arg4, double arg5) { - return false; - } - - @Override - public boolean b(Vec3D arg0) { - return false; - } - - @Override - public AxisAlignedBB g(double arg0) { - return this; - } - - @Override - public AxisAlignedBB a(Vec3D arg0) { - return this; - } - - @Override - public AxisAlignedBB b(AxisAlignedBB arg0) { - return this; - } - - @Override - public AxisAlignedBB b(double arg0, double arg1, double arg2) { - return this; - } - - @Override - public boolean c(AxisAlignedBB arg0) { - return false; - } - - @Override - public AxisAlignedBB d(double arg0, double arg1, double arg2) { - return this; - } - - @Override - public double a(EnumAxis arg0) { - return 0.0; - } - - @Override - public MovingObjectPosition a(Vec3D arg0, Vec3D arg1, BlockPosition arg2) { - return null; - } - - @Override - public double b(EnumAxis arg0) { - return 0.0; - } - - @Override - public boolean e(double arg0, double arg1, double arg2) { - return false; - } - - @Override - public AxisAlignedBB f(double arg0, double arg1, double arg2) { - return this; - } - - - - -} \ No newline at end of file +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_13_R2; + +import net.minecraft.server.v1_13_R2.AxisAlignedBB; +import net.minecraft.server.v1_13_R2.BlockPosition; +import net.minecraft.server.v1_13_R2.EnumDirection.EnumAxis; +import net.minecraft.server.v1_13_R2.MovingObjectPosition; +import net.minecraft.server.v1_13_R2.Vec3D; + +public class NullBoundingBox extends AxisAlignedBB { + + public NullBoundingBox() { + super(0, 0, 0, 0, 0, 0); + } + + @Override + public double a() { + return 0.0; + } + + @Override + public AxisAlignedBB a(AxisAlignedBB arg0) { + return this; + } + + @Override + public AxisAlignedBB a(double arg0, double arg1, double arg2) { + return this; + } + + @Override + public MovingObjectPosition b(Vec3D arg0, Vec3D arg1) { + return null; + } + + @Override + public AxisAlignedBB grow(double arg0, double arg1, double arg2) { + return this; + } + + @Override + public AxisAlignedBB shrink(double arg0) { + return this; + } + + @Override + public AxisAlignedBB a(BlockPosition arg0) { + return this; + } + + @Override + public boolean a(double arg0, double arg1, double arg2, double arg3, double arg4, double arg5) { + return false; + } + + @Override + public boolean b(Vec3D arg0) { + return false; + } + + @Override + public AxisAlignedBB g(double arg0) { + return this; + } + + @Override + public AxisAlignedBB a(Vec3D arg0) { + return this; + } + + @Override + public AxisAlignedBB b(AxisAlignedBB arg0) { + return this; + } + + @Override + public AxisAlignedBB b(double arg0, double arg1, double arg2) { + return this; + } + + @Override + public boolean c(AxisAlignedBB arg0) { + return false; + } + + @Override + public AxisAlignedBB d(double arg0, double arg1, double arg2) { + return this; + } + + @Override + public double a(EnumAxis arg0) { + return 0.0; + } + + @Override + public MovingObjectPosition a(Vec3D arg0, Vec3D arg1, BlockPosition arg2) { + return null; + } + + @Override + public double b(EnumAxis arg0) { + return 0.0; + } + + @Override + public boolean e(double arg0, double arg1, double arg2) { + return false; + } + + @Override + public AxisAlignedBB f(double arg0, double arg1, double arg2) { + return this; + } + + + + +} diff --git a/NMS/v1_8_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R1/CraftNMSArmorStand.java b/NMS/v1_8_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R1/CraftNMSArmorStand.java index 7ab1c5f5..fb655c5e 100644 --- a/NMS/v1_8_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R1/CraftNMSArmorStand.java +++ b/NMS/v1_8_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R1/CraftNMSArmorStand.java @@ -1,67 +1,81 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_8_R1; - -import java.util.Collection; - -import org.bukkit.EntityEffect; -import org.bukkit.Location; -import org.bukkit.craftbukkit.v1_8_R1.CraftServer; -import org.bukkit.craftbukkit.v1_8_R1.entity.CraftArmorStand; -import org.bukkit.entity.Entity; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -import org.bukkit.inventory.ItemStack; -import org.bukkit.potion.PotionEffect; -import org.bukkit.util.EulerAngle; -import org.bukkit.util.Vector; - -public class CraftNMSArmorStand extends CraftArmorStand { - - public CraftNMSArmorStand(CraftServer server, EntityNMSArmorStand entity) { - super(server, entity); - } - - // Disallow all the bukkit methods. - - @Override - public void remove() { - // Cannot be removed, this is the most important to override. - } - - // Methods from Armor stand class - @Override public void setArms(boolean arms) { } - @Override public void setBasePlate(boolean basePlate) { } - @Override public void setBodyPose(EulerAngle pose) { } - @Override public void setBoots(ItemStack item) { } - @Override public void setChestplate(ItemStack item) { } - @Override public void setGravity(boolean gravity) { } - @Override public void setHeadPose(EulerAngle pose) { } - @Override public void setHelmet(ItemStack item) { } - @Override public void setItemInHand(ItemStack item) { } - @Override public void setLeftArmPose(EulerAngle pose) { } - @Override public void setLeftLegPose(EulerAngle pose) { } - @Override public void setLeggings(ItemStack item) { } - @Override public void setRightArmPose(EulerAngle pose) { } - @Override public void setRightLegPose(EulerAngle pose) { } - @Override public void setSmall(boolean small) { } - @Override public void setVisible(boolean visible) { } - - // Methods from LivingEntity class - @Override public boolean addPotionEffect(PotionEffect effect) { return false; } - @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } - @Override public boolean addPotionEffects(Collection effects) { return false; } - @Override public void setRemoveWhenFarAway(boolean remove) { } - - // Methods from Entity - @Override public void setVelocity(Vector vel) { } - @Override public boolean teleport(Location loc) { return false; } - @Override public boolean teleport(Entity entity) { return false; } - @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } - @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } - @Override public void setFireTicks(int ticks) { } - @Override public boolean setPassenger(Entity entity) { return false; } - @Override public boolean eject() { return false; } - @Override public boolean leaveVehicle() { return false; } - @Override public void playEffect(EntityEffect effect) { } - @Override public void setCustomName(String name) { } - @Override public void setCustomNameVisible(boolean flag) { } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_8_R1; + +import java.util.Collection; + +import org.bukkit.EntityEffect; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_8_R1.CraftServer; +import org.bukkit.craftbukkit.v1_8_R1.entity.CraftArmorStand; +import org.bukkit.entity.Entity; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import org.bukkit.inventory.ItemStack; +import org.bukkit.potion.PotionEffect; +import org.bukkit.util.EulerAngle; +import org.bukkit.util.Vector; + +public class CraftNMSArmorStand extends CraftArmorStand { + + public CraftNMSArmorStand(CraftServer server, EntityNMSArmorStand entity) { + super(server, entity); + } + + // Disallow all the bukkit methods. + + @Override + public void remove() { + // Cannot be removed, this is the most important to override. + } + + // Methods from Armor stand class + @Override public void setArms(boolean arms) { } + @Override public void setBasePlate(boolean basePlate) { } + @Override public void setBodyPose(EulerAngle pose) { } + @Override public void setBoots(ItemStack item) { } + @Override public void setChestplate(ItemStack item) { } + @Override public void setGravity(boolean gravity) { } + @Override public void setHeadPose(EulerAngle pose) { } + @Override public void setHelmet(ItemStack item) { } + @Override public void setItemInHand(ItemStack item) { } + @Override public void setLeftArmPose(EulerAngle pose) { } + @Override public void setLeftLegPose(EulerAngle pose) { } + @Override public void setLeggings(ItemStack item) { } + @Override public void setRightArmPose(EulerAngle pose) { } + @Override public void setRightLegPose(EulerAngle pose) { } + @Override public void setSmall(boolean small) { } + @Override public void setVisible(boolean visible) { } + + // Methods from LivingEntity class + @Override public boolean addPotionEffect(PotionEffect effect) { return false; } + @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } + @Override public boolean addPotionEffects(Collection effects) { return false; } + @Override public void setRemoveWhenFarAway(boolean remove) { } + + // Methods from Entity + @Override public void setVelocity(Vector vel) { } + @Override public boolean teleport(Location loc) { return false; } + @Override public boolean teleport(Entity entity) { return false; } + @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } + @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } + @Override public void setFireTicks(int ticks) { } + @Override public boolean setPassenger(Entity entity) { return false; } + @Override public boolean eject() { return false; } + @Override public boolean leaveVehicle() { return false; } + @Override public void playEffect(EntityEffect effect) { } + @Override public void setCustomName(String name) { } + @Override public void setCustomNameVisible(boolean flag) { } + +} diff --git a/NMS/v1_8_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R1/CraftNMSItem.java b/NMS/v1_8_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R1/CraftNMSItem.java index 7d078330..cdfec619 100644 --- a/NMS/v1_8_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R1/CraftNMSItem.java +++ b/NMS/v1_8_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R1/CraftNMSItem.java @@ -1,42 +1,56 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_8_R1; - -import org.bukkit.EntityEffect; -import org.bukkit.Location; -import org.bukkit.craftbukkit.v1_8_R1.CraftServer; -import org.bukkit.craftbukkit.v1_8_R1.entity.CraftItem; -import org.bukkit.entity.Entity; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -import org.bukkit.inventory.ItemStack; -import org.bukkit.util.Vector; - -public class CraftNMSItem extends CraftItem { - - public CraftNMSItem(CraftServer server, EntityNMSItem entity) { - super(server, entity); - } - - // Disallow all the bukkit methods. - - @Override - public void remove() { - // Cannot be removed, this is the most important to override. - } - - // Methods from Entity - @Override public void setVelocity(Vector vel) { } - @Override public boolean teleport(Location loc) { return false; } - @Override public boolean teleport(Entity entity) { return false; } - @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } - @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } - @Override public void setFireTicks(int ticks) { } - @Override public boolean setPassenger(Entity entity) { return false; } - @Override public boolean eject() { return false; } - @Override public boolean leaveVehicle() { return false; } - @Override public void playEffect(EntityEffect effect) { } - @Override public void setCustomName(String name) { } - @Override public void setCustomNameVisible(boolean flag) { } - - // Methods from Item - @Override public void setItemStack(ItemStack stack) { } - @Override public void setPickupDelay(int delay) { } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_8_R1; + +import org.bukkit.EntityEffect; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_8_R1.CraftServer; +import org.bukkit.craftbukkit.v1_8_R1.entity.CraftItem; +import org.bukkit.entity.Entity; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import org.bukkit.inventory.ItemStack; +import org.bukkit.util.Vector; + +public class CraftNMSItem extends CraftItem { + + public CraftNMSItem(CraftServer server, EntityNMSItem entity) { + super(server, entity); + } + + // Disallow all the bukkit methods. + + @Override + public void remove() { + // Cannot be removed, this is the most important to override. + } + + // Methods from Entity + @Override public void setVelocity(Vector vel) { } + @Override public boolean teleport(Location loc) { return false; } + @Override public boolean teleport(Entity entity) { return false; } + @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } + @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } + @Override public void setFireTicks(int ticks) { } + @Override public boolean setPassenger(Entity entity) { return false; } + @Override public boolean eject() { return false; } + @Override public boolean leaveVehicle() { return false; } + @Override public void playEffect(EntityEffect effect) { } + @Override public void setCustomName(String name) { } + @Override public void setCustomNameVisible(boolean flag) { } + + // Methods from Item + @Override public void setItemStack(ItemStack stack) { } + @Override public void setPickupDelay(int delay) { } +} diff --git a/NMS/v1_8_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R1/CraftNMSSlime.java b/NMS/v1_8_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R1/CraftNMSSlime.java index 189e669d..c74b00b6 100644 --- a/NMS/v1_8_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R1/CraftNMSSlime.java +++ b/NMS/v1_8_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R1/CraftNMSSlime.java @@ -1,49 +1,63 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_8_R1; - -import java.util.Collection; - -import org.bukkit.EntityEffect; -import org.bukkit.Location; -import org.bukkit.craftbukkit.v1_8_R1.CraftServer; -import org.bukkit.craftbukkit.v1_8_R1.entity.CraftSlime; -import org.bukkit.entity.Entity; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -import org.bukkit.potion.PotionEffect; -import org.bukkit.util.Vector; - -public class CraftNMSSlime extends CraftSlime { - - public CraftNMSSlime(CraftServer server, EntityNMSSlime entity) { - super(server, entity); - } - - // Disallow all the bukkit methods. - - @Override - public void remove() { - // Cannot be removed, this is the most important to override. - } - - // Methods from LivingEntity class - @Override public boolean addPotionEffect(PotionEffect effect) { return false; } - @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } - @Override public boolean addPotionEffects(Collection effects) { return false; } - @Override public void setRemoveWhenFarAway(boolean remove) { } - - // Methods from Entity - @Override public void setVelocity(Vector vel) { } - @Override public boolean teleport(Location loc) { return false; } - @Override public boolean teleport(Entity entity) { return false; } - @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } - @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } - @Override public void setFireTicks(int ticks) { } - @Override public boolean setPassenger(Entity entity) { return false; } - @Override public boolean eject() { return false; } - @Override public boolean leaveVehicle() { return false; } - @Override public void playEffect(EntityEffect effect) { } - @Override public void setCustomName(String name) { } - @Override public void setCustomNameVisible(boolean flag) { } - - // Methods from Slime - @Override public void setSize(int size) { } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_8_R1; + +import java.util.Collection; + +import org.bukkit.EntityEffect; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_8_R1.CraftServer; +import org.bukkit.craftbukkit.v1_8_R1.entity.CraftSlime; +import org.bukkit.entity.Entity; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import org.bukkit.potion.PotionEffect; +import org.bukkit.util.Vector; + +public class CraftNMSSlime extends CraftSlime { + + public CraftNMSSlime(CraftServer server, EntityNMSSlime entity) { + super(server, entity); + } + + // Disallow all the bukkit methods. + + @Override + public void remove() { + // Cannot be removed, this is the most important to override. + } + + // Methods from LivingEntity class + @Override public boolean addPotionEffect(PotionEffect effect) { return false; } + @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } + @Override public boolean addPotionEffects(Collection effects) { return false; } + @Override public void setRemoveWhenFarAway(boolean remove) { } + + // Methods from Entity + @Override public void setVelocity(Vector vel) { } + @Override public boolean teleport(Location loc) { return false; } + @Override public boolean teleport(Entity entity) { return false; } + @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } + @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } + @Override public void setFireTicks(int ticks) { } + @Override public boolean setPassenger(Entity entity) { return false; } + @Override public boolean eject() { return false; } + @Override public boolean leaveVehicle() { return false; } + @Override public void playEffect(EntityEffect effect) { } + @Override public void setCustomName(String name) { } + @Override public void setCustomNameVisible(boolean flag) { } + + // Methods from Slime + @Override public void setSize(int size) { } +} diff --git a/NMS/v1_8_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R1/EntityNMSArmorStand.java b/NMS/v1_8_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R1/EntityNMSArmorStand.java index 8c062f85..bee85142 100644 --- a/NMS/v1_8_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R1/EntityNMSArmorStand.java +++ b/NMS/v1_8_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R1/EntityNMSArmorStand.java @@ -1,229 +1,243 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_8_R1; - -import org.bukkit.craftbukkit.v1_8_R1.entity.CraftEntity; - -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; -import com.gmail.filoghost.holographicdisplays.disk.Configuration; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; -import com.gmail.filoghost.holographicdisplays.util.Utils; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectionUtils; - -import net.minecraft.server.v1_8_R1.AxisAlignedBB; -import net.minecraft.server.v1_8_R1.DamageSource; -import net.minecraft.server.v1_8_R1.EntityArmorStand; -import net.minecraft.server.v1_8_R1.EntityHuman; -import net.minecraft.server.v1_8_R1.EntityPlayer; -import net.minecraft.server.v1_8_R1.ItemStack; -import net.minecraft.server.v1_8_R1.MathHelper; -import net.minecraft.server.v1_8_R1.NBTTagCompound; -import net.minecraft.server.v1_8_R1.PacketPlayOutEntityTeleport; -import net.minecraft.server.v1_8_R1.Vec3D; -import net.minecraft.server.v1_8_R1.World; - -public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorStand { - - private static final ReflectField DISABLED_SLOTS_FIELD = new ReflectField(EntityArmorStand.class, "bg"); - - private boolean lockTick; - private HologramLine parentPiece; - - public EntityNMSArmorStand(World world, HologramLine parentPiece) { - super(world); - setInvisible(true); - setSmall(true); - setArms(false); - setGravity(true); - setBasePlate(true); - // There is no "Marker" tag in v1_8_R1 - this.parentPiece = parentPiece; - try { - DISABLED_SLOTS_FIELD.set(this, Integer.MAX_VALUE); - } catch (Exception e) { - // There's still the overridden method. - } - forceSetBoundingBox(new NullBoundingBox()); - } - - - @Override - public void b(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public boolean c(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public boolean d(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public void e(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - - @Override - public boolean isInvulnerable(DamageSource source) { - /* - * The field Entity.invulnerable is private. - * It's only used while saving NBTTags, but since the entity would be killed - * on chunk unload, we prefer to override isInvulnerable(). - */ - return true; - } - - @Override - public void setCustomName(String customName) { - // Locks the custom name. - } - - @Override - public void setCustomNameVisible(boolean visible) { - // Locks the custom name. - } - - @Override - public boolean a(EntityHuman human, Vec3D vec3d) { - // Prevent stand being equipped - return true; - } - - @Override - public boolean d(int i, ItemStack item) { - // Prevent stand being equipped - return false; - } - - @Override - public void setEquipment(int i, ItemStack item) { - // Prevent stand being equipped - } - - @Override - public void a(AxisAlignedBB boundingBox) { - // Do not change it! - } - - public void forceSetBoundingBox(AxisAlignedBB boundingBox) { - super.a(boundingBox); - } - - @Override - public int getId() { - if (Configuration.preciseHologramMovement) { - StackTraceElement element = ReflectionUtils.getStackTraceElement(2); - if (element != null && element.getFileName() != null && element.getFileName().equals("EntityTrackerEntry.java") && element.getLineNumber() > 137 && element.getLineNumber() < 147) { - // Then this method is being called when creating a new packet, we return a fake ID! - return -1; - } - } - - return super.getId(); - } - - @Override - public void s_() { - if (!lockTick) { - super.s_(); - } - } - - @Override - public void makeSound(String sound, float f1, float f2) { - // Remove sounds. - } - - @Override - public void setCustomNameNMS(String name) { - if (name != null && name.length() > 300) { - name = name.substring(0, 300); - } - super.setCustomName(name); - super.setCustomNameVisible(name != null && !name.isEmpty()); - } - - @Override - public String getCustomNameNMS() { - return super.getCustomName(); - } - - @Override - public void setLockTick(boolean lock) { - lockTick = lock; - } - - @Override - public void die() { - setLockTick(false); - super.die(); - } - - @Override - public CraftEntity getBukkitEntity() { - if (super.bukkitEntity == null) { - this.bukkitEntity = new CraftNMSArmorStand(this.world.getServer(), this); - } - return this.bukkitEntity; - } - - @Override - public void killEntityNMS() { - die(); - } - - @Override - public void setLocationNMS(double x, double y, double z) { - super.setPosition(x, y, z); - - if (Configuration.preciseHologramMovement) { - // Send a packet near to update the position. - PacketPlayOutEntityTeleport teleportPacket = new PacketPlayOutEntityTeleport( - getIdNMS(), - MathHelper.floor(this.locX * 32.0D), - MathHelper.floor(this.locY * 32.0D), - MathHelper.floor(this.locZ * 32.0D), - (byte) (int) (this.yaw * 256.0F / 360.0F), - (byte) (int) (this.pitch * 256.0F / 360.0F), - this.onGround - ); - - for (Object obj : this.world.players) { - if (obj instanceof EntityPlayer) { - EntityPlayer nmsPlayer = (EntityPlayer) obj; - - double distanceSquared = Utils.square(nmsPlayer.locX - this.locX) + Utils.square(nmsPlayer.locZ - this.locZ); - if (distanceSquared < 8192 && nmsPlayer.playerConnection != null) { - nmsPlayer.playerConnection.sendPacket(teleportPacket); - } - } - } - } - } - - @Override - public boolean isDeadNMS() { - return this.dead; - } - - @Override - public int getIdNMS() { - return super.getId(); // Return the real ID without checking the stack trace. - } - - @Override - public HologramLine getHologramLine() { - return parentPiece; - } - - @Override - public org.bukkit.entity.Entity getBukkitEntityNMS() { - return getBukkitEntity(); - } -} \ No newline at end of file +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_8_R1; + +import org.bukkit.craftbukkit.v1_8_R1.entity.CraftEntity; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; +import com.gmail.filoghost.holographicdisplays.disk.Configuration; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; +import com.gmail.filoghost.holographicdisplays.util.Utils; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectionUtils; + +import net.minecraft.server.v1_8_R1.AxisAlignedBB; +import net.minecraft.server.v1_8_R1.DamageSource; +import net.minecraft.server.v1_8_R1.EntityArmorStand; +import net.minecraft.server.v1_8_R1.EntityHuman; +import net.minecraft.server.v1_8_R1.EntityPlayer; +import net.minecraft.server.v1_8_R1.ItemStack; +import net.minecraft.server.v1_8_R1.MathHelper; +import net.minecraft.server.v1_8_R1.NBTTagCompound; +import net.minecraft.server.v1_8_R1.PacketPlayOutEntityTeleport; +import net.minecraft.server.v1_8_R1.Vec3D; +import net.minecraft.server.v1_8_R1.World; + +public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorStand { + + private static final ReflectField DISABLED_SLOTS_FIELD = new ReflectField(EntityArmorStand.class, "bg"); + + private boolean lockTick; + private HologramLine parentPiece; + + public EntityNMSArmorStand(World world, HologramLine parentPiece) { + super(world); + setInvisible(true); + setSmall(true); + setArms(false); + setGravity(true); + setBasePlate(true); + // There is no "Marker" tag in v1_8_R1 + this.parentPiece = parentPiece; + try { + DISABLED_SLOTS_FIELD.set(this, Integer.MAX_VALUE); + } catch (Exception e) { + // There's still the overridden method. + } + forceSetBoundingBox(new NullBoundingBox()); + } + + + @Override + public void b(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public boolean c(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public boolean d(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public void e(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + + @Override + public boolean isInvulnerable(DamageSource source) { + /* + * The field Entity.invulnerable is private. + * It's only used while saving NBTTags, but since the entity would be killed + * on chunk unload, we prefer to override isInvulnerable(). + */ + return true; + } + + @Override + public void setCustomName(String customName) { + // Locks the custom name. + } + + @Override + public void setCustomNameVisible(boolean visible) { + // Locks the custom name. + } + + @Override + public boolean a(EntityHuman human, Vec3D vec3d) { + // Prevent stand being equipped + return true; + } + + @Override + public boolean d(int i, ItemStack item) { + // Prevent stand being equipped + return false; + } + + @Override + public void setEquipment(int i, ItemStack item) { + // Prevent stand being equipped + } + + @Override + public void a(AxisAlignedBB boundingBox) { + // Do not change it! + } + + public void forceSetBoundingBox(AxisAlignedBB boundingBox) { + super.a(boundingBox); + } + + @Override + public int getId() { + if (Configuration.preciseHologramMovement) { + StackTraceElement element = ReflectionUtils.getStackTraceElement(2); + if (element != null && element.getFileName() != null && element.getFileName().equals("EntityTrackerEntry.java") && element.getLineNumber() > 137 && element.getLineNumber() < 147) { + // Then this method is being called when creating a new packet, we return a fake ID! + return -1; + } + } + + return super.getId(); + } + + @Override + public void s_() { + if (!lockTick) { + super.s_(); + } + } + + @Override + public void makeSound(String sound, float f1, float f2) { + // Remove sounds. + } + + @Override + public void setCustomNameNMS(String name) { + if (name != null && name.length() > 300) { + name = name.substring(0, 300); + } + super.setCustomName(name); + super.setCustomNameVisible(name != null && !name.isEmpty()); + } + + @Override + public String getCustomNameNMS() { + return super.getCustomName(); + } + + @Override + public void setLockTick(boolean lock) { + lockTick = lock; + } + + @Override + public void die() { + setLockTick(false); + super.die(); + } + + @Override + public CraftEntity getBukkitEntity() { + if (super.bukkitEntity == null) { + this.bukkitEntity = new CraftNMSArmorStand(this.world.getServer(), this); + } + return this.bukkitEntity; + } + + @Override + public void killEntityNMS() { + die(); + } + + @Override + public void setLocationNMS(double x, double y, double z) { + super.setPosition(x, y, z); + + if (Configuration.preciseHologramMovement) { + // Send a packet near to update the position. + PacketPlayOutEntityTeleport teleportPacket = new PacketPlayOutEntityTeleport( + getIdNMS(), + MathHelper.floor(this.locX * 32.0D), + MathHelper.floor(this.locY * 32.0D), + MathHelper.floor(this.locZ * 32.0D), + (byte) (int) (this.yaw * 256.0F / 360.0F), + (byte) (int) (this.pitch * 256.0F / 360.0F), + this.onGround + ); + + for (Object obj : this.world.players) { + if (obj instanceof EntityPlayer) { + EntityPlayer nmsPlayer = (EntityPlayer) obj; + + double distanceSquared = Utils.square(nmsPlayer.locX - this.locX) + Utils.square(nmsPlayer.locZ - this.locZ); + if (distanceSquared < 8192 && nmsPlayer.playerConnection != null) { + nmsPlayer.playerConnection.sendPacket(teleportPacket); + } + } + } + } + } + + @Override + public boolean isDeadNMS() { + return this.dead; + } + + @Override + public int getIdNMS() { + return super.getId(); // Return the real ID without checking the stack trace. + } + + @Override + public HologramLine getHologramLine() { + return parentPiece; + } + + @Override + public org.bukkit.entity.Entity getBukkitEntityNMS() { + return getBukkitEntity(); + } +} diff --git a/NMS/v1_8_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R1/EntityNMSItem.java b/NMS/v1_8_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R1/EntityNMSItem.java index 30b9c237..e7f553f6 100644 --- a/NMS/v1_8_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R1/EntityNMSItem.java +++ b/NMS/v1_8_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R1/EntityNMSItem.java @@ -1,232 +1,246 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_8_R1; - -import org.bukkit.craftbukkit.v1_8_R1.entity.CraftEntity; -import org.bukkit.craftbukkit.v1_8_R1.inventory.CraftItemStack; -import org.bukkit.entity.Player; - -import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.ItemUtils; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; - -import net.minecraft.server.v1_8_R1.AxisAlignedBB; -import net.minecraft.server.v1_8_R1.Blocks; -import net.minecraft.server.v1_8_R1.DamageSource; -import net.minecraft.server.v1_8_R1.Entity; -import net.minecraft.server.v1_8_R1.EntityHuman; -import net.minecraft.server.v1_8_R1.EntityItem; -import net.minecraft.server.v1_8_R1.EntityPlayer; -import net.minecraft.server.v1_8_R1.ItemStack; -import net.minecraft.server.v1_8_R1.NBTTagCompound; -import net.minecraft.server.v1_8_R1.NBTTagList; -import net.minecraft.server.v1_8_R1.NBTTagString; -import net.minecraft.server.v1_8_R1.World; - -public class EntityNMSItem extends EntityItem implements NMSItem { - - private static final ReflectField RIDER_PITCH_DELTA = new ReflectField(Entity.class, "ap"); - private static final ReflectField RIDER_YAW_DELTA = new ReflectField(Entity.class, "aq"); - - private boolean lockTick; - private ItemLine parentPiece; - private ItemPickupManager itemPickupManager; - - public EntityNMSItem(World world, ItemLine piece, ItemPickupManager itemPickupManager) { - super(world); - super.pickupDelay = Integer.MAX_VALUE; - this.parentPiece = piece; - this.itemPickupManager = itemPickupManager; - forceSetBoundingBox(new NullBoundingBox()); - } - - @Override - public void s_() { - - // So it won't get removed. - ticksLived = 0; - - if (!lockTick) { - super.s_(); - } - } - - // Method called when a player is near. - @Override - public void d(EntityHuman human) { - - if (human.locY < this.locY - 1.5 || human.locY > this.locY + 1.0) { - // Too low or too high, it's a bit weird. - return; - } - - if (parentPiece.getPickupHandler() != null && human instanceof EntityPlayer) { - itemPickupManager.handleItemLinePickup((Player) human.getBukkitEntity(), parentPiece.getPickupHandler(), parentPiece.getParent()); - // It is never added to the inventory. - } - } - - @Override - public void b(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public boolean c(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public boolean d(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public void e(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public boolean isInvulnerable(DamageSource source) { - /* - * The field Entity.invulnerable is private. - * It's only used while saving NBTTags, but since the entity would be killed - * on chunk unload, we prefer to override isInvulnerable(). - */ - return true; - } - - @Override - public void a(AxisAlignedBB boundingBox) { - // Do not change it! - } - - public void forceSetBoundingBox(AxisAlignedBB boundingBox) { - super.a(boundingBox); - } - - @Override - public void inactiveTick() { - // Check inactive ticks. - - if (!lockTick) { - super.inactiveTick(); - } - } - - @Override - public void setLockTick(boolean lock) { - lockTick = lock; - } - - @Override - public void die() { - setLockTick(false); - super.die(); - } - - @Override - public CraftEntity getBukkitEntity() { - if (super.bukkitEntity == null) { - this.bukkitEntity = new CraftNMSItem(this.world.getServer(), this); - } - return this.bukkitEntity; - } - - @Override - public boolean isDeadNMS() { - return this.dead; - } - - @Override - public void killEntityNMS() { - die(); - } - - @Override - public void setLocationNMS(double x, double y, double z) { - super.setPosition(x, y, z); - } - - @Override - public void setItemStackNMS(org.bukkit.inventory.ItemStack stack) { - ItemStack newItem = CraftItemStack.asNMSCopy(stack); - - if (newItem == null) { - newItem = new ItemStack(Blocks.BEDROCK); - } - - if (newItem.getTag() == null) { - newItem.setTag(new NBTTagCompound()); - } - NBTTagCompound display = newItem.getTag().getCompound("display"); - - if (!newItem.getTag().hasKey("display")) { - newItem.getTag().set("display", display); - } - - NBTTagList tagList = new NBTTagList(); - tagList.add(new NBTTagString(ItemUtils.ANTISTACK_LORE)); // Antistack lore - - display.set("Lore", tagList); - newItem.count = 0; - setItemStack(newItem); - } - - @Override - public int getIdNMS() { - return this.getId(); - } - - @Override - public ItemLine getHologramLine() { - return parentPiece; - } - - @Override - public void allowPickup(boolean pickup) { - if (pickup) { - super.pickupDelay = 0; - } else { - super.pickupDelay = Integer.MAX_VALUE; - } - } - - @Override - public org.bukkit.entity.Entity getBukkitEntityNMS() { - return getBukkitEntity(); - } - - @Override - public void setPassengerOfNMS(NMSEntityBase vehicleBase) { - if (vehicleBase == null || !(vehicleBase instanceof Entity)) { - // It should never dismount - return; - } - - Entity entity = (Entity) vehicleBase; - - try { - RIDER_PITCH_DELTA.set(this, 0.0); - RIDER_YAW_DELTA.set(this, 0.0); - } catch (Exception ex) { - ConsoleLogger.logDebugException(ex); - } - - if (this.vehicle != null) { - this.vehicle.passenger = null; - } - - this.vehicle = entity; - entity.passenger = this; - } - - @Override - public Object getRawItemStack() { - return super.getItemStack(); - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_8_R1; + +import org.bukkit.craftbukkit.v1_8_R1.entity.CraftEntity; +import org.bukkit.craftbukkit.v1_8_R1.inventory.CraftItemStack; +import org.bukkit.entity.Player; + +import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.ItemUtils; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; + +import net.minecraft.server.v1_8_R1.AxisAlignedBB; +import net.minecraft.server.v1_8_R1.Blocks; +import net.minecraft.server.v1_8_R1.DamageSource; +import net.minecraft.server.v1_8_R1.Entity; +import net.minecraft.server.v1_8_R1.EntityHuman; +import net.minecraft.server.v1_8_R1.EntityItem; +import net.minecraft.server.v1_8_R1.EntityPlayer; +import net.minecraft.server.v1_8_R1.ItemStack; +import net.minecraft.server.v1_8_R1.NBTTagCompound; +import net.minecraft.server.v1_8_R1.NBTTagList; +import net.minecraft.server.v1_8_R1.NBTTagString; +import net.minecraft.server.v1_8_R1.World; + +public class EntityNMSItem extends EntityItem implements NMSItem { + + private static final ReflectField RIDER_PITCH_DELTA = new ReflectField(Entity.class, "ap"); + private static final ReflectField RIDER_YAW_DELTA = new ReflectField(Entity.class, "aq"); + + private boolean lockTick; + private ItemLine parentPiece; + private ItemPickupManager itemPickupManager; + + public EntityNMSItem(World world, ItemLine piece, ItemPickupManager itemPickupManager) { + super(world); + super.pickupDelay = Integer.MAX_VALUE; + this.parentPiece = piece; + this.itemPickupManager = itemPickupManager; + forceSetBoundingBox(new NullBoundingBox()); + } + + @Override + public void s_() { + + // So it won't get removed. + ticksLived = 0; + + if (!lockTick) { + super.s_(); + } + } + + // Method called when a player is near. + @Override + public void d(EntityHuman human) { + + if (human.locY < this.locY - 1.5 || human.locY > this.locY + 1.0) { + // Too low or too high, it's a bit weird. + return; + } + + if (parentPiece.getPickupHandler() != null && human instanceof EntityPlayer) { + itemPickupManager.handleItemLinePickup((Player) human.getBukkitEntity(), parentPiece.getPickupHandler(), parentPiece.getParent()); + // It is never added to the inventory. + } + } + + @Override + public void b(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public boolean c(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public boolean d(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public void e(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public boolean isInvulnerable(DamageSource source) { + /* + * The field Entity.invulnerable is private. + * It's only used while saving NBTTags, but since the entity would be killed + * on chunk unload, we prefer to override isInvulnerable(). + */ + return true; + } + + @Override + public void a(AxisAlignedBB boundingBox) { + // Do not change it! + } + + public void forceSetBoundingBox(AxisAlignedBB boundingBox) { + super.a(boundingBox); + } + + @Override + public void inactiveTick() { + // Check inactive ticks. + + if (!lockTick) { + super.inactiveTick(); + } + } + + @Override + public void setLockTick(boolean lock) { + lockTick = lock; + } + + @Override + public void die() { + setLockTick(false); + super.die(); + } + + @Override + public CraftEntity getBukkitEntity() { + if (super.bukkitEntity == null) { + this.bukkitEntity = new CraftNMSItem(this.world.getServer(), this); + } + return this.bukkitEntity; + } + + @Override + public boolean isDeadNMS() { + return this.dead; + } + + @Override + public void killEntityNMS() { + die(); + } + + @Override + public void setLocationNMS(double x, double y, double z) { + super.setPosition(x, y, z); + } + + @Override + public void setItemStackNMS(org.bukkit.inventory.ItemStack stack) { + ItemStack newItem = CraftItemStack.asNMSCopy(stack); + + if (newItem == null) { + newItem = new ItemStack(Blocks.BEDROCK); + } + + if (newItem.getTag() == null) { + newItem.setTag(new NBTTagCompound()); + } + NBTTagCompound display = newItem.getTag().getCompound("display"); + + if (!newItem.getTag().hasKey("display")) { + newItem.getTag().set("display", display); + } + + NBTTagList tagList = new NBTTagList(); + tagList.add(new NBTTagString(ItemUtils.ANTISTACK_LORE)); // Antistack lore + + display.set("Lore", tagList); + newItem.count = 0; + setItemStack(newItem); + } + + @Override + public int getIdNMS() { + return this.getId(); + } + + @Override + public ItemLine getHologramLine() { + return parentPiece; + } + + @Override + public void allowPickup(boolean pickup) { + if (pickup) { + super.pickupDelay = 0; + } else { + super.pickupDelay = Integer.MAX_VALUE; + } + } + + @Override + public org.bukkit.entity.Entity getBukkitEntityNMS() { + return getBukkitEntity(); + } + + @Override + public void setPassengerOfNMS(NMSEntityBase vehicleBase) { + if (vehicleBase == null || !(vehicleBase instanceof Entity)) { + // It should never dismount + return; + } + + Entity entity = (Entity) vehicleBase; + + try { + RIDER_PITCH_DELTA.set(this, 0.0); + RIDER_YAW_DELTA.set(this, 0.0); + } catch (Exception ex) { + ConsoleLogger.logDebugException(ex); + } + + if (this.vehicle != null) { + this.vehicle.passenger = null; + } + + this.vehicle = entity; + entity.passenger = this; + } + + @Override + public Object getRawItemStack() { + return super.getItemStack(); + } +} diff --git a/NMS/v1_8_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R1/EntityNMSSlime.java b/NMS/v1_8_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R1/EntityNMSSlime.java index 0913f36b..b2754571 100644 --- a/NMS/v1_8_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R1/EntityNMSSlime.java +++ b/NMS/v1_8_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R1/EntityNMSSlime.java @@ -1,194 +1,208 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_8_R1; - -import org.bukkit.Bukkit; -import org.bukkit.craftbukkit.v1_8_R1.entity.CraftEntity; -import org.bukkit.event.player.PlayerInteractEntityEvent; - -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSSlime; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; - -import net.minecraft.server.v1_8_R1.AxisAlignedBB; -import net.minecraft.server.v1_8_R1.DamageSource; -import net.minecraft.server.v1_8_R1.Entity; -import net.minecraft.server.v1_8_R1.EntityDamageSource; -import net.minecraft.server.v1_8_R1.EntityPlayer; -import net.minecraft.server.v1_8_R1.EntitySlime; -import net.minecraft.server.v1_8_R1.NBTTagCompound; -import net.minecraft.server.v1_8_R1.World; - -public class EntityNMSSlime extends EntitySlime implements NMSSlime { - - private static final ReflectField RIDER_PITCH_DELTA = new ReflectField(Entity.class, "ap"); - private static final ReflectField RIDER_YAW_DELTA = new ReflectField(Entity.class, "aq"); - - private boolean lockTick; - private HologramLine parentPiece; - - public EntityNMSSlime(World world, HologramLine parentPiece) { - super(world); - super.persistent = true; - a(0.0F, 0.0F); - setSize(1); - setInvisible(true); - this.parentPiece = parentPiece; - forceSetBoundingBox(new NullBoundingBox()); - } - - @Override - public void a(AxisAlignedBB boundingBox) { - // Do not change it! - } - - public void forceSetBoundingBox(AxisAlignedBB boundingBox) { - super.a(boundingBox); - } - - @Override - public void s_() { - // Checks every 20 ticks. - if (ticksLived % 20 == 0) { - // The slime dies without a vehicle. - if (this.vehicle == null) { - die(); - } - } - - if (!lockTick) { - super.s_(); - } - } - - @Override - public void b(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public boolean c(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public boolean d(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public void e(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public boolean damageEntity(DamageSource damageSource, float amount) { - if (damageSource instanceof EntityDamageSource) { - EntityDamageSource entityDamageSource = (EntityDamageSource) damageSource; - if (entityDamageSource.getEntity() instanceof EntityPlayer) { - Bukkit.getPluginManager().callEvent(new PlayerInteractEntityEvent(((EntityPlayer) entityDamageSource.getEntity()).getBukkitEntity(), getBukkitEntity())); - } - } - return false; - } - - @Override - public boolean isInvulnerable(DamageSource source) { - /* - * The field Entity.invulnerable is private. - * It's only used while saving NBTTags, but since the entity would be killed - * on chunk unload, we prefer to override isInvulnerable(). - */ - return true; - } - - @Override - public void setCustomName(String customName) { - // Locks the custom name. - } - - @Override - public void setCustomNameVisible(boolean visible) { - // Locks the custom name. - } - - @Override - public void makeSound(String sound, float volume, float pitch) { - // Remove sounds. - } - - @Override - public void setLockTick(boolean lock) { - lockTick = lock; - } - - @Override - public void die() { - setLockTick(false); - super.die(); - } - - @Override - public CraftEntity getBukkitEntity() { - if (super.bukkitEntity == null) { - this.bukkitEntity = new CraftNMSSlime(this.world.getServer(), this); - } - return this.bukkitEntity; - } - - @Override - public boolean isDeadNMS() { - return super.dead; - } - - @Override - public void killEntityNMS() { - die(); - } - - @Override - public void setLocationNMS(double x, double y, double z) { - super.setPosition(x, y, z); - } - - @Override - public int getIdNMS() { - return this.getId(); - } - - @Override - public HologramLine getHologramLine() { - return parentPiece; - } - - @Override - public org.bukkit.entity.Entity getBukkitEntityNMS() { - return getBukkitEntity(); - } - - @Override - public void setPassengerOfNMS(NMSEntityBase vehicleBase) { - if (vehicleBase == null || !(vehicleBase instanceof Entity)) { - // It should never dismount - return; - } - - Entity entity = (Entity) vehicleBase; - - try { - RIDER_PITCH_DELTA.set(this, 0.0); - RIDER_YAW_DELTA.set(this, 0.0); - } catch (Exception ex) { - ConsoleLogger.logDebugException(ex); - } - - if (this.vehicle != null) { - this.vehicle.passenger = null; - } - - this.vehicle = entity; - entity.passenger = this; - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_8_R1; + +import org.bukkit.Bukkit; +import org.bukkit.craftbukkit.v1_8_R1.entity.CraftEntity; +import org.bukkit.event.player.PlayerInteractEntityEvent; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSSlime; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; + +import net.minecraft.server.v1_8_R1.AxisAlignedBB; +import net.minecraft.server.v1_8_R1.DamageSource; +import net.minecraft.server.v1_8_R1.Entity; +import net.minecraft.server.v1_8_R1.EntityDamageSource; +import net.minecraft.server.v1_8_R1.EntityPlayer; +import net.minecraft.server.v1_8_R1.EntitySlime; +import net.minecraft.server.v1_8_R1.NBTTagCompound; +import net.minecraft.server.v1_8_R1.World; + +public class EntityNMSSlime extends EntitySlime implements NMSSlime { + + private static final ReflectField RIDER_PITCH_DELTA = new ReflectField(Entity.class, "ap"); + private static final ReflectField RIDER_YAW_DELTA = new ReflectField(Entity.class, "aq"); + + private boolean lockTick; + private HologramLine parentPiece; + + public EntityNMSSlime(World world, HologramLine parentPiece) { + super(world); + super.persistent = true; + a(0.0F, 0.0F); + setSize(1); + setInvisible(true); + this.parentPiece = parentPiece; + forceSetBoundingBox(new NullBoundingBox()); + } + + @Override + public void a(AxisAlignedBB boundingBox) { + // Do not change it! + } + + public void forceSetBoundingBox(AxisAlignedBB boundingBox) { + super.a(boundingBox); + } + + @Override + public void s_() { + // Checks every 20 ticks. + if (ticksLived % 20 == 0) { + // The slime dies without a vehicle. + if (this.vehicle == null) { + die(); + } + } + + if (!lockTick) { + super.s_(); + } + } + + @Override + public void b(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public boolean c(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public boolean d(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public void e(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public boolean damageEntity(DamageSource damageSource, float amount) { + if (damageSource instanceof EntityDamageSource) { + EntityDamageSource entityDamageSource = (EntityDamageSource) damageSource; + if (entityDamageSource.getEntity() instanceof EntityPlayer) { + Bukkit.getPluginManager().callEvent(new PlayerInteractEntityEvent(((EntityPlayer) entityDamageSource.getEntity()).getBukkitEntity(), getBukkitEntity())); + } + } + return false; + } + + @Override + public boolean isInvulnerable(DamageSource source) { + /* + * The field Entity.invulnerable is private. + * It's only used while saving NBTTags, but since the entity would be killed + * on chunk unload, we prefer to override isInvulnerable(). + */ + return true; + } + + @Override + public void setCustomName(String customName) { + // Locks the custom name. + } + + @Override + public void setCustomNameVisible(boolean visible) { + // Locks the custom name. + } + + @Override + public void makeSound(String sound, float volume, float pitch) { + // Remove sounds. + } + + @Override + public void setLockTick(boolean lock) { + lockTick = lock; + } + + @Override + public void die() { + setLockTick(false); + super.die(); + } + + @Override + public CraftEntity getBukkitEntity() { + if (super.bukkitEntity == null) { + this.bukkitEntity = new CraftNMSSlime(this.world.getServer(), this); + } + return this.bukkitEntity; + } + + @Override + public boolean isDeadNMS() { + return super.dead; + } + + @Override + public void killEntityNMS() { + die(); + } + + @Override + public void setLocationNMS(double x, double y, double z) { + super.setPosition(x, y, z); + } + + @Override + public int getIdNMS() { + return this.getId(); + } + + @Override + public HologramLine getHologramLine() { + return parentPiece; + } + + @Override + public org.bukkit.entity.Entity getBukkitEntityNMS() { + return getBukkitEntity(); + } + + @Override + public void setPassengerOfNMS(NMSEntityBase vehicleBase) { + if (vehicleBase == null || !(vehicleBase instanceof Entity)) { + // It should never dismount + return; + } + + Entity entity = (Entity) vehicleBase; + + try { + RIDER_PITCH_DELTA.set(this, 0.0); + RIDER_YAW_DELTA.set(this, 0.0); + } catch (Exception ex) { + ConsoleLogger.logDebugException(ex); + } + + if (this.vehicle != null) { + this.vehicle.passenger = null; + } + + this.vehicle = entity; + entity.passenger = this; + } +} diff --git a/NMS/v1_8_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R1/NmsManagerImpl.java b/NMS/v1_8_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R1/NmsManagerImpl.java index 5698a871..67113933 100644 --- a/NMS/v1_8_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R1/NmsManagerImpl.java +++ b/NMS/v1_8_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R1/NmsManagerImpl.java @@ -1,130 +1,144 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_8_R1; - -import java.lang.reflect.Method; -import java.util.Map; - -import org.bukkit.Bukkit; -import org.bukkit.craftbukkit.v1_8_R1.CraftWorld; -import org.bukkit.craftbukkit.v1_8_R1.entity.CraftEntity; -import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason; -import org.bukkit.inventory.ItemStack; - -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; -import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.Validator; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; - -import net.minecraft.server.v1_8_R1.Entity; -import net.minecraft.server.v1_8_R1.EntityTypes; -import net.minecraft.server.v1_8_R1.MathHelper; -import net.minecraft.server.v1_8_R1.World; -import net.minecraft.server.v1_8_R1.WorldServer; - -public class NmsManagerImpl implements NMSManager { - - private static final ReflectField, String>> ENTITY_NAMES_BY_CLASS_FIELD = new ReflectField, String>>(EntityTypes.class, "d"); - private static final ReflectField, Integer>> ENTITY_IDS_BY_CLASS_FIELD = new ReflectField, Integer>>(EntityTypes.class, "f"); - - private Method validateEntityMethod; - - @Override - public void setup() throws Exception { - registerCustomEntity(EntityNMSArmorStand.class, "ArmorStand", 30); - registerCustomEntity(EntityNMSItem.class, "Item", 1); - registerCustomEntity(EntityNMSSlime.class, "Slime", 55); - - validateEntityMethod = World.class.getDeclaredMethod("a", Entity.class); - validateEntityMethod.setAccessible(true); - } - - public void registerCustomEntity(Class entityClass, String name, int id) throws Exception { - ENTITY_NAMES_BY_CLASS_FIELD.getStatic().put(entityClass, name); - ENTITY_IDS_BY_CLASS_FIELD.getStatic().put(entityClass, Integer.valueOf(id)); - } - - @Override - public NMSItem spawnNMSItem(org.bukkit.World bukkitWorld, double x, double y, double z, ItemLine parentPiece, ItemStack stack, ItemPickupManager itemPickupManager) { - WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); - EntityNMSItem customItem = new EntityNMSItem(nmsWorld, parentPiece, itemPickupManager); - customItem.setLocationNMS(x, y, z); - customItem.setItemStackNMS(stack); - if (!addEntityToWorld(nmsWorld, customItem)) { - ConsoleLogger.handleSpawnFail(parentPiece); - } - return customItem; - } - - @Override - public EntityNMSSlime spawnNMSSlime(org.bukkit.World bukkitWorld, double x, double y, double z, HologramLine parentPiece) { - WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); - EntityNMSSlime touchSlime = new EntityNMSSlime(nmsWorld, parentPiece); - touchSlime.setLocationNMS(x, y, z); - if (!addEntityToWorld(nmsWorld, touchSlime)) { - ConsoleLogger.handleSpawnFail(parentPiece); - } - return touchSlime; - } - - @Override - public NMSArmorStand spawnNMSArmorStand(org.bukkit.World world, double x, double y, double z, HologramLine parentPiece) { - WorldServer nmsWorld = ((CraftWorld) world).getHandle(); - EntityNMSArmorStand invisibleArmorStand = new EntityNMSArmorStand(nmsWorld, parentPiece); - invisibleArmorStand.setLocationNMS(x, y, z); - if (!addEntityToWorld(nmsWorld, invisibleArmorStand)) { - ConsoleLogger.handleSpawnFail(parentPiece); - } - return invisibleArmorStand; - } - - @SuppressWarnings("unchecked") - private boolean addEntityToWorld(WorldServer nmsWorld, Entity nmsEntity) { - Validator.isTrue(Bukkit.isPrimaryThread(), "Async entity add"); - - if (validateEntityMethod == null) { - return nmsWorld.addEntity(nmsEntity, SpawnReason.CUSTOM); - } - - final int chunkX = MathHelper.floor(nmsEntity.locX / 16.0); - final int chunkZ = MathHelper.floor(nmsEntity.locZ / 16.0); - - if (!nmsWorld.chunkProviderServer.isChunkLoaded(chunkX, chunkZ)) { - // This should never happen - nmsEntity.dead = true; - return false; - } - - nmsWorld.getChunkAt(chunkX, chunkZ).a(nmsEntity); - nmsWorld.entityList.add(nmsEntity); - - try { - validateEntityMethod.invoke(nmsWorld, nmsEntity); - } catch (Exception e) { - e.printStackTrace(); - return false; - } - return true; - } - - @Override - public boolean isNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { - return ((CraftEntity) bukkitEntity).getHandle() instanceof NMSEntityBase; - } - - @Override - public NMSEntityBase getNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { - - Entity nmsEntity = ((CraftEntity) bukkitEntity).getHandle(); - if (nmsEntity instanceof NMSEntityBase) { - return ((NMSEntityBase) nmsEntity); - } - - return null; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_8_R1; + +import java.lang.reflect.Method; +import java.util.Map; + +import org.bukkit.Bukkit; +import org.bukkit.craftbukkit.v1_8_R1.CraftWorld; +import org.bukkit.craftbukkit.v1_8_R1.entity.CraftEntity; +import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason; +import org.bukkit.inventory.ItemStack; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; +import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.Validator; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; + +import net.minecraft.server.v1_8_R1.Entity; +import net.minecraft.server.v1_8_R1.EntityTypes; +import net.minecraft.server.v1_8_R1.MathHelper; +import net.minecraft.server.v1_8_R1.World; +import net.minecraft.server.v1_8_R1.WorldServer; + +public class NmsManagerImpl implements NMSManager { + + private static final ReflectField, String>> ENTITY_NAMES_BY_CLASS_FIELD = new ReflectField, String>>(EntityTypes.class, "d"); + private static final ReflectField, Integer>> ENTITY_IDS_BY_CLASS_FIELD = new ReflectField, Integer>>(EntityTypes.class, "f"); + + private Method validateEntityMethod; + + @Override + public void setup() throws Exception { + registerCustomEntity(EntityNMSArmorStand.class, "ArmorStand", 30); + registerCustomEntity(EntityNMSItem.class, "Item", 1); + registerCustomEntity(EntityNMSSlime.class, "Slime", 55); + + validateEntityMethod = World.class.getDeclaredMethod("a", Entity.class); + validateEntityMethod.setAccessible(true); + } + + public void registerCustomEntity(Class entityClass, String name, int id) throws Exception { + ENTITY_NAMES_BY_CLASS_FIELD.getStatic().put(entityClass, name); + ENTITY_IDS_BY_CLASS_FIELD.getStatic().put(entityClass, Integer.valueOf(id)); + } + + @Override + public NMSItem spawnNMSItem(org.bukkit.World bukkitWorld, double x, double y, double z, ItemLine parentPiece, ItemStack stack, ItemPickupManager itemPickupManager) { + WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); + EntityNMSItem customItem = new EntityNMSItem(nmsWorld, parentPiece, itemPickupManager); + customItem.setLocationNMS(x, y, z); + customItem.setItemStackNMS(stack); + if (!addEntityToWorld(nmsWorld, customItem)) { + ConsoleLogger.handleSpawnFail(parentPiece); + } + return customItem; + } + + @Override + public EntityNMSSlime spawnNMSSlime(org.bukkit.World bukkitWorld, double x, double y, double z, HologramLine parentPiece) { + WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); + EntityNMSSlime touchSlime = new EntityNMSSlime(nmsWorld, parentPiece); + touchSlime.setLocationNMS(x, y, z); + if (!addEntityToWorld(nmsWorld, touchSlime)) { + ConsoleLogger.handleSpawnFail(parentPiece); + } + return touchSlime; + } + + @Override + public NMSArmorStand spawnNMSArmorStand(org.bukkit.World world, double x, double y, double z, HologramLine parentPiece) { + WorldServer nmsWorld = ((CraftWorld) world).getHandle(); + EntityNMSArmorStand invisibleArmorStand = new EntityNMSArmorStand(nmsWorld, parentPiece); + invisibleArmorStand.setLocationNMS(x, y, z); + if (!addEntityToWorld(nmsWorld, invisibleArmorStand)) { + ConsoleLogger.handleSpawnFail(parentPiece); + } + return invisibleArmorStand; + } + + @SuppressWarnings("unchecked") + private boolean addEntityToWorld(WorldServer nmsWorld, Entity nmsEntity) { + Validator.isTrue(Bukkit.isPrimaryThread(), "Async entity add"); + + if (validateEntityMethod == null) { + return nmsWorld.addEntity(nmsEntity, SpawnReason.CUSTOM); + } + + final int chunkX = MathHelper.floor(nmsEntity.locX / 16.0); + final int chunkZ = MathHelper.floor(nmsEntity.locZ / 16.0); + + if (!nmsWorld.chunkProviderServer.isChunkLoaded(chunkX, chunkZ)) { + // This should never happen + nmsEntity.dead = true; + return false; + } + + nmsWorld.getChunkAt(chunkX, chunkZ).a(nmsEntity); + nmsWorld.entityList.add(nmsEntity); + + try { + validateEntityMethod.invoke(nmsWorld, nmsEntity); + } catch (Exception e) { + e.printStackTrace(); + return false; + } + return true; + } + + @Override + public boolean isNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { + return ((CraftEntity) bukkitEntity).getHandle() instanceof NMSEntityBase; + } + + @Override + public NMSEntityBase getNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { + + Entity nmsEntity = ((CraftEntity) bukkitEntity).getHandle(); + if (nmsEntity instanceof NMSEntityBase) { + return ((NMSEntityBase) nmsEntity); + } + + return null; + } + +} diff --git a/NMS/v1_8_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R1/NullBoundingBox.java b/NMS/v1_8_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R1/NullBoundingBox.java index 2e513d9b..7b80d9eb 100644 --- a/NMS/v1_8_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R1/NullBoundingBox.java +++ b/NMS/v1_8_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R1/NullBoundingBox.java @@ -1,75 +1,89 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_8_R1; - -import net.minecraft.server.v1_8_R1.AxisAlignedBB; -import net.minecraft.server.v1_8_R1.MovingObjectPosition; -import net.minecraft.server.v1_8_R1.Vec3D; - -public class NullBoundingBox extends AxisAlignedBB { - - public NullBoundingBox() { - super(0, 0, 0, 0, 0, 0); - } - - @Override - public double a() { - return 0.0; - } - - @Override - public double a(AxisAlignedBB arg0, double arg1) { - return 0.0; - } - - @Override - public AxisAlignedBB a(AxisAlignedBB arg0) { - return this; - } - - @Override - public AxisAlignedBB a(double arg0, double arg1, double arg2) { - return this; - } - - @Override - public MovingObjectPosition a(Vec3D arg0, Vec3D arg1) { - return super.a(arg0, arg1); - } - - @Override - public boolean a(Vec3D arg0) { - return false; - } - - @Override - public double b(AxisAlignedBB arg0, double arg1) { - return 0.0; - } - - @Override - public boolean b(AxisAlignedBB arg0) { - return false; - } - - @Override - public double c(AxisAlignedBB arg0, double arg1) { - return 0.0; - } - - @Override - public AxisAlignedBB c(double arg0, double arg1, double arg2) { - return this; - } - - @Override - public AxisAlignedBB grow(double arg0, double arg1, double arg2) { - return this; - } - - @Override - public AxisAlignedBB shrink(double arg0, double arg1, double arg2) { - return this; - } - - - -} \ No newline at end of file +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_8_R1; + +import net.minecraft.server.v1_8_R1.AxisAlignedBB; +import net.minecraft.server.v1_8_R1.MovingObjectPosition; +import net.minecraft.server.v1_8_R1.Vec3D; + +public class NullBoundingBox extends AxisAlignedBB { + + public NullBoundingBox() { + super(0, 0, 0, 0, 0, 0); + } + + @Override + public double a() { + return 0.0; + } + + @Override + public double a(AxisAlignedBB arg0, double arg1) { + return 0.0; + } + + @Override + public AxisAlignedBB a(AxisAlignedBB arg0) { + return this; + } + + @Override + public AxisAlignedBB a(double arg0, double arg1, double arg2) { + return this; + } + + @Override + public MovingObjectPosition a(Vec3D arg0, Vec3D arg1) { + return super.a(arg0, arg1); + } + + @Override + public boolean a(Vec3D arg0) { + return false; + } + + @Override + public double b(AxisAlignedBB arg0, double arg1) { + return 0.0; + } + + @Override + public boolean b(AxisAlignedBB arg0) { + return false; + } + + @Override + public double c(AxisAlignedBB arg0, double arg1) { + return 0.0; + } + + @Override + public AxisAlignedBB c(double arg0, double arg1, double arg2) { + return this; + } + + @Override + public AxisAlignedBB grow(double arg0, double arg1, double arg2) { + return this; + } + + @Override + public AxisAlignedBB shrink(double arg0, double arg1, double arg2) { + return this; + } + + + +} diff --git a/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/CraftNMSArmorStand.java b/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/CraftNMSArmorStand.java index 4e32e499..413065b1 100644 --- a/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/CraftNMSArmorStand.java +++ b/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/CraftNMSArmorStand.java @@ -1,67 +1,81 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_8_R2; - -import java.util.Collection; - -import org.bukkit.EntityEffect; -import org.bukkit.Location; -import org.bukkit.craftbukkit.v1_8_R2.CraftServer; -import org.bukkit.craftbukkit.v1_8_R2.entity.CraftArmorStand; -import org.bukkit.entity.Entity; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -import org.bukkit.inventory.ItemStack; -import org.bukkit.potion.PotionEffect; -import org.bukkit.util.EulerAngle; -import org.bukkit.util.Vector; - -public class CraftNMSArmorStand extends CraftArmorStand { - - public CraftNMSArmorStand(CraftServer server, EntityNMSArmorStand entity) { - super(server, entity); - } - - // Disallow all the bukkit methods. - - @Override - public void remove() { - // Cannot be removed, this is the most important to override. - } - - // Methods from Armor stand class - @Override public void setArms(boolean arms) { } - @Override public void setBasePlate(boolean basePlate) { } - @Override public void setBodyPose(EulerAngle pose) { } - @Override public void setBoots(ItemStack item) { } - @Override public void setChestplate(ItemStack item) { } - @Override public void setGravity(boolean gravity) { } - @Override public void setHeadPose(EulerAngle pose) { } - @Override public void setHelmet(ItemStack item) { } - @Override public void setItemInHand(ItemStack item) { } - @Override public void setLeftArmPose(EulerAngle pose) { } - @Override public void setLeftLegPose(EulerAngle pose) { } - @Override public void setLeggings(ItemStack item) { } - @Override public void setRightArmPose(EulerAngle pose) { } - @Override public void setRightLegPose(EulerAngle pose) { } - @Override public void setSmall(boolean small) { } - @Override public void setVisible(boolean visible) { } - - // Methods from LivingEntity class - @Override public boolean addPotionEffect(PotionEffect effect) { return false; } - @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } - @Override public boolean addPotionEffects(Collection effects) { return false; } - @Override public void setRemoveWhenFarAway(boolean remove) { } - - // Methods from Entity - @Override public void setVelocity(Vector vel) { } - @Override public boolean teleport(Location loc) { return false; } - @Override public boolean teleport(Entity entity) { return false; } - @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } - @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } - @Override public void setFireTicks(int ticks) { } - @Override public boolean setPassenger(Entity entity) { return false; } - @Override public boolean eject() { return false; } - @Override public boolean leaveVehicle() { return false; } - @Override public void playEffect(EntityEffect effect) { } - @Override public void setCustomName(String name) { } - @Override public void setCustomNameVisible(boolean flag) { } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_8_R2; + +import java.util.Collection; + +import org.bukkit.EntityEffect; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_8_R2.CraftServer; +import org.bukkit.craftbukkit.v1_8_R2.entity.CraftArmorStand; +import org.bukkit.entity.Entity; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import org.bukkit.inventory.ItemStack; +import org.bukkit.potion.PotionEffect; +import org.bukkit.util.EulerAngle; +import org.bukkit.util.Vector; + +public class CraftNMSArmorStand extends CraftArmorStand { + + public CraftNMSArmorStand(CraftServer server, EntityNMSArmorStand entity) { + super(server, entity); + } + + // Disallow all the bukkit methods. + + @Override + public void remove() { + // Cannot be removed, this is the most important to override. + } + + // Methods from Armor stand class + @Override public void setArms(boolean arms) { } + @Override public void setBasePlate(boolean basePlate) { } + @Override public void setBodyPose(EulerAngle pose) { } + @Override public void setBoots(ItemStack item) { } + @Override public void setChestplate(ItemStack item) { } + @Override public void setGravity(boolean gravity) { } + @Override public void setHeadPose(EulerAngle pose) { } + @Override public void setHelmet(ItemStack item) { } + @Override public void setItemInHand(ItemStack item) { } + @Override public void setLeftArmPose(EulerAngle pose) { } + @Override public void setLeftLegPose(EulerAngle pose) { } + @Override public void setLeggings(ItemStack item) { } + @Override public void setRightArmPose(EulerAngle pose) { } + @Override public void setRightLegPose(EulerAngle pose) { } + @Override public void setSmall(boolean small) { } + @Override public void setVisible(boolean visible) { } + + // Methods from LivingEntity class + @Override public boolean addPotionEffect(PotionEffect effect) { return false; } + @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } + @Override public boolean addPotionEffects(Collection effects) { return false; } + @Override public void setRemoveWhenFarAway(boolean remove) { } + + // Methods from Entity + @Override public void setVelocity(Vector vel) { } + @Override public boolean teleport(Location loc) { return false; } + @Override public boolean teleport(Entity entity) { return false; } + @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } + @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } + @Override public void setFireTicks(int ticks) { } + @Override public boolean setPassenger(Entity entity) { return false; } + @Override public boolean eject() { return false; } + @Override public boolean leaveVehicle() { return false; } + @Override public void playEffect(EntityEffect effect) { } + @Override public void setCustomName(String name) { } + @Override public void setCustomNameVisible(boolean flag) { } + +} diff --git a/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/CraftNMSItem.java b/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/CraftNMSItem.java index a8d4735b..27d0962b 100644 --- a/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/CraftNMSItem.java +++ b/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/CraftNMSItem.java @@ -1,42 +1,56 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_8_R2; - -import org.bukkit.EntityEffect; -import org.bukkit.Location; -import org.bukkit.craftbukkit.v1_8_R2.CraftServer; -import org.bukkit.craftbukkit.v1_8_R2.entity.CraftItem; -import org.bukkit.entity.Entity; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -import org.bukkit.inventory.ItemStack; -import org.bukkit.util.Vector; - -public class CraftNMSItem extends CraftItem { - - public CraftNMSItem(CraftServer server, EntityNMSItem entity) { - super(server, entity); - } - - // Disallow all the bukkit methods. - - @Override - public void remove() { - // Cannot be removed, this is the most important to override. - } - - // Methods from Entity - @Override public void setVelocity(Vector vel) { } - @Override public boolean teleport(Location loc) { return false; } - @Override public boolean teleport(Entity entity) { return false; } - @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } - @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } - @Override public void setFireTicks(int ticks) { } - @Override public boolean setPassenger(Entity entity) { return false; } - @Override public boolean eject() { return false; } - @Override public boolean leaveVehicle() { return false; } - @Override public void playEffect(EntityEffect effect) { } - @Override public void setCustomName(String name) { } - @Override public void setCustomNameVisible(boolean flag) { } - - // Methods from Item - @Override public void setItemStack(ItemStack stack) { } - @Override public void setPickupDelay(int delay) { } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_8_R2; + +import org.bukkit.EntityEffect; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_8_R2.CraftServer; +import org.bukkit.craftbukkit.v1_8_R2.entity.CraftItem; +import org.bukkit.entity.Entity; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import org.bukkit.inventory.ItemStack; +import org.bukkit.util.Vector; + +public class CraftNMSItem extends CraftItem { + + public CraftNMSItem(CraftServer server, EntityNMSItem entity) { + super(server, entity); + } + + // Disallow all the bukkit methods. + + @Override + public void remove() { + // Cannot be removed, this is the most important to override. + } + + // Methods from Entity + @Override public void setVelocity(Vector vel) { } + @Override public boolean teleport(Location loc) { return false; } + @Override public boolean teleport(Entity entity) { return false; } + @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } + @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } + @Override public void setFireTicks(int ticks) { } + @Override public boolean setPassenger(Entity entity) { return false; } + @Override public boolean eject() { return false; } + @Override public boolean leaveVehicle() { return false; } + @Override public void playEffect(EntityEffect effect) { } + @Override public void setCustomName(String name) { } + @Override public void setCustomNameVisible(boolean flag) { } + + // Methods from Item + @Override public void setItemStack(ItemStack stack) { } + @Override public void setPickupDelay(int delay) { } +} diff --git a/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/CraftNMSSlime.java b/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/CraftNMSSlime.java index 44ebe3dd..39592d75 100644 --- a/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/CraftNMSSlime.java +++ b/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/CraftNMSSlime.java @@ -1,49 +1,63 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_8_R2; - -import java.util.Collection; - -import org.bukkit.EntityEffect; -import org.bukkit.Location; -import org.bukkit.craftbukkit.v1_8_R2.CraftServer; -import org.bukkit.craftbukkit.v1_8_R2.entity.CraftSlime; -import org.bukkit.entity.Entity; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -import org.bukkit.potion.PotionEffect; -import org.bukkit.util.Vector; - -public class CraftNMSSlime extends CraftSlime { - - public CraftNMSSlime(CraftServer server, EntityNMSSlime entity) { - super(server, entity); - } - - // Disallow all the bukkit methods. - - @Override - public void remove() { - // Cannot be removed, this is the most important to override. - } - - // Methods from LivingEntity class - @Override public boolean addPotionEffect(PotionEffect effect) { return false; } - @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } - @Override public boolean addPotionEffects(Collection effects) { return false; } - @Override public void setRemoveWhenFarAway(boolean remove) { } - - // Methods from Entity - @Override public void setVelocity(Vector vel) { } - @Override public boolean teleport(Location loc) { return false; } - @Override public boolean teleport(Entity entity) { return false; } - @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } - @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } - @Override public void setFireTicks(int ticks) { } - @Override public boolean setPassenger(Entity entity) { return false; } - @Override public boolean eject() { return false; } - @Override public boolean leaveVehicle() { return false; } - @Override public void playEffect(EntityEffect effect) { } - @Override public void setCustomName(String name) { } - @Override public void setCustomNameVisible(boolean flag) { } - - // Methods from Slime - @Override public void setSize(int size) { } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_8_R2; + +import java.util.Collection; + +import org.bukkit.EntityEffect; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_8_R2.CraftServer; +import org.bukkit.craftbukkit.v1_8_R2.entity.CraftSlime; +import org.bukkit.entity.Entity; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import org.bukkit.potion.PotionEffect; +import org.bukkit.util.Vector; + +public class CraftNMSSlime extends CraftSlime { + + public CraftNMSSlime(CraftServer server, EntityNMSSlime entity) { + super(server, entity); + } + + // Disallow all the bukkit methods. + + @Override + public void remove() { + // Cannot be removed, this is the most important to override. + } + + // Methods from LivingEntity class + @Override public boolean addPotionEffect(PotionEffect effect) { return false; } + @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } + @Override public boolean addPotionEffects(Collection effects) { return false; } + @Override public void setRemoveWhenFarAway(boolean remove) { } + + // Methods from Entity + @Override public void setVelocity(Vector vel) { } + @Override public boolean teleport(Location loc) { return false; } + @Override public boolean teleport(Entity entity) { return false; } + @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } + @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } + @Override public void setFireTicks(int ticks) { } + @Override public boolean setPassenger(Entity entity) { return false; } + @Override public boolean eject() { return false; } + @Override public boolean leaveVehicle() { return false; } + @Override public void playEffect(EntityEffect effect) { } + @Override public void setCustomName(String name) { } + @Override public void setCustomNameVisible(boolean flag) { } + + // Methods from Slime + @Override public void setSize(int size) { } +} diff --git a/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/EntityNMSArmorStand.java b/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/EntityNMSArmorStand.java index 4920622d..6b9c249e 100644 --- a/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/EntityNMSArmorStand.java +++ b/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/EntityNMSArmorStand.java @@ -1,237 +1,251 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_8_R2; - -import org.bukkit.craftbukkit.v1_8_R2.entity.CraftEntity; - -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; -import com.gmail.filoghost.holographicdisplays.disk.Configuration; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.Utils; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectMethod; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectionUtils; - -import net.minecraft.server.v1_8_R2.AxisAlignedBB; -import net.minecraft.server.v1_8_R2.DamageSource; -import net.minecraft.server.v1_8_R2.EntityArmorStand; -import net.minecraft.server.v1_8_R2.EntityHuman; -import net.minecraft.server.v1_8_R2.EntityPlayer; -import net.minecraft.server.v1_8_R2.ItemStack; -import net.minecraft.server.v1_8_R2.MathHelper; -import net.minecraft.server.v1_8_R2.NBTTagCompound; -import net.minecraft.server.v1_8_R2.PacketPlayOutEntityTeleport; -import net.minecraft.server.v1_8_R2.Vec3D; -import net.minecraft.server.v1_8_R2.World; - -public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorStand { - - private static final ReflectField DISABLED_SLOTS_FIELD = new ReflectField(EntityArmorStand.class, "bi"); - private static final ReflectMethod SET_MARKER_METHOD = new ReflectMethod(EntityArmorStand.class, "n", boolean.class); - - private boolean lockTick; - private HologramLine parentPiece; - - public EntityNMSArmorStand(World world, HologramLine parentPiece) { - super(world); - setInvisible(true); - setSmall(true); - setArms(false); - setGravity(true); - setBasePlate(true); - try { - SET_MARKER_METHOD.invoke(this, true); - } catch (Exception e) { - ConsoleLogger.logDebugException(e); - // It will still work, but the offset will be wrong. - } - this.parentPiece = parentPiece; - try { - DISABLED_SLOTS_FIELD.set(this, Integer.MAX_VALUE); - } catch (Exception e) { - // There's still the overridden method. - } - forceSetBoundingBox(new NullBoundingBox()); - } - - - @Override - public void b(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public boolean c(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public boolean d(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public void e(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - - @Override - public boolean isInvulnerable(DamageSource source) { - /* - * The field Entity.invulnerable is private. - * It's only used while saving NBTTags, but since the entity would be killed - * on chunk unload, we prefer to override isInvulnerable(). - */ - return true; - } - - @Override - public void setCustomName(String customName) { - // Locks the custom name. - } - - @Override - public void setCustomNameVisible(boolean visible) { - // Locks the custom name. - } - - @Override - public boolean a(EntityHuman human, Vec3D vec3d) { - // Prevent stand being equipped - return true; - } - - @Override - public boolean d(int i, ItemStack item) { - // Prevent stand being equipped - return false; - } - - @Override - public void setEquipment(int i, ItemStack item) { - // Prevent stand being equipped - } - - @Override - public void a(AxisAlignedBB boundingBox) { - // Do not change it! - } - - public void forceSetBoundingBox(AxisAlignedBB boundingBox) { - super.a(boundingBox); - } - - @Override - public int getId() { - if (Configuration.preciseHologramMovement) { - StackTraceElement element = ReflectionUtils.getStackTraceElement(2); - if (element != null && element.getFileName() != null && element.getFileName().equals("EntityTrackerEntry.java") && element.getLineNumber() > 137 && element.getLineNumber() < 147) { - // Then this method is being called when creating a new packet, we return a fake ID! - return -1; - } - } - - return super.getId(); - } - - @Override - public void t_() { - if (!lockTick) { - super.t_(); - } - } - - @Override - public void makeSound(String sound, float f1, float f2) { - // Remove sounds. - } - - @Override - public void setCustomNameNMS(String name) { - if (name != null && name.length() > 300) { - name = name.substring(0, 300); - } - super.setCustomName(name); - super.setCustomNameVisible(name != null && !name.isEmpty()); - } - - @Override - public String getCustomNameNMS() { - return super.getCustomName(); - } - - @Override - public void setLockTick(boolean lock) { - lockTick = lock; - } - - @Override - public void die() { - setLockTick(false); - super.die(); - } - - @Override - public CraftEntity getBukkitEntity() { - if (super.bukkitEntity == null) { - this.bukkitEntity = new CraftNMSArmorStand(this.world.getServer(), this); - } - return this.bukkitEntity; - } - - @Override - public void killEntityNMS() { - die(); - } - - @Override - public void setLocationNMS(double x, double y, double z) { - super.setPosition(x, y, z); - - if (Configuration.preciseHologramMovement) { - // Send a packet near to update the position. - PacketPlayOutEntityTeleport teleportPacket = new PacketPlayOutEntityTeleport( - getIdNMS(), - MathHelper.floor(this.locX * 32.0D), - MathHelper.floor(this.locY * 32.0D), - MathHelper.floor(this.locZ * 32.0D), - (byte) (int) (this.yaw * 256.0F / 360.0F), - (byte) (int) (this.pitch * 256.0F / 360.0F), - this.onGround - ); - - for (Object obj : this.world.players) { - if (obj instanceof EntityPlayer) { - EntityPlayer nmsPlayer = (EntityPlayer) obj; - - double distanceSquared = Utils.square(nmsPlayer.locX - this.locX) + Utils.square(nmsPlayer.locZ - this.locZ); - if (distanceSquared < 8192 && nmsPlayer.playerConnection != null) { - nmsPlayer.playerConnection.sendPacket(teleportPacket); - } - } - } - } - } - - @Override - public boolean isDeadNMS() { - return this.dead; - } - - @Override - public int getIdNMS() { - return super.getId(); // Return the real ID without checking the stack trace. - } - - @Override - public HologramLine getHologramLine() { - return parentPiece; - } - - @Override - public org.bukkit.entity.Entity getBukkitEntityNMS() { - return getBukkitEntity(); - } -} \ No newline at end of file +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_8_R2; + +import org.bukkit.craftbukkit.v1_8_R2.entity.CraftEntity; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; +import com.gmail.filoghost.holographicdisplays.disk.Configuration; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.Utils; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectMethod; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectionUtils; + +import net.minecraft.server.v1_8_R2.AxisAlignedBB; +import net.minecraft.server.v1_8_R2.DamageSource; +import net.minecraft.server.v1_8_R2.EntityArmorStand; +import net.minecraft.server.v1_8_R2.EntityHuman; +import net.minecraft.server.v1_8_R2.EntityPlayer; +import net.minecraft.server.v1_8_R2.ItemStack; +import net.minecraft.server.v1_8_R2.MathHelper; +import net.minecraft.server.v1_8_R2.NBTTagCompound; +import net.minecraft.server.v1_8_R2.PacketPlayOutEntityTeleport; +import net.minecraft.server.v1_8_R2.Vec3D; +import net.minecraft.server.v1_8_R2.World; + +public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorStand { + + private static final ReflectField DISABLED_SLOTS_FIELD = new ReflectField(EntityArmorStand.class, "bi"); + private static final ReflectMethod SET_MARKER_METHOD = new ReflectMethod(EntityArmorStand.class, "n", boolean.class); + + private boolean lockTick; + private HologramLine parentPiece; + + public EntityNMSArmorStand(World world, HologramLine parentPiece) { + super(world); + setInvisible(true); + setSmall(true); + setArms(false); + setGravity(true); + setBasePlate(true); + try { + SET_MARKER_METHOD.invoke(this, true); + } catch (Exception e) { + ConsoleLogger.logDebugException(e); + // It will still work, but the offset will be wrong. + } + this.parentPiece = parentPiece; + try { + DISABLED_SLOTS_FIELD.set(this, Integer.MAX_VALUE); + } catch (Exception e) { + // There's still the overridden method. + } + forceSetBoundingBox(new NullBoundingBox()); + } + + + @Override + public void b(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public boolean c(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public boolean d(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public void e(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + + @Override + public boolean isInvulnerable(DamageSource source) { + /* + * The field Entity.invulnerable is private. + * It's only used while saving NBTTags, but since the entity would be killed + * on chunk unload, we prefer to override isInvulnerable(). + */ + return true; + } + + @Override + public void setCustomName(String customName) { + // Locks the custom name. + } + + @Override + public void setCustomNameVisible(boolean visible) { + // Locks the custom name. + } + + @Override + public boolean a(EntityHuman human, Vec3D vec3d) { + // Prevent stand being equipped + return true; + } + + @Override + public boolean d(int i, ItemStack item) { + // Prevent stand being equipped + return false; + } + + @Override + public void setEquipment(int i, ItemStack item) { + // Prevent stand being equipped + } + + @Override + public void a(AxisAlignedBB boundingBox) { + // Do not change it! + } + + public void forceSetBoundingBox(AxisAlignedBB boundingBox) { + super.a(boundingBox); + } + + @Override + public int getId() { + if (Configuration.preciseHologramMovement) { + StackTraceElement element = ReflectionUtils.getStackTraceElement(2); + if (element != null && element.getFileName() != null && element.getFileName().equals("EntityTrackerEntry.java") && element.getLineNumber() > 137 && element.getLineNumber() < 147) { + // Then this method is being called when creating a new packet, we return a fake ID! + return -1; + } + } + + return super.getId(); + } + + @Override + public void t_() { + if (!lockTick) { + super.t_(); + } + } + + @Override + public void makeSound(String sound, float f1, float f2) { + // Remove sounds. + } + + @Override + public void setCustomNameNMS(String name) { + if (name != null && name.length() > 300) { + name = name.substring(0, 300); + } + super.setCustomName(name); + super.setCustomNameVisible(name != null && !name.isEmpty()); + } + + @Override + public String getCustomNameNMS() { + return super.getCustomName(); + } + + @Override + public void setLockTick(boolean lock) { + lockTick = lock; + } + + @Override + public void die() { + setLockTick(false); + super.die(); + } + + @Override + public CraftEntity getBukkitEntity() { + if (super.bukkitEntity == null) { + this.bukkitEntity = new CraftNMSArmorStand(this.world.getServer(), this); + } + return this.bukkitEntity; + } + + @Override + public void killEntityNMS() { + die(); + } + + @Override + public void setLocationNMS(double x, double y, double z) { + super.setPosition(x, y, z); + + if (Configuration.preciseHologramMovement) { + // Send a packet near to update the position. + PacketPlayOutEntityTeleport teleportPacket = new PacketPlayOutEntityTeleport( + getIdNMS(), + MathHelper.floor(this.locX * 32.0D), + MathHelper.floor(this.locY * 32.0D), + MathHelper.floor(this.locZ * 32.0D), + (byte) (int) (this.yaw * 256.0F / 360.0F), + (byte) (int) (this.pitch * 256.0F / 360.0F), + this.onGround + ); + + for (Object obj : this.world.players) { + if (obj instanceof EntityPlayer) { + EntityPlayer nmsPlayer = (EntityPlayer) obj; + + double distanceSquared = Utils.square(nmsPlayer.locX - this.locX) + Utils.square(nmsPlayer.locZ - this.locZ); + if (distanceSquared < 8192 && nmsPlayer.playerConnection != null) { + nmsPlayer.playerConnection.sendPacket(teleportPacket); + } + } + } + } + } + + @Override + public boolean isDeadNMS() { + return this.dead; + } + + @Override + public int getIdNMS() { + return super.getId(); // Return the real ID without checking the stack trace. + } + + @Override + public HologramLine getHologramLine() { + return parentPiece; + } + + @Override + public org.bukkit.entity.Entity getBukkitEntityNMS() { + return getBukkitEntity(); + } +} diff --git a/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/EntityNMSItem.java b/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/EntityNMSItem.java index 3ca0d33b..614e7427 100644 --- a/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/EntityNMSItem.java +++ b/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/EntityNMSItem.java @@ -1,232 +1,246 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_8_R2; - -import org.bukkit.craftbukkit.v1_8_R2.entity.CraftEntity; -import org.bukkit.craftbukkit.v1_8_R2.inventory.CraftItemStack; -import org.bukkit.entity.Player; - -import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.ItemUtils; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; - -import net.minecraft.server.v1_8_R2.AxisAlignedBB; -import net.minecraft.server.v1_8_R2.Blocks; -import net.minecraft.server.v1_8_R2.DamageSource; -import net.minecraft.server.v1_8_R2.Entity; -import net.minecraft.server.v1_8_R2.EntityHuman; -import net.minecraft.server.v1_8_R2.EntityItem; -import net.minecraft.server.v1_8_R2.EntityPlayer; -import net.minecraft.server.v1_8_R2.ItemStack; -import net.minecraft.server.v1_8_R2.NBTTagCompound; -import net.minecraft.server.v1_8_R2.NBTTagList; -import net.minecraft.server.v1_8_R2.NBTTagString; -import net.minecraft.server.v1_8_R2.World; - -public class EntityNMSItem extends EntityItem implements NMSItem { - - private static final ReflectField RIDER_PITCH_DELTA = new ReflectField(Entity.class, "ar"); - private static final ReflectField RIDER_YAW_DELTA = new ReflectField(Entity.class, "as"); - - private boolean lockTick; - private ItemLine parentPiece; - private ItemPickupManager itemPickupManager; - - public EntityNMSItem(World world, ItemLine piece, ItemPickupManager itemPickupManager) { - super(world); - super.pickupDelay = Integer.MAX_VALUE; - this.parentPiece = piece; - this.itemPickupManager = itemPickupManager; - forceSetBoundingBox(new NullBoundingBox()); - } - - @Override - public void t_() { - - // So it won't get removed. - ticksLived = 0; - - if (!lockTick) { - super.t_(); - } - } - - // Method called when a player is near. - @Override - public void d(EntityHuman human) { - - if (human.locY < this.locY - 1.5 || human.locY > this.locY + 1.0) { - // Too low or too high, it's a bit weird. - return; - } - - if (parentPiece.getPickupHandler() != null && human instanceof EntityPlayer) { - itemPickupManager.handleItemLinePickup((Player) human.getBukkitEntity(), parentPiece.getPickupHandler(), parentPiece.getParent()); - // It is never added to the inventory. - } - } - - @Override - public void b(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public boolean c(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public boolean d(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public void e(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public boolean isInvulnerable(DamageSource source) { - /* - * The field Entity.invulnerable is private. - * It's only used while saving NBTTags, but since the entity would be killed - * on chunk unload, we prefer to override isInvulnerable(). - */ - return true; - } - - @Override - public void a(AxisAlignedBB boundingBox) { - // Do not change it! - } - - public void forceSetBoundingBox(AxisAlignedBB boundingBox) { - super.a(boundingBox); - } - - @Override - public void inactiveTick() { - // Check inactive ticks. - - if (!lockTick) { - super.inactiveTick(); - } - } - - @Override - public void setLockTick(boolean lock) { - lockTick = lock; - } - - @Override - public void die() { - setLockTick(false); - super.die(); - } - - @Override - public CraftEntity getBukkitEntity() { - if (super.bukkitEntity == null) { - this.bukkitEntity = new CraftNMSItem(this.world.getServer(), this); - } - return this.bukkitEntity; - } - - @Override - public boolean isDeadNMS() { - return this.dead; - } - - @Override - public void killEntityNMS() { - die(); - } - - @Override - public void setLocationNMS(double x, double y, double z) { - super.setPosition(x, y, z); - } - - @Override - public void setItemStackNMS(org.bukkit.inventory.ItemStack stack) { - ItemStack newItem = CraftItemStack.asNMSCopy(stack); - - if (newItem == null) { - newItem = new ItemStack(Blocks.BEDROCK); - } - - if (newItem.getTag() == null) { - newItem.setTag(new NBTTagCompound()); - } - NBTTagCompound display = newItem.getTag().getCompound("display"); - - if (!newItem.getTag().hasKey("display")) { - newItem.getTag().set("display", display); - } - - NBTTagList tagList = new NBTTagList(); - tagList.add(new NBTTagString(ItemUtils.ANTISTACK_LORE)); // Antistack lore - - display.set("Lore", tagList); - newItem.count = 0; - setItemStack(newItem); - } - - @Override - public int getIdNMS() { - return this.getId(); - } - - @Override - public ItemLine getHologramLine() { - return parentPiece; - } - - @Override - public void allowPickup(boolean pickup) { - if (pickup) { - super.pickupDelay = 0; - } else { - super.pickupDelay = Integer.MAX_VALUE; - } - } - - @Override - public org.bukkit.entity.Entity getBukkitEntityNMS() { - return getBukkitEntity(); - } - - @Override - public void setPassengerOfNMS(NMSEntityBase vehicleBase) { - if (vehicleBase == null || !(vehicleBase instanceof Entity)) { - // It should never dismount - return; - } - - Entity entity = (Entity) vehicleBase; - - try { - RIDER_PITCH_DELTA.set(this, 0.0); - RIDER_YAW_DELTA.set(this, 0.0); - } catch (Exception ex) { - ConsoleLogger.logDebugException(ex); - } - - if (this.vehicle != null) { - this.vehicle.passenger = null; - } - - this.vehicle = entity; - entity.passenger = this; - } - - @Override - public Object getRawItemStack() { - return super.getItemStack(); - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_8_R2; + +import org.bukkit.craftbukkit.v1_8_R2.entity.CraftEntity; +import org.bukkit.craftbukkit.v1_8_R2.inventory.CraftItemStack; +import org.bukkit.entity.Player; + +import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.ItemUtils; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; + +import net.minecraft.server.v1_8_R2.AxisAlignedBB; +import net.minecraft.server.v1_8_R2.Blocks; +import net.minecraft.server.v1_8_R2.DamageSource; +import net.minecraft.server.v1_8_R2.Entity; +import net.minecraft.server.v1_8_R2.EntityHuman; +import net.minecraft.server.v1_8_R2.EntityItem; +import net.minecraft.server.v1_8_R2.EntityPlayer; +import net.minecraft.server.v1_8_R2.ItemStack; +import net.minecraft.server.v1_8_R2.NBTTagCompound; +import net.minecraft.server.v1_8_R2.NBTTagList; +import net.minecraft.server.v1_8_R2.NBTTagString; +import net.minecraft.server.v1_8_R2.World; + +public class EntityNMSItem extends EntityItem implements NMSItem { + + private static final ReflectField RIDER_PITCH_DELTA = new ReflectField(Entity.class, "ar"); + private static final ReflectField RIDER_YAW_DELTA = new ReflectField(Entity.class, "as"); + + private boolean lockTick; + private ItemLine parentPiece; + private ItemPickupManager itemPickupManager; + + public EntityNMSItem(World world, ItemLine piece, ItemPickupManager itemPickupManager) { + super(world); + super.pickupDelay = Integer.MAX_VALUE; + this.parentPiece = piece; + this.itemPickupManager = itemPickupManager; + forceSetBoundingBox(new NullBoundingBox()); + } + + @Override + public void t_() { + + // So it won't get removed. + ticksLived = 0; + + if (!lockTick) { + super.t_(); + } + } + + // Method called when a player is near. + @Override + public void d(EntityHuman human) { + + if (human.locY < this.locY - 1.5 || human.locY > this.locY + 1.0) { + // Too low or too high, it's a bit weird. + return; + } + + if (parentPiece.getPickupHandler() != null && human instanceof EntityPlayer) { + itemPickupManager.handleItemLinePickup((Player) human.getBukkitEntity(), parentPiece.getPickupHandler(), parentPiece.getParent()); + // It is never added to the inventory. + } + } + + @Override + public void b(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public boolean c(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public boolean d(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public void e(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public boolean isInvulnerable(DamageSource source) { + /* + * The field Entity.invulnerable is private. + * It's only used while saving NBTTags, but since the entity would be killed + * on chunk unload, we prefer to override isInvulnerable(). + */ + return true; + } + + @Override + public void a(AxisAlignedBB boundingBox) { + // Do not change it! + } + + public void forceSetBoundingBox(AxisAlignedBB boundingBox) { + super.a(boundingBox); + } + + @Override + public void inactiveTick() { + // Check inactive ticks. + + if (!lockTick) { + super.inactiveTick(); + } + } + + @Override + public void setLockTick(boolean lock) { + lockTick = lock; + } + + @Override + public void die() { + setLockTick(false); + super.die(); + } + + @Override + public CraftEntity getBukkitEntity() { + if (super.bukkitEntity == null) { + this.bukkitEntity = new CraftNMSItem(this.world.getServer(), this); + } + return this.bukkitEntity; + } + + @Override + public boolean isDeadNMS() { + return this.dead; + } + + @Override + public void killEntityNMS() { + die(); + } + + @Override + public void setLocationNMS(double x, double y, double z) { + super.setPosition(x, y, z); + } + + @Override + public void setItemStackNMS(org.bukkit.inventory.ItemStack stack) { + ItemStack newItem = CraftItemStack.asNMSCopy(stack); + + if (newItem == null) { + newItem = new ItemStack(Blocks.BEDROCK); + } + + if (newItem.getTag() == null) { + newItem.setTag(new NBTTagCompound()); + } + NBTTagCompound display = newItem.getTag().getCompound("display"); + + if (!newItem.getTag().hasKey("display")) { + newItem.getTag().set("display", display); + } + + NBTTagList tagList = new NBTTagList(); + tagList.add(new NBTTagString(ItemUtils.ANTISTACK_LORE)); // Antistack lore + + display.set("Lore", tagList); + newItem.count = 0; + setItemStack(newItem); + } + + @Override + public int getIdNMS() { + return this.getId(); + } + + @Override + public ItemLine getHologramLine() { + return parentPiece; + } + + @Override + public void allowPickup(boolean pickup) { + if (pickup) { + super.pickupDelay = 0; + } else { + super.pickupDelay = Integer.MAX_VALUE; + } + } + + @Override + public org.bukkit.entity.Entity getBukkitEntityNMS() { + return getBukkitEntity(); + } + + @Override + public void setPassengerOfNMS(NMSEntityBase vehicleBase) { + if (vehicleBase == null || !(vehicleBase instanceof Entity)) { + // It should never dismount + return; + } + + Entity entity = (Entity) vehicleBase; + + try { + RIDER_PITCH_DELTA.set(this, 0.0); + RIDER_YAW_DELTA.set(this, 0.0); + } catch (Exception ex) { + ConsoleLogger.logDebugException(ex); + } + + if (this.vehicle != null) { + this.vehicle.passenger = null; + } + + this.vehicle = entity; + entity.passenger = this; + } + + @Override + public Object getRawItemStack() { + return super.getItemStack(); + } +} diff --git a/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/EntityNMSSlime.java b/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/EntityNMSSlime.java index 148f35b9..fc12f58e 100644 --- a/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/EntityNMSSlime.java +++ b/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/EntityNMSSlime.java @@ -1,194 +1,208 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_8_R2; - -import org.bukkit.Bukkit; -import org.bukkit.craftbukkit.v1_8_R2.entity.CraftEntity; -import org.bukkit.event.player.PlayerInteractEntityEvent; - -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSSlime; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; - -import net.minecraft.server.v1_8_R2.AxisAlignedBB; -import net.minecraft.server.v1_8_R2.DamageSource; -import net.minecraft.server.v1_8_R2.Entity; -import net.minecraft.server.v1_8_R2.EntityDamageSource; -import net.minecraft.server.v1_8_R2.EntityPlayer; -import net.minecraft.server.v1_8_R2.EntitySlime; -import net.minecraft.server.v1_8_R2.NBTTagCompound; -import net.minecraft.server.v1_8_R2.World; - -public class EntityNMSSlime extends EntitySlime implements NMSSlime { - - private static final ReflectField RIDER_PITCH_DELTA = new ReflectField(Entity.class, "ar"); - private static final ReflectField RIDER_YAW_DELTA = new ReflectField(Entity.class, "as"); - - private boolean lockTick; - private HologramLine parentPiece; - - public EntityNMSSlime(World world, HologramLine parentPiece) { - super(world); - super.persistent = true; - a(0.0F, 0.0F); - setSize(1); - setInvisible(true); - this.parentPiece = parentPiece; - forceSetBoundingBox(new NullBoundingBox()); - } - - @Override - public void a(AxisAlignedBB boundingBox) { - // Do not change it! - } - - public void forceSetBoundingBox(AxisAlignedBB boundingBox) { - super.a(boundingBox); - } - - @Override - public void t_() { - // Checks every 20 ticks. - if (ticksLived % 20 == 0) { - // The slime dies without a vehicle. - if (this.vehicle == null) { - die(); - } - } - - if (!lockTick) { - super.t_(); - } - } - - @Override - public void b(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public boolean c(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public boolean d(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public void e(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public boolean damageEntity(DamageSource damageSource, float amount) { - if (damageSource instanceof EntityDamageSource) { - EntityDamageSource entityDamageSource = (EntityDamageSource) damageSource; - if (entityDamageSource.getEntity() instanceof EntityPlayer) { - Bukkit.getPluginManager().callEvent(new PlayerInteractEntityEvent(((EntityPlayer) entityDamageSource.getEntity()).getBukkitEntity(), getBukkitEntity())); - } - } - return false; - } - - @Override - public boolean isInvulnerable(DamageSource source) { - /* - * The field Entity.invulnerable is private. - * It's only used while saving NBTTags, but since the entity would be killed - * on chunk unload, we prefer to override isInvulnerable(). - */ - return true; - } - - @Override - public void setCustomName(String customName) { - // Locks the custom name. - } - - @Override - public void setCustomNameVisible(boolean visible) { - // Locks the custom name. - } - - @Override - public void makeSound(String sound, float volume, float pitch) { - // Remove sounds. - } - - @Override - public void setLockTick(boolean lock) { - lockTick = lock; - } - - @Override - public void die() { - setLockTick(false); - super.die(); - } - - @Override - public CraftEntity getBukkitEntity() { - if (super.bukkitEntity == null) { - this.bukkitEntity = new CraftNMSSlime(this.world.getServer(), this); - } - return this.bukkitEntity; - } - - @Override - public boolean isDeadNMS() { - return super.dead; - } - - @Override - public void killEntityNMS() { - die(); - } - - @Override - public void setLocationNMS(double x, double y, double z) { - super.setPosition(x, y, z); - } - - @Override - public int getIdNMS() { - return this.getId(); - } - - @Override - public HologramLine getHologramLine() { - return parentPiece; - } - - @Override - public org.bukkit.entity.Entity getBukkitEntityNMS() { - return getBukkitEntity(); - } - - @Override - public void setPassengerOfNMS(NMSEntityBase vehicleBase) { - if (vehicleBase == null || !(vehicleBase instanceof Entity)) { - // It should never dismount - return; - } - - Entity entity = (Entity) vehicleBase; - - try { - RIDER_PITCH_DELTA.set(this, 0.0); - RIDER_YAW_DELTA.set(this, 0.0); - } catch (Exception ex) { - ConsoleLogger.logDebugException(ex); - } - - if (this.vehicle != null) { - this.vehicle.passenger = null; - } - - this.vehicle = entity; - entity.passenger = this; - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_8_R2; + +import org.bukkit.Bukkit; +import org.bukkit.craftbukkit.v1_8_R2.entity.CraftEntity; +import org.bukkit.event.player.PlayerInteractEntityEvent; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSSlime; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; + +import net.minecraft.server.v1_8_R2.AxisAlignedBB; +import net.minecraft.server.v1_8_R2.DamageSource; +import net.minecraft.server.v1_8_R2.Entity; +import net.minecraft.server.v1_8_R2.EntityDamageSource; +import net.minecraft.server.v1_8_R2.EntityPlayer; +import net.minecraft.server.v1_8_R2.EntitySlime; +import net.minecraft.server.v1_8_R2.NBTTagCompound; +import net.minecraft.server.v1_8_R2.World; + +public class EntityNMSSlime extends EntitySlime implements NMSSlime { + + private static final ReflectField RIDER_PITCH_DELTA = new ReflectField(Entity.class, "ar"); + private static final ReflectField RIDER_YAW_DELTA = new ReflectField(Entity.class, "as"); + + private boolean lockTick; + private HologramLine parentPiece; + + public EntityNMSSlime(World world, HologramLine parentPiece) { + super(world); + super.persistent = true; + a(0.0F, 0.0F); + setSize(1); + setInvisible(true); + this.parentPiece = parentPiece; + forceSetBoundingBox(new NullBoundingBox()); + } + + @Override + public void a(AxisAlignedBB boundingBox) { + // Do not change it! + } + + public void forceSetBoundingBox(AxisAlignedBB boundingBox) { + super.a(boundingBox); + } + + @Override + public void t_() { + // Checks every 20 ticks. + if (ticksLived % 20 == 0) { + // The slime dies without a vehicle. + if (this.vehicle == null) { + die(); + } + } + + if (!lockTick) { + super.t_(); + } + } + + @Override + public void b(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public boolean c(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public boolean d(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public void e(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public boolean damageEntity(DamageSource damageSource, float amount) { + if (damageSource instanceof EntityDamageSource) { + EntityDamageSource entityDamageSource = (EntityDamageSource) damageSource; + if (entityDamageSource.getEntity() instanceof EntityPlayer) { + Bukkit.getPluginManager().callEvent(new PlayerInteractEntityEvent(((EntityPlayer) entityDamageSource.getEntity()).getBukkitEntity(), getBukkitEntity())); + } + } + return false; + } + + @Override + public boolean isInvulnerable(DamageSource source) { + /* + * The field Entity.invulnerable is private. + * It's only used while saving NBTTags, but since the entity would be killed + * on chunk unload, we prefer to override isInvulnerable(). + */ + return true; + } + + @Override + public void setCustomName(String customName) { + // Locks the custom name. + } + + @Override + public void setCustomNameVisible(boolean visible) { + // Locks the custom name. + } + + @Override + public void makeSound(String sound, float volume, float pitch) { + // Remove sounds. + } + + @Override + public void setLockTick(boolean lock) { + lockTick = lock; + } + + @Override + public void die() { + setLockTick(false); + super.die(); + } + + @Override + public CraftEntity getBukkitEntity() { + if (super.bukkitEntity == null) { + this.bukkitEntity = new CraftNMSSlime(this.world.getServer(), this); + } + return this.bukkitEntity; + } + + @Override + public boolean isDeadNMS() { + return super.dead; + } + + @Override + public void killEntityNMS() { + die(); + } + + @Override + public void setLocationNMS(double x, double y, double z) { + super.setPosition(x, y, z); + } + + @Override + public int getIdNMS() { + return this.getId(); + } + + @Override + public HologramLine getHologramLine() { + return parentPiece; + } + + @Override + public org.bukkit.entity.Entity getBukkitEntityNMS() { + return getBukkitEntity(); + } + + @Override + public void setPassengerOfNMS(NMSEntityBase vehicleBase) { + if (vehicleBase == null || !(vehicleBase instanceof Entity)) { + // It should never dismount + return; + } + + Entity entity = (Entity) vehicleBase; + + try { + RIDER_PITCH_DELTA.set(this, 0.0); + RIDER_YAW_DELTA.set(this, 0.0); + } catch (Exception ex) { + ConsoleLogger.logDebugException(ex); + } + + if (this.vehicle != null) { + this.vehicle.passenger = null; + } + + this.vehicle = entity; + entity.passenger = this; + } +} diff --git a/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/NmsManagerImpl.java b/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/NmsManagerImpl.java index d50c87e0..687355ac 100644 --- a/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/NmsManagerImpl.java +++ b/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/NmsManagerImpl.java @@ -1,129 +1,143 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_8_R2; - -import java.lang.reflect.Method; -import java.util.Map; - -import org.bukkit.Bukkit; -import org.bukkit.craftbukkit.v1_8_R2.CraftWorld; -import org.bukkit.craftbukkit.v1_8_R2.entity.CraftEntity; -import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason; -import org.bukkit.inventory.ItemStack; - -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; -import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.Validator; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; - -import net.minecraft.server.v1_8_R2.Entity; -import net.minecraft.server.v1_8_R2.EntityTypes; -import net.minecraft.server.v1_8_R2.MathHelper; -import net.minecraft.server.v1_8_R2.World; -import net.minecraft.server.v1_8_R2.WorldServer; - -public class NmsManagerImpl implements NMSManager { - - private static final ReflectField, String>> ENTITY_NAMES_BY_CLASS_FIELD = new ReflectField, String>>(EntityTypes.class, "d"); - private static final ReflectField, Integer>> ENTITY_IDS_BY_CLASS_FIELD = new ReflectField, Integer>>(EntityTypes.class, "f"); - - private Method validateEntityMethod; - - @Override - public void setup() throws Exception { - registerCustomEntity(EntityNMSArmorStand.class, "ArmorStand", 30); - registerCustomEntity(EntityNMSItem.class, "Item", 1); - registerCustomEntity(EntityNMSSlime.class, "Slime", 55); - - validateEntityMethod = World.class.getDeclaredMethod("a", Entity.class); - validateEntityMethod.setAccessible(true); - } - - public void registerCustomEntity(Class entityClass, String name, int id) throws Exception { - ENTITY_NAMES_BY_CLASS_FIELD.getStatic().put(entityClass, name); - ENTITY_IDS_BY_CLASS_FIELD.getStatic().put(entityClass, Integer.valueOf(id)); - } - - @Override - public NMSItem spawnNMSItem(org.bukkit.World bukkitWorld, double x, double y, double z, ItemLine parentPiece, ItemStack stack, ItemPickupManager itemPickupManager) { - WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); - EntityNMSItem customItem = new EntityNMSItem(nmsWorld, parentPiece, itemPickupManager); - customItem.setLocationNMS(x, y, z); - customItem.setItemStackNMS(stack); - if (!addEntityToWorld(nmsWorld, customItem)) { - ConsoleLogger.handleSpawnFail(parentPiece); - } - return customItem; - } - - @Override - public EntityNMSSlime spawnNMSSlime(org.bukkit.World bukkitWorld, double x, double y, double z, HologramLine parentPiece) { - WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); - EntityNMSSlime touchSlime = new EntityNMSSlime(nmsWorld, parentPiece); - touchSlime.setLocationNMS(x, y, z); - if (!addEntityToWorld(nmsWorld, touchSlime)) { - ConsoleLogger.handleSpawnFail(parentPiece); - } - return touchSlime; - } - - @Override - public NMSArmorStand spawnNMSArmorStand(org.bukkit.World world, double x, double y, double z, HologramLine parentPiece) { - WorldServer nmsWorld = ((CraftWorld) world).getHandle(); - EntityNMSArmorStand invisibleArmorStand = new EntityNMSArmorStand(nmsWorld, parentPiece); - invisibleArmorStand.setLocationNMS(x, y, z); - if (!addEntityToWorld(nmsWorld, invisibleArmorStand)) { - ConsoleLogger.handleSpawnFail(parentPiece); - } - return invisibleArmorStand; - } - - private boolean addEntityToWorld(WorldServer nmsWorld, Entity nmsEntity) { - Validator.isTrue(Bukkit.isPrimaryThread(), "Async entity add"); - - if (validateEntityMethod == null) { - return nmsWorld.addEntity(nmsEntity, SpawnReason.CUSTOM); - } - - final int chunkX = MathHelper.floor(nmsEntity.locX / 16.0); - final int chunkZ = MathHelper.floor(nmsEntity.locZ / 16.0); - - if (!nmsWorld.chunkProviderServer.isChunkLoaded(chunkX, chunkZ)) { - // This should never happen - nmsEntity.dead = true; - return false; - } - - nmsWorld.getChunkAt(chunkX, chunkZ).a(nmsEntity); - nmsWorld.entityList.add(nmsEntity); - - try { - validateEntityMethod.invoke(nmsWorld, nmsEntity); - } catch (Exception e) { - e.printStackTrace(); - return false; - } - return true; - } - - @Override - public boolean isNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { - return ((CraftEntity) bukkitEntity).getHandle() instanceof NMSEntityBase; - } - - @Override - public NMSEntityBase getNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { - - Entity nmsEntity = ((CraftEntity) bukkitEntity).getHandle(); - if (nmsEntity instanceof NMSEntityBase) { - return ((NMSEntityBase) nmsEntity); - } - - return null; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_8_R2; + +import java.lang.reflect.Method; +import java.util.Map; + +import org.bukkit.Bukkit; +import org.bukkit.craftbukkit.v1_8_R2.CraftWorld; +import org.bukkit.craftbukkit.v1_8_R2.entity.CraftEntity; +import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason; +import org.bukkit.inventory.ItemStack; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; +import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.Validator; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; + +import net.minecraft.server.v1_8_R2.Entity; +import net.minecraft.server.v1_8_R2.EntityTypes; +import net.minecraft.server.v1_8_R2.MathHelper; +import net.minecraft.server.v1_8_R2.World; +import net.minecraft.server.v1_8_R2.WorldServer; + +public class NmsManagerImpl implements NMSManager { + + private static final ReflectField, String>> ENTITY_NAMES_BY_CLASS_FIELD = new ReflectField, String>>(EntityTypes.class, "d"); + private static final ReflectField, Integer>> ENTITY_IDS_BY_CLASS_FIELD = new ReflectField, Integer>>(EntityTypes.class, "f"); + + private Method validateEntityMethod; + + @Override + public void setup() throws Exception { + registerCustomEntity(EntityNMSArmorStand.class, "ArmorStand", 30); + registerCustomEntity(EntityNMSItem.class, "Item", 1); + registerCustomEntity(EntityNMSSlime.class, "Slime", 55); + + validateEntityMethod = World.class.getDeclaredMethod("a", Entity.class); + validateEntityMethod.setAccessible(true); + } + + public void registerCustomEntity(Class entityClass, String name, int id) throws Exception { + ENTITY_NAMES_BY_CLASS_FIELD.getStatic().put(entityClass, name); + ENTITY_IDS_BY_CLASS_FIELD.getStatic().put(entityClass, Integer.valueOf(id)); + } + + @Override + public NMSItem spawnNMSItem(org.bukkit.World bukkitWorld, double x, double y, double z, ItemLine parentPiece, ItemStack stack, ItemPickupManager itemPickupManager) { + WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); + EntityNMSItem customItem = new EntityNMSItem(nmsWorld, parentPiece, itemPickupManager); + customItem.setLocationNMS(x, y, z); + customItem.setItemStackNMS(stack); + if (!addEntityToWorld(nmsWorld, customItem)) { + ConsoleLogger.handleSpawnFail(parentPiece); + } + return customItem; + } + + @Override + public EntityNMSSlime spawnNMSSlime(org.bukkit.World bukkitWorld, double x, double y, double z, HologramLine parentPiece) { + WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); + EntityNMSSlime touchSlime = new EntityNMSSlime(nmsWorld, parentPiece); + touchSlime.setLocationNMS(x, y, z); + if (!addEntityToWorld(nmsWorld, touchSlime)) { + ConsoleLogger.handleSpawnFail(parentPiece); + } + return touchSlime; + } + + @Override + public NMSArmorStand spawnNMSArmorStand(org.bukkit.World world, double x, double y, double z, HologramLine parentPiece) { + WorldServer nmsWorld = ((CraftWorld) world).getHandle(); + EntityNMSArmorStand invisibleArmorStand = new EntityNMSArmorStand(nmsWorld, parentPiece); + invisibleArmorStand.setLocationNMS(x, y, z); + if (!addEntityToWorld(nmsWorld, invisibleArmorStand)) { + ConsoleLogger.handleSpawnFail(parentPiece); + } + return invisibleArmorStand; + } + + private boolean addEntityToWorld(WorldServer nmsWorld, Entity nmsEntity) { + Validator.isTrue(Bukkit.isPrimaryThread(), "Async entity add"); + + if (validateEntityMethod == null) { + return nmsWorld.addEntity(nmsEntity, SpawnReason.CUSTOM); + } + + final int chunkX = MathHelper.floor(nmsEntity.locX / 16.0); + final int chunkZ = MathHelper.floor(nmsEntity.locZ / 16.0); + + if (!nmsWorld.chunkProviderServer.isChunkLoaded(chunkX, chunkZ)) { + // This should never happen + nmsEntity.dead = true; + return false; + } + + nmsWorld.getChunkAt(chunkX, chunkZ).a(nmsEntity); + nmsWorld.entityList.add(nmsEntity); + + try { + validateEntityMethod.invoke(nmsWorld, nmsEntity); + } catch (Exception e) { + e.printStackTrace(); + return false; + } + return true; + } + + @Override + public boolean isNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { + return ((CraftEntity) bukkitEntity).getHandle() instanceof NMSEntityBase; + } + + @Override + public NMSEntityBase getNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { + + Entity nmsEntity = ((CraftEntity) bukkitEntity).getHandle(); + if (nmsEntity instanceof NMSEntityBase) { + return ((NMSEntityBase) nmsEntity); + } + + return null; + } + +} diff --git a/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/NullBoundingBox.java b/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/NullBoundingBox.java index 80833d52..a8b3da47 100644 --- a/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/NullBoundingBox.java +++ b/NMS/v1_8_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R2/NullBoundingBox.java @@ -1,75 +1,89 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_8_R2; - -import net.minecraft.server.v1_8_R2.AxisAlignedBB; -import net.minecraft.server.v1_8_R2.MovingObjectPosition; -import net.minecraft.server.v1_8_R2.Vec3D; - -public class NullBoundingBox extends AxisAlignedBB { - - public NullBoundingBox() { - super(0, 0, 0, 0, 0, 0); - } - - @Override - public double a() { - return 0.0; - } - - @Override - public double a(AxisAlignedBB arg0, double arg1) { - return 0.0; - } - - @Override - public AxisAlignedBB a(AxisAlignedBB arg0) { - return this; - } - - @Override - public AxisAlignedBB a(double arg0, double arg1, double arg2) { - return this; - } - - @Override - public MovingObjectPosition a(Vec3D arg0, Vec3D arg1) { - return super.a(arg0, arg1); - } - - @Override - public boolean a(Vec3D arg0) { - return false; - } - - @Override - public double b(AxisAlignedBB arg0, double arg1) { - return 0.0; - } - - @Override - public boolean b(AxisAlignedBB arg0) { - return false; - } - - @Override - public double c(AxisAlignedBB arg0, double arg1) { - return 0.0; - } - - @Override - public AxisAlignedBB c(double arg0, double arg1, double arg2) { - return this; - } - - @Override - public AxisAlignedBB grow(double arg0, double arg1, double arg2) { - return this; - } - - @Override - public AxisAlignedBB shrink(double arg0, double arg1, double arg2) { - return this; - } - - - -} \ No newline at end of file +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_8_R2; + +import net.minecraft.server.v1_8_R2.AxisAlignedBB; +import net.minecraft.server.v1_8_R2.MovingObjectPosition; +import net.minecraft.server.v1_8_R2.Vec3D; + +public class NullBoundingBox extends AxisAlignedBB { + + public NullBoundingBox() { + super(0, 0, 0, 0, 0, 0); + } + + @Override + public double a() { + return 0.0; + } + + @Override + public double a(AxisAlignedBB arg0, double arg1) { + return 0.0; + } + + @Override + public AxisAlignedBB a(AxisAlignedBB arg0) { + return this; + } + + @Override + public AxisAlignedBB a(double arg0, double arg1, double arg2) { + return this; + } + + @Override + public MovingObjectPosition a(Vec3D arg0, Vec3D arg1) { + return super.a(arg0, arg1); + } + + @Override + public boolean a(Vec3D arg0) { + return false; + } + + @Override + public double b(AxisAlignedBB arg0, double arg1) { + return 0.0; + } + + @Override + public boolean b(AxisAlignedBB arg0) { + return false; + } + + @Override + public double c(AxisAlignedBB arg0, double arg1) { + return 0.0; + } + + @Override + public AxisAlignedBB c(double arg0, double arg1, double arg2) { + return this; + } + + @Override + public AxisAlignedBB grow(double arg0, double arg1, double arg2) { + return this; + } + + @Override + public AxisAlignedBB shrink(double arg0, double arg1, double arg2) { + return this; + } + + + +} diff --git a/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/CraftNMSArmorStand.java b/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/CraftNMSArmorStand.java index 521b4b21..d4d83d8c 100644 --- a/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/CraftNMSArmorStand.java +++ b/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/CraftNMSArmorStand.java @@ -1,67 +1,81 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_8_R3; - -import java.util.Collection; - -import org.bukkit.EntityEffect; -import org.bukkit.Location; -import org.bukkit.craftbukkit.v1_8_R3.CraftServer; -import org.bukkit.craftbukkit.v1_8_R3.entity.CraftArmorStand; -import org.bukkit.entity.Entity; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -import org.bukkit.inventory.ItemStack; -import org.bukkit.potion.PotionEffect; -import org.bukkit.util.EulerAngle; -import org.bukkit.util.Vector; - -public class CraftNMSArmorStand extends CraftArmorStand { - - public CraftNMSArmorStand(CraftServer server, EntityNMSArmorStand entity) { - super(server, entity); - } - - // Disallow all the bukkit methods. - - @Override - public void remove() { - // Cannot be removed, this is the most important to override. - } - - // Methods from Armor stand class - @Override public void setArms(boolean arms) { } - @Override public void setBasePlate(boolean basePlate) { } - @Override public void setBodyPose(EulerAngle pose) { } - @Override public void setBoots(ItemStack item) { } - @Override public void setChestplate(ItemStack item) { } - @Override public void setGravity(boolean gravity) { } - @Override public void setHeadPose(EulerAngle pose) { } - @Override public void setHelmet(ItemStack item) { } - @Override public void setItemInHand(ItemStack item) { } - @Override public void setLeftArmPose(EulerAngle pose) { } - @Override public void setLeftLegPose(EulerAngle pose) { } - @Override public void setLeggings(ItemStack item) { } - @Override public void setRightArmPose(EulerAngle pose) { } - @Override public void setRightLegPose(EulerAngle pose) { } - @Override public void setSmall(boolean small) { } - @Override public void setVisible(boolean visible) { } - - // Methods from LivingEntity class - @Override public boolean addPotionEffect(PotionEffect effect) { return false; } - @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } - @Override public boolean addPotionEffects(Collection effects) { return false; } - @Override public void setRemoveWhenFarAway(boolean remove) { } - - // Methods from Entity - @Override public void setVelocity(Vector vel) { } - @Override public boolean teleport(Location loc) { return false; } - @Override public boolean teleport(Entity entity) { return false; } - @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } - @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } - @Override public void setFireTicks(int ticks) { } - @Override public boolean setPassenger(Entity entity) { return false; } - @Override public boolean eject() { return false; } - @Override public boolean leaveVehicle() { return false; } - @Override public void playEffect(EntityEffect effect) { } - @Override public void setCustomName(String name) { } - @Override public void setCustomNameVisible(boolean flag) { } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_8_R3; + +import java.util.Collection; + +import org.bukkit.EntityEffect; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_8_R3.CraftServer; +import org.bukkit.craftbukkit.v1_8_R3.entity.CraftArmorStand; +import org.bukkit.entity.Entity; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import org.bukkit.inventory.ItemStack; +import org.bukkit.potion.PotionEffect; +import org.bukkit.util.EulerAngle; +import org.bukkit.util.Vector; + +public class CraftNMSArmorStand extends CraftArmorStand { + + public CraftNMSArmorStand(CraftServer server, EntityNMSArmorStand entity) { + super(server, entity); + } + + // Disallow all the bukkit methods. + + @Override + public void remove() { + // Cannot be removed, this is the most important to override. + } + + // Methods from Armor stand class + @Override public void setArms(boolean arms) { } + @Override public void setBasePlate(boolean basePlate) { } + @Override public void setBodyPose(EulerAngle pose) { } + @Override public void setBoots(ItemStack item) { } + @Override public void setChestplate(ItemStack item) { } + @Override public void setGravity(boolean gravity) { } + @Override public void setHeadPose(EulerAngle pose) { } + @Override public void setHelmet(ItemStack item) { } + @Override public void setItemInHand(ItemStack item) { } + @Override public void setLeftArmPose(EulerAngle pose) { } + @Override public void setLeftLegPose(EulerAngle pose) { } + @Override public void setLeggings(ItemStack item) { } + @Override public void setRightArmPose(EulerAngle pose) { } + @Override public void setRightLegPose(EulerAngle pose) { } + @Override public void setSmall(boolean small) { } + @Override public void setVisible(boolean visible) { } + + // Methods from LivingEntity class + @Override public boolean addPotionEffect(PotionEffect effect) { return false; } + @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } + @Override public boolean addPotionEffects(Collection effects) { return false; } + @Override public void setRemoveWhenFarAway(boolean remove) { } + + // Methods from Entity + @Override public void setVelocity(Vector vel) { } + @Override public boolean teleport(Location loc) { return false; } + @Override public boolean teleport(Entity entity) { return false; } + @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } + @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } + @Override public void setFireTicks(int ticks) { } + @Override public boolean setPassenger(Entity entity) { return false; } + @Override public boolean eject() { return false; } + @Override public boolean leaveVehicle() { return false; } + @Override public void playEffect(EntityEffect effect) { } + @Override public void setCustomName(String name) { } + @Override public void setCustomNameVisible(boolean flag) { } + +} diff --git a/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/CraftNMSItem.java b/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/CraftNMSItem.java index 6eb6aeb3..69437772 100644 --- a/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/CraftNMSItem.java +++ b/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/CraftNMSItem.java @@ -1,42 +1,56 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_8_R3; - -import org.bukkit.EntityEffect; -import org.bukkit.Location; -import org.bukkit.craftbukkit.v1_8_R3.CraftServer; -import org.bukkit.craftbukkit.v1_8_R3.entity.CraftItem; -import org.bukkit.entity.Entity; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -import org.bukkit.inventory.ItemStack; -import org.bukkit.util.Vector; - -public class CraftNMSItem extends CraftItem { - - public CraftNMSItem(CraftServer server, EntityNMSItem entity) { - super(server, entity); - } - - // Disallow all the bukkit methods. - - @Override - public void remove() { - // Cannot be removed, this is the most important to override. - } - - // Methods from Entity - @Override public void setVelocity(Vector vel) { } - @Override public boolean teleport(Location loc) { return false; } - @Override public boolean teleport(Entity entity) { return false; } - @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } - @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } - @Override public void setFireTicks(int ticks) { } - @Override public boolean setPassenger(Entity entity) { return false; } - @Override public boolean eject() { return false; } - @Override public boolean leaveVehicle() { return false; } - @Override public void playEffect(EntityEffect effect) { } - @Override public void setCustomName(String name) { } - @Override public void setCustomNameVisible(boolean flag) { } - - // Methods from Item - @Override public void setItemStack(ItemStack stack) { } - @Override public void setPickupDelay(int delay) { } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_8_R3; + +import org.bukkit.EntityEffect; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_8_R3.CraftServer; +import org.bukkit.craftbukkit.v1_8_R3.entity.CraftItem; +import org.bukkit.entity.Entity; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import org.bukkit.inventory.ItemStack; +import org.bukkit.util.Vector; + +public class CraftNMSItem extends CraftItem { + + public CraftNMSItem(CraftServer server, EntityNMSItem entity) { + super(server, entity); + } + + // Disallow all the bukkit methods. + + @Override + public void remove() { + // Cannot be removed, this is the most important to override. + } + + // Methods from Entity + @Override public void setVelocity(Vector vel) { } + @Override public boolean teleport(Location loc) { return false; } + @Override public boolean teleport(Entity entity) { return false; } + @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } + @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } + @Override public void setFireTicks(int ticks) { } + @Override public boolean setPassenger(Entity entity) { return false; } + @Override public boolean eject() { return false; } + @Override public boolean leaveVehicle() { return false; } + @Override public void playEffect(EntityEffect effect) { } + @Override public void setCustomName(String name) { } + @Override public void setCustomNameVisible(boolean flag) { } + + // Methods from Item + @Override public void setItemStack(ItemStack stack) { } + @Override public void setPickupDelay(int delay) { } +} diff --git a/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/CraftNMSSlime.java b/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/CraftNMSSlime.java index b0f3bb28..0d9d671d 100644 --- a/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/CraftNMSSlime.java +++ b/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/CraftNMSSlime.java @@ -1,49 +1,63 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_8_R3; - -import java.util.Collection; - -import org.bukkit.EntityEffect; -import org.bukkit.Location; -import org.bukkit.craftbukkit.v1_8_R3.CraftServer; -import org.bukkit.craftbukkit.v1_8_R3.entity.CraftSlime; -import org.bukkit.entity.Entity; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -import org.bukkit.potion.PotionEffect; -import org.bukkit.util.Vector; - -public class CraftNMSSlime extends CraftSlime { - - public CraftNMSSlime(CraftServer server, EntityNMSSlime entity) { - super(server, entity); - } - - // Disallow all the bukkit methods. - - @Override - public void remove() { - // Cannot be removed, this is the most important to override. - } - - // Methods from LivingEntity class - @Override public boolean addPotionEffect(PotionEffect effect) { return false; } - @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } - @Override public boolean addPotionEffects(Collection effects) { return false; } - @Override public void setRemoveWhenFarAway(boolean remove) { } - - // Methods from Entity - @Override public void setVelocity(Vector vel) { } - @Override public boolean teleport(Location loc) { return false; } - @Override public boolean teleport(Entity entity) { return false; } - @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } - @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } - @Override public void setFireTicks(int ticks) { } - @Override public boolean setPassenger(Entity entity) { return false; } - @Override public boolean eject() { return false; } - @Override public boolean leaveVehicle() { return false; } - @Override public void playEffect(EntityEffect effect) { } - @Override public void setCustomName(String name) { } - @Override public void setCustomNameVisible(boolean flag) { } - - // Methods from Slime - @Override public void setSize(int size) { } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_8_R3; + +import java.util.Collection; + +import org.bukkit.EntityEffect; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_8_R3.CraftServer; +import org.bukkit.craftbukkit.v1_8_R3.entity.CraftSlime; +import org.bukkit.entity.Entity; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import org.bukkit.potion.PotionEffect; +import org.bukkit.util.Vector; + +public class CraftNMSSlime extends CraftSlime { + + public CraftNMSSlime(CraftServer server, EntityNMSSlime entity) { + super(server, entity); + } + + // Disallow all the bukkit methods. + + @Override + public void remove() { + // Cannot be removed, this is the most important to override. + } + + // Methods from LivingEntity class + @Override public boolean addPotionEffect(PotionEffect effect) { return false; } + @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } + @Override public boolean addPotionEffects(Collection effects) { return false; } + @Override public void setRemoveWhenFarAway(boolean remove) { } + + // Methods from Entity + @Override public void setVelocity(Vector vel) { } + @Override public boolean teleport(Location loc) { return false; } + @Override public boolean teleport(Entity entity) { return false; } + @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } + @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } + @Override public void setFireTicks(int ticks) { } + @Override public boolean setPassenger(Entity entity) { return false; } + @Override public boolean eject() { return false; } + @Override public boolean leaveVehicle() { return false; } + @Override public void playEffect(EntityEffect effect) { } + @Override public void setCustomName(String name) { } + @Override public void setCustomNameVisible(boolean flag) { } + + // Methods from Slime + @Override public void setSize(int size) { } +} diff --git a/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/EntityNMSArmorStand.java b/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/EntityNMSArmorStand.java index ab0821fd..ac0e3c6e 100644 --- a/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/EntityNMSArmorStand.java +++ b/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/EntityNMSArmorStand.java @@ -1,237 +1,251 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_8_R3; - -import org.bukkit.craftbukkit.v1_8_R3.entity.CraftEntity; - -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; -import com.gmail.filoghost.holographicdisplays.disk.Configuration; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.Utils; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectMethod; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectionUtils; - -import net.minecraft.server.v1_8_R3.AxisAlignedBB; -import net.minecraft.server.v1_8_R3.DamageSource; -import net.minecraft.server.v1_8_R3.EntityArmorStand; -import net.minecraft.server.v1_8_R3.EntityHuman; -import net.minecraft.server.v1_8_R3.EntityPlayer; -import net.minecraft.server.v1_8_R3.ItemStack; -import net.minecraft.server.v1_8_R3.MathHelper; -import net.minecraft.server.v1_8_R3.NBTTagCompound; -import net.minecraft.server.v1_8_R3.PacketPlayOutEntityTeleport; -import net.minecraft.server.v1_8_R3.Vec3D; -import net.minecraft.server.v1_8_R3.World; - -public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorStand { - - private static final ReflectField DISABLED_SLOTS_FIELD = new ReflectField(EntityArmorStand.class, "bi"); - private static final ReflectMethod SET_MARKER_METHOD = new ReflectMethod(EntityArmorStand.class, "n", boolean.class); - - private boolean lockTick; - private HologramLine parentPiece; - - public EntityNMSArmorStand(World world, HologramLine parentPiece) { - super(world); - setInvisible(true); - setSmall(true); - setArms(false); - setGravity(true); - setBasePlate(true); - try { - SET_MARKER_METHOD.invoke(this, true); - } catch (Exception e) { - ConsoleLogger.logDebugException(e); - // It will still work, but the offset will be wrong. - } - this.parentPiece = parentPiece; - try { - DISABLED_SLOTS_FIELD.set(this, Integer.MAX_VALUE); - } catch (Exception e) { - // There's still the overridden method. - } - forceSetBoundingBox(new NullBoundingBox()); - } - - - @Override - public void b(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public boolean c(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public boolean d(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public void e(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - - @Override - public boolean isInvulnerable(DamageSource source) { - /* - * The field Entity.invulnerable is private. - * It's only used while saving NBTTags, but since the entity would be killed - * on chunk unload, we prefer to override isInvulnerable(). - */ - return true; - } - - @Override - public void setCustomName(String customName) { - // Locks the custom name. - } - - @Override - public void setCustomNameVisible(boolean visible) { - // Locks the custom name. - } - - @Override - public boolean a(EntityHuman human, Vec3D vec3d) { - // Prevent stand being equipped - return true; - } - - @Override - public boolean d(int i, ItemStack item) { - // Prevent stand being equipped - return false; - } - - @Override - public void setEquipment(int i, ItemStack item) { - // Prevent stand being equipped - } - - @Override - public void a(AxisAlignedBB boundingBox) { - // Do not change it! - } - - public void forceSetBoundingBox(AxisAlignedBB boundingBox) { - super.a(boundingBox); - } - - @Override - public int getId() { - if (Configuration.preciseHologramMovement) { - StackTraceElement element = ReflectionUtils.getStackTraceElement(2); - if (element != null && element.getFileName() != null && element.getFileName().equals("EntityTrackerEntry.java") && element.getLineNumber() > 137 && element.getLineNumber() < 147) { - // Then this method is being called when creating a new packet, we return a fake ID! - return -1; - } - } - - return super.getId(); - } - - @Override - public void t_() { - if (!lockTick) { - super.t_(); - } - } - - @Override - public void makeSound(String sound, float f1, float f2) { - // Remove sounds. - } - - @Override - public void setCustomNameNMS(String name) { - if (name != null && name.length() > 300) { - name = name.substring(0, 300); - } - super.setCustomName(name); - super.setCustomNameVisible(name != null && !name.isEmpty()); - } - - @Override - public String getCustomNameNMS() { - return super.getCustomName(); - } - - @Override - public void setLockTick(boolean lock) { - lockTick = lock; - } - - @Override - public void die() { - setLockTick(false); - super.die(); - } - - @Override - public CraftEntity getBukkitEntity() { - if (super.bukkitEntity == null) { - this.bukkitEntity = new CraftNMSArmorStand(this.world.getServer(), this); - } - return this.bukkitEntity; - } - - @Override - public void killEntityNMS() { - die(); - } - - @Override - public void setLocationNMS(double x, double y, double z) { - super.setPosition(x, y, z); - - if (Configuration.preciseHologramMovement) { - // Send a packet near to update the position. - PacketPlayOutEntityTeleport teleportPacket = new PacketPlayOutEntityTeleport( - getIdNMS(), - MathHelper.floor(this.locX * 32.0D), - MathHelper.floor(this.locY * 32.0D), - MathHelper.floor(this.locZ * 32.0D), - (byte) (int) (this.yaw * 256.0F / 360.0F), - (byte) (int) (this.pitch * 256.0F / 360.0F), - this.onGround - ); - - for (Object obj : this.world.players) { - if (obj instanceof EntityPlayer) { - EntityPlayer nmsPlayer = (EntityPlayer) obj; - - double distanceSquared = Utils.square(nmsPlayer.locX - this.locX) + Utils.square(nmsPlayer.locZ - this.locZ); - if (distanceSquared < 8192 && nmsPlayer.playerConnection != null) { - nmsPlayer.playerConnection.sendPacket(teleportPacket); - } - } - } - } - } - - @Override - public boolean isDeadNMS() { - return this.dead; - } - - @Override - public int getIdNMS() { - return super.getId(); // Return the real ID without checking the stack trace. - } - - @Override - public HologramLine getHologramLine() { - return parentPiece; - } - - @Override - public org.bukkit.entity.Entity getBukkitEntityNMS() { - return getBukkitEntity(); - } -} \ No newline at end of file +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_8_R3; + +import org.bukkit.craftbukkit.v1_8_R3.entity.CraftEntity; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; +import com.gmail.filoghost.holographicdisplays.disk.Configuration; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.Utils; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectMethod; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectionUtils; + +import net.minecraft.server.v1_8_R3.AxisAlignedBB; +import net.minecraft.server.v1_8_R3.DamageSource; +import net.minecraft.server.v1_8_R3.EntityArmorStand; +import net.minecraft.server.v1_8_R3.EntityHuman; +import net.minecraft.server.v1_8_R3.EntityPlayer; +import net.minecraft.server.v1_8_R3.ItemStack; +import net.minecraft.server.v1_8_R3.MathHelper; +import net.minecraft.server.v1_8_R3.NBTTagCompound; +import net.minecraft.server.v1_8_R3.PacketPlayOutEntityTeleport; +import net.minecraft.server.v1_8_R3.Vec3D; +import net.minecraft.server.v1_8_R3.World; + +public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorStand { + + private static final ReflectField DISABLED_SLOTS_FIELD = new ReflectField(EntityArmorStand.class, "bi"); + private static final ReflectMethod SET_MARKER_METHOD = new ReflectMethod(EntityArmorStand.class, "n", boolean.class); + + private boolean lockTick; + private HologramLine parentPiece; + + public EntityNMSArmorStand(World world, HologramLine parentPiece) { + super(world); + setInvisible(true); + setSmall(true); + setArms(false); + setGravity(true); + setBasePlate(true); + try { + SET_MARKER_METHOD.invoke(this, true); + } catch (Exception e) { + ConsoleLogger.logDebugException(e); + // It will still work, but the offset will be wrong. + } + this.parentPiece = parentPiece; + try { + DISABLED_SLOTS_FIELD.set(this, Integer.MAX_VALUE); + } catch (Exception e) { + // There's still the overridden method. + } + forceSetBoundingBox(new NullBoundingBox()); + } + + + @Override + public void b(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public boolean c(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public boolean d(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public void e(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + + @Override + public boolean isInvulnerable(DamageSource source) { + /* + * The field Entity.invulnerable is private. + * It's only used while saving NBTTags, but since the entity would be killed + * on chunk unload, we prefer to override isInvulnerable(). + */ + return true; + } + + @Override + public void setCustomName(String customName) { + // Locks the custom name. + } + + @Override + public void setCustomNameVisible(boolean visible) { + // Locks the custom name. + } + + @Override + public boolean a(EntityHuman human, Vec3D vec3d) { + // Prevent stand being equipped + return true; + } + + @Override + public boolean d(int i, ItemStack item) { + // Prevent stand being equipped + return false; + } + + @Override + public void setEquipment(int i, ItemStack item) { + // Prevent stand being equipped + } + + @Override + public void a(AxisAlignedBB boundingBox) { + // Do not change it! + } + + public void forceSetBoundingBox(AxisAlignedBB boundingBox) { + super.a(boundingBox); + } + + @Override + public int getId() { + if (Configuration.preciseHologramMovement) { + StackTraceElement element = ReflectionUtils.getStackTraceElement(2); + if (element != null && element.getFileName() != null && element.getFileName().equals("EntityTrackerEntry.java") && element.getLineNumber() > 137 && element.getLineNumber() < 147) { + // Then this method is being called when creating a new packet, we return a fake ID! + return -1; + } + } + + return super.getId(); + } + + @Override + public void t_() { + if (!lockTick) { + super.t_(); + } + } + + @Override + public void makeSound(String sound, float f1, float f2) { + // Remove sounds. + } + + @Override + public void setCustomNameNMS(String name) { + if (name != null && name.length() > 300) { + name = name.substring(0, 300); + } + super.setCustomName(name); + super.setCustomNameVisible(name != null && !name.isEmpty()); + } + + @Override + public String getCustomNameNMS() { + return super.getCustomName(); + } + + @Override + public void setLockTick(boolean lock) { + lockTick = lock; + } + + @Override + public void die() { + setLockTick(false); + super.die(); + } + + @Override + public CraftEntity getBukkitEntity() { + if (super.bukkitEntity == null) { + this.bukkitEntity = new CraftNMSArmorStand(this.world.getServer(), this); + } + return this.bukkitEntity; + } + + @Override + public void killEntityNMS() { + die(); + } + + @Override + public void setLocationNMS(double x, double y, double z) { + super.setPosition(x, y, z); + + if (Configuration.preciseHologramMovement) { + // Send a packet near to update the position. + PacketPlayOutEntityTeleport teleportPacket = new PacketPlayOutEntityTeleport( + getIdNMS(), + MathHelper.floor(this.locX * 32.0D), + MathHelper.floor(this.locY * 32.0D), + MathHelper.floor(this.locZ * 32.0D), + (byte) (int) (this.yaw * 256.0F / 360.0F), + (byte) (int) (this.pitch * 256.0F / 360.0F), + this.onGround + ); + + for (Object obj : this.world.players) { + if (obj instanceof EntityPlayer) { + EntityPlayer nmsPlayer = (EntityPlayer) obj; + + double distanceSquared = Utils.square(nmsPlayer.locX - this.locX) + Utils.square(nmsPlayer.locZ - this.locZ); + if (distanceSquared < 8192 && nmsPlayer.playerConnection != null) { + nmsPlayer.playerConnection.sendPacket(teleportPacket); + } + } + } + } + } + + @Override + public boolean isDeadNMS() { + return this.dead; + } + + @Override + public int getIdNMS() { + return super.getId(); // Return the real ID without checking the stack trace. + } + + @Override + public HologramLine getHologramLine() { + return parentPiece; + } + + @Override + public org.bukkit.entity.Entity getBukkitEntityNMS() { + return getBukkitEntity(); + } +} diff --git a/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/EntityNMSItem.java b/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/EntityNMSItem.java index 74fbae79..5c8dac64 100644 --- a/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/EntityNMSItem.java +++ b/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/EntityNMSItem.java @@ -1,232 +1,246 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_8_R3; - -import org.bukkit.craftbukkit.v1_8_R3.entity.CraftEntity; -import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack; -import org.bukkit.entity.Player; - -import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.ItemUtils; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; - -import net.minecraft.server.v1_8_R3.AxisAlignedBB; -import net.minecraft.server.v1_8_R3.Blocks; -import net.minecraft.server.v1_8_R3.DamageSource; -import net.minecraft.server.v1_8_R3.Entity; -import net.minecraft.server.v1_8_R3.EntityHuman; -import net.minecraft.server.v1_8_R3.EntityItem; -import net.minecraft.server.v1_8_R3.EntityPlayer; -import net.minecraft.server.v1_8_R3.ItemStack; -import net.minecraft.server.v1_8_R3.NBTTagCompound; -import net.minecraft.server.v1_8_R3.NBTTagList; -import net.minecraft.server.v1_8_R3.NBTTagString; -import net.minecraft.server.v1_8_R3.World; - -public class EntityNMSItem extends EntityItem implements NMSItem { - - private static final ReflectField RIDER_PITCH_DELTA = new ReflectField(Entity.class, "ar"); - private static final ReflectField RIDER_YAW_DELTA = new ReflectField(Entity.class, "as"); - - private boolean lockTick; - private ItemLine parentPiece; - private ItemPickupManager itemPickupManager; - - public EntityNMSItem(World world, ItemLine piece, ItemPickupManager itemPickupManager) { - super(world); - super.pickupDelay = Integer.MAX_VALUE; - this.parentPiece = piece; - this.itemPickupManager = itemPickupManager; - forceSetBoundingBox(new NullBoundingBox()); - } - - @Override - public void t_() { - - // So it won't get removed. - ticksLived = 0; - - if (!lockTick) { - super.t_(); - } - } - - // Method called when a player is near. - @Override - public void d(EntityHuman human) { - - if (human.locY < this.locY - 1.5 || human.locY > this.locY + 1.0) { - // Too low or too high, it's a bit weird. - return; - } - - if (parentPiece.getPickupHandler() != null && human instanceof EntityPlayer) { - itemPickupManager.handleItemLinePickup((Player) human.getBukkitEntity(), parentPiece.getPickupHandler(), parentPiece.getParent()); - // It is never added to the inventory. - } - } - - @Override - public void b(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public boolean c(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public boolean d(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public void e(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public boolean isInvulnerable(DamageSource source) { - /* - * The field Entity.invulnerable is private. - * It's only used while saving NBTTags, but since the entity would be killed - * on chunk unload, we prefer to override isInvulnerable(). - */ - return true; - } - - @Override - public void a(AxisAlignedBB boundingBox) { - // Do not change it! - } - - public void forceSetBoundingBox(AxisAlignedBB boundingBox) { - super.a(boundingBox); - } - - @Override - public void inactiveTick() { - // Check inactive ticks. - - if (!lockTick) { - super.inactiveTick(); - } - } - - @Override - public void setLockTick(boolean lock) { - lockTick = lock; - } - - @Override - public void die() { - setLockTick(false); - super.die(); - } - - @Override - public CraftEntity getBukkitEntity() { - if (super.bukkitEntity == null) { - this.bukkitEntity = new CraftNMSItem(this.world.getServer(), this); - } - return this.bukkitEntity; - } - - @Override - public boolean isDeadNMS() { - return this.dead; - } - - @Override - public void killEntityNMS() { - die(); - } - - @Override - public void setLocationNMS(double x, double y, double z) { - super.setPosition(x, y, z); - } - - @Override - public void setItemStackNMS(org.bukkit.inventory.ItemStack stack) { - ItemStack newItem = CraftItemStack.asNMSCopy(stack); - - if (newItem == null) { - newItem = new ItemStack(Blocks.BEDROCK); - } - - if (newItem.getTag() == null) { - newItem.setTag(new NBTTagCompound()); - } - NBTTagCompound display = newItem.getTag().getCompound("display"); - - if (!newItem.getTag().hasKey("display")) { - newItem.getTag().set("display", display); - } - - NBTTagList tagList = new NBTTagList(); - tagList.add(new NBTTagString(ItemUtils.ANTISTACK_LORE)); // Antistack lore - - display.set("Lore", tagList); - newItem.count = 0; - setItemStack(newItem); - } - - @Override - public int getIdNMS() { - return this.getId(); - } - - @Override - public ItemLine getHologramLine() { - return parentPiece; - } - - @Override - public void allowPickup(boolean pickup) { - if (pickup) { - super.pickupDelay = 0; - } else { - super.pickupDelay = Integer.MAX_VALUE; - } - } - - @Override - public org.bukkit.entity.Entity getBukkitEntityNMS() { - return getBukkitEntity(); - } - - @Override - public void setPassengerOfNMS(NMSEntityBase vehicleBase) { - if (vehicleBase == null || !(vehicleBase instanceof Entity)) { - // It should never dismount - return; - } - - Entity entity = (Entity) vehicleBase; - - try { - RIDER_PITCH_DELTA.set(this, 0.0); - RIDER_YAW_DELTA.set(this, 0.0); - } catch (Exception ex) { - ConsoleLogger.logDebugException(ex); - } - - if (this.vehicle != null) { - this.vehicle.passenger = null; - } - - this.vehicle = entity; - entity.passenger = this; - } - - @Override - public Object getRawItemStack() { - return super.getItemStack(); - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_8_R3; + +import org.bukkit.craftbukkit.v1_8_R3.entity.CraftEntity; +import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack; +import org.bukkit.entity.Player; + +import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.ItemUtils; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; + +import net.minecraft.server.v1_8_R3.AxisAlignedBB; +import net.minecraft.server.v1_8_R3.Blocks; +import net.minecraft.server.v1_8_R3.DamageSource; +import net.minecraft.server.v1_8_R3.Entity; +import net.minecraft.server.v1_8_R3.EntityHuman; +import net.minecraft.server.v1_8_R3.EntityItem; +import net.minecraft.server.v1_8_R3.EntityPlayer; +import net.minecraft.server.v1_8_R3.ItemStack; +import net.minecraft.server.v1_8_R3.NBTTagCompound; +import net.minecraft.server.v1_8_R3.NBTTagList; +import net.minecraft.server.v1_8_R3.NBTTagString; +import net.minecraft.server.v1_8_R3.World; + +public class EntityNMSItem extends EntityItem implements NMSItem { + + private static final ReflectField RIDER_PITCH_DELTA = new ReflectField(Entity.class, "ar"); + private static final ReflectField RIDER_YAW_DELTA = new ReflectField(Entity.class, "as"); + + private boolean lockTick; + private ItemLine parentPiece; + private ItemPickupManager itemPickupManager; + + public EntityNMSItem(World world, ItemLine piece, ItemPickupManager itemPickupManager) { + super(world); + super.pickupDelay = Integer.MAX_VALUE; + this.parentPiece = piece; + this.itemPickupManager = itemPickupManager; + forceSetBoundingBox(new NullBoundingBox()); + } + + @Override + public void t_() { + + // So it won't get removed. + ticksLived = 0; + + if (!lockTick) { + super.t_(); + } + } + + // Method called when a player is near. + @Override + public void d(EntityHuman human) { + + if (human.locY < this.locY - 1.5 || human.locY > this.locY + 1.0) { + // Too low or too high, it's a bit weird. + return; + } + + if (parentPiece.getPickupHandler() != null && human instanceof EntityPlayer) { + itemPickupManager.handleItemLinePickup((Player) human.getBukkitEntity(), parentPiece.getPickupHandler(), parentPiece.getParent()); + // It is never added to the inventory. + } + } + + @Override + public void b(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public boolean c(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public boolean d(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public void e(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public boolean isInvulnerable(DamageSource source) { + /* + * The field Entity.invulnerable is private. + * It's only used while saving NBTTags, but since the entity would be killed + * on chunk unload, we prefer to override isInvulnerable(). + */ + return true; + } + + @Override + public void a(AxisAlignedBB boundingBox) { + // Do not change it! + } + + public void forceSetBoundingBox(AxisAlignedBB boundingBox) { + super.a(boundingBox); + } + + @Override + public void inactiveTick() { + // Check inactive ticks. + + if (!lockTick) { + super.inactiveTick(); + } + } + + @Override + public void setLockTick(boolean lock) { + lockTick = lock; + } + + @Override + public void die() { + setLockTick(false); + super.die(); + } + + @Override + public CraftEntity getBukkitEntity() { + if (super.bukkitEntity == null) { + this.bukkitEntity = new CraftNMSItem(this.world.getServer(), this); + } + return this.bukkitEntity; + } + + @Override + public boolean isDeadNMS() { + return this.dead; + } + + @Override + public void killEntityNMS() { + die(); + } + + @Override + public void setLocationNMS(double x, double y, double z) { + super.setPosition(x, y, z); + } + + @Override + public void setItemStackNMS(org.bukkit.inventory.ItemStack stack) { + ItemStack newItem = CraftItemStack.asNMSCopy(stack); + + if (newItem == null) { + newItem = new ItemStack(Blocks.BEDROCK); + } + + if (newItem.getTag() == null) { + newItem.setTag(new NBTTagCompound()); + } + NBTTagCompound display = newItem.getTag().getCompound("display"); + + if (!newItem.getTag().hasKey("display")) { + newItem.getTag().set("display", display); + } + + NBTTagList tagList = new NBTTagList(); + tagList.add(new NBTTagString(ItemUtils.ANTISTACK_LORE)); // Antistack lore + + display.set("Lore", tagList); + newItem.count = 0; + setItemStack(newItem); + } + + @Override + public int getIdNMS() { + return this.getId(); + } + + @Override + public ItemLine getHologramLine() { + return parentPiece; + } + + @Override + public void allowPickup(boolean pickup) { + if (pickup) { + super.pickupDelay = 0; + } else { + super.pickupDelay = Integer.MAX_VALUE; + } + } + + @Override + public org.bukkit.entity.Entity getBukkitEntityNMS() { + return getBukkitEntity(); + } + + @Override + public void setPassengerOfNMS(NMSEntityBase vehicleBase) { + if (vehicleBase == null || !(vehicleBase instanceof Entity)) { + // It should never dismount + return; + } + + Entity entity = (Entity) vehicleBase; + + try { + RIDER_PITCH_DELTA.set(this, 0.0); + RIDER_YAW_DELTA.set(this, 0.0); + } catch (Exception ex) { + ConsoleLogger.logDebugException(ex); + } + + if (this.vehicle != null) { + this.vehicle.passenger = null; + } + + this.vehicle = entity; + entity.passenger = this; + } + + @Override + public Object getRawItemStack() { + return super.getItemStack(); + } +} diff --git a/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/EntityNMSSlime.java b/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/EntityNMSSlime.java index 54c75888..1d434e75 100644 --- a/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/EntityNMSSlime.java +++ b/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/EntityNMSSlime.java @@ -1,194 +1,208 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_8_R3; - -import org.bukkit.Bukkit; -import org.bukkit.craftbukkit.v1_8_R3.entity.CraftEntity; -import org.bukkit.event.player.PlayerInteractEntityEvent; - -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSSlime; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; - -import net.minecraft.server.v1_8_R3.AxisAlignedBB; -import net.minecraft.server.v1_8_R3.DamageSource; -import net.minecraft.server.v1_8_R3.Entity; -import net.minecraft.server.v1_8_R3.EntityDamageSource; -import net.minecraft.server.v1_8_R3.EntityPlayer; -import net.minecraft.server.v1_8_R3.EntitySlime; -import net.minecraft.server.v1_8_R3.NBTTagCompound; -import net.minecraft.server.v1_8_R3.World; - -public class EntityNMSSlime extends EntitySlime implements NMSSlime { - - private static final ReflectField RIDER_PITCH_DELTA = new ReflectField(Entity.class, "ar"); - private static final ReflectField RIDER_YAW_DELTA = new ReflectField(Entity.class, "as"); - - private boolean lockTick; - private HologramLine parentPiece; - - public EntityNMSSlime(World world, HologramLine parentPiece) { - super(world); - super.persistent = true; - a(0.0F, 0.0F); - setSize(1); - setInvisible(true); - this.parentPiece = parentPiece; - forceSetBoundingBox(new NullBoundingBox()); - } - - @Override - public void a(AxisAlignedBB boundingBox) { - // Do not change it! - } - - public void forceSetBoundingBox(AxisAlignedBB boundingBox) { - super.a(boundingBox); - } - - @Override - public void t_() { - // Checks every 20 ticks. - if (ticksLived % 20 == 0) { - // The slime dies without a vehicle. - if (this.vehicle == null) { - die(); - } - } - - if (!lockTick) { - super.t_(); - } - } - - @Override - public void b(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public boolean c(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public boolean d(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public void e(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public boolean damageEntity(DamageSource damageSource, float amount) { - if (damageSource instanceof EntityDamageSource) { - EntityDamageSource entityDamageSource = (EntityDamageSource) damageSource; - if (entityDamageSource.getEntity() instanceof EntityPlayer) { - Bukkit.getPluginManager().callEvent(new PlayerInteractEntityEvent(((EntityPlayer) entityDamageSource.getEntity()).getBukkitEntity(), getBukkitEntity())); - } - } - return false; - } - - @Override - public boolean isInvulnerable(DamageSource source) { - /* - * The field Entity.invulnerable is private. - * It's only used while saving NBTTags, but since the entity would be killed - * on chunk unload, we prefer to override isInvulnerable(). - */ - return true; - } - - @Override - public void setCustomName(String customName) { - // Locks the custom name. - } - - @Override - public void setCustomNameVisible(boolean visible) { - // Locks the custom name. - } - - @Override - public void makeSound(String sound, float volume, float pitch) { - // Remove sounds. - } - - @Override - public void setLockTick(boolean lock) { - lockTick = lock; - } - - @Override - public void die() { - setLockTick(false); - super.die(); - } - - @Override - public CraftEntity getBukkitEntity() { - if (super.bukkitEntity == null) { - this.bukkitEntity = new CraftNMSSlime(this.world.getServer(), this); - } - return this.bukkitEntity; - } - - @Override - public boolean isDeadNMS() { - return super.dead; - } - - @Override - public void killEntityNMS() { - die(); - } - - @Override - public void setLocationNMS(double x, double y, double z) { - super.setPosition(x, y, z); - } - - @Override - public int getIdNMS() { - return this.getId(); - } - - @Override - public HologramLine getHologramLine() { - return parentPiece; - } - - @Override - public org.bukkit.entity.Entity getBukkitEntityNMS() { - return getBukkitEntity(); - } - - @Override - public void setPassengerOfNMS(NMSEntityBase vehicleBase) { - if (vehicleBase == null || !(vehicleBase instanceof Entity)) { - // It should never dismount - return; - } - - Entity entity = (Entity) vehicleBase; - - try { - RIDER_PITCH_DELTA.set(this, 0.0); - RIDER_YAW_DELTA.set(this, 0.0); - } catch (Exception ex) { - ConsoleLogger.logDebugException(ex); - } - - if (this.vehicle != null) { - this.vehicle.passenger = null; - } - - this.vehicle = entity; - entity.passenger = this; - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_8_R3; + +import org.bukkit.Bukkit; +import org.bukkit.craftbukkit.v1_8_R3.entity.CraftEntity; +import org.bukkit.event.player.PlayerInteractEntityEvent; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSSlime; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; + +import net.minecraft.server.v1_8_R3.AxisAlignedBB; +import net.minecraft.server.v1_8_R3.DamageSource; +import net.minecraft.server.v1_8_R3.Entity; +import net.minecraft.server.v1_8_R3.EntityDamageSource; +import net.minecraft.server.v1_8_R3.EntityPlayer; +import net.minecraft.server.v1_8_R3.EntitySlime; +import net.minecraft.server.v1_8_R3.NBTTagCompound; +import net.minecraft.server.v1_8_R3.World; + +public class EntityNMSSlime extends EntitySlime implements NMSSlime { + + private static final ReflectField RIDER_PITCH_DELTA = new ReflectField(Entity.class, "ar"); + private static final ReflectField RIDER_YAW_DELTA = new ReflectField(Entity.class, "as"); + + private boolean lockTick; + private HologramLine parentPiece; + + public EntityNMSSlime(World world, HologramLine parentPiece) { + super(world); + super.persistent = true; + a(0.0F, 0.0F); + setSize(1); + setInvisible(true); + this.parentPiece = parentPiece; + forceSetBoundingBox(new NullBoundingBox()); + } + + @Override + public void a(AxisAlignedBB boundingBox) { + // Do not change it! + } + + public void forceSetBoundingBox(AxisAlignedBB boundingBox) { + super.a(boundingBox); + } + + @Override + public void t_() { + // Checks every 20 ticks. + if (ticksLived % 20 == 0) { + // The slime dies without a vehicle. + if (this.vehicle == null) { + die(); + } + } + + if (!lockTick) { + super.t_(); + } + } + + @Override + public void b(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public boolean c(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public boolean d(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public void e(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public boolean damageEntity(DamageSource damageSource, float amount) { + if (damageSource instanceof EntityDamageSource) { + EntityDamageSource entityDamageSource = (EntityDamageSource) damageSource; + if (entityDamageSource.getEntity() instanceof EntityPlayer) { + Bukkit.getPluginManager().callEvent(new PlayerInteractEntityEvent(((EntityPlayer) entityDamageSource.getEntity()).getBukkitEntity(), getBukkitEntity())); + } + } + return false; + } + + @Override + public boolean isInvulnerable(DamageSource source) { + /* + * The field Entity.invulnerable is private. + * It's only used while saving NBTTags, but since the entity would be killed + * on chunk unload, we prefer to override isInvulnerable(). + */ + return true; + } + + @Override + public void setCustomName(String customName) { + // Locks the custom name. + } + + @Override + public void setCustomNameVisible(boolean visible) { + // Locks the custom name. + } + + @Override + public void makeSound(String sound, float volume, float pitch) { + // Remove sounds. + } + + @Override + public void setLockTick(boolean lock) { + lockTick = lock; + } + + @Override + public void die() { + setLockTick(false); + super.die(); + } + + @Override + public CraftEntity getBukkitEntity() { + if (super.bukkitEntity == null) { + this.bukkitEntity = new CraftNMSSlime(this.world.getServer(), this); + } + return this.bukkitEntity; + } + + @Override + public boolean isDeadNMS() { + return super.dead; + } + + @Override + public void killEntityNMS() { + die(); + } + + @Override + public void setLocationNMS(double x, double y, double z) { + super.setPosition(x, y, z); + } + + @Override + public int getIdNMS() { + return this.getId(); + } + + @Override + public HologramLine getHologramLine() { + return parentPiece; + } + + @Override + public org.bukkit.entity.Entity getBukkitEntityNMS() { + return getBukkitEntity(); + } + + @Override + public void setPassengerOfNMS(NMSEntityBase vehicleBase) { + if (vehicleBase == null || !(vehicleBase instanceof Entity)) { + // It should never dismount + return; + } + + Entity entity = (Entity) vehicleBase; + + try { + RIDER_PITCH_DELTA.set(this, 0.0); + RIDER_YAW_DELTA.set(this, 0.0); + } catch (Exception ex) { + ConsoleLogger.logDebugException(ex); + } + + if (this.vehicle != null) { + this.vehicle.passenger = null; + } + + this.vehicle = entity; + entity.passenger = this; + } +} diff --git a/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/NmsManagerImpl.java b/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/NmsManagerImpl.java index 7edd5f15..9d112970 100644 --- a/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/NmsManagerImpl.java +++ b/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/NmsManagerImpl.java @@ -1,129 +1,143 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_8_R3; - -import java.lang.reflect.Method; -import java.util.Map; - -import org.bukkit.Bukkit; -import org.bukkit.craftbukkit.v1_8_R3.CraftWorld; -import org.bukkit.craftbukkit.v1_8_R3.entity.CraftEntity; -import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason; -import org.bukkit.inventory.ItemStack; - -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; -import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.Validator; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; - -import net.minecraft.server.v1_8_R3.Entity; -import net.minecraft.server.v1_8_R3.EntityTypes; -import net.minecraft.server.v1_8_R3.MathHelper; -import net.minecraft.server.v1_8_R3.World; -import net.minecraft.server.v1_8_R3.WorldServer; - -public class NmsManagerImpl implements NMSManager { - - private static final ReflectField, String>> ENTITY_NAMES_BY_CLASS_FIELD = new ReflectField, String>>(EntityTypes.class, "d"); - private static final ReflectField, Integer>> ENTITY_IDS_BY_CLASS_FIELD = new ReflectField, Integer>>(EntityTypes.class, "f"); - - private Method validateEntityMethod; - - @Override - public void setup() throws Exception { - registerCustomEntity(EntityNMSArmorStand.class, "ArmorStand", 30); - registerCustomEntity(EntityNMSItem.class, "Item", 1); - registerCustomEntity(EntityNMSSlime.class, "Slime", 55); - - validateEntityMethod = World.class.getDeclaredMethod("a", Entity.class); - validateEntityMethod.setAccessible(true); - } - - public void registerCustomEntity(Class entityClass, String name, int id) throws Exception { - ENTITY_NAMES_BY_CLASS_FIELD.getStatic().put(entityClass, name); - ENTITY_IDS_BY_CLASS_FIELD.getStatic().put(entityClass, Integer.valueOf(id)); - } - - @Override - public NMSItem spawnNMSItem(org.bukkit.World bukkitWorld, double x, double y, double z, ItemLine parentPiece, ItemStack stack, ItemPickupManager itemPickupManager) { - WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); - EntityNMSItem customItem = new EntityNMSItem(nmsWorld, parentPiece, itemPickupManager); - customItem.setLocationNMS(x, y, z); - customItem.setItemStackNMS(stack); - if (!addEntityToWorld(nmsWorld, customItem)) { - ConsoleLogger.handleSpawnFail(parentPiece); - } - return customItem; - } - - @Override - public EntityNMSSlime spawnNMSSlime(org.bukkit.World bukkitWorld, double x, double y, double z, HologramLine parentPiece) { - WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); - EntityNMSSlime touchSlime = new EntityNMSSlime(nmsWorld, parentPiece); - touchSlime.setLocationNMS(x, y, z); - if (!addEntityToWorld(nmsWorld, touchSlime)) { - ConsoleLogger.handleSpawnFail(parentPiece); - } - return touchSlime; - } - - @Override - public NMSArmorStand spawnNMSArmorStand(org.bukkit.World world, double x, double y, double z, HologramLine parentPiece) { - WorldServer nmsWorld = ((CraftWorld) world).getHandle(); - EntityNMSArmorStand invisibleArmorStand = new EntityNMSArmorStand(nmsWorld, parentPiece); - invisibleArmorStand.setLocationNMS(x, y, z); - if (!addEntityToWorld(nmsWorld, invisibleArmorStand)) { - ConsoleLogger.handleSpawnFail(parentPiece); - } - return invisibleArmorStand; - } - - private boolean addEntityToWorld(WorldServer nmsWorld, Entity nmsEntity) { - Validator.isTrue(Bukkit.isPrimaryThread(), "Async entity add"); - - if (validateEntityMethod == null) { - return nmsWorld.addEntity(nmsEntity, SpawnReason.CUSTOM); - } - - final int chunkX = MathHelper.floor(nmsEntity.locX / 16.0); - final int chunkZ = MathHelper.floor(nmsEntity.locZ / 16.0); - - if (!nmsWorld.chunkProviderServer.isChunkLoaded(chunkX, chunkZ)) { - // This should never happen - nmsEntity.dead = true; - return false; - } - - nmsWorld.getChunkAt(chunkX, chunkZ).a(nmsEntity); - nmsWorld.entityList.add(nmsEntity); - - try { - validateEntityMethod.invoke(nmsWorld, nmsEntity); - } catch (Exception e) { - e.printStackTrace(); - return false; - } - return true; - } - - @Override - public boolean isNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { - return ((CraftEntity) bukkitEntity).getHandle() instanceof NMSEntityBase; - } - - @Override - public NMSEntityBase getNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { - - Entity nmsEntity = ((CraftEntity) bukkitEntity).getHandle(); - if (nmsEntity instanceof NMSEntityBase) { - return ((NMSEntityBase) nmsEntity); - } - - return null; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_8_R3; + +import java.lang.reflect.Method; +import java.util.Map; + +import org.bukkit.Bukkit; +import org.bukkit.craftbukkit.v1_8_R3.CraftWorld; +import org.bukkit.craftbukkit.v1_8_R3.entity.CraftEntity; +import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason; +import org.bukkit.inventory.ItemStack; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; +import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.Validator; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; + +import net.minecraft.server.v1_8_R3.Entity; +import net.minecraft.server.v1_8_R3.EntityTypes; +import net.minecraft.server.v1_8_R3.MathHelper; +import net.minecraft.server.v1_8_R3.World; +import net.minecraft.server.v1_8_R3.WorldServer; + +public class NmsManagerImpl implements NMSManager { + + private static final ReflectField, String>> ENTITY_NAMES_BY_CLASS_FIELD = new ReflectField, String>>(EntityTypes.class, "d"); + private static final ReflectField, Integer>> ENTITY_IDS_BY_CLASS_FIELD = new ReflectField, Integer>>(EntityTypes.class, "f"); + + private Method validateEntityMethod; + + @Override + public void setup() throws Exception { + registerCustomEntity(EntityNMSArmorStand.class, "ArmorStand", 30); + registerCustomEntity(EntityNMSItem.class, "Item", 1); + registerCustomEntity(EntityNMSSlime.class, "Slime", 55); + + validateEntityMethod = World.class.getDeclaredMethod("a", Entity.class); + validateEntityMethod.setAccessible(true); + } + + public void registerCustomEntity(Class entityClass, String name, int id) throws Exception { + ENTITY_NAMES_BY_CLASS_FIELD.getStatic().put(entityClass, name); + ENTITY_IDS_BY_CLASS_FIELD.getStatic().put(entityClass, Integer.valueOf(id)); + } + + @Override + public NMSItem spawnNMSItem(org.bukkit.World bukkitWorld, double x, double y, double z, ItemLine parentPiece, ItemStack stack, ItemPickupManager itemPickupManager) { + WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); + EntityNMSItem customItem = new EntityNMSItem(nmsWorld, parentPiece, itemPickupManager); + customItem.setLocationNMS(x, y, z); + customItem.setItemStackNMS(stack); + if (!addEntityToWorld(nmsWorld, customItem)) { + ConsoleLogger.handleSpawnFail(parentPiece); + } + return customItem; + } + + @Override + public EntityNMSSlime spawnNMSSlime(org.bukkit.World bukkitWorld, double x, double y, double z, HologramLine parentPiece) { + WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); + EntityNMSSlime touchSlime = new EntityNMSSlime(nmsWorld, parentPiece); + touchSlime.setLocationNMS(x, y, z); + if (!addEntityToWorld(nmsWorld, touchSlime)) { + ConsoleLogger.handleSpawnFail(parentPiece); + } + return touchSlime; + } + + @Override + public NMSArmorStand spawnNMSArmorStand(org.bukkit.World world, double x, double y, double z, HologramLine parentPiece) { + WorldServer nmsWorld = ((CraftWorld) world).getHandle(); + EntityNMSArmorStand invisibleArmorStand = new EntityNMSArmorStand(nmsWorld, parentPiece); + invisibleArmorStand.setLocationNMS(x, y, z); + if (!addEntityToWorld(nmsWorld, invisibleArmorStand)) { + ConsoleLogger.handleSpawnFail(parentPiece); + } + return invisibleArmorStand; + } + + private boolean addEntityToWorld(WorldServer nmsWorld, Entity nmsEntity) { + Validator.isTrue(Bukkit.isPrimaryThread(), "Async entity add"); + + if (validateEntityMethod == null) { + return nmsWorld.addEntity(nmsEntity, SpawnReason.CUSTOM); + } + + final int chunkX = MathHelper.floor(nmsEntity.locX / 16.0); + final int chunkZ = MathHelper.floor(nmsEntity.locZ / 16.0); + + if (!nmsWorld.chunkProviderServer.isChunkLoaded(chunkX, chunkZ)) { + // This should never happen + nmsEntity.dead = true; + return false; + } + + nmsWorld.getChunkAt(chunkX, chunkZ).a(nmsEntity); + nmsWorld.entityList.add(nmsEntity); + + try { + validateEntityMethod.invoke(nmsWorld, nmsEntity); + } catch (Exception e) { + e.printStackTrace(); + return false; + } + return true; + } + + @Override + public boolean isNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { + return ((CraftEntity) bukkitEntity).getHandle() instanceof NMSEntityBase; + } + + @Override + public NMSEntityBase getNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { + + Entity nmsEntity = ((CraftEntity) bukkitEntity).getHandle(); + if (nmsEntity instanceof NMSEntityBase) { + return ((NMSEntityBase) nmsEntity); + } + + return null; + } + +} diff --git a/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/NullBoundingBox.java b/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/NullBoundingBox.java index a7e0f805..e95acfe0 100644 --- a/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/NullBoundingBox.java +++ b/NMS/v1_8_R3/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_8_R3/NullBoundingBox.java @@ -1,75 +1,89 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_8_R3; - -import net.minecraft.server.v1_8_R3.AxisAlignedBB; -import net.minecraft.server.v1_8_R3.MovingObjectPosition; -import net.minecraft.server.v1_8_R3.Vec3D; - -public class NullBoundingBox extends AxisAlignedBB { - - public NullBoundingBox() { - super(0, 0, 0, 0, 0, 0); - } - - @Override - public double a() { - return 0.0; - } - - @Override - public double a(AxisAlignedBB arg0, double arg1) { - return 0.0; - } - - @Override - public AxisAlignedBB a(AxisAlignedBB arg0) { - return this; - } - - @Override - public AxisAlignedBB a(double arg0, double arg1, double arg2) { - return this; - } - - @Override - public MovingObjectPosition a(Vec3D arg0, Vec3D arg1) { - return super.a(arg0, arg1); - } - - @Override - public boolean a(Vec3D arg0) { - return false; - } - - @Override - public double b(AxisAlignedBB arg0, double arg1) { - return 0.0; - } - - @Override - public boolean b(AxisAlignedBB arg0) { - return false; - } - - @Override - public double c(AxisAlignedBB arg0, double arg1) { - return 0.0; - } - - @Override - public AxisAlignedBB c(double arg0, double arg1, double arg2) { - return this; - } - - @Override - public AxisAlignedBB grow(double arg0, double arg1, double arg2) { - return this; - } - - @Override - public AxisAlignedBB shrink(double arg0, double arg1, double arg2) { - return this; - } - - - -} \ No newline at end of file +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_8_R3; + +import net.minecraft.server.v1_8_R3.AxisAlignedBB; +import net.minecraft.server.v1_8_R3.MovingObjectPosition; +import net.minecraft.server.v1_8_R3.Vec3D; + +public class NullBoundingBox extends AxisAlignedBB { + + public NullBoundingBox() { + super(0, 0, 0, 0, 0, 0); + } + + @Override + public double a() { + return 0.0; + } + + @Override + public double a(AxisAlignedBB arg0, double arg1) { + return 0.0; + } + + @Override + public AxisAlignedBB a(AxisAlignedBB arg0) { + return this; + } + + @Override + public AxisAlignedBB a(double arg0, double arg1, double arg2) { + return this; + } + + @Override + public MovingObjectPosition a(Vec3D arg0, Vec3D arg1) { + return super.a(arg0, arg1); + } + + @Override + public boolean a(Vec3D arg0) { + return false; + } + + @Override + public double b(AxisAlignedBB arg0, double arg1) { + return 0.0; + } + + @Override + public boolean b(AxisAlignedBB arg0) { + return false; + } + + @Override + public double c(AxisAlignedBB arg0, double arg1) { + return 0.0; + } + + @Override + public AxisAlignedBB c(double arg0, double arg1, double arg2) { + return this; + } + + @Override + public AxisAlignedBB grow(double arg0, double arg1, double arg2) { + return this; + } + + @Override + public AxisAlignedBB shrink(double arg0, double arg1, double arg2) { + return this; + } + + + +} diff --git a/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/CraftNMSArmorStand.java b/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/CraftNMSArmorStand.java index 3030e0b4..6aa4344a 100644 --- a/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/CraftNMSArmorStand.java +++ b/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/CraftNMSArmorStand.java @@ -1,68 +1,82 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_9_R1; - -import java.util.Collection; - -import org.bukkit.EntityEffect; -import org.bukkit.Location; -import org.bukkit.craftbukkit.v1_9_R1.CraftServer; -import org.bukkit.craftbukkit.v1_9_R1.entity.CraftArmorStand; -import org.bukkit.entity.Entity; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -import org.bukkit.inventory.ItemStack; -import org.bukkit.potion.PotionEffect; -import org.bukkit.util.EulerAngle; -import org.bukkit.util.Vector; - -public class CraftNMSArmorStand extends CraftArmorStand { - - public CraftNMSArmorStand(CraftServer server, EntityNMSArmorStand entity) { - super(server, entity); - } - - // Disallow all the bukkit methods. - - @Override - public void remove() { - // Cannot be removed, this is the most important to override. - } - - // Methods from Armor stand class - @Override public void setArms(boolean arms) { } - @Override public void setBasePlate(boolean basePlate) { } - @Override public void setBodyPose(EulerAngle pose) { } - @Override public void setBoots(ItemStack item) { } - @Override public void setChestplate(ItemStack item) { } - @Override public void setGravity(boolean gravity) { } - @Override public void setHeadPose(EulerAngle pose) { } - @Override public void setHelmet(ItemStack item) { } - @Override public void setItemInHand(ItemStack item) { } - @Override public void setLeftArmPose(EulerAngle pose) { } - @Override public void setLeftLegPose(EulerAngle pose) { } - @Override public void setLeggings(ItemStack item) { } - @Override public void setRightArmPose(EulerAngle pose) { } - @Override public void setRightLegPose(EulerAngle pose) { } - @Override public void setSmall(boolean small) { } - @Override public void setVisible(boolean visible) { } - @Override public void setMarker(boolean marker) { } - - // Methods from LivingEntity class - @Override public boolean addPotionEffect(PotionEffect effect) { return false; } - @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } - @Override public boolean addPotionEffects(Collection effects) { return false; } - @Override public void setRemoveWhenFarAway(boolean remove) { } - - // Methods from Entity - @Override public void setVelocity(Vector vel) { } - @Override public boolean teleport(Location loc) { return false; } - @Override public boolean teleport(Entity entity) { return false; } - @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } - @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } - @Override public void setFireTicks(int ticks) { } - @Override public boolean setPassenger(Entity entity) { return false; } - @Override public boolean eject() { return false; } - @Override public boolean leaveVehicle() { return false; } - @Override public void playEffect(EntityEffect effect) { } - @Override public void setCustomName(String name) { } - @Override public void setCustomNameVisible(boolean flag) { } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_9_R1; + +import java.util.Collection; + +import org.bukkit.EntityEffect; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_9_R1.CraftServer; +import org.bukkit.craftbukkit.v1_9_R1.entity.CraftArmorStand; +import org.bukkit.entity.Entity; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import org.bukkit.inventory.ItemStack; +import org.bukkit.potion.PotionEffect; +import org.bukkit.util.EulerAngle; +import org.bukkit.util.Vector; + +public class CraftNMSArmorStand extends CraftArmorStand { + + public CraftNMSArmorStand(CraftServer server, EntityNMSArmorStand entity) { + super(server, entity); + } + + // Disallow all the bukkit methods. + + @Override + public void remove() { + // Cannot be removed, this is the most important to override. + } + + // Methods from Armor stand class + @Override public void setArms(boolean arms) { } + @Override public void setBasePlate(boolean basePlate) { } + @Override public void setBodyPose(EulerAngle pose) { } + @Override public void setBoots(ItemStack item) { } + @Override public void setChestplate(ItemStack item) { } + @Override public void setGravity(boolean gravity) { } + @Override public void setHeadPose(EulerAngle pose) { } + @Override public void setHelmet(ItemStack item) { } + @Override public void setItemInHand(ItemStack item) { } + @Override public void setLeftArmPose(EulerAngle pose) { } + @Override public void setLeftLegPose(EulerAngle pose) { } + @Override public void setLeggings(ItemStack item) { } + @Override public void setRightArmPose(EulerAngle pose) { } + @Override public void setRightLegPose(EulerAngle pose) { } + @Override public void setSmall(boolean small) { } + @Override public void setVisible(boolean visible) { } + @Override public void setMarker(boolean marker) { } + + // Methods from LivingEntity class + @Override public boolean addPotionEffect(PotionEffect effect) { return false; } + @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } + @Override public boolean addPotionEffects(Collection effects) { return false; } + @Override public void setRemoveWhenFarAway(boolean remove) { } + + // Methods from Entity + @Override public void setVelocity(Vector vel) { } + @Override public boolean teleport(Location loc) { return false; } + @Override public boolean teleport(Entity entity) { return false; } + @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } + @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } + @Override public void setFireTicks(int ticks) { } + @Override public boolean setPassenger(Entity entity) { return false; } + @Override public boolean eject() { return false; } + @Override public boolean leaveVehicle() { return false; } + @Override public void playEffect(EntityEffect effect) { } + @Override public void setCustomName(String name) { } + @Override public void setCustomNameVisible(boolean flag) { } + +} diff --git a/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/CraftNMSItem.java b/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/CraftNMSItem.java index 1e314635..1bd2fe99 100644 --- a/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/CraftNMSItem.java +++ b/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/CraftNMSItem.java @@ -1,43 +1,57 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_9_R1; - -import org.bukkit.EntityEffect; -import org.bukkit.Location; -import org.bukkit.craftbukkit.v1_9_R1.CraftServer; -import org.bukkit.craftbukkit.v1_9_R1.entity.CraftItem; -import org.bukkit.entity.Entity; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -import org.bukkit.inventory.ItemStack; -import org.bukkit.util.Vector; - -public class CraftNMSItem extends CraftItem { - - public CraftNMSItem(CraftServer server, EntityNMSItem entity) { - super(server, entity); - } - - // Disallow all the bukkit methods. - - @Override - public void remove() { - // Cannot be removed, this is the most important to override. - } - - // Methods from Entity - @Override public void setVelocity(Vector vel) { } - @Override public boolean teleport(Location loc) { return false; } - @Override public boolean teleport(Entity entity) { return false; } - @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } - @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } - @Override public void setFireTicks(int ticks) { } - @Override public boolean setPassenger(Entity entity) { return false; } - @Override public boolean eject() { return false; } - @Override public boolean leaveVehicle() { return false; } - @Override public void playEffect(EntityEffect effect) { } - @Override public void setCustomName(String name) { } - @Override public void setCustomNameVisible(boolean flag) { } - - // Methods from Item - @Override public void setItemStack(ItemStack stack) { } - @Override public void setPickupDelay(int delay) { } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_9_R1; + +import org.bukkit.EntityEffect; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_9_R1.CraftServer; +import org.bukkit.craftbukkit.v1_9_R1.entity.CraftItem; +import org.bukkit.entity.Entity; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import org.bukkit.inventory.ItemStack; +import org.bukkit.util.Vector; + +public class CraftNMSItem extends CraftItem { + + public CraftNMSItem(CraftServer server, EntityNMSItem entity) { + super(server, entity); + } + + // Disallow all the bukkit methods. + + @Override + public void remove() { + // Cannot be removed, this is the most important to override. + } + + // Methods from Entity + @Override public void setVelocity(Vector vel) { } + @Override public boolean teleport(Location loc) { return false; } + @Override public boolean teleport(Entity entity) { return false; } + @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } + @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } + @Override public void setFireTicks(int ticks) { } + @Override public boolean setPassenger(Entity entity) { return false; } + @Override public boolean eject() { return false; } + @Override public boolean leaveVehicle() { return false; } + @Override public void playEffect(EntityEffect effect) { } + @Override public void setCustomName(String name) { } + @Override public void setCustomNameVisible(boolean flag) { } + + // Methods from Item + @Override public void setItemStack(ItemStack stack) { } + @Override public void setPickupDelay(int delay) { } + +} diff --git a/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/CraftNMSSlime.java b/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/CraftNMSSlime.java index cd756f50..754aa179 100644 --- a/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/CraftNMSSlime.java +++ b/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/CraftNMSSlime.java @@ -1,50 +1,64 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_9_R1; - -import java.util.Collection; - -import org.bukkit.EntityEffect; -import org.bukkit.Location; -import org.bukkit.craftbukkit.v1_9_R1.CraftServer; -import org.bukkit.craftbukkit.v1_9_R1.entity.CraftSlime; -import org.bukkit.entity.Entity; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -import org.bukkit.potion.PotionEffect; -import org.bukkit.util.Vector; - -public class CraftNMSSlime extends CraftSlime { - - public CraftNMSSlime(CraftServer server, EntityNMSSlime entity) { - super(server, entity); - } - - // Disallow all the bukkit methods. - - @Override - public void remove() { - // Cannot be removed, this is the most important to override. - } - - // Methods from LivingEntity class - @Override public boolean addPotionEffect(PotionEffect effect) { return false; } - @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } - @Override public boolean addPotionEffects(Collection effects) { return false; } - @Override public void setRemoveWhenFarAway(boolean remove) { } - - // Methods from Entity - @Override public void setVelocity(Vector vel) { } - @Override public boolean teleport(Location loc) { return false; } - @Override public boolean teleport(Entity entity) { return false; } - @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } - @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } - @Override public void setFireTicks(int ticks) { } - @Override public boolean setPassenger(Entity entity) { return false; } - @Override public boolean eject() { return false; } - @Override public boolean leaveVehicle() { return false; } - @Override public void playEffect(EntityEffect effect) { } - @Override public void setCustomName(String name) { } - @Override public void setCustomNameVisible(boolean flag) { } - - // Methods from Slime - @Override public void setSize(int size) { } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_9_R1; + +import java.util.Collection; + +import org.bukkit.EntityEffect; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_9_R1.CraftServer; +import org.bukkit.craftbukkit.v1_9_R1.entity.CraftSlime; +import org.bukkit.entity.Entity; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import org.bukkit.potion.PotionEffect; +import org.bukkit.util.Vector; + +public class CraftNMSSlime extends CraftSlime { + + public CraftNMSSlime(CraftServer server, EntityNMSSlime entity) { + super(server, entity); + } + + // Disallow all the bukkit methods. + + @Override + public void remove() { + // Cannot be removed, this is the most important to override. + } + + // Methods from LivingEntity class + @Override public boolean addPotionEffect(PotionEffect effect) { return false; } + @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } + @Override public boolean addPotionEffects(Collection effects) { return false; } + @Override public void setRemoveWhenFarAway(boolean remove) { } + + // Methods from Entity + @Override public void setVelocity(Vector vel) { } + @Override public boolean teleport(Location loc) { return false; } + @Override public boolean teleport(Entity entity) { return false; } + @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } + @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } + @Override public void setFireTicks(int ticks) { } + @Override public boolean setPassenger(Entity entity) { return false; } + @Override public boolean eject() { return false; } + @Override public boolean leaveVehicle() { return false; } + @Override public void playEffect(EntityEffect effect) { } + @Override public void setCustomName(String name) { } + @Override public void setCustomNameVisible(boolean flag) { } + + // Methods from Slime + @Override public void setSize(int size) { } + +} diff --git a/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/EntityNMSArmorStand.java b/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/EntityNMSArmorStand.java index 2c54ea11..32a6b527 100644 --- a/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/EntityNMSArmorStand.java +++ b/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/EntityNMSArmorStand.java @@ -1,238 +1,252 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_9_R1; - -import org.bukkit.craftbukkit.v1_9_R1.entity.CraftEntity; - -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; -import com.gmail.filoghost.holographicdisplays.disk.Configuration; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; -import com.gmail.filoghost.holographicdisplays.util.Utils; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectionUtils; - -import net.minecraft.server.v1_9_R1.AxisAlignedBB; -import net.minecraft.server.v1_9_R1.DamageSource; -import net.minecraft.server.v1_9_R1.EntityArmorStand; -import net.minecraft.server.v1_9_R1.EntityHuman; -import net.minecraft.server.v1_9_R1.EntityPlayer; -import net.minecraft.server.v1_9_R1.EnumHand; -import net.minecraft.server.v1_9_R1.EnumInteractionResult; -import net.minecraft.server.v1_9_R1.EnumItemSlot; -import net.minecraft.server.v1_9_R1.ItemStack; -import net.minecraft.server.v1_9_R1.NBTTagCompound; -import net.minecraft.server.v1_9_R1.PacketPlayOutEntityTeleport; -import net.minecraft.server.v1_9_R1.SoundEffect; -import net.minecraft.server.v1_9_R1.Vec3D; -import net.minecraft.server.v1_9_R1.World; - -public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorStand { - - private static final ReflectField DISABLED_SLOTS_FIELD = new ReflectField(EntityArmorStand.class, "bz"); - - private boolean lockTick; - private HologramLine parentPiece; - - public EntityNMSArmorStand(World world, HologramLine parentPiece) { - super(world); - super.setInvisible(true); - super.setSmall(true); - super.setArms(false); - super.setGravity(true); - super.setBasePlate(true); - super.setMarker(true); - this.parentPiece = parentPiece; - try { - DISABLED_SLOTS_FIELD.set(this, Integer.MAX_VALUE); - } catch (Exception e) { - // There's still the overridden method. - } - forceSetBoundingBox(new NullBoundingBox()); - } - - - @Override - public void b(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public boolean c(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public boolean d(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public void e(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public void f(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public void a(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - - @Override - public boolean isInvulnerable(DamageSource source) { - /* - * The field Entity.invulnerable is private. - * It's only used while saving NBTTags, but since the entity would be killed - * on chunk unload, we prefer to override isInvulnerable(). - */ - return true; - } - - @Override - public boolean isCollidable() { - return false; - } - - @Override - public void setCustomName(String customName) { - // Locks the custom name. - } - - @Override - public void setCustomNameVisible(boolean visible) { - // Locks the custom name. - } - - @Override - public EnumInteractionResult a(EntityHuman human, Vec3D vec3d, ItemStack itemstack, EnumHand enumhand) { - // Prevent stand being equipped - return EnumInteractionResult.PASS; - } - - @Override - public boolean c(int i, ItemStack item) { - // Prevent stand being equipped - return false; - } - - @Override - public void setSlot(EnumItemSlot enumitemslot, ItemStack itemstack) { - // Prevent stand being equipped - } - - @Override - public void a(AxisAlignedBB boundingBox) { - // Do not change it! - } - - public void forceSetBoundingBox(AxisAlignedBB boundingBox) { - super.a(boundingBox); - } - - @Override - public int getId() { - if (Configuration.preciseHologramMovement) { - StackTraceElement element = ReflectionUtils.getStackTraceElement(2); - if (element != null && element.getFileName() != null && element.getFileName().equals("EntityTrackerEntry.java") && element.getLineNumber() > 142 && element.getLineNumber() < 152) { - // Then this method is being called when creating a new packet, we return a fake ID! - return -1; - } - } - - return super.getId(); - } - - @Override - public void m() { - if (!lockTick) { - super.m(); - } - } - - @Override - public void a(SoundEffect soundeffect, float f, float f1) { - // Remove sounds. - } - - @Override - public void setCustomNameNMS(String name) { - if (name != null && name.length() > 300) { - name = name.substring(0, 300); - } - super.setCustomName(name); - super.setCustomNameVisible(name != null && !name.isEmpty()); - } - - @Override - public String getCustomNameNMS() { - return super.getCustomName(); - } - - @Override - public void setLockTick(boolean lock) { - lockTick = lock; - } - - @Override - public void die() { - // Prevent being killed. - } - - @Override - public CraftEntity getBukkitEntity() { - if (super.bukkitEntity == null) { - super.bukkitEntity = new CraftNMSArmorStand(super.world.getServer(), this); - } - return super.bukkitEntity; - } - - @Override - public void killEntityNMS() { - super.dead = true; - } - - @Override - public void setLocationNMS(double x, double y, double z) { - super.setPosition(x, y, z); - - if (Configuration.preciseHologramMovement) { - // Send a packet near to update the position. - PacketPlayOutEntityTeleport teleportPacket = new PacketPlayOutEntityTeleport(this); - - for (Object obj : super.world.players) { - if (obj instanceof EntityPlayer) { - EntityPlayer nmsPlayer = (EntityPlayer) obj; - - double distanceSquared = Utils.square(nmsPlayer.locX - super.locX) + Utils.square(nmsPlayer.locZ - super.locZ); - if (distanceSquared < 8192 && nmsPlayer.playerConnection != null) { - nmsPlayer.playerConnection.sendPacket(teleportPacket); - } - } - } - } - } - - @Override - public boolean isDeadNMS() { - return super.dead; - } - - @Override - public int getIdNMS() { - return super.getId(); // Return the real ID without checking the stack trace. - } - - @Override - public HologramLine getHologramLine() { - return parentPiece; - } - - @Override - public org.bukkit.entity.Entity getBukkitEntityNMS() { - return getBukkitEntity(); - } -} \ No newline at end of file +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_9_R1; + +import org.bukkit.craftbukkit.v1_9_R1.entity.CraftEntity; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; +import com.gmail.filoghost.holographicdisplays.disk.Configuration; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; +import com.gmail.filoghost.holographicdisplays.util.Utils; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectionUtils; + +import net.minecraft.server.v1_9_R1.AxisAlignedBB; +import net.minecraft.server.v1_9_R1.DamageSource; +import net.minecraft.server.v1_9_R1.EntityArmorStand; +import net.minecraft.server.v1_9_R1.EntityHuman; +import net.minecraft.server.v1_9_R1.EntityPlayer; +import net.minecraft.server.v1_9_R1.EnumHand; +import net.minecraft.server.v1_9_R1.EnumInteractionResult; +import net.minecraft.server.v1_9_R1.EnumItemSlot; +import net.minecraft.server.v1_9_R1.ItemStack; +import net.minecraft.server.v1_9_R1.NBTTagCompound; +import net.minecraft.server.v1_9_R1.PacketPlayOutEntityTeleport; +import net.minecraft.server.v1_9_R1.SoundEffect; +import net.minecraft.server.v1_9_R1.Vec3D; +import net.minecraft.server.v1_9_R1.World; + +public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorStand { + + private static final ReflectField DISABLED_SLOTS_FIELD = new ReflectField(EntityArmorStand.class, "bz"); + + private boolean lockTick; + private HologramLine parentPiece; + + public EntityNMSArmorStand(World world, HologramLine parentPiece) { + super(world); + super.setInvisible(true); + super.setSmall(true); + super.setArms(false); + super.setGravity(true); + super.setBasePlate(true); + super.setMarker(true); + this.parentPiece = parentPiece; + try { + DISABLED_SLOTS_FIELD.set(this, Integer.MAX_VALUE); + } catch (Exception e) { + // There's still the overridden method. + } + forceSetBoundingBox(new NullBoundingBox()); + } + + + @Override + public void b(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public boolean c(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public boolean d(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public void e(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public void f(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public void a(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + + @Override + public boolean isInvulnerable(DamageSource source) { + /* + * The field Entity.invulnerable is private. + * It's only used while saving NBTTags, but since the entity would be killed + * on chunk unload, we prefer to override isInvulnerable(). + */ + return true; + } + + @Override + public boolean isCollidable() { + return false; + } + + @Override + public void setCustomName(String customName) { + // Locks the custom name. + } + + @Override + public void setCustomNameVisible(boolean visible) { + // Locks the custom name. + } + + @Override + public EnumInteractionResult a(EntityHuman human, Vec3D vec3d, ItemStack itemstack, EnumHand enumhand) { + // Prevent stand being equipped + return EnumInteractionResult.PASS; + } + + @Override + public boolean c(int i, ItemStack item) { + // Prevent stand being equipped + return false; + } + + @Override + public void setSlot(EnumItemSlot enumitemslot, ItemStack itemstack) { + // Prevent stand being equipped + } + + @Override + public void a(AxisAlignedBB boundingBox) { + // Do not change it! + } + + public void forceSetBoundingBox(AxisAlignedBB boundingBox) { + super.a(boundingBox); + } + + @Override + public int getId() { + if (Configuration.preciseHologramMovement) { + StackTraceElement element = ReflectionUtils.getStackTraceElement(2); + if (element != null && element.getFileName() != null && element.getFileName().equals("EntityTrackerEntry.java") && element.getLineNumber() > 142 && element.getLineNumber() < 152) { + // Then this method is being called when creating a new packet, we return a fake ID! + return -1; + } + } + + return super.getId(); + } + + @Override + public void m() { + if (!lockTick) { + super.m(); + } + } + + @Override + public void a(SoundEffect soundeffect, float f, float f1) { + // Remove sounds. + } + + @Override + public void setCustomNameNMS(String name) { + if (name != null && name.length() > 300) { + name = name.substring(0, 300); + } + super.setCustomName(name); + super.setCustomNameVisible(name != null && !name.isEmpty()); + } + + @Override + public String getCustomNameNMS() { + return super.getCustomName(); + } + + @Override + public void setLockTick(boolean lock) { + lockTick = lock; + } + + @Override + public void die() { + // Prevent being killed. + } + + @Override + public CraftEntity getBukkitEntity() { + if (super.bukkitEntity == null) { + super.bukkitEntity = new CraftNMSArmorStand(super.world.getServer(), this); + } + return super.bukkitEntity; + } + + @Override + public void killEntityNMS() { + super.dead = true; + } + + @Override + public void setLocationNMS(double x, double y, double z) { + super.setPosition(x, y, z); + + if (Configuration.preciseHologramMovement) { + // Send a packet near to update the position. + PacketPlayOutEntityTeleport teleportPacket = new PacketPlayOutEntityTeleport(this); + + for (Object obj : super.world.players) { + if (obj instanceof EntityPlayer) { + EntityPlayer nmsPlayer = (EntityPlayer) obj; + + double distanceSquared = Utils.square(nmsPlayer.locX - super.locX) + Utils.square(nmsPlayer.locZ - super.locZ); + if (distanceSquared < 8192 && nmsPlayer.playerConnection != null) { + nmsPlayer.playerConnection.sendPacket(teleportPacket); + } + } + } + } + } + + @Override + public boolean isDeadNMS() { + return super.dead; + } + + @Override + public int getIdNMS() { + return super.getId(); // Return the real ID without checking the stack trace. + } + + @Override + public HologramLine getHologramLine() { + return parentPiece; + } + + @Override + public org.bukkit.entity.Entity getBukkitEntityNMS() { + return getBukkitEntity(); + } +} diff --git a/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/EntityNMSItem.java b/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/EntityNMSItem.java index 74e0bbc2..1765c3bc 100644 --- a/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/EntityNMSItem.java +++ b/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/EntityNMSItem.java @@ -1,270 +1,284 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_9_R1; - -import org.bukkit.craftbukkit.v1_9_R1.entity.CraftEntity; -import org.bukkit.craftbukkit.v1_9_R1.inventory.CraftItemStack; -import org.bukkit.entity.Player; - -import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.ItemUtils; -import com.gmail.filoghost.holographicdisplays.util.Utils; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; - -import net.minecraft.server.v1_9_R1.AxisAlignedBB; -import net.minecraft.server.v1_9_R1.Blocks; -import net.minecraft.server.v1_9_R1.DamageSource; -import net.minecraft.server.v1_9_R1.Entity; -import net.minecraft.server.v1_9_R1.EntityHuman; -import net.minecraft.server.v1_9_R1.EntityItem; -import net.minecraft.server.v1_9_R1.EntityPlayer; -import net.minecraft.server.v1_9_R1.ItemStack; -import net.minecraft.server.v1_9_R1.NBTTagCompound; -import net.minecraft.server.v1_9_R1.NBTTagList; -import net.minecraft.server.v1_9_R1.NBTTagString; -import net.minecraft.server.v1_9_R1.PacketPlayOutMount; -import net.minecraft.server.v1_9_R1.World; - -public class EntityNMSItem extends EntityItem implements NMSItem { - - private static final ReflectField VEHICLE_FIELD = new ReflectField(Entity.class, "as"); - - private boolean lockTick; - private ItemLine parentPiece; - private ItemPickupManager itemPickupManager; - - private int resendMountPacketTicks; - - public EntityNMSItem(World world, ItemLine piece, ItemPickupManager itemPickupManager) { - super(world); - super.pickupDelay = Integer.MAX_VALUE; - this.parentPiece = piece; - this.itemPickupManager = itemPickupManager; - forceSetBoundingBox(new NullBoundingBox()); - } - - @Override - public void m() { - - // So it won't get removed. - ticksLived = 0; - - if (resendMountPacketTicks++ > 20) { - resendMountPacketTicks = 0; - - if (by() != null) { - // Send a packet near to "remind" players that the item is riding the armor stand (Spigot bug or client bug) - PacketPlayOutMount mountPacket = new PacketPlayOutMount(by()); - - for (Object obj : super.world.players) { - if (obj instanceof EntityPlayer) { - EntityPlayer nmsPlayer = (EntityPlayer) obj; - - double distanceSquared = Utils.square(nmsPlayer.locX - super.locX) + Utils.square(nmsPlayer.locZ - super.locZ); - if (distanceSquared < 1024 && nmsPlayer.playerConnection != null) { - nmsPlayer.playerConnection.sendPacket(mountPacket); - } - } - } - } - } - - if (!lockTick) { - super.m(); - } - } - - // Method called when a player is near. - @Override - public void d(EntityHuman human) { - - if (human.locY < super.locY - 1.5 || human.locY > super.locY + 1.0) { - // Too low or too high, it's a bit weird. - return; - } - - if (parentPiece.getPickupHandler() != null && human instanceof EntityPlayer) { - itemPickupManager.handleItemLinePickup((Player) human.getBukkitEntity(), parentPiece.getPickupHandler(), parentPiece.getParent()); - // It is never added to the inventory. - } - } - - @Override - public void b(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public boolean c(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public boolean d(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public void e(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public void f(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public void a(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public boolean isInvulnerable(DamageSource source) { - /* - * The field Entity.invulnerable is private. - * It's only used while saving NBTTags, but since the entity would be killed - * on chunk unload, we prefer to override isInvulnerable(). - */ - return true; - } - - @Override - public boolean isCollidable() { - return false; - } - - @Override - public void a(AxisAlignedBB boundingBox) { - // Do not change it! - } - - public void forceSetBoundingBox(AxisAlignedBB boundingBox) { - super.a(boundingBox); - } - - @Override - public void inactiveTick() { - // Check inactive ticks. - - if (!lockTick) { - super.inactiveTick(); - } - } - - @Override - public void setLockTick(boolean lock) { - lockTick = lock; - } - - @Override - public void die() { - // Prevent being killed. - } - - @Override - public CraftEntity getBukkitEntity() { - if (super.bukkitEntity == null) { - super.bukkitEntity = new CraftNMSItem(super.world.getServer(), this); - } - return super.bukkitEntity; - } - - @Override - public boolean isDeadNMS() { - return super.dead; - } - - @Override - public void killEntityNMS() { - super.dead = true; - } - - @Override - public void setLocationNMS(double x, double y, double z) { - super.setPosition(x, y, z); - } - - @Override - public void setItemStackNMS(org.bukkit.inventory.ItemStack stack) { - ItemStack newItem = CraftItemStack.asNMSCopy(stack); - - if (newItem == null) { - newItem = new ItemStack(Blocks.BEDROCK); - } - - if (newItem.getTag() == null) { - newItem.setTag(new NBTTagCompound()); - } - NBTTagCompound display = newItem.getTag().getCompound("display"); - - if (!newItem.getTag().hasKey("display")) { - newItem.getTag().set("display", display); - } - - NBTTagList tagList = new NBTTagList(); - tagList.add(new NBTTagString(ItemUtils.ANTISTACK_LORE)); // Antistack lore - - display.set("Lore", tagList); - newItem.count = 0; - setItemStack(newItem); - } - - @Override - public int getIdNMS() { - return super.getId(); - } - - @Override - public ItemLine getHologramLine() { - return parentPiece; - } - - @Override - public void allowPickup(boolean pickup) { - if (pickup) { - super.pickupDelay = 0; - } else { - super.pickupDelay = Integer.MAX_VALUE; - } - } - - @Override - public org.bukkit.entity.Entity getBukkitEntityNMS() { - return getBukkitEntity(); - } - - @Override - public void setPassengerOfNMS(NMSEntityBase vehicleBase) { - if (vehicleBase == null || !(vehicleBase instanceof Entity)) { - // It should never dismount - return; - } - - Entity entity = (Entity) vehicleBase; - - try { - if (super.by() != null) { - Entity oldVehicle = super.by(); - VEHICLE_FIELD.set(this, null); - oldVehicle.passengers.remove(this); - } - - VEHICLE_FIELD.set(this, entity); - entity.passengers.clear(); - entity.passengers.add(this); - - } catch (Exception ex) { - ConsoleLogger.logDebugException(ex); - } - } - - @Override - public Object getRawItemStack() { - return super.getItemStack(); - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_9_R1; + +import org.bukkit.craftbukkit.v1_9_R1.entity.CraftEntity; +import org.bukkit.craftbukkit.v1_9_R1.inventory.CraftItemStack; +import org.bukkit.entity.Player; + +import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.ItemUtils; +import com.gmail.filoghost.holographicdisplays.util.Utils; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; + +import net.minecraft.server.v1_9_R1.AxisAlignedBB; +import net.minecraft.server.v1_9_R1.Blocks; +import net.minecraft.server.v1_9_R1.DamageSource; +import net.minecraft.server.v1_9_R1.Entity; +import net.minecraft.server.v1_9_R1.EntityHuman; +import net.minecraft.server.v1_9_R1.EntityItem; +import net.minecraft.server.v1_9_R1.EntityPlayer; +import net.minecraft.server.v1_9_R1.ItemStack; +import net.minecraft.server.v1_9_R1.NBTTagCompound; +import net.minecraft.server.v1_9_R1.NBTTagList; +import net.minecraft.server.v1_9_R1.NBTTagString; +import net.minecraft.server.v1_9_R1.PacketPlayOutMount; +import net.minecraft.server.v1_9_R1.World; + +public class EntityNMSItem extends EntityItem implements NMSItem { + + private static final ReflectField VEHICLE_FIELD = new ReflectField(Entity.class, "as"); + + private boolean lockTick; + private ItemLine parentPiece; + private ItemPickupManager itemPickupManager; + + private int resendMountPacketTicks; + + public EntityNMSItem(World world, ItemLine piece, ItemPickupManager itemPickupManager) { + super(world); + super.pickupDelay = Integer.MAX_VALUE; + this.parentPiece = piece; + this.itemPickupManager = itemPickupManager; + forceSetBoundingBox(new NullBoundingBox()); + } + + @Override + public void m() { + + // So it won't get removed. + ticksLived = 0; + + if (resendMountPacketTicks++ > 20) { + resendMountPacketTicks = 0; + + if (by() != null) { + // Send a packet near to "remind" players that the item is riding the armor stand (Spigot bug or client bug) + PacketPlayOutMount mountPacket = new PacketPlayOutMount(by()); + + for (Object obj : super.world.players) { + if (obj instanceof EntityPlayer) { + EntityPlayer nmsPlayer = (EntityPlayer) obj; + + double distanceSquared = Utils.square(nmsPlayer.locX - super.locX) + Utils.square(nmsPlayer.locZ - super.locZ); + if (distanceSquared < 1024 && nmsPlayer.playerConnection != null) { + nmsPlayer.playerConnection.sendPacket(mountPacket); + } + } + } + } + } + + if (!lockTick) { + super.m(); + } + } + + // Method called when a player is near. + @Override + public void d(EntityHuman human) { + + if (human.locY < super.locY - 1.5 || human.locY > super.locY + 1.0) { + // Too low or too high, it's a bit weird. + return; + } + + if (parentPiece.getPickupHandler() != null && human instanceof EntityPlayer) { + itemPickupManager.handleItemLinePickup((Player) human.getBukkitEntity(), parentPiece.getPickupHandler(), parentPiece.getParent()); + // It is never added to the inventory. + } + } + + @Override + public void b(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public boolean c(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public boolean d(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public void e(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public void f(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public void a(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public boolean isInvulnerable(DamageSource source) { + /* + * The field Entity.invulnerable is private. + * It's only used while saving NBTTags, but since the entity would be killed + * on chunk unload, we prefer to override isInvulnerable(). + */ + return true; + } + + @Override + public boolean isCollidable() { + return false; + } + + @Override + public void a(AxisAlignedBB boundingBox) { + // Do not change it! + } + + public void forceSetBoundingBox(AxisAlignedBB boundingBox) { + super.a(boundingBox); + } + + @Override + public void inactiveTick() { + // Check inactive ticks. + + if (!lockTick) { + super.inactiveTick(); + } + } + + @Override + public void setLockTick(boolean lock) { + lockTick = lock; + } + + @Override + public void die() { + // Prevent being killed. + } + + @Override + public CraftEntity getBukkitEntity() { + if (super.bukkitEntity == null) { + super.bukkitEntity = new CraftNMSItem(super.world.getServer(), this); + } + return super.bukkitEntity; + } + + @Override + public boolean isDeadNMS() { + return super.dead; + } + + @Override + public void killEntityNMS() { + super.dead = true; + } + + @Override + public void setLocationNMS(double x, double y, double z) { + super.setPosition(x, y, z); + } + + @Override + public void setItemStackNMS(org.bukkit.inventory.ItemStack stack) { + ItemStack newItem = CraftItemStack.asNMSCopy(stack); + + if (newItem == null) { + newItem = new ItemStack(Blocks.BEDROCK); + } + + if (newItem.getTag() == null) { + newItem.setTag(new NBTTagCompound()); + } + NBTTagCompound display = newItem.getTag().getCompound("display"); + + if (!newItem.getTag().hasKey("display")) { + newItem.getTag().set("display", display); + } + + NBTTagList tagList = new NBTTagList(); + tagList.add(new NBTTagString(ItemUtils.ANTISTACK_LORE)); // Antistack lore + + display.set("Lore", tagList); + newItem.count = 0; + setItemStack(newItem); + } + + @Override + public int getIdNMS() { + return super.getId(); + } + + @Override + public ItemLine getHologramLine() { + return parentPiece; + } + + @Override + public void allowPickup(boolean pickup) { + if (pickup) { + super.pickupDelay = 0; + } else { + super.pickupDelay = Integer.MAX_VALUE; + } + } + + @Override + public org.bukkit.entity.Entity getBukkitEntityNMS() { + return getBukkitEntity(); + } + + @Override + public void setPassengerOfNMS(NMSEntityBase vehicleBase) { + if (vehicleBase == null || !(vehicleBase instanceof Entity)) { + // It should never dismount + return; + } + + Entity entity = (Entity) vehicleBase; + + try { + if (super.by() != null) { + Entity oldVehicle = super.by(); + VEHICLE_FIELD.set(this, null); + oldVehicle.passengers.remove(this); + } + + VEHICLE_FIELD.set(this, entity); + entity.passengers.clear(); + entity.passengers.add(this); + + } catch (Exception ex) { + ConsoleLogger.logDebugException(ex); + } + } + + @Override + public Object getRawItemStack() { + return super.getItemStack(); + } +} diff --git a/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/EntityNMSSlime.java b/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/EntityNMSSlime.java index f8db5934..849b23d1 100644 --- a/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/EntityNMSSlime.java +++ b/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/EntityNMSSlime.java @@ -1,229 +1,243 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_9_R1; - -import org.bukkit.Bukkit; -import org.bukkit.craftbukkit.v1_9_R1.entity.CraftEntity; -import org.bukkit.event.player.PlayerInteractEntityEvent; - -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSSlime; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.Utils; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; - -import net.minecraft.server.v1_9_R1.AxisAlignedBB; -import net.minecraft.server.v1_9_R1.DamageSource; -import net.minecraft.server.v1_9_R1.Entity; -import net.minecraft.server.v1_9_R1.EntityDamageSource; -import net.minecraft.server.v1_9_R1.EntityPlayer; -import net.minecraft.server.v1_9_R1.EntitySlime; -import net.minecraft.server.v1_9_R1.NBTTagCompound; -import net.minecraft.server.v1_9_R1.PacketPlayOutMount; -import net.minecraft.server.v1_9_R1.SoundEffect; -import net.minecraft.server.v1_9_R1.World; - -public class EntityNMSSlime extends EntitySlime implements NMSSlime { - - private static final ReflectField VEHICLE_FIELD = new ReflectField(Entity.class, "as"); - - private boolean lockTick; - private HologramLine parentPiece; - - private int resendMountPacketTicks; - - public EntityNMSSlime(World world, HologramLine parentPiece) { - super(world); - super.persistent = true; - a(0.0F, 0.0F); - setSize(1); - setInvisible(true); - this.parentPiece = parentPiece; - forceSetBoundingBox(new NullBoundingBox()); - } - - @Override - public void a(AxisAlignedBB boundingBox) { - // Do not change it! - } - - public void forceSetBoundingBox(AxisAlignedBB boundingBox) { - super.a(boundingBox); - } - - @Override - public void m() { - - // So it won't get removed. - ticksLived = 0; - - if (resendMountPacketTicks++ > 20) { - resendMountPacketTicks = 0; - - if (by() != null) { - // Send a packet near to "remind" players that the slime is riding the armor stand (Spigot bug or client bug) - PacketPlayOutMount mountPacket = new PacketPlayOutMount(by()); - - for (Object obj : super.world.players) { - if (obj instanceof EntityPlayer) { - EntityPlayer nmsPlayer = (EntityPlayer) obj; - - double distanceSquared = Utils.square(nmsPlayer.locX - super.locX) + Utils.square(nmsPlayer.locZ - super.locZ); - if (distanceSquared < 1024 && nmsPlayer.playerConnection != null) { - nmsPlayer.playerConnection.sendPacket(mountPacket); - } - } - } - } - } - - if (!lockTick) { - super.m(); - } - } - - @Override - public void b(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public boolean c(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public boolean d(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public void e(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public void f(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public void a(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public boolean damageEntity(DamageSource damageSource, float amount) { - if (damageSource instanceof EntityDamageSource) { - EntityDamageSource entityDamageSource = (EntityDamageSource) damageSource; - if (entityDamageSource.getEntity() instanceof EntityPlayer) { - Bukkit.getPluginManager().callEvent(new PlayerInteractEntityEvent(((EntityPlayer) entityDamageSource.getEntity()).getBukkitEntity(), getBukkitEntity())); // Bukkit takes care of the exceptions - } - } - return false; - } - - @Override - public boolean isInvulnerable(DamageSource source) { - /* - * The field Entity.invulnerable is private. - * It's only used while saving NBTTags, but since the entity would be killed - * on chunk unload, we prefer to override isInvulnerable(). - */ - return true; - } - - @Override - public boolean isCollidable() { - return false; - } - - @Override - public void setCustomName(String customName) { - // Locks the custom name. - } - - @Override - public void setCustomNameVisible(boolean visible) { - // Locks the custom name. - } - - @Override - public void a(SoundEffect soundeffect, float f, float f1) { - // Remove sounds. - } - - @Override - public void setLockTick(boolean lock) { - lockTick = lock; - } - - @Override - public void die() { - // Prevent being killed. - } - - @Override - public CraftEntity getBukkitEntity() { - if (super.bukkitEntity == null) { - super.bukkitEntity = new CraftNMSSlime(super.world.getServer(), this); - } - return super.bukkitEntity; - } - - @Override - public boolean isDeadNMS() { - return super.dead; - } - - @Override - public void killEntityNMS() { - super.dead = true; - } - - @Override - public void setLocationNMS(double x, double y, double z) { - super.setPosition(x, y, z); - } - - @Override - public int getIdNMS() { - return super.getId(); - } - - @Override - public HologramLine getHologramLine() { - return parentPiece; - } - - @Override - public org.bukkit.entity.Entity getBukkitEntityNMS() { - return getBukkitEntity(); - } - - @Override - public void setPassengerOfNMS(NMSEntityBase vehicleBase) { - if (vehicleBase == null || !(vehicleBase instanceof Entity)) { - // It should never dismount - return; - } - - Entity entity = (Entity) vehicleBase; - - try { - if (super.by() != null) { - Entity oldVehicle = super.by(); - VEHICLE_FIELD.set(this, null); - oldVehicle.passengers.remove(this); - } - - VEHICLE_FIELD.set(this, entity); - entity.passengers.clear(); - entity.passengers.add(this); - - } catch (Exception ex) { - ConsoleLogger.logDebugException(ex); - } - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_9_R1; + +import org.bukkit.Bukkit; +import org.bukkit.craftbukkit.v1_9_R1.entity.CraftEntity; +import org.bukkit.event.player.PlayerInteractEntityEvent; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSSlime; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.Utils; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; + +import net.minecraft.server.v1_9_R1.AxisAlignedBB; +import net.minecraft.server.v1_9_R1.DamageSource; +import net.minecraft.server.v1_9_R1.Entity; +import net.minecraft.server.v1_9_R1.EntityDamageSource; +import net.minecraft.server.v1_9_R1.EntityPlayer; +import net.minecraft.server.v1_9_R1.EntitySlime; +import net.minecraft.server.v1_9_R1.NBTTagCompound; +import net.minecraft.server.v1_9_R1.PacketPlayOutMount; +import net.minecraft.server.v1_9_R1.SoundEffect; +import net.minecraft.server.v1_9_R1.World; + +public class EntityNMSSlime extends EntitySlime implements NMSSlime { + + private static final ReflectField VEHICLE_FIELD = new ReflectField(Entity.class, "as"); + + private boolean lockTick; + private HologramLine parentPiece; + + private int resendMountPacketTicks; + + public EntityNMSSlime(World world, HologramLine parentPiece) { + super(world); + super.persistent = true; + a(0.0F, 0.0F); + setSize(1); + setInvisible(true); + this.parentPiece = parentPiece; + forceSetBoundingBox(new NullBoundingBox()); + } + + @Override + public void a(AxisAlignedBB boundingBox) { + // Do not change it! + } + + public void forceSetBoundingBox(AxisAlignedBB boundingBox) { + super.a(boundingBox); + } + + @Override + public void m() { + + // So it won't get removed. + ticksLived = 0; + + if (resendMountPacketTicks++ > 20) { + resendMountPacketTicks = 0; + + if (by() != null) { + // Send a packet near to "remind" players that the slime is riding the armor stand (Spigot bug or client bug) + PacketPlayOutMount mountPacket = new PacketPlayOutMount(by()); + + for (Object obj : super.world.players) { + if (obj instanceof EntityPlayer) { + EntityPlayer nmsPlayer = (EntityPlayer) obj; + + double distanceSquared = Utils.square(nmsPlayer.locX - super.locX) + Utils.square(nmsPlayer.locZ - super.locZ); + if (distanceSquared < 1024 && nmsPlayer.playerConnection != null) { + nmsPlayer.playerConnection.sendPacket(mountPacket); + } + } + } + } + } + + if (!lockTick) { + super.m(); + } + } + + @Override + public void b(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public boolean c(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public boolean d(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public void e(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public void f(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public void a(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public boolean damageEntity(DamageSource damageSource, float amount) { + if (damageSource instanceof EntityDamageSource) { + EntityDamageSource entityDamageSource = (EntityDamageSource) damageSource; + if (entityDamageSource.getEntity() instanceof EntityPlayer) { + Bukkit.getPluginManager().callEvent(new PlayerInteractEntityEvent(((EntityPlayer) entityDamageSource.getEntity()).getBukkitEntity(), getBukkitEntity())); // Bukkit takes care of the exceptions + } + } + return false; + } + + @Override + public boolean isInvulnerable(DamageSource source) { + /* + * The field Entity.invulnerable is private. + * It's only used while saving NBTTags, but since the entity would be killed + * on chunk unload, we prefer to override isInvulnerable(). + */ + return true; + } + + @Override + public boolean isCollidable() { + return false; + } + + @Override + public void setCustomName(String customName) { + // Locks the custom name. + } + + @Override + public void setCustomNameVisible(boolean visible) { + // Locks the custom name. + } + + @Override + public void a(SoundEffect soundeffect, float f, float f1) { + // Remove sounds. + } + + @Override + public void setLockTick(boolean lock) { + lockTick = lock; + } + + @Override + public void die() { + // Prevent being killed. + } + + @Override + public CraftEntity getBukkitEntity() { + if (super.bukkitEntity == null) { + super.bukkitEntity = new CraftNMSSlime(super.world.getServer(), this); + } + return super.bukkitEntity; + } + + @Override + public boolean isDeadNMS() { + return super.dead; + } + + @Override + public void killEntityNMS() { + super.dead = true; + } + + @Override + public void setLocationNMS(double x, double y, double z) { + super.setPosition(x, y, z); + } + + @Override + public int getIdNMS() { + return super.getId(); + } + + @Override + public HologramLine getHologramLine() { + return parentPiece; + } + + @Override + public org.bukkit.entity.Entity getBukkitEntityNMS() { + return getBukkitEntity(); + } + + @Override + public void setPassengerOfNMS(NMSEntityBase vehicleBase) { + if (vehicleBase == null || !(vehicleBase instanceof Entity)) { + // It should never dismount + return; + } + + Entity entity = (Entity) vehicleBase; + + try { + if (super.by() != null) { + Entity oldVehicle = super.by(); + VEHICLE_FIELD.set(this, null); + oldVehicle.passengers.remove(this); + } + + VEHICLE_FIELD.set(this, entity); + entity.passengers.clear(); + entity.passengers.add(this); + + } catch (Exception ex) { + ConsoleLogger.logDebugException(ex); + } + } +} diff --git a/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/NmsManagerImpl.java b/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/NmsManagerImpl.java index 448b4fa1..7fa18a46 100644 --- a/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/NmsManagerImpl.java +++ b/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/NmsManagerImpl.java @@ -1,124 +1,138 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_9_R1; - -import java.lang.reflect.Method; -import java.util.Map; - -import org.bukkit.Bukkit; -import org.bukkit.craftbukkit.v1_9_R1.CraftWorld; -import org.bukkit.craftbukkit.v1_9_R1.entity.CraftEntity; -import org.bukkit.inventory.ItemStack; - -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; -import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.Validator; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; - -import net.minecraft.server.v1_9_R1.Entity; -import net.minecraft.server.v1_9_R1.EntityTypes; -import net.minecraft.server.v1_9_R1.MathHelper; -import net.minecraft.server.v1_9_R1.World; -import net.minecraft.server.v1_9_R1.WorldServer; - -public class NmsManagerImpl implements NMSManager { - - private static final ReflectField, String>> ENTITY_NAMES_BY_CLASS_FIELD = new ReflectField, String>>(EntityTypes.class, "d"); - private static final ReflectField, Integer>> ENTITY_IDS_BY_CLASS_FIELD = new ReflectField, Integer>>(EntityTypes.class, "f"); - - private Method validateEntityMethod; - - @Override - public void setup() throws Exception { - registerCustomEntity(EntityNMSArmorStand.class, "ArmorStand", 30); - registerCustomEntity(EntityNMSItem.class, "Item", 1); - registerCustomEntity(EntityNMSSlime.class, "Slime", 55); - - validateEntityMethod = World.class.getDeclaredMethod("b", Entity.class); - validateEntityMethod.setAccessible(true); - } - - public void registerCustomEntity(Class entityClass, String name, int id) throws Exception { - ENTITY_NAMES_BY_CLASS_FIELD.getStatic().put(entityClass, name); - ENTITY_IDS_BY_CLASS_FIELD.getStatic().put(entityClass, Integer.valueOf(id)); - } - - @Override - public NMSItem spawnNMSItem(org.bukkit.World bukkitWorld, double x, double y, double z, ItemLine parentPiece, ItemStack stack, ItemPickupManager itemPickupManager) { - WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); - EntityNMSItem customItem = new EntityNMSItem(nmsWorld, parentPiece, itemPickupManager); - customItem.setLocationNMS(x, y, z); - customItem.setItemStackNMS(stack); - if (!addEntityToWorld(nmsWorld, customItem)) { - ConsoleLogger.handleSpawnFail(parentPiece); - } - return customItem; - } - - @Override - public EntityNMSSlime spawnNMSSlime(org.bukkit.World bukkitWorld, double x, double y, double z, HologramLine parentPiece) { - WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); - EntityNMSSlime touchSlime = new EntityNMSSlime(nmsWorld, parentPiece); - touchSlime.setLocationNMS(x, y, z); - if (!addEntityToWorld(nmsWorld, touchSlime)) { - ConsoleLogger.handleSpawnFail(parentPiece); - } - return touchSlime; - } - - @Override - public NMSArmorStand spawnNMSArmorStand(org.bukkit.World world, double x, double y, double z, HologramLine parentPiece) { - WorldServer nmsWorld = ((CraftWorld) world).getHandle(); - EntityNMSArmorStand invisibleArmorStand = new EntityNMSArmorStand(nmsWorld, parentPiece); - invisibleArmorStand.setLocationNMS(x, y, z); - if (!addEntityToWorld(nmsWorld, invisibleArmorStand)) { - ConsoleLogger.handleSpawnFail(parentPiece); - } - return invisibleArmorStand; - } - - private boolean addEntityToWorld(WorldServer nmsWorld, Entity nmsEntity) { - Validator.isTrue(Bukkit.isPrimaryThread(), "Async entity add"); - - final int chunkX = MathHelper.floor(nmsEntity.locX / 16.0); - final int chunkZ = MathHelper.floor(nmsEntity.locZ / 16.0); - - if (!nmsWorld.getChunkProviderServer().isChunkLoaded(chunkX, chunkZ)) { - // This should never happen - nmsEntity.dead = true; - return false; - } - - nmsWorld.getChunkAt(chunkX, chunkZ).a(nmsEntity); - nmsWorld.entityList.add(nmsEntity); - - try { - validateEntityMethod.invoke(nmsWorld, nmsEntity); - } catch (Exception e) { - e.printStackTrace(); - return false; - } - return true; - } - - @Override - public boolean isNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { - return ((CraftEntity) bukkitEntity).getHandle() instanceof NMSEntityBase; - } - - @Override - public NMSEntityBase getNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { - - Entity nmsEntity = ((CraftEntity) bukkitEntity).getHandle(); - if (nmsEntity instanceof NMSEntityBase) { - return ((NMSEntityBase) nmsEntity); - } - - return null; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_9_R1; + +import java.lang.reflect.Method; +import java.util.Map; + +import org.bukkit.Bukkit; +import org.bukkit.craftbukkit.v1_9_R1.CraftWorld; +import org.bukkit.craftbukkit.v1_9_R1.entity.CraftEntity; +import org.bukkit.inventory.ItemStack; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; +import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.Validator; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; + +import net.minecraft.server.v1_9_R1.Entity; +import net.minecraft.server.v1_9_R1.EntityTypes; +import net.minecraft.server.v1_9_R1.MathHelper; +import net.minecraft.server.v1_9_R1.World; +import net.minecraft.server.v1_9_R1.WorldServer; + +public class NmsManagerImpl implements NMSManager { + + private static final ReflectField, String>> ENTITY_NAMES_BY_CLASS_FIELD = new ReflectField, String>>(EntityTypes.class, "d"); + private static final ReflectField, Integer>> ENTITY_IDS_BY_CLASS_FIELD = new ReflectField, Integer>>(EntityTypes.class, "f"); + + private Method validateEntityMethod; + + @Override + public void setup() throws Exception { + registerCustomEntity(EntityNMSArmorStand.class, "ArmorStand", 30); + registerCustomEntity(EntityNMSItem.class, "Item", 1); + registerCustomEntity(EntityNMSSlime.class, "Slime", 55); + + validateEntityMethod = World.class.getDeclaredMethod("b", Entity.class); + validateEntityMethod.setAccessible(true); + } + + public void registerCustomEntity(Class entityClass, String name, int id) throws Exception { + ENTITY_NAMES_BY_CLASS_FIELD.getStatic().put(entityClass, name); + ENTITY_IDS_BY_CLASS_FIELD.getStatic().put(entityClass, Integer.valueOf(id)); + } + + @Override + public NMSItem spawnNMSItem(org.bukkit.World bukkitWorld, double x, double y, double z, ItemLine parentPiece, ItemStack stack, ItemPickupManager itemPickupManager) { + WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); + EntityNMSItem customItem = new EntityNMSItem(nmsWorld, parentPiece, itemPickupManager); + customItem.setLocationNMS(x, y, z); + customItem.setItemStackNMS(stack); + if (!addEntityToWorld(nmsWorld, customItem)) { + ConsoleLogger.handleSpawnFail(parentPiece); + } + return customItem; + } + + @Override + public EntityNMSSlime spawnNMSSlime(org.bukkit.World bukkitWorld, double x, double y, double z, HologramLine parentPiece) { + WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); + EntityNMSSlime touchSlime = new EntityNMSSlime(nmsWorld, parentPiece); + touchSlime.setLocationNMS(x, y, z); + if (!addEntityToWorld(nmsWorld, touchSlime)) { + ConsoleLogger.handleSpawnFail(parentPiece); + } + return touchSlime; + } + + @Override + public NMSArmorStand spawnNMSArmorStand(org.bukkit.World world, double x, double y, double z, HologramLine parentPiece) { + WorldServer nmsWorld = ((CraftWorld) world).getHandle(); + EntityNMSArmorStand invisibleArmorStand = new EntityNMSArmorStand(nmsWorld, parentPiece); + invisibleArmorStand.setLocationNMS(x, y, z); + if (!addEntityToWorld(nmsWorld, invisibleArmorStand)) { + ConsoleLogger.handleSpawnFail(parentPiece); + } + return invisibleArmorStand; + } + + private boolean addEntityToWorld(WorldServer nmsWorld, Entity nmsEntity) { + Validator.isTrue(Bukkit.isPrimaryThread(), "Async entity add"); + + final int chunkX = MathHelper.floor(nmsEntity.locX / 16.0); + final int chunkZ = MathHelper.floor(nmsEntity.locZ / 16.0); + + if (!nmsWorld.getChunkProviderServer().isChunkLoaded(chunkX, chunkZ)) { + // This should never happen + nmsEntity.dead = true; + return false; + } + + nmsWorld.getChunkAt(chunkX, chunkZ).a(nmsEntity); + nmsWorld.entityList.add(nmsEntity); + + try { + validateEntityMethod.invoke(nmsWorld, nmsEntity); + } catch (Exception e) { + e.printStackTrace(); + return false; + } + return true; + } + + @Override + public boolean isNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { + return ((CraftEntity) bukkitEntity).getHandle() instanceof NMSEntityBase; + } + + @Override + public NMSEntityBase getNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { + + Entity nmsEntity = ((CraftEntity) bukkitEntity).getHandle(); + if (nmsEntity instanceof NMSEntityBase) { + return ((NMSEntityBase) nmsEntity); + } + + return null; + } + +} diff --git a/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/NullBoundingBox.java b/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/NullBoundingBox.java index bccfc048..308b0499 100644 --- a/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/NullBoundingBox.java +++ b/NMS/v1_9_R1/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R1/NullBoundingBox.java @@ -1,111 +1,125 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_9_R1; - -import net.minecraft.server.v1_9_R1.AxisAlignedBB; -import net.minecraft.server.v1_9_R1.BlockPosition; -import net.minecraft.server.v1_9_R1.MovingObjectPosition; -import net.minecraft.server.v1_9_R1.Vec3D; - -public class NullBoundingBox extends AxisAlignedBB { - - public NullBoundingBox() { - super(0, 0, 0, 0, 0, 0); - } - - @Override - public double a() { - return 0.0; - } - - @Override - public double a(AxisAlignedBB arg0, double arg1) { - return 0.0; - } - - @Override - public AxisAlignedBB a(AxisAlignedBB arg0) { - return this; - } - - @Override - public AxisAlignedBB a(double arg0, double arg1, double arg2) { - return this; - } - - @Override - public MovingObjectPosition a(Vec3D arg0, Vec3D arg1) { - return super.a(arg0, arg1); - } - - @Override - public boolean a(Vec3D arg0) { - return false; - } - - @Override - public double b(AxisAlignedBB arg0, double arg1) { - return 0.0; - } - - @Override - public boolean b(AxisAlignedBB arg0) { - return false; - } - - @Override - public double c(AxisAlignedBB arg0, double arg1) { - return 0.0; - } - - @Override - public AxisAlignedBB c(double arg0, double arg1, double arg2) { - return this; - } - - @Override - public AxisAlignedBB grow(double arg0, double arg1, double arg2) { - return this; - } - - @Override - public AxisAlignedBB shrink(double arg0) { - return this; - } - - @Override - public AxisAlignedBB a(BlockPosition arg0) { - return this; - } - - @Override - public boolean a(double arg0, double arg1, double arg2, double arg3, double arg4, double arg5) { - return false; - } - - @Override - public boolean b(Vec3D arg0) { - return false; - } - - @Override - public boolean c(Vec3D arg0) { - return false; - } - - @Override - public boolean d(Vec3D arg0) { - return false; - } - - @Override - public AxisAlignedBB e(double arg0) { - return this; - } - - @Override - public AxisAlignedBB g(double arg0) { - return this; - } - - - -} \ No newline at end of file +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_9_R1; + +import net.minecraft.server.v1_9_R1.AxisAlignedBB; +import net.minecraft.server.v1_9_R1.BlockPosition; +import net.minecraft.server.v1_9_R1.MovingObjectPosition; +import net.minecraft.server.v1_9_R1.Vec3D; + +public class NullBoundingBox extends AxisAlignedBB { + + public NullBoundingBox() { + super(0, 0, 0, 0, 0, 0); + } + + @Override + public double a() { + return 0.0; + } + + @Override + public double a(AxisAlignedBB arg0, double arg1) { + return 0.0; + } + + @Override + public AxisAlignedBB a(AxisAlignedBB arg0) { + return this; + } + + @Override + public AxisAlignedBB a(double arg0, double arg1, double arg2) { + return this; + } + + @Override + public MovingObjectPosition a(Vec3D arg0, Vec3D arg1) { + return super.a(arg0, arg1); + } + + @Override + public boolean a(Vec3D arg0) { + return false; + } + + @Override + public double b(AxisAlignedBB arg0, double arg1) { + return 0.0; + } + + @Override + public boolean b(AxisAlignedBB arg0) { + return false; + } + + @Override + public double c(AxisAlignedBB arg0, double arg1) { + return 0.0; + } + + @Override + public AxisAlignedBB c(double arg0, double arg1, double arg2) { + return this; + } + + @Override + public AxisAlignedBB grow(double arg0, double arg1, double arg2) { + return this; + } + + @Override + public AxisAlignedBB shrink(double arg0) { + return this; + } + + @Override + public AxisAlignedBB a(BlockPosition arg0) { + return this; + } + + @Override + public boolean a(double arg0, double arg1, double arg2, double arg3, double arg4, double arg5) { + return false; + } + + @Override + public boolean b(Vec3D arg0) { + return false; + } + + @Override + public boolean c(Vec3D arg0) { + return false; + } + + @Override + public boolean d(Vec3D arg0) { + return false; + } + + @Override + public AxisAlignedBB e(double arg0) { + return this; + } + + @Override + public AxisAlignedBB g(double arg0) { + return this; + } + + + +} diff --git a/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/CraftNMSArmorStand.java b/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/CraftNMSArmorStand.java index 8e639987..d3396a07 100644 --- a/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/CraftNMSArmorStand.java +++ b/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/CraftNMSArmorStand.java @@ -1,68 +1,82 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_9_R2; - -import java.util.Collection; - -import org.bukkit.EntityEffect; -import org.bukkit.Location; -import org.bukkit.craftbukkit.v1_9_R2.CraftServer; -import org.bukkit.craftbukkit.v1_9_R2.entity.CraftArmorStand; -import org.bukkit.entity.Entity; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -import org.bukkit.inventory.ItemStack; -import org.bukkit.potion.PotionEffect; -import org.bukkit.util.EulerAngle; -import org.bukkit.util.Vector; - -public class CraftNMSArmorStand extends CraftArmorStand { - - public CraftNMSArmorStand(CraftServer server, EntityNMSArmorStand entity) { - super(server, entity); - } - - // Disallow all the bukkit methods. - - @Override - public void remove() { - // Cannot be removed, this is the most important to override. - } - - // Methods from Armor stand class - @Override public void setArms(boolean arms) { } - @Override public void setBasePlate(boolean basePlate) { } - @Override public void setBodyPose(EulerAngle pose) { } - @Override public void setBoots(ItemStack item) { } - @Override public void setChestplate(ItemStack item) { } - @Override public void setGravity(boolean gravity) { } - @Override public void setHeadPose(EulerAngle pose) { } - @Override public void setHelmet(ItemStack item) { } - @Override public void setItemInHand(ItemStack item) { } - @Override public void setLeftArmPose(EulerAngle pose) { } - @Override public void setLeftLegPose(EulerAngle pose) { } - @Override public void setLeggings(ItemStack item) { } - @Override public void setRightArmPose(EulerAngle pose) { } - @Override public void setRightLegPose(EulerAngle pose) { } - @Override public void setSmall(boolean small) { } - @Override public void setVisible(boolean visible) { } - @Override public void setMarker(boolean marker) { } - - // Methods from LivingEntity class - @Override public boolean addPotionEffect(PotionEffect effect) { return false; } - @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } - @Override public boolean addPotionEffects(Collection effects) { return false; } - @Override public void setRemoveWhenFarAway(boolean remove) { } - - // Methods from Entity - @Override public void setVelocity(Vector vel) { } - @Override public boolean teleport(Location loc) { return false; } - @Override public boolean teleport(Entity entity) { return false; } - @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } - @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } - @Override public void setFireTicks(int ticks) { } - @Override public boolean setPassenger(Entity entity) { return false; } - @Override public boolean eject() { return false; } - @Override public boolean leaveVehicle() { return false; } - @Override public void playEffect(EntityEffect effect) { } - @Override public void setCustomName(String name) { } - @Override public void setCustomNameVisible(boolean flag) { } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_9_R2; + +import java.util.Collection; + +import org.bukkit.EntityEffect; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_9_R2.CraftServer; +import org.bukkit.craftbukkit.v1_9_R2.entity.CraftArmorStand; +import org.bukkit.entity.Entity; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import org.bukkit.inventory.ItemStack; +import org.bukkit.potion.PotionEffect; +import org.bukkit.util.EulerAngle; +import org.bukkit.util.Vector; + +public class CraftNMSArmorStand extends CraftArmorStand { + + public CraftNMSArmorStand(CraftServer server, EntityNMSArmorStand entity) { + super(server, entity); + } + + // Disallow all the bukkit methods. + + @Override + public void remove() { + // Cannot be removed, this is the most important to override. + } + + // Methods from Armor stand class + @Override public void setArms(boolean arms) { } + @Override public void setBasePlate(boolean basePlate) { } + @Override public void setBodyPose(EulerAngle pose) { } + @Override public void setBoots(ItemStack item) { } + @Override public void setChestplate(ItemStack item) { } + @Override public void setGravity(boolean gravity) { } + @Override public void setHeadPose(EulerAngle pose) { } + @Override public void setHelmet(ItemStack item) { } + @Override public void setItemInHand(ItemStack item) { } + @Override public void setLeftArmPose(EulerAngle pose) { } + @Override public void setLeftLegPose(EulerAngle pose) { } + @Override public void setLeggings(ItemStack item) { } + @Override public void setRightArmPose(EulerAngle pose) { } + @Override public void setRightLegPose(EulerAngle pose) { } + @Override public void setSmall(boolean small) { } + @Override public void setVisible(boolean visible) { } + @Override public void setMarker(boolean marker) { } + + // Methods from LivingEntity class + @Override public boolean addPotionEffect(PotionEffect effect) { return false; } + @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } + @Override public boolean addPotionEffects(Collection effects) { return false; } + @Override public void setRemoveWhenFarAway(boolean remove) { } + + // Methods from Entity + @Override public void setVelocity(Vector vel) { } + @Override public boolean teleport(Location loc) { return false; } + @Override public boolean teleport(Entity entity) { return false; } + @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } + @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } + @Override public void setFireTicks(int ticks) { } + @Override public boolean setPassenger(Entity entity) { return false; } + @Override public boolean eject() { return false; } + @Override public boolean leaveVehicle() { return false; } + @Override public void playEffect(EntityEffect effect) { } + @Override public void setCustomName(String name) { } + @Override public void setCustomNameVisible(boolean flag) { } + +} diff --git a/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/CraftNMSItem.java b/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/CraftNMSItem.java index bcfac4ae..bae9d8ce 100644 --- a/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/CraftNMSItem.java +++ b/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/CraftNMSItem.java @@ -1,43 +1,57 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_9_R2; - -import org.bukkit.EntityEffect; -import org.bukkit.Location; -import org.bukkit.craftbukkit.v1_9_R2.CraftServer; -import org.bukkit.craftbukkit.v1_9_R2.entity.CraftItem; -import org.bukkit.entity.Entity; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -import org.bukkit.inventory.ItemStack; -import org.bukkit.util.Vector; - -public class CraftNMSItem extends CraftItem { - - public CraftNMSItem(CraftServer server, EntityNMSItem entity) { - super(server, entity); - } - - // Disallow all the bukkit methods. - - @Override - public void remove() { - // Cannot be removed, this is the most important to override. - } - - // Methods from Entity - @Override public void setVelocity(Vector vel) { } - @Override public boolean teleport(Location loc) { return false; } - @Override public boolean teleport(Entity entity) { return false; } - @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } - @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } - @Override public void setFireTicks(int ticks) { } - @Override public boolean setPassenger(Entity entity) { return false; } - @Override public boolean eject() { return false; } - @Override public boolean leaveVehicle() { return false; } - @Override public void playEffect(EntityEffect effect) { } - @Override public void setCustomName(String name) { } - @Override public void setCustomNameVisible(boolean flag) { } - - // Methods from Item - @Override public void setItemStack(ItemStack stack) { } - @Override public void setPickupDelay(int delay) { } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_9_R2; + +import org.bukkit.EntityEffect; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_9_R2.CraftServer; +import org.bukkit.craftbukkit.v1_9_R2.entity.CraftItem; +import org.bukkit.entity.Entity; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import org.bukkit.inventory.ItemStack; +import org.bukkit.util.Vector; + +public class CraftNMSItem extends CraftItem { + + public CraftNMSItem(CraftServer server, EntityNMSItem entity) { + super(server, entity); + } + + // Disallow all the bukkit methods. + + @Override + public void remove() { + // Cannot be removed, this is the most important to override. + } + + // Methods from Entity + @Override public void setVelocity(Vector vel) { } + @Override public boolean teleport(Location loc) { return false; } + @Override public boolean teleport(Entity entity) { return false; } + @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } + @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } + @Override public void setFireTicks(int ticks) { } + @Override public boolean setPassenger(Entity entity) { return false; } + @Override public boolean eject() { return false; } + @Override public boolean leaveVehicle() { return false; } + @Override public void playEffect(EntityEffect effect) { } + @Override public void setCustomName(String name) { } + @Override public void setCustomNameVisible(boolean flag) { } + + // Methods from Item + @Override public void setItemStack(ItemStack stack) { } + @Override public void setPickupDelay(int delay) { } + +} diff --git a/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/CraftNMSSlime.java b/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/CraftNMSSlime.java index a84e1487..a046f504 100644 --- a/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/CraftNMSSlime.java +++ b/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/CraftNMSSlime.java @@ -1,50 +1,64 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_9_R2; - -import java.util.Collection; - -import org.bukkit.EntityEffect; -import org.bukkit.Location; -import org.bukkit.craftbukkit.v1_9_R2.CraftServer; -import org.bukkit.craftbukkit.v1_9_R2.entity.CraftSlime; -import org.bukkit.entity.Entity; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -import org.bukkit.potion.PotionEffect; -import org.bukkit.util.Vector; - -public class CraftNMSSlime extends CraftSlime { - - public CraftNMSSlime(CraftServer server, EntityNMSSlime entity) { - super(server, entity); - } - - // Disallow all the bukkit methods. - - @Override - public void remove() { - // Cannot be removed, this is the most important to override. - } - - // Methods from LivingEntity class - @Override public boolean addPotionEffect(PotionEffect effect) { return false; } - @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } - @Override public boolean addPotionEffects(Collection effects) { return false; } - @Override public void setRemoveWhenFarAway(boolean remove) { } - - // Methods from Entity - @Override public void setVelocity(Vector vel) { } - @Override public boolean teleport(Location loc) { return false; } - @Override public boolean teleport(Entity entity) { return false; } - @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } - @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } - @Override public void setFireTicks(int ticks) { } - @Override public boolean setPassenger(Entity entity) { return false; } - @Override public boolean eject() { return false; } - @Override public boolean leaveVehicle() { return false; } - @Override public void playEffect(EntityEffect effect) { } - @Override public void setCustomName(String name) { } - @Override public void setCustomNameVisible(boolean flag) { } - - // Methods from Slime - @Override public void setSize(int size) { } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_9_R2; + +import java.util.Collection; + +import org.bukkit.EntityEffect; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_9_R2.CraftServer; +import org.bukkit.craftbukkit.v1_9_R2.entity.CraftSlime; +import org.bukkit.entity.Entity; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import org.bukkit.potion.PotionEffect; +import org.bukkit.util.Vector; + +public class CraftNMSSlime extends CraftSlime { + + public CraftNMSSlime(CraftServer server, EntityNMSSlime entity) { + super(server, entity); + } + + // Disallow all the bukkit methods. + + @Override + public void remove() { + // Cannot be removed, this is the most important to override. + } + + // Methods from LivingEntity class + @Override public boolean addPotionEffect(PotionEffect effect) { return false; } + @Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; } + @Override public boolean addPotionEffects(Collection effects) { return false; } + @Override public void setRemoveWhenFarAway(boolean remove) { } + + // Methods from Entity + @Override public void setVelocity(Vector vel) { } + @Override public boolean teleport(Location loc) { return false; } + @Override public boolean teleport(Entity entity) { return false; } + @Override public boolean teleport(Location loc, TeleportCause cause) { return false; } + @Override public boolean teleport(Entity entity, TeleportCause cause) { return false; } + @Override public void setFireTicks(int ticks) { } + @Override public boolean setPassenger(Entity entity) { return false; } + @Override public boolean eject() { return false; } + @Override public boolean leaveVehicle() { return false; } + @Override public void playEffect(EntityEffect effect) { } + @Override public void setCustomName(String name) { } + @Override public void setCustomNameVisible(boolean flag) { } + + // Methods from Slime + @Override public void setSize(int size) { } + +} diff --git a/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/EntityNMSArmorStand.java b/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/EntityNMSArmorStand.java index d9735e9b..ac95f21e 100644 --- a/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/EntityNMSArmorStand.java +++ b/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/EntityNMSArmorStand.java @@ -1,231 +1,245 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_9_R2; - -import org.bukkit.craftbukkit.v1_9_R2.entity.CraftEntity; - -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; -import com.gmail.filoghost.holographicdisplays.disk.Configuration; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; -import com.gmail.filoghost.holographicdisplays.util.Utils; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectionUtils; - -import net.minecraft.server.v1_9_R2.AxisAlignedBB; -import net.minecraft.server.v1_9_R2.DamageSource; -import net.minecraft.server.v1_9_R2.EntityArmorStand; -import net.minecraft.server.v1_9_R2.EntityHuman; -import net.minecraft.server.v1_9_R2.EntityPlayer; -import net.minecraft.server.v1_9_R2.EnumHand; -import net.minecraft.server.v1_9_R2.EnumInteractionResult; -import net.minecraft.server.v1_9_R2.EnumItemSlot; -import net.minecraft.server.v1_9_R2.ItemStack; -import net.minecraft.server.v1_9_R2.NBTTagCompound; -import net.minecraft.server.v1_9_R2.PacketPlayOutEntityTeleport; -import net.minecraft.server.v1_9_R2.SoundEffect; -import net.minecraft.server.v1_9_R2.Vec3D; -import net.minecraft.server.v1_9_R2.World; - -public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorStand { - - private boolean lockTick; - private HologramLine parentPiece; - - public EntityNMSArmorStand(World world, HologramLine parentPiece) { - super(world); - super.setInvisible(true); - super.setSmall(true); - super.setArms(false); - super.setGravity(true); - super.setBasePlate(true); - super.setMarker(true); - this.parentPiece = parentPiece; - forceSetBoundingBox(new NullBoundingBox()); - } - - - @Override - public void b(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public boolean c(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public boolean d(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public NBTTagCompound e(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return nbttagcompound; - } - - @Override - public void f(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public void a(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - - @Override - public boolean isInvulnerable(DamageSource source) { - /* - * The field Entity.invulnerable is private. - * It's only used while saving NBTTags, but since the entity would be killed - * on chunk unload, we prefer to override isInvulnerable(). - */ - return true; - } - - @Override - public boolean isCollidable() { - return false; - } - - @Override - public void setCustomName(String customName) { - // Locks the custom name. - } - - @Override - public void setCustomNameVisible(boolean visible) { - // Locks the custom name. - } - - @Override - public EnumInteractionResult a(EntityHuman human, Vec3D vec3d, ItemStack itemstack, EnumHand enumhand) { - // Prevent stand being equipped - return EnumInteractionResult.PASS; - } - - @Override - public boolean c(int i, ItemStack item) { - // Prevent stand being equipped - return false; - } - - @Override - public void setSlot(EnumItemSlot enumitemslot, ItemStack itemstack) { - // Prevent stand being equipped - } - - @Override - public void a(AxisAlignedBB boundingBox) { - // Do not change it! - } - - public void forceSetBoundingBox(AxisAlignedBB boundingBox) { - super.a(boundingBox); - } - - @Override - public int getId() { - if (Configuration.preciseHologramMovement) { - StackTraceElement element = ReflectionUtils.getStackTraceElement(2); - if (element != null && element.getFileName() != null && element.getFileName().equals("EntityTrackerEntry.java") && element.getLineNumber() > 142 && element.getLineNumber() < 152) { - // Then this method is being called when creating a new packet, we return a fake ID! - return -1; - } - } - - return super.getId(); - } - - @Override - public void m() { - if (!lockTick) { - super.m(); - } - } - - @Override - public void a(SoundEffect soundeffect, float f, float f1) { - // Remove sounds. - } - - @Override - public void setCustomNameNMS(String name) { - if (name != null && name.length() > 300) { - name = name.substring(0, 300); - } - super.setCustomName(name); - super.setCustomNameVisible(name != null && !name.isEmpty()); - } - - @Override - public String getCustomNameNMS() { - return super.getCustomName(); - } - - @Override - public void setLockTick(boolean lock) { - lockTick = lock; - } - - @Override - public void die() { - // Prevent being killed. - } - - @Override - public CraftEntity getBukkitEntity() { - if (super.bukkitEntity == null) { - super.bukkitEntity = new CraftNMSArmorStand(super.world.getServer(), this); - } - return super.bukkitEntity; - } - - @Override - public void killEntityNMS() { - super.dead = true; - } - - @Override - public void setLocationNMS(double x, double y, double z) { - super.setPosition(x, y, z); - - if (Configuration.preciseHologramMovement) { - // Send a packet near to update the position. - PacketPlayOutEntityTeleport teleportPacket = new PacketPlayOutEntityTeleport(this); - - for (Object obj : super.world.players) { - if (obj instanceof EntityPlayer) { - EntityPlayer nmsPlayer = (EntityPlayer) obj; - - double distanceSquared = Utils.square(nmsPlayer.locX - super.locX) + Utils.square(nmsPlayer.locZ - super.locZ); - if (distanceSquared < 8192 && nmsPlayer.playerConnection != null) { - nmsPlayer.playerConnection.sendPacket(teleportPacket); - } - } - } - } - } - - @Override - public boolean isDeadNMS() { - return super.dead; - } - - @Override - public int getIdNMS() { - return super.getId(); // Return the real ID without checking the stack trace. - } - - @Override - public HologramLine getHologramLine() { - return parentPiece; - } - - @Override - public org.bukkit.entity.Entity getBukkitEntityNMS() { - return getBukkitEntity(); - } -} \ No newline at end of file +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_9_R2; + +import org.bukkit.craftbukkit.v1_9_R2.entity.CraftEntity; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; +import com.gmail.filoghost.holographicdisplays.disk.Configuration; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; +import com.gmail.filoghost.holographicdisplays.util.Utils; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectionUtils; + +import net.minecraft.server.v1_9_R2.AxisAlignedBB; +import net.minecraft.server.v1_9_R2.DamageSource; +import net.minecraft.server.v1_9_R2.EntityArmorStand; +import net.minecraft.server.v1_9_R2.EntityHuman; +import net.minecraft.server.v1_9_R2.EntityPlayer; +import net.minecraft.server.v1_9_R2.EnumHand; +import net.minecraft.server.v1_9_R2.EnumInteractionResult; +import net.minecraft.server.v1_9_R2.EnumItemSlot; +import net.minecraft.server.v1_9_R2.ItemStack; +import net.minecraft.server.v1_9_R2.NBTTagCompound; +import net.minecraft.server.v1_9_R2.PacketPlayOutEntityTeleport; +import net.minecraft.server.v1_9_R2.SoundEffect; +import net.minecraft.server.v1_9_R2.Vec3D; +import net.minecraft.server.v1_9_R2.World; + +public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorStand { + + private boolean lockTick; + private HologramLine parentPiece; + + public EntityNMSArmorStand(World world, HologramLine parentPiece) { + super(world); + super.setInvisible(true); + super.setSmall(true); + super.setArms(false); + super.setGravity(true); + super.setBasePlate(true); + super.setMarker(true); + this.parentPiece = parentPiece; + forceSetBoundingBox(new NullBoundingBox()); + } + + + @Override + public void b(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public boolean c(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public boolean d(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public NBTTagCompound e(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return nbttagcompound; + } + + @Override + public void f(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public void a(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + + @Override + public boolean isInvulnerable(DamageSource source) { + /* + * The field Entity.invulnerable is private. + * It's only used while saving NBTTags, but since the entity would be killed + * on chunk unload, we prefer to override isInvulnerable(). + */ + return true; + } + + @Override + public boolean isCollidable() { + return false; + } + + @Override + public void setCustomName(String customName) { + // Locks the custom name. + } + + @Override + public void setCustomNameVisible(boolean visible) { + // Locks the custom name. + } + + @Override + public EnumInteractionResult a(EntityHuman human, Vec3D vec3d, ItemStack itemstack, EnumHand enumhand) { + // Prevent stand being equipped + return EnumInteractionResult.PASS; + } + + @Override + public boolean c(int i, ItemStack item) { + // Prevent stand being equipped + return false; + } + + @Override + public void setSlot(EnumItemSlot enumitemslot, ItemStack itemstack) { + // Prevent stand being equipped + } + + @Override + public void a(AxisAlignedBB boundingBox) { + // Do not change it! + } + + public void forceSetBoundingBox(AxisAlignedBB boundingBox) { + super.a(boundingBox); + } + + @Override + public int getId() { + if (Configuration.preciseHologramMovement) { + StackTraceElement element = ReflectionUtils.getStackTraceElement(2); + if (element != null && element.getFileName() != null && element.getFileName().equals("EntityTrackerEntry.java") && element.getLineNumber() > 142 && element.getLineNumber() < 152) { + // Then this method is being called when creating a new packet, we return a fake ID! + return -1; + } + } + + return super.getId(); + } + + @Override + public void m() { + if (!lockTick) { + super.m(); + } + } + + @Override + public void a(SoundEffect soundeffect, float f, float f1) { + // Remove sounds. + } + + @Override + public void setCustomNameNMS(String name) { + if (name != null && name.length() > 300) { + name = name.substring(0, 300); + } + super.setCustomName(name); + super.setCustomNameVisible(name != null && !name.isEmpty()); + } + + @Override + public String getCustomNameNMS() { + return super.getCustomName(); + } + + @Override + public void setLockTick(boolean lock) { + lockTick = lock; + } + + @Override + public void die() { + // Prevent being killed. + } + + @Override + public CraftEntity getBukkitEntity() { + if (super.bukkitEntity == null) { + super.bukkitEntity = new CraftNMSArmorStand(super.world.getServer(), this); + } + return super.bukkitEntity; + } + + @Override + public void killEntityNMS() { + super.dead = true; + } + + @Override + public void setLocationNMS(double x, double y, double z) { + super.setPosition(x, y, z); + + if (Configuration.preciseHologramMovement) { + // Send a packet near to update the position. + PacketPlayOutEntityTeleport teleportPacket = new PacketPlayOutEntityTeleport(this); + + for (Object obj : super.world.players) { + if (obj instanceof EntityPlayer) { + EntityPlayer nmsPlayer = (EntityPlayer) obj; + + double distanceSquared = Utils.square(nmsPlayer.locX - super.locX) + Utils.square(nmsPlayer.locZ - super.locZ); + if (distanceSquared < 8192 && nmsPlayer.playerConnection != null) { + nmsPlayer.playerConnection.sendPacket(teleportPacket); + } + } + } + } + } + + @Override + public boolean isDeadNMS() { + return super.dead; + } + + @Override + public int getIdNMS() { + return super.getId(); // Return the real ID without checking the stack trace. + } + + @Override + public HologramLine getHologramLine() { + return parentPiece; + } + + @Override + public org.bukkit.entity.Entity getBukkitEntityNMS() { + return getBukkitEntity(); + } +} diff --git a/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/EntityNMSItem.java b/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/EntityNMSItem.java index 2c16ba72..ab19138d 100644 --- a/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/EntityNMSItem.java +++ b/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/EntityNMSItem.java @@ -1,271 +1,285 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_9_R2; - -import org.bukkit.craftbukkit.v1_9_R2.entity.CraftEntity; -import org.bukkit.craftbukkit.v1_9_R2.inventory.CraftItemStack; -import org.bukkit.entity.Player; - -import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.ItemUtils; -import com.gmail.filoghost.holographicdisplays.util.Utils; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; - -import net.minecraft.server.v1_9_R2.AxisAlignedBB; -import net.minecraft.server.v1_9_R2.Blocks; -import net.minecraft.server.v1_9_R2.DamageSource; -import net.minecraft.server.v1_9_R2.Entity; -import net.minecraft.server.v1_9_R2.EntityHuman; -import net.minecraft.server.v1_9_R2.EntityItem; -import net.minecraft.server.v1_9_R2.EntityPlayer; -import net.minecraft.server.v1_9_R2.ItemStack; -import net.minecraft.server.v1_9_R2.NBTTagCompound; -import net.minecraft.server.v1_9_R2.NBTTagList; -import net.minecraft.server.v1_9_R2.NBTTagString; -import net.minecraft.server.v1_9_R2.PacketPlayOutMount; -import net.minecraft.server.v1_9_R2.World; - -public class EntityNMSItem extends EntityItem implements NMSItem { - - private static final ReflectField VEHICLE_FIELD = new ReflectField(Entity.class, "at"); - - private boolean lockTick; - private ItemLine parentPiece; - private ItemPickupManager itemPickupManager; - - private int resendMountPacketTicks; - - public EntityNMSItem(World world, ItemLine piece, ItemPickupManager itemPickupManager) { - super(world); - super.pickupDelay = Integer.MAX_VALUE; - this.parentPiece = piece; - this.itemPickupManager = itemPickupManager; - forceSetBoundingBox(new NullBoundingBox()); - } - - @Override - public void m() { - - // So it won't get removed. - ticksLived = 0; - - if (resendMountPacketTicks++ > 20) { - resendMountPacketTicks = 0; - - if (bz() != null) { - // Send a packet near to "remind" players that the item is riding the armor stand (Spigot bug or client bug) - PacketPlayOutMount mountPacket = new PacketPlayOutMount(bz()); - - for (Object obj : super.world.players) { - if (obj instanceof EntityPlayer) { - EntityPlayer nmsPlayer = (EntityPlayer) obj; - - double distanceSquared = Utils.square(nmsPlayer.locX - super.locX) + Utils.square(nmsPlayer.locZ - super.locZ); - if (distanceSquared < 1024 && nmsPlayer.playerConnection != null) { - nmsPlayer.playerConnection.sendPacket(mountPacket); - } - } - } - } - } - - if (!lockTick) { - super.m(); - } - } - - // Method called when a player is near. - @Override - public void d(EntityHuman human) { - - if (human.locY < super.locY - 1.5 || human.locY > super.locY + 1.0) { - // Too low or too high, it's a bit weird. - return; - } - - if (parentPiece.getPickupHandler() != null && human instanceof EntityPlayer) { - itemPickupManager.handleItemLinePickup((Player) human.getBukkitEntity(), parentPiece.getPickupHandler(), parentPiece.getParent()); - // It is never added to the inventory. - } - } - - @Override - public void b(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public boolean c(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public boolean d(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public NBTTagCompound e(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return nbttagcompound; - } - - @Override - public void f(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public void a(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public boolean isInvulnerable(DamageSource source) { - /* - * The field Entity.invulnerable is private. - * It's only used while saving NBTTags, but since the entity would be killed - * on chunk unload, we prefer to override isInvulnerable(). - */ - return true; - } - - @Override - public boolean isCollidable() { - return false; - } - - @Override - public void a(AxisAlignedBB boundingBox) { - // Do not change it! - } - - public void forceSetBoundingBox(AxisAlignedBB boundingBox) { - super.a(boundingBox); - } - - @Override - public void inactiveTick() { - // Check inactive ticks. - - if (!lockTick) { - super.inactiveTick(); - } - } - - @Override - public void setLockTick(boolean lock) { - lockTick = lock; - } - - @Override - public void die() { - // Prevent being killed. - } - - @Override - public CraftEntity getBukkitEntity() { - if (super.bukkitEntity == null) { - super.bukkitEntity = new CraftNMSItem(super.world.getServer(), this); - } - return super.bukkitEntity; - } - - @Override - public boolean isDeadNMS() { - return super.dead; - } - - @Override - public void killEntityNMS() { - super.dead = true; - } - - @Override - public void setLocationNMS(double x, double y, double z) { - super.setPosition(x, y, z); - } - - @Override - public void setItemStackNMS(org.bukkit.inventory.ItemStack stack) { - ItemStack newItem = CraftItemStack.asNMSCopy(stack); - - if (newItem == null) { - newItem = new ItemStack(Blocks.BEDROCK); - } - - if (newItem.getTag() == null) { - newItem.setTag(new NBTTagCompound()); - } - NBTTagCompound display = newItem.getTag().getCompound("display"); - - if (!newItem.getTag().hasKey("display")) { - newItem.getTag().set("display", display); - } - - NBTTagList tagList = new NBTTagList(); - tagList.add(new NBTTagString(ItemUtils.ANTISTACK_LORE)); // Antistack lore - - display.set("Lore", tagList); - newItem.count = 0; - setItemStack(newItem); - } - - @Override - public int getIdNMS() { - return super.getId(); - } - - @Override - public ItemLine getHologramLine() { - return parentPiece; - } - - @Override - public void allowPickup(boolean pickup) { - if (pickup) { - super.pickupDelay = 0; - } else { - super.pickupDelay = Integer.MAX_VALUE; - } - } - - @Override - public org.bukkit.entity.Entity getBukkitEntityNMS() { - return getBukkitEntity(); - } - - @Override - public void setPassengerOfNMS(NMSEntityBase vehicleBase) { - if (vehicleBase == null || !(vehicleBase instanceof Entity)) { - // It should never dismount - return; - } - - Entity entity = (Entity) vehicleBase; - - try { - if (super.bz() != null) { - Entity oldVehicle = super.bz(); - VEHICLE_FIELD.set(this, null); - oldVehicle.passengers.remove(this); - } - - VEHICLE_FIELD.set(this, entity); - entity.passengers.clear(); - entity.passengers.add(this); - - } catch (Exception ex) { - ConsoleLogger.logDebugException(ex); - } - } - - @Override - public Object getRawItemStack() { - return super.getItemStack(); - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_9_R2; + +import org.bukkit.craftbukkit.v1_9_R2.entity.CraftEntity; +import org.bukkit.craftbukkit.v1_9_R2.inventory.CraftItemStack; +import org.bukkit.entity.Player; + +import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.ItemUtils; +import com.gmail.filoghost.holographicdisplays.util.Utils; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; + +import net.minecraft.server.v1_9_R2.AxisAlignedBB; +import net.minecraft.server.v1_9_R2.Blocks; +import net.minecraft.server.v1_9_R2.DamageSource; +import net.minecraft.server.v1_9_R2.Entity; +import net.minecraft.server.v1_9_R2.EntityHuman; +import net.minecraft.server.v1_9_R2.EntityItem; +import net.minecraft.server.v1_9_R2.EntityPlayer; +import net.minecraft.server.v1_9_R2.ItemStack; +import net.minecraft.server.v1_9_R2.NBTTagCompound; +import net.minecraft.server.v1_9_R2.NBTTagList; +import net.minecraft.server.v1_9_R2.NBTTagString; +import net.minecraft.server.v1_9_R2.PacketPlayOutMount; +import net.minecraft.server.v1_9_R2.World; + +public class EntityNMSItem extends EntityItem implements NMSItem { + + private static final ReflectField VEHICLE_FIELD = new ReflectField(Entity.class, "at"); + + private boolean lockTick; + private ItemLine parentPiece; + private ItemPickupManager itemPickupManager; + + private int resendMountPacketTicks; + + public EntityNMSItem(World world, ItemLine piece, ItemPickupManager itemPickupManager) { + super(world); + super.pickupDelay = Integer.MAX_VALUE; + this.parentPiece = piece; + this.itemPickupManager = itemPickupManager; + forceSetBoundingBox(new NullBoundingBox()); + } + + @Override + public void m() { + + // So it won't get removed. + ticksLived = 0; + + if (resendMountPacketTicks++ > 20) { + resendMountPacketTicks = 0; + + if (bz() != null) { + // Send a packet near to "remind" players that the item is riding the armor stand (Spigot bug or client bug) + PacketPlayOutMount mountPacket = new PacketPlayOutMount(bz()); + + for (Object obj : super.world.players) { + if (obj instanceof EntityPlayer) { + EntityPlayer nmsPlayer = (EntityPlayer) obj; + + double distanceSquared = Utils.square(nmsPlayer.locX - super.locX) + Utils.square(nmsPlayer.locZ - super.locZ); + if (distanceSquared < 1024 && nmsPlayer.playerConnection != null) { + nmsPlayer.playerConnection.sendPacket(mountPacket); + } + } + } + } + } + + if (!lockTick) { + super.m(); + } + } + + // Method called when a player is near. + @Override + public void d(EntityHuman human) { + + if (human.locY < super.locY - 1.5 || human.locY > super.locY + 1.0) { + // Too low or too high, it's a bit weird. + return; + } + + if (parentPiece.getPickupHandler() != null && human instanceof EntityPlayer) { + itemPickupManager.handleItemLinePickup((Player) human.getBukkitEntity(), parentPiece.getPickupHandler(), parentPiece.getParent()); + // It is never added to the inventory. + } + } + + @Override + public void b(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public boolean c(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public boolean d(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public NBTTagCompound e(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return nbttagcompound; + } + + @Override + public void f(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public void a(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public boolean isInvulnerable(DamageSource source) { + /* + * The field Entity.invulnerable is private. + * It's only used while saving NBTTags, but since the entity would be killed + * on chunk unload, we prefer to override isInvulnerable(). + */ + return true; + } + + @Override + public boolean isCollidable() { + return false; + } + + @Override + public void a(AxisAlignedBB boundingBox) { + // Do not change it! + } + + public void forceSetBoundingBox(AxisAlignedBB boundingBox) { + super.a(boundingBox); + } + + @Override + public void inactiveTick() { + // Check inactive ticks. + + if (!lockTick) { + super.inactiveTick(); + } + } + + @Override + public void setLockTick(boolean lock) { + lockTick = lock; + } + + @Override + public void die() { + // Prevent being killed. + } + + @Override + public CraftEntity getBukkitEntity() { + if (super.bukkitEntity == null) { + super.bukkitEntity = new CraftNMSItem(super.world.getServer(), this); + } + return super.bukkitEntity; + } + + @Override + public boolean isDeadNMS() { + return super.dead; + } + + @Override + public void killEntityNMS() { + super.dead = true; + } + + @Override + public void setLocationNMS(double x, double y, double z) { + super.setPosition(x, y, z); + } + + @Override + public void setItemStackNMS(org.bukkit.inventory.ItemStack stack) { + ItemStack newItem = CraftItemStack.asNMSCopy(stack); + + if (newItem == null) { + newItem = new ItemStack(Blocks.BEDROCK); + } + + if (newItem.getTag() == null) { + newItem.setTag(new NBTTagCompound()); + } + NBTTagCompound display = newItem.getTag().getCompound("display"); + + if (!newItem.getTag().hasKey("display")) { + newItem.getTag().set("display", display); + } + + NBTTagList tagList = new NBTTagList(); + tagList.add(new NBTTagString(ItemUtils.ANTISTACK_LORE)); // Antistack lore + + display.set("Lore", tagList); + newItem.count = 0; + setItemStack(newItem); + } + + @Override + public int getIdNMS() { + return super.getId(); + } + + @Override + public ItemLine getHologramLine() { + return parentPiece; + } + + @Override + public void allowPickup(boolean pickup) { + if (pickup) { + super.pickupDelay = 0; + } else { + super.pickupDelay = Integer.MAX_VALUE; + } + } + + @Override + public org.bukkit.entity.Entity getBukkitEntityNMS() { + return getBukkitEntity(); + } + + @Override + public void setPassengerOfNMS(NMSEntityBase vehicleBase) { + if (vehicleBase == null || !(vehicleBase instanceof Entity)) { + // It should never dismount + return; + } + + Entity entity = (Entity) vehicleBase; + + try { + if (super.bz() != null) { + Entity oldVehicle = super.bz(); + VEHICLE_FIELD.set(this, null); + oldVehicle.passengers.remove(this); + } + + VEHICLE_FIELD.set(this, entity); + entity.passengers.clear(); + entity.passengers.add(this); + + } catch (Exception ex) { + ConsoleLogger.logDebugException(ex); + } + } + + @Override + public Object getRawItemStack() { + return super.getItemStack(); + } +} diff --git a/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/EntityNMSSlime.java b/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/EntityNMSSlime.java index 06f637ad..d75188a3 100644 --- a/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/EntityNMSSlime.java +++ b/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/EntityNMSSlime.java @@ -1,230 +1,244 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_9_R2; - -import org.bukkit.Bukkit; -import org.bukkit.craftbukkit.v1_9_R2.entity.CraftEntity; -import org.bukkit.event.player.PlayerInteractEntityEvent; - -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSSlime; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.Utils; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; - -import net.minecraft.server.v1_9_R2.AxisAlignedBB; -import net.minecraft.server.v1_9_R2.DamageSource; -import net.minecraft.server.v1_9_R2.Entity; -import net.minecraft.server.v1_9_R2.EntityDamageSource; -import net.minecraft.server.v1_9_R2.EntityPlayer; -import net.minecraft.server.v1_9_R2.EntitySlime; -import net.minecraft.server.v1_9_R2.NBTTagCompound; -import net.minecraft.server.v1_9_R2.PacketPlayOutMount; -import net.minecraft.server.v1_9_R2.SoundEffect; -import net.minecraft.server.v1_9_R2.World; - -public class EntityNMSSlime extends EntitySlime implements NMSSlime { - - private static final ReflectField VEHICLE_FIELD = new ReflectField(Entity.class, "at"); - - private boolean lockTick; - private HologramLine parentPiece; - - private int resendMountPacketTicks; - - public EntityNMSSlime(World world, HologramLine parentPiece) { - super(world); - super.persistent = true; - a(0.0F, 0.0F); - setSize(1); - setInvisible(true); - this.parentPiece = parentPiece; - forceSetBoundingBox(new NullBoundingBox()); - } - - @Override - public void a(AxisAlignedBB boundingBox) { - // Do not change it! - } - - public void forceSetBoundingBox(AxisAlignedBB boundingBox) { - super.a(boundingBox); - } - - @Override - public void m() { - - // So it won't get removed. - ticksLived = 0; - - if (resendMountPacketTicks++ > 20) { - resendMountPacketTicks = 0; - - if (bz() != null) { - // Send a packet near to "remind" players that the slime is riding the armor stand (Spigot bug or client bug) - PacketPlayOutMount mountPacket = new PacketPlayOutMount(bz()); - - for (Object obj : super.world.players) { - if (obj instanceof EntityPlayer) { - EntityPlayer nmsPlayer = (EntityPlayer) obj; - - double distanceSquared = Utils.square(nmsPlayer.locX - super.locX) + Utils.square(nmsPlayer.locZ - super.locZ); - if (distanceSquared < 1024 && nmsPlayer.playerConnection != null) { - nmsPlayer.playerConnection.sendPacket(mountPacket); - } - } - } - } - } - - if (!lockTick) { - super.m(); - } - } - - @Override - public void b(NBTTagCompound nbttagcompound) { - // Do not save NBT. - } - - @Override - public boolean c(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public boolean d(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return false; - } - - @Override - public NBTTagCompound e(NBTTagCompound nbttagcompound) { - // Do not save NBT. - return nbttagcompound; - } - - @Override - public void f(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public void a(NBTTagCompound nbttagcompound) { - // Do not load NBT. - } - - @Override - public boolean damageEntity(DamageSource damageSource, float amount) { - if (damageSource instanceof EntityDamageSource) { - EntityDamageSource entityDamageSource = (EntityDamageSource) damageSource; - if (entityDamageSource.getEntity() instanceof EntityPlayer) { - Bukkit.getPluginManager().callEvent(new PlayerInteractEntityEvent(((EntityPlayer) entityDamageSource.getEntity()).getBukkitEntity(), getBukkitEntity())); // Bukkit takes care of the exceptions - } - } - return false; - } - - @Override - public boolean isInvulnerable(DamageSource source) { - /* - * The field Entity.invulnerable is private. - * It's only used while saving NBTTags, but since the entity would be killed - * on chunk unload, we prefer to override isInvulnerable(). - */ - return true; - } - - @Override - public boolean isCollidable() { - return false; - } - - @Override - public void setCustomName(String customName) { - // Locks the custom name. - } - - @Override - public void setCustomNameVisible(boolean visible) { - // Locks the custom name. - } - - @Override - public void a(SoundEffect soundeffect, float f, float f1) { - // Remove sounds. - } - - @Override - public void setLockTick(boolean lock) { - lockTick = lock; - } - - @Override - public void die() { - // Prevent being killed. - } - - @Override - public CraftEntity getBukkitEntity() { - if (super.bukkitEntity == null) { - super.bukkitEntity = new CraftNMSSlime(super.world.getServer(), this); - } - return super.bukkitEntity; - } - - @Override - public boolean isDeadNMS() { - return super.dead; - } - - @Override - public void killEntityNMS() { - super.dead = true; - } - - @Override - public void setLocationNMS(double x, double y, double z) { - super.setPosition(x, y, z); - } - - @Override - public int getIdNMS() { - return super.getId(); - } - - @Override - public HologramLine getHologramLine() { - return parentPiece; - } - - @Override - public org.bukkit.entity.Entity getBukkitEntityNMS() { - return getBukkitEntity(); - } - - @Override - public void setPassengerOfNMS(NMSEntityBase vehicleBase) { - if (vehicleBase == null || !(vehicleBase instanceof Entity)) { - // It should never dismount - return; - } - - Entity entity = (Entity) vehicleBase; - - try { - if (super.bz() != null) { - Entity oldVehicle = super.bz(); - VEHICLE_FIELD.set(this, null); - oldVehicle.passengers.remove(this); - } - - VEHICLE_FIELD.set(this, entity); - entity.passengers.clear(); - entity.passengers.add(this); - - } catch (Exception ex) { - ConsoleLogger.logDebugException(ex); - } - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_9_R2; + +import org.bukkit.Bukkit; +import org.bukkit.craftbukkit.v1_9_R2.entity.CraftEntity; +import org.bukkit.event.player.PlayerInteractEntityEvent; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSSlime; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.Utils; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; + +import net.minecraft.server.v1_9_R2.AxisAlignedBB; +import net.minecraft.server.v1_9_R2.DamageSource; +import net.minecraft.server.v1_9_R2.Entity; +import net.minecraft.server.v1_9_R2.EntityDamageSource; +import net.minecraft.server.v1_9_R2.EntityPlayer; +import net.minecraft.server.v1_9_R2.EntitySlime; +import net.minecraft.server.v1_9_R2.NBTTagCompound; +import net.minecraft.server.v1_9_R2.PacketPlayOutMount; +import net.minecraft.server.v1_9_R2.SoundEffect; +import net.minecraft.server.v1_9_R2.World; + +public class EntityNMSSlime extends EntitySlime implements NMSSlime { + + private static final ReflectField VEHICLE_FIELD = new ReflectField(Entity.class, "at"); + + private boolean lockTick; + private HologramLine parentPiece; + + private int resendMountPacketTicks; + + public EntityNMSSlime(World world, HologramLine parentPiece) { + super(world); + super.persistent = true; + a(0.0F, 0.0F); + setSize(1); + setInvisible(true); + this.parentPiece = parentPiece; + forceSetBoundingBox(new NullBoundingBox()); + } + + @Override + public void a(AxisAlignedBB boundingBox) { + // Do not change it! + } + + public void forceSetBoundingBox(AxisAlignedBB boundingBox) { + super.a(boundingBox); + } + + @Override + public void m() { + + // So it won't get removed. + ticksLived = 0; + + if (resendMountPacketTicks++ > 20) { + resendMountPacketTicks = 0; + + if (bz() != null) { + // Send a packet near to "remind" players that the slime is riding the armor stand (Spigot bug or client bug) + PacketPlayOutMount mountPacket = new PacketPlayOutMount(bz()); + + for (Object obj : super.world.players) { + if (obj instanceof EntityPlayer) { + EntityPlayer nmsPlayer = (EntityPlayer) obj; + + double distanceSquared = Utils.square(nmsPlayer.locX - super.locX) + Utils.square(nmsPlayer.locZ - super.locZ); + if (distanceSquared < 1024 && nmsPlayer.playerConnection != null) { + nmsPlayer.playerConnection.sendPacket(mountPacket); + } + } + } + } + } + + if (!lockTick) { + super.m(); + } + } + + @Override + public void b(NBTTagCompound nbttagcompound) { + // Do not save NBT. + } + + @Override + public boolean c(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public boolean d(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return false; + } + + @Override + public NBTTagCompound e(NBTTagCompound nbttagcompound) { + // Do not save NBT. + return nbttagcompound; + } + + @Override + public void f(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public void a(NBTTagCompound nbttagcompound) { + // Do not load NBT. + } + + @Override + public boolean damageEntity(DamageSource damageSource, float amount) { + if (damageSource instanceof EntityDamageSource) { + EntityDamageSource entityDamageSource = (EntityDamageSource) damageSource; + if (entityDamageSource.getEntity() instanceof EntityPlayer) { + Bukkit.getPluginManager().callEvent(new PlayerInteractEntityEvent(((EntityPlayer) entityDamageSource.getEntity()).getBukkitEntity(), getBukkitEntity())); // Bukkit takes care of the exceptions + } + } + return false; + } + + @Override + public boolean isInvulnerable(DamageSource source) { + /* + * The field Entity.invulnerable is private. + * It's only used while saving NBTTags, but since the entity would be killed + * on chunk unload, we prefer to override isInvulnerable(). + */ + return true; + } + + @Override + public boolean isCollidable() { + return false; + } + + @Override + public void setCustomName(String customName) { + // Locks the custom name. + } + + @Override + public void setCustomNameVisible(boolean visible) { + // Locks the custom name. + } + + @Override + public void a(SoundEffect soundeffect, float f, float f1) { + // Remove sounds. + } + + @Override + public void setLockTick(boolean lock) { + lockTick = lock; + } + + @Override + public void die() { + // Prevent being killed. + } + + @Override + public CraftEntity getBukkitEntity() { + if (super.bukkitEntity == null) { + super.bukkitEntity = new CraftNMSSlime(super.world.getServer(), this); + } + return super.bukkitEntity; + } + + @Override + public boolean isDeadNMS() { + return super.dead; + } + + @Override + public void killEntityNMS() { + super.dead = true; + } + + @Override + public void setLocationNMS(double x, double y, double z) { + super.setPosition(x, y, z); + } + + @Override + public int getIdNMS() { + return super.getId(); + } + + @Override + public HologramLine getHologramLine() { + return parentPiece; + } + + @Override + public org.bukkit.entity.Entity getBukkitEntityNMS() { + return getBukkitEntity(); + } + + @Override + public void setPassengerOfNMS(NMSEntityBase vehicleBase) { + if (vehicleBase == null || !(vehicleBase instanceof Entity)) { + // It should never dismount + return; + } + + Entity entity = (Entity) vehicleBase; + + try { + if (super.bz() != null) { + Entity oldVehicle = super.bz(); + VEHICLE_FIELD.set(this, null); + oldVehicle.passengers.remove(this); + } + + VEHICLE_FIELD.set(this, entity); + entity.passengers.clear(); + entity.passengers.add(this); + + } catch (Exception ex) { + ConsoleLogger.logDebugException(ex); + } + } +} diff --git a/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/NmsManagerImpl.java b/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/NmsManagerImpl.java index 04d4977f..1c47f83a 100644 --- a/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/NmsManagerImpl.java +++ b/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/NmsManagerImpl.java @@ -1,124 +1,138 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_9_R2; - -import java.lang.reflect.Method; -import java.util.Map; - -import org.bukkit.Bukkit; -import org.bukkit.craftbukkit.v1_9_R2.CraftWorld; -import org.bukkit.craftbukkit.v1_9_R2.entity.CraftEntity; -import org.bukkit.inventory.ItemStack; - -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; -import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.Validator; -import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; - -import net.minecraft.server.v1_9_R2.Entity; -import net.minecraft.server.v1_9_R2.EntityTypes; -import net.minecraft.server.v1_9_R2.MathHelper; -import net.minecraft.server.v1_9_R2.World; -import net.minecraft.server.v1_9_R2.WorldServer; - -public class NmsManagerImpl implements NMSManager { - - private static final ReflectField, String>> ENTITY_NAMES_BY_CLASS_FIELD = new ReflectField, String>>(EntityTypes.class, "d"); - private static final ReflectField, Integer>> ENTITY_IDS_BY_CLASS_FIELD = new ReflectField, Integer>>(EntityTypes.class, "f"); - - private Method validateEntityMethod; - - @Override - public void setup() throws Exception { - registerCustomEntity(EntityNMSArmorStand.class, "ArmorStand", 30); - registerCustomEntity(EntityNMSItem.class, "Item", 1); - registerCustomEntity(EntityNMSSlime.class, "Slime", 55); - - validateEntityMethod = World.class.getDeclaredMethod("b", Entity.class); - validateEntityMethod.setAccessible(true); - } - - public void registerCustomEntity(Class entityClass, String name, int id) throws Exception { - ENTITY_NAMES_BY_CLASS_FIELD.getStatic().put(entityClass, name); - ENTITY_IDS_BY_CLASS_FIELD.getStatic().put(entityClass, Integer.valueOf(id)); - } - - @Override - public NMSItem spawnNMSItem(org.bukkit.World bukkitWorld, double x, double y, double z, ItemLine parentPiece, ItemStack stack, ItemPickupManager itemPickupManager) { - WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); - EntityNMSItem customItem = new EntityNMSItem(nmsWorld, parentPiece, itemPickupManager); - customItem.setLocationNMS(x, y, z); - customItem.setItemStackNMS(stack); - if (!addEntityToWorld(nmsWorld, customItem)) { - ConsoleLogger.handleSpawnFail(parentPiece); - } - return customItem; - } - - @Override - public EntityNMSSlime spawnNMSSlime(org.bukkit.World bukkitWorld, double x, double y, double z, HologramLine parentPiece) { - WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); - EntityNMSSlime touchSlime = new EntityNMSSlime(nmsWorld, parentPiece); - touchSlime.setLocationNMS(x, y, z); - if (!addEntityToWorld(nmsWorld, touchSlime)) { - ConsoleLogger.handleSpawnFail(parentPiece); - } - return touchSlime; - } - - @Override - public NMSArmorStand spawnNMSArmorStand(org.bukkit.World world, double x, double y, double z, HologramLine parentPiece) { - WorldServer nmsWorld = ((CraftWorld) world).getHandle(); - EntityNMSArmorStand invisibleArmorStand = new EntityNMSArmorStand(nmsWorld, parentPiece); - invisibleArmorStand.setLocationNMS(x, y, z); - if (!addEntityToWorld(nmsWorld, invisibleArmorStand)) { - ConsoleLogger.handleSpawnFail(parentPiece); - } - return invisibleArmorStand; - } - - private boolean addEntityToWorld(WorldServer nmsWorld, Entity nmsEntity) { - Validator.isTrue(Bukkit.isPrimaryThread(), "Async entity add"); - - final int chunkX = MathHelper.floor(nmsEntity.locX / 16.0); - final int chunkZ = MathHelper.floor(nmsEntity.locZ / 16.0); - - if (!nmsWorld.getChunkProviderServer().isLoaded(chunkX, chunkZ)) { - // This should never happen - nmsEntity.dead = true; - return false; - } - - nmsWorld.getChunkAt(chunkX, chunkZ).a(nmsEntity); - nmsWorld.entityList.add(nmsEntity); - - try { - validateEntityMethod.invoke(nmsWorld, nmsEntity); - } catch (Exception e) { - e.printStackTrace(); - return false; - } - return true; - } - - @Override - public boolean isNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { - return ((CraftEntity) bukkitEntity).getHandle() instanceof NMSEntityBase; - } - - @Override - public NMSEntityBase getNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { - - Entity nmsEntity = ((CraftEntity) bukkitEntity).getHandle(); - if (nmsEntity instanceof NMSEntityBase) { - return ((NMSEntityBase) nmsEntity); - } - - return null; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_9_R2; + +import java.lang.reflect.Method; +import java.util.Map; + +import org.bukkit.Bukkit; +import org.bukkit.craftbukkit.v1_9_R2.CraftWorld; +import org.bukkit.craftbukkit.v1_9_R2.entity.CraftEntity; +import org.bukkit.inventory.ItemStack; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; +import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.Validator; +import com.gmail.filoghost.holographicdisplays.util.reflection.ReflectField; + +import net.minecraft.server.v1_9_R2.Entity; +import net.minecraft.server.v1_9_R2.EntityTypes; +import net.minecraft.server.v1_9_R2.MathHelper; +import net.minecraft.server.v1_9_R2.World; +import net.minecraft.server.v1_9_R2.WorldServer; + +public class NmsManagerImpl implements NMSManager { + + private static final ReflectField, String>> ENTITY_NAMES_BY_CLASS_FIELD = new ReflectField, String>>(EntityTypes.class, "d"); + private static final ReflectField, Integer>> ENTITY_IDS_BY_CLASS_FIELD = new ReflectField, Integer>>(EntityTypes.class, "f"); + + private Method validateEntityMethod; + + @Override + public void setup() throws Exception { + registerCustomEntity(EntityNMSArmorStand.class, "ArmorStand", 30); + registerCustomEntity(EntityNMSItem.class, "Item", 1); + registerCustomEntity(EntityNMSSlime.class, "Slime", 55); + + validateEntityMethod = World.class.getDeclaredMethod("b", Entity.class); + validateEntityMethod.setAccessible(true); + } + + public void registerCustomEntity(Class entityClass, String name, int id) throws Exception { + ENTITY_NAMES_BY_CLASS_FIELD.getStatic().put(entityClass, name); + ENTITY_IDS_BY_CLASS_FIELD.getStatic().put(entityClass, Integer.valueOf(id)); + } + + @Override + public NMSItem spawnNMSItem(org.bukkit.World bukkitWorld, double x, double y, double z, ItemLine parentPiece, ItemStack stack, ItemPickupManager itemPickupManager) { + WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); + EntityNMSItem customItem = new EntityNMSItem(nmsWorld, parentPiece, itemPickupManager); + customItem.setLocationNMS(x, y, z); + customItem.setItemStackNMS(stack); + if (!addEntityToWorld(nmsWorld, customItem)) { + ConsoleLogger.handleSpawnFail(parentPiece); + } + return customItem; + } + + @Override + public EntityNMSSlime spawnNMSSlime(org.bukkit.World bukkitWorld, double x, double y, double z, HologramLine parentPiece) { + WorldServer nmsWorld = ((CraftWorld) bukkitWorld).getHandle(); + EntityNMSSlime touchSlime = new EntityNMSSlime(nmsWorld, parentPiece); + touchSlime.setLocationNMS(x, y, z); + if (!addEntityToWorld(nmsWorld, touchSlime)) { + ConsoleLogger.handleSpawnFail(parentPiece); + } + return touchSlime; + } + + @Override + public NMSArmorStand spawnNMSArmorStand(org.bukkit.World world, double x, double y, double z, HologramLine parentPiece) { + WorldServer nmsWorld = ((CraftWorld) world).getHandle(); + EntityNMSArmorStand invisibleArmorStand = new EntityNMSArmorStand(nmsWorld, parentPiece); + invisibleArmorStand.setLocationNMS(x, y, z); + if (!addEntityToWorld(nmsWorld, invisibleArmorStand)) { + ConsoleLogger.handleSpawnFail(parentPiece); + } + return invisibleArmorStand; + } + + private boolean addEntityToWorld(WorldServer nmsWorld, Entity nmsEntity) { + Validator.isTrue(Bukkit.isPrimaryThread(), "Async entity add"); + + final int chunkX = MathHelper.floor(nmsEntity.locX / 16.0); + final int chunkZ = MathHelper.floor(nmsEntity.locZ / 16.0); + + if (!nmsWorld.getChunkProviderServer().isLoaded(chunkX, chunkZ)) { + // This should never happen + nmsEntity.dead = true; + return false; + } + + nmsWorld.getChunkAt(chunkX, chunkZ).a(nmsEntity); + nmsWorld.entityList.add(nmsEntity); + + try { + validateEntityMethod.invoke(nmsWorld, nmsEntity); + } catch (Exception e) { + e.printStackTrace(); + return false; + } + return true; + } + + @Override + public boolean isNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { + return ((CraftEntity) bukkitEntity).getHandle() instanceof NMSEntityBase; + } + + @Override + public NMSEntityBase getNMSEntityBase(org.bukkit.entity.Entity bukkitEntity) { + + Entity nmsEntity = ((CraftEntity) bukkitEntity).getHandle(); + if (nmsEntity instanceof NMSEntityBase) { + return ((NMSEntityBase) nmsEntity); + } + + return null; + } + +} diff --git a/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/NullBoundingBox.java b/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/NullBoundingBox.java index cb711364..195888e2 100644 --- a/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/NullBoundingBox.java +++ b/NMS/v1_9_R2/src/main/java/com/gmail/filoghost/holographicdisplays/nms/v1_9_R2/NullBoundingBox.java @@ -1,111 +1,125 @@ -package com.gmail.filoghost.holographicdisplays.nms.v1_9_R2; - -import net.minecraft.server.v1_9_R2.AxisAlignedBB; -import net.minecraft.server.v1_9_R2.BlockPosition; -import net.minecraft.server.v1_9_R2.MovingObjectPosition; -import net.minecraft.server.v1_9_R2.Vec3D; - -public class NullBoundingBox extends AxisAlignedBB { - - public NullBoundingBox() { - super(0, 0, 0, 0, 0, 0); - } - - @Override - public double a() { - return 0.0; - } - - @Override - public double a(AxisAlignedBB arg0, double arg1) { - return 0.0; - } - - @Override - public AxisAlignedBB a(AxisAlignedBB arg0) { - return this; - } - - @Override - public AxisAlignedBB a(double arg0, double arg1, double arg2) { - return this; - } - - @Override - public MovingObjectPosition a(Vec3D arg0, Vec3D arg1) { - return super.a(arg0, arg1); - } - - @Override - public boolean a(Vec3D arg0) { - return false; - } - - @Override - public double b(AxisAlignedBB arg0, double arg1) { - return 0.0; - } - - @Override - public boolean b(AxisAlignedBB arg0) { - return false; - } - - @Override - public double c(AxisAlignedBB arg0, double arg1) { - return 0.0; - } - - @Override - public AxisAlignedBB c(double arg0, double arg1, double arg2) { - return this; - } - - @Override - public AxisAlignedBB grow(double arg0, double arg1, double arg2) { - return this; - } - - @Override - public AxisAlignedBB shrink(double arg0) { - return this; - } - - @Override - public AxisAlignedBB a(BlockPosition arg0) { - return this; - } - - @Override - public boolean a(double arg0, double arg1, double arg2, double arg3, double arg4, double arg5) { - return false; - } - - @Override - public boolean b(Vec3D arg0) { - return false; - } - - @Override - public boolean c(Vec3D arg0) { - return false; - } - - @Override - public boolean d(Vec3D arg0) { - return false; - } - - @Override - public AxisAlignedBB e(double arg0) { - return this; - } - - @Override - public AxisAlignedBB g(double arg0) { - return this; - } - - - -} \ No newline at end of file +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.nms.v1_9_R2; + +import net.minecraft.server.v1_9_R2.AxisAlignedBB; +import net.minecraft.server.v1_9_R2.BlockPosition; +import net.minecraft.server.v1_9_R2.MovingObjectPosition; +import net.minecraft.server.v1_9_R2.Vec3D; + +public class NullBoundingBox extends AxisAlignedBB { + + public NullBoundingBox() { + super(0, 0, 0, 0, 0, 0); + } + + @Override + public double a() { + return 0.0; + } + + @Override + public double a(AxisAlignedBB arg0, double arg1) { + return 0.0; + } + + @Override + public AxisAlignedBB a(AxisAlignedBB arg0) { + return this; + } + + @Override + public AxisAlignedBB a(double arg0, double arg1, double arg2) { + return this; + } + + @Override + public MovingObjectPosition a(Vec3D arg0, Vec3D arg1) { + return super.a(arg0, arg1); + } + + @Override + public boolean a(Vec3D arg0) { + return false; + } + + @Override + public double b(AxisAlignedBB arg0, double arg1) { + return 0.0; + } + + @Override + public boolean b(AxisAlignedBB arg0) { + return false; + } + + @Override + public double c(AxisAlignedBB arg0, double arg1) { + return 0.0; + } + + @Override + public AxisAlignedBB c(double arg0, double arg1, double arg2) { + return this; + } + + @Override + public AxisAlignedBB grow(double arg0, double arg1, double arg2) { + return this; + } + + @Override + public AxisAlignedBB shrink(double arg0) { + return this; + } + + @Override + public AxisAlignedBB a(BlockPosition arg0) { + return this; + } + + @Override + public boolean a(double arg0, double arg1, double arg2, double arg3, double arg4, double arg5) { + return false; + } + + @Override + public boolean b(Vec3D arg0) { + return false; + } + + @Override + public boolean c(Vec3D arg0) { + return false; + } + + @Override + public boolean d(Vec3D arg0) { + return false; + } + + @Override + public AxisAlignedBB e(double arg0) { + return this; + } + + @Override + public AxisAlignedBB g(double arg0) { + return this; + } + + + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holograms/api/FloatingItem.java b/Plugin/src/main/java/com/gmail/filoghost/holograms/api/FloatingItem.java index 1302baf0..4546948e 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holograms/api/FloatingItem.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holograms/api/FloatingItem.java @@ -1,69 +1,83 @@ -package com.gmail.filoghost.holograms.api; - -import org.bukkit.Location; -import org.bukkit.World; -import org.bukkit.inventory.ItemStack; - -/** - * @deprecated Please use the new API! - */ -@Deprecated -public interface FloatingItem { - - @Deprecated - public boolean update(); - - @Deprecated - public void hide(); - - @Deprecated - public void setItemStack(ItemStack itemstack); - - @Deprecated - public ItemStack getItemStack(); - - @Deprecated - public Location getLocation(); - - @Deprecated - public double getX(); - - @Deprecated - public double getY(); - - @Deprecated - public double getZ(); - - @Deprecated - public World getWorld(); - - @Deprecated - public void teleport(Location location); - - @Deprecated - public void setTouchHandler(ItemTouchHandler handler); - - @Deprecated - public ItemTouchHandler getTouchHandler(); - - @Deprecated - public boolean hasTouchHandler(); - - @Deprecated - public void setPickupHandler(PickupHandler handler); - - @Deprecated - public PickupHandler getPickupHandler(); - - @Deprecated - public boolean hasPickupHandler(); - - @Deprecated - public long getCreationTimestamp(); - - @Deprecated - public void delete(); - - @Deprecated - public boolean isDeleted(); -} +/* + * 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 . + */ +package com.gmail.filoghost.holograms.api; + +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.inventory.ItemStack; + +/** + * @deprecated Please use the new API! + */ +@Deprecated +public interface FloatingItem { + + @Deprecated + public boolean update(); + + @Deprecated + public void hide(); + + @Deprecated + public void setItemStack(ItemStack itemstack); + + @Deprecated + public ItemStack getItemStack(); + + @Deprecated + public Location getLocation(); + + @Deprecated + public double getX(); + + @Deprecated + public double getY(); + + @Deprecated + public double getZ(); + + @Deprecated + public World getWorld(); + + @Deprecated + public void teleport(Location location); + + @Deprecated + public void setTouchHandler(ItemTouchHandler handler); + + @Deprecated + public ItemTouchHandler getTouchHandler(); + + @Deprecated + public boolean hasTouchHandler(); + + @Deprecated + public void setPickupHandler(PickupHandler handler); + + @Deprecated + public PickupHandler getPickupHandler(); + + @Deprecated + public boolean hasPickupHandler(); + + @Deprecated + public long getCreationTimestamp(); + + @Deprecated + public void delete(); + + @Deprecated + public boolean isDeleted(); +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holograms/api/Hologram.java b/Plugin/src/main/java/com/gmail/filoghost/holograms/api/Hologram.java index 69022116..c1fca823 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holograms/api/Hologram.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holograms/api/Hologram.java @@ -1,78 +1,92 @@ -package com.gmail.filoghost.holograms.api; - -import org.bukkit.Location; -import org.bukkit.World; - -/** - * @deprecated Please use the new API! - */ -@Deprecated -public interface Hologram { - - @Deprecated - public boolean update(); - - @Deprecated - public void hide(); - - @Deprecated - public void addLine(String text); - - @Deprecated - public void removeLine(int index); - - @Deprecated - public void setLine(int index, String text); - - @Deprecated - public void insertLine(int index, String text); - - @Deprecated - public String[] getLines(); - - @Deprecated - public int getLinesLength(); - - @Deprecated - public void clearLines(); - - @Deprecated - public Location getLocation(); - - @Deprecated - public double getX(); - - @Deprecated - public double getY(); - - @Deprecated - public double getZ(); - - @Deprecated - public World getWorld(); - - @Deprecated - public void setLocation(Location location); - - @Deprecated - public void teleport(Location location); - - @Deprecated - public void setTouchHandler(TouchHandler handler); - - @Deprecated - public TouchHandler getTouchHandler(); - - @Deprecated - public boolean hasTouchHandler(); - - @Deprecated - public long getCreationTimestamp(); - - @Deprecated - public void delete(); - - @Deprecated - public boolean isDeleted(); - -} +/* + * 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 . + */ +package com.gmail.filoghost.holograms.api; + +import org.bukkit.Location; +import org.bukkit.World; + +/** + * @deprecated Please use the new API! + */ +@Deprecated +public interface Hologram { + + @Deprecated + public boolean update(); + + @Deprecated + public void hide(); + + @Deprecated + public void addLine(String text); + + @Deprecated + public void removeLine(int index); + + @Deprecated + public void setLine(int index, String text); + + @Deprecated + public void insertLine(int index, String text); + + @Deprecated + public String[] getLines(); + + @Deprecated + public int getLinesLength(); + + @Deprecated + public void clearLines(); + + @Deprecated + public Location getLocation(); + + @Deprecated + public double getX(); + + @Deprecated + public double getY(); + + @Deprecated + public double getZ(); + + @Deprecated + public World getWorld(); + + @Deprecated + public void setLocation(Location location); + + @Deprecated + public void teleport(Location location); + + @Deprecated + public void setTouchHandler(TouchHandler handler); + + @Deprecated + public TouchHandler getTouchHandler(); + + @Deprecated + public boolean hasTouchHandler(); + + @Deprecated + public long getCreationTimestamp(); + + @Deprecated + public void delete(); + + @Deprecated + public boolean isDeleted(); + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holograms/api/HolographicDisplaysAPI.java b/Plugin/src/main/java/com/gmail/filoghost/holograms/api/HolographicDisplaysAPI.java index a2df7582..43192677 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holograms/api/HolographicDisplaysAPI.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holograms/api/HolographicDisplaysAPI.java @@ -1,131 +1,145 @@ -package com.gmail.filoghost.holograms.api; - -import java.util.ArrayList; -import java.util.List; - -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 com.gmail.filoghost.holograms.api.replacements.FakeFloatingItem; -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.util.Utils; -import com.gmail.filoghost.holographicdisplays.util.Validator; - -/** - * @deprecated Please use the new API! - */ -@Deprecated -public class HolographicDisplaysAPI { - - @Deprecated - public static Hologram createHologram(Plugin plugin, Location source, String... lines) { - CraftHologram hologram = (CraftHologram) BackendAPI.getImplementation().createHologram(plugin, source); - for (String line : lines) { - hologram.appendTextLine(line); - } - return hologram; - } - - @Deprecated - public static FloatingItem createFloatingItem(Plugin plugin, Location source, ItemStack itemstack) { - 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); - } - - @Deprecated - public static Hologram createIndividualHologram(Plugin plugin, Location source, Player whoCanSee, String... lines) { - List whoCanSeeList = new ArrayList(); - whoCanSeeList.add(whoCanSee); - return createIndividualHologram(plugin, source, whoCanSeeList, lines); - } - - @Deprecated - public static Hologram createIndividualHologram(Plugin plugin, Location source, List whoCanSee, String... lines) { - - Validator.notNull(plugin, "plugin"); - Validator.notNull(source, "source"); - Validator.notNull(source.getWorld(), "source's world"); - - CraftHologram hologram = (CraftHologram) BackendAPI.getImplementation().createHologram(plugin, source); - - hologram.getVisibilityManager().setVisibleByDefault(false); - if (whoCanSee != null) { - for (Player player : whoCanSee) { - hologram.getVisibilityManager().showTo(player); - } - } - - for (String line : lines) { - hologram.appendTextLine(line); - } - - return hologram; - } - - @Deprecated - public static FloatingItem createIndividualFloatingItem(Plugin plugin, Location source, Player whoCanSee, ItemStack itemstack) { - List whoCanSeeList = new ArrayList(); - whoCanSeeList.add(whoCanSee); - return createIndividualFloatingItem(plugin, source, whoCanSeeList, itemstack); - } - - @Deprecated - public static FloatingItem createIndividualFloatingItem(Plugin plugin, Location source, List whoCanSee, ItemStack itemstack) { - - 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); - - hologram.getVisibilityManager().setVisibleByDefault(false); - if (whoCanSee != null) { - for (Player player : whoCanSee) { - hologram.getVisibilityManager().showTo(player); - } - } - - return new FakeFloatingItem(hologram, itemstack); - } - - @Deprecated - public static Hologram[] getHolograms(Plugin plugin) { - Validator.notNull(plugin, "plugin cannot be null"); - - List pluginHolograms = Utils.newList(); - for (PluginHologram pluginHologram : PluginHologramManager.getHolograms()) { - if (pluginHologram.getOwner().equals(plugin)) { - pluginHolograms.add(pluginHologram); - } - } - - return pluginHolograms.toArray(new Hologram[0]); - } - - @Deprecated - public static FloatingItem[] getFloatingItems(Plugin plugin) { - Validator.notNull(plugin, "plugin cannot be null"); - return new FloatingItem[0]; - } - - @Deprecated - public static boolean isHologramEntity(Entity bukkitEntity) { - return HologramsAPI.isHologramEntity(bukkitEntity); - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holograms.api; + +import java.util.ArrayList; +import java.util.List; + +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 com.gmail.filoghost.holograms.api.replacements.FakeFloatingItem; +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.util.Utils; +import com.gmail.filoghost.holographicdisplays.util.Validator; + +/** + * @deprecated Please use the new API! + */ +@Deprecated +public class HolographicDisplaysAPI { + + @Deprecated + public static Hologram createHologram(Plugin plugin, Location source, String... lines) { + CraftHologram hologram = (CraftHologram) BackendAPI.getImplementation().createHologram(plugin, source); + for (String line : lines) { + hologram.appendTextLine(line); + } + return hologram; + } + + @Deprecated + public static FloatingItem createFloatingItem(Plugin plugin, Location source, ItemStack itemstack) { + 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); + } + + @Deprecated + public static Hologram createIndividualHologram(Plugin plugin, Location source, Player whoCanSee, String... lines) { + List whoCanSeeList = new ArrayList(); + whoCanSeeList.add(whoCanSee); + return createIndividualHologram(plugin, source, whoCanSeeList, lines); + } + + @Deprecated + public static Hologram createIndividualHologram(Plugin plugin, Location source, List whoCanSee, String... lines) { + + Validator.notNull(plugin, "plugin"); + Validator.notNull(source, "source"); + Validator.notNull(source.getWorld(), "source's world"); + + CraftHologram hologram = (CraftHologram) BackendAPI.getImplementation().createHologram(plugin, source); + + hologram.getVisibilityManager().setVisibleByDefault(false); + if (whoCanSee != null) { + for (Player player : whoCanSee) { + hologram.getVisibilityManager().showTo(player); + } + } + + for (String line : lines) { + hologram.appendTextLine(line); + } + + return hologram; + } + + @Deprecated + public static FloatingItem createIndividualFloatingItem(Plugin plugin, Location source, Player whoCanSee, ItemStack itemstack) { + List whoCanSeeList = new ArrayList(); + whoCanSeeList.add(whoCanSee); + return createIndividualFloatingItem(plugin, source, whoCanSeeList, itemstack); + } + + @Deprecated + public static FloatingItem createIndividualFloatingItem(Plugin plugin, Location source, List whoCanSee, ItemStack itemstack) { + + 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); + + hologram.getVisibilityManager().setVisibleByDefault(false); + if (whoCanSee != null) { + for (Player player : whoCanSee) { + hologram.getVisibilityManager().showTo(player); + } + } + + return new FakeFloatingItem(hologram, itemstack); + } + + @Deprecated + public static Hologram[] getHolograms(Plugin plugin) { + Validator.notNull(plugin, "plugin cannot be null"); + + List pluginHolograms = Utils.newList(); + for (PluginHologram pluginHologram : PluginHologramManager.getHolograms()) { + if (pluginHologram.getOwner().equals(plugin)) { + pluginHolograms.add(pluginHologram); + } + } + + return pluginHolograms.toArray(new Hologram[0]); + } + + @Deprecated + public static FloatingItem[] getFloatingItems(Plugin plugin) { + Validator.notNull(plugin, "plugin cannot be null"); + return new FloatingItem[0]; + } + + @Deprecated + public static boolean isHologramEntity(Entity bukkitEntity) { + return HologramsAPI.isHologramEntity(bukkitEntity); + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holograms/api/ItemTouchHandler.java b/Plugin/src/main/java/com/gmail/filoghost/holograms/api/ItemTouchHandler.java index d5b2d426..8597e5e3 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holograms/api/ItemTouchHandler.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holograms/api/ItemTouchHandler.java @@ -1,14 +1,28 @@ -package com.gmail.filoghost.holograms.api; - -import org.bukkit.entity.Player; - -/** - * @deprecated Please use the new API! - */ -@Deprecated -public interface ItemTouchHandler { - - @Deprecated - public void onTouch(FloatingItem floatingItem, Player player); - -} +/* + * 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 . + */ +package com.gmail.filoghost.holograms.api; + +import org.bukkit.entity.Player; + +/** + * @deprecated Please use the new API! + */ +@Deprecated +public interface ItemTouchHandler { + + @Deprecated + public void onTouch(FloatingItem floatingItem, Player player); + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holograms/api/PickupHandler.java b/Plugin/src/main/java/com/gmail/filoghost/holograms/api/PickupHandler.java index 69db2129..e3029b71 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holograms/api/PickupHandler.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holograms/api/PickupHandler.java @@ -1,14 +1,28 @@ -package com.gmail.filoghost.holograms.api; - -import org.bukkit.entity.Player; - -/** - * @deprecated Please use the new API! - */ -@Deprecated -public interface PickupHandler { - - @Deprecated - public void onPickup(FloatingItem floatingItem, Player player); - -} +/* + * 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 . + */ +package com.gmail.filoghost.holograms.api; + +import org.bukkit.entity.Player; + +/** + * @deprecated Please use the new API! + */ +@Deprecated +public interface PickupHandler { + + @Deprecated + public void onPickup(FloatingItem floatingItem, Player player); + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holograms/api/TouchHandler.java b/Plugin/src/main/java/com/gmail/filoghost/holograms/api/TouchHandler.java index 908b1e2f..c39c074e 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holograms/api/TouchHandler.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holograms/api/TouchHandler.java @@ -1,14 +1,28 @@ -package com.gmail.filoghost.holograms.api; - -import org.bukkit.entity.Player; - -/** - * @deprecated Please use the new API! - */ -@Deprecated -public interface TouchHandler { - - @Deprecated - public void onTouch(Hologram hologram, Player player); - -} +/* + * 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 . + */ +package com.gmail.filoghost.holograms.api; + +import org.bukkit.entity.Player; + +/** + * @deprecated Please use the new API! + */ +@Deprecated +public interface TouchHandler { + + @Deprecated + public void onTouch(Hologram hologram, Player player); + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holograms/api/replacements/FakeFloatingItem.java b/Plugin/src/main/java/com/gmail/filoghost/holograms/api/replacements/FakeFloatingItem.java index 45a0723b..92d3ef73 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holograms/api/replacements/FakeFloatingItem.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holograms/api/replacements/FakeFloatingItem.java @@ -1,130 +1,144 @@ -package com.gmail.filoghost.holograms.api.replacements; - -import org.bukkit.Location; -import org.bukkit.World; -import org.bukkit.inventory.ItemStack; - -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; - -/** - * Do not use this class! - */ -@SuppressWarnings("deprecation") -public class FakeFloatingItem implements FloatingItem { - - public CraftHologram hologram; - private CraftItemLine mainLine; - - public FakeFloatingItem(CraftHologram hologram, ItemStack item) { - this.hologram = hologram; - mainLine = hologram.appendItemLine(item); - } - - @Override - public boolean update() { - return true; - } - - @Override - public void hide() { - - } - - @Override - public void setItemStack(ItemStack itemstack) { - mainLine.setItemStack(itemstack); - } - - @Override - public ItemStack getItemStack() { - return mainLine.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) { - if (handler != null) { - mainLine.setTouchHandler(new OldItemTouchHandlerWrapper(this, handler)); - } else { - mainLine.setTouchHandler(null); - } - } - - @Override - public ItemTouchHandler getTouchHandler() { - return ((OldItemTouchHandlerWrapper) mainLine.getTouchHandler()).oldHandler; - } - - @Override - public boolean hasTouchHandler() { - return mainLine.getTouchHandler() != null; - } - - @Override - public void setPickupHandler(PickupHandler handler) { - if (handler != null) { - mainLine.setPickupHandler(new OldPickupHandlerWrapper(this, handler)); - } else { - mainLine.setPickupHandler(null); - } - } - - @Override - public PickupHandler getPickupHandler() { - return ((OldPickupHandlerWrapper) mainLine.getPickupHandler()).oldHandler; - } - - @Override - public boolean hasPickupHandler() { - return mainLine.getPickupHandler() != null; - } - - @Override - public long getCreationTimestamp() { - return hologram.getCreationTimestamp(); - } - - @Override - public void delete() { - hologram.delete(); - } - - @Override - public boolean isDeleted() { - return hologram.isDeleted(); - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holograms.api.replacements; + +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.inventory.ItemStack; + +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; + +/** + * Do not use this class! + */ +@SuppressWarnings("deprecation") +public class FakeFloatingItem implements FloatingItem { + + public CraftHologram hologram; + private CraftItemLine mainLine; + + public FakeFloatingItem(CraftHologram hologram, ItemStack item) { + this.hologram = hologram; + mainLine = hologram.appendItemLine(item); + } + + @Override + public boolean update() { + return true; + } + + @Override + public void hide() { + + } + + @Override + public void setItemStack(ItemStack itemstack) { + mainLine.setItemStack(itemstack); + } + + @Override + public ItemStack getItemStack() { + return mainLine.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) { + if (handler != null) { + mainLine.setTouchHandler(new OldItemTouchHandlerWrapper(this, handler)); + } else { + mainLine.setTouchHandler(null); + } + } + + @Override + public ItemTouchHandler getTouchHandler() { + return ((OldItemTouchHandlerWrapper) mainLine.getTouchHandler()).oldHandler; + } + + @Override + public boolean hasTouchHandler() { + return mainLine.getTouchHandler() != null; + } + + @Override + public void setPickupHandler(PickupHandler handler) { + if (handler != null) { + mainLine.setPickupHandler(new OldPickupHandlerWrapper(this, handler)); + } else { + mainLine.setPickupHandler(null); + } + } + + @Override + public PickupHandler getPickupHandler() { + return ((OldPickupHandlerWrapper) mainLine.getPickupHandler()).oldHandler; + } + + @Override + public boolean hasPickupHandler() { + return mainLine.getPickupHandler() != null; + } + + @Override + public long getCreationTimestamp() { + return hologram.getCreationTimestamp(); + } + + @Override + public void delete() { + hologram.delete(); + } + + @Override + public boolean isDeleted() { + return hologram.isDeleted(); + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holograms/api/replacements/OldItemTouchHandlerWrapper.java b/Plugin/src/main/java/com/gmail/filoghost/holograms/api/replacements/OldItemTouchHandlerWrapper.java index a464bc94..aa8e2c3f 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holograms/api/replacements/OldItemTouchHandlerWrapper.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holograms/api/replacements/OldItemTouchHandlerWrapper.java @@ -1,24 +1,38 @@ -package com.gmail.filoghost.holograms.api.replacements; - -import org.bukkit.entity.Player; - -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 ItemTouchHandler oldHandler; - private FloatingItem item; - - public OldItemTouchHandlerWrapper(FloatingItem item, ItemTouchHandler oldHandler) { - this.item = item; - this.oldHandler = oldHandler; - } - - @Override - public void onTouch(Player player) { - oldHandler.onTouch(item, player); - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holograms.api.replacements; + +import org.bukkit.entity.Player; + +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 ItemTouchHandler oldHandler; + private FloatingItem item; + + public OldItemTouchHandlerWrapper(FloatingItem item, ItemTouchHandler oldHandler) { + this.item = item; + this.oldHandler = oldHandler; + } + + @Override + public void onTouch(Player player) { + oldHandler.onTouch(item, player); + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holograms/api/replacements/OldPickupHandlerWrapper.java b/Plugin/src/main/java/com/gmail/filoghost/holograms/api/replacements/OldPickupHandlerWrapper.java index 8a08e1dd..c14f7112 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holograms/api/replacements/OldPickupHandlerWrapper.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holograms/api/replacements/OldPickupHandlerWrapper.java @@ -1,24 +1,38 @@ -package com.gmail.filoghost.holograms.api.replacements; - -import org.bukkit.entity.Player; - -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 PickupHandler oldHandler; - private FloatingItem item; - - public OldPickupHandlerWrapper(FloatingItem item, PickupHandler oldPickupHandler) { - this.item = item; - this.oldHandler = oldPickupHandler; - } - - @Override - public void onPickup(Player player) { - oldHandler.onPickup(item, player); - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holograms.api.replacements; + +import org.bukkit.entity.Player; + +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 PickupHandler oldHandler; + private FloatingItem item; + + public OldPickupHandlerWrapper(FloatingItem item, PickupHandler oldPickupHandler) { + this.item = item; + this.oldHandler = oldPickupHandler; + } + + @Override + public void onPickup(Player player) { + oldHandler.onPickup(item, player); + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holograms/api/replacements/OldTouchHandlerWrapper.java b/Plugin/src/main/java/com/gmail/filoghost/holograms/api/replacements/OldTouchHandlerWrapper.java index c42b892b..3ec50de2 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holograms/api/replacements/OldTouchHandlerWrapper.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holograms/api/replacements/OldTouchHandlerWrapper.java @@ -1,24 +1,38 @@ -package com.gmail.filoghost.holograms.api.replacements; - -import org.bukkit.entity.Player; - -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 TouchHandler oldHandler; - private CraftHologram hologram; - - public OldTouchHandlerWrapper(CraftHologram hologram, TouchHandler oldHandler) { - this.hologram = hologram; - this.oldHandler = oldHandler; - } - - @Override - public void onTouch(Player player) { - oldHandler.onTouch(hologram, player); - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holograms.api.replacements; + +import org.bukkit.entity.Player; + +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 TouchHandler oldHandler; + private CraftHologram hologram; + + public OldTouchHandlerWrapper(CraftHologram hologram, TouchHandler oldHandler) { + this.hologram = hologram; + this.oldHandler = oldHandler; + } + + @Override + public void onTouch(Player player) { + oldHandler.onTouch(hologram, player); + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/HolographicDisplays.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/HolographicDisplays.java index 94a3be79..6ffc36ea 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/HolographicDisplays.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/HolographicDisplays.java @@ -1,311 +1,325 @@ -package com.gmail.filoghost.holographicdisplays; - -import java.io.File; -import java.util.Set; -import java.util.logging.Level; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import org.bstats.bukkit.MetricsLite; -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.plugin.java.JavaPlugin; - -import com.gmail.filoghost.holographicdisplays.SimpleUpdater.ResponseHandler; -import com.gmail.filoghost.holographicdisplays.api.internal.BackendAPI; -import com.gmail.filoghost.holographicdisplays.bridge.bungeecord.BungeeServerTracker; -import com.gmail.filoghost.holographicdisplays.bridge.protocollib.ProtocolLibHook; -import com.gmail.filoghost.holographicdisplays.commands.main.HologramsCommandHandler; -import com.gmail.filoghost.holographicdisplays.disk.Configuration; -import com.gmail.filoghost.holographicdisplays.disk.HologramDatabase; -import com.gmail.filoghost.holographicdisplays.disk.UnicodeSymbols; -import com.gmail.filoghost.holographicdisplays.exception.HologramNotFoundException; -import com.gmail.filoghost.holographicdisplays.exception.InvalidFormatException; -import com.gmail.filoghost.holographicdisplays.exception.WorldNotFoundException; -import com.gmail.filoghost.holographicdisplays.listener.MainListener; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; -import com.gmail.filoghost.holographicdisplays.object.DefaultBackendAPI; -import com.gmail.filoghost.holographicdisplays.object.NamedHologram; -import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; -import com.gmail.filoghost.holographicdisplays.object.PluginHologram; -import com.gmail.filoghost.holographicdisplays.object.PluginHologramManager; -import com.gmail.filoghost.holographicdisplays.placeholder.AnimationsRegister; -import com.gmail.filoghost.holographicdisplays.placeholder.PlaceholdersManager; -import com.gmail.filoghost.holographicdisplays.task.BungeeCleanupTask; -import com.gmail.filoghost.holographicdisplays.task.StartupLoadHologramsTask; -import com.gmail.filoghost.holographicdisplays.task.WorldPlayerCounterTask; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.NMSVersion; -import com.gmail.filoghost.holographicdisplays.util.Utils; -import com.gmail.filoghost.holographicdisplays.util.VersionUtils; - -public class HolographicDisplays extends JavaPlugin { - - // The main instance of the plugin. - private static HolographicDisplays instance; - - // The manager for net.minecraft.server access. - private static NMSManager nmsManager; - - // The listener for all the Bukkit and NMS events. - private static MainListener mainListener; - - // The command handler, just in case a plugin wants to register more commands. - private HologramsCommandHandler commandHandler; - - // The new version found by the updater, null if there is no new version. - private static String newVersion; - - // Not null if ProtocolLib is installed and successfully loaded. - private static ProtocolLibHook protocolLibHook; - - @Override - public void onEnable() { - - // Warn about plugin reloaders and the /reload command. - if (instance != null || System.getProperty("HolographicDisplaysLoaded") != null) { - Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[HolographicDisplays] Please do not use /reload or plugin reloaders. Use the command \"/holograms reload\" instead. You will receive no support for doing this operation."); - } - - System.setProperty("HolographicDisplaysLoaded", "true"); - instance = this; - ConsoleLogger.setLogger(instance.getLogger()); - - // Load placeholders.yml. - UnicodeSymbols.load(this); - - // Load the configuration. - Configuration.load(this); - - if (Configuration.updateNotification) { - new SimpleUpdater(this, 75097).checkForUpdates(new ResponseHandler() { - - @Override - public void onUpdateFound(final String newVersion) { - - HolographicDisplays.newVersion = newVersion; - ConsoleLogger.log(Level.INFO, "Found a new version available: " + newVersion); - ConsoleLogger.log(Level.INFO, "Download it on Bukkit Dev:"); - ConsoleLogger.log(Level.INFO, "dev.bukkit.org/bukkit-plugins/holographic-displays"); - } - }); - } - - if (!NMSVersion.isValid()) { - printWarnAndDisable( - "******************************************************", - " This version of HolographicDisplays only", - " works on server versions from 1.8 to 1.13.1.", - " The plugin will be disabled.", - "******************************************************" - ); - return; - } - - try { - nmsManager = (NMSManager) Class.forName("com.gmail.filoghost.holographicdisplays.nms." + NMSVersion.getCurrent() + ".NmsManagerImpl").getConstructor().newInstance(); - } catch (Throwable t) { - t.printStackTrace(); - printWarnAndDisable( - "******************************************************", - " HolographicDisplays was unable to instantiate", - " the NMS manager. The plugin will be disabled.", - "******************************************************" - ); - return; - } - - try { - nmsManager.setup(); - } catch (Exception e) { - e.printStackTrace(); - printWarnAndDisable( - "******************************************************", - " HolographicDisplays was unable to register", - " custom entities, the plugin will be disabled.", - " Are you using the correct Bukkit/Spigot version?", - "******************************************************" - ); - return; - } - - // ProtocolLib check. - try { - if (Bukkit.getPluginManager().isPluginEnabled("ProtocolLib")) { - - String requiredVersionError = null; - - try { - String protocolVersion = Bukkit.getPluginManager().getPlugin("ProtocolLib").getDescription().getVersion(); - Matcher versionNumbersMatcher = Pattern.compile("([0-9\\.])+").matcher(protocolVersion); - - if (versionNumbersMatcher.find()) { - String versionNumbers = versionNumbersMatcher.group(); - - if (NMSVersion.isBetween(NMSVersion.v1_8_R1, NMSVersion.v1_8_R3)) { - if (!VersionUtils.isVersionBetweenEqual(versionNumbers, "3.6.4", "3.6.5") && !VersionUtils.isVersionGreaterEqual(versionNumbers, "4.1")) { - requiredVersionError = "between 3.6.4 and 3.6.5 or higher than 4.1"; - } - } else { - if (!VersionUtils.isVersionGreaterEqual(versionNumbers, "4.0")) { - requiredVersionError = "higher than 4.0"; - } - } - - } else { - throw new RuntimeException("could not find version numbers pattern"); - } - - } catch (Exception e) { - ConsoleLogger.log(Level.WARNING, "Could not check ProtocolLib version (" + e.getClass().getName() + ": " + e.getMessage() + "), enabling support anyway and hoping for the best. If you get errors, please contact the author."); - } - - if (requiredVersionError == null) { - ProtocolLibHook protocolLibHook; - - if (Utils.classExists("com.comphenix.protocol.wrappers.WrappedDataWatcher$WrappedDataWatcherObject")) { - // Only the new version contains this class - ConsoleLogger.log(Level.INFO, "Found ProtocolLib, using new version."); - protocolLibHook = new com.gmail.filoghost.holographicdisplays.bridge.protocollib.current.ProtocolLibHookImpl(); - } else { - ConsoleLogger.log(Level.INFO, "Found ProtocolLib, using old version."); - protocolLibHook = new com.gmail.filoghost.holographicdisplays.bridge.protocollib.old.ProtocolLibHookImpl(); - } - - if (protocolLibHook.hook(this, nmsManager)) { - HolographicDisplays.protocolLibHook = protocolLibHook; - ConsoleLogger.log(Level.INFO, "Enabled player relative placeholders with ProtocolLib."); - } - - } else { - Bukkit.getConsoleSender().sendMessage( - ChatColor.RED + "[Holographic Displays] Detected incompatible version of ProtocolLib, support disabled. " + - "For this server version you must be using a ProtocolLib version " + requiredVersionError + "."); - } - } - - } catch (Exception ex) { - ConsoleLogger.log(Level.WARNING, "Failed to load ProtocolLib support. Is it updated?", ex); - } - - // Load animation files and the placeholder manager. - PlaceholdersManager.load(this); - try { - AnimationsRegister.loadAnimations(this); - } catch (Exception ex) { - ConsoleLogger.log(Level.WARNING, "Failed to load animation files!", ex); - } - - // Initalize other static classes. - HologramDatabase.loadYamlFile(this); - BungeeServerTracker.startTask(Configuration.bungeeRefreshSeconds); - - // Start repeating tasks. - Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new BungeeCleanupTask(), 5 * 60 * 20, 5 * 60 * 20); - Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new WorldPlayerCounterTask(), 0L, 3 * 20); - - Set savedHologramsNames = HologramDatabase.getHolograms(); - if (savedHologramsNames != null && savedHologramsNames.size() > 0) { - for (String singleHologramName : savedHologramsNames) { - try { - NamedHologram singleHologram = HologramDatabase.loadHologram(singleHologramName); - NamedHologramManager.addHologram(singleHologram); - } catch (HologramNotFoundException e) { - ConsoleLogger.log(Level.WARNING, "Hologram '" + singleHologramName + "' not found, skipping it."); - } catch (InvalidFormatException e) { - ConsoleLogger.log(Level.WARNING, "Hologram '" + singleHologramName + "' has an invalid location format."); - } catch (WorldNotFoundException e) { - ConsoleLogger.log(Level.WARNING, "Hologram '" + singleHologramName + "' was in the world '" + e.getMessage() + "' but it wasn't loaded."); - } catch (Exception e) { - ConsoleLogger.log(Level.WARNING, "Unhandled exception while loading the hologram '" + singleHologramName + "'. Please contact the developer.", e); - } - } - } - - if (getCommand("holograms") == null) { - printWarnAndDisable( - "******************************************************", - " HolographicDisplays was unable to register", - " the command \"holograms\". Do not modify", - " plugin.yml removing commands, if this is", - " the case.", - "******************************************************" - ); - return; - } - - getCommand("holograms").setExecutor(commandHandler = new HologramsCommandHandler()); - Bukkit.getPluginManager().registerEvents(mainListener = new MainListener(nmsManager), this); - - // Register bStats metrics - new MetricsLite(this); - - // The entities are loaded when the server is ready. - Bukkit.getScheduler().scheduleSyncDelayedTask(this, new StartupLoadHologramsTask(), 10L); - - // Enable the API. - BackendAPI.setImplementation(new DefaultBackendAPI()); - } - - - @Override - public void onDisable() { - for (NamedHologram hologram : NamedHologramManager.getHolograms()) { - hologram.despawnEntities(); - } - for (PluginHologram hologram : PluginHologramManager.getHolograms()) { - hologram.despawnEntities(); - } - } - - public static NMSManager getNMSManager() { - return nmsManager; - } - - public static MainListener getMainListener() { - return mainListener; - } - - public HologramsCommandHandler getCommandHandler() { - return commandHandler; - } - - private static void printWarnAndDisable(String... messages) { - StringBuffer buffer = new StringBuffer("\n "); - for (String message : messages) { - buffer.append('\n'); - buffer.append(message); - } - buffer.append('\n'); - System.out.println(buffer.toString()); - try { - Thread.sleep(5000); - } catch (InterruptedException ex) { } - instance.setEnabled(false); - } - - public static HolographicDisplays getInstance() { - return instance; - } - - - public static String getNewVersion() { - return newVersion; - } - - - public static boolean hasProtocolLibHook() { - return protocolLibHook != null; - } - - - public static ProtocolLibHook getProtocolLibHook() { - return protocolLibHook; - } - - - public static boolean isConfigFile(File file) { - return file.getName().toLowerCase().endsWith(".yml") && instance.getResource(file.getName()) != null; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays; + +import java.io.File; +import java.util.Set; +import java.util.logging.Level; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.bstats.bukkit.MetricsLite; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.plugin.java.JavaPlugin; + +import com.gmail.filoghost.holographicdisplays.SimpleUpdater.ResponseHandler; +import com.gmail.filoghost.holographicdisplays.api.internal.BackendAPI; +import com.gmail.filoghost.holographicdisplays.bridge.bungeecord.BungeeServerTracker; +import com.gmail.filoghost.holographicdisplays.bridge.protocollib.ProtocolLibHook; +import com.gmail.filoghost.holographicdisplays.commands.main.HologramsCommandHandler; +import com.gmail.filoghost.holographicdisplays.disk.Configuration; +import com.gmail.filoghost.holographicdisplays.disk.HologramDatabase; +import com.gmail.filoghost.holographicdisplays.disk.UnicodeSymbols; +import com.gmail.filoghost.holographicdisplays.exception.HologramNotFoundException; +import com.gmail.filoghost.holographicdisplays.exception.InvalidFormatException; +import com.gmail.filoghost.holographicdisplays.exception.WorldNotFoundException; +import com.gmail.filoghost.holographicdisplays.listener.MainListener; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; +import com.gmail.filoghost.holographicdisplays.object.DefaultBackendAPI; +import com.gmail.filoghost.holographicdisplays.object.NamedHologram; +import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; +import com.gmail.filoghost.holographicdisplays.object.PluginHologram; +import com.gmail.filoghost.holographicdisplays.object.PluginHologramManager; +import com.gmail.filoghost.holographicdisplays.placeholder.AnimationsRegister; +import com.gmail.filoghost.holographicdisplays.placeholder.PlaceholdersManager; +import com.gmail.filoghost.holographicdisplays.task.BungeeCleanupTask; +import com.gmail.filoghost.holographicdisplays.task.StartupLoadHologramsTask; +import com.gmail.filoghost.holographicdisplays.task.WorldPlayerCounterTask; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.NMSVersion; +import com.gmail.filoghost.holographicdisplays.util.Utils; +import com.gmail.filoghost.holographicdisplays.util.VersionUtils; + +public class HolographicDisplays extends JavaPlugin { + + // The main instance of the plugin. + private static HolographicDisplays instance; + + // The manager for net.minecraft.server access. + private static NMSManager nmsManager; + + // The listener for all the Bukkit and NMS events. + private static MainListener mainListener; + + // The command handler, just in case a plugin wants to register more commands. + private HologramsCommandHandler commandHandler; + + // The new version found by the updater, null if there is no new version. + private static String newVersion; + + // Not null if ProtocolLib is installed and successfully loaded. + private static ProtocolLibHook protocolLibHook; + + @Override + public void onEnable() { + + // Warn about plugin reloaders and the /reload command. + if (instance != null || System.getProperty("HolographicDisplaysLoaded") != null) { + Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[HolographicDisplays] Please do not use /reload or plugin reloaders. Use the command \"/holograms reload\" instead. You will receive no support for doing this operation."); + } + + System.setProperty("HolographicDisplaysLoaded", "true"); + instance = this; + ConsoleLogger.setLogger(instance.getLogger()); + + // Load placeholders.yml. + UnicodeSymbols.load(this); + + // Load the configuration. + Configuration.load(this); + + if (Configuration.updateNotification) { + new SimpleUpdater(this, 75097).checkForUpdates(new ResponseHandler() { + + @Override + public void onUpdateFound(final String newVersion) { + + HolographicDisplays.newVersion = newVersion; + ConsoleLogger.log(Level.INFO, "Found a new version available: " + newVersion); + ConsoleLogger.log(Level.INFO, "Download it on Bukkit Dev:"); + ConsoleLogger.log(Level.INFO, "dev.bukkit.org/bukkit-plugins/holographic-displays"); + } + }); + } + + if (!NMSVersion.isValid()) { + printWarnAndDisable( + "******************************************************", + " This version of HolographicDisplays only", + " works on server versions from 1.8 to 1.13.1.", + " The plugin will be disabled.", + "******************************************************" + ); + return; + } + + try { + nmsManager = (NMSManager) Class.forName("com.gmail.filoghost.holographicdisplays.nms." + NMSVersion.getCurrent() + ".NmsManagerImpl").getConstructor().newInstance(); + } catch (Throwable t) { + t.printStackTrace(); + printWarnAndDisable( + "******************************************************", + " HolographicDisplays was unable to instantiate", + " the NMS manager. The plugin will be disabled.", + "******************************************************" + ); + return; + } + + try { + nmsManager.setup(); + } catch (Exception e) { + e.printStackTrace(); + printWarnAndDisable( + "******************************************************", + " HolographicDisplays was unable to register", + " custom entities, the plugin will be disabled.", + " Are you using the correct Bukkit/Spigot version?", + "******************************************************" + ); + return; + } + + // ProtocolLib check. + try { + if (Bukkit.getPluginManager().isPluginEnabled("ProtocolLib")) { + + String requiredVersionError = null; + + try { + String protocolVersion = Bukkit.getPluginManager().getPlugin("ProtocolLib").getDescription().getVersion(); + Matcher versionNumbersMatcher = Pattern.compile("([0-9\\.])+").matcher(protocolVersion); + + if (versionNumbersMatcher.find()) { + String versionNumbers = versionNumbersMatcher.group(); + + if (NMSVersion.isBetween(NMSVersion.v1_8_R1, NMSVersion.v1_8_R3)) { + if (!VersionUtils.isVersionBetweenEqual(versionNumbers, "3.6.4", "3.6.5") && !VersionUtils.isVersionGreaterEqual(versionNumbers, "4.1")) { + requiredVersionError = "between 3.6.4 and 3.6.5 or higher than 4.1"; + } + } else { + if (!VersionUtils.isVersionGreaterEqual(versionNumbers, "4.0")) { + requiredVersionError = "higher than 4.0"; + } + } + + } else { + throw new RuntimeException("could not find version numbers pattern"); + } + + } catch (Exception e) { + ConsoleLogger.log(Level.WARNING, "Could not check ProtocolLib version (" + e.getClass().getName() + ": " + e.getMessage() + "), enabling support anyway and hoping for the best. If you get errors, please contact the author."); + } + + if (requiredVersionError == null) { + ProtocolLibHook protocolLibHook; + + if (Utils.classExists("com.comphenix.protocol.wrappers.WrappedDataWatcher$WrappedDataWatcherObject")) { + // Only the new version contains this class + ConsoleLogger.log(Level.INFO, "Found ProtocolLib, using new version."); + protocolLibHook = new com.gmail.filoghost.holographicdisplays.bridge.protocollib.current.ProtocolLibHookImpl(); + } else { + ConsoleLogger.log(Level.INFO, "Found ProtocolLib, using old version."); + protocolLibHook = new com.gmail.filoghost.holographicdisplays.bridge.protocollib.old.ProtocolLibHookImpl(); + } + + if (protocolLibHook.hook(this, nmsManager)) { + HolographicDisplays.protocolLibHook = protocolLibHook; + ConsoleLogger.log(Level.INFO, "Enabled player relative placeholders with ProtocolLib."); + } + + } else { + Bukkit.getConsoleSender().sendMessage( + ChatColor.RED + "[Holographic Displays] Detected incompatible version of ProtocolLib, support disabled. " + + "For this server version you must be using a ProtocolLib version " + requiredVersionError + "."); + } + } + + } catch (Exception ex) { + ConsoleLogger.log(Level.WARNING, "Failed to load ProtocolLib support. Is it updated?", ex); + } + + // Load animation files and the placeholder manager. + PlaceholdersManager.load(this); + try { + AnimationsRegister.loadAnimations(this); + } catch (Exception ex) { + ConsoleLogger.log(Level.WARNING, "Failed to load animation files!", ex); + } + + // Initalize other static classes. + HologramDatabase.loadYamlFile(this); + BungeeServerTracker.startTask(Configuration.bungeeRefreshSeconds); + + // Start repeating tasks. + Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new BungeeCleanupTask(), 5 * 60 * 20, 5 * 60 * 20); + Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new WorldPlayerCounterTask(), 0L, 3 * 20); + + Set savedHologramsNames = HologramDatabase.getHolograms(); + if (savedHologramsNames != null && savedHologramsNames.size() > 0) { + for (String singleHologramName : savedHologramsNames) { + try { + NamedHologram singleHologram = HologramDatabase.loadHologram(singleHologramName); + NamedHologramManager.addHologram(singleHologram); + } catch (HologramNotFoundException e) { + ConsoleLogger.log(Level.WARNING, "Hologram '" + singleHologramName + "' not found, skipping it."); + } catch (InvalidFormatException e) { + ConsoleLogger.log(Level.WARNING, "Hologram '" + singleHologramName + "' has an invalid location format."); + } catch (WorldNotFoundException e) { + ConsoleLogger.log(Level.WARNING, "Hologram '" + singleHologramName + "' was in the world '" + e.getMessage() + "' but it wasn't loaded."); + } catch (Exception e) { + ConsoleLogger.log(Level.WARNING, "Unhandled exception while loading the hologram '" + singleHologramName + "'. Please contact the developer.", e); + } + } + } + + if (getCommand("holograms") == null) { + printWarnAndDisable( + "******************************************************", + " HolographicDisplays was unable to register", + " the command \"holograms\". Do not modify", + " plugin.yml removing commands, if this is", + " the case.", + "******************************************************" + ); + return; + } + + getCommand("holograms").setExecutor(commandHandler = new HologramsCommandHandler()); + Bukkit.getPluginManager().registerEvents(mainListener = new MainListener(nmsManager), this); + + // Register bStats metrics + new MetricsLite(this); + + // The entities are loaded when the server is ready. + Bukkit.getScheduler().scheduleSyncDelayedTask(this, new StartupLoadHologramsTask(), 10L); + + // Enable the API. + BackendAPI.setImplementation(new DefaultBackendAPI()); + } + + + @Override + public void onDisable() { + for (NamedHologram hologram : NamedHologramManager.getHolograms()) { + hologram.despawnEntities(); + } + for (PluginHologram hologram : PluginHologramManager.getHolograms()) { + hologram.despawnEntities(); + } + } + + public static NMSManager getNMSManager() { + return nmsManager; + } + + public static MainListener getMainListener() { + return mainListener; + } + + public HologramsCommandHandler getCommandHandler() { + return commandHandler; + } + + private static void printWarnAndDisable(String... messages) { + StringBuffer buffer = new StringBuffer("\n "); + for (String message : messages) { + buffer.append('\n'); + buffer.append(message); + } + buffer.append('\n'); + System.out.println(buffer.toString()); + try { + Thread.sleep(5000); + } catch (InterruptedException ex) { } + instance.setEnabled(false); + } + + public static HolographicDisplays getInstance() { + return instance; + } + + + public static String getNewVersion() { + return newVersion; + } + + + public static boolean hasProtocolLibHook() { + return protocolLibHook != null; + } + + + public static ProtocolLibHook getProtocolLibHook() { + return protocolLibHook; + } + + + public static boolean isConfigFile(File file) { + return file.getName().toLowerCase().endsWith(".yml") && instance.getResource(file.getName()) != null; + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/SimpleUpdater.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/SimpleUpdater.java index d113f54e..be034cec 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/SimpleUpdater.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/SimpleUpdater.java @@ -1,188 +1,202 @@ -package com.gmail.filoghost.holographicdisplays; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import org.bukkit.Bukkit; -import org.bukkit.plugin.Plugin; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; -import org.json.simple.JSONValue; - -import com.google.common.primitives.Ints; - -/** - * A very simple and lightweight updater, without download features. - * @author filoghost - */ -public final class SimpleUpdater { - - private static Pattern VERSION_PATTERN = Pattern.compile("v?([0-9\\.]+)"); - - private Plugin plugin; - private int projectId; - - - public SimpleUpdater(Plugin plugin, int projectId) { - if (plugin == null) { - throw new NullPointerException("Plugin cannot be null"); - } - - this.plugin = plugin; - this.projectId = projectId; - } - - - /** - * This method creates a new async thread to check for updates. - * @param responseHandler the response handler - */ - public void checkForUpdates(final ResponseHandler responseHandler) { - Thread updaterThread = new Thread(new Runnable() { - - @Override - public void run() { - - try { - JSONArray filesArray = (JSONArray) readJson("https://api.curseforge.com/servermods/files?projectIds=" + projectId); - - if (filesArray.size() == 0) { - // The array cannot be empty, there must be at least one file. The project ID is not valid or curse returned a wrong response. - return; - } - - String updateName = (String) ((JSONObject) filesArray.get(filesArray.size() - 1)).get("name"); - final int[] remoteVersion = extractVersion(updateName); - int[] localVersion = extractVersion(plugin.getDescription().getVersion()); - - if (isNewerVersion(localVersion, remoteVersion)) { - Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { - - @Override - public void run() { - responseHandler.onUpdateFound("v" + Ints.join(".", remoteVersion)); - } - }); - } - - } catch (IOException e) { - plugin.getLogger().warning("Could not contact BukkitDev to check for updates."); - } catch (InvalidVersionException e) { - plugin.getLogger().warning("Could not check for updates because of a version format error: " + e.getMessage() + "."); - plugin.getLogger().warning("Please notify the author of this error."); - } catch (Exception e) { - e.printStackTrace(); - plugin.getLogger().warning("Unable to check for updates: unhandled exception."); - } - - } - }); - - updaterThread.start(); - } - - - private Object readJson(String url) throws MalformedURLException, IOException { - URLConnection conn = new URL(url).openConnection(); - conn.setConnectTimeout(5000); - conn.setReadTimeout(8000); - conn.addRequestProperty("User-Agent", "Updater (by filoghost)"); - conn.setDoOutput(true); - - return JSONValue.parse(new BufferedReader(new InputStreamReader(conn.getInputStream()))); - } - - - /** - * Compare the remote version found with the local version, from an array of int. - * Examples: - * v1.12 is newer than v1.2 ([1, 12] is newer than [1, 2]) - * v2.01 is equal to v2.1 ([2, 1] is equal to [2, 1]) - * - * @return true if the remote version is newer, false if equal or older - */ - private boolean isNewerVersion(int[] localVersion, int[] remoteVersion) { - int longest = Math.max(localVersion.length, remoteVersion.length); - - for (int i = 0; i < longest; i++) { - int remoteVersionPart = i < remoteVersion.length ? remoteVersion[i] : 0; - int localVersionPart = i < localVersion.length ? localVersion[i] : 0; - int diff = remoteVersionPart - localVersionPart; - - if (diff > 0) { - return true; - } else if (diff < 0) { - return false; - } - - // Continue the loop until diff = 0. - } - - // If we get here, they're the same version. - return false; - } - - - /** - * Extracts the version from a string, e.g.: - * "Holographic Displays v1.3" returns [1, 3] - */ - private int[] extractVersion(String input) throws InvalidVersionException { - if (input == null) { - throw new InvalidVersionException("input was null"); - } - - Matcher matcher = VERSION_PATTERN.matcher(input); - - if (!matcher.find()) { - throw new InvalidVersionException("version pattern not found in \"" + input + "\""); - } - - // Get the first group of the matcher (without the "v") - String version = matcher.group(1); - - // Replace multiple full stops (probably typos) with a single full stop, and split the version with them. - String[] versionParts = version.replaceAll("[\\.]{2,}", ".").split("\\."); - - // Convert the strings to integers in order to compare them - int[] versionNumbers = new int[versionParts.length]; - for (int i = 0; i < versionParts.length; i++) { - try { - versionNumbers[i] = Integer.parseInt(versionParts[i]); - } catch (NumberFormatException e) { - throw new InvalidVersionException("invalid number in \"" + input + "\""); - } - } - - return versionNumbers; - } - - - public interface ResponseHandler { - - /** - * Called when the updater finds a new version. - * @param newVersion - the new version - */ - public void onUpdateFound(final String newVersion); - - } - - - private class InvalidVersionException extends Exception { - - private static final long serialVersionUID = -3586635317155798274L; - - public InvalidVersionException(String message) { - super(message); - } - - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.bukkit.Bukkit; +import org.bukkit.plugin.Plugin; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.JSONValue; + +import com.google.common.primitives.Ints; + +/** + * A very simple and lightweight updater, without download features. + * @author filoghost + */ +public final class SimpleUpdater { + + private static Pattern VERSION_PATTERN = Pattern.compile("v?([0-9\\.]+)"); + + private Plugin plugin; + private int projectId; + + + public SimpleUpdater(Plugin plugin, int projectId) { + if (plugin == null) { + throw new NullPointerException("Plugin cannot be null"); + } + + this.plugin = plugin; + this.projectId = projectId; + } + + + /** + * This method creates a new async thread to check for updates. + * @param responseHandler the response handler + */ + public void checkForUpdates(final ResponseHandler responseHandler) { + Thread updaterThread = new Thread(new Runnable() { + + @Override + public void run() { + + try { + JSONArray filesArray = (JSONArray) readJson("https://api.curseforge.com/servermods/files?projectIds=" + projectId); + + if (filesArray.size() == 0) { + // The array cannot be empty, there must be at least one file. The project ID is not valid or curse returned a wrong response. + return; + } + + String updateName = (String) ((JSONObject) filesArray.get(filesArray.size() - 1)).get("name"); + final int[] remoteVersion = extractVersion(updateName); + int[] localVersion = extractVersion(plugin.getDescription().getVersion()); + + if (isNewerVersion(localVersion, remoteVersion)) { + Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { + + @Override + public void run() { + responseHandler.onUpdateFound("v" + Ints.join(".", remoteVersion)); + } + }); + } + + } catch (IOException e) { + plugin.getLogger().warning("Could not contact BukkitDev to check for updates."); + } catch (InvalidVersionException e) { + plugin.getLogger().warning("Could not check for updates because of a version format error: " + e.getMessage() + "."); + plugin.getLogger().warning("Please notify the author of this error."); + } catch (Exception e) { + e.printStackTrace(); + plugin.getLogger().warning("Unable to check for updates: unhandled exception."); + } + + } + }); + + updaterThread.start(); + } + + + private Object readJson(String url) throws MalformedURLException, IOException { + URLConnection conn = new URL(url).openConnection(); + conn.setConnectTimeout(5000); + conn.setReadTimeout(8000); + conn.addRequestProperty("User-Agent", "Updater (by filoghost)"); + conn.setDoOutput(true); + + return JSONValue.parse(new BufferedReader(new InputStreamReader(conn.getInputStream()))); + } + + + /** + * Compare the remote version found with the local version, from an array of int. + * Examples: + * v1.12 is newer than v1.2 ([1, 12] is newer than [1, 2]) + * v2.01 is equal to v2.1 ([2, 1] is equal to [2, 1]) + * + * @return true if the remote version is newer, false if equal or older + */ + private boolean isNewerVersion(int[] localVersion, int[] remoteVersion) { + int longest = Math.max(localVersion.length, remoteVersion.length); + + for (int i = 0; i < longest; i++) { + int remoteVersionPart = i < remoteVersion.length ? remoteVersion[i] : 0; + int localVersionPart = i < localVersion.length ? localVersion[i] : 0; + int diff = remoteVersionPart - localVersionPart; + + if (diff > 0) { + return true; + } else if (diff < 0) { + return false; + } + + // Continue the loop until diff = 0. + } + + // If we get here, they're the same version. + return false; + } + + + /** + * Extracts the version from a string, e.g.: + * "Holographic Displays v1.3" returns [1, 3] + */ + private int[] extractVersion(String input) throws InvalidVersionException { + if (input == null) { + throw new InvalidVersionException("input was null"); + } + + Matcher matcher = VERSION_PATTERN.matcher(input); + + if (!matcher.find()) { + throw new InvalidVersionException("version pattern not found in \"" + input + "\""); + } + + // Get the first group of the matcher (without the "v") + String version = matcher.group(1); + + // Replace multiple full stops (probably typos) with a single full stop, and split the version with them. + String[] versionParts = version.replaceAll("[\\.]{2,}", ".").split("\\."); + + // Convert the strings to integers in order to compare them + int[] versionNumbers = new int[versionParts.length]; + for (int i = 0; i < versionParts.length; i++) { + try { + versionNumbers[i] = Integer.parseInt(versionParts[i]); + } catch (NumberFormatException e) { + throw new InvalidVersionException("invalid number in \"" + input + "\""); + } + } + + return versionNumbers; + } + + + public interface ResponseHandler { + + /** + * Called when the updater finds a new version. + * @param newVersion - the new version + */ + public void onUpdateFound(final String newVersion); + + } + + + private class InvalidVersionException extends Exception { + + private static final long serialVersionUID = -3586635317155798274L; + + public InvalidVersionException(String message) { + super(message); + } + + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/bungeecord/BungeeChannel.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/bungeecord/BungeeChannel.java index b53e2827..070a8d38 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/bungeecord/BungeeChannel.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/bungeecord/BungeeChannel.java @@ -1,94 +1,108 @@ -package com.gmail.filoghost.holographicdisplays.bridge.bungeecord; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.EOFException; -import java.io.IOException; -import java.util.Collection; -import java.util.logging.Level; - -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; -import org.bukkit.plugin.messaging.PluginMessageListener; - -import com.gmail.filoghost.holographicdisplays.HolographicDisplays; -import com.gmail.filoghost.holographicdisplays.disk.Configuration; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.NMSVersion; - -public class BungeeChannel implements PluginMessageListener { - - private static BungeeChannel instance; - - public static BungeeChannel getInstance() { - if (instance == null) { - instance = new BungeeChannel(HolographicDisplays.getInstance()); - } - return instance; - } - - private BungeeChannel(Plugin plugin) { - Bukkit.getMessenger().registerOutgoingPluginChannel(plugin, "BungeeCord"); - Bukkit.getMessenger().registerIncomingPluginChannel(plugin, "BungeeCord", this); - - if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_13_R1)) { - // TODO implement when RedisBungee will be updated - } else { - Bukkit.getMessenger().registerOutgoingPluginChannel(plugin, "RedisBungee"); - Bukkit.getMessenger().registerIncomingPluginChannel(plugin, "RedisBungee", this); - } - } - - @Override - public void onPluginMessageReceived(String channel, Player player, byte[] message) { - String targetChannel = Configuration.useRedisBungee ? "RedisBungee" : "BungeeCord"; - - if (channel.equals(targetChannel)) { - DataInputStream in = new DataInputStream(new ByteArrayInputStream(message)); - - try { - String subChannel = in.readUTF(); - - if (subChannel.equals("PlayerCount")) { - String server = in.readUTF(); - - if (in.available() > 0) { - int online = in.readInt(); - - BungeeServerInfo serverInfo = BungeeServerTracker.getOrCreateServerInfo(server); - serverInfo.setOnlinePlayers(online); - } - } - - } catch (EOFException e) { - // Do nothing. - } catch (IOException e) { - // This should never happen. - e.printStackTrace(); - } - } - } - - - public void askPlayerCount(String server) { - ByteArrayOutputStream b = new ByteArrayOutputStream(); - DataOutputStream out = new DataOutputStream(b); - - try { - out.writeUTF("PlayerCount"); - out.writeUTF(server); - } catch (IOException e) { - // It should not happen. - ConsoleLogger.log(Level.WARNING, "I/O Exception while asking for player count on server '" + server + "'.", e); - } - - // OR, if you don't need to send it to a specific player - Collection players = Bukkit.getOnlinePlayers(); - if (players.size() > 0) { - players.iterator().next().sendPluginMessage(HolographicDisplays.getInstance(), Configuration.useRedisBungee ? "RedisBungee" : "BungeeCord", b.toByteArray()); - } - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.bridge.bungeecord; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.EOFException; +import java.io.IOException; +import java.util.Collection; +import java.util.logging.Level; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.messaging.PluginMessageListener; + +import com.gmail.filoghost.holographicdisplays.HolographicDisplays; +import com.gmail.filoghost.holographicdisplays.disk.Configuration; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.NMSVersion; + +public class BungeeChannel implements PluginMessageListener { + + private static BungeeChannel instance; + + public static BungeeChannel getInstance() { + if (instance == null) { + instance = new BungeeChannel(HolographicDisplays.getInstance()); + } + return instance; + } + + private BungeeChannel(Plugin plugin) { + Bukkit.getMessenger().registerOutgoingPluginChannel(plugin, "BungeeCord"); + Bukkit.getMessenger().registerIncomingPluginChannel(plugin, "BungeeCord", this); + + if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_13_R1)) { + // TODO implement when RedisBungee will be updated + } else { + Bukkit.getMessenger().registerOutgoingPluginChannel(plugin, "RedisBungee"); + Bukkit.getMessenger().registerIncomingPluginChannel(plugin, "RedisBungee", this); + } + } + + @Override + public void onPluginMessageReceived(String channel, Player player, byte[] message) { + String targetChannel = Configuration.useRedisBungee ? "RedisBungee" : "BungeeCord"; + + if (channel.equals(targetChannel)) { + DataInputStream in = new DataInputStream(new ByteArrayInputStream(message)); + + try { + String subChannel = in.readUTF(); + + if (subChannel.equals("PlayerCount")) { + String server = in.readUTF(); + + if (in.available() > 0) { + int online = in.readInt(); + + BungeeServerInfo serverInfo = BungeeServerTracker.getOrCreateServerInfo(server); + serverInfo.setOnlinePlayers(online); + } + } + + } catch (EOFException e) { + // Do nothing. + } catch (IOException e) { + // This should never happen. + e.printStackTrace(); + } + } + } + + + public void askPlayerCount(String server) { + ByteArrayOutputStream b = new ByteArrayOutputStream(); + DataOutputStream out = new DataOutputStream(b); + + try { + out.writeUTF("PlayerCount"); + out.writeUTF(server); + } catch (IOException e) { + // It should not happen. + ConsoleLogger.log(Level.WARNING, "I/O Exception while asking for player count on server '" + server + "'.", e); + } + + // OR, if you don't need to send it to a specific player + Collection players = Bukkit.getOnlinePlayers(); + if (players.size() > 0) { + players.iterator().next().sendPluginMessage(HolographicDisplays.getInstance(), Configuration.useRedisBungee ? "RedisBungee" : "BungeeCord", b.toByteArray()); + } + } +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/bungeecord/BungeeServerInfo.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/bungeecord/BungeeServerInfo.java index 96243657..ad9925c6 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/bungeecord/BungeeServerInfo.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/bungeecord/BungeeServerInfo.java @@ -1,82 +1,96 @@ -package com.gmail.filoghost.holographicdisplays.bridge.bungeecord; - -import com.gmail.filoghost.holographicdisplays.disk.Configuration; - -public class BungeeServerInfo { - - private volatile boolean isOnline; - private volatile int onlinePlayers; - private volatile int maxPlayers; - - // The two lines of a motd - private volatile String motd1; // Should never be null - private volatile String motd2; // Should never be null - - private volatile long lastRequest; - - protected BungeeServerInfo() { - isOnline = false; - this.motd1 = ""; - this.motd2 = ""; - updateLastRequest(); - } - - public boolean isOnline() { - return isOnline; - } - - public void setOnline(boolean isOnline) { - this.isOnline = isOnline; - } - - public int getOnlinePlayers() { - return onlinePlayers; - } - - public void setOnlinePlayers(int onlinePlayers) { - this.onlinePlayers = onlinePlayers; - } - - public int getMaxPlayers() { - return maxPlayers; - } - - public void setMaxPlayers(int maxPlayers) { - this.maxPlayers = maxPlayers; - } - - public String getMotd1() { - return motd1; - } - - public String getMotd2() { - return motd2; - } - - public void setMotd(String motd) { - - if (motd == null) { - this.motd1 = ""; - this.motd2 = ""; - return; - } - - if (motd.contains("\n")) { - String[] split = motd.split("\n"); - this.motd1 = Configuration.pingerTrimMotd ? split[0].trim() : split[0]; - this.motd2 = Configuration.pingerTrimMotd ? split[1].trim() : split[1]; - } else { - this.motd1 = Configuration.pingerTrimMotd ? motd.trim() : motd; - this.motd2 = ""; - } - } - - public long getLastRequest() { - return lastRequest; - } - - public void updateLastRequest() { - this.lastRequest = System.currentTimeMillis(); - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.bridge.bungeecord; + +import com.gmail.filoghost.holographicdisplays.disk.Configuration; + +public class BungeeServerInfo { + + private volatile boolean isOnline; + private volatile int onlinePlayers; + private volatile int maxPlayers; + + // The two lines of a motd + private volatile String motd1; // Should never be null + private volatile String motd2; // Should never be null + + private volatile long lastRequest; + + protected BungeeServerInfo() { + isOnline = false; + this.motd1 = ""; + this.motd2 = ""; + updateLastRequest(); + } + + public boolean isOnline() { + return isOnline; + } + + public void setOnline(boolean isOnline) { + this.isOnline = isOnline; + } + + public int getOnlinePlayers() { + return onlinePlayers; + } + + public void setOnlinePlayers(int onlinePlayers) { + this.onlinePlayers = onlinePlayers; + } + + public int getMaxPlayers() { + return maxPlayers; + } + + public void setMaxPlayers(int maxPlayers) { + this.maxPlayers = maxPlayers; + } + + public String getMotd1() { + return motd1; + } + + public String getMotd2() { + return motd2; + } + + public void setMotd(String motd) { + + if (motd == null) { + this.motd1 = ""; + this.motd2 = ""; + return; + } + + if (motd.contains("\n")) { + String[] split = motd.split("\n"); + this.motd1 = Configuration.pingerTrimMotd ? split[0].trim() : split[0]; + this.motd2 = Configuration.pingerTrimMotd ? split[1].trim() : split[1]; + } else { + this.motd1 = Configuration.pingerTrimMotd ? motd.trim() : motd; + this.motd2 = ""; + } + } + + public long getLastRequest() { + return lastRequest; + } + + public void updateLastRequest() { + this.lastRequest = System.currentTimeMillis(); + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/bungeecord/BungeeServerTracker.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/bungeecord/BungeeServerTracker.java index bff9cf53..89984cff 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/bungeecord/BungeeServerTracker.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/bungeecord/BungeeServerTracker.java @@ -1,205 +1,219 @@ -package com.gmail.filoghost.holographicdisplays.bridge.bungeecord; - -import java.io.IOException; -import java.net.SocketTimeoutException; -import java.net.UnknownHostException; -import java.util.Map; -import java.util.Map.Entry; -import java.util.concurrent.ConcurrentHashMap; -import java.util.logging.Level; - -import org.bukkit.Bukkit; -import org.bukkit.scheduler.BukkitRunnable; - -import com.gmail.filoghost.holographicdisplays.HolographicDisplays; -import com.gmail.filoghost.holographicdisplays.bridge.bungeecord.serverpinger.PingResponse; -import com.gmail.filoghost.holographicdisplays.bridge.bungeecord.serverpinger.ServerPinger; -import com.gmail.filoghost.holographicdisplays.disk.Configuration; -import com.gmail.filoghost.holographicdisplays.disk.ServerAddress; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; - -public class BungeeServerTracker { - - private static Map trackedServers = new ConcurrentHashMap(); - private static int taskID = -1; - - public static void resetTrackedServers() { - trackedServers.clear(); - } - - public static void track(String server) { - if (!trackedServers.containsKey(server)) { - BungeeServerInfo info = new BungeeServerInfo(); - info.setMotd(Configuration.pingerOfflineMotd); - trackedServers.put(server, info); - - if (!Configuration.pingerEnable) { - BungeeChannel.getInstance().askPlayerCount(server); - } - } - } - - public static void untrack(String server) { - trackedServers.remove(server); - } - - protected static BungeeServerInfo getOrCreateServerInfo(String server) { - BungeeServerInfo info = trackedServers.get(server); - if (info == null) { - info = new BungeeServerInfo(); - info.setMotd(Configuration.pingerOfflineMotd); - trackedServers.put(server, info); - } - - return info; - } - - public static int getPlayersOnline(String server) { - BungeeServerInfo info = trackedServers.get(server); - if (info != null) { - info.updateLastRequest(); - return info.getOnlinePlayers(); - } else { - // It was not tracked, add it. - track(server); - return 0; - } - } - - public static String getMaxPlayers(String server) { - - if (!Configuration.pingerEnable) { - return "[Please enable pinger]"; - } - - BungeeServerInfo info = trackedServers.get(server); - if (info != null) { - info.updateLastRequest(); - return String.valueOf(info.getMaxPlayers()); - } else { - // It was not tracked, add it. - track(server); - return "0"; - } - } - - public static String getMotd1(String server) { - - if (!Configuration.pingerEnable) { - return "[Please enable pinger]"; - } - - BungeeServerInfo info = trackedServers.get(server); - if (info != null) { - info.updateLastRequest(); - return info.getMotd1(); - } else { - // It was not tracked, add it. - track(server); - return Configuration.pingerOfflineMotd; - } - } - - public static String getMotd2(String server) { - - if (!Configuration.pingerEnable) { - return "[Please enable pinger]"; - } - - BungeeServerInfo info = trackedServers.get(server); - if (info != null) { - info.updateLastRequest(); - return info.getMotd2(); - } else { - // It was not tracked, add it. - track(server); - return ""; - } - } - - public static String getOnlineStatus(String server) { - - if (!Configuration.pingerEnable) { - return "[Please enable pinger]"; - } - - BungeeServerInfo info = trackedServers.get(server); - if (info != null) { - info.updateLastRequest(); - return info.isOnline() ? Configuration.pingerStatusOnline : Configuration.pingerStatusOffline; - } else { - // It was not tracked, add it. - track(server); - return "0"; - } - } - - public static Map getTrackedServers() { - return trackedServers; - } - - public static void startTask(int refreshSeconds) { - - if (taskID != -1) { - Bukkit.getScheduler().cancelTask(taskID); - } - - taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(HolographicDisplays.getInstance(), new Runnable() { - - @Override - public void run() { - - if (Configuration.pingerEnable) { - new BukkitRunnable() { - - @Override - public void run() { - for (Entry entry : Configuration.pingerServers.entrySet()) { - - BungeeServerInfo serverInfo = getOrCreateServerInfo(entry.getKey()); - boolean displayOffline = false; - - try { - PingResponse data = ServerPinger.fetchData(entry.getValue(), Configuration.pingerTimeout); - - if (data.isOnline()) { - serverInfo.setOnline(true); - serverInfo.setOnlinePlayers(data.getOnlinePlayers()); - serverInfo.setMaxPlayers(data.getMaxPlayers()); - serverInfo.setMotd(data.getMotd()); - } else { - displayOffline = true; - } - } catch (SocketTimeoutException e) { - displayOffline = true; - } catch (UnknownHostException e) { - ConsoleLogger.log(Level.WARNING, "Couldn't fetch data from " + entry.getKey() + "(" + entry.getValue().toString() + "): unknown host address."); - displayOffline = true; - } catch (IOException e) { - displayOffline = true; - } catch (Exception e) { - displayOffline = true; - ConsoleLogger.log(Level.WARNING, "Couldn't fetch data from " + entry.getKey() + "(" + entry.getValue().toString() + "), unhandled exception: " + e.toString()); - ConsoleLogger.logDebugException(e); - } - - if (displayOffline) { - serverInfo.setOnline(false); - serverInfo.setOnlinePlayers(0); - serverInfo.setMaxPlayers(0); - serverInfo.setMotd(Configuration.pingerOfflineMotd); - } - } - } - }.runTaskAsynchronously(HolographicDisplays.getInstance()); - - } else { - for (String server : trackedServers.keySet()) { - BungeeChannel.getInstance().askPlayerCount(server); - } - } - - } - }, 1, refreshSeconds * 20); - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.bridge.bungeecord; + +import java.io.IOException; +import java.net.SocketTimeoutException; +import java.net.UnknownHostException; +import java.util.Map; +import java.util.Map.Entry; +import java.util.concurrent.ConcurrentHashMap; +import java.util.logging.Level; + +import org.bukkit.Bukkit; +import org.bukkit.scheduler.BukkitRunnable; + +import com.gmail.filoghost.holographicdisplays.HolographicDisplays; +import com.gmail.filoghost.holographicdisplays.bridge.bungeecord.serverpinger.PingResponse; +import com.gmail.filoghost.holographicdisplays.bridge.bungeecord.serverpinger.ServerPinger; +import com.gmail.filoghost.holographicdisplays.disk.Configuration; +import com.gmail.filoghost.holographicdisplays.disk.ServerAddress; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; + +public class BungeeServerTracker { + + private static Map trackedServers = new ConcurrentHashMap(); + private static int taskID = -1; + + public static void resetTrackedServers() { + trackedServers.clear(); + } + + public static void track(String server) { + if (!trackedServers.containsKey(server)) { + BungeeServerInfo info = new BungeeServerInfo(); + info.setMotd(Configuration.pingerOfflineMotd); + trackedServers.put(server, info); + + if (!Configuration.pingerEnable) { + BungeeChannel.getInstance().askPlayerCount(server); + } + } + } + + public static void untrack(String server) { + trackedServers.remove(server); + } + + protected static BungeeServerInfo getOrCreateServerInfo(String server) { + BungeeServerInfo info = trackedServers.get(server); + if (info == null) { + info = new BungeeServerInfo(); + info.setMotd(Configuration.pingerOfflineMotd); + trackedServers.put(server, info); + } + + return info; + } + + public static int getPlayersOnline(String server) { + BungeeServerInfo info = trackedServers.get(server); + if (info != null) { + info.updateLastRequest(); + return info.getOnlinePlayers(); + } else { + // It was not tracked, add it. + track(server); + return 0; + } + } + + public static String getMaxPlayers(String server) { + + if (!Configuration.pingerEnable) { + return "[Please enable pinger]"; + } + + BungeeServerInfo info = trackedServers.get(server); + if (info != null) { + info.updateLastRequest(); + return String.valueOf(info.getMaxPlayers()); + } else { + // It was not tracked, add it. + track(server); + return "0"; + } + } + + public static String getMotd1(String server) { + + if (!Configuration.pingerEnable) { + return "[Please enable pinger]"; + } + + BungeeServerInfo info = trackedServers.get(server); + if (info != null) { + info.updateLastRequest(); + return info.getMotd1(); + } else { + // It was not tracked, add it. + track(server); + return Configuration.pingerOfflineMotd; + } + } + + public static String getMotd2(String server) { + + if (!Configuration.pingerEnable) { + return "[Please enable pinger]"; + } + + BungeeServerInfo info = trackedServers.get(server); + if (info != null) { + info.updateLastRequest(); + return info.getMotd2(); + } else { + // It was not tracked, add it. + track(server); + return ""; + } + } + + public static String getOnlineStatus(String server) { + + if (!Configuration.pingerEnable) { + return "[Please enable pinger]"; + } + + BungeeServerInfo info = trackedServers.get(server); + if (info != null) { + info.updateLastRequest(); + return info.isOnline() ? Configuration.pingerStatusOnline : Configuration.pingerStatusOffline; + } else { + // It was not tracked, add it. + track(server); + return "0"; + } + } + + public static Map getTrackedServers() { + return trackedServers; + } + + public static void startTask(int refreshSeconds) { + + if (taskID != -1) { + Bukkit.getScheduler().cancelTask(taskID); + } + + taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(HolographicDisplays.getInstance(), new Runnable() { + + @Override + public void run() { + + if (Configuration.pingerEnable) { + new BukkitRunnable() { + + @Override + public void run() { + for (Entry entry : Configuration.pingerServers.entrySet()) { + + BungeeServerInfo serverInfo = getOrCreateServerInfo(entry.getKey()); + boolean displayOffline = false; + + try { + PingResponse data = ServerPinger.fetchData(entry.getValue(), Configuration.pingerTimeout); + + if (data.isOnline()) { + serverInfo.setOnline(true); + serverInfo.setOnlinePlayers(data.getOnlinePlayers()); + serverInfo.setMaxPlayers(data.getMaxPlayers()); + serverInfo.setMotd(data.getMotd()); + } else { + displayOffline = true; + } + } catch (SocketTimeoutException e) { + displayOffline = true; + } catch (UnknownHostException e) { + ConsoleLogger.log(Level.WARNING, "Couldn't fetch data from " + entry.getKey() + "(" + entry.getValue().toString() + "): unknown host address."); + displayOffline = true; + } catch (IOException e) { + displayOffline = true; + } catch (Exception e) { + displayOffline = true; + ConsoleLogger.log(Level.WARNING, "Couldn't fetch data from " + entry.getKey() + "(" + entry.getValue().toString() + "), unhandled exception: " + e.toString()); + ConsoleLogger.logDebugException(e); + } + + if (displayOffline) { + serverInfo.setOnline(false); + serverInfo.setOnlinePlayers(0); + serverInfo.setMaxPlayers(0); + serverInfo.setMotd(Configuration.pingerOfflineMotd); + } + } + } + }.runTaskAsynchronously(HolographicDisplays.getInstance()); + + } else { + for (String server : trackedServers.keySet()) { + BungeeChannel.getInstance().askPlayerCount(server); + } + } + + } + }, 1, refreshSeconds * 20); + } +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/bungeecord/serverpinger/PacketUtils.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/bungeecord/serverpinger/PacketUtils.java index f0cb59de..413ea358 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/bungeecord/serverpinger/PacketUtils.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/bungeecord/serverpinger/PacketUtils.java @@ -1,67 +1,81 @@ -package com.gmail.filoghost.holographicdisplays.bridge.bungeecord.serverpinger; - -import java.lang.RuntimeException; -import java.io.Closeable; -import java.io.DataInputStream; -import java.io.IOException; -import java.lang.Character; -import java.lang.String; -import java.io.DataOutputStream; -import java.nio.charset.Charset; - -class PacketUtils -{ - public static final Charset UTF16BE = Charset.forName("UTF-16BE");; - public static final Charset UTF8 = Charset.forName("UTF-8"); - - public static void a(final DataOutputStream out, final String s) throws IOException { - final int len = s.length(); - final byte[] data = new byte[len / 2]; - for (int i = 0; i < len; i += 2) { - data[i / 2] = (byte)((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16)); - } - out.write(data); - } - - public static void writeString(final DataOutputStream out, final String s, final Charset charset) throws IOException { - if (charset == PacketUtils.UTF8) { - writeVarInt(out, s.length()); - } - else { - out.writeShort(s.length()); - } - out.write(s.getBytes(charset)); - } - - public static int readVarInt(final DataInputStream in) throws IOException { - int i = 0; - int j = 0; - while (true) { - final int k = in.readByte(); - i |= (k & 0x7F) << j++ * 7; - if (j > 5) { - throw new RuntimeException("VarInt too big"); - } - if ((k & 0x80) != 0x80) { - return i; - } - } - } - - public static void writeVarInt(final DataOutputStream out, int paramInt) throws IOException { - while ((paramInt & 0xFFFFFF80) != 0x0) { - out.write((paramInt & 0x7F) | 0x80); - paramInt >>>= 7; - } - out.write(paramInt); - } - - public static void closeQuietly(Closeable closeable) { - try { - if (closeable != null) { - closeable.close(); - } - } catch (IOException e) { } - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.bridge.bungeecord.serverpinger; + +import java.lang.RuntimeException; +import java.io.Closeable; +import java.io.DataInputStream; +import java.io.IOException; +import java.lang.Character; +import java.lang.String; +import java.io.DataOutputStream; +import java.nio.charset.Charset; + +class PacketUtils +{ + public static final Charset UTF16BE = Charset.forName("UTF-16BE");; + public static final Charset UTF8 = Charset.forName("UTF-8"); + + public static void a(final DataOutputStream out, final String s) throws IOException { + final int len = s.length(); + final byte[] data = new byte[len / 2]; + for (int i = 0; i < len; i += 2) { + data[i / 2] = (byte)((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16)); + } + out.write(data); + } + + public static void writeString(final DataOutputStream out, final String s, final Charset charset) throws IOException { + if (charset == PacketUtils.UTF8) { + writeVarInt(out, s.length()); + } + else { + out.writeShort(s.length()); + } + out.write(s.getBytes(charset)); + } + + public static int readVarInt(final DataInputStream in) throws IOException { + int i = 0; + int j = 0; + while (true) { + final int k = in.readByte(); + i |= (k & 0x7F) << j++ * 7; + if (j > 5) { + throw new RuntimeException("VarInt too big"); + } + if ((k & 0x80) != 0x80) { + return i; + } + } + } + + public static void writeVarInt(final DataOutputStream out, int paramInt) throws IOException { + while ((paramInt & 0xFFFFFF80) != 0x0) { + out.write((paramInt & 0x7F) | 0x80); + paramInt >>>= 7; + } + out.write(paramInt); + } + + public static void closeQuietly(Closeable closeable) { + try { + if (closeable != null) { + closeable.close(); + } + } catch (IOException e) { } + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/bungeecord/serverpinger/PingResponse.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/bungeecord/serverpinger/PingResponse.java index 29976b87..53b0c2b9 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/bungeecord/serverpinger/PingResponse.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/bungeecord/serverpinger/PingResponse.java @@ -1,96 +1,110 @@ -package com.gmail.filoghost.holographicdisplays.bridge.bungeecord.serverpinger; - -import java.lang.String; -import java.util.logging.Level; - -import org.json.simple.JSONObject; -import org.json.simple.JSONValue; - -import com.gmail.filoghost.holographicdisplays.disk.ServerAddress; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; - -public class PingResponse -{ - private boolean isOnline; - private String motd; - private int onlinePlayers; - private int maxPlayers; - - public PingResponse(boolean isOnline, String motd, int onlinePlayers, int maxPlayers) { - this.isOnline = isOnline; - this.motd = motd; - this.onlinePlayers = onlinePlayers; - this.maxPlayers = maxPlayers; - } - - public PingResponse(String jsonString, ServerAddress address) { - - if (jsonString == null || jsonString.isEmpty()) { - motd = "Invalid ping response"; - ConsoleLogger.logDebug(Level.WARNING, "Received empty Json response from IP \"" + address.toString() + "\"!"); - return; - } - - Object jsonObject = JSONValue.parse(jsonString); - - if (!(jsonObject instanceof JSONObject)) { - motd = "Invalid ping response"; - ConsoleLogger.logDebug(Level.WARNING, "Received invalid Json response from IP \"" + address.toString() + "\": " + jsonString); - return; - } - - JSONObject json = (JSONObject) jsonObject; - isOnline = true; - - Object descriptionObject = json.get("description"); - - if (descriptionObject != null) { - if (descriptionObject instanceof JSONObject) { - Object text = ((JSONObject) descriptionObject).get("text"); - if (text != null) { - motd = text.toString(); - } else { - motd = "Invalid ping response (text not found)"; - } - } else { - motd = descriptionObject.toString(); - } - } else { - motd = "Invalid ping response (description not found)"; - ConsoleLogger.logDebug(Level.WARNING, "Received invalid Json response from IP \"" + address.toString() + "\": " + jsonString); - } - - Object playersObject = json.get("players"); - - if (playersObject instanceof JSONObject) { - JSONObject playersJson = (JSONObject) playersObject; - - Object onlineObject = playersJson.get("online"); - if (onlineObject instanceof Number) { - onlinePlayers = ((Number) onlineObject).intValue(); - } - - Object maxObject = playersJson.get("max"); - if (maxObject instanceof Number) { - maxPlayers = ((Number) maxObject).intValue(); - } - } - } - - public boolean isOnline() { - return isOnline; - } - - public String getMotd() { - return motd; - } - - public int getOnlinePlayers() { - return onlinePlayers; - } - - public int getMaxPlayers() { - return maxPlayers; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.bridge.bungeecord.serverpinger; + +import java.lang.String; +import java.util.logging.Level; + +import org.json.simple.JSONObject; +import org.json.simple.JSONValue; + +import com.gmail.filoghost.holographicdisplays.disk.ServerAddress; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; + +public class PingResponse +{ + private boolean isOnline; + private String motd; + private int onlinePlayers; + private int maxPlayers; + + public PingResponse(boolean isOnline, String motd, int onlinePlayers, int maxPlayers) { + this.isOnline = isOnline; + this.motd = motd; + this.onlinePlayers = onlinePlayers; + this.maxPlayers = maxPlayers; + } + + public PingResponse(String jsonString, ServerAddress address) { + + if (jsonString == null || jsonString.isEmpty()) { + motd = "Invalid ping response"; + ConsoleLogger.logDebug(Level.WARNING, "Received empty Json response from IP \"" + address.toString() + "\"!"); + return; + } + + Object jsonObject = JSONValue.parse(jsonString); + + if (!(jsonObject instanceof JSONObject)) { + motd = "Invalid ping response"; + ConsoleLogger.logDebug(Level.WARNING, "Received invalid Json response from IP \"" + address.toString() + "\": " + jsonString); + return; + } + + JSONObject json = (JSONObject) jsonObject; + isOnline = true; + + Object descriptionObject = json.get("description"); + + if (descriptionObject != null) { + if (descriptionObject instanceof JSONObject) { + Object text = ((JSONObject) descriptionObject).get("text"); + if (text != null) { + motd = text.toString(); + } else { + motd = "Invalid ping response (text not found)"; + } + } else { + motd = descriptionObject.toString(); + } + } else { + motd = "Invalid ping response (description not found)"; + ConsoleLogger.logDebug(Level.WARNING, "Received invalid Json response from IP \"" + address.toString() + "\": " + jsonString); + } + + Object playersObject = json.get("players"); + + if (playersObject instanceof JSONObject) { + JSONObject playersJson = (JSONObject) playersObject; + + Object onlineObject = playersJson.get("online"); + if (onlineObject instanceof Number) { + onlinePlayers = ((Number) onlineObject).intValue(); + } + + Object maxObject = playersJson.get("max"); + if (maxObject instanceof Number) { + maxPlayers = ((Number) maxObject).intValue(); + } + } + } + + public boolean isOnline() { + return isOnline; + } + + public String getMotd() { + return motd; + } + + public int getOnlinePlayers() { + return onlinePlayers; + } + + public int getMaxPlayers() { + return maxPlayers; + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/bungeecord/serverpinger/ServerPinger.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/bungeecord/serverpinger/ServerPinger.java index 76058137..5f730032 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/bungeecord/serverpinger/ServerPinger.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/bungeecord/serverpinger/ServerPinger.java @@ -1,53 +1,67 @@ -package com.gmail.filoghost.holographicdisplays.bridge.bungeecord.serverpinger; - -import java.io.ByteArrayOutputStream; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.net.Socket; -import java.net.SocketTimeoutException; -import java.net.UnknownHostException; - -import com.gmail.filoghost.holographicdisplays.disk.ServerAddress; - -public class ServerPinger { - - public static PingResponse fetchData(final ServerAddress serverAddress, int timeout) throws SocketTimeoutException, UnknownHostException, IOException, Exception { - - Socket socket = null; - DataOutputStream dataOut = null; - DataInputStream dataIn = null; - - try { - socket = new Socket(serverAddress.getAddress(), serverAddress.getPort()); - socket.setSoTimeout(timeout); - dataOut = new DataOutputStream(socket.getOutputStream()); - dataIn = new DataInputStream(socket.getInputStream()); - final ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); - final DataOutputStream handshake = new DataOutputStream(byteOut); - handshake.write(0); - PacketUtils.writeVarInt(handshake, 4); - PacketUtils.writeString(handshake, serverAddress.getAddress(), PacketUtils.UTF8); - handshake.writeShort(serverAddress.getPort()); - PacketUtils.writeVarInt(handshake, 1); - byte[] bytes = byteOut.toByteArray(); - PacketUtils.writeVarInt(dataOut, bytes.length); - dataOut.write(bytes); - bytes = new byte[] { 0 }; - PacketUtils.writeVarInt(dataOut, bytes.length); - dataOut.write(bytes); - PacketUtils.readVarInt(dataIn); - PacketUtils.readVarInt(dataIn); - final byte[] responseData = new byte[PacketUtils.readVarInt(dataIn)]; - dataIn.readFully(responseData); - final String jsonString = new String(responseData, PacketUtils.UTF8); - return new PingResponse(jsonString, serverAddress); - } - finally { - PacketUtils.closeQuietly(dataOut); - PacketUtils.closeQuietly(dataIn); - PacketUtils.closeQuietly(socket); - } - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.bridge.bungeecord.serverpinger; + +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.Socket; +import java.net.SocketTimeoutException; +import java.net.UnknownHostException; + +import com.gmail.filoghost.holographicdisplays.disk.ServerAddress; + +public class ServerPinger { + + public static PingResponse fetchData(final ServerAddress serverAddress, int timeout) throws SocketTimeoutException, UnknownHostException, IOException, Exception { + + Socket socket = null; + DataOutputStream dataOut = null; + DataInputStream dataIn = null; + + try { + socket = new Socket(serverAddress.getAddress(), serverAddress.getPort()); + socket.setSoTimeout(timeout); + dataOut = new DataOutputStream(socket.getOutputStream()); + dataIn = new DataInputStream(socket.getInputStream()); + final ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); + final DataOutputStream handshake = new DataOutputStream(byteOut); + handshake.write(0); + PacketUtils.writeVarInt(handshake, 4); + PacketUtils.writeString(handshake, serverAddress.getAddress(), PacketUtils.UTF8); + handshake.writeShort(serverAddress.getPort()); + PacketUtils.writeVarInt(handshake, 1); + byte[] bytes = byteOut.toByteArray(); + PacketUtils.writeVarInt(dataOut, bytes.length); + dataOut.write(bytes); + bytes = new byte[] { 0 }; + PacketUtils.writeVarInt(dataOut, bytes.length); + dataOut.write(bytes); + PacketUtils.readVarInt(dataIn); + PacketUtils.readVarInt(dataIn); + final byte[] responseData = new byte[PacketUtils.readVarInt(dataIn)]; + dataIn.readFully(responseData); + final String jsonString = new String(responseData, PacketUtils.UTF8); + return new PingResponse(jsonString, serverAddress); + } + finally { + PacketUtils.closeQuietly(dataOut); + PacketUtils.closeQuietly(dataIn); + PacketUtils.closeQuietly(socket); + } + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/protocollib/ProtocolLibHook.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/protocollib/ProtocolLibHook.java index b08bc1a4..d902eff9 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/protocollib/ProtocolLibHook.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/protocollib/ProtocolLibHook.java @@ -1,17 +1,31 @@ -package com.gmail.filoghost.holographicdisplays.bridge.protocollib; - -import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; - -import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; -import com.gmail.filoghost.holographicdisplays.object.CraftHologram; - -public interface ProtocolLibHook { - - public boolean hook(Plugin plugin, NMSManager nmsManager); - - public void sendDestroyEntitiesPacket(Player player, CraftHologram hologram); - - public void sendCreateEntitiesPacket(Player player, CraftHologram hologram); - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.bridge.protocollib; + +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; + +import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; +import com.gmail.filoghost.holographicdisplays.object.CraftHologram; + +public interface ProtocolLibHook { + + public boolean hook(Plugin plugin, NMSManager nmsManager); + + public void sendDestroyEntitiesPacket(Player player, CraftHologram hologram); + + public void sendCreateEntitiesPacket(Player player, CraftHologram hologram); + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/protocollib/current/ProtocolLibHookImpl.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/protocollib/current/ProtocolLibHookImpl.java index d04ba1c1..5a374440 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/protocollib/current/ProtocolLibHookImpl.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/protocollib/current/ProtocolLibHookImpl.java @@ -1,401 +1,415 @@ -package com.gmail.filoghost.holographicdisplays.bridge.protocollib.current; - -import java.util.List; -import java.util.Optional; - -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.entity.Entity; -import org.bukkit.entity.EntityType; -import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; - -import com.comphenix.protocol.PacketType; -import com.comphenix.protocol.ProtocolLibrary; -import com.comphenix.protocol.events.ListenerPriority; -import com.comphenix.protocol.events.PacketAdapter; -import com.comphenix.protocol.events.PacketAdapter.AdapterParameteters; -import com.comphenix.protocol.events.PacketContainer; -import com.comphenix.protocol.events.PacketEvent; -import com.comphenix.protocol.utility.MinecraftReflection; -import com.comphenix.protocol.wrappers.WrappedChatComponent; -import com.comphenix.protocol.wrappers.WrappedDataWatcher; -import com.comphenix.protocol.wrappers.WrappedDataWatcher.Registry; -import com.comphenix.protocol.wrappers.WrappedDataWatcher.Serializer; -import com.comphenix.protocol.wrappers.WrappedDataWatcher.WrappedDataWatcherObject; -import com.comphenix.protocol.wrappers.WrappedWatchableObject; -import com.gmail.filoghost.holographicdisplays.api.Hologram; -import com.gmail.filoghost.holographicdisplays.bridge.protocollib.ProtocolLibHook; -import com.gmail.filoghost.holographicdisplays.bridge.protocollib.current.WrapperPlayServerSpawnEntity.ObjectTypes; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; -import com.gmail.filoghost.holographicdisplays.object.CraftHologram; -import com.gmail.filoghost.holographicdisplays.object.line.CraftHologramLine; -import com.gmail.filoghost.holographicdisplays.object.line.CraftItemLine; -import com.gmail.filoghost.holographicdisplays.object.line.CraftTextLine; -import com.gmail.filoghost.holographicdisplays.object.line.CraftTouchSlimeLine; -import com.gmail.filoghost.holographicdisplays.object.line.CraftTouchableLine; -import com.gmail.filoghost.holographicdisplays.util.NMSVersion; -import com.gmail.filoghost.holographicdisplays.util.Utils; - -/** - * This is for the ProtocolLib versions containing the WrappedDataWatcher.WrappedDataWatcherObject class. - */ -public class ProtocolLibHookImpl implements ProtocolLibHook { - - private NMSManager nmsManager; - - private Serializer - itemSerializer, - intSerializer, - byteSerializer, - stringSerializer, - booleanSerializer, - chatComponentSerializer; - - private int itemstackMetadataWatcherIndex; - private int customNameWatcherIndex; - - @Override - public boolean hook(Plugin plugin, NMSManager nmsManager) { - - String version = Bukkit.getPluginManager().getPlugin("ProtocolLib").getDescription().getVersion(); - if (version.startsWith("3.7-SNAPSHOT")) { - Bukkit.getConsoleSender().sendMessage( - ChatColor.RED + "[Holographic Displays] Detected development version of ProtocolLib, support disabled. " + - "Related functions (the placeholders {player} {displayname} and the visibility API) will not work.\n" + - "The reason is that this version of ProtocolLib is unstable and partly broken. " + - "Please update ProtocolLib."); - return false; - } - - this.nmsManager = nmsManager; - - if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_9_R1)) { - if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_10_R1)) { - itemstackMetadataWatcherIndex = 6; - } else { - itemstackMetadataWatcherIndex = 5; - } - } else { - itemstackMetadataWatcherIndex = 10; - } - - customNameWatcherIndex = 2; - - if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_9_R1)) { - itemSerializer = Registry.get(MinecraftReflection.getItemStackClass()); - intSerializer = Registry.get(Integer.class); - byteSerializer = Registry.get(Byte.class); - stringSerializer = Registry.get(String.class); - booleanSerializer = Registry.get(Boolean.class); - } - - if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_13_R1)) { - chatComponentSerializer = Registry.get(MinecraftReflection.getIChatBaseComponentClass(), true); - } - - AdapterParameteters params = PacketAdapter - .params() - .plugin(plugin) - .types( PacketType.Play.Server.SPAWN_ENTITY_LIVING, - PacketType.Play.Server.SPAWN_ENTITY, - PacketType.Play.Server.ENTITY_METADATA) - .serverSide() - .listenerPriority(ListenerPriority.NORMAL); - - ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(params) { - - @Override - public void onPacketSending(PacketEvent event) { - - PacketContainer packet = event.getPacket(); - - if (event.getPlayer().getClass().getName().equals("com.comphenix.net.sf.cglib.proxy.Factory")) { - return; // Ignore temporary players (reference: https://github.com/dmulloy2/ProtocolLib/issues/349) - } - - // Spawn entity packet - if (packet.getType() == PacketType.Play.Server.SPAWN_ENTITY_LIVING) { - - WrapperPlayServerSpawnEntityLiving spawnEntityPacket = new WrapperPlayServerSpawnEntityLiving(packet); - Entity entity = spawnEntityPacket.getEntity(event); - - if (entity == null || !isHologramType(entity.getType())) { - return; - } - - Hologram hologram = getHologram(entity); - if (hologram == null) { - return; - } - - Player player = event.getPlayer(); - if (!hologram.getVisibilityManager().isVisibleTo(player)) { - event.setCancelled(true); - return; - } - - WrappedWatchableObject customNameWatchableObject = spawnEntityPacket.getMetadata().getWatchableObject(customNameWatcherIndex); - replacePlayerRelativePlaceholders(customNameWatchableObject, event.getPlayer()); - - } else if (packet.getType() == PacketType.Play.Server.SPAWN_ENTITY) { - - WrapperPlayServerSpawnEntity spawnEntityPacket = new WrapperPlayServerSpawnEntity(packet); - Entity entity = spawnEntityPacket.getEntity(event); - - if (entity == null) { - return; - } - - if (!isHologramType(entity.getType())) { - return; - } - - Hologram hologram = getHologram(entity); - if (hologram == null) { - return; - } - - Player player = event.getPlayer(); - if (!hologram.getVisibilityManager().isVisibleTo(player)) { - event.setCancelled(true); - return; - } - - } else if (packet.getType() == PacketType.Play.Server.ENTITY_METADATA) { - - WrapperPlayServerEntityMetadata entityMetadataPacket = new WrapperPlayServerEntityMetadata(packet); - Entity entity = entityMetadataPacket.getEntity(event); - - if (entity == null) { - return; - } - - if (!isHologramType(entity.getType())) { - return; - } - - Hologram hologram = getHologram(entity); - if (hologram == null) { - return; - } - - Player player = event.getPlayer(); - if (!hologram.getVisibilityManager().isVisibleTo(player)) { - event.setCancelled(true); - return; - } - - List dataWatcherValues = entityMetadataPacket.getEntityMetadata(); - for (int i = 0; i < dataWatcherValues.size(); i++) { - - WrappedWatchableObject watchableObject = dataWatcherValues.get(i); - if (watchableObject.getIndex() == customNameWatcherIndex) { - - if (replacePlayerRelativePlaceholders(watchableObject, event.getPlayer())) { - entityMetadataPacket.setEntityMetadata(dataWatcherValues); - event.setPacket(entityMetadataPacket.getHandle()); - } - } - } - } - } - }); - - return true; - } - - - private boolean replacePlayerRelativePlaceholders(WrappedWatchableObject customNameWatchableObject, Player player) { - if (customNameWatchableObject == null) { - return true; - } - - Object customNameWatchableObjectValue = customNameWatchableObject.getValue(); - String customName; - - if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_13_R1)) { - if (!(customNameWatchableObjectValue instanceof Optional)) { - return false; - } - - Optional customNameOptional = (Optional) customNameWatchableObjectValue; - if (!customNameOptional.isPresent()) { - return false; - } - - WrappedChatComponent componentWrapper = WrappedChatComponent.fromHandle(customNameOptional.get()); - customName = componentWrapper.getJson(); - - } else { - if (!(customNameWatchableObjectValue instanceof String)) { - return false; - } - - customName = (String) customNameWatchableObjectValue; - } - - if (!customName.contains("{player}") && !customName.contains("{displayname}")) { - return false; - } - - customName = customName.replace("{player}", player.getName()).replace("{displayname}", player.getDisplayName()); - - if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_13_R1)) { - customNameWatchableObject.setValue(Optional.of(WrappedChatComponent.fromJson(customName).getHandle())); - } else { - customNameWatchableObject.setValue(customName); - } - - return true; - } - - - - @Override - public void sendDestroyEntitiesPacket(Player player, CraftHologram hologram) { - List ids = Utils.newList(); - for (CraftHologramLine line : hologram.getLinesUnsafe()) { - if (line.isSpawned()) { - for (int id : line.getEntitiesIDs()) { - ids.add(id); - } - } - } - - if (!ids.isEmpty()) { - WrapperPlayServerEntityDestroy packet = new WrapperPlayServerEntityDestroy(); - packet.setEntities(ids); - packet.sendPacket(player); - } - } - - - @Override - public void sendCreateEntitiesPacket(Player player, CraftHologram hologram) { - for (CraftHologramLine line : hologram.getLinesUnsafe()) { - if (line.isSpawned()) { - - if (line instanceof CraftTextLine) { - CraftTextLine textLine = (CraftTextLine) line; - - if (textLine.isSpawned()) { - sendSpawnArmorStandPacket(player, (NMSArmorStand) textLine.getNmsNameble()); - } - - } else if (line instanceof CraftItemLine) { - CraftItemLine itemLine = (CraftItemLine) line; - - if (itemLine.isSpawned()) { - AbstractPacket itemPacket = new WrapperPlayServerSpawnEntity(itemLine.getNmsItem().getBukkitEntityNMS(), ObjectTypes.ITEM_STACK, 1); - itemPacket.sendPacket(player); - - sendSpawnArmorStandPacket(player, (NMSArmorStand) itemLine.getNmsVehicle()); - sendVehicleAttachPacket(player, itemLine.getNmsVehicle().getIdNMS(), itemLine.getNmsItem().getIdNMS()); - - WrapperPlayServerEntityMetadata itemDataPacket = new WrapperPlayServerEntityMetadata(); - WrappedDataWatcher dataWatcher = new WrappedDataWatcher(); - - if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_9_R1)) { - Object itemStackObject = NMSVersion.isGreaterEqualThan(NMSVersion.v1_11_R1) ? itemLine.getNmsItem().getRawItemStack() : Optional.of(itemLine.getNmsItem().getRawItemStack()); - dataWatcher.setObject(new WrappedDataWatcherObject(itemstackMetadataWatcherIndex, itemSerializer), itemStackObject); - dataWatcher.setObject(new WrappedDataWatcherObject(1, intSerializer), 300); - dataWatcher.setObject(new WrappedDataWatcherObject(0, byteSerializer), (byte) 0); - } else { - dataWatcher.setObject(itemstackMetadataWatcherIndex, itemLine.getNmsItem().getRawItemStack()); - dataWatcher.setObject(1, 300); - dataWatcher.setObject(0, (byte) 0); - } - - itemDataPacket.setEntityMetadata(dataWatcher.getWatchableObjects()); - itemDataPacket.setEntityId(itemLine.getNmsItem().getIdNMS()); - itemDataPacket.sendPacket(player); - } - } - - // Unsafe cast, however both CraftTextLine and CraftItemLine are touchable. - CraftTouchableLine touchableLine = (CraftTouchableLine) line; - - if (touchableLine.isSpawned() && touchableLine.getTouchSlime() != null) { - - CraftTouchSlimeLine touchSlime = touchableLine.getTouchSlime(); - - if (touchSlime.isSpawned()) { - sendSpawnArmorStandPacket(player, (NMSArmorStand) touchSlime.getNmsVehicle()); - - AbstractPacket slimePacket = new WrapperPlayServerSpawnEntityLiving(touchSlime.getNmsSlime().getBukkitEntityNMS()); - slimePacket.sendPacket(player); - - sendVehicleAttachPacket(player, touchSlime.getNmsVehicle().getIdNMS(), touchSlime.getNmsSlime().getIdNMS()); - } - } - } - } - } - - - private void sendSpawnArmorStandPacket(Player receiver, NMSArmorStand armorStand) { - if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_11_R1)) { - WrapperPlayServerSpawnEntity spawnPacket = new WrapperPlayServerSpawnEntity(armorStand.getBukkitEntityNMS(), WrapperPlayServerSpawnEntity.ObjectTypes.ARMOR_STAND, 1); - spawnPacket.sendPacket(receiver); - - WrapperPlayServerEntityMetadata dataPacket = new WrapperPlayServerEntityMetadata(); - WrappedDataWatcher dataWatcher = new WrappedDataWatcher(); - - dataWatcher.setObject(new WrappedDataWatcherObject(0, byteSerializer), (byte) 0x20); // Entity status: invisible - - String customName = armorStand.getCustomNameNMS(); - if (customName != null && !customName.isEmpty()) { - if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_13_R1)) { - dataWatcher.setObject(new WrappedDataWatcherObject(customNameWatcherIndex, chatComponentSerializer), Optional.of(WrappedChatComponent.fromText(customName).getHandle())); - } else { - dataWatcher.setObject(new WrappedDataWatcherObject(customNameWatcherIndex, stringSerializer), customName); - } - dataWatcher.setObject(new WrappedDataWatcherObject(3, booleanSerializer), true); // Custom name visible - } - - dataWatcher.setObject(new WrappedDataWatcherObject(5, booleanSerializer), true); // No gravity - dataWatcher.setObject(new WrappedDataWatcherObject(11, byteSerializer), (byte) (0x01 | 0x08 | 0x10)); // Armor stand data: small, no base plate, marker - - dataPacket.setEntityMetadata(dataWatcher.getWatchableObjects()); - dataPacket.setEntityId(armorStand.getIdNMS()); - dataPacket.sendPacket(receiver); - - } else { - WrapperPlayServerSpawnEntityLiving spawnPacket = new WrapperPlayServerSpawnEntityLiving(armorStand.getBukkitEntityNMS()); - spawnPacket.sendPacket(receiver); - } - } - - - private void sendVehicleAttachPacket(Player receiver, int vehicleId, int passengerId) { - if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_9_R1)) { - WrapperPlayServerMount attachPacket = new WrapperPlayServerMount(); - attachPacket.setVehicleId(vehicleId); - attachPacket.setPassengers(new int[] {passengerId}); - attachPacket.sendPacket(receiver); - } else { - WrapperPlayServerAttachEntity attachPacket = new WrapperPlayServerAttachEntity(); - attachPacket.setVehicleId(vehicleId); - attachPacket.setEntityId(passengerId); - attachPacket.sendPacket(receiver); - } - } - - - private boolean isHologramType(EntityType type) { - return type == EntityType.ARMOR_STAND || type == EntityType.DROPPED_ITEM || type == EntityType.SLIME; - } - - - private Hologram getHologram(Entity bukkitEntity) { - NMSEntityBase entity = nmsManager.getNMSEntityBase(bukkitEntity); - if (entity != null) { - return entity.getHologramLine().getParent(); - } - - return null; - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.bridge.protocollib.current; + +import java.util.List; +import java.util.Optional; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; + +import com.comphenix.protocol.PacketType; +import com.comphenix.protocol.ProtocolLibrary; +import com.comphenix.protocol.events.ListenerPriority; +import com.comphenix.protocol.events.PacketAdapter; +import com.comphenix.protocol.events.PacketAdapter.AdapterParameteters; +import com.comphenix.protocol.events.PacketContainer; +import com.comphenix.protocol.events.PacketEvent; +import com.comphenix.protocol.utility.MinecraftReflection; +import com.comphenix.protocol.wrappers.WrappedChatComponent; +import com.comphenix.protocol.wrappers.WrappedDataWatcher; +import com.comphenix.protocol.wrappers.WrappedDataWatcher.Registry; +import com.comphenix.protocol.wrappers.WrappedDataWatcher.Serializer; +import com.comphenix.protocol.wrappers.WrappedDataWatcher.WrappedDataWatcherObject; +import com.comphenix.protocol.wrappers.WrappedWatchableObject; +import com.gmail.filoghost.holographicdisplays.api.Hologram; +import com.gmail.filoghost.holographicdisplays.bridge.protocollib.ProtocolLibHook; +import com.gmail.filoghost.holographicdisplays.bridge.protocollib.current.WrapperPlayServerSpawnEntity.ObjectTypes; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; +import com.gmail.filoghost.holographicdisplays.object.CraftHologram; +import com.gmail.filoghost.holographicdisplays.object.line.CraftHologramLine; +import com.gmail.filoghost.holographicdisplays.object.line.CraftItemLine; +import com.gmail.filoghost.holographicdisplays.object.line.CraftTextLine; +import com.gmail.filoghost.holographicdisplays.object.line.CraftTouchSlimeLine; +import com.gmail.filoghost.holographicdisplays.object.line.CraftTouchableLine; +import com.gmail.filoghost.holographicdisplays.util.NMSVersion; +import com.gmail.filoghost.holographicdisplays.util.Utils; + +/** + * This is for the ProtocolLib versions containing the WrappedDataWatcher.WrappedDataWatcherObject class. + */ +public class ProtocolLibHookImpl implements ProtocolLibHook { + + private NMSManager nmsManager; + + private Serializer + itemSerializer, + intSerializer, + byteSerializer, + stringSerializer, + booleanSerializer, + chatComponentSerializer; + + private int itemstackMetadataWatcherIndex; + private int customNameWatcherIndex; + + @Override + public boolean hook(Plugin plugin, NMSManager nmsManager) { + + String version = Bukkit.getPluginManager().getPlugin("ProtocolLib").getDescription().getVersion(); + if (version.startsWith("3.7-SNAPSHOT")) { + Bukkit.getConsoleSender().sendMessage( + ChatColor.RED + "[Holographic Displays] Detected development version of ProtocolLib, support disabled. " + + "Related functions (the placeholders {player} {displayname} and the visibility API) will not work.\n" + + "The reason is that this version of ProtocolLib is unstable and partly broken. " + + "Please update ProtocolLib."); + return false; + } + + this.nmsManager = nmsManager; + + if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_9_R1)) { + if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_10_R1)) { + itemstackMetadataWatcherIndex = 6; + } else { + itemstackMetadataWatcherIndex = 5; + } + } else { + itemstackMetadataWatcherIndex = 10; + } + + customNameWatcherIndex = 2; + + if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_9_R1)) { + itemSerializer = Registry.get(MinecraftReflection.getItemStackClass()); + intSerializer = Registry.get(Integer.class); + byteSerializer = Registry.get(Byte.class); + stringSerializer = Registry.get(String.class); + booleanSerializer = Registry.get(Boolean.class); + } + + if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_13_R1)) { + chatComponentSerializer = Registry.get(MinecraftReflection.getIChatBaseComponentClass(), true); + } + + AdapterParameteters params = PacketAdapter + .params() + .plugin(plugin) + .types( PacketType.Play.Server.SPAWN_ENTITY_LIVING, + PacketType.Play.Server.SPAWN_ENTITY, + PacketType.Play.Server.ENTITY_METADATA) + .serverSide() + .listenerPriority(ListenerPriority.NORMAL); + + ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(params) { + + @Override + public void onPacketSending(PacketEvent event) { + + PacketContainer packet = event.getPacket(); + + if (event.getPlayer().getClass().getName().equals("com.comphenix.net.sf.cglib.proxy.Factory")) { + return; // Ignore temporary players (reference: https://github.com/dmulloy2/ProtocolLib/issues/349) + } + + // Spawn entity packet + if (packet.getType() == PacketType.Play.Server.SPAWN_ENTITY_LIVING) { + + WrapperPlayServerSpawnEntityLiving spawnEntityPacket = new WrapperPlayServerSpawnEntityLiving(packet); + Entity entity = spawnEntityPacket.getEntity(event); + + if (entity == null || !isHologramType(entity.getType())) { + return; + } + + Hologram hologram = getHologram(entity); + if (hologram == null) { + return; + } + + Player player = event.getPlayer(); + if (!hologram.getVisibilityManager().isVisibleTo(player)) { + event.setCancelled(true); + return; + } + + WrappedWatchableObject customNameWatchableObject = spawnEntityPacket.getMetadata().getWatchableObject(customNameWatcherIndex); + replacePlayerRelativePlaceholders(customNameWatchableObject, event.getPlayer()); + + } else if (packet.getType() == PacketType.Play.Server.SPAWN_ENTITY) { + + WrapperPlayServerSpawnEntity spawnEntityPacket = new WrapperPlayServerSpawnEntity(packet); + Entity entity = spawnEntityPacket.getEntity(event); + + if (entity == null) { + return; + } + + if (!isHologramType(entity.getType())) { + return; + } + + Hologram hologram = getHologram(entity); + if (hologram == null) { + return; + } + + Player player = event.getPlayer(); + if (!hologram.getVisibilityManager().isVisibleTo(player)) { + event.setCancelled(true); + return; + } + + } else if (packet.getType() == PacketType.Play.Server.ENTITY_METADATA) { + + WrapperPlayServerEntityMetadata entityMetadataPacket = new WrapperPlayServerEntityMetadata(packet); + Entity entity = entityMetadataPacket.getEntity(event); + + if (entity == null) { + return; + } + + if (!isHologramType(entity.getType())) { + return; + } + + Hologram hologram = getHologram(entity); + if (hologram == null) { + return; + } + + Player player = event.getPlayer(); + if (!hologram.getVisibilityManager().isVisibleTo(player)) { + event.setCancelled(true); + return; + } + + List dataWatcherValues = entityMetadataPacket.getEntityMetadata(); + for (int i = 0; i < dataWatcherValues.size(); i++) { + + WrappedWatchableObject watchableObject = dataWatcherValues.get(i); + if (watchableObject.getIndex() == customNameWatcherIndex) { + + if (replacePlayerRelativePlaceholders(watchableObject, event.getPlayer())) { + entityMetadataPacket.setEntityMetadata(dataWatcherValues); + event.setPacket(entityMetadataPacket.getHandle()); + } + } + } + } + } + }); + + return true; + } + + + private boolean replacePlayerRelativePlaceholders(WrappedWatchableObject customNameWatchableObject, Player player) { + if (customNameWatchableObject == null) { + return true; + } + + Object customNameWatchableObjectValue = customNameWatchableObject.getValue(); + String customName; + + if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_13_R1)) { + if (!(customNameWatchableObjectValue instanceof Optional)) { + return false; + } + + Optional customNameOptional = (Optional) customNameWatchableObjectValue; + if (!customNameOptional.isPresent()) { + return false; + } + + WrappedChatComponent componentWrapper = WrappedChatComponent.fromHandle(customNameOptional.get()); + customName = componentWrapper.getJson(); + + } else { + if (!(customNameWatchableObjectValue instanceof String)) { + return false; + } + + customName = (String) customNameWatchableObjectValue; + } + + if (!customName.contains("{player}") && !customName.contains("{displayname}")) { + return false; + } + + customName = customName.replace("{player}", player.getName()).replace("{displayname}", player.getDisplayName()); + + if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_13_R1)) { + customNameWatchableObject.setValue(Optional.of(WrappedChatComponent.fromJson(customName).getHandle())); + } else { + customNameWatchableObject.setValue(customName); + } + + return true; + } + + + + @Override + public void sendDestroyEntitiesPacket(Player player, CraftHologram hologram) { + List ids = Utils.newList(); + for (CraftHologramLine line : hologram.getLinesUnsafe()) { + if (line.isSpawned()) { + for (int id : line.getEntitiesIDs()) { + ids.add(id); + } + } + } + + if (!ids.isEmpty()) { + WrapperPlayServerEntityDestroy packet = new WrapperPlayServerEntityDestroy(); + packet.setEntities(ids); + packet.sendPacket(player); + } + } + + + @Override + public void sendCreateEntitiesPacket(Player player, CraftHologram hologram) { + for (CraftHologramLine line : hologram.getLinesUnsafe()) { + if (line.isSpawned()) { + + if (line instanceof CraftTextLine) { + CraftTextLine textLine = (CraftTextLine) line; + + if (textLine.isSpawned()) { + sendSpawnArmorStandPacket(player, (NMSArmorStand) textLine.getNmsNameble()); + } + + } else if (line instanceof CraftItemLine) { + CraftItemLine itemLine = (CraftItemLine) line; + + if (itemLine.isSpawned()) { + AbstractPacket itemPacket = new WrapperPlayServerSpawnEntity(itemLine.getNmsItem().getBukkitEntityNMS(), ObjectTypes.ITEM_STACK, 1); + itemPacket.sendPacket(player); + + sendSpawnArmorStandPacket(player, (NMSArmorStand) itemLine.getNmsVehicle()); + sendVehicleAttachPacket(player, itemLine.getNmsVehicle().getIdNMS(), itemLine.getNmsItem().getIdNMS()); + + WrapperPlayServerEntityMetadata itemDataPacket = new WrapperPlayServerEntityMetadata(); + WrappedDataWatcher dataWatcher = new WrappedDataWatcher(); + + if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_9_R1)) { + Object itemStackObject = NMSVersion.isGreaterEqualThan(NMSVersion.v1_11_R1) ? itemLine.getNmsItem().getRawItemStack() : Optional.of(itemLine.getNmsItem().getRawItemStack()); + dataWatcher.setObject(new WrappedDataWatcherObject(itemstackMetadataWatcherIndex, itemSerializer), itemStackObject); + dataWatcher.setObject(new WrappedDataWatcherObject(1, intSerializer), 300); + dataWatcher.setObject(new WrappedDataWatcherObject(0, byteSerializer), (byte) 0); + } else { + dataWatcher.setObject(itemstackMetadataWatcherIndex, itemLine.getNmsItem().getRawItemStack()); + dataWatcher.setObject(1, 300); + dataWatcher.setObject(0, (byte) 0); + } + + itemDataPacket.setEntityMetadata(dataWatcher.getWatchableObjects()); + itemDataPacket.setEntityId(itemLine.getNmsItem().getIdNMS()); + itemDataPacket.sendPacket(player); + } + } + + // Unsafe cast, however both CraftTextLine and CraftItemLine are touchable. + CraftTouchableLine touchableLine = (CraftTouchableLine) line; + + if (touchableLine.isSpawned() && touchableLine.getTouchSlime() != null) { + + CraftTouchSlimeLine touchSlime = touchableLine.getTouchSlime(); + + if (touchSlime.isSpawned()) { + sendSpawnArmorStandPacket(player, (NMSArmorStand) touchSlime.getNmsVehicle()); + + AbstractPacket slimePacket = new WrapperPlayServerSpawnEntityLiving(touchSlime.getNmsSlime().getBukkitEntityNMS()); + slimePacket.sendPacket(player); + + sendVehicleAttachPacket(player, touchSlime.getNmsVehicle().getIdNMS(), touchSlime.getNmsSlime().getIdNMS()); + } + } + } + } + } + + + private void sendSpawnArmorStandPacket(Player receiver, NMSArmorStand armorStand) { + if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_11_R1)) { + WrapperPlayServerSpawnEntity spawnPacket = new WrapperPlayServerSpawnEntity(armorStand.getBukkitEntityNMS(), WrapperPlayServerSpawnEntity.ObjectTypes.ARMOR_STAND, 1); + spawnPacket.sendPacket(receiver); + + WrapperPlayServerEntityMetadata dataPacket = new WrapperPlayServerEntityMetadata(); + WrappedDataWatcher dataWatcher = new WrappedDataWatcher(); + + dataWatcher.setObject(new WrappedDataWatcherObject(0, byteSerializer), (byte) 0x20); // Entity status: invisible + + String customName = armorStand.getCustomNameNMS(); + if (customName != null && !customName.isEmpty()) { + if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_13_R1)) { + dataWatcher.setObject(new WrappedDataWatcherObject(customNameWatcherIndex, chatComponentSerializer), Optional.of(WrappedChatComponent.fromText(customName).getHandle())); + } else { + dataWatcher.setObject(new WrappedDataWatcherObject(customNameWatcherIndex, stringSerializer), customName); + } + dataWatcher.setObject(new WrappedDataWatcherObject(3, booleanSerializer), true); // Custom name visible + } + + dataWatcher.setObject(new WrappedDataWatcherObject(5, booleanSerializer), true); // No gravity + dataWatcher.setObject(new WrappedDataWatcherObject(11, byteSerializer), (byte) (0x01 | 0x08 | 0x10)); // Armor stand data: small, no base plate, marker + + dataPacket.setEntityMetadata(dataWatcher.getWatchableObjects()); + dataPacket.setEntityId(armorStand.getIdNMS()); + dataPacket.sendPacket(receiver); + + } else { + WrapperPlayServerSpawnEntityLiving spawnPacket = new WrapperPlayServerSpawnEntityLiving(armorStand.getBukkitEntityNMS()); + spawnPacket.sendPacket(receiver); + } + } + + + private void sendVehicleAttachPacket(Player receiver, int vehicleId, int passengerId) { + if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_9_R1)) { + WrapperPlayServerMount attachPacket = new WrapperPlayServerMount(); + attachPacket.setVehicleId(vehicleId); + attachPacket.setPassengers(new int[] {passengerId}); + attachPacket.sendPacket(receiver); + } else { + WrapperPlayServerAttachEntity attachPacket = new WrapperPlayServerAttachEntity(); + attachPacket.setVehicleId(vehicleId); + attachPacket.setEntityId(passengerId); + attachPacket.sendPacket(receiver); + } + } + + + private boolean isHologramType(EntityType type) { + return type == EntityType.ARMOR_STAND || type == EntityType.DROPPED_ITEM || type == EntityType.SLIME; + } + + + private Hologram getHologram(Entity bukkitEntity) { + NMSEntityBase entity = nmsManager.getNMSEntityBase(bukkitEntity); + if (entity != null) { + return entity.getHologramLine().getParent(); + } + + return null; + } +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/protocollib/old/ProtocolLibHookImpl.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/protocollib/old/ProtocolLibHookImpl.java index a6010854..e148d6eb 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/protocollib/old/ProtocolLibHookImpl.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/protocollib/old/ProtocolLibHookImpl.java @@ -1,280 +1,294 @@ -package com.gmail.filoghost.holographicdisplays.bridge.protocollib.old; - -import java.lang.reflect.Constructor; -import java.util.List; - -import org.bukkit.entity.Entity; -import org.bukkit.entity.EntityType; -import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; - -import com.comphenix.protocol.PacketType; -import com.comphenix.protocol.ProtocolLibrary; -import com.comphenix.protocol.events.ListenerPriority; -import com.comphenix.protocol.events.PacketAdapter; -import com.comphenix.protocol.events.PacketContainer; -import com.comphenix.protocol.events.PacketEvent; -import com.comphenix.protocol.wrappers.WrappedDataWatcher; -import com.comphenix.protocol.wrappers.WrappedWatchableObject; -import com.gmail.filoghost.holographicdisplays.api.Hologram; -import com.gmail.filoghost.holographicdisplays.bridge.protocollib.ProtocolLibHook; -import com.gmail.filoghost.holographicdisplays.bridge.protocollib.old.WrapperPlayServerSpawnEntity.ObjectTypes; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; -import com.gmail.filoghost.holographicdisplays.object.CraftHologram; -import com.gmail.filoghost.holographicdisplays.object.line.CraftHologramLine; -import com.gmail.filoghost.holographicdisplays.object.line.CraftItemLine; -import com.gmail.filoghost.holographicdisplays.object.line.CraftTextLine; -import com.gmail.filoghost.holographicdisplays.object.line.CraftTouchSlimeLine; -import com.gmail.filoghost.holographicdisplays.object.line.CraftTouchableLine; -import com.gmail.filoghost.holographicdisplays.util.Utils; - -/** - * This is for the ProtocolLib versions without the WrappedDataWatcher.WrappedDataWatcherObject class. - * - * These versions are only used for 1.8. - */ -public class ProtocolLibHookImpl implements ProtocolLibHook { - - private NMSManager nmsManager; - - private int customNameWatcherIndex = 2; - - @Override - public boolean hook(Plugin plugin, NMSManager nmsManager) { - this.nmsManager = nmsManager; - - ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(plugin, ListenerPriority.NORMAL, PacketType.Play.Server.SPAWN_ENTITY_LIVING, PacketType.Play.Server.SPAWN_ENTITY, PacketType.Play.Server.ENTITY_METADATA) { - - @Override - public void onPacketSending(PacketEvent event) { - - PacketContainer packet = event.getPacket(); - - // Spawn entity packet - if (packet.getType() == PacketType.Play.Server.SPAWN_ENTITY_LIVING) { - - WrapperPlayServerSpawnEntityLiving spawnEntityPacket = new WrapperPlayServerSpawnEntityLiving(packet); - Entity entity = spawnEntityPacket.getEntity(event); - - if (entity == null || !isHologramType(entity.getType())) { - return; - } - - Hologram hologram = getHologram(entity); - if (hologram == null) { - return; - } - - Player player = event.getPlayer(); - if (!hologram.getVisibilityManager().isVisibleTo(player)) { - event.setCancelled(true); - return; - } - - WrappedDataWatcher dataWatcher = spawnEntityPacket.getMetadata(); - String customName = dataWatcher.getString(customNameWatcherIndex); - - if (customName == null) { - return; - } - - if (customName.contains("{player}") || customName.contains("{displayname}")) { - - WrappedDataWatcher dataWatcherClone = dataWatcher.deepClone(); - dataWatcherClone.setObject(customNameWatcherIndex, customName.replace("{player}", player.getName()).replace("{displayname}", player.getDisplayName())); - spawnEntityPacket.setMetadata(dataWatcherClone); - event.setPacket(spawnEntityPacket.getHandle()); - - } - - } else if (packet.getType() == PacketType.Play.Server.SPAWN_ENTITY) { - - WrapperPlayServerSpawnEntity spawnEntityPacket = new WrapperPlayServerSpawnEntity(packet); - int objectId = spawnEntityPacket.getType(); - if (objectId != ObjectTypes.ITEM_STACK && objectId != ObjectTypes.ARMOR_STAND) { - return; - } - - Entity entity = spawnEntityPacket.getEntity(event); - if (entity == null) { - return; - } - - Hologram hologram = getHologram(entity); - if (hologram == null) { - return; - } - - Player player = event.getPlayer(); - if (!hologram.getVisibilityManager().isVisibleTo(player)) { - event.setCancelled(true); - return; - } - - } else if (packet.getType() == PacketType.Play.Server.ENTITY_METADATA) { - - WrapperPlayServerEntityMetadata entityMetadataPacket = new WrapperPlayServerEntityMetadata(packet); - Entity entity = entityMetadataPacket.getEntity(event); - - if (entity == null) { - return; - } - - if (entity.getType() != EntityType.ARMOR_STAND) { - // Enough, only armorstands are used with custom names. - return; - } - - Hologram hologram = getHologram(entity); - if (hologram == null) { - return; - } - - Player player = event.getPlayer(); - if (!hologram.getVisibilityManager().isVisibleTo(player)) { - event.setCancelled(true); - return; - } - - List dataWatcherValues = entityMetadataPacket.getEntityMetadata(); - - for (int i = 0; i < dataWatcherValues.size(); i++) { - WrappedWatchableObject dataWatcherValue = dataWatcherValues.get(i); - - if (dataWatcherValue.getIndex() == customNameWatcherIndex && dataWatcherValue.getValue() != null) { - - Object customNameObject = dataWatcherValue.getValue(); - if (customNameObject == null || customNameObject instanceof String == false) { - return; - } - - String customName = (String) customNameObject; - - if (customName.contains("{player}") || customName.contains("{displayname}")) { - - entityMetadataPacket = new WrapperPlayServerEntityMetadata(packet.deepClone()); - List clonedList = entityMetadataPacket.getEntityMetadata(); - WrappedWatchableObject clonedElement = clonedList.get(i); - clonedElement.setValue(customName.replace("{player}", player.getName()).replace("{displayname}", player.getDisplayName())); - entityMetadataPacket.setEntityMetadata(clonedList); - event.setPacket(entityMetadataPacket.getHandle()); - return; - - } - } - } - } - } - }); - - return true; - } - - public void sendDestroyEntitiesPacket(Player player, CraftHologram hologram) { - List ids = Utils.newList(); - for (CraftHologramLine line : hologram.getLinesUnsafe()) { - if (line.isSpawned()) { - for (int id : line.getEntitiesIDs()) { - ids.add(id); - } - } - } - - if (!ids.isEmpty()) { - WrapperPlayServerEntityDestroy packet = new WrapperPlayServerEntityDestroy(); - packet.setEntities(ids); - packet.sendPacket(player); - } - } - - public void sendCreateEntitiesPacket(Player player, CraftHologram hologram) { - for (CraftHologramLine line : hologram.getLinesUnsafe()) { - if (line.isSpawned()) { - - if (line instanceof CraftTextLine) { - CraftTextLine textLine = (CraftTextLine) line; - - if (textLine.isSpawned()) { - AbstractPacket nameablePacket = new WrapperPlayServerSpawnEntityLiving(textLine.getNmsNameble().getBukkitEntityNMS()); - nameablePacket.sendPacket(player); - } - - } else if (line instanceof CraftItemLine) { - CraftItemLine itemLine = (CraftItemLine) line; - - if (itemLine.isSpawned()) { - AbstractPacket itemPacket = new WrapperPlayServerSpawnEntity(itemLine.getNmsItem().getBukkitEntityNMS(), ObjectTypes.ITEM_STACK, 1); - itemPacket.sendPacket(player); - - AbstractPacket vehiclePacket = new WrapperPlayServerSpawnEntityLiving(itemLine.getNmsVehicle().getBukkitEntityNMS()); - vehiclePacket.sendPacket(player); - - WrapperPlayServerAttachEntity attachPacket = new WrapperPlayServerAttachEntity(); - attachPacket.setVehicleId(itemLine.getNmsVehicle().getIdNMS()); - attachPacket.setEntityId(itemLine.getNmsItem().getIdNMS()); - attachPacket.sendPacket(player); - - WrapperPlayServerEntityMetadata itemDataPacket = new WrapperPlayServerEntityMetadata(); - - List metadata = Utils.newList(); - metadata.add(createWrappedWatchableObject(10, itemLine.getItemStack())); - metadata.add(createWrappedWatchableObject(1, (short) 300)); - metadata.add(createWrappedWatchableObject(0, (byte) 0)); - itemDataPacket.setEntityMetadata(metadata); - itemDataPacket.setEntityId(itemLine.getNmsItem().getIdNMS()); - itemDataPacket.sendPacket(player); - } - } - - // Unsafe cast, however both CraftTextLine and CraftItemLine are touchable. - CraftTouchableLine touchableLine = (CraftTouchableLine) line; - - if (touchableLine.isSpawned() && touchableLine.getTouchSlime() != null) { - - CraftTouchSlimeLine touchSlime = touchableLine.getTouchSlime(); - - if (touchSlime.isSpawned()) { - AbstractPacket vehiclePacket = new WrapperPlayServerSpawnEntityLiving(touchSlime.getNmsVehicle().getBukkitEntityNMS()); - vehiclePacket.sendPacket(player); - - AbstractPacket slimePacket = new WrapperPlayServerSpawnEntityLiving(touchSlime.getNmsSlime().getBukkitEntityNMS()); - slimePacket.sendPacket(player); - - WrapperPlayServerAttachEntity attachPacket = new WrapperPlayServerAttachEntity(); - attachPacket.setVehicleId(touchSlime.getNmsVehicle().getIdNMS()); - attachPacket.setEntityId(touchSlime.getNmsSlime().getIdNMS()); - attachPacket.sendPacket(player); - } - } - } - } - } - - // This is just for compiling - private Constructor wrappedWatchableObjectConstructor; - private WrappedWatchableObject createWrappedWatchableObject(int index, Object value) { - try { - if (wrappedWatchableObjectConstructor == null) { - wrappedWatchableObjectConstructor = WrappedWatchableObject.class.getConstructor(int.class, Object.class); - } - - return (WrappedWatchableObject) wrappedWatchableObjectConstructor.newInstance(index, value); - } catch (Exception ex) { - throw new IllegalStateException("Could not invoke WrappedWatchableObject constructor", ex); - } - } - - private boolean isHologramType(EntityType type) { - return type == EntityType.ARMOR_STAND || type == EntityType.DROPPED_ITEM || type == EntityType.SLIME; - } - - private Hologram getHologram(Entity bukkitEntity) { - NMSEntityBase entity = nmsManager.getNMSEntityBase(bukkitEntity); - if (entity != null) { - return entity.getHologramLine().getParent(); - } - - return null; - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.bridge.protocollib.old; + +import java.lang.reflect.Constructor; +import java.util.List; + +import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; + +import com.comphenix.protocol.PacketType; +import com.comphenix.protocol.ProtocolLibrary; +import com.comphenix.protocol.events.ListenerPriority; +import com.comphenix.protocol.events.PacketAdapter; +import com.comphenix.protocol.events.PacketContainer; +import com.comphenix.protocol.events.PacketEvent; +import com.comphenix.protocol.wrappers.WrappedDataWatcher; +import com.comphenix.protocol.wrappers.WrappedWatchableObject; +import com.gmail.filoghost.holographicdisplays.api.Hologram; +import com.gmail.filoghost.holographicdisplays.bridge.protocollib.ProtocolLibHook; +import com.gmail.filoghost.holographicdisplays.bridge.protocollib.old.WrapperPlayServerSpawnEntity.ObjectTypes; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; +import com.gmail.filoghost.holographicdisplays.object.CraftHologram; +import com.gmail.filoghost.holographicdisplays.object.line.CraftHologramLine; +import com.gmail.filoghost.holographicdisplays.object.line.CraftItemLine; +import com.gmail.filoghost.holographicdisplays.object.line.CraftTextLine; +import com.gmail.filoghost.holographicdisplays.object.line.CraftTouchSlimeLine; +import com.gmail.filoghost.holographicdisplays.object.line.CraftTouchableLine; +import com.gmail.filoghost.holographicdisplays.util.Utils; + +/** + * This is for the ProtocolLib versions without the WrappedDataWatcher.WrappedDataWatcherObject class. + * + * These versions are only used for 1.8. + */ +public class ProtocolLibHookImpl implements ProtocolLibHook { + + private NMSManager nmsManager; + + private int customNameWatcherIndex = 2; + + @Override + public boolean hook(Plugin plugin, NMSManager nmsManager) { + this.nmsManager = nmsManager; + + ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(plugin, ListenerPriority.NORMAL, PacketType.Play.Server.SPAWN_ENTITY_LIVING, PacketType.Play.Server.SPAWN_ENTITY, PacketType.Play.Server.ENTITY_METADATA) { + + @Override + public void onPacketSending(PacketEvent event) { + + PacketContainer packet = event.getPacket(); + + // Spawn entity packet + if (packet.getType() == PacketType.Play.Server.SPAWN_ENTITY_LIVING) { + + WrapperPlayServerSpawnEntityLiving spawnEntityPacket = new WrapperPlayServerSpawnEntityLiving(packet); + Entity entity = spawnEntityPacket.getEntity(event); + + if (entity == null || !isHologramType(entity.getType())) { + return; + } + + Hologram hologram = getHologram(entity); + if (hologram == null) { + return; + } + + Player player = event.getPlayer(); + if (!hologram.getVisibilityManager().isVisibleTo(player)) { + event.setCancelled(true); + return; + } + + WrappedDataWatcher dataWatcher = spawnEntityPacket.getMetadata(); + String customName = dataWatcher.getString(customNameWatcherIndex); + + if (customName == null) { + return; + } + + if (customName.contains("{player}") || customName.contains("{displayname}")) { + + WrappedDataWatcher dataWatcherClone = dataWatcher.deepClone(); + dataWatcherClone.setObject(customNameWatcherIndex, customName.replace("{player}", player.getName()).replace("{displayname}", player.getDisplayName())); + spawnEntityPacket.setMetadata(dataWatcherClone); + event.setPacket(spawnEntityPacket.getHandle()); + + } + + } else if (packet.getType() == PacketType.Play.Server.SPAWN_ENTITY) { + + WrapperPlayServerSpawnEntity spawnEntityPacket = new WrapperPlayServerSpawnEntity(packet); + int objectId = spawnEntityPacket.getType(); + if (objectId != ObjectTypes.ITEM_STACK && objectId != ObjectTypes.ARMOR_STAND) { + return; + } + + Entity entity = spawnEntityPacket.getEntity(event); + if (entity == null) { + return; + } + + Hologram hologram = getHologram(entity); + if (hologram == null) { + return; + } + + Player player = event.getPlayer(); + if (!hologram.getVisibilityManager().isVisibleTo(player)) { + event.setCancelled(true); + return; + } + + } else if (packet.getType() == PacketType.Play.Server.ENTITY_METADATA) { + + WrapperPlayServerEntityMetadata entityMetadataPacket = new WrapperPlayServerEntityMetadata(packet); + Entity entity = entityMetadataPacket.getEntity(event); + + if (entity == null) { + return; + } + + if (entity.getType() != EntityType.ARMOR_STAND) { + // Enough, only armorstands are used with custom names. + return; + } + + Hologram hologram = getHologram(entity); + if (hologram == null) { + return; + } + + Player player = event.getPlayer(); + if (!hologram.getVisibilityManager().isVisibleTo(player)) { + event.setCancelled(true); + return; + } + + List dataWatcherValues = entityMetadataPacket.getEntityMetadata(); + + for (int i = 0; i < dataWatcherValues.size(); i++) { + WrappedWatchableObject dataWatcherValue = dataWatcherValues.get(i); + + if (dataWatcherValue.getIndex() == customNameWatcherIndex && dataWatcherValue.getValue() != null) { + + Object customNameObject = dataWatcherValue.getValue(); + if (customNameObject == null || customNameObject instanceof String == false) { + return; + } + + String customName = (String) customNameObject; + + if (customName.contains("{player}") || customName.contains("{displayname}")) { + + entityMetadataPacket = new WrapperPlayServerEntityMetadata(packet.deepClone()); + List clonedList = entityMetadataPacket.getEntityMetadata(); + WrappedWatchableObject clonedElement = clonedList.get(i); + clonedElement.setValue(customName.replace("{player}", player.getName()).replace("{displayname}", player.getDisplayName())); + entityMetadataPacket.setEntityMetadata(clonedList); + event.setPacket(entityMetadataPacket.getHandle()); + return; + + } + } + } + } + } + }); + + return true; + } + + public void sendDestroyEntitiesPacket(Player player, CraftHologram hologram) { + List ids = Utils.newList(); + for (CraftHologramLine line : hologram.getLinesUnsafe()) { + if (line.isSpawned()) { + for (int id : line.getEntitiesIDs()) { + ids.add(id); + } + } + } + + if (!ids.isEmpty()) { + WrapperPlayServerEntityDestroy packet = new WrapperPlayServerEntityDestroy(); + packet.setEntities(ids); + packet.sendPacket(player); + } + } + + public void sendCreateEntitiesPacket(Player player, CraftHologram hologram) { + for (CraftHologramLine line : hologram.getLinesUnsafe()) { + if (line.isSpawned()) { + + if (line instanceof CraftTextLine) { + CraftTextLine textLine = (CraftTextLine) line; + + if (textLine.isSpawned()) { + AbstractPacket nameablePacket = new WrapperPlayServerSpawnEntityLiving(textLine.getNmsNameble().getBukkitEntityNMS()); + nameablePacket.sendPacket(player); + } + + } else if (line instanceof CraftItemLine) { + CraftItemLine itemLine = (CraftItemLine) line; + + if (itemLine.isSpawned()) { + AbstractPacket itemPacket = new WrapperPlayServerSpawnEntity(itemLine.getNmsItem().getBukkitEntityNMS(), ObjectTypes.ITEM_STACK, 1); + itemPacket.sendPacket(player); + + AbstractPacket vehiclePacket = new WrapperPlayServerSpawnEntityLiving(itemLine.getNmsVehicle().getBukkitEntityNMS()); + vehiclePacket.sendPacket(player); + + WrapperPlayServerAttachEntity attachPacket = new WrapperPlayServerAttachEntity(); + attachPacket.setVehicleId(itemLine.getNmsVehicle().getIdNMS()); + attachPacket.setEntityId(itemLine.getNmsItem().getIdNMS()); + attachPacket.sendPacket(player); + + WrapperPlayServerEntityMetadata itemDataPacket = new WrapperPlayServerEntityMetadata(); + + List metadata = Utils.newList(); + metadata.add(createWrappedWatchableObject(10, itemLine.getItemStack())); + metadata.add(createWrappedWatchableObject(1, (short) 300)); + metadata.add(createWrappedWatchableObject(0, (byte) 0)); + itemDataPacket.setEntityMetadata(metadata); + itemDataPacket.setEntityId(itemLine.getNmsItem().getIdNMS()); + itemDataPacket.sendPacket(player); + } + } + + // Unsafe cast, however both CraftTextLine and CraftItemLine are touchable. + CraftTouchableLine touchableLine = (CraftTouchableLine) line; + + if (touchableLine.isSpawned() && touchableLine.getTouchSlime() != null) { + + CraftTouchSlimeLine touchSlime = touchableLine.getTouchSlime(); + + if (touchSlime.isSpawned()) { + AbstractPacket vehiclePacket = new WrapperPlayServerSpawnEntityLiving(touchSlime.getNmsVehicle().getBukkitEntityNMS()); + vehiclePacket.sendPacket(player); + + AbstractPacket slimePacket = new WrapperPlayServerSpawnEntityLiving(touchSlime.getNmsSlime().getBukkitEntityNMS()); + slimePacket.sendPacket(player); + + WrapperPlayServerAttachEntity attachPacket = new WrapperPlayServerAttachEntity(); + attachPacket.setVehicleId(touchSlime.getNmsVehicle().getIdNMS()); + attachPacket.setEntityId(touchSlime.getNmsSlime().getIdNMS()); + attachPacket.sendPacket(player); + } + } + } + } + } + + // This is just for compiling + private Constructor wrappedWatchableObjectConstructor; + private WrappedWatchableObject createWrappedWatchableObject(int index, Object value) { + try { + if (wrappedWatchableObjectConstructor == null) { + wrappedWatchableObjectConstructor = WrappedWatchableObject.class.getConstructor(int.class, Object.class); + } + + return (WrappedWatchableObject) wrappedWatchableObjectConstructor.newInstance(index, value); + } catch (Exception ex) { + throw new IllegalStateException("Could not invoke WrappedWatchableObject constructor", ex); + } + } + + private boolean isHologramType(EntityType type) { + return type == EntityType.ARMOR_STAND || type == EntityType.DROPPED_ITEM || type == EntityType.SLIME; + } + + private Hologram getHologram(Entity bukkitEntity) { + NMSEntityBase entity = nmsManager.getNMSEntityBase(bukkitEntity); + if (entity != null) { + return entity.getHologramLine().getParent(); + } + + return null; + } +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/Colors.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/Colors.java index 7130f0db..13e545ba 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/Colors.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/Colors.java @@ -1,19 +1,33 @@ -package com.gmail.filoghost.holographicdisplays.commands; - -import org.bukkit.ChatColor; - -public class Colors { - - public static final String - - PRIMARY = "" + ChatColor.AQUA, - PRIMARY_SHADOW = "" + ChatColor.DARK_AQUA, - - SECONDARY = "" + ChatColor.WHITE, - SECONDARY_SHADOW = "" + ChatColor.GRAY, - - BOLD = "" + ChatColor.BOLD, - - ERROR = "" + ChatColor.RED; - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.commands; + +import org.bukkit.ChatColor; + +public class Colors { + + public static final String + + PRIMARY = "" + ChatColor.AQUA, + PRIMARY_SHADOW = "" + ChatColor.DARK_AQUA, + + SECONDARY = "" + ChatColor.WHITE, + SECONDARY_SHADOW = "" + ChatColor.GRAY, + + BOLD = "" + ChatColor.BOLD, + + ERROR = "" + ChatColor.RED; + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/CommandValidator.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/CommandValidator.java index 12ba4510..a6f88d80 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/CommandValidator.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/CommandValidator.java @@ -1,50 +1,64 @@ -package com.gmail.filoghost.holographicdisplays.commands; - -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import com.gmail.filoghost.holographicdisplays.exception.CommandException; - -public class CommandValidator { - - public static void notNull(Object obj, String string) throws CommandException { - if (obj == null) { - throw new CommandException(string); - } - } - - public static void isTrue(boolean b, String string) throws CommandException { - if (!b) { - throw new CommandException(string); - } - } - - public static int getInteger(String integer) throws CommandException { - try { - return Integer.parseInt(integer); - } catch (NumberFormatException ex) { - throw new CommandException("Invalid number: '" + integer + "'."); - } - } - - public static boolean isInteger(String integer) { - try { - Integer.parseInt(integer); - return true; - } catch (NumberFormatException ex) { - return false; - } - } - - public static Player getPlayerSender(CommandSender sender) throws CommandException { - if (sender instanceof Player) { - return (Player) sender; - } else { - throw new CommandException("You must be a player to use this command."); - } - } - - public static boolean isPlayerSender(CommandSender sender) { - return sender instanceof Player; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.commands; + +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import com.gmail.filoghost.holographicdisplays.exception.CommandException; + +public class CommandValidator { + + public static void notNull(Object obj, String string) throws CommandException { + if (obj == null) { + throw new CommandException(string); + } + } + + public static void isTrue(boolean b, String string) throws CommandException { + if (!b) { + throw new CommandException(string); + } + } + + public static int getInteger(String integer) throws CommandException { + try { + return Integer.parseInt(integer); + } catch (NumberFormatException ex) { + throw new CommandException("Invalid number: '" + integer + "'."); + } + } + + public static boolean isInteger(String integer) { + try { + Integer.parseInt(integer); + return true; + } catch (NumberFormatException ex) { + return false; + } + } + + public static Player getPlayerSender(CommandSender sender) throws CommandException { + if (sender instanceof Player) { + return (Player) sender; + } else { + throw new CommandException("You must be a player to use this command."); + } + } + + public static boolean isPlayerSender(CommandSender sender) { + return sender instanceof Player; + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/Strings.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/Strings.java index 2a469185..33c89786 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/Strings.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/Strings.java @@ -1,25 +1,39 @@ -package com.gmail.filoghost.holographicdisplays.commands; - -import org.bukkit.ChatColor; -import org.bukkit.command.CommandSender; - -public class Strings { - - public static final String BASE_PERM = "holograms."; - - public static final String TIP_PREFIX = "" + ChatColor.YELLOW + ChatColor.BOLD + "TIP" + Colors.SECONDARY_SHADOW + " "; - - - public static String formatTitle(String input) { - return "" + Colors.PRIMARY_SHADOW + ChatColor.BOLD + "----- " + input + Colors.PRIMARY_SHADOW + ChatColor.BOLD + " -----"; - } - - public static String noSuchHologram(String name) { - return ChatColor.RED + "Cannot find a hologram named \"" + name + "\"."; - } - - public static void sendWarning(CommandSender recipient, String warning) { - recipient.sendMessage(ChatColor.RED + "( " + ChatColor.DARK_RED + ChatColor.BOLD + "!" + ChatColor.RED + " ) " + Colors.SECONDARY_SHADOW + warning); - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.commands; + +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; + +public class Strings { + + public static final String BASE_PERM = "holograms."; + + public static final String TIP_PREFIX = "" + ChatColor.YELLOW + ChatColor.BOLD + "TIP" + Colors.SECONDARY_SHADOW + " "; + + + public static String formatTitle(String input) { + return "" + Colors.PRIMARY_SHADOW + ChatColor.BOLD + "----- " + input + Colors.PRIMARY_SHADOW + ChatColor.BOLD + " -----"; + } + + public static String noSuchHologram(String name) { + return ChatColor.RED + "Cannot find a hologram named \"" + name + "\"."; + } + + public static void sendWarning(CommandSender recipient, String warning) { + recipient.sendMessage(ChatColor.RED + "( " + ChatColor.DARK_RED + ChatColor.BOLD + "!" + ChatColor.RED + " ) " + Colors.SECONDARY_SHADOW + warning); + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/HologramSubCommand.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/HologramSubCommand.java index a7a9c914..fd330f69 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/HologramSubCommand.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/HologramSubCommand.java @@ -1,72 +1,86 @@ -package com.gmail.filoghost.holographicdisplays.commands.main; - -import java.util.List; - -import org.bukkit.command.CommandSender; - -import com.gmail.filoghost.holographicdisplays.exception.CommandException; - -public abstract class HologramSubCommand { - - private String name; - private String permission; - private String[] aliases; - - public HologramSubCommand(String name) { - this(name, new String[0]); - } - - public HologramSubCommand(String name, String... aliases) { - this.name = name; - this.aliases = aliases; - } - - public String getName() { - return name; - } - - public void setPermission(String permission) { - this.permission = permission; - } - - public String getPermission() { - return permission; - } - - public final boolean hasPermission(CommandSender sender) { - if (permission == null) return true; - return sender.hasPermission(permission); - } - - public abstract String getPossibleArguments(); - - public abstract int getMinimumArguments(); - - public abstract void execute(CommandSender sender, String label, String[] args) throws CommandException; - - public abstract List getTutorial(); - - public abstract SubCommandType getType(); - - public enum SubCommandType { - GENERIC, EDIT_LINES, HIDDEN - } - - - public final boolean isValidTrigger(String name) { - if (this.name.equalsIgnoreCase(name)) { - return true; - } - - if (aliases != null) { - for (String alias : aliases) { - if (alias.equalsIgnoreCase(name)) { - return true; - } - } - } - - return false; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.commands.main; + +import java.util.List; + +import org.bukkit.command.CommandSender; + +import com.gmail.filoghost.holographicdisplays.exception.CommandException; + +public abstract class HologramSubCommand { + + private String name; + private String permission; + private String[] aliases; + + public HologramSubCommand(String name) { + this(name, new String[0]); + } + + public HologramSubCommand(String name, String... aliases) { + this.name = name; + this.aliases = aliases; + } + + public String getName() { + return name; + } + + public void setPermission(String permission) { + this.permission = permission; + } + + public String getPermission() { + return permission; + } + + public final boolean hasPermission(CommandSender sender) { + if (permission == null) return true; + return sender.hasPermission(permission); + } + + public abstract String getPossibleArguments(); + + public abstract int getMinimumArguments(); + + public abstract void execute(CommandSender sender, String label, String[] args) throws CommandException; + + public abstract List getTutorial(); + + public abstract SubCommandType getType(); + + public enum SubCommandType { + GENERIC, EDIT_LINES, HIDDEN + } + + + public final boolean isValidTrigger(String name) { + if (this.name.equalsIgnoreCase(name)) { + return true; + } + + if (aliases != null) { + for (String alias : aliases) { + if (alias.equalsIgnoreCase(name)) { + return true; + } + } + } + + return false; + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/HologramsCommandHandler.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/HologramsCommandHandler.java index 0bdf791e..ded1dec9 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/HologramsCommandHandler.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/HologramsCommandHandler.java @@ -1,108 +1,122 @@ -package com.gmail.filoghost.holographicdisplays.commands.main; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; - -import com.gmail.filoghost.holographicdisplays.HolographicDisplays; -import com.gmail.filoghost.holographicdisplays.commands.Colors; -import com.gmail.filoghost.holographicdisplays.commands.Strings; -import com.gmail.filoghost.holographicdisplays.commands.main.subs.AddlineCommand; -import com.gmail.filoghost.holographicdisplays.commands.main.subs.AlignCommand; -import com.gmail.filoghost.holographicdisplays.commands.main.subs.CopyCommand; -import com.gmail.filoghost.holographicdisplays.commands.main.subs.CreateCommand; -import com.gmail.filoghost.holographicdisplays.commands.main.subs.DeleteCommand; -import com.gmail.filoghost.holographicdisplays.commands.main.subs.EditCommand; -import com.gmail.filoghost.holographicdisplays.commands.main.subs.HelpCommand; -import com.gmail.filoghost.holographicdisplays.commands.main.subs.InfoCommand; -import com.gmail.filoghost.holographicdisplays.commands.main.subs.InsertlineCommand; -import com.gmail.filoghost.holographicdisplays.commands.main.subs.ListCommand; -import com.gmail.filoghost.holographicdisplays.commands.main.subs.MovehereCommand; -import com.gmail.filoghost.holographicdisplays.commands.main.subs.NearCommand; -import com.gmail.filoghost.holographicdisplays.commands.main.subs.ReadimageCommand; -import com.gmail.filoghost.holographicdisplays.commands.main.subs.ReadtextCommand; -import com.gmail.filoghost.holographicdisplays.commands.main.subs.ReloadCommand; -import com.gmail.filoghost.holographicdisplays.commands.main.subs.RemovelineCommand; -import com.gmail.filoghost.holographicdisplays.commands.main.subs.SetlineCommand; -import com.gmail.filoghost.holographicdisplays.commands.main.subs.TeleportCommand; -import com.gmail.filoghost.holographicdisplays.exception.CommandException; -import com.gmail.filoghost.holographicdisplays.util.Utils; - -public class HologramsCommandHandler implements CommandExecutor { - - private List subCommands; - - public HologramsCommandHandler() { - subCommands = Utils.newList(); - - registerSubCommand(new AddlineCommand()); - registerSubCommand(new CreateCommand()); - registerSubCommand(new DeleteCommand()); - registerSubCommand(new EditCommand(this)); - registerSubCommand(new ListCommand()); - registerSubCommand(new NearCommand()); - registerSubCommand(new TeleportCommand()); - registerSubCommand(new MovehereCommand()); - registerSubCommand(new AlignCommand()); - registerSubCommand(new CopyCommand()); - registerSubCommand(new ReloadCommand()); - - registerSubCommand(new RemovelineCommand()); - registerSubCommand(new SetlineCommand()); - registerSubCommand(new InsertlineCommand()); - registerSubCommand(new ReadtextCommand()); - registerSubCommand(new ReadimageCommand()); - registerSubCommand(new InfoCommand()); - - registerSubCommand(new HelpCommand(this)); - } - - public void registerSubCommand(HologramSubCommand subCommand) { - subCommands.add(subCommand); - } - - public List getSubCommands() { - return new ArrayList(subCommands); - } - - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - - if (args.length == 0) { - sender.sendMessage(Colors.PRIMARY_SHADOW + "Server is running " + Colors.PRIMARY + "Holographic Displays " + Colors.PRIMARY_SHADOW + "v" + HolographicDisplays.getInstance().getDescription().getVersion() + " by " + Colors.PRIMARY + "filoghost"); - if (sender.hasPermission(Strings.BASE_PERM + "help")) { - sender.sendMessage(Colors.PRIMARY_SHADOW + "Commands: " + Colors.PRIMARY + "/" + label + " help"); - } - return true; - } - - for (HologramSubCommand subCommand : subCommands) { - if (subCommand.isValidTrigger(args[0])) { - - if (!subCommand.hasPermission(sender)) { - sender.sendMessage(Colors.ERROR + "You don't have permission."); - return true; - } - - if (args.length - 1 >= subCommand.getMinimumArguments()) { - try { - subCommand.execute(sender, label, Arrays.copyOfRange(args, 1, args.length)); - } catch (CommandException e) { - sender.sendMessage(Colors.ERROR + e.getMessage()); - } - } else { - sender.sendMessage(Colors.ERROR + "Usage: /" + label + " " + subCommand.getName() + " " + subCommand.getPossibleArguments()); - } - - return true; - } - } - - sender.sendMessage(Colors.ERROR + "Unknown sub-command. Type \"/" + label + " help\" for a list of commands."); - return true; - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.commands.main; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; + +import com.gmail.filoghost.holographicdisplays.HolographicDisplays; +import com.gmail.filoghost.holographicdisplays.commands.Colors; +import com.gmail.filoghost.holographicdisplays.commands.Strings; +import com.gmail.filoghost.holographicdisplays.commands.main.subs.AddlineCommand; +import com.gmail.filoghost.holographicdisplays.commands.main.subs.AlignCommand; +import com.gmail.filoghost.holographicdisplays.commands.main.subs.CopyCommand; +import com.gmail.filoghost.holographicdisplays.commands.main.subs.CreateCommand; +import com.gmail.filoghost.holographicdisplays.commands.main.subs.DeleteCommand; +import com.gmail.filoghost.holographicdisplays.commands.main.subs.EditCommand; +import com.gmail.filoghost.holographicdisplays.commands.main.subs.HelpCommand; +import com.gmail.filoghost.holographicdisplays.commands.main.subs.InfoCommand; +import com.gmail.filoghost.holographicdisplays.commands.main.subs.InsertlineCommand; +import com.gmail.filoghost.holographicdisplays.commands.main.subs.ListCommand; +import com.gmail.filoghost.holographicdisplays.commands.main.subs.MovehereCommand; +import com.gmail.filoghost.holographicdisplays.commands.main.subs.NearCommand; +import com.gmail.filoghost.holographicdisplays.commands.main.subs.ReadimageCommand; +import com.gmail.filoghost.holographicdisplays.commands.main.subs.ReadtextCommand; +import com.gmail.filoghost.holographicdisplays.commands.main.subs.ReloadCommand; +import com.gmail.filoghost.holographicdisplays.commands.main.subs.RemovelineCommand; +import com.gmail.filoghost.holographicdisplays.commands.main.subs.SetlineCommand; +import com.gmail.filoghost.holographicdisplays.commands.main.subs.TeleportCommand; +import com.gmail.filoghost.holographicdisplays.exception.CommandException; +import com.gmail.filoghost.holographicdisplays.util.Utils; + +public class HologramsCommandHandler implements CommandExecutor { + + private List subCommands; + + public HologramsCommandHandler() { + subCommands = Utils.newList(); + + registerSubCommand(new AddlineCommand()); + registerSubCommand(new CreateCommand()); + registerSubCommand(new DeleteCommand()); + registerSubCommand(new EditCommand(this)); + registerSubCommand(new ListCommand()); + registerSubCommand(new NearCommand()); + registerSubCommand(new TeleportCommand()); + registerSubCommand(new MovehereCommand()); + registerSubCommand(new AlignCommand()); + registerSubCommand(new CopyCommand()); + registerSubCommand(new ReloadCommand()); + + registerSubCommand(new RemovelineCommand()); + registerSubCommand(new SetlineCommand()); + registerSubCommand(new InsertlineCommand()); + registerSubCommand(new ReadtextCommand()); + registerSubCommand(new ReadimageCommand()); + registerSubCommand(new InfoCommand()); + + registerSubCommand(new HelpCommand(this)); + } + + public void registerSubCommand(HologramSubCommand subCommand) { + subCommands.add(subCommand); + } + + public List getSubCommands() { + return new ArrayList(subCommands); + } + + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + + if (args.length == 0) { + sender.sendMessage(Colors.PRIMARY_SHADOW + "Server is running " + Colors.PRIMARY + "Holographic Displays " + Colors.PRIMARY_SHADOW + "v" + HolographicDisplays.getInstance().getDescription().getVersion() + " by " + Colors.PRIMARY + "filoghost"); + if (sender.hasPermission(Strings.BASE_PERM + "help")) { + sender.sendMessage(Colors.PRIMARY_SHADOW + "Commands: " + Colors.PRIMARY + "/" + label + " help"); + } + return true; + } + + for (HologramSubCommand subCommand : subCommands) { + if (subCommand.isValidTrigger(args[0])) { + + if (!subCommand.hasPermission(sender)) { + sender.sendMessage(Colors.ERROR + "You don't have permission."); + return true; + } + + if (args.length - 1 >= subCommand.getMinimumArguments()) { + try { + subCommand.execute(sender, label, Arrays.copyOfRange(args, 1, args.length)); + } catch (CommandException e) { + sender.sendMessage(Colors.ERROR + e.getMessage()); + } + } else { + sender.sendMessage(Colors.ERROR + "Usage: /" + label + " " + subCommand.getName() + " " + subCommand.getPossibleArguments()); + } + + return true; + } + } + + sender.sendMessage(Colors.ERROR + "Unknown sub-command. Type \"/" + label + " help\" for a list of commands."); + return true; + } +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/AddlineCommand.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/AddlineCommand.java index 1748f8fd..9fe18a54 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/AddlineCommand.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/AddlineCommand.java @@ -1,76 +1,90 @@ -package com.gmail.filoghost.holographicdisplays.commands.main.subs; - -import java.util.Arrays; -import java.util.List; - -import org.bukkit.Bukkit; -import org.bukkit.Material; -import org.bukkit.command.CommandSender; - -import com.gmail.filoghost.holographicdisplays.commands.Colors; -import com.gmail.filoghost.holographicdisplays.commands.CommandValidator; -import com.gmail.filoghost.holographicdisplays.commands.Strings; -import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; -import com.gmail.filoghost.holographicdisplays.disk.HologramDatabase; -import com.gmail.filoghost.holographicdisplays.event.NamedHologramEditedEvent; -import com.gmail.filoghost.holographicdisplays.exception.CommandException; -import com.gmail.filoghost.holographicdisplays.object.NamedHologram; -import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; -import com.gmail.filoghost.holographicdisplays.util.ItemUtils; -import com.gmail.filoghost.holographicdisplays.util.Utils; - -public class AddlineCommand extends HologramSubCommand { - - public AddlineCommand() { - super("addline"); - setPermission(Strings.BASE_PERM + "addline"); - } - - @Override - public String getPossibleArguments() { - return " "; - } - - @Override - public int getMinimumArguments() { - return 2; - } - - @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - NamedHologram hologram = NamedHologramManager.getHologram(args[0].toLowerCase()); - CommandValidator.notNull(hologram, Strings.noSuchHologram(args[0].toLowerCase())); - String line = Utils.join(args, " ", 1, args.length); - - // Check material validity - if (line.toLowerCase().startsWith("icon:")) { - String iconMaterial = ItemUtils.stripSpacingChars(line.substring("icon:".length(), line.length())); - - if (iconMaterial.contains(":")) { - iconMaterial = iconMaterial.split(":")[0]; - } - - Material mat = ItemUtils.matchMaterial(iconMaterial); - CommandValidator.notNull(mat, "Invalid icon material."); - } - - hologram.getLinesUnsafe().add(HologramDatabase.readLineFromString(line, hologram)); - hologram.refreshAll(); - - HologramDatabase.saveHologram(hologram); - HologramDatabase.trySaveToDisk(); - sender.sendMessage(Colors.PRIMARY + "Line added!"); - Bukkit.getPluginManager().callEvent(new NamedHologramEditedEvent(hologram)); - } - - @Override - public List getTutorial() { - return Arrays.asList("Adds a line to an existing hologram."); - } - - @Override - public SubCommandType getType() { - return SubCommandType.EDIT_LINES; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.commands.main.subs; + +import java.util.Arrays; +import java.util.List; + +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.command.CommandSender; + +import com.gmail.filoghost.holographicdisplays.commands.Colors; +import com.gmail.filoghost.holographicdisplays.commands.CommandValidator; +import com.gmail.filoghost.holographicdisplays.commands.Strings; +import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; +import com.gmail.filoghost.holographicdisplays.disk.HologramDatabase; +import com.gmail.filoghost.holographicdisplays.event.NamedHologramEditedEvent; +import com.gmail.filoghost.holographicdisplays.exception.CommandException; +import com.gmail.filoghost.holographicdisplays.object.NamedHologram; +import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; +import com.gmail.filoghost.holographicdisplays.util.ItemUtils; +import com.gmail.filoghost.holographicdisplays.util.Utils; + +public class AddlineCommand extends HologramSubCommand { + + public AddlineCommand() { + super("addline"); + setPermission(Strings.BASE_PERM + "addline"); + } + + @Override + public String getPossibleArguments() { + return " "; + } + + @Override + public int getMinimumArguments() { + return 2; + } + + @Override + public void execute(CommandSender sender, String label, String[] args) throws CommandException { + NamedHologram hologram = NamedHologramManager.getHologram(args[0].toLowerCase()); + CommandValidator.notNull(hologram, Strings.noSuchHologram(args[0].toLowerCase())); + String line = Utils.join(args, " ", 1, args.length); + + // Check material validity + if (line.toLowerCase().startsWith("icon:")) { + String iconMaterial = ItemUtils.stripSpacingChars(line.substring("icon:".length(), line.length())); + + if (iconMaterial.contains(":")) { + iconMaterial = iconMaterial.split(":")[0]; + } + + Material mat = ItemUtils.matchMaterial(iconMaterial); + CommandValidator.notNull(mat, "Invalid icon material."); + } + + hologram.getLinesUnsafe().add(HologramDatabase.readLineFromString(line, hologram)); + hologram.refreshAll(); + + HologramDatabase.saveHologram(hologram); + HologramDatabase.trySaveToDisk(); + sender.sendMessage(Colors.PRIMARY + "Line added!"); + Bukkit.getPluginManager().callEvent(new NamedHologramEditedEvent(hologram)); + } + + @Override + public List getTutorial() { + return Arrays.asList("Adds a line to an existing hologram."); + } + + @Override + public SubCommandType getType() { + return SubCommandType.EDIT_LINES; + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/AlignCommand.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/AlignCommand.java index a391eac5..c18c14f4 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/AlignCommand.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/AlignCommand.java @@ -1,79 +1,93 @@ -package com.gmail.filoghost.holographicdisplays.commands.main.subs; - -import java.util.Arrays; -import java.util.List; - -import org.bukkit.Location; -import org.bukkit.command.CommandSender; - -import com.gmail.filoghost.holographicdisplays.commands.Colors; -import com.gmail.filoghost.holographicdisplays.commands.CommandValidator; -import com.gmail.filoghost.holographicdisplays.commands.Strings; -import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; -import com.gmail.filoghost.holographicdisplays.disk.HologramDatabase; -import com.gmail.filoghost.holographicdisplays.exception.CommandException; -import com.gmail.filoghost.holographicdisplays.object.NamedHologram; -import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; - -public class AlignCommand extends HologramSubCommand { - - public AlignCommand() { - super("align"); - setPermission(Strings.BASE_PERM + "align"); - } - - @Override - public String getPossibleArguments() { - return " "; - } - - @Override - public int getMinimumArguments() { - return 3; - } - - @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - NamedHologram hologram = NamedHologramManager.getHologram(args[1].toLowerCase()); - NamedHologram referenceHologram = NamedHologramManager.getHologram(args[2].toLowerCase()); - - CommandValidator.notNull(hologram, Strings.noSuchHologram(args[1].toLowerCase())); - CommandValidator.notNull(referenceHologram, Strings.noSuchHologram(args[2].toLowerCase())); - - CommandValidator.isTrue(hologram != referenceHologram, "The hologram must not be the same!"); - - Location loc = hologram.getLocation(); - - if (args[0].equalsIgnoreCase("x")) { - loc.setX(referenceHologram.getX()); - } else if (args[0].equalsIgnoreCase("y")) { - loc.setY(referenceHologram.getY()); - } else if (args[0].equalsIgnoreCase("z")) { - loc.setZ(referenceHologram.getZ()); - } else if (args[0].equalsIgnoreCase("xz")) { - loc.setX(referenceHologram.getX()); - loc.setZ(referenceHologram.getZ()); - } else { - throw new CommandException("You must specify either X, Y, Z or XZ, " + args[0] + " is not a valid axis."); - } - - hologram.teleport(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ()); - hologram.despawnEntities(); - hologram.refreshAll(); - - HologramDatabase.saveHologram(hologram); - HologramDatabase.trySaveToDisk(); - sender.sendMessage(Colors.PRIMARY + "Hologram \"" + hologram.getName() + "\" aligned to the hologram \"" + referenceHologram.getName() + "\" on the " + args[0].toUpperCase() + " axis."); - } - - @Override - public List getTutorial() { - return Arrays.asList("Aligns the first hologram to the second, in the specified axis."); - } - - @Override - public SubCommandType getType() { - return SubCommandType.GENERIC; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.commands.main.subs; + +import java.util.Arrays; +import java.util.List; + +import org.bukkit.Location; +import org.bukkit.command.CommandSender; + +import com.gmail.filoghost.holographicdisplays.commands.Colors; +import com.gmail.filoghost.holographicdisplays.commands.CommandValidator; +import com.gmail.filoghost.holographicdisplays.commands.Strings; +import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; +import com.gmail.filoghost.holographicdisplays.disk.HologramDatabase; +import com.gmail.filoghost.holographicdisplays.exception.CommandException; +import com.gmail.filoghost.holographicdisplays.object.NamedHologram; +import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; + +public class AlignCommand extends HologramSubCommand { + + public AlignCommand() { + super("align"); + setPermission(Strings.BASE_PERM + "align"); + } + + @Override + public String getPossibleArguments() { + return " "; + } + + @Override + public int getMinimumArguments() { + return 3; + } + + @Override + public void execute(CommandSender sender, String label, String[] args) throws CommandException { + NamedHologram hologram = NamedHologramManager.getHologram(args[1].toLowerCase()); + NamedHologram referenceHologram = NamedHologramManager.getHologram(args[2].toLowerCase()); + + CommandValidator.notNull(hologram, Strings.noSuchHologram(args[1].toLowerCase())); + CommandValidator.notNull(referenceHologram, Strings.noSuchHologram(args[2].toLowerCase())); + + CommandValidator.isTrue(hologram != referenceHologram, "The hologram must not be the same!"); + + Location loc = hologram.getLocation(); + + if (args[0].equalsIgnoreCase("x")) { + loc.setX(referenceHologram.getX()); + } else if (args[0].equalsIgnoreCase("y")) { + loc.setY(referenceHologram.getY()); + } else if (args[0].equalsIgnoreCase("z")) { + loc.setZ(referenceHologram.getZ()); + } else if (args[0].equalsIgnoreCase("xz")) { + loc.setX(referenceHologram.getX()); + loc.setZ(referenceHologram.getZ()); + } else { + throw new CommandException("You must specify either X, Y, Z or XZ, " + args[0] + " is not a valid axis."); + } + + hologram.teleport(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ()); + hologram.despawnEntities(); + hologram.refreshAll(); + + HologramDatabase.saveHologram(hologram); + HologramDatabase.trySaveToDisk(); + sender.sendMessage(Colors.PRIMARY + "Hologram \"" + hologram.getName() + "\" aligned to the hologram \"" + referenceHologram.getName() + "\" on the " + args[0].toUpperCase() + " axis."); + } + + @Override + public List getTutorial() { + return Arrays.asList("Aligns the first hologram to the second, in the specified axis."); + } + + @Override + public SubCommandType getType() { + return SubCommandType.GENERIC; + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/CopyCommand.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/CopyCommand.java index 62c22e2a..565ff108 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/CopyCommand.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/CopyCommand.java @@ -1,69 +1,83 @@ -package com.gmail.filoghost.holographicdisplays.commands.main.subs; - -import java.util.Arrays; -import java.util.List; - -import org.bukkit.command.CommandSender; - -import com.gmail.filoghost.holographicdisplays.commands.Colors; -import com.gmail.filoghost.holographicdisplays.commands.CommandValidator; -import com.gmail.filoghost.holographicdisplays.commands.Strings; -import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; -import com.gmail.filoghost.holographicdisplays.disk.HologramDatabase; -import com.gmail.filoghost.holographicdisplays.exception.CommandException; -import com.gmail.filoghost.holographicdisplays.object.NamedHologram; -import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; -import com.gmail.filoghost.holographicdisplays.object.line.CraftHologramLine; - -public class CopyCommand extends HologramSubCommand { - - public CopyCommand() { - super("copy"); - setPermission(Strings.BASE_PERM + "copy"); - } - - @Override - public String getPossibleArguments() { - return " "; - } - - @Override - public int getMinimumArguments() { - return 2; - } - - @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - - NamedHologram hologramToCopy = NamedHologramManager.getHologram(args[0].toLowerCase()); - NamedHologram intoHologram = NamedHologramManager.getHologram(args[1].toLowerCase()); - - CommandValidator.notNull(hologramToCopy, Strings.noSuchHologram(args[0].toLowerCase())); - CommandValidator.notNull(intoHologram, Strings.noSuchHologram(args[1].toLowerCase())); - - intoHologram.clearLines(); - for (CraftHologramLine line : hologramToCopy.getLinesUnsafe()) { - String lineString = HologramDatabase.saveLineToString(line); - intoHologram.getLinesUnsafe().add(HologramDatabase.readLineFromString(lineString, intoHologram)); - } - - intoHologram.refreshAll(); - - HologramDatabase.saveHologram(intoHologram); - HologramDatabase.trySaveToDisk(); - - sender.sendMessage(Colors.PRIMARY + "Hologram \"" + hologramToCopy.getName() + "\" copied into hologram \"" + intoHologram.getName() + "\"!"); - } - - @Override - public List getTutorial() { - return Arrays.asList( - "Copies the contents of a hologram into another one."); - } - - @Override - public SubCommandType getType() { - return SubCommandType.GENERIC; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.commands.main.subs; + +import java.util.Arrays; +import java.util.List; + +import org.bukkit.command.CommandSender; + +import com.gmail.filoghost.holographicdisplays.commands.Colors; +import com.gmail.filoghost.holographicdisplays.commands.CommandValidator; +import com.gmail.filoghost.holographicdisplays.commands.Strings; +import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; +import com.gmail.filoghost.holographicdisplays.disk.HologramDatabase; +import com.gmail.filoghost.holographicdisplays.exception.CommandException; +import com.gmail.filoghost.holographicdisplays.object.NamedHologram; +import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; +import com.gmail.filoghost.holographicdisplays.object.line.CraftHologramLine; + +public class CopyCommand extends HologramSubCommand { + + public CopyCommand() { + super("copy"); + setPermission(Strings.BASE_PERM + "copy"); + } + + @Override + public String getPossibleArguments() { + return " "; + } + + @Override + public int getMinimumArguments() { + return 2; + } + + @Override + public void execute(CommandSender sender, String label, String[] args) throws CommandException { + + NamedHologram hologramToCopy = NamedHologramManager.getHologram(args[0].toLowerCase()); + NamedHologram intoHologram = NamedHologramManager.getHologram(args[1].toLowerCase()); + + CommandValidator.notNull(hologramToCopy, Strings.noSuchHologram(args[0].toLowerCase())); + CommandValidator.notNull(intoHologram, Strings.noSuchHologram(args[1].toLowerCase())); + + intoHologram.clearLines(); + for (CraftHologramLine line : hologramToCopy.getLinesUnsafe()) { + String lineString = HologramDatabase.saveLineToString(line); + intoHologram.getLinesUnsafe().add(HologramDatabase.readLineFromString(lineString, intoHologram)); + } + + intoHologram.refreshAll(); + + HologramDatabase.saveHologram(intoHologram); + HologramDatabase.trySaveToDisk(); + + sender.sendMessage(Colors.PRIMARY + "Hologram \"" + hologramToCopy.getName() + "\" copied into hologram \"" + intoHologram.getName() + "\"!"); + } + + @Override + public List getTutorial() { + return Arrays.asList( + "Copies the contents of a hologram into another one."); + } + + @Override + public SubCommandType getType() { + return SubCommandType.GENERIC; + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/CreateCommand.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/CreateCommand.java index 79e78488..b25fb416 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/CreateCommand.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/CreateCommand.java @@ -1,98 +1,112 @@ -package com.gmail.filoghost.holographicdisplays.commands.main.subs; - -import java.util.Arrays; -import java.util.List; - -import org.bukkit.Location; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; - -import com.gmail.filoghost.holographicdisplays.commands.Colors; -import com.gmail.filoghost.holographicdisplays.commands.CommandValidator; -import com.gmail.filoghost.holographicdisplays.commands.Strings; -import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; -import com.gmail.filoghost.holographicdisplays.disk.HologramDatabase; -import com.gmail.filoghost.holographicdisplays.exception.CommandException; -import com.gmail.filoghost.holographicdisplays.object.NamedHologram; -import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; -import com.gmail.filoghost.holographicdisplays.util.Utils; - -public class CreateCommand extends HologramSubCommand { - - public CreateCommand() { - super("create"); - setPermission(Strings.BASE_PERM + "create"); - } - - @Override - public String getPossibleArguments() { - return " [text]"; - } - - @Override - public int getMinimumArguments() { - return 1; - } - - @SuppressWarnings("deprecation") - @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - Player player = CommandValidator.getPlayerSender(sender); - String name = args[0].toLowerCase(); - - if (!name.matches("[a-zA-Z0-9_\\-]+")) { - throw new CommandException("The name must contain only alphanumeric chars, underscores and hyphens."); - } - - CommandValidator.isTrue(!NamedHologramManager.isExistingHologram(name), "A hologram with that name already exists."); - - Location spawnLoc = player.getLocation(); - boolean moveUp = player.isOnGround(); - - if (moveUp) { - spawnLoc.add(0.0, 1.2, 0.0); - } - - NamedHologram hologram = new NamedHologram(spawnLoc, name); - NamedHologramManager.addHologram(hologram); - - if (args.length > 1) { - - String text = Utils.join(args, " ", 1, args.length); - CommandValidator.isTrue(!text.equalsIgnoreCase("{empty}"), "The first line should not be empty."); - - hologram.getLinesUnsafe().add(HologramDatabase.readLineFromString(text, hologram)); - player.sendMessage(Colors.SECONDARY_SHADOW + "(Change the lines with /" + label + " edit " + hologram.getName() + ")"); - } else { - hologram.appendTextLine("Default hologram. Change it with " + Colors.PRIMARY + "/" + label + " edit " + hologram.getName()); - } - - hologram.refreshAll(); - - HologramDatabase.saveHologram(hologram); - HologramDatabase.trySaveToDisk(); - Location look = player.getLocation(); - look.setPitch(90); - player.teleport(look, TeleportCause.PLUGIN); - player.sendMessage(Colors.PRIMARY + "You created a hologram named '" + hologram.getName() + "'."); - - if (moveUp) { - player.sendMessage(Colors.SECONDARY_SHADOW + "(You were on the ground, the hologram was automatically moved up. If you use /" + label + " movehere " + hologram.getName() + ", the hologram will be moved to your feet)"); - } - } - - @Override - public List getTutorial() { - return Arrays.asList( - "Creates a new hologram with the given name, that must", - "be alphanumeric. The name will be used as reference to", - "that hologram for editing commands."); - } - - @Override - public SubCommandType getType() { - return SubCommandType.GENERIC; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.commands.main.subs; + +import java.util.Arrays; +import java.util.List; + +import org.bukkit.Location; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; + +import com.gmail.filoghost.holographicdisplays.commands.Colors; +import com.gmail.filoghost.holographicdisplays.commands.CommandValidator; +import com.gmail.filoghost.holographicdisplays.commands.Strings; +import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; +import com.gmail.filoghost.holographicdisplays.disk.HologramDatabase; +import com.gmail.filoghost.holographicdisplays.exception.CommandException; +import com.gmail.filoghost.holographicdisplays.object.NamedHologram; +import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; +import com.gmail.filoghost.holographicdisplays.util.Utils; + +public class CreateCommand extends HologramSubCommand { + + public CreateCommand() { + super("create"); + setPermission(Strings.BASE_PERM + "create"); + } + + @Override + public String getPossibleArguments() { + return " [text]"; + } + + @Override + public int getMinimumArguments() { + return 1; + } + + @SuppressWarnings("deprecation") + @Override + public void execute(CommandSender sender, String label, String[] args) throws CommandException { + Player player = CommandValidator.getPlayerSender(sender); + String name = args[0].toLowerCase(); + + if (!name.matches("[a-zA-Z0-9_\\-]+")) { + throw new CommandException("The name must contain only alphanumeric chars, underscores and hyphens."); + } + + CommandValidator.isTrue(!NamedHologramManager.isExistingHologram(name), "A hologram with that name already exists."); + + Location spawnLoc = player.getLocation(); + boolean moveUp = player.isOnGround(); + + if (moveUp) { + spawnLoc.add(0.0, 1.2, 0.0); + } + + NamedHologram hologram = new NamedHologram(spawnLoc, name); + NamedHologramManager.addHologram(hologram); + + if (args.length > 1) { + + String text = Utils.join(args, " ", 1, args.length); + CommandValidator.isTrue(!text.equalsIgnoreCase("{empty}"), "The first line should not be empty."); + + hologram.getLinesUnsafe().add(HologramDatabase.readLineFromString(text, hologram)); + player.sendMessage(Colors.SECONDARY_SHADOW + "(Change the lines with /" + label + " edit " + hologram.getName() + ")"); + } else { + hologram.appendTextLine("Default hologram. Change it with " + Colors.PRIMARY + "/" + label + " edit " + hologram.getName()); + } + + hologram.refreshAll(); + + HologramDatabase.saveHologram(hologram); + HologramDatabase.trySaveToDisk(); + Location look = player.getLocation(); + look.setPitch(90); + player.teleport(look, TeleportCause.PLUGIN); + player.sendMessage(Colors.PRIMARY + "You created a hologram named '" + hologram.getName() + "'."); + + if (moveUp) { + player.sendMessage(Colors.SECONDARY_SHADOW + "(You were on the ground, the hologram was automatically moved up. If you use /" + label + " movehere " + hologram.getName() + ", the hologram will be moved to your feet)"); + } + } + + @Override + public List getTutorial() { + return Arrays.asList( + "Creates a new hologram with the given name, that must", + "be alphanumeric. The name will be used as reference to", + "that hologram for editing commands."); + } + + @Override + public SubCommandType getType() { + return SubCommandType.GENERIC; + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/DeleteCommand.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/DeleteCommand.java index 8c87d0f1..a03aa14b 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/DeleteCommand.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/DeleteCommand.java @@ -1,59 +1,73 @@ -package com.gmail.filoghost.holographicdisplays.commands.main.subs; - -import java.util.Arrays; -import java.util.List; - -import org.bukkit.command.CommandSender; - -import com.gmail.filoghost.holographicdisplays.commands.Colors; -import com.gmail.filoghost.holographicdisplays.commands.CommandValidator; -import com.gmail.filoghost.holographicdisplays.commands.Strings; -import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; -import com.gmail.filoghost.holographicdisplays.disk.HologramDatabase; -import com.gmail.filoghost.holographicdisplays.exception.CommandException; -import com.gmail.filoghost.holographicdisplays.object.NamedHologram; -import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; - -public class DeleteCommand extends HologramSubCommand { - - public DeleteCommand() { - super("delete", "remove"); - setPermission(Strings.BASE_PERM + "delete"); - } - - @Override - public String getPossibleArguments() { - return ""; - } - - @Override - public int getMinimumArguments() { - return 1; - } - - - - @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - NamedHologram hologram = NamedHologramManager.getHologram(args[0].toLowerCase()); - CommandValidator.notNull(hologram, Strings.noSuchHologram(args[0].toLowerCase())); - - hologram.delete(); - NamedHologramManager.removeHologram(hologram); - HologramDatabase.deleteHologram(hologram.getName()); - - HologramDatabase.trySaveToDisk(); - sender.sendMessage(Colors.PRIMARY + "You deleted the hologram '" + hologram.getName() + "'."); - } - - @Override - public List getTutorial() { - return Arrays.asList("Deletes a hologram. Cannot be undone."); - } - - @Override - public SubCommandType getType() { - return SubCommandType.GENERIC; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.commands.main.subs; + +import java.util.Arrays; +import java.util.List; + +import org.bukkit.command.CommandSender; + +import com.gmail.filoghost.holographicdisplays.commands.Colors; +import com.gmail.filoghost.holographicdisplays.commands.CommandValidator; +import com.gmail.filoghost.holographicdisplays.commands.Strings; +import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; +import com.gmail.filoghost.holographicdisplays.disk.HologramDatabase; +import com.gmail.filoghost.holographicdisplays.exception.CommandException; +import com.gmail.filoghost.holographicdisplays.object.NamedHologram; +import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; + +public class DeleteCommand extends HologramSubCommand { + + public DeleteCommand() { + super("delete", "remove"); + setPermission(Strings.BASE_PERM + "delete"); + } + + @Override + public String getPossibleArguments() { + return ""; + } + + @Override + public int getMinimumArguments() { + return 1; + } + + + + @Override + public void execute(CommandSender sender, String label, String[] args) throws CommandException { + NamedHologram hologram = NamedHologramManager.getHologram(args[0].toLowerCase()); + CommandValidator.notNull(hologram, Strings.noSuchHologram(args[0].toLowerCase())); + + hologram.delete(); + NamedHologramManager.removeHologram(hologram); + HologramDatabase.deleteHologram(hologram.getName()); + + HologramDatabase.trySaveToDisk(); + sender.sendMessage(Colors.PRIMARY + "You deleted the hologram '" + hologram.getName() + "'."); + } + + @Override + public List getTutorial() { + return Arrays.asList("Deletes a hologram. Cannot be undone."); + } + + @Override + public SubCommandType getType() { + return SubCommandType.GENERIC; + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/EditCommand.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/EditCommand.java index 3ff1c9fe..c8922638 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/EditCommand.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/EditCommand.java @@ -1,93 +1,107 @@ -package com.gmail.filoghost.holographicdisplays.commands.main.subs; - -import java.util.Arrays; -import java.util.List; - -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -import com.gmail.filoghost.holographicdisplays.commands.Colors; -import com.gmail.filoghost.holographicdisplays.commands.CommandValidator; -import com.gmail.filoghost.holographicdisplays.commands.Strings; -import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; -import com.gmail.filoghost.holographicdisplays.commands.main.HologramsCommandHandler; -import com.gmail.filoghost.holographicdisplays.exception.CommandException; -import com.gmail.filoghost.holographicdisplays.object.NamedHologram; -import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; -import com.gmail.filoghost.holographicdisplays.util.Utils; - -import net.md_5.bungee.api.ChatColor; -import net.md_5.bungee.api.chat.ClickEvent; -import net.md_5.bungee.api.chat.ComponentBuilder; -import net.md_5.bungee.api.chat.HoverEvent; -import net.md_5.bungee.api.chat.TextComponent; - -public class EditCommand extends HologramSubCommand { - - private HologramsCommandHandler mainCommandHandler; - - public EditCommand(HologramsCommandHandler mainCommandHandler) { - super("edit"); - setPermission(Strings.BASE_PERM + "edit"); - this.mainCommandHandler = mainCommandHandler; - } - - @Override - public String getPossibleArguments() { - return ""; - } - - @Override - public int getMinimumArguments() { - return 1; - } - - - @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - String name = args[0].toLowerCase(); - NamedHologram hologram = NamedHologramManager.getHologram(name); - CommandValidator.notNull(hologram, Strings.noSuchHologram(name)); - - sender.sendMessage(""); - sender.sendMessage(Strings.formatTitle("How to edit the hologram '" + name + "'")); - for (HologramSubCommand subCommand : mainCommandHandler.getSubCommands()) { - if (subCommand.getType() == SubCommandType.EDIT_LINES) { - String usage = "/" + label + " " + subCommand.getName() + (subCommand.getPossibleArguments().length() > 0 ? " " + subCommand.getPossibleArguments().replace("", hologram.getName()).replace("", hologram.getName()) : ""); - - if (CommandValidator.isPlayerSender(sender)) { - - List help = Utils.newList(); - help.add(Colors.PRIMARY + usage); - for (String tutLine : subCommand.getTutorial()) { - help.add(Colors.SECONDARY_SHADOW + tutLine); - } - - ((Player) sender).spigot().sendMessage(new ComponentBuilder(usage) - .color(ChatColor.AQUA) - .event(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, usage)) - .event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, TextComponent.fromLegacyText(Utils.join(help, "\n")))) - .create()); - - } else { - sender.sendMessage(Colors.PRIMARY + usage); - } - } - } - - if (CommandValidator.isPlayerSender(sender)) { - HelpCommand.sendHoverTip((Player) sender); - } - } - - @Override - public List getTutorial() { - return Arrays.asList("Shows the commands to manipulate an existing hologram."); - } - - @Override - public SubCommandType getType() { - return SubCommandType.GENERIC; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.commands.main.subs; + +import java.util.Arrays; +import java.util.List; + +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import com.gmail.filoghost.holographicdisplays.commands.Colors; +import com.gmail.filoghost.holographicdisplays.commands.CommandValidator; +import com.gmail.filoghost.holographicdisplays.commands.Strings; +import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; +import com.gmail.filoghost.holographicdisplays.commands.main.HologramsCommandHandler; +import com.gmail.filoghost.holographicdisplays.exception.CommandException; +import com.gmail.filoghost.holographicdisplays.object.NamedHologram; +import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; +import com.gmail.filoghost.holographicdisplays.util.Utils; + +import net.md_5.bungee.api.ChatColor; +import net.md_5.bungee.api.chat.ClickEvent; +import net.md_5.bungee.api.chat.ComponentBuilder; +import net.md_5.bungee.api.chat.HoverEvent; +import net.md_5.bungee.api.chat.TextComponent; + +public class EditCommand extends HologramSubCommand { + + private HologramsCommandHandler mainCommandHandler; + + public EditCommand(HologramsCommandHandler mainCommandHandler) { + super("edit"); + setPermission(Strings.BASE_PERM + "edit"); + this.mainCommandHandler = mainCommandHandler; + } + + @Override + public String getPossibleArguments() { + return ""; + } + + @Override + public int getMinimumArguments() { + return 1; + } + + + @Override + public void execute(CommandSender sender, String label, String[] args) throws CommandException { + String name = args[0].toLowerCase(); + NamedHologram hologram = NamedHologramManager.getHologram(name); + CommandValidator.notNull(hologram, Strings.noSuchHologram(name)); + + sender.sendMessage(""); + sender.sendMessage(Strings.formatTitle("How to edit the hologram '" + name + "'")); + for (HologramSubCommand subCommand : mainCommandHandler.getSubCommands()) { + if (subCommand.getType() == SubCommandType.EDIT_LINES) { + String usage = "/" + label + " " + subCommand.getName() + (subCommand.getPossibleArguments().length() > 0 ? " " + subCommand.getPossibleArguments().replace("", hologram.getName()).replace("", hologram.getName()) : ""); + + if (CommandValidator.isPlayerSender(sender)) { + + List help = Utils.newList(); + help.add(Colors.PRIMARY + usage); + for (String tutLine : subCommand.getTutorial()) { + help.add(Colors.SECONDARY_SHADOW + tutLine); + } + + ((Player) sender).spigot().sendMessage(new ComponentBuilder(usage) + .color(ChatColor.AQUA) + .event(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, usage)) + .event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, TextComponent.fromLegacyText(Utils.join(help, "\n")))) + .create()); + + } else { + sender.sendMessage(Colors.PRIMARY + usage); + } + } + } + + if (CommandValidator.isPlayerSender(sender)) { + HelpCommand.sendHoverTip((Player) sender); + } + } + + @Override + public List getTutorial() { + return Arrays.asList("Shows the commands to manipulate an existing hologram."); + } + + @Override + public SubCommandType getType() { + return SubCommandType.GENERIC; + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/HelpCommand.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/HelpCommand.java index bcf1ee6b..f3ec8aeb 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/HelpCommand.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/HelpCommand.java @@ -1,101 +1,115 @@ -package com.gmail.filoghost.holographicdisplays.commands.main.subs; - -import java.util.List; - -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -import com.gmail.filoghost.holographicdisplays.commands.Colors; -import com.gmail.filoghost.holographicdisplays.commands.CommandValidator; -import com.gmail.filoghost.holographicdisplays.commands.Strings; -import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; -import com.gmail.filoghost.holographicdisplays.commands.main.HologramsCommandHandler; -import com.gmail.filoghost.holographicdisplays.exception.CommandException; -import com.gmail.filoghost.holographicdisplays.util.Utils; - -import net.md_5.bungee.api.ChatColor; -import net.md_5.bungee.api.chat.ClickEvent; -import net.md_5.bungee.api.chat.ComponentBuilder; -import net.md_5.bungee.api.chat.HoverEvent; -import net.md_5.bungee.api.chat.TextComponent; -import net.md_5.bungee.api.chat.ComponentBuilder.FormatRetention; - -public class HelpCommand extends HologramSubCommand { - - private HologramsCommandHandler mainCommandHandler; - - public HelpCommand(HologramsCommandHandler mainCommandHandler) { - super("help"); - setPermission(Strings.BASE_PERM + "help"); - this.mainCommandHandler = mainCommandHandler; - } - - @Override - public String getPossibleArguments() { - return ""; - } - - @Override - public int getMinimumArguments() { - return 0; - } - - - @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - sender.sendMessage(""); - sender.sendMessage(Strings.formatTitle("Holographic Displays Commands")); - for (HologramSubCommand subCommand : mainCommandHandler.getSubCommands()) { - if (subCommand.getType() == SubCommandType.GENERIC) { - String usage = "/" + label + " " + subCommand.getName() + (subCommand.getPossibleArguments().length() > 0 ? " " + subCommand.getPossibleArguments() : ""); - - if (CommandValidator.isPlayerSender(sender)) { - - List help = Utils.newList(); - help.add(Colors.PRIMARY + usage); - for (String tutLine : subCommand.getTutorial()) { - help.add(Colors.SECONDARY_SHADOW + tutLine); - } - - ((Player) sender).spigot().sendMessage(new ComponentBuilder(usage) - .color(ChatColor.AQUA) - .event(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, usage)) - .event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, TextComponent.fromLegacyText(Utils.join(help, "\n")))) - .create()); - - } else { - sender.sendMessage(Colors.PRIMARY + usage); - } - } - } - - if (CommandValidator.isPlayerSender(sender)) { - sendHoverTip((Player) sender); - } - } - - public static void sendHoverTip(Player player) { - player.sendMessage(""); - player.spigot().sendMessage(new ComponentBuilder("TIP").color(ChatColor.YELLOW).bold(true) - .append(" Try to ", FormatRetention.NONE).color(ChatColor.GRAY) - .append("hover").color(ChatColor.WHITE).italic(true).underlined(true) - .event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, TextComponent.fromLegacyText(ChatColor.LIGHT_PURPLE + "Hover on the commands to get info about them."))) - .append(" or ", FormatRetention.NONE).color(ChatColor.GRAY) - .append("click").color(ChatColor.WHITE).italic(true).underlined(true) - .event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, TextComponent.fromLegacyText(ChatColor.LIGHT_PURPLE + "Click on the commands to insert them in the chat."))) - .append(" on the commands!", FormatRetention.NONE).color(ChatColor.GRAY) - .create()); - } - - @Override - public List getTutorial() { - return null; - } - - @Override - public SubCommandType getType() { - return SubCommandType.HIDDEN; - } - - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.commands.main.subs; + +import java.util.List; + +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import com.gmail.filoghost.holographicdisplays.commands.Colors; +import com.gmail.filoghost.holographicdisplays.commands.CommandValidator; +import com.gmail.filoghost.holographicdisplays.commands.Strings; +import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; +import com.gmail.filoghost.holographicdisplays.commands.main.HologramsCommandHandler; +import com.gmail.filoghost.holographicdisplays.exception.CommandException; +import com.gmail.filoghost.holographicdisplays.util.Utils; + +import net.md_5.bungee.api.ChatColor; +import net.md_5.bungee.api.chat.ClickEvent; +import net.md_5.bungee.api.chat.ComponentBuilder; +import net.md_5.bungee.api.chat.HoverEvent; +import net.md_5.bungee.api.chat.TextComponent; +import net.md_5.bungee.api.chat.ComponentBuilder.FormatRetention; + +public class HelpCommand extends HologramSubCommand { + + private HologramsCommandHandler mainCommandHandler; + + public HelpCommand(HologramsCommandHandler mainCommandHandler) { + super("help"); + setPermission(Strings.BASE_PERM + "help"); + this.mainCommandHandler = mainCommandHandler; + } + + @Override + public String getPossibleArguments() { + return ""; + } + + @Override + public int getMinimumArguments() { + return 0; + } + + + @Override + public void execute(CommandSender sender, String label, String[] args) throws CommandException { + sender.sendMessage(""); + sender.sendMessage(Strings.formatTitle("Holographic Displays Commands")); + for (HologramSubCommand subCommand : mainCommandHandler.getSubCommands()) { + if (subCommand.getType() == SubCommandType.GENERIC) { + String usage = "/" + label + " " + subCommand.getName() + (subCommand.getPossibleArguments().length() > 0 ? " " + subCommand.getPossibleArguments() : ""); + + if (CommandValidator.isPlayerSender(sender)) { + + List help = Utils.newList(); + help.add(Colors.PRIMARY + usage); + for (String tutLine : subCommand.getTutorial()) { + help.add(Colors.SECONDARY_SHADOW + tutLine); + } + + ((Player) sender).spigot().sendMessage(new ComponentBuilder(usage) + .color(ChatColor.AQUA) + .event(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, usage)) + .event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, TextComponent.fromLegacyText(Utils.join(help, "\n")))) + .create()); + + } else { + sender.sendMessage(Colors.PRIMARY + usage); + } + } + } + + if (CommandValidator.isPlayerSender(sender)) { + sendHoverTip((Player) sender); + } + } + + public static void sendHoverTip(Player player) { + player.sendMessage(""); + player.spigot().sendMessage(new ComponentBuilder("TIP").color(ChatColor.YELLOW).bold(true) + .append(" Try to ", FormatRetention.NONE).color(ChatColor.GRAY) + .append("hover").color(ChatColor.WHITE).italic(true).underlined(true) + .event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, TextComponent.fromLegacyText(ChatColor.LIGHT_PURPLE + "Hover on the commands to get info about them."))) + .append(" or ", FormatRetention.NONE).color(ChatColor.GRAY) + .append("click").color(ChatColor.WHITE).italic(true).underlined(true) + .event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, TextComponent.fromLegacyText(ChatColor.LIGHT_PURPLE + "Click on the commands to insert them in the chat."))) + .append(" on the commands!", FormatRetention.NONE).color(ChatColor.GRAY) + .create()); + } + + @Override + public List getTutorial() { + return null; + } + + @Override + public SubCommandType getType() { + return SubCommandType.HIDDEN; + } + + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/InfoCommand.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/InfoCommand.java index 5c580a8e..02470c3d 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/InfoCommand.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/InfoCommand.java @@ -1,61 +1,75 @@ -package com.gmail.filoghost.holographicdisplays.commands.main.subs; - -import java.util.Arrays; -import java.util.List; - -import org.bukkit.command.CommandSender; -import com.gmail.filoghost.holographicdisplays.commands.Colors; -import com.gmail.filoghost.holographicdisplays.commands.CommandValidator; -import com.gmail.filoghost.holographicdisplays.commands.Strings; -import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; -import com.gmail.filoghost.holographicdisplays.disk.HologramDatabase; -import com.gmail.filoghost.holographicdisplays.exception.CommandException; -import com.gmail.filoghost.holographicdisplays.object.NamedHologram; -import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; -import com.gmail.filoghost.holographicdisplays.object.line.CraftHologramLine; -import com.gmail.filoghost.holographicdisplays.object.line.CraftTextLine; - -public class InfoCommand extends HologramSubCommand { - - public InfoCommand() { - super("info", "details"); - setPermission(Strings.BASE_PERM + "info"); - } - - @Override - public String getPossibleArguments() { - return ""; - } - - @Override - public int getMinimumArguments() { - return 1; - } - - - @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - String name = args[0].toLowerCase(); - NamedHologram hologram = NamedHologramManager.getHologram(name); - CommandValidator.notNull(hologram, Strings.noSuchHologram(name)); - - sender.sendMessage(""); - sender.sendMessage(Strings.formatTitle("Lines of the hologram '" + name + "'")); - int index = 0; - - for (CraftHologramLine line : hologram.getLinesUnsafe()) { - sender.sendMessage(Colors.SECONDARY + Colors.BOLD + (++index) + Colors.SECONDARY_SHADOW + ". " + Colors.SECONDARY + (line instanceof CraftTextLine ? ((CraftTextLine) line).getText() : HologramDatabase.saveLineToString(line))); - } - } - - @Override - public List getTutorial() { - return Arrays.asList("Shows the lines of a hologram."); - } - - @Override - public SubCommandType getType() { - return SubCommandType.EDIT_LINES; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.commands.main.subs; + +import java.util.Arrays; +import java.util.List; + +import org.bukkit.command.CommandSender; +import com.gmail.filoghost.holographicdisplays.commands.Colors; +import com.gmail.filoghost.holographicdisplays.commands.CommandValidator; +import com.gmail.filoghost.holographicdisplays.commands.Strings; +import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; +import com.gmail.filoghost.holographicdisplays.disk.HologramDatabase; +import com.gmail.filoghost.holographicdisplays.exception.CommandException; +import com.gmail.filoghost.holographicdisplays.object.NamedHologram; +import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; +import com.gmail.filoghost.holographicdisplays.object.line.CraftHologramLine; +import com.gmail.filoghost.holographicdisplays.object.line.CraftTextLine; + +public class InfoCommand extends HologramSubCommand { + + public InfoCommand() { + super("info", "details"); + setPermission(Strings.BASE_PERM + "info"); + } + + @Override + public String getPossibleArguments() { + return ""; + } + + @Override + public int getMinimumArguments() { + return 1; + } + + + @Override + public void execute(CommandSender sender, String label, String[] args) throws CommandException { + String name = args[0].toLowerCase(); + NamedHologram hologram = NamedHologramManager.getHologram(name); + CommandValidator.notNull(hologram, Strings.noSuchHologram(name)); + + sender.sendMessage(""); + sender.sendMessage(Strings.formatTitle("Lines of the hologram '" + name + "'")); + int index = 0; + + for (CraftHologramLine line : hologram.getLinesUnsafe()) { + sender.sendMessage(Colors.SECONDARY + Colors.BOLD + (++index) + Colors.SECONDARY_SHADOW + ". " + Colors.SECONDARY + (line instanceof CraftTextLine ? ((CraftTextLine) line).getText() : HologramDatabase.saveLineToString(line))); + } + } + + @Override + public List getTutorial() { + return Arrays.asList("Shows the lines of a hologram."); + } + + @Override + public SubCommandType getType() { + return SubCommandType.EDIT_LINES; + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/InsertlineCommand.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/InsertlineCommand.java index d24eed68..3b711a65 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/InsertlineCommand.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/InsertlineCommand.java @@ -1,78 +1,92 @@ -package com.gmail.filoghost.holographicdisplays.commands.main.subs; - -import java.util.Arrays; -import java.util.List; - -import org.bukkit.Bukkit; -import org.bukkit.command.CommandSender; - -import com.gmail.filoghost.holographicdisplays.commands.Colors; -import com.gmail.filoghost.holographicdisplays.commands.CommandValidator; -import com.gmail.filoghost.holographicdisplays.commands.Strings; -import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; -import com.gmail.filoghost.holographicdisplays.disk.HologramDatabase; -import com.gmail.filoghost.holographicdisplays.event.NamedHologramEditedEvent; -import com.gmail.filoghost.holographicdisplays.exception.CommandException; -import com.gmail.filoghost.holographicdisplays.object.NamedHologram; -import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; -import com.gmail.filoghost.holographicdisplays.util.Utils; - -public class InsertlineCommand extends HologramSubCommand { - - - public InsertlineCommand() { - super("insertline"); - setPermission(Strings.BASE_PERM + "insertline"); - } - - @Override - public String getPossibleArguments() { - return " "; - } - - @Override - public int getMinimumArguments() { - return 3; - } - - - @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - NamedHologram hologram = NamedHologramManager.getHologram(args[0].toLowerCase()); - CommandValidator.notNull(hologram, Strings.noSuchHologram(args[0].toLowerCase())); - - int insertAfter = CommandValidator.getInteger(args[1]); - int oldLinesAmount = hologram.size(); - - CommandValidator.isTrue(insertAfter >= 0 && insertAfter <= oldLinesAmount, "The number must be between 0 and " + hologram.size() + "(amount of lines of the hologram)."); - - hologram.getLinesUnsafe().add(insertAfter, HologramDatabase.readLineFromString(Utils.join(args, " ", 2, args.length), hologram)); - hologram.refreshAll(); - - HologramDatabase.saveHologram(hologram); - HologramDatabase.trySaveToDisk(); - - if (insertAfter == 0) { - sender.sendMessage(Colors.PRIMARY + "Line inserted before line n.1!"); - } else if (insertAfter == oldLinesAmount) { - sender.sendMessage(Colors.PRIMARY + "Line appended at the end!"); - sender.sendMessage(Strings.TIP_PREFIX + "Next time use /" + label + " addline to add a line at the end."); - } else { - sender.sendMessage(Colors.PRIMARY + "Line inserted between lines " + insertAfter + " and " + (insertAfter + 1) + "!"); - } - Bukkit.getPluginManager().callEvent(new NamedHologramEditedEvent(hologram)); - } - - @Override - public List getTutorial() { - return Arrays.asList("Inserts a line after the specified index.", - "If the index is 0, the line will be put before", - "the first line of the hologram."); - } - - @Override - public SubCommandType getType() { - return SubCommandType.EDIT_LINES; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.commands.main.subs; + +import java.util.Arrays; +import java.util.List; + +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; + +import com.gmail.filoghost.holographicdisplays.commands.Colors; +import com.gmail.filoghost.holographicdisplays.commands.CommandValidator; +import com.gmail.filoghost.holographicdisplays.commands.Strings; +import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; +import com.gmail.filoghost.holographicdisplays.disk.HologramDatabase; +import com.gmail.filoghost.holographicdisplays.event.NamedHologramEditedEvent; +import com.gmail.filoghost.holographicdisplays.exception.CommandException; +import com.gmail.filoghost.holographicdisplays.object.NamedHologram; +import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; +import com.gmail.filoghost.holographicdisplays.util.Utils; + +public class InsertlineCommand extends HologramSubCommand { + + + public InsertlineCommand() { + super("insertline"); + setPermission(Strings.BASE_PERM + "insertline"); + } + + @Override + public String getPossibleArguments() { + return " "; + } + + @Override + public int getMinimumArguments() { + return 3; + } + + + @Override + public void execute(CommandSender sender, String label, String[] args) throws CommandException { + NamedHologram hologram = NamedHologramManager.getHologram(args[0].toLowerCase()); + CommandValidator.notNull(hologram, Strings.noSuchHologram(args[0].toLowerCase())); + + int insertAfter = CommandValidator.getInteger(args[1]); + int oldLinesAmount = hologram.size(); + + CommandValidator.isTrue(insertAfter >= 0 && insertAfter <= oldLinesAmount, "The number must be between 0 and " + hologram.size() + "(amount of lines of the hologram)."); + + hologram.getLinesUnsafe().add(insertAfter, HologramDatabase.readLineFromString(Utils.join(args, " ", 2, args.length), hologram)); + hologram.refreshAll(); + + HologramDatabase.saveHologram(hologram); + HologramDatabase.trySaveToDisk(); + + if (insertAfter == 0) { + sender.sendMessage(Colors.PRIMARY + "Line inserted before line n.1!"); + } else if (insertAfter == oldLinesAmount) { + sender.sendMessage(Colors.PRIMARY + "Line appended at the end!"); + sender.sendMessage(Strings.TIP_PREFIX + "Next time use /" + label + " addline to add a line at the end."); + } else { + sender.sendMessage(Colors.PRIMARY + "Line inserted between lines " + insertAfter + " and " + (insertAfter + 1) + "!"); + } + Bukkit.getPluginManager().callEvent(new NamedHologramEditedEvent(hologram)); + } + + @Override + public List getTutorial() { + return Arrays.asList("Inserts a line after the specified index.", + "If the index is 0, the line will be put before", + "the first line of the hologram."); + } + + @Override + public SubCommandType getType() { + return SubCommandType.EDIT_LINES; + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/ListCommand.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/ListCommand.java index cd8779cc..46c9f54c 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/ListCommand.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/ListCommand.java @@ -1,81 +1,95 @@ -package com.gmail.filoghost.holographicdisplays.commands.main.subs; - -import java.util.Arrays; -import java.util.List; - -import org.bukkit.command.CommandSender; - -import com.gmail.filoghost.holographicdisplays.commands.Colors; -import com.gmail.filoghost.holographicdisplays.commands.CommandValidator; -import com.gmail.filoghost.holographicdisplays.commands.Strings; -import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; -import com.gmail.filoghost.holographicdisplays.exception.CommandException; -import com.gmail.filoghost.holographicdisplays.object.NamedHologram; -import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; - -public class ListCommand extends HologramSubCommand { - - private static final int HOLOGRAMS_PER_PAGE = 10; - - public ListCommand() { - super("list"); - setPermission(Strings.BASE_PERM + "list"); - } - - @Override - public String getPossibleArguments() { - return "[page]"; - } - - @Override - public int getMinimumArguments() { - return 0; - } - - @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - - int page = args.length > 0 ? CommandValidator.getInteger(args[0]) : 1; - - if (page < 1) { - throw new CommandException("Page number must be 1 or greater."); - } - - int totalPages = NamedHologramManager.size() / HOLOGRAMS_PER_PAGE; - if (NamedHologramManager.size() % HOLOGRAMS_PER_PAGE != 0) { - totalPages++; - } - - - if (NamedHologramManager.size() == 0) { - throw new CommandException("There are no holograms yet. Create one with /" + label + " create."); - } - - sender.sendMessage(""); - sender.sendMessage(Strings.formatTitle("Holograms list " + Colors.SECONDARY + "(Page " + page + " of " + totalPages + ")")); - int fromIndex = (page - 1) * HOLOGRAMS_PER_PAGE; - int toIndex = fromIndex + HOLOGRAMS_PER_PAGE; - - for (int i = fromIndex; i < toIndex; i++) { - if (i < NamedHologramManager.size()) { - NamedHologram hologram = NamedHologramManager.get(i); - sender.sendMessage(Colors.SECONDARY_SHADOW + "- " + Colors.SECONDARY + Colors.BOLD + hologram.getName() + " " + Colors.SECONDARY_SHADOW + "at x: " + (int) hologram.getX() + ", y: " + (int) hologram.getY() + ", z: " + (int) hologram.getZ() + " (lines: " + hologram.size() + ", world: \"" + hologram.getWorld().getName() + "\")"); - } - } - if (page < totalPages) { - sender.sendMessage(Strings.TIP_PREFIX + "See the next page with /" + label + " list " + (page + 1)); - } - - } - - @Override - public List getTutorial() { - return Arrays.asList("Lists all the existing holograms."); - } - - @Override - public SubCommandType getType() { - return SubCommandType.GENERIC; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.commands.main.subs; + +import java.util.Arrays; +import java.util.List; + +import org.bukkit.command.CommandSender; + +import com.gmail.filoghost.holographicdisplays.commands.Colors; +import com.gmail.filoghost.holographicdisplays.commands.CommandValidator; +import com.gmail.filoghost.holographicdisplays.commands.Strings; +import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; +import com.gmail.filoghost.holographicdisplays.exception.CommandException; +import com.gmail.filoghost.holographicdisplays.object.NamedHologram; +import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; + +public class ListCommand extends HologramSubCommand { + + private static final int HOLOGRAMS_PER_PAGE = 10; + + public ListCommand() { + super("list"); + setPermission(Strings.BASE_PERM + "list"); + } + + @Override + public String getPossibleArguments() { + return "[page]"; + } + + @Override + public int getMinimumArguments() { + return 0; + } + + @Override + public void execute(CommandSender sender, String label, String[] args) throws CommandException { + + int page = args.length > 0 ? CommandValidator.getInteger(args[0]) : 1; + + if (page < 1) { + throw new CommandException("Page number must be 1 or greater."); + } + + int totalPages = NamedHologramManager.size() / HOLOGRAMS_PER_PAGE; + if (NamedHologramManager.size() % HOLOGRAMS_PER_PAGE != 0) { + totalPages++; + } + + + if (NamedHologramManager.size() == 0) { + throw new CommandException("There are no holograms yet. Create one with /" + label + " create."); + } + + sender.sendMessage(""); + sender.sendMessage(Strings.formatTitle("Holograms list " + Colors.SECONDARY + "(Page " + page + " of " + totalPages + ")")); + int fromIndex = (page - 1) * HOLOGRAMS_PER_PAGE; + int toIndex = fromIndex + HOLOGRAMS_PER_PAGE; + + for (int i = fromIndex; i < toIndex; i++) { + if (i < NamedHologramManager.size()) { + NamedHologram hologram = NamedHologramManager.get(i); + sender.sendMessage(Colors.SECONDARY_SHADOW + "- " + Colors.SECONDARY + Colors.BOLD + hologram.getName() + " " + Colors.SECONDARY_SHADOW + "at x: " + (int) hologram.getX() + ", y: " + (int) hologram.getY() + ", z: " + (int) hologram.getZ() + " (lines: " + hologram.size() + ", world: \"" + hologram.getWorld().getName() + "\")"); + } + } + if (page < totalPages) { + sender.sendMessage(Strings.TIP_PREFIX + "See the next page with /" + label + " list " + (page + 1)); + } + + } + + @Override + public List getTutorial() { + return Arrays.asList("Lists all the existing holograms."); + } + + @Override + public SubCommandType getType() { + return SubCommandType.GENERIC; + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/MovehereCommand.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/MovehereCommand.java index 896b00f1..2e4accb0 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/MovehereCommand.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/MovehereCommand.java @@ -1,67 +1,81 @@ -package com.gmail.filoghost.holographicdisplays.commands.main.subs; - -import java.util.Arrays; -import java.util.List; - -import org.bukkit.Location; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; - -import com.gmail.filoghost.holographicdisplays.commands.Colors; -import com.gmail.filoghost.holographicdisplays.commands.CommandValidator; -import com.gmail.filoghost.holographicdisplays.commands.Strings; -import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; -import com.gmail.filoghost.holographicdisplays.disk.HologramDatabase; -import com.gmail.filoghost.holographicdisplays.exception.CommandException; -import com.gmail.filoghost.holographicdisplays.object.NamedHologram; -import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; - -public class MovehereCommand extends HologramSubCommand { - - - public MovehereCommand() { - super("movehere"); - setPermission(Strings.BASE_PERM + "movehere"); - } - - @Override - public String getPossibleArguments() { - return ""; - } - - @Override - public int getMinimumArguments() { - return 1; - } - - - @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - Player player = CommandValidator.getPlayerSender(sender); - NamedHologram hologram = NamedHologramManager.getHologram(args[0].toLowerCase()); - CommandValidator.notNull(hologram, Strings.noSuchHologram(args[0].toLowerCase())); - - hologram.teleport(player.getWorld(), player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ()); - hologram.despawnEntities(); - hologram.refreshAll(); - - HologramDatabase.saveHologram(hologram); - HologramDatabase.trySaveToDisk(); - Location to = player.getLocation(); - to.setPitch(90); - player.teleport(to, TeleportCause.PLUGIN); - player.sendMessage(Colors.PRIMARY + "You moved the hologram '" + hologram.getName() + "' near to you."); - } - - @Override - public List getTutorial() { - return Arrays.asList("Moves a hologram to your location."); - } - - @Override - public SubCommandType getType() { - return SubCommandType.GENERIC; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.commands.main.subs; + +import java.util.Arrays; +import java.util.List; + +import org.bukkit.Location; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; + +import com.gmail.filoghost.holographicdisplays.commands.Colors; +import com.gmail.filoghost.holographicdisplays.commands.CommandValidator; +import com.gmail.filoghost.holographicdisplays.commands.Strings; +import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; +import com.gmail.filoghost.holographicdisplays.disk.HologramDatabase; +import com.gmail.filoghost.holographicdisplays.exception.CommandException; +import com.gmail.filoghost.holographicdisplays.object.NamedHologram; +import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; + +public class MovehereCommand extends HologramSubCommand { + + + public MovehereCommand() { + super("movehere"); + setPermission(Strings.BASE_PERM + "movehere"); + } + + @Override + public String getPossibleArguments() { + return ""; + } + + @Override + public int getMinimumArguments() { + return 1; + } + + + @Override + public void execute(CommandSender sender, String label, String[] args) throws CommandException { + Player player = CommandValidator.getPlayerSender(sender); + NamedHologram hologram = NamedHologramManager.getHologram(args[0].toLowerCase()); + CommandValidator.notNull(hologram, Strings.noSuchHologram(args[0].toLowerCase())); + + hologram.teleport(player.getWorld(), player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ()); + hologram.despawnEntities(); + hologram.refreshAll(); + + HologramDatabase.saveHologram(hologram); + HologramDatabase.trySaveToDisk(); + Location to = player.getLocation(); + to.setPitch(90); + player.teleport(to, TeleportCause.PLUGIN); + player.sendMessage(Colors.PRIMARY + "You moved the hologram '" + hologram.getName() + "' near to you."); + } + + @Override + public List getTutorial() { + return Arrays.asList("Moves a hologram to your location."); + } + + @Override + public SubCommandType getType() { + return SubCommandType.GENERIC; + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/NearCommand.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/NearCommand.java index a386b7a4..4812f39d 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/NearCommand.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/NearCommand.java @@ -1,70 +1,84 @@ -package com.gmail.filoghost.holographicdisplays.commands.main.subs; - -import java.util.Arrays; -import java.util.List; - -import org.bukkit.World; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -import com.gmail.filoghost.holographicdisplays.commands.Colors; -import com.gmail.filoghost.holographicdisplays.commands.CommandValidator; -import com.gmail.filoghost.holographicdisplays.commands.Strings; -import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; -import com.gmail.filoghost.holographicdisplays.exception.CommandException; -import com.gmail.filoghost.holographicdisplays.object.NamedHologram; -import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; -import com.gmail.filoghost.holographicdisplays.util.Utils; - -public class NearCommand extends HologramSubCommand { - - public NearCommand() { - super("near"); - setPermission(Strings.BASE_PERM + "near"); - } - - @Override - public String getPossibleArguments() { - return ""; - } - - @Override - public int getMinimumArguments() { - return 1; - } - - @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - Player player = CommandValidator.getPlayerSender(sender); - int radius = CommandValidator.getInteger(args[0]); - CommandValidator.isTrue(radius > 0, "Radius must be at least 1."); - - World world = player.getWorld(); - int radiusSquared = radius * radius; - List nearHolograms = Utils.newList(); - - for (NamedHologram hologram : NamedHologramManager.getHolograms()) { - if (hologram.getLocation().getWorld().equals(world) && hologram.getLocation().distanceSquared(player.getLocation()) <= radiusSquared) { - nearHolograms.add(hologram); - } - } - - CommandValidator.isTrue(!nearHolograms.isEmpty(), "There are no holograms in the given radius."); - - player.sendMessage(Strings.formatTitle("Near holograms")); - for (NamedHologram nearHologram : nearHolograms) { - player.sendMessage(Colors.SECONDARY_SHADOW + "- " + Colors.SECONDARY + Colors.BOLD + nearHologram.getName() + " " + Colors.SECONDARY_SHADOW + "at x: " + (int) nearHologram.getX() + ", y: " + (int) nearHologram.getY() + ", z: " + (int) nearHologram.getZ() + " (lines: " + nearHologram.size() + ")"); - } - } - - @Override - public List getTutorial() { - return Arrays.asList("Get a list of near holograms."); - } - - @Override - public SubCommandType getType() { - return SubCommandType.GENERIC; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.commands.main.subs; + +import java.util.Arrays; +import java.util.List; + +import org.bukkit.World; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import com.gmail.filoghost.holographicdisplays.commands.Colors; +import com.gmail.filoghost.holographicdisplays.commands.CommandValidator; +import com.gmail.filoghost.holographicdisplays.commands.Strings; +import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; +import com.gmail.filoghost.holographicdisplays.exception.CommandException; +import com.gmail.filoghost.holographicdisplays.object.NamedHologram; +import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; +import com.gmail.filoghost.holographicdisplays.util.Utils; + +public class NearCommand extends HologramSubCommand { + + public NearCommand() { + super("near"); + setPermission(Strings.BASE_PERM + "near"); + } + + @Override + public String getPossibleArguments() { + return ""; + } + + @Override + public int getMinimumArguments() { + return 1; + } + + @Override + public void execute(CommandSender sender, String label, String[] args) throws CommandException { + Player player = CommandValidator.getPlayerSender(sender); + int radius = CommandValidator.getInteger(args[0]); + CommandValidator.isTrue(radius > 0, "Radius must be at least 1."); + + World world = player.getWorld(); + int radiusSquared = radius * radius; + List nearHolograms = Utils.newList(); + + for (NamedHologram hologram : NamedHologramManager.getHolograms()) { + if (hologram.getLocation().getWorld().equals(world) && hologram.getLocation().distanceSquared(player.getLocation()) <= radiusSquared) { + nearHolograms.add(hologram); + } + } + + CommandValidator.isTrue(!nearHolograms.isEmpty(), "There are no holograms in the given radius."); + + player.sendMessage(Strings.formatTitle("Near holograms")); + for (NamedHologram nearHologram : nearHolograms) { + player.sendMessage(Colors.SECONDARY_SHADOW + "- " + Colors.SECONDARY + Colors.BOLD + nearHologram.getName() + " " + Colors.SECONDARY_SHADOW + "at x: " + (int) nearHologram.getX() + ", y: " + (int) nearHologram.getY() + ", z: " + (int) nearHologram.getZ() + " (lines: " + nearHologram.size() + ")"); + } + } + + @Override + public List getTutorial() { + return Arrays.asList("Get a list of near holograms."); + } + + @Override + public SubCommandType getType() { + return SubCommandType.GENERIC; + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/ReadimageCommand.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/ReadimageCommand.java index 7891bd14..58463aae 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/ReadimageCommand.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/ReadimageCommand.java @@ -1,165 +1,179 @@ -package com.gmail.filoghost.holographicdisplays.commands.main.subs; - -import java.awt.image.BufferedImage; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.Arrays; -import java.util.List; - -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.command.CommandSender; - -import com.gmail.filoghost.holographicdisplays.HolographicDisplays; -import com.gmail.filoghost.holographicdisplays.commands.Colors; -import com.gmail.filoghost.holographicdisplays.commands.CommandValidator; -import com.gmail.filoghost.holographicdisplays.commands.Strings; -import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; -import com.gmail.filoghost.holographicdisplays.disk.HologramDatabase; -import com.gmail.filoghost.holographicdisplays.event.NamedHologramEditedEvent; -import com.gmail.filoghost.holographicdisplays.exception.CommandException; -import com.gmail.filoghost.holographicdisplays.exception.TooWideException; -import com.gmail.filoghost.holographicdisplays.exception.UnreadableImageException; -import com.gmail.filoghost.holographicdisplays.image.ImageMessage; -import com.gmail.filoghost.holographicdisplays.object.NamedHologram; -import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; -import com.gmail.filoghost.holographicdisplays.util.FileUtils; -import com.gmail.filoghost.holographicdisplays.util.Utils; - -public class ReadimageCommand extends HologramSubCommand { - - - public ReadimageCommand() { - super("readimage", "image"); - setPermission(Strings.BASE_PERM + "readimage"); - } - - @Override - public String getPossibleArguments() { - return " "; - } - - @Override - public int getMinimumArguments() { - return 3; - } - - - @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - - boolean append = false; - - List newArgs = Utils.newList(); - - for (int i = 0; i < args.length; i++) { - if (args[i].equalsIgnoreCase("-a") || args[i].equalsIgnoreCase("-append")) { - append = true; - } else { - newArgs.add(args[i]); - } - } - - args = newArgs.toArray(new String[0]); - - NamedHologram hologram = NamedHologramManager.getHologram(args[0].toLowerCase()); - CommandValidator.notNull(hologram, Strings.noSuchHologram(args[0].toLowerCase())); - - int width = CommandValidator.getInteger(args[2]); - - CommandValidator.isTrue(width >= 2, "The width of the image must be 2 or greater."); - - boolean isUrl = false; - - try { - String fileName = args[1]; - BufferedImage image = null; - - if (fileName.startsWith("http://") || fileName.startsWith("https://")) { - isUrl = true; - image = FileUtils.readImage(new URL(fileName)); - } else { - - if (fileName.matches(".*[a-zA-Z0-9\\-]+\\.[a-zA-Z0-9\\-]{1,4}\\/.+")) { - Strings.sendWarning(sender, "The image path seems to be an URL. If so, please use http:// or https:// in the path."); - } - - File targetImage = new File(HolographicDisplays.getInstance().getDataFolder(), fileName); - CommandValidator.isTrue(FileUtils.isParentFolder(HolographicDisplays.getInstance().getDataFolder(), targetImage), "The image must be inside HolographicDisplays' folder."); - CommandValidator.isTrue(!HolographicDisplays.isConfigFile(targetImage), "Cannot read default configuration files."); - - image = FileUtils.readImage(targetImage); - } - - if (!append) { - hologram.clearLines(); - } - - ImageMessage imageMessage = new ImageMessage(image, width); - String[] newLines = imageMessage.getLines(); - for (int i = 0; i < newLines.length; i++) { - hologram.appendTextLine(newLines[i]); - } - - hologram.refreshAll(); - - if (newLines.length < 5) { - sender.sendMessage(Strings.TIP_PREFIX + "The image has a very low height. You can increase it by increasing the width, it will scale automatically."); - } - - HologramDatabase.saveHologram(hologram); - HologramDatabase.trySaveToDisk(); - - if (append) { - sender.sendMessage(Colors.PRIMARY + "The image was appended int the end of the hologram!"); - } else { - sender.sendMessage(Colors.PRIMARY + "The image was drawn in the hologram!"); - } - Bukkit.getPluginManager().callEvent(new NamedHologramEditedEvent(hologram)); - - } catch (CommandException e) { - throw e; - } catch (MalformedURLException e) { - throw new CommandException("The provided URL was not valid."); - } catch (TooWideException e) { - throw new CommandException("The image is too large. Max width allowed is " + ImageMessage.MAX_WIDTH + " pixels."); - } catch (UnreadableImageException e) { - throw new CommandException("The plugin was unable to read the image. Be sure that the format is supported."); - } catch (FileNotFoundException e) { - throw new CommandException("The image \"" + args[1] + "\" doesn't exist in the plugin's folder."); - } catch (IOException e) { - e.printStackTrace(); - throw new CommandException("I/O exception while reading the image. " + (isUrl ? "Is the URL valid?" : "Is it in use?")); - } catch (Exception e) { - e.printStackTrace(); - throw new CommandException("Unhandled exception while reading the image! Please look the console."); - } - } - - @Override - public List getTutorial() { - return Arrays.asList("Reads an image from a file. Tutorial:", - "1) Move the image in the plugin's folder", - "2) Do not use spaces in the name", - "3) Do /holograms read ", - "4) Choose to automatically resize the image", - "5) (Optional) Use the flag '-a' if you only want to append", - " the image to the hologram without clearing the lines", - "", - "Example: you have an image named 'logo.png', you want to append", - "it to the lines of the hologram named 'test', with a width of", - "50 pixels. In this case you would execute the following command:", - ChatColor.YELLOW + "/holograms readimage test logo.png 50 -a", - "", - "The symbols used to create the image are taken from the config.yml."); - } - - @Override - public SubCommandType getType() { - return SubCommandType.EDIT_LINES; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.commands.main.subs; + +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Arrays; +import java.util.List; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; + +import com.gmail.filoghost.holographicdisplays.HolographicDisplays; +import com.gmail.filoghost.holographicdisplays.commands.Colors; +import com.gmail.filoghost.holographicdisplays.commands.CommandValidator; +import com.gmail.filoghost.holographicdisplays.commands.Strings; +import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; +import com.gmail.filoghost.holographicdisplays.disk.HologramDatabase; +import com.gmail.filoghost.holographicdisplays.event.NamedHologramEditedEvent; +import com.gmail.filoghost.holographicdisplays.exception.CommandException; +import com.gmail.filoghost.holographicdisplays.exception.TooWideException; +import com.gmail.filoghost.holographicdisplays.exception.UnreadableImageException; +import com.gmail.filoghost.holographicdisplays.image.ImageMessage; +import com.gmail.filoghost.holographicdisplays.object.NamedHologram; +import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; +import com.gmail.filoghost.holographicdisplays.util.FileUtils; +import com.gmail.filoghost.holographicdisplays.util.Utils; + +public class ReadimageCommand extends HologramSubCommand { + + + public ReadimageCommand() { + super("readimage", "image"); + setPermission(Strings.BASE_PERM + "readimage"); + } + + @Override + public String getPossibleArguments() { + return " "; + } + + @Override + public int getMinimumArguments() { + return 3; + } + + + @Override + public void execute(CommandSender sender, String label, String[] args) throws CommandException { + + boolean append = false; + + List newArgs = Utils.newList(); + + for (int i = 0; i < args.length; i++) { + if (args[i].equalsIgnoreCase("-a") || args[i].equalsIgnoreCase("-append")) { + append = true; + } else { + newArgs.add(args[i]); + } + } + + args = newArgs.toArray(new String[0]); + + NamedHologram hologram = NamedHologramManager.getHologram(args[0].toLowerCase()); + CommandValidator.notNull(hologram, Strings.noSuchHologram(args[0].toLowerCase())); + + int width = CommandValidator.getInteger(args[2]); + + CommandValidator.isTrue(width >= 2, "The width of the image must be 2 or greater."); + + boolean isUrl = false; + + try { + String fileName = args[1]; + BufferedImage image = null; + + if (fileName.startsWith("http://") || fileName.startsWith("https://")) { + isUrl = true; + image = FileUtils.readImage(new URL(fileName)); + } else { + + if (fileName.matches(".*[a-zA-Z0-9\\-]+\\.[a-zA-Z0-9\\-]{1,4}\\/.+")) { + Strings.sendWarning(sender, "The image path seems to be an URL. If so, please use http:// or https:// in the path."); + } + + File targetImage = new File(HolographicDisplays.getInstance().getDataFolder(), fileName); + CommandValidator.isTrue(FileUtils.isParentFolder(HolographicDisplays.getInstance().getDataFolder(), targetImage), "The image must be inside HolographicDisplays' folder."); + CommandValidator.isTrue(!HolographicDisplays.isConfigFile(targetImage), "Cannot read default configuration files."); + + image = FileUtils.readImage(targetImage); + } + + if (!append) { + hologram.clearLines(); + } + + ImageMessage imageMessage = new ImageMessage(image, width); + String[] newLines = imageMessage.getLines(); + for (int i = 0; i < newLines.length; i++) { + hologram.appendTextLine(newLines[i]); + } + + hologram.refreshAll(); + + if (newLines.length < 5) { + sender.sendMessage(Strings.TIP_PREFIX + "The image has a very low height. You can increase it by increasing the width, it will scale automatically."); + } + + HologramDatabase.saveHologram(hologram); + HologramDatabase.trySaveToDisk(); + + if (append) { + sender.sendMessage(Colors.PRIMARY + "The image was appended int the end of the hologram!"); + } else { + sender.sendMessage(Colors.PRIMARY + "The image was drawn in the hologram!"); + } + Bukkit.getPluginManager().callEvent(new NamedHologramEditedEvent(hologram)); + + } catch (CommandException e) { + throw e; + } catch (MalformedURLException e) { + throw new CommandException("The provided URL was not valid."); + } catch (TooWideException e) { + throw new CommandException("The image is too large. Max width allowed is " + ImageMessage.MAX_WIDTH + " pixels."); + } catch (UnreadableImageException e) { + throw new CommandException("The plugin was unable to read the image. Be sure that the format is supported."); + } catch (FileNotFoundException e) { + throw new CommandException("The image \"" + args[1] + "\" doesn't exist in the plugin's folder."); + } catch (IOException e) { + e.printStackTrace(); + throw new CommandException("I/O exception while reading the image. " + (isUrl ? "Is the URL valid?" : "Is it in use?")); + } catch (Exception e) { + e.printStackTrace(); + throw new CommandException("Unhandled exception while reading the image! Please look the console."); + } + } + + @Override + public List getTutorial() { + return Arrays.asList("Reads an image from a file. Tutorial:", + "1) Move the image in the plugin's folder", + "2) Do not use spaces in the name", + "3) Do /holograms read ", + "4) Choose to automatically resize the image", + "5) (Optional) Use the flag '-a' if you only want to append", + " the image to the hologram without clearing the lines", + "", + "Example: you have an image named 'logo.png', you want to append", + "it to the lines of the hologram named 'test', with a width of", + "50 pixels. In this case you would execute the following command:", + ChatColor.YELLOW + "/holograms readimage test logo.png 50 -a", + "", + "The symbols used to create the image are taken from the config.yml."); + } + + @Override + public SubCommandType getType() { + return SubCommandType.EDIT_LINES; + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/ReadtextCommand.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/ReadtextCommand.java index 93b4a071..54f4c6ca 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/ReadtextCommand.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/ReadtextCommand.java @@ -1,115 +1,129 @@ -package com.gmail.filoghost.holographicdisplays.commands.main.subs; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.util.Arrays; -import java.util.List; - -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.command.CommandSender; - -import com.gmail.filoghost.holographicdisplays.HolographicDisplays; -import com.gmail.filoghost.holographicdisplays.commands.Colors; -import com.gmail.filoghost.holographicdisplays.commands.CommandValidator; -import com.gmail.filoghost.holographicdisplays.commands.Strings; -import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; -import com.gmail.filoghost.holographicdisplays.disk.HologramDatabase; -import com.gmail.filoghost.holographicdisplays.disk.StringConverter; -import com.gmail.filoghost.holographicdisplays.event.NamedHologramEditedEvent; -import com.gmail.filoghost.holographicdisplays.exception.CommandException; -import com.gmail.filoghost.holographicdisplays.object.NamedHologram; -import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; -import com.gmail.filoghost.holographicdisplays.util.FileUtils; - -public class ReadtextCommand extends HologramSubCommand { - - public ReadtextCommand() { - super("readtext", "readlines"); - setPermission(Strings.BASE_PERM + "readtext"); - } - - @Override - public String getPossibleArguments() { - return " "; - } - - @Override - public int getMinimumArguments() { - return 2; - } - - @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - NamedHologram hologram = NamedHologramManager.getHologram(args[0].toLowerCase()); - CommandValidator.notNull(hologram, Strings.noSuchHologram(args[0].toLowerCase())); - - try { - String fileName = args[1]; - File targetFile = new File(HolographicDisplays.getInstance().getDataFolder(), fileName); - CommandValidator.isTrue(FileUtils.isParentFolder(HolographicDisplays.getInstance().getDataFolder(), targetFile), "The file must be inside HolographicDisplays' folder."); - CommandValidator.isTrue(!HolographicDisplays.isConfigFile(targetFile), "Cannot read default configuration files."); - - List lines = FileUtils.readLines(targetFile); - hologram.clearLines(); - - int linesAmount = lines.size(); - if (linesAmount > 40) { - Strings.sendWarning(sender, "The file contained more than 40 lines, that have been limited."); - linesAmount = 40; - } - - for (int i = 0; i < linesAmount; i++) { - hologram.appendTextLine(StringConverter.toReadableFormat(lines.get(i))); - } - - hologram.refreshAll(); - - HologramDatabase.saveHologram(hologram); - HologramDatabase.trySaveToDisk(); - - if (args[1].contains(".")) { - if (isImageExtension(args[1].substring(args[1].lastIndexOf('.') + 1))) { - Strings.sendWarning(sender, "The read file has an image's extension. If it is an image, you should use /" + label + " readimage."); - } - } - - sender.sendMessage(Colors.PRIMARY + "The lines were pasted into the hologram!"); - Bukkit.getPluginManager().callEvent(new NamedHologramEditedEvent(hologram)); - - } catch (CommandException e) { - throw e; - } catch (FileNotFoundException e) { - throw new CommandException("A file named '" + args[1] + "' doesn't exist in the plugin's folder."); - } catch (IOException e) { - throw new CommandException("I/O exception while reading the file. Is it in use?"); - } catch (Exception e) { - e.printStackTrace(); - throw new CommandException("Unhandled exception while reading the file! Please look the console."); - } - } - - @Override - public List getTutorial() { - return Arrays.asList("Reads the lines from a text file. Tutorial:", - "1) Create a new text file in the plugin's folder", - "2) Do not use spaces in the name", - "3) Each line will be a line in the hologram", - "4) Do /holograms readlines ", - "", - "Example: you have a file named 'info.txt', and you want", - "to paste it in the hologram named 'test'. In this case you", - "would execute "+ ChatColor.YELLOW + "/holograms readlines test info.txt"); - } - - @Override - public SubCommandType getType() { - return SubCommandType.EDIT_LINES; - } - - private boolean isImageExtension(String input) { - return Arrays.asList("jpg", "png", "jpeg", "gif").contains(input.toLowerCase()); - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.commands.main.subs; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; + +import com.gmail.filoghost.holographicdisplays.HolographicDisplays; +import com.gmail.filoghost.holographicdisplays.commands.Colors; +import com.gmail.filoghost.holographicdisplays.commands.CommandValidator; +import com.gmail.filoghost.holographicdisplays.commands.Strings; +import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; +import com.gmail.filoghost.holographicdisplays.disk.HologramDatabase; +import com.gmail.filoghost.holographicdisplays.disk.StringConverter; +import com.gmail.filoghost.holographicdisplays.event.NamedHologramEditedEvent; +import com.gmail.filoghost.holographicdisplays.exception.CommandException; +import com.gmail.filoghost.holographicdisplays.object.NamedHologram; +import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; +import com.gmail.filoghost.holographicdisplays.util.FileUtils; + +public class ReadtextCommand extends HologramSubCommand { + + public ReadtextCommand() { + super("readtext", "readlines"); + setPermission(Strings.BASE_PERM + "readtext"); + } + + @Override + public String getPossibleArguments() { + return " "; + } + + @Override + public int getMinimumArguments() { + return 2; + } + + @Override + public void execute(CommandSender sender, String label, String[] args) throws CommandException { + NamedHologram hologram = NamedHologramManager.getHologram(args[0].toLowerCase()); + CommandValidator.notNull(hologram, Strings.noSuchHologram(args[0].toLowerCase())); + + try { + String fileName = args[1]; + File targetFile = new File(HolographicDisplays.getInstance().getDataFolder(), fileName); + CommandValidator.isTrue(FileUtils.isParentFolder(HolographicDisplays.getInstance().getDataFolder(), targetFile), "The file must be inside HolographicDisplays' folder."); + CommandValidator.isTrue(!HolographicDisplays.isConfigFile(targetFile), "Cannot read default configuration files."); + + List lines = FileUtils.readLines(targetFile); + hologram.clearLines(); + + int linesAmount = lines.size(); + if (linesAmount > 40) { + Strings.sendWarning(sender, "The file contained more than 40 lines, that have been limited."); + linesAmount = 40; + } + + for (int i = 0; i < linesAmount; i++) { + hologram.appendTextLine(StringConverter.toReadableFormat(lines.get(i))); + } + + hologram.refreshAll(); + + HologramDatabase.saveHologram(hologram); + HologramDatabase.trySaveToDisk(); + + if (args[1].contains(".")) { + if (isImageExtension(args[1].substring(args[1].lastIndexOf('.') + 1))) { + Strings.sendWarning(sender, "The read file has an image's extension. If it is an image, you should use /" + label + " readimage."); + } + } + + sender.sendMessage(Colors.PRIMARY + "The lines were pasted into the hologram!"); + Bukkit.getPluginManager().callEvent(new NamedHologramEditedEvent(hologram)); + + } catch (CommandException e) { + throw e; + } catch (FileNotFoundException e) { + throw new CommandException("A file named '" + args[1] + "' doesn't exist in the plugin's folder."); + } catch (IOException e) { + throw new CommandException("I/O exception while reading the file. Is it in use?"); + } catch (Exception e) { + e.printStackTrace(); + throw new CommandException("Unhandled exception while reading the file! Please look the console."); + } + } + + @Override + public List getTutorial() { + return Arrays.asList("Reads the lines from a text file. Tutorial:", + "1) Create a new text file in the plugin's folder", + "2) Do not use spaces in the name", + "3) Each line will be a line in the hologram", + "4) Do /holograms readlines ", + "", + "Example: you have a file named 'info.txt', and you want", + "to paste it in the hologram named 'test'. In this case you", + "would execute "+ ChatColor.YELLOW + "/holograms readlines test info.txt"); + } + + @Override + public SubCommandType getType() { + return SubCommandType.EDIT_LINES; + } + + private boolean isImageExtension(String input) { + return Arrays.asList("jpg", "png", "jpeg", "gif").contains(input.toLowerCase()); + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/ReloadCommand.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/ReloadCommand.java index 396580b7..9685ae26 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/ReloadCommand.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/ReloadCommand.java @@ -1,106 +1,120 @@ -package com.gmail.filoghost.holographicdisplays.commands.main.subs; - -import java.util.Arrays; -import java.util.List; -import java.util.Set; - -import org.bukkit.Bukkit; -import org.bukkit.command.CommandSender; - -import com.gmail.filoghost.holographicdisplays.HolographicDisplays; -import com.gmail.filoghost.holographicdisplays.bridge.bungeecord.BungeeServerTracker; -import com.gmail.filoghost.holographicdisplays.commands.Colors; -import com.gmail.filoghost.holographicdisplays.commands.Strings; -import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; -import com.gmail.filoghost.holographicdisplays.disk.Configuration; -import com.gmail.filoghost.holographicdisplays.disk.HologramDatabase; -import com.gmail.filoghost.holographicdisplays.disk.UnicodeSymbols; -import com.gmail.filoghost.holographicdisplays.event.HolographicDisplaysReloadEvent; -import com.gmail.filoghost.holographicdisplays.exception.CommandException; -import com.gmail.filoghost.holographicdisplays.exception.HologramNotFoundException; -import com.gmail.filoghost.holographicdisplays.exception.InvalidFormatException; -import com.gmail.filoghost.holographicdisplays.exception.WorldNotFoundException; -import com.gmail.filoghost.holographicdisplays.object.CraftHologram; -import com.gmail.filoghost.holographicdisplays.object.NamedHologram; -import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; -import com.gmail.filoghost.holographicdisplays.placeholder.AnimationsRegister; -import com.gmail.filoghost.holographicdisplays.placeholder.PlaceholdersManager; - -public class ReloadCommand extends HologramSubCommand { - - public ReloadCommand() { - super("reload"); - setPermission(Strings.BASE_PERM + "reload"); - } - - @Override - public String getPossibleArguments() { - return ""; - } - - @Override - public int getMinimumArguments() { - return 0; - } - - @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - try { - - long startMillis = System.currentTimeMillis(); - - UnicodeSymbols.load(HolographicDisplays.getInstance()); - Configuration.load(HolographicDisplays.getInstance()); - - BungeeServerTracker.resetTrackedServers(); - BungeeServerTracker.startTask(Configuration.bungeeRefreshSeconds); - - HologramDatabase.loadYamlFile(HolographicDisplays.getInstance()); - AnimationsRegister.loadAnimations(HolographicDisplays.getInstance()); - - PlaceholdersManager.untrackAll(); - NamedHologramManager.clearAll(); - - Set savedHolograms = HologramDatabase.getHolograms(); - if (savedHolograms != null && savedHolograms.size() > 0) { - for (String singleSavedHologram : savedHolograms) { - try { - NamedHologram singleHologramEntity = HologramDatabase.loadHologram(singleSavedHologram); - NamedHologramManager.addHologram(singleHologramEntity); - } catch (HologramNotFoundException e) { - Strings.sendWarning(sender, "Hologram '" + singleSavedHologram + "' not found, skipping it."); - } catch (InvalidFormatException e) { - Strings.sendWarning(sender, "Hologram '" + singleSavedHologram + "' has an invalid location format."); - } catch (WorldNotFoundException e) { - Strings.sendWarning(sender, "Hologram '" + singleSavedHologram + "' was in the world '" + e.getMessage() + "' but it wasn't loaded."); - } - } - } - - for (CraftHologram hologram : NamedHologramManager.getHolograms()) { - hologram.refreshAll(); - } - - long endMillis = System.currentTimeMillis(); - - sender.sendMessage(Colors.PRIMARY + "Configuration reloaded successfully in " + (endMillis - startMillis) + "ms!"); - - } catch (Exception ex) { - ex.printStackTrace(); - throw new CommandException("Exception while reloading the configuration. Please look the console."); - } - - Bukkit.getPluginManager().callEvent(new HolographicDisplaysReloadEvent()); - } - - @Override - public List getTutorial() { - return Arrays.asList("Reloads the holograms from the database."); - } - - @Override - public SubCommandType getType() { - return SubCommandType.GENERIC; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.commands.main.subs; + +import java.util.Arrays; +import java.util.List; +import java.util.Set; + +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; + +import com.gmail.filoghost.holographicdisplays.HolographicDisplays; +import com.gmail.filoghost.holographicdisplays.bridge.bungeecord.BungeeServerTracker; +import com.gmail.filoghost.holographicdisplays.commands.Colors; +import com.gmail.filoghost.holographicdisplays.commands.Strings; +import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; +import com.gmail.filoghost.holographicdisplays.disk.Configuration; +import com.gmail.filoghost.holographicdisplays.disk.HologramDatabase; +import com.gmail.filoghost.holographicdisplays.disk.UnicodeSymbols; +import com.gmail.filoghost.holographicdisplays.event.HolographicDisplaysReloadEvent; +import com.gmail.filoghost.holographicdisplays.exception.CommandException; +import com.gmail.filoghost.holographicdisplays.exception.HologramNotFoundException; +import com.gmail.filoghost.holographicdisplays.exception.InvalidFormatException; +import com.gmail.filoghost.holographicdisplays.exception.WorldNotFoundException; +import com.gmail.filoghost.holographicdisplays.object.CraftHologram; +import com.gmail.filoghost.holographicdisplays.object.NamedHologram; +import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; +import com.gmail.filoghost.holographicdisplays.placeholder.AnimationsRegister; +import com.gmail.filoghost.holographicdisplays.placeholder.PlaceholdersManager; + +public class ReloadCommand extends HologramSubCommand { + + public ReloadCommand() { + super("reload"); + setPermission(Strings.BASE_PERM + "reload"); + } + + @Override + public String getPossibleArguments() { + return ""; + } + + @Override + public int getMinimumArguments() { + return 0; + } + + @Override + public void execute(CommandSender sender, String label, String[] args) throws CommandException { + try { + + long startMillis = System.currentTimeMillis(); + + UnicodeSymbols.load(HolographicDisplays.getInstance()); + Configuration.load(HolographicDisplays.getInstance()); + + BungeeServerTracker.resetTrackedServers(); + BungeeServerTracker.startTask(Configuration.bungeeRefreshSeconds); + + HologramDatabase.loadYamlFile(HolographicDisplays.getInstance()); + AnimationsRegister.loadAnimations(HolographicDisplays.getInstance()); + + PlaceholdersManager.untrackAll(); + NamedHologramManager.clearAll(); + + Set savedHolograms = HologramDatabase.getHolograms(); + if (savedHolograms != null && savedHolograms.size() > 0) { + for (String singleSavedHologram : savedHolograms) { + try { + NamedHologram singleHologramEntity = HologramDatabase.loadHologram(singleSavedHologram); + NamedHologramManager.addHologram(singleHologramEntity); + } catch (HologramNotFoundException e) { + Strings.sendWarning(sender, "Hologram '" + singleSavedHologram + "' not found, skipping it."); + } catch (InvalidFormatException e) { + Strings.sendWarning(sender, "Hologram '" + singleSavedHologram + "' has an invalid location format."); + } catch (WorldNotFoundException e) { + Strings.sendWarning(sender, "Hologram '" + singleSavedHologram + "' was in the world '" + e.getMessage() + "' but it wasn't loaded."); + } + } + } + + for (CraftHologram hologram : NamedHologramManager.getHolograms()) { + hologram.refreshAll(); + } + + long endMillis = System.currentTimeMillis(); + + sender.sendMessage(Colors.PRIMARY + "Configuration reloaded successfully in " + (endMillis - startMillis) + "ms!"); + + } catch (Exception ex) { + ex.printStackTrace(); + throw new CommandException("Exception while reloading the configuration. Please look the console."); + } + + Bukkit.getPluginManager().callEvent(new HolographicDisplaysReloadEvent()); + } + + @Override + public List getTutorial() { + return Arrays.asList("Reloads the holograms from the database."); + } + + @Override + public SubCommandType getType() { + return SubCommandType.GENERIC; + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/RemovelineCommand.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/RemovelineCommand.java index b15f8dc5..11195b43 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/RemovelineCommand.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/RemovelineCommand.java @@ -1,68 +1,82 @@ -package com.gmail.filoghost.holographicdisplays.commands.main.subs; - -import java.util.Arrays; -import java.util.List; - -import org.bukkit.Bukkit; -import org.bukkit.command.CommandSender; - -import com.gmail.filoghost.holographicdisplays.commands.Colors; -import com.gmail.filoghost.holographicdisplays.commands.CommandValidator; -import com.gmail.filoghost.holographicdisplays.commands.Strings; -import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; -import com.gmail.filoghost.holographicdisplays.disk.HologramDatabase; -import com.gmail.filoghost.holographicdisplays.event.NamedHologramEditedEvent; -import com.gmail.filoghost.holographicdisplays.exception.CommandException; -import com.gmail.filoghost.holographicdisplays.object.NamedHologram; -import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; - -public class RemovelineCommand extends HologramSubCommand { - - public RemovelineCommand() { - super("removeline"); - setPermission(Strings.BASE_PERM + "removeline"); - } - - @Override - public String getPossibleArguments() { - return " "; - } - - @Override - public int getMinimumArguments() { - return 2; - } - - - @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - NamedHologram hologram = NamedHologramManager.getHologram(args[0].toLowerCase()); - CommandValidator.notNull(hologram, Strings.noSuchHologram(args[0].toLowerCase())); - - int lineNumber = CommandValidator.getInteger(args[1]); - - CommandValidator.isTrue(lineNumber >= 1 && lineNumber <= hologram.size(), "The line number must be between 1 and " + hologram.size() + "."); - int index = lineNumber - 1; - - CommandValidator.isTrue(hologram.size() > 1, "The hologram should have at least 1 line. If you want to delete it, use /" + label + " delete."); - - hologram.removeLine(index); - hologram.refreshAll(); - - HologramDatabase.saveHologram(hologram); - HologramDatabase.trySaveToDisk(); - sender.sendMessage(Colors.PRIMARY + "Line " + lineNumber + " removed!"); - Bukkit.getPluginManager().callEvent(new NamedHologramEditedEvent(hologram)); - } - - @Override - public List getTutorial() { - return Arrays.asList("Removes a line from a hologram."); - } - - @Override - public SubCommandType getType() { - return SubCommandType.EDIT_LINES; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.commands.main.subs; + +import java.util.Arrays; +import java.util.List; + +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; + +import com.gmail.filoghost.holographicdisplays.commands.Colors; +import com.gmail.filoghost.holographicdisplays.commands.CommandValidator; +import com.gmail.filoghost.holographicdisplays.commands.Strings; +import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; +import com.gmail.filoghost.holographicdisplays.disk.HologramDatabase; +import com.gmail.filoghost.holographicdisplays.event.NamedHologramEditedEvent; +import com.gmail.filoghost.holographicdisplays.exception.CommandException; +import com.gmail.filoghost.holographicdisplays.object.NamedHologram; +import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; + +public class RemovelineCommand extends HologramSubCommand { + + public RemovelineCommand() { + super("removeline"); + setPermission(Strings.BASE_PERM + "removeline"); + } + + @Override + public String getPossibleArguments() { + return " "; + } + + @Override + public int getMinimumArguments() { + return 2; + } + + + @Override + public void execute(CommandSender sender, String label, String[] args) throws CommandException { + NamedHologram hologram = NamedHologramManager.getHologram(args[0].toLowerCase()); + CommandValidator.notNull(hologram, Strings.noSuchHologram(args[0].toLowerCase())); + + int lineNumber = CommandValidator.getInteger(args[1]); + + CommandValidator.isTrue(lineNumber >= 1 && lineNumber <= hologram.size(), "The line number must be between 1 and " + hologram.size() + "."); + int index = lineNumber - 1; + + CommandValidator.isTrue(hologram.size() > 1, "The hologram should have at least 1 line. If you want to delete it, use /" + label + " delete."); + + hologram.removeLine(index); + hologram.refreshAll(); + + HologramDatabase.saveHologram(hologram); + HologramDatabase.trySaveToDisk(); + sender.sendMessage(Colors.PRIMARY + "Line " + lineNumber + " removed!"); + Bukkit.getPluginManager().callEvent(new NamedHologramEditedEvent(hologram)); + } + + @Override + public List getTutorial() { + return Arrays.asList("Removes a line from a hologram."); + } + + @Override + public SubCommandType getType() { + return SubCommandType.EDIT_LINES; + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/SetlineCommand.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/SetlineCommand.java index 81699c31..b873184f 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/SetlineCommand.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/SetlineCommand.java @@ -1,83 +1,97 @@ -package com.gmail.filoghost.holographicdisplays.commands.main.subs; - -import java.util.Arrays; -import java.util.List; - -import org.bukkit.Bukkit; -import org.bukkit.Material; -import org.bukkit.command.CommandSender; - -import com.gmail.filoghost.holographicdisplays.commands.Colors; -import com.gmail.filoghost.holographicdisplays.commands.CommandValidator; -import com.gmail.filoghost.holographicdisplays.commands.Strings; -import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; -import com.gmail.filoghost.holographicdisplays.disk.HologramDatabase; -import com.gmail.filoghost.holographicdisplays.event.NamedHologramEditedEvent; -import com.gmail.filoghost.holographicdisplays.exception.CommandException; -import com.gmail.filoghost.holographicdisplays.object.NamedHologram; -import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; -import com.gmail.filoghost.holographicdisplays.util.ItemUtils; -import com.gmail.filoghost.holographicdisplays.util.Utils; - -public class SetlineCommand extends HologramSubCommand { - - public SetlineCommand() { - super("setline"); - setPermission(Strings.BASE_PERM + "setline"); - } - - @Override - public String getPossibleArguments() { - return " "; - } - - @Override - public int getMinimumArguments() { - return 3; - } - - - @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - NamedHologram hologram = NamedHologramManager.getHologram(args[0].toLowerCase()); - CommandValidator.notNull(hologram, Strings.noSuchHologram(args[0].toLowerCase())); - String line = Utils.join(args, " ", 2, args.length); - - // Check material validity - if (line.toLowerCase().startsWith("icon:")) { - String iconMaterial = ItemUtils.stripSpacingChars(line.substring("icon:".length(), line.length())); - - if (iconMaterial.contains(":")) { - iconMaterial = iconMaterial.split(":")[0]; - } - - Material mat = ItemUtils.matchMaterial(iconMaterial); - CommandValidator.notNull(mat, "Invalid icon material."); - } - - int lineNumber = CommandValidator.getInteger(args[1]); - CommandValidator.isTrue(lineNumber >= 1 && lineNumber <= hologram.size(), "The line number must be between 1 and " + hologram.size() + "."); - int index = lineNumber - 1; - - hologram.getLinesUnsafe().get(index).despawn(); - hologram.getLinesUnsafe().set(index, HologramDatabase.readLineFromString(line, hologram)); - hologram.refreshAll(); - - HologramDatabase.saveHologram(hologram); - HologramDatabase.trySaveToDisk(); - sender.sendMessage(Colors.PRIMARY + "Line " + lineNumber + " changed!"); - Bukkit.getPluginManager().callEvent(new NamedHologramEditedEvent(hologram)); - - } - - @Override - public List getTutorial() { - return Arrays.asList("Changes a line of a hologram."); - } - - @Override - public SubCommandType getType() { - return SubCommandType.EDIT_LINES; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.commands.main.subs; + +import java.util.Arrays; +import java.util.List; + +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.command.CommandSender; + +import com.gmail.filoghost.holographicdisplays.commands.Colors; +import com.gmail.filoghost.holographicdisplays.commands.CommandValidator; +import com.gmail.filoghost.holographicdisplays.commands.Strings; +import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; +import com.gmail.filoghost.holographicdisplays.disk.HologramDatabase; +import com.gmail.filoghost.holographicdisplays.event.NamedHologramEditedEvent; +import com.gmail.filoghost.holographicdisplays.exception.CommandException; +import com.gmail.filoghost.holographicdisplays.object.NamedHologram; +import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; +import com.gmail.filoghost.holographicdisplays.util.ItemUtils; +import com.gmail.filoghost.holographicdisplays.util.Utils; + +public class SetlineCommand extends HologramSubCommand { + + public SetlineCommand() { + super("setline"); + setPermission(Strings.BASE_PERM + "setline"); + } + + @Override + public String getPossibleArguments() { + return " "; + } + + @Override + public int getMinimumArguments() { + return 3; + } + + + @Override + public void execute(CommandSender sender, String label, String[] args) throws CommandException { + NamedHologram hologram = NamedHologramManager.getHologram(args[0].toLowerCase()); + CommandValidator.notNull(hologram, Strings.noSuchHologram(args[0].toLowerCase())); + String line = Utils.join(args, " ", 2, args.length); + + // Check material validity + if (line.toLowerCase().startsWith("icon:")) { + String iconMaterial = ItemUtils.stripSpacingChars(line.substring("icon:".length(), line.length())); + + if (iconMaterial.contains(":")) { + iconMaterial = iconMaterial.split(":")[0]; + } + + Material mat = ItemUtils.matchMaterial(iconMaterial); + CommandValidator.notNull(mat, "Invalid icon material."); + } + + int lineNumber = CommandValidator.getInteger(args[1]); + CommandValidator.isTrue(lineNumber >= 1 && lineNumber <= hologram.size(), "The line number must be between 1 and " + hologram.size() + "."); + int index = lineNumber - 1; + + hologram.getLinesUnsafe().get(index).despawn(); + hologram.getLinesUnsafe().set(index, HologramDatabase.readLineFromString(line, hologram)); + hologram.refreshAll(); + + HologramDatabase.saveHologram(hologram); + HologramDatabase.trySaveToDisk(); + sender.sendMessage(Colors.PRIMARY + "Line " + lineNumber + " changed!"); + Bukkit.getPluginManager().callEvent(new NamedHologramEditedEvent(hologram)); + + } + + @Override + public List getTutorial() { + return Arrays.asList("Changes a line of a hologram."); + } + + @Override + public SubCommandType getType() { + return SubCommandType.EDIT_LINES; + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/TeleportCommand.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/TeleportCommand.java index abba403e..b4f76f7d 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/TeleportCommand.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/commands/main/subs/TeleportCommand.java @@ -1,60 +1,74 @@ -package com.gmail.filoghost.holographicdisplays.commands.main.subs; - -import java.util.Arrays; -import java.util.List; - -import org.bukkit.Location; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; - -import com.gmail.filoghost.holographicdisplays.commands.Colors; -import com.gmail.filoghost.holographicdisplays.commands.CommandValidator; -import com.gmail.filoghost.holographicdisplays.commands.Strings; -import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; -import com.gmail.filoghost.holographicdisplays.exception.CommandException; -import com.gmail.filoghost.holographicdisplays.object.NamedHologram; -import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; - - -public class TeleportCommand extends HologramSubCommand { - - public TeleportCommand() { - super("teleport", "tp"); - setPermission(Strings.BASE_PERM + "teleport"); - } - - @Override - public String getPossibleArguments() { - return ""; - } - - @Override - public int getMinimumArguments() { - return 1; - } - - @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - Player player = CommandValidator.getPlayerSender(sender); - NamedHologram hologram = NamedHologramManager.getHologram(args[0].toLowerCase()); - CommandValidator.notNull(hologram, Strings.noSuchHologram(args[0].toLowerCase())); - - Location loc = hologram.getLocation(); - loc.setPitch(90); - player.teleport(loc, TeleportCause.PLUGIN); - player.sendMessage(Colors.PRIMARY + "You were teleported to the hologram named '" + hologram.getName() + "'."); - - } - - @Override - public List getTutorial() { - return Arrays.asList("Teleports you to the given hologram."); - } - - @Override - public SubCommandType getType() { - return SubCommandType.GENERIC; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.commands.main.subs; + +import java.util.Arrays; +import java.util.List; + +import org.bukkit.Location; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; + +import com.gmail.filoghost.holographicdisplays.commands.Colors; +import com.gmail.filoghost.holographicdisplays.commands.CommandValidator; +import com.gmail.filoghost.holographicdisplays.commands.Strings; +import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand; +import com.gmail.filoghost.holographicdisplays.exception.CommandException; +import com.gmail.filoghost.holographicdisplays.object.NamedHologram; +import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; + + +public class TeleportCommand extends HologramSubCommand { + + public TeleportCommand() { + super("teleport", "tp"); + setPermission(Strings.BASE_PERM + "teleport"); + } + + @Override + public String getPossibleArguments() { + return ""; + } + + @Override + public int getMinimumArguments() { + return 1; + } + + @Override + public void execute(CommandSender sender, String label, String[] args) throws CommandException { + Player player = CommandValidator.getPlayerSender(sender); + NamedHologram hologram = NamedHologramManager.getHologram(args[0].toLowerCase()); + CommandValidator.notNull(hologram, Strings.noSuchHologram(args[0].toLowerCase())); + + Location loc = hologram.getLocation(); + loc.setPitch(90); + player.teleport(loc, TeleportCause.PLUGIN); + player.sendMessage(Colors.PRIMARY + "You were teleported to the hologram named '" + hologram.getName() + "'."); + + } + + @Override + public List getTutorial() { + return Arrays.asList("Teleports you to the given hologram."); + } + + @Override + public SubCommandType getType() { + return SubCommandType.GENERIC; + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/disk/HologramDatabase.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/disk/HologramDatabase.java index 7b9f5331..11bbd29b 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/disk/HologramDatabase.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/disk/HologramDatabase.java @@ -1,154 +1,168 @@ -package com.gmail.filoghost.holographicdisplays.disk; - -import java.io.File; -import java.io.IOException; -import java.util.List; -import java.util.Set; -import java.util.logging.Level; - -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.inventory.ItemStack; -import org.bukkit.plugin.Plugin; - -import com.gmail.filoghost.holographicdisplays.exception.HologramNotFoundException; -import com.gmail.filoghost.holographicdisplays.exception.InvalidFormatException; -import com.gmail.filoghost.holographicdisplays.exception.WorldNotFoundException; -import com.gmail.filoghost.holographicdisplays.object.CraftHologram; -import com.gmail.filoghost.holographicdisplays.object.NamedHologram; -import com.gmail.filoghost.holographicdisplays.object.line.CraftHologramLine; -import com.gmail.filoghost.holographicdisplays.object.line.CraftItemLine; -import com.gmail.filoghost.holographicdisplays.object.line.CraftTextLine; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.ItemUtils; -import com.gmail.filoghost.holographicdisplays.util.Utils; - -public class HologramDatabase { - - private static File file; - private static FileConfiguration config; - - public static void loadYamlFile(Plugin plugin) { - file = new File(plugin.getDataFolder(), "database.yml"); - - if (!file.exists()) { - plugin.getDataFolder().mkdirs(); - plugin.saveResource("database.yml", true); - } - - config = YamlConfiguration.loadConfiguration(file); - } - - public static NamedHologram loadHologram(String name) throws HologramNotFoundException, InvalidFormatException, WorldNotFoundException { - - ConfigurationSection configSection = config.getConfigurationSection(name); - - if (configSection == null) { - throw new HologramNotFoundException(); - } - - List lines = configSection.getStringList("lines"); - String locationString = configSection.getString("location"); - - if (lines == null || locationString == null || lines.size() == 0) { - throw new HologramNotFoundException(); - } - - Location loc = LocationSerializer.locationFromString(locationString); - - NamedHologram hologram = new NamedHologram(loc, name); - for (int i = 0; i < lines.size(); i++) { - hologram.getLinesUnsafe().add(readLineFromString(lines.get(i), hologram)); - } - - return hologram; - } - - public static CraftHologramLine readLineFromString(String rawText, CraftHologram hologram) { - if (rawText.toLowerCase().startsWith("icon:")) { - String iconMaterial = ItemUtils.stripSpacingChars(rawText.substring("icon:".length(), rawText.length())); - - short dataValue = 0; - - if (iconMaterial.contains(":")) { - try { - dataValue = (short) Integer.parseInt(iconMaterial.split(":")[1]); - } catch (NumberFormatException e) { } - iconMaterial = iconMaterial.split(":")[0]; - } - - Material mat = ItemUtils.matchMaterial(iconMaterial); - if (mat == null) { - mat = Material.BEDROCK; - } - - return new CraftItemLine(hologram, new ItemStack(mat, 1, dataValue)); - - } else { - - if (rawText.trim().equalsIgnoreCase("{empty}")) { - return new CraftTextLine(hologram, ""); - } else { - return new CraftTextLine(hologram, StringConverter.toReadableFormat(rawText)); - } - } - } - - public static String saveLineToString(CraftHologramLine line) { - if (line instanceof CraftTextLine) { - return StringConverter.toSaveableFormat(((CraftTextLine) line).getText()); - - } else if (line instanceof CraftItemLine) { - CraftItemLine itemLine = (CraftItemLine) line; - return "ICON: " + itemLine.getItemStack().getType().toString().replace("_", " ").toLowerCase() + (itemLine.getItemStack().getDurability() != 0 ? ":" + itemLine.getItemStack().getDurability() : ""); - } else { - - return "Unknown"; - } - } - - public static void deleteHologram(String name) { - config.set(name, null); - } - - public static void saveHologram(NamedHologram hologram) { - - ConfigurationSection configSection = config.isConfigurationSection(hologram.getName()) ? config.getConfigurationSection(hologram.getName()) : config.createSection(hologram.getName()); - - configSection.set("location", LocationSerializer.locationToString(hologram.getLocation())); - List lines = Utils.newList(); - - for (CraftHologramLine line : hologram.getLinesUnsafe()) { - - lines.add(saveLineToString(line)); - } - - configSection.set("lines", lines); - } - - public static Set getHolograms() { - return config.getKeys(false); - } - - public static boolean isExistingHologram(String name) { - return config.isConfigurationSection(name); - } - - public static void saveToDisk() throws IOException { - if (config != null && file != null) { - config.save(file); - } - } - - public static void trySaveToDisk() { - try { - saveToDisk(); - } catch (IOException ex) { - ex.printStackTrace(); - ConsoleLogger.log(Level.SEVERE, "Unable to save database.yml to disk!"); - } - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.disk; + +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.Set; +import java.util.logging.Level; + +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.inventory.ItemStack; +import org.bukkit.plugin.Plugin; + +import com.gmail.filoghost.holographicdisplays.exception.HologramNotFoundException; +import com.gmail.filoghost.holographicdisplays.exception.InvalidFormatException; +import com.gmail.filoghost.holographicdisplays.exception.WorldNotFoundException; +import com.gmail.filoghost.holographicdisplays.object.CraftHologram; +import com.gmail.filoghost.holographicdisplays.object.NamedHologram; +import com.gmail.filoghost.holographicdisplays.object.line.CraftHologramLine; +import com.gmail.filoghost.holographicdisplays.object.line.CraftItemLine; +import com.gmail.filoghost.holographicdisplays.object.line.CraftTextLine; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.ItemUtils; +import com.gmail.filoghost.holographicdisplays.util.Utils; + +public class HologramDatabase { + + private static File file; + private static FileConfiguration config; + + public static void loadYamlFile(Plugin plugin) { + file = new File(plugin.getDataFolder(), "database.yml"); + + if (!file.exists()) { + plugin.getDataFolder().mkdirs(); + plugin.saveResource("database.yml", true); + } + + config = YamlConfiguration.loadConfiguration(file); + } + + public static NamedHologram loadHologram(String name) throws HologramNotFoundException, InvalidFormatException, WorldNotFoundException { + + ConfigurationSection configSection = config.getConfigurationSection(name); + + if (configSection == null) { + throw new HologramNotFoundException(); + } + + List lines = configSection.getStringList("lines"); + String locationString = configSection.getString("location"); + + if (lines == null || locationString == null || lines.size() == 0) { + throw new HologramNotFoundException(); + } + + Location loc = LocationSerializer.locationFromString(locationString); + + NamedHologram hologram = new NamedHologram(loc, name); + for (int i = 0; i < lines.size(); i++) { + hologram.getLinesUnsafe().add(readLineFromString(lines.get(i), hologram)); + } + + return hologram; + } + + public static CraftHologramLine readLineFromString(String rawText, CraftHologram hologram) { + if (rawText.toLowerCase().startsWith("icon:")) { + String iconMaterial = ItemUtils.stripSpacingChars(rawText.substring("icon:".length(), rawText.length())); + + short dataValue = 0; + + if (iconMaterial.contains(":")) { + try { + dataValue = (short) Integer.parseInt(iconMaterial.split(":")[1]); + } catch (NumberFormatException e) { } + iconMaterial = iconMaterial.split(":")[0]; + } + + Material mat = ItemUtils.matchMaterial(iconMaterial); + if (mat == null) { + mat = Material.BEDROCK; + } + + return new CraftItemLine(hologram, new ItemStack(mat, 1, dataValue)); + + } else { + + if (rawText.trim().equalsIgnoreCase("{empty}")) { + return new CraftTextLine(hologram, ""); + } else { + return new CraftTextLine(hologram, StringConverter.toReadableFormat(rawText)); + } + } + } + + public static String saveLineToString(CraftHologramLine line) { + if (line instanceof CraftTextLine) { + return StringConverter.toSaveableFormat(((CraftTextLine) line).getText()); + + } else if (line instanceof CraftItemLine) { + CraftItemLine itemLine = (CraftItemLine) line; + return "ICON: " + itemLine.getItemStack().getType().toString().replace("_", " ").toLowerCase() + (itemLine.getItemStack().getDurability() != 0 ? ":" + itemLine.getItemStack().getDurability() : ""); + } else { + + return "Unknown"; + } + } + + public static void deleteHologram(String name) { + config.set(name, null); + } + + public static void saveHologram(NamedHologram hologram) { + + ConfigurationSection configSection = config.isConfigurationSection(hologram.getName()) ? config.getConfigurationSection(hologram.getName()) : config.createSection(hologram.getName()); + + configSection.set("location", LocationSerializer.locationToString(hologram.getLocation())); + List lines = Utils.newList(); + + for (CraftHologramLine line : hologram.getLinesUnsafe()) { + + lines.add(saveLineToString(line)); + } + + configSection.set("lines", lines); + } + + public static Set getHolograms() { + return config.getKeys(false); + } + + public static boolean isExistingHologram(String name) { + return config.isConfigurationSection(name); + } + + public static void saveToDisk() throws IOException { + if (config != null && file != null) { + config.save(file); + } + } + + public static void trySaveToDisk() { + try { + saveToDisk(); + } catch (IOException ex) { + ex.printStackTrace(); + ConsoleLogger.log(Level.SEVERE, "Unable to save database.yml to disk!"); + } + } +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/event/HolographicDisplaysReloadEvent.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/event/HolographicDisplaysReloadEvent.java index a3b16c69..642a10e1 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/event/HolographicDisplaysReloadEvent.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/event/HolographicDisplaysReloadEvent.java @@ -1,18 +1,32 @@ -package com.gmail.filoghost.holographicdisplays.event; - -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -public class HolographicDisplaysReloadEvent extends Event { - - private static final HandlerList handlers = new HandlerList(); - - @Override - public HandlerList getHandlers() { - return handlers; - } - - public static HandlerList getHandlerList() { - return handlers; - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.event; + +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +public class HolographicDisplaysReloadEvent extends Event { + + private static final HandlerList handlers = new HandlerList(); + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/event/NamedHologramEditedEvent.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/event/NamedHologramEditedEvent.java index 6b857724..0bcfabb1 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/event/NamedHologramEditedEvent.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/event/NamedHologramEditedEvent.java @@ -1,30 +1,44 @@ -package com.gmail.filoghost.holographicdisplays.event; - -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -import com.gmail.filoghost.holographicdisplays.object.NamedHologram; - -public class NamedHologramEditedEvent extends Event { - - private static final HandlerList handlers = new HandlerList(); - - private NamedHologram namedHologram; - - public NamedHologramEditedEvent(NamedHologram namedHologram) { - this.namedHologram = namedHologram; - } - - public NamedHologram getNamedHologram() { - return namedHologram; - } - - @Override - public HandlerList getHandlers() { - return handlers; - } - - public static HandlerList getHandlerList() { - return handlers; - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.event; + +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +import com.gmail.filoghost.holographicdisplays.object.NamedHologram; + +public class NamedHologramEditedEvent extends Event { + + private static final HandlerList handlers = new HandlerList(); + + private NamedHologram namedHologram; + + public NamedHologramEditedEvent(NamedHologram namedHologram) { + this.namedHologram = namedHologram; + } + + public NamedHologram getNamedHologram() { + return namedHologram; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/exception/CommandException.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/exception/CommandException.java index 9a11c47c..9f6b7592 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/exception/CommandException.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/exception/CommandException.java @@ -1,10 +1,24 @@ -package com.gmail.filoghost.holographicdisplays.exception; - -public class CommandException extends Exception { - - private static final long serialVersionUID = 1L; - - public CommandException(String message) { - super(message); - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.exception; + +public class CommandException extends Exception { + + private static final long serialVersionUID = 1L; + + public CommandException(String message) { + super(message); + } +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/exception/HologramNotFoundException.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/exception/HologramNotFoundException.java index 12ad0ed5..1bd24395 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/exception/HologramNotFoundException.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/exception/HologramNotFoundException.java @@ -1,7 +1,21 @@ -package com.gmail.filoghost.holographicdisplays.exception; - -public class HologramNotFoundException extends Exception { - - private static final long serialVersionUID = 1L; - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.exception; + +public class HologramNotFoundException extends Exception { + + private static final long serialVersionUID = 1L; + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/exception/InvalidCharactersException.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/exception/InvalidCharactersException.java index 748ffa09..4611529a 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/exception/InvalidCharactersException.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/exception/InvalidCharactersException.java @@ -1,10 +1,24 @@ -package com.gmail.filoghost.holographicdisplays.exception; - -public class InvalidCharactersException extends Exception { - - private static final long serialVersionUID = 1L; - - public InvalidCharactersException(String message) { - super(message); - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.exception; + +public class InvalidCharactersException extends Exception { + + private static final long serialVersionUID = 1L; + + public InvalidCharactersException(String message) { + super(message); + } +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/exception/InvalidMaterialException.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/exception/InvalidMaterialException.java index 059f1c06..a04c1225 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/exception/InvalidMaterialException.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/exception/InvalidMaterialException.java @@ -1,11 +1,25 @@ -package com.gmail.filoghost.holographicdisplays.exception; - -public class InvalidMaterialException extends Exception { - - private static final long serialVersionUID = 1L; - - public InvalidMaterialException(String message) { - super(message); - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.exception; + +public class InvalidMaterialException extends Exception { + + private static final long serialVersionUID = 1L; + + public InvalidMaterialException(String message) { + super(message); + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/exception/SpawnFailedException.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/exception/SpawnFailedException.java index cdccdbf4..85fd106b 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/exception/SpawnFailedException.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/exception/SpawnFailedException.java @@ -1,7 +1,21 @@ -package com.gmail.filoghost.holographicdisplays.exception; - -public class SpawnFailedException extends Exception { - - private static final long serialVersionUID = 1L; - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.exception; + +public class SpawnFailedException extends Exception { + + private static final long serialVersionUID = 1L; + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/exception/TooWideException.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/exception/TooWideException.java index 6187e83b..3ce4983f 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/exception/TooWideException.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/exception/TooWideException.java @@ -1,17 +1,31 @@ -package com.gmail.filoghost.holographicdisplays.exception; - -public class TooWideException extends Exception { - - private static final long serialVersionUID = 1L; - - private int width; - - public TooWideException(int width) { - this.width = width; - } - - public int getWidth() { - return width; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.exception; + +public class TooWideException extends Exception { + + private static final long serialVersionUID = 1L; + + private int width; + + public TooWideException(int width) { + this.width = width; + } + + public int getWidth() { + return width; + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/image/ImageMessage.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/image/ImageMessage.java index fc017236..d3072d55 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/image/ImageMessage.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/image/ImageMessage.java @@ -1,207 +1,221 @@ -package com.gmail.filoghost.holographicdisplays.image; - -import java.awt.Color; -import java.awt.Graphics2D; -import java.awt.Image; -import java.awt.image.BufferedImage; -import java.util.Map; -import java.util.Map.Entry; - -import org.bukkit.ChatColor; - -import com.gmail.filoghost.holographicdisplays.disk.Configuration; -import com.gmail.filoghost.holographicdisplays.exception.TooWideException; -import com.gmail.filoghost.holographicdisplays.util.Utils; - -/** - * Huge thanks to bobacadodl for this awesome library! - * Bukkit thread: https://forums.bukkit.org/threads/lib-imagemessage-v2-1-send-images-to-players-via-the-chat.204902 - */ -public class ImageMessage { - - public static final int MAX_WIDTH = 150; - - private static final Map colorsMap = Utils.newMap(); - - private static final Map graysMap = Utils.newMap(); - - - static { - colorsMap.put(ChatColor.DARK_BLUE, new Color(0, 0, 170)); - colorsMap.put(ChatColor.DARK_GREEN, new Color(0, 170, 0)); - colorsMap.put(ChatColor.DARK_AQUA, new Color(0, 170, 170)); - colorsMap.put(ChatColor.DARK_RED, new Color(170, 0, 0)); - colorsMap.put(ChatColor.DARK_PURPLE, new Color(170, 0, 170)); - colorsMap.put(ChatColor.GOLD, new Color(255, 170, 0)); - colorsMap.put(ChatColor.BLUE, new Color(85, 85, 255)); - colorsMap.put(ChatColor.GREEN, new Color(85, 255, 85)); - colorsMap.put(ChatColor.AQUA, new Color(85, 255, 255)); - colorsMap.put(ChatColor.RED, new Color(255, 85, 85)); - colorsMap.put(ChatColor.LIGHT_PURPLE, new Color(255, 85, 255)); - colorsMap.put(ChatColor.YELLOW, new Color(255, 255, 85)); - - graysMap.put(ChatColor.BLACK, new Color(0, 0, 0)); - graysMap.put(ChatColor.DARK_GRAY, new Color(85, 85, 85)); - graysMap.put(ChatColor.GRAY, new Color(170, 170, 170)); - graysMap.put(ChatColor.WHITE, new Color(255, 255, 255)); - } - - - private String[] lines; - - public ImageMessage(BufferedImage image, int width) throws TooWideException { - ChatColor[][] chatColors = toChatColorArray(image, width); - lines = toImgMessage(chatColors); - } - - private ChatColor[][] toChatColorArray(BufferedImage image, int width) throws TooWideException { - double ratio = (double) image.getHeight() / image.getWidth(); - int height = (int) (((double)width) * ratio); - if (height == 0) { - height = 1; - } - - if (width > MAX_WIDTH) { - throw new TooWideException(width); - } - - BufferedImage resized = resizeImage(image, width, height); - - ChatColor[][] chatImg = new ChatColor[resized.getWidth()][resized.getHeight()]; - for (int x = 0; x < resized.getWidth(); x++) { - for (int y = 0; y < resized.getHeight(); y++) { - int rgb = resized.getRGB(x, y); - chatImg[x][y] = getClosestChatColor(new Color(rgb, true)); - } - } - return chatImg; - } - - private String[] toImgMessage(ChatColor[][] colors) { - - String[] lines = new String[colors[0].length]; - ChatColor transparencyColor = Configuration.transparencyColor; - String transparencySymbol = Configuration.transparencySymbol; - String imageSymbol = Configuration.imageSymbol; - - for (int y = 0; y < colors[0].length; y++) { - - StringBuffer line = new StringBuffer(); - - ChatColor previous = ChatColor.RESET; - - for (int x = 0; x < colors.length; x++) { - - ChatColor currentColor = colors[x][y]; - - if (currentColor == null) { - - // Use the trasparent char - if (previous != transparencyColor) { - - // Change the previous chat color and append the newer - line.append(transparencyColor); - previous = transparencyColor; - - } - line.append(transparencySymbol); - - } else { - - if (previous != currentColor) { - line.append(currentColor.toString()); - previous = currentColor; - } - - line.append(imageSymbol); - } - } - - lines[y] = line.toString(); - } - - return lines; - } - - private BufferedImage resizeImage(BufferedImage originalImage, int width, int height) { - return toBufferedImage(originalImage.getScaledInstance(width, height, Image.SCALE_DEFAULT)); - } - - private BufferedImage toBufferedImage(Image img) { - - // Creates a buffered image with transparency. - BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB); - - // Draws the image on to the buffered image. - Graphics2D graphics = bimage.createGraphics(); - graphics.drawImage(img, 0, 0, null); - graphics.dispose(); - - // Returns the buffered image. - return bimage; - } - - private double getDistance(Color c1, Color c2) { - double rmean = (c1.getRed() + c2.getRed()) / 2.0; - double r = c1.getRed() - c2.getRed(); - double g = c1.getGreen() - c2.getGreen(); - int b = c1.getBlue() - c2.getBlue(); - double weightR = 2 + rmean / 256.0; - double weightG = 4.0; - double weightB = 2 + (255 - rmean) / 256.0; - return weightR * r * r + weightG * g * g + weightB * b * b; - } - - private boolean areIdentical(Color c1, Color c2) { - return Math.abs(c1.getRed() - c2.getRed()) <= 5 && - Math.abs(c1.getGreen() - c2.getGreen()) <= 5 && - Math.abs(c1.getBlue() - c2.getBlue()) <= 5; - - } - - private ChatColor getClosestChatColor(Color color) { - if (color.getAlpha() < 80) return null; - - for (Entry entry : colorsMap.entrySet()) { - if (areIdentical(entry.getValue(), color)) { - return entry.getKey(); - } - } - - double bestGrayDistance = -1; - ChatColor bestGrayMatch = null; - - for (Entry entry : graysMap.entrySet()) { - double distance = getDistance(color, entry.getValue()); - - if (distance < bestGrayDistance || bestGrayDistance == -1) { - bestGrayDistance = distance; - bestGrayMatch = entry.getKey(); - } - } - - if (bestGrayDistance < 17500) { - return bestGrayMatch; - } - - double bestColorDistance = -1; - ChatColor bestColorMatch = null; - - for (Entry entry : colorsMap.entrySet()) { - double distance = getDistance(color, entry.getValue()); - - if (distance < bestColorDistance || bestColorDistance == -1) { - bestColorDistance = distance; - bestColorMatch = entry.getKey(); - } - } - - // Minecraft has 15 colors - return bestColorMatch; - } - - - public String[] getLines() { - return lines; - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.image; + +import java.awt.Color; +import java.awt.Graphics2D; +import java.awt.Image; +import java.awt.image.BufferedImage; +import java.util.Map; +import java.util.Map.Entry; + +import org.bukkit.ChatColor; + +import com.gmail.filoghost.holographicdisplays.disk.Configuration; +import com.gmail.filoghost.holographicdisplays.exception.TooWideException; +import com.gmail.filoghost.holographicdisplays.util.Utils; + +/** + * Huge thanks to bobacadodl for this awesome library! + * Bukkit thread: https://forums.bukkit.org/threads/lib-imagemessage-v2-1-send-images-to-players-via-the-chat.204902 + */ +public class ImageMessage { + + public static final int MAX_WIDTH = 150; + + private static final Map colorsMap = Utils.newMap(); + + private static final Map graysMap = Utils.newMap(); + + + static { + colorsMap.put(ChatColor.DARK_BLUE, new Color(0, 0, 170)); + colorsMap.put(ChatColor.DARK_GREEN, new Color(0, 170, 0)); + colorsMap.put(ChatColor.DARK_AQUA, new Color(0, 170, 170)); + colorsMap.put(ChatColor.DARK_RED, new Color(170, 0, 0)); + colorsMap.put(ChatColor.DARK_PURPLE, new Color(170, 0, 170)); + colorsMap.put(ChatColor.GOLD, new Color(255, 170, 0)); + colorsMap.put(ChatColor.BLUE, new Color(85, 85, 255)); + colorsMap.put(ChatColor.GREEN, new Color(85, 255, 85)); + colorsMap.put(ChatColor.AQUA, new Color(85, 255, 255)); + colorsMap.put(ChatColor.RED, new Color(255, 85, 85)); + colorsMap.put(ChatColor.LIGHT_PURPLE, new Color(255, 85, 255)); + colorsMap.put(ChatColor.YELLOW, new Color(255, 255, 85)); + + graysMap.put(ChatColor.BLACK, new Color(0, 0, 0)); + graysMap.put(ChatColor.DARK_GRAY, new Color(85, 85, 85)); + graysMap.put(ChatColor.GRAY, new Color(170, 170, 170)); + graysMap.put(ChatColor.WHITE, new Color(255, 255, 255)); + } + + + private String[] lines; + + public ImageMessage(BufferedImage image, int width) throws TooWideException { + ChatColor[][] chatColors = toChatColorArray(image, width); + lines = toImgMessage(chatColors); + } + + private ChatColor[][] toChatColorArray(BufferedImage image, int width) throws TooWideException { + double ratio = (double) image.getHeight() / image.getWidth(); + int height = (int) (((double)width) * ratio); + if (height == 0) { + height = 1; + } + + if (width > MAX_WIDTH) { + throw new TooWideException(width); + } + + BufferedImage resized = resizeImage(image, width, height); + + ChatColor[][] chatImg = new ChatColor[resized.getWidth()][resized.getHeight()]; + for (int x = 0; x < resized.getWidth(); x++) { + for (int y = 0; y < resized.getHeight(); y++) { + int rgb = resized.getRGB(x, y); + chatImg[x][y] = getClosestChatColor(new Color(rgb, true)); + } + } + return chatImg; + } + + private String[] toImgMessage(ChatColor[][] colors) { + + String[] lines = new String[colors[0].length]; + ChatColor transparencyColor = Configuration.transparencyColor; + String transparencySymbol = Configuration.transparencySymbol; + String imageSymbol = Configuration.imageSymbol; + + for (int y = 0; y < colors[0].length; y++) { + + StringBuffer line = new StringBuffer(); + + ChatColor previous = ChatColor.RESET; + + for (int x = 0; x < colors.length; x++) { + + ChatColor currentColor = colors[x][y]; + + if (currentColor == null) { + + // Use the trasparent char + if (previous != transparencyColor) { + + // Change the previous chat color and append the newer + line.append(transparencyColor); + previous = transparencyColor; + + } + line.append(transparencySymbol); + + } else { + + if (previous != currentColor) { + line.append(currentColor.toString()); + previous = currentColor; + } + + line.append(imageSymbol); + } + } + + lines[y] = line.toString(); + } + + return lines; + } + + private BufferedImage resizeImage(BufferedImage originalImage, int width, int height) { + return toBufferedImage(originalImage.getScaledInstance(width, height, Image.SCALE_DEFAULT)); + } + + private BufferedImage toBufferedImage(Image img) { + + // Creates a buffered image with transparency. + BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB); + + // Draws the image on to the buffered image. + Graphics2D graphics = bimage.createGraphics(); + graphics.drawImage(img, 0, 0, null); + graphics.dispose(); + + // Returns the buffered image. + return bimage; + } + + private double getDistance(Color c1, Color c2) { + double rmean = (c1.getRed() + c2.getRed()) / 2.0; + double r = c1.getRed() - c2.getRed(); + double g = c1.getGreen() - c2.getGreen(); + int b = c1.getBlue() - c2.getBlue(); + double weightR = 2 + rmean / 256.0; + double weightG = 4.0; + double weightB = 2 + (255 - rmean) / 256.0; + return weightR * r * r + weightG * g * g + weightB * b * b; + } + + private boolean areIdentical(Color c1, Color c2) { + return Math.abs(c1.getRed() - c2.getRed()) <= 5 && + Math.abs(c1.getGreen() - c2.getGreen()) <= 5 && + Math.abs(c1.getBlue() - c2.getBlue()) <= 5; + + } + + private ChatColor getClosestChatColor(Color color) { + if (color.getAlpha() < 80) return null; + + for (Entry entry : colorsMap.entrySet()) { + if (areIdentical(entry.getValue(), color)) { + return entry.getKey(); + } + } + + double bestGrayDistance = -1; + ChatColor bestGrayMatch = null; + + for (Entry entry : graysMap.entrySet()) { + double distance = getDistance(color, entry.getValue()); + + if (distance < bestGrayDistance || bestGrayDistance == -1) { + bestGrayDistance = distance; + bestGrayMatch = entry.getKey(); + } + } + + if (bestGrayDistance < 17500) { + return bestGrayMatch; + } + + double bestColorDistance = -1; + ChatColor bestColorMatch = null; + + for (Entry entry : colorsMap.entrySet()) { + double distance = getDistance(color, entry.getValue()); + + if (distance < bestColorDistance || bestColorDistance == -1) { + bestColorDistance = distance; + bestColorMatch = entry.getKey(); + } + } + + // Minecraft has 15 colors + return bestColorMatch; + } + + + public String[] getLines() { + return lines; + } +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/listener/MainListener.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/listener/MainListener.java index 93856a0d..7e2dd353 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/listener/MainListener.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/listener/MainListener.java @@ -1,179 +1,193 @@ -package com.gmail.filoghost.holographicdisplays.listener; - -import java.util.Map; -import java.util.logging.Level; - -import org.bukkit.Bukkit; -import org.bukkit.Chunk; -import org.bukkit.entity.Entity; -import org.bukkit.entity.EntityType; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; -import org.bukkit.event.entity.CreatureSpawnEvent; -import org.bukkit.event.entity.ItemSpawnEvent; -import org.bukkit.event.entity.ProjectileLaunchEvent; -import org.bukkit.event.player.PlayerInteractEntityEvent; -import org.bukkit.event.player.PlayerJoinEvent; -import org.bukkit.event.player.PlayerQuitEvent; -import org.bukkit.event.world.ChunkLoadEvent; -import org.bukkit.event.world.ChunkUnloadEvent; -import org.bukkit.plugin.Plugin; - -import com.gmail.filoghost.holographicdisplays.HolographicDisplays; -import com.gmail.filoghost.holographicdisplays.api.Hologram; -import com.gmail.filoghost.holographicdisplays.api.handler.PickupHandler; -import com.gmail.filoghost.holographicdisplays.commands.Colors; -import com.gmail.filoghost.holographicdisplays.commands.Strings; -import com.gmail.filoghost.holographicdisplays.disk.Configuration; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; -import com.gmail.filoghost.holographicdisplays.object.CraftHologram; -import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; -import com.gmail.filoghost.holographicdisplays.object.PluginHologram; -import com.gmail.filoghost.holographicdisplays.object.PluginHologramManager; -import com.gmail.filoghost.holographicdisplays.object.line.CraftTouchSlimeLine; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.Utils; - -public class MainListener implements Listener, ItemPickupManager { - - private NMSManager nmsManager; - - private Map anticlickSpam = Utils.newMap(); - - public MainListener(NMSManager nmsManager) { - this.nmsManager = nmsManager; - } - - @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) - public void onChunkUnload(ChunkUnloadEvent event) { - for (Entity entity : event.getChunk().getEntities()) { - if (!entity.isDead()) { - NMSEntityBase entityBase = nmsManager.getNMSEntityBase(entity); - - if (entityBase != null) { - ((CraftHologram) entityBase.getHologramLine().getParent()).despawnEntities(); - } - } - } - } - - @EventHandler (priority = EventPriority.MONITOR) - public void onChunkLoad(ChunkLoadEvent event) { - final Chunk chunk = event.getChunk(); - - // Other plugins could call this event wrongly, check if the chunk is actually loaded. - if (chunk.isLoaded()) { - - // In case another plugin loads the chunk asynchronously always make sure to load the holograms on the main thread. - if (Bukkit.isPrimaryThread()) { - processChunkLoad(chunk); - } else { - Bukkit.getScheduler().runTask(HolographicDisplays.getInstance(), new Runnable() { - @Override - public void run() { - processChunkLoad(chunk); - } - }); - } - } - } - - // This method should be always called synchronously. - public void processChunkLoad(Chunk chunk) { - NamedHologramManager.onChunkLoad(chunk); - PluginHologramManager.onChunkLoad(chunk); - } - - @EventHandler (priority = EventPriority.MONITOR, ignoreCancelled = false) - public void onCreatureSpawn(CreatureSpawnEvent event) { - if (nmsManager.isNMSEntityBase(event.getEntity())) { - if (event.isCancelled()) { - event.setCancelled(false); - } - } - } - - @EventHandler (priority = EventPriority.MONITOR, ignoreCancelled = false) - public void onProjectileLaunch(ProjectileLaunchEvent event) { - if (nmsManager.isNMSEntityBase(event.getEntity())) { - if (event.isCancelled()) { - event.setCancelled(false); - } - } - } - - @EventHandler (priority = EventPriority.MONITOR, ignoreCancelled = false) - public void onItemSpawn(ItemSpawnEvent event) { - if (nmsManager.isNMSEntityBase(event.getEntity())) { - if (event.isCancelled()) { - event.setCancelled(false); - } - } - } - - @EventHandler (priority = EventPriority.MONITOR, ignoreCancelled = true) - public void onSlimeInteract(PlayerInteractEntityEvent event) { - if (event.getRightClicked().getType() == EntityType.SLIME) { - - Player clicker = event.getPlayer(); - NMSEntityBase entityBase = nmsManager.getNMSEntityBase(event.getRightClicked()); - - if (entityBase != null && entityBase.getHologramLine() instanceof CraftTouchSlimeLine && !isSpectatorMode(clicker)) { - - CraftTouchSlimeLine touchSlime = (CraftTouchSlimeLine) entityBase.getHologramLine(); - - if (touchSlime.getTouchablePiece().getTouchHandler() != null && touchSlime.getParent().getVisibilityManager().isVisibleTo(clicker)) { - - Long lastClick = anticlickSpam.get(clicker); - if (lastClick != null && System.currentTimeMillis() - lastClick.longValue() < 100) { - return; - } - - anticlickSpam.put(event.getPlayer(), System.currentTimeMillis()); - - try { - touchSlime.getTouchablePiece().getTouchHandler().onTouch(event.getPlayer()); - } catch (Throwable t) { - Plugin plugin = touchSlime.getParent() instanceof PluginHologram ? ((PluginHologram) touchSlime.getParent()).getOwner() : HolographicDisplays.getInstance(); - ConsoleLogger.log(Level.WARNING, "The plugin " + plugin.getName() + " generated an exception when the player " + event.getPlayer().getName() + " touched a hologram.", t); - } - } - } - } - } - - public boolean isSpectatorMode(Player player) { - return player.getGameMode().toString().equals("SPECTATOR"); - } - - @Override - public void handleItemLinePickup(Player player, PickupHandler pickupHandler, Hologram hologram) { - try { - if (hologram.getVisibilityManager().isVisibleTo(player)) { - pickupHandler.onPickup(player); - } - } catch (Throwable t) { - Plugin plugin = hologram instanceof PluginHologram ? ((PluginHologram) hologram).getOwner() : HolographicDisplays.getInstance(); - ConsoleLogger.log(Level.WARNING, "The plugin " + plugin.getName() + " generated an exception when the player " + player.getName() + " picked up an item from a hologram.", t); - } - } - - @EventHandler - public void onJoin(PlayerJoinEvent event) { - if (Configuration.updateNotification && HolographicDisplays.getNewVersion() != null) { - if (event.getPlayer().hasPermission(Strings.BASE_PERM + "update")) { - event.getPlayer().sendMessage(Colors.PRIMARY_SHADOW + "[HolographicDisplays] " + Colors.PRIMARY + "Found an update: " + HolographicDisplays.getNewVersion() + ". Download:"); - event.getPlayer().sendMessage(Colors.PRIMARY_SHADOW + ">> " + Colors.PRIMARY + "http://dev.bukkit.org/bukkit-plugins/holographic-displays"); - } - } - } - - @EventHandler - public void onQuit(PlayerQuitEvent event) { - anticlickSpam.remove(event.getPlayer()); - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.listener; + +import java.util.Map; +import java.util.logging.Level; + +import org.bukkit.Bukkit; +import org.bukkit.Chunk; +import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.CreatureSpawnEvent; +import org.bukkit.event.entity.ItemSpawnEvent; +import org.bukkit.event.entity.ProjectileLaunchEvent; +import org.bukkit.event.player.PlayerInteractEntityEvent; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.event.world.ChunkLoadEvent; +import org.bukkit.event.world.ChunkUnloadEvent; +import org.bukkit.plugin.Plugin; + +import com.gmail.filoghost.holographicdisplays.HolographicDisplays; +import com.gmail.filoghost.holographicdisplays.api.Hologram; +import com.gmail.filoghost.holographicdisplays.api.handler.PickupHandler; +import com.gmail.filoghost.holographicdisplays.commands.Colors; +import com.gmail.filoghost.holographicdisplays.commands.Strings; +import com.gmail.filoghost.holographicdisplays.disk.Configuration; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.ItemPickupManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; +import com.gmail.filoghost.holographicdisplays.object.CraftHologram; +import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; +import com.gmail.filoghost.holographicdisplays.object.PluginHologram; +import com.gmail.filoghost.holographicdisplays.object.PluginHologramManager; +import com.gmail.filoghost.holographicdisplays.object.line.CraftTouchSlimeLine; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.Utils; + +public class MainListener implements Listener, ItemPickupManager { + + private NMSManager nmsManager; + + private Map anticlickSpam = Utils.newMap(); + + public MainListener(NMSManager nmsManager) { + this.nmsManager = nmsManager; + } + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onChunkUnload(ChunkUnloadEvent event) { + for (Entity entity : event.getChunk().getEntities()) { + if (!entity.isDead()) { + NMSEntityBase entityBase = nmsManager.getNMSEntityBase(entity); + + if (entityBase != null) { + ((CraftHologram) entityBase.getHologramLine().getParent()).despawnEntities(); + } + } + } + } + + @EventHandler (priority = EventPriority.MONITOR) + public void onChunkLoad(ChunkLoadEvent event) { + final Chunk chunk = event.getChunk(); + + // Other plugins could call this event wrongly, check if the chunk is actually loaded. + if (chunk.isLoaded()) { + + // In case another plugin loads the chunk asynchronously always make sure to load the holograms on the main thread. + if (Bukkit.isPrimaryThread()) { + processChunkLoad(chunk); + } else { + Bukkit.getScheduler().runTask(HolographicDisplays.getInstance(), new Runnable() { + @Override + public void run() { + processChunkLoad(chunk); + } + }); + } + } + } + + // This method should be always called synchronously. + public void processChunkLoad(Chunk chunk) { + NamedHologramManager.onChunkLoad(chunk); + PluginHologramManager.onChunkLoad(chunk); + } + + @EventHandler (priority = EventPriority.MONITOR, ignoreCancelled = false) + public void onCreatureSpawn(CreatureSpawnEvent event) { + if (nmsManager.isNMSEntityBase(event.getEntity())) { + if (event.isCancelled()) { + event.setCancelled(false); + } + } + } + + @EventHandler (priority = EventPriority.MONITOR, ignoreCancelled = false) + public void onProjectileLaunch(ProjectileLaunchEvent event) { + if (nmsManager.isNMSEntityBase(event.getEntity())) { + if (event.isCancelled()) { + event.setCancelled(false); + } + } + } + + @EventHandler (priority = EventPriority.MONITOR, ignoreCancelled = false) + public void onItemSpawn(ItemSpawnEvent event) { + if (nmsManager.isNMSEntityBase(event.getEntity())) { + if (event.isCancelled()) { + event.setCancelled(false); + } + } + } + + @EventHandler (priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onSlimeInteract(PlayerInteractEntityEvent event) { + if (event.getRightClicked().getType() == EntityType.SLIME) { + + Player clicker = event.getPlayer(); + NMSEntityBase entityBase = nmsManager.getNMSEntityBase(event.getRightClicked()); + + if (entityBase != null && entityBase.getHologramLine() instanceof CraftTouchSlimeLine && !isSpectatorMode(clicker)) { + + CraftTouchSlimeLine touchSlime = (CraftTouchSlimeLine) entityBase.getHologramLine(); + + if (touchSlime.getTouchablePiece().getTouchHandler() != null && touchSlime.getParent().getVisibilityManager().isVisibleTo(clicker)) { + + Long lastClick = anticlickSpam.get(clicker); + if (lastClick != null && System.currentTimeMillis() - lastClick.longValue() < 100) { + return; + } + + anticlickSpam.put(event.getPlayer(), System.currentTimeMillis()); + + try { + touchSlime.getTouchablePiece().getTouchHandler().onTouch(event.getPlayer()); + } catch (Throwable t) { + Plugin plugin = touchSlime.getParent() instanceof PluginHologram ? ((PluginHologram) touchSlime.getParent()).getOwner() : HolographicDisplays.getInstance(); + ConsoleLogger.log(Level.WARNING, "The plugin " + plugin.getName() + " generated an exception when the player " + event.getPlayer().getName() + " touched a hologram.", t); + } + } + } + } + } + + public boolean isSpectatorMode(Player player) { + return player.getGameMode().toString().equals("SPECTATOR"); + } + + @Override + public void handleItemLinePickup(Player player, PickupHandler pickupHandler, Hologram hologram) { + try { + if (hologram.getVisibilityManager().isVisibleTo(player)) { + pickupHandler.onPickup(player); + } + } catch (Throwable t) { + Plugin plugin = hologram instanceof PluginHologram ? ((PluginHologram) hologram).getOwner() : HolographicDisplays.getInstance(); + ConsoleLogger.log(Level.WARNING, "The plugin " + plugin.getName() + " generated an exception when the player " + player.getName() + " picked up an item from a hologram.", t); + } + } + + @EventHandler + public void onJoin(PlayerJoinEvent event) { + if (Configuration.updateNotification && HolographicDisplays.getNewVersion() != null) { + if (event.getPlayer().hasPermission(Strings.BASE_PERM + "update")) { + event.getPlayer().sendMessage(Colors.PRIMARY_SHADOW + "[HolographicDisplays] " + Colors.PRIMARY + "Found an update: " + HolographicDisplays.getNewVersion() + ". Download:"); + event.getPlayer().sendMessage(Colors.PRIMARY_SHADOW + ">> " + Colors.PRIMARY + "http://dev.bukkit.org/bukkit-plugins/holographic-displays"); + } + } + } + + @EventHandler + public void onQuit(PlayerQuitEvent event) { + anticlickSpam.remove(event.getPlayer()); + } +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/CraftHologram.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/CraftHologram.java index d28a1b1d..0542c4a3 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/CraftHologram.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/CraftHologram.java @@ -1,446 +1,460 @@ -package com.gmail.filoghost.holographicdisplays.object; - -import java.util.List; - -import org.bukkit.Chunk; -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; -import com.gmail.filoghost.holographicdisplays.object.line.CraftTextLine; -import com.gmail.filoghost.holographicdisplays.placeholder.PlaceholdersManager; -import com.gmail.filoghost.holographicdisplays.util.Utils; -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 { - - // Position variables. - private World world; - private double x, y, z; - private int chunkX, chunkZ; - - // The entities that represent lines. - private final List lines; - - private CraftVisibilityManager visibilityManager; - private boolean allowPlaceholders; - private long creationTimestamp; - private boolean deleted; - - public CraftHologram(Location location) { - Validator.notNull(location, "location"); - updateLocation(location.getWorld(), location.getX(), location.getY(), location.getZ()); - - lines = Utils.newList(); - allowPlaceholders = false; - creationTimestamp = System.currentTimeMillis(); - visibilityManager = new CraftVisibilityManager(this); - } - - public boolean isInChunk(Chunk chunk) { - return chunk.getWorld() == world && chunk.getX() == chunkX && chunk.getZ() == chunkZ; - } - - @Override - public World getWorld() { - return world; - } - - @Override - public double getX() { - return x; - } - - @Override - public double getY() { - return y; - } - - @Override - public double getZ() { - return z; - } - - @Override - public Location getLocation() { - return new Location(world, x, y, z); - } - - private void updateLocation(World world, double x, double y, double z) { - Validator.notNull(world, "world"); - - this.world = world; - this.x = x; - this.y = y; - this.z = z; - chunkX = Utils.floor(x) >> 4; - chunkZ = Utils.floor(z) >> 4; - } - - @Override - public boolean isDeleted() { - return deleted; - } - - @Override - public void delete() { - if (!deleted) { - deleted = true; - clearLines(); - } - } - - public List getLinesUnsafe() { - return lines; - } - - @Override - public CraftHologramLine getLine(int index) { - return lines.get(index); - } - - @Override - public CraftTextLine appendTextLine(String text) { - Validator.isTrue(!deleted, "hologram already deleted"); - - CraftTextLine line = new CraftTextLine(this, text); - lines.add(line); - refreshSingleLines(); - return line; - } - - @Override - public CraftItemLine appendItemLine(ItemStack itemStack) { - Validator.isTrue(!deleted, "hologram already deleted"); - Validator.notNull(itemStack, "itemStack"); - - CraftItemLine line = new CraftItemLine(this, itemStack); - lines.add(line); - refreshSingleLines(); - return line; - } - - @Override - public CraftTextLine insertTextLine(int index, String text) { - Validator.isTrue(!deleted, "hologram already deleted"); - - CraftTextLine line = new CraftTextLine(this, text); - lines.add(index, line); - refreshSingleLines(); - return line; - } - - @Override - public CraftItemLine insertItemLine(int index, ItemStack itemStack) { - Validator.isTrue(!deleted, "hologram already deleted"); - Validator.notNull(itemStack, "itemStack"); - - CraftItemLine line = new CraftItemLine(this, itemStack); - lines.add(index, line); - refreshSingleLines(); - return line; - } - - @Override - public void removeLine(int index) { - Validator.isTrue(!deleted, "hologram already deleted"); - - lines.remove(index).despawn(); - refreshSingleLines(); - } - - public void removeLine(CraftHologramLine line) { - Validator.isTrue(!deleted, "hologram already deleted"); - - lines.remove(line); - line.despawn(); - refreshSingleLines(); - } - - @Override - public void clearLines() { - for (CraftHologramLine line : lines) { - line.despawn(); - } - - lines.clear(); - } - - @Override - public int size() { - return lines.size(); - } - - @Override - public double getHeight() { - if (lines.isEmpty()) { - return 0; - } - - double height = 0.0; - - for (CraftHologramLine line : lines) { - height += line.getHeight(); - } - - height += Configuration.spaceBetweenLines * (lines.size() - 1); - return height; - } - - @Override - public CraftVisibilityManager getVisibilityManager() { - return visibilityManager; - } - - - @Override - public long getCreationTimestamp() { - return creationTimestamp; - } - - @Override - public boolean isAllowPlaceholders() { - return allowPlaceholders; - } - - @Override - public void setAllowPlaceholders(boolean allowPlaceholders) { - if (this.allowPlaceholders != allowPlaceholders) { - - if (allowPlaceholders) { - // Now allowed, previously weren't - for (CraftHologramLine line : lines) { - if (line instanceof CraftTextLine) { - PlaceholdersManager.trackIfNecessary((CraftTextLine) line); - } - } - - } else { - - // Now not allowed - for (CraftHologramLine line : lines) { - if (line instanceof CraftTextLine) { - PlaceholdersManager.untrack((CraftTextLine) line); - } - } - } - - this.allowPlaceholders = allowPlaceholders; - } - } - - - public void refreshAll() { - if (world.isChunkLoaded(chunkX, chunkZ)) { - spawnEntities(); - } - } - - public void refreshSingleLines() { - if (world.isChunkLoaded(chunkX, chunkZ)) { - - double currentY = this.y; - boolean first = true; - - for (CraftHologramLine line : lines) { - - currentY -= line.getHeight(); - - if (first) { - first = false; - } else { - currentY -= Configuration.spaceBetweenLines; - } - - if (line.isSpawned()) { - line.teleport(x, currentY, z); - } else { - line.spawn(world, x, currentY, z); - if (allowPlaceholders && line instanceof CraftTextLine) { - PlaceholdersManager.trackIfNecessary((CraftTextLine) line); - } - } - } - } - } - - /** - * Forces the entities to spawn, without checking if the chunk is loaded. - */ - public void spawnEntities() { - Validator.isTrue(!deleted, "hologram already deleted"); - - despawnEntities(); - - double currentY = this.y; - boolean first = true; - - for (CraftHologramLine line : lines) { - - currentY -= line.getHeight(); - - if (first) { - first = false; - } else { - currentY -= Configuration.spaceBetweenLines; - } - - line.spawn(world, x, currentY, z); - if (allowPlaceholders && line instanceof CraftTextLine) { - PlaceholdersManager.trackIfNecessary((CraftTextLine) line); - } - } - } - - /** - * Called by the PluginHologramManager when the chunk is unloaded. - */ - public void despawnEntities() { - for (CraftHologramLine piece : lines) { - piece.despawn(); - } - } - - @Override - public void teleport(Location location) { - Validator.notNull(location, "location"); - teleport(location.getWorld(), location.getX(), location.getY(), location.getZ()); - } - - @Override - public void teleport(World world, double x, double y, double z) { - Validator.isTrue(!deleted, "hologram already deleted"); - Validator.notNull(world, "world"); - - updateLocation(world, x, y, z); - - if (this.world != world) { - despawnEntities(); - refreshAll(); - return; - } - - double currentY = y; - boolean first = true; - - for (CraftHologramLine line : lines) { - - if (!line.isSpawned()) { - continue; - } - - currentY -= line.getHeight(); - - if (first) { - first = false; - } else { - currentY -= Configuration.spaceBetweenLines; - } - - line.teleport(x, currentY, z); - } - } - - @Override - 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() - * Two holograms can never be equal. Even if they have the same position and the same elements, they are still two different objects. - * The equals and hashcode methods are not overriden, two holograms are equal only if they have the same memory address. - */ - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.object; + +import java.util.List; + +import org.bukkit.Chunk; +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; +import com.gmail.filoghost.holographicdisplays.object.line.CraftTextLine; +import com.gmail.filoghost.holographicdisplays.placeholder.PlaceholdersManager; +import com.gmail.filoghost.holographicdisplays.util.Utils; +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 { + + // Position variables. + private World world; + private double x, y, z; + private int chunkX, chunkZ; + + // The entities that represent lines. + private final List lines; + + private CraftVisibilityManager visibilityManager; + private boolean allowPlaceholders; + private long creationTimestamp; + private boolean deleted; + + public CraftHologram(Location location) { + Validator.notNull(location, "location"); + updateLocation(location.getWorld(), location.getX(), location.getY(), location.getZ()); + + lines = Utils.newList(); + allowPlaceholders = false; + creationTimestamp = System.currentTimeMillis(); + visibilityManager = new CraftVisibilityManager(this); + } + + public boolean isInChunk(Chunk chunk) { + return chunk.getWorld() == world && chunk.getX() == chunkX && chunk.getZ() == chunkZ; + } + + @Override + public World getWorld() { + return world; + } + + @Override + public double getX() { + return x; + } + + @Override + public double getY() { + return y; + } + + @Override + public double getZ() { + return z; + } + + @Override + public Location getLocation() { + return new Location(world, x, y, z); + } + + private void updateLocation(World world, double x, double y, double z) { + Validator.notNull(world, "world"); + + this.world = world; + this.x = x; + this.y = y; + this.z = z; + chunkX = Utils.floor(x) >> 4; + chunkZ = Utils.floor(z) >> 4; + } + + @Override + public boolean isDeleted() { + return deleted; + } + + @Override + public void delete() { + if (!deleted) { + deleted = true; + clearLines(); + } + } + + public List getLinesUnsafe() { + return lines; + } + + @Override + public CraftHologramLine getLine(int index) { + return lines.get(index); + } + + @Override + public CraftTextLine appendTextLine(String text) { + Validator.isTrue(!deleted, "hologram already deleted"); + + CraftTextLine line = new CraftTextLine(this, text); + lines.add(line); + refreshSingleLines(); + return line; + } + + @Override + public CraftItemLine appendItemLine(ItemStack itemStack) { + Validator.isTrue(!deleted, "hologram already deleted"); + Validator.notNull(itemStack, "itemStack"); + + CraftItemLine line = new CraftItemLine(this, itemStack); + lines.add(line); + refreshSingleLines(); + return line; + } + + @Override + public CraftTextLine insertTextLine(int index, String text) { + Validator.isTrue(!deleted, "hologram already deleted"); + + CraftTextLine line = new CraftTextLine(this, text); + lines.add(index, line); + refreshSingleLines(); + return line; + } + + @Override + public CraftItemLine insertItemLine(int index, ItemStack itemStack) { + Validator.isTrue(!deleted, "hologram already deleted"); + Validator.notNull(itemStack, "itemStack"); + + CraftItemLine line = new CraftItemLine(this, itemStack); + lines.add(index, line); + refreshSingleLines(); + return line; + } + + @Override + public void removeLine(int index) { + Validator.isTrue(!deleted, "hologram already deleted"); + + lines.remove(index).despawn(); + refreshSingleLines(); + } + + public void removeLine(CraftHologramLine line) { + Validator.isTrue(!deleted, "hologram already deleted"); + + lines.remove(line); + line.despawn(); + refreshSingleLines(); + } + + @Override + public void clearLines() { + for (CraftHologramLine line : lines) { + line.despawn(); + } + + lines.clear(); + } + + @Override + public int size() { + return lines.size(); + } + + @Override + public double getHeight() { + if (lines.isEmpty()) { + return 0; + } + + double height = 0.0; + + for (CraftHologramLine line : lines) { + height += line.getHeight(); + } + + height += Configuration.spaceBetweenLines * (lines.size() - 1); + return height; + } + + @Override + public CraftVisibilityManager getVisibilityManager() { + return visibilityManager; + } + + + @Override + public long getCreationTimestamp() { + return creationTimestamp; + } + + @Override + public boolean isAllowPlaceholders() { + return allowPlaceholders; + } + + @Override + public void setAllowPlaceholders(boolean allowPlaceholders) { + if (this.allowPlaceholders != allowPlaceholders) { + + if (allowPlaceholders) { + // Now allowed, previously weren't + for (CraftHologramLine line : lines) { + if (line instanceof CraftTextLine) { + PlaceholdersManager.trackIfNecessary((CraftTextLine) line); + } + } + + } else { + + // Now not allowed + for (CraftHologramLine line : lines) { + if (line instanceof CraftTextLine) { + PlaceholdersManager.untrack((CraftTextLine) line); + } + } + } + + this.allowPlaceholders = allowPlaceholders; + } + } + + + public void refreshAll() { + if (world.isChunkLoaded(chunkX, chunkZ)) { + spawnEntities(); + } + } + + public void refreshSingleLines() { + if (world.isChunkLoaded(chunkX, chunkZ)) { + + double currentY = this.y; + boolean first = true; + + for (CraftHologramLine line : lines) { + + currentY -= line.getHeight(); + + if (first) { + first = false; + } else { + currentY -= Configuration.spaceBetweenLines; + } + + if (line.isSpawned()) { + line.teleport(x, currentY, z); + } else { + line.spawn(world, x, currentY, z); + if (allowPlaceholders && line instanceof CraftTextLine) { + PlaceholdersManager.trackIfNecessary((CraftTextLine) line); + } + } + } + } + } + + /** + * Forces the entities to spawn, without checking if the chunk is loaded. + */ + public void spawnEntities() { + Validator.isTrue(!deleted, "hologram already deleted"); + + despawnEntities(); + + double currentY = this.y; + boolean first = true; + + for (CraftHologramLine line : lines) { + + currentY -= line.getHeight(); + + if (first) { + first = false; + } else { + currentY -= Configuration.spaceBetweenLines; + } + + line.spawn(world, x, currentY, z); + if (allowPlaceholders && line instanceof CraftTextLine) { + PlaceholdersManager.trackIfNecessary((CraftTextLine) line); + } + } + } + + /** + * Called by the PluginHologramManager when the chunk is unloaded. + */ + public void despawnEntities() { + for (CraftHologramLine piece : lines) { + piece.despawn(); + } + } + + @Override + public void teleport(Location location) { + Validator.notNull(location, "location"); + teleport(location.getWorld(), location.getX(), location.getY(), location.getZ()); + } + + @Override + public void teleport(World world, double x, double y, double z) { + Validator.isTrue(!deleted, "hologram already deleted"); + Validator.notNull(world, "world"); + + updateLocation(world, x, y, z); + + if (this.world != world) { + despawnEntities(); + refreshAll(); + return; + } + + double currentY = y; + boolean first = true; + + for (CraftHologramLine line : lines) { + + if (!line.isSpawned()) { + continue; + } + + currentY -= line.getHeight(); + + if (first) { + first = false; + } else { + currentY -= Configuration.spaceBetweenLines; + } + + line.teleport(x, currentY, z); + } + } + + @Override + 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() + * Two holograms can never be equal. Even if they have the same position and the same elements, they are still two different objects. + * The equals and hashcode methods are not overriden, two holograms are equal only if they have the same memory address. + */ + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/CraftVisibilityManager.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/CraftVisibilityManager.java index 32099a89..ef194c5b 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/CraftVisibilityManager.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/CraftVisibilityManager.java @@ -1,171 +1,185 @@ -package com.gmail.filoghost.holographicdisplays.object; - -import java.util.HashSet; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; - -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; - -import com.gmail.filoghost.holographicdisplays.HolographicDisplays; -import com.gmail.filoghost.holographicdisplays.api.VisibilityManager; -import com.gmail.filoghost.holographicdisplays.util.Validator; - -public class CraftVisibilityManager implements VisibilityManager { - - private final CraftHologram hologram; - private Map playersVisibilityMap; - private boolean visibleByDefault; - - private static final int VISIBILITY_DISTANCE_SQUARED = 64 * 64; - - public CraftVisibilityManager(CraftHologram hologram) { - Validator.notNull(hologram, "hologram"); - this.hologram = hologram; - this.visibleByDefault = true; - } - - @Override - public boolean isVisibleByDefault() { - return visibleByDefault; - } - - @Override - public void setVisibleByDefault(boolean visibleByDefault) { - if (this.visibleByDefault != visibleByDefault) { - - boolean oldVisibleByDefault = this.visibleByDefault; - this.visibleByDefault = visibleByDefault; - - for (Player player : Bukkit.getOnlinePlayers()) { - - if (playersVisibilityMap != null && playersVisibilityMap.containsKey(player.getName().toLowerCase())) { - // Has a specific value set - continue; - } - - if (oldVisibleByDefault) { - // If previously was visible, now is NOT visible by default, because the value has changed - sendDestroyPacketIfNear(player, hologram); - } else { - // Opposite case - sendCreatePacketIfNear(player, hologram); - } - } - } - } - - @Override - public void showTo(Player player) { - Validator.notNull(player, "player"); - - boolean wasVisible = isVisibleTo(player); - - if (playersVisibilityMap == null) { - // Lazy initialization - playersVisibilityMap = new ConcurrentHashMap(); - } - - playersVisibilityMap.put(player.getName().toLowerCase(), true); - - if (!wasVisible) { - sendCreatePacketIfNear(player, hologram); - } - } - - - @Override - public void hideTo(Player player) { - Validator.notNull(player, "player"); - - boolean wasVisible = isVisibleTo(player); - - if (playersVisibilityMap == null) { - // Lazy initialization - playersVisibilityMap = new ConcurrentHashMap(); - } - - playersVisibilityMap.put(player.getName().toLowerCase(), false); - - if (wasVisible) { - sendDestroyPacketIfNear(player, hologram); - } - } - - @Override - public boolean isVisibleTo(Player player) { - Validator.notNull(player, "player"); - - if (playersVisibilityMap != null) { - - Boolean value = playersVisibilityMap.get(player.getName().toLowerCase()); - if (value != null) { - return value; - } - } - - return visibleByDefault; - } - - @Override - public void resetVisibility(Player player) { - Validator.notNull(player, "player"); - - if (playersVisibilityMap == null) { - return; - } - - boolean wasVisible = isVisibleTo(player); - - playersVisibilityMap.remove(player.getName().toLowerCase()); - - if (visibleByDefault && !wasVisible) { - sendCreatePacketIfNear(player, hologram); - - } else if (!visibleByDefault && wasVisible) { - sendDestroyPacketIfNear(player, hologram); - } - } - - @Override - public void resetVisibilityAll() { - if (playersVisibilityMap != null) { - - // We need to refresh all the players - Set playerNames = new HashSet(playersVisibilityMap.keySet()); - - for (String playerName : playerNames) { - Player onlinePlayer = Bukkit.getPlayerExact(playerName); - if (onlinePlayer != null) { - resetVisibility(onlinePlayer); - } - } - - playersVisibilityMap.clear(); - playersVisibilityMap = null; - } - } - - private static void sendCreatePacketIfNear(Player player, CraftHologram hologram) { - if (HolographicDisplays.hasProtocolLibHook() && isNear(player, hologram)) { - HolographicDisplays.getProtocolLibHook().sendCreateEntitiesPacket(player, hologram); - } - } - - private static void sendDestroyPacketIfNear(Player player, CraftHologram hologram) { - if (HolographicDisplays.hasProtocolLibHook() && isNear(player, hologram)) { - HolographicDisplays.getProtocolLibHook().sendDestroyEntitiesPacket(player, hologram); - } - } - - private static boolean isNear(Player player, CraftHologram hologram) { - return player.isOnline() && player.getWorld().equals(hologram.getWorld()) && player.getLocation().distanceSquared(hologram.getLocation()) < VISIBILITY_DISTANCE_SQUARED; - } - - @Override - public String toString() { - return "CraftVisibilityManager [playersMap=" + playersVisibilityMap + ", visibleByDefault=" + visibleByDefault + "]"; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.object; + +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +import com.gmail.filoghost.holographicdisplays.HolographicDisplays; +import com.gmail.filoghost.holographicdisplays.api.VisibilityManager; +import com.gmail.filoghost.holographicdisplays.util.Validator; + +public class CraftVisibilityManager implements VisibilityManager { + + private final CraftHologram hologram; + private Map playersVisibilityMap; + private boolean visibleByDefault; + + private static final int VISIBILITY_DISTANCE_SQUARED = 64 * 64; + + public CraftVisibilityManager(CraftHologram hologram) { + Validator.notNull(hologram, "hologram"); + this.hologram = hologram; + this.visibleByDefault = true; + } + + @Override + public boolean isVisibleByDefault() { + return visibleByDefault; + } + + @Override + public void setVisibleByDefault(boolean visibleByDefault) { + if (this.visibleByDefault != visibleByDefault) { + + boolean oldVisibleByDefault = this.visibleByDefault; + this.visibleByDefault = visibleByDefault; + + for (Player player : Bukkit.getOnlinePlayers()) { + + if (playersVisibilityMap != null && playersVisibilityMap.containsKey(player.getName().toLowerCase())) { + // Has a specific value set + continue; + } + + if (oldVisibleByDefault) { + // If previously was visible, now is NOT visible by default, because the value has changed + sendDestroyPacketIfNear(player, hologram); + } else { + // Opposite case + sendCreatePacketIfNear(player, hologram); + } + } + } + } + + @Override + public void showTo(Player player) { + Validator.notNull(player, "player"); + + boolean wasVisible = isVisibleTo(player); + + if (playersVisibilityMap == null) { + // Lazy initialization + playersVisibilityMap = new ConcurrentHashMap(); + } + + playersVisibilityMap.put(player.getName().toLowerCase(), true); + + if (!wasVisible) { + sendCreatePacketIfNear(player, hologram); + } + } + + + @Override + public void hideTo(Player player) { + Validator.notNull(player, "player"); + + boolean wasVisible = isVisibleTo(player); + + if (playersVisibilityMap == null) { + // Lazy initialization + playersVisibilityMap = new ConcurrentHashMap(); + } + + playersVisibilityMap.put(player.getName().toLowerCase(), false); + + if (wasVisible) { + sendDestroyPacketIfNear(player, hologram); + } + } + + @Override + public boolean isVisibleTo(Player player) { + Validator.notNull(player, "player"); + + if (playersVisibilityMap != null) { + + Boolean value = playersVisibilityMap.get(player.getName().toLowerCase()); + if (value != null) { + return value; + } + } + + return visibleByDefault; + } + + @Override + public void resetVisibility(Player player) { + Validator.notNull(player, "player"); + + if (playersVisibilityMap == null) { + return; + } + + boolean wasVisible = isVisibleTo(player); + + playersVisibilityMap.remove(player.getName().toLowerCase()); + + if (visibleByDefault && !wasVisible) { + sendCreatePacketIfNear(player, hologram); + + } else if (!visibleByDefault && wasVisible) { + sendDestroyPacketIfNear(player, hologram); + } + } + + @Override + public void resetVisibilityAll() { + if (playersVisibilityMap != null) { + + // We need to refresh all the players + Set playerNames = new HashSet(playersVisibilityMap.keySet()); + + for (String playerName : playerNames) { + Player onlinePlayer = Bukkit.getPlayerExact(playerName); + if (onlinePlayer != null) { + resetVisibility(onlinePlayer); + } + } + + playersVisibilityMap.clear(); + playersVisibilityMap = null; + } + } + + private static void sendCreatePacketIfNear(Player player, CraftHologram hologram) { + if (HolographicDisplays.hasProtocolLibHook() && isNear(player, hologram)) { + HolographicDisplays.getProtocolLibHook().sendCreateEntitiesPacket(player, hologram); + } + } + + private static void sendDestroyPacketIfNear(Player player, CraftHologram hologram) { + if (HolographicDisplays.hasProtocolLibHook() && isNear(player, hologram)) { + HolographicDisplays.getProtocolLibHook().sendDestroyEntitiesPacket(player, hologram); + } + } + + private static boolean isNear(Player player, CraftHologram hologram) { + return player.isOnline() && player.getWorld().equals(hologram.getWorld()) && player.getLocation().distanceSquared(hologram.getLocation()) < VISIBILITY_DISTANCE_SQUARED; + } + + @Override + public String toString() { + return "CraftVisibilityManager [playersMap=" + playersVisibilityMap + ", visibleByDefault=" + visibleByDefault + "]"; + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/DefaultBackendAPI.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/DefaultBackendAPI.java index 7b4f8c8a..96ea380a 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/DefaultBackendAPI.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/DefaultBackendAPI.java @@ -1,70 +1,84 @@ -package com.gmail.filoghost.holographicdisplays.object; - -import java.util.Collection; - -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.entity.Entity; -import org.bukkit.plugin.Plugin; - -import com.gmail.filoghost.holographicdisplays.HolographicDisplays; -import com.gmail.filoghost.holographicdisplays.api.Hologram; -import com.gmail.filoghost.holographicdisplays.api.internal.BackendAPI; -import com.gmail.filoghost.holographicdisplays.api.placeholder.PlaceholderReplacer; -import com.gmail.filoghost.holographicdisplays.object.PluginHologram; -import com.gmail.filoghost.holographicdisplays.object.PluginHologramManager; -import com.gmail.filoghost.holographicdisplays.placeholder.Placeholder; -import com.gmail.filoghost.holographicdisplays.placeholder.PlaceholdersRegister; -import com.gmail.filoghost.holographicdisplays.util.Validator; - -public class DefaultBackendAPI extends BackendAPI { - - public Hologram createHologram(Plugin plugin, Location source) { - Validator.notNull(plugin, "plugin"); - Validator.notNull(source, "source"); - Validator.notNull(source.getWorld(), "source's world"); - Validator.isTrue(Bukkit.isPrimaryThread(), "Async hologram creation"); - - PluginHologram hologram = new PluginHologram(source, plugin); - PluginHologramManager.addHologram(hologram); - - return hologram; - } - - public boolean registerPlaceholder(Plugin plugin, String textPlaceholder, double refreshRate, PlaceholderReplacer replacer) { - Validator.notNull(textPlaceholder, "textPlaceholder"); - Validator.isTrue(refreshRate >= 0, "refreshRate should be positive"); - Validator.notNull(replacer, "replacer"); - - return PlaceholdersRegister.register(new Placeholder(plugin, textPlaceholder, refreshRate, replacer)); - } - - public boolean isHologramEntity(Entity bukkitEntity) { - Validator.notNull(bukkitEntity, "bukkitEntity"); - return HolographicDisplays.getNMSManager().isNMSEntityBase(bukkitEntity); - } - - public Collection getHolograms(Plugin plugin) { - Validator.notNull(plugin, "plugin"); - return PluginHologramManager.getHolograms(plugin); - } - - public Collection getRegisteredPlaceholders(Plugin plugin) { - Validator.notNull(plugin, "plugin"); - return PlaceholdersRegister.getTextPlaceholdersByPlugin(plugin); - } - - public boolean unregisterPlaceholder(Plugin plugin, String textPlaceholder) { - Validator.notNull(plugin, "plugin"); - Validator.notNull(textPlaceholder, "textPlaceholder"); - return PlaceholdersRegister.unregister(plugin, textPlaceholder); - } - - public void unregisterPlaceholders(Plugin plugin) { - Validator.notNull(plugin, "plugin"); - for (String placeholder : getRegisteredPlaceholders(plugin)) { - unregisterPlaceholder(plugin, placeholder); - } - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.object; + +import java.util.Collection; + +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.entity.Entity; +import org.bukkit.plugin.Plugin; + +import com.gmail.filoghost.holographicdisplays.HolographicDisplays; +import com.gmail.filoghost.holographicdisplays.api.Hologram; +import com.gmail.filoghost.holographicdisplays.api.internal.BackendAPI; +import com.gmail.filoghost.holographicdisplays.api.placeholder.PlaceholderReplacer; +import com.gmail.filoghost.holographicdisplays.object.PluginHologram; +import com.gmail.filoghost.holographicdisplays.object.PluginHologramManager; +import com.gmail.filoghost.holographicdisplays.placeholder.Placeholder; +import com.gmail.filoghost.holographicdisplays.placeholder.PlaceholdersRegister; +import com.gmail.filoghost.holographicdisplays.util.Validator; + +public class DefaultBackendAPI extends BackendAPI { + + public Hologram createHologram(Plugin plugin, Location source) { + Validator.notNull(plugin, "plugin"); + Validator.notNull(source, "source"); + Validator.notNull(source.getWorld(), "source's world"); + Validator.isTrue(Bukkit.isPrimaryThread(), "Async hologram creation"); + + PluginHologram hologram = new PluginHologram(source, plugin); + PluginHologramManager.addHologram(hologram); + + return hologram; + } + + public boolean registerPlaceholder(Plugin plugin, String textPlaceholder, double refreshRate, PlaceholderReplacer replacer) { + Validator.notNull(textPlaceholder, "textPlaceholder"); + Validator.isTrue(refreshRate >= 0, "refreshRate should be positive"); + Validator.notNull(replacer, "replacer"); + + return PlaceholdersRegister.register(new Placeholder(plugin, textPlaceholder, refreshRate, replacer)); + } + + public boolean isHologramEntity(Entity bukkitEntity) { + Validator.notNull(bukkitEntity, "bukkitEntity"); + return HolographicDisplays.getNMSManager().isNMSEntityBase(bukkitEntity); + } + + public Collection getHolograms(Plugin plugin) { + Validator.notNull(plugin, "plugin"); + return PluginHologramManager.getHolograms(plugin); + } + + public Collection getRegisteredPlaceholders(Plugin plugin) { + Validator.notNull(plugin, "plugin"); + return PlaceholdersRegister.getTextPlaceholdersByPlugin(plugin); + } + + public boolean unregisterPlaceholder(Plugin plugin, String textPlaceholder) { + Validator.notNull(plugin, "plugin"); + Validator.notNull(textPlaceholder, "textPlaceholder"); + return PlaceholdersRegister.unregister(plugin, textPlaceholder); + } + + public void unregisterPlaceholders(Plugin plugin) { + Validator.notNull(plugin, "plugin"); + for (String placeholder : getRegisteredPlaceholders(plugin)) { + unregisterPlaceholder(plugin, placeholder); + } + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/NamedHologram.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/NamedHologram.java index 370833f7..934cf2cf 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/NamedHologram.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/NamedHologram.java @@ -1,30 +1,44 @@ -package com.gmail.filoghost.holographicdisplays.object; - -import org.bukkit.Location; - -public class NamedHologram extends CraftHologram { - - private final String name; - - public NamedHologram(Location source, String name) { - super(source); - this.name = name; - setAllowPlaceholders(true); - } - - public String getName() { - return name; - } - - @Override - public void delete() { - super.delete(); - NamedHologramManager.removeHologram(this); - } - - @Override - public String toString() { - return "NamedHologram [name=" + name + ", super=" + super.toString() + "]"; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.object; + +import org.bukkit.Location; + +public class NamedHologram extends CraftHologram { + + private final String name; + + public NamedHologram(Location source, String name) { + super(source); + this.name = name; + setAllowPlaceholders(true); + } + + public String getName() { + return name; + } + + @Override + public void delete() { + super.delete(); + NamedHologramManager.removeHologram(this); + } + + @Override + public String toString() { + return "NamedHologram [name=" + name + ", super=" + super.toString() + "]"; + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/NamedHologramManager.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/NamedHologramManager.java index 6bb5dc3e..61c2af2d 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/NamedHologramManager.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/NamedHologramManager.java @@ -1,79 +1,93 @@ -package com.gmail.filoghost.holographicdisplays.object; - -import java.util.ArrayList; -import java.util.List; - -import org.bukkit.Chunk; - -import com.gmail.filoghost.holographicdisplays.util.Utils; - -/** - * This class is only used by the plugin itself. Other plugins should just use the API. - */ -public class NamedHologramManager { - - private static List pluginHolograms = Utils.newList(); - - public static void addHologram(NamedHologram hologram) { - pluginHolograms.add(hologram); - } - - public static void removeHologram(NamedHologram hologram) { - pluginHolograms.remove(hologram); - if (!hologram.isDeleted()) { - hologram.delete(); - } - } - - public static List getHolograms() { - return new ArrayList(pluginHolograms); - } - - public static NamedHologram getHologram(String name) { - for (NamedHologram hologram : pluginHolograms) { - if (hologram.getName().equals(name)) { - return hologram; - } - } - return null; - } - - public static boolean isExistingHologram(String name) { - return (getHologram(name) != null); - } - - public static void onChunkLoad(Chunk chunk) { - // Load the holograms in that chunk. - for (NamedHologram hologram : pluginHolograms) { - if (hologram.isInChunk(chunk)) { - hologram.spawnEntities(); - } - } - } - - public static void onChunkUnload(Chunk chunk) { - // Hide the holograms in that chunk. - for (NamedHologram hologram : pluginHolograms) { - if (hologram.isInChunk(chunk)) { - hologram.despawnEntities(); - } - } - } - - public static void clearAll() { - List oldHolograms = new ArrayList(pluginHolograms); - pluginHolograms.clear(); - - for (NamedHologram hologram : oldHolograms) { - hologram.delete(); - } - } - - public static int size() { - return pluginHolograms.size(); - } - - public static NamedHologram get(int i) { - return pluginHolograms.get(i); - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.object; + +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.Chunk; + +import com.gmail.filoghost.holographicdisplays.util.Utils; + +/** + * This class is only used by the plugin itself. Other plugins should just use the API. + */ +public class NamedHologramManager { + + private static List pluginHolograms = Utils.newList(); + + public static void addHologram(NamedHologram hologram) { + pluginHolograms.add(hologram); + } + + public static void removeHologram(NamedHologram hologram) { + pluginHolograms.remove(hologram); + if (!hologram.isDeleted()) { + hologram.delete(); + } + } + + public static List getHolograms() { + return new ArrayList(pluginHolograms); + } + + public static NamedHologram getHologram(String name) { + for (NamedHologram hologram : pluginHolograms) { + if (hologram.getName().equals(name)) { + return hologram; + } + } + return null; + } + + public static boolean isExistingHologram(String name) { + return (getHologram(name) != null); + } + + public static void onChunkLoad(Chunk chunk) { + // Load the holograms in that chunk. + for (NamedHologram hologram : pluginHolograms) { + if (hologram.isInChunk(chunk)) { + hologram.spawnEntities(); + } + } + } + + public static void onChunkUnload(Chunk chunk) { + // Hide the holograms in that chunk. + for (NamedHologram hologram : pluginHolograms) { + if (hologram.isInChunk(chunk)) { + hologram.despawnEntities(); + } + } + } + + public static void clearAll() { + List oldHolograms = new ArrayList(pluginHolograms); + pluginHolograms.clear(); + + for (NamedHologram hologram : oldHolograms) { + hologram.delete(); + } + } + + public static int size() { + return pluginHolograms.size(); + } + + public static NamedHologram get(int i) { + return pluginHolograms.get(i); + } +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/PluginHologram.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/PluginHologram.java index 67a89f21..54e77522 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/PluginHologram.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/PluginHologram.java @@ -1,31 +1,45 @@ -package com.gmail.filoghost.holographicdisplays.object; - -import org.bukkit.Location; -import org.bukkit.plugin.Plugin; - -import com.gmail.filoghost.holographicdisplays.util.Validator; - -/** - * This class is only used by the plugin itself. Do not attempt to use it. - */ -public class PluginHologram extends CraftHologram { - - private Plugin plugin; - - public PluginHologram(Location source, Plugin plugin) { - super(source); - Validator.notNull(plugin, "plugin"); - this.plugin = plugin; - } - - public Plugin getOwner() { - return plugin; - } - - @Override - public void delete() { - super.delete(); - PluginHologramManager.removeHologram(this); - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.object; + +import org.bukkit.Location; +import org.bukkit.plugin.Plugin; + +import com.gmail.filoghost.holographicdisplays.util.Validator; + +/** + * This class is only used by the plugin itself. Do not attempt to use it. + */ +public class PluginHologram extends CraftHologram { + + private Plugin plugin; + + public PluginHologram(Location source, Plugin plugin) { + super(source); + Validator.notNull(plugin, "plugin"); + this.plugin = plugin; + } + + public Plugin getOwner() { + return plugin; + } + + @Override + public void delete() { + super.delete(); + PluginHologramManager.removeHologram(this); + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/PluginHologramManager.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/PluginHologramManager.java index ce5540ab..79ad6015 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/PluginHologramManager.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/PluginHologramManager.java @@ -1,74 +1,88 @@ -package com.gmail.filoghost.holographicdisplays.object; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Set; - -import org.bukkit.Chunk; -import org.bukkit.plugin.Plugin; - -import com.gmail.filoghost.holographicdisplays.api.Hologram; -import com.gmail.filoghost.holographicdisplays.util.Utils; - -/** - * This class is only used by the plugin itself. Other plugins should just use the API. - */ -public class PluginHologramManager { - - private static List pluginHolograms = Utils.newList(); - - public static void addHologram(PluginHologram hologram) { - pluginHolograms.add(hologram); - } - - public static void removeHologram(PluginHologram hologram) { - pluginHolograms.remove(hologram); - if (!hologram.isDeleted()) { - hologram.delete(); - } - } - - public static List getHolograms() { - return new ArrayList(pluginHolograms); - } - - public static Set getHolograms(Plugin plugin) { - Set ownedHolograms = Utils.newSet(); - - for (PluginHologram hologram : pluginHolograms) { - if (hologram.getOwner().equals(plugin)) { - ownedHolograms.add(hologram); - } - } - - return Collections.unmodifiableSet(ownedHolograms); - } - - public static void onChunkLoad(Chunk chunk) { - // Load the holograms in that chunk. - for (PluginHologram hologram : pluginHolograms) { - if (hologram.isInChunk(chunk)) { - hologram.spawnEntities(); - } - } - } - - public static void onChunkUnload(Chunk chunk) { - // Hide the holograms in that chunk. - for (PluginHologram hologram : pluginHolograms) { - if (hologram.isInChunk(chunk)) { - hologram.despawnEntities(); - } - } - } - - public static void clearAll() { - List oldHolograms = new ArrayList(pluginHolograms); - pluginHolograms.clear(); - - for (PluginHologram hologram : oldHolograms) { - hologram.delete(); - } - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.object; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Set; + +import org.bukkit.Chunk; +import org.bukkit.plugin.Plugin; + +import com.gmail.filoghost.holographicdisplays.api.Hologram; +import com.gmail.filoghost.holographicdisplays.util.Utils; + +/** + * This class is only used by the plugin itself. Other plugins should just use the API. + */ +public class PluginHologramManager { + + private static List pluginHolograms = Utils.newList(); + + public static void addHologram(PluginHologram hologram) { + pluginHolograms.add(hologram); + } + + public static void removeHologram(PluginHologram hologram) { + pluginHolograms.remove(hologram); + if (!hologram.isDeleted()) { + hologram.delete(); + } + } + + public static List getHolograms() { + return new ArrayList(pluginHolograms); + } + + public static Set getHolograms(Plugin plugin) { + Set ownedHolograms = Utils.newSet(); + + for (PluginHologram hologram : pluginHolograms) { + if (hologram.getOwner().equals(plugin)) { + ownedHolograms.add(hologram); + } + } + + return Collections.unmodifiableSet(ownedHolograms); + } + + public static void onChunkLoad(Chunk chunk) { + // Load the holograms in that chunk. + for (PluginHologram hologram : pluginHolograms) { + if (hologram.isInChunk(chunk)) { + hologram.spawnEntities(); + } + } + } + + public static void onChunkUnload(Chunk chunk) { + // Hide the holograms in that chunk. + for (PluginHologram hologram : pluginHolograms) { + if (hologram.isInChunk(chunk)) { + hologram.despawnEntities(); + } + } + } + + public static void clearAll() { + List oldHolograms = new ArrayList(pluginHolograms); + pluginHolograms.clear(); + + for (PluginHologram hologram : oldHolograms) { + hologram.delete(); + } + } +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/line/CraftHologramLine.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/line/CraftHologramLine.java index 50b1e34d..dc62b115 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/line/CraftHologramLine.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/line/CraftHologramLine.java @@ -1,58 +1,72 @@ -package com.gmail.filoghost.holographicdisplays.object.line; - -import org.bukkit.World; - -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; -import com.gmail.filoghost.holographicdisplays.object.CraftHologram; -import com.gmail.filoghost.holographicdisplays.util.Validator; - -public abstract class CraftHologramLine implements HologramLine { - - private final double height; - private final CraftHologram parent; - - // This field is necessary for teleport. - private boolean isSpawned; - - protected CraftHologramLine(double height, CraftHologram parent) { - Validator.notNull(parent, "parent hologram"); - this.height = height; - this.parent = parent; - } - - public final double getHeight() { - return height; - } - - @Override - public final CraftHologram getParent() { - return parent; - } - - public void removeLine() { - parent.removeLine(this); - } - - public void spawn(World world, double x, double y, double z) { - Validator.notNull(world, "world"); - - // Remove the old entities when spawning the new ones. - despawn(); - isSpawned = true; - - // Do nothing, there are no entities in this class. - } - - public void despawn() { - isSpawned = false; - } - - public final boolean isSpawned() { - return isSpawned; - } - - public abstract int[] getEntitiesIDs(); - - public abstract void teleport(double x, double y, double z); - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.object.line; + +import org.bukkit.World; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; +import com.gmail.filoghost.holographicdisplays.object.CraftHologram; +import com.gmail.filoghost.holographicdisplays.util.Validator; + +public abstract class CraftHologramLine implements HologramLine { + + private final double height; + private final CraftHologram parent; + + // This field is necessary for teleport. + private boolean isSpawned; + + protected CraftHologramLine(double height, CraftHologram parent) { + Validator.notNull(parent, "parent hologram"); + this.height = height; + this.parent = parent; + } + + public final double getHeight() { + return height; + } + + @Override + public final CraftHologram getParent() { + return parent; + } + + public void removeLine() { + parent.removeLine(this); + } + + public void spawn(World world, double x, double y, double z) { + Validator.notNull(world, "world"); + + // Remove the old entities when spawning the new ones. + despawn(); + isSpawned = true; + + // Do nothing, there are no entities in this class. + } + + public void despawn() { + isSpawned = false; + } + + public final boolean isSpawned() { + return isSpawned; + } + + public abstract int[] getEntitiesIDs(); + + public abstract void teleport(double x, double y, double z); + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/line/CraftItemLine.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/line/CraftItemLine.java index 3bb316de..33f3a2c5 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/line/CraftItemLine.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/line/CraftItemLine.java @@ -1,144 +1,158 @@ -package com.gmail.filoghost.holographicdisplays.object.line; - -import org.apache.commons.lang.ArrayUtils; -import org.bukkit.Location; -import org.bukkit.World; -import org.bukkit.inventory.ItemStack; - -import com.gmail.filoghost.holographicdisplays.HolographicDisplays; -import com.gmail.filoghost.holographicdisplays.api.handler.PickupHandler; -import com.gmail.filoghost.holographicdisplays.api.handler.TouchHandler; -import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; -import com.gmail.filoghost.holographicdisplays.object.CraftHologram; -import com.gmail.filoghost.holographicdisplays.util.Offsets; -import com.gmail.filoghost.holographicdisplays.util.Validator; - -public class CraftItemLine extends CraftTouchableLine implements ItemLine { - - private ItemStack itemStack; - private PickupHandler pickupHandler; - - private NMSItem nmsItem; - private NMSEntityBase nmsVehicle; - - public CraftItemLine(CraftHologram parent, ItemStack itemStack) { - super(0.7, parent); - setItemStack(itemStack); - } - - @Override - public ItemStack getItemStack() { - return itemStack; - } - - @Override - public void setItemStack(ItemStack itemStack) { - Validator.notNull(itemStack, "itemStack"); - this.itemStack = itemStack; - - if (nmsItem != null) { - nmsItem.setItemStackNMS(itemStack); - } - } - - @Override - public PickupHandler getPickupHandler() { - return pickupHandler; - } - - @Override - public void setPickupHandler(PickupHandler pickupHandler) { - this.pickupHandler = pickupHandler; - } - - @Override - public void setTouchHandler(TouchHandler touchHandler) { - if (nmsItem != null) { - Location loc = nmsItem.getBukkitEntityNMS().getLocation(); - super.setTouchHandler(touchHandler, loc.getWorld(), loc.getX(), loc.getY() - getItemOffset(), loc.getZ()); - } else { - super.setTouchHandler(touchHandler, null, 0, 0, 0); - } - } - - @Override - public void spawn(World world, double x, double y, double z) { - super.spawn(world, x, y, z); - - if (itemStack != null) { - double offset = getItemOffset(); - - nmsItem = HolographicDisplays.getNMSManager().spawnNMSItem(world, x, y + offset, z, this, itemStack, HolographicDisplays.getMainListener()); - nmsVehicle = HolographicDisplays.getNMSManager().spawnNMSArmorStand(world, x, y + offset, z, this); - - nmsItem.setPassengerOfNMS(nmsVehicle); - - nmsItem.setLockTick(true); - nmsVehicle.setLockTick(true); - } - } - - - @Override - public void despawn() { - super.despawn(); - - if (nmsVehicle != null) { - nmsVehicle.killEntityNMS(); - nmsVehicle = null; - } - - if (nmsItem != null) { - nmsItem.killEntityNMS(); - nmsItem = null; - } - } - - @Override - public void teleport(double x, double y, double z) { - super.teleport(x, y, z); - - double offset = getItemOffset(); - - if (nmsVehicle != null) { - nmsVehicle.setLocationNMS(x, y + offset, z); - } - - if (nmsItem != null) { - nmsItem.setLocationNMS(x, y + offset, z); - } - } - - @Override - public int[] getEntitiesIDs() { - if (isSpawned()) { - if (touchSlime != null) { - return ArrayUtils.addAll(new int[] {nmsVehicle.getIdNMS(), nmsItem.getIdNMS()}, touchSlime.getEntitiesIDs()); - } else { - return new int[] {nmsVehicle.getIdNMS(), nmsItem.getIdNMS()}; - } - } else { - return new int[0]; - } - } - - public NMSItem getNmsItem() { - return nmsItem; - } - - public NMSEntityBase getNmsVehicle() { - return nmsVehicle; - } - - private double getItemOffset() { - return Offsets.ARMOR_STAND_WITH_ITEM; - } - - @Override - public String toString() { - return "CraftItemLine [itemStack=" + itemStack + ", pickupHandler=" + pickupHandler + "]"; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.object.line; + +import org.apache.commons.lang.ArrayUtils; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.inventory.ItemStack; + +import com.gmail.filoghost.holographicdisplays.HolographicDisplays; +import com.gmail.filoghost.holographicdisplays.api.handler.PickupHandler; +import com.gmail.filoghost.holographicdisplays.api.handler.TouchHandler; +import com.gmail.filoghost.holographicdisplays.api.line.ItemLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSItem; +import com.gmail.filoghost.holographicdisplays.object.CraftHologram; +import com.gmail.filoghost.holographicdisplays.util.Offsets; +import com.gmail.filoghost.holographicdisplays.util.Validator; + +public class CraftItemLine extends CraftTouchableLine implements ItemLine { + + private ItemStack itemStack; + private PickupHandler pickupHandler; + + private NMSItem nmsItem; + private NMSEntityBase nmsVehicle; + + public CraftItemLine(CraftHologram parent, ItemStack itemStack) { + super(0.7, parent); + setItemStack(itemStack); + } + + @Override + public ItemStack getItemStack() { + return itemStack; + } + + @Override + public void setItemStack(ItemStack itemStack) { + Validator.notNull(itemStack, "itemStack"); + this.itemStack = itemStack; + + if (nmsItem != null) { + nmsItem.setItemStackNMS(itemStack); + } + } + + @Override + public PickupHandler getPickupHandler() { + return pickupHandler; + } + + @Override + public void setPickupHandler(PickupHandler pickupHandler) { + this.pickupHandler = pickupHandler; + } + + @Override + public void setTouchHandler(TouchHandler touchHandler) { + if (nmsItem != null) { + Location loc = nmsItem.getBukkitEntityNMS().getLocation(); + super.setTouchHandler(touchHandler, loc.getWorld(), loc.getX(), loc.getY() - getItemOffset(), loc.getZ()); + } else { + super.setTouchHandler(touchHandler, null, 0, 0, 0); + } + } + + @Override + public void spawn(World world, double x, double y, double z) { + super.spawn(world, x, y, z); + + if (itemStack != null) { + double offset = getItemOffset(); + + nmsItem = HolographicDisplays.getNMSManager().spawnNMSItem(world, x, y + offset, z, this, itemStack, HolographicDisplays.getMainListener()); + nmsVehicle = HolographicDisplays.getNMSManager().spawnNMSArmorStand(world, x, y + offset, z, this); + + nmsItem.setPassengerOfNMS(nmsVehicle); + + nmsItem.setLockTick(true); + nmsVehicle.setLockTick(true); + } + } + + + @Override + public void despawn() { + super.despawn(); + + if (nmsVehicle != null) { + nmsVehicle.killEntityNMS(); + nmsVehicle = null; + } + + if (nmsItem != null) { + nmsItem.killEntityNMS(); + nmsItem = null; + } + } + + @Override + public void teleport(double x, double y, double z) { + super.teleport(x, y, z); + + double offset = getItemOffset(); + + if (nmsVehicle != null) { + nmsVehicle.setLocationNMS(x, y + offset, z); + } + + if (nmsItem != null) { + nmsItem.setLocationNMS(x, y + offset, z); + } + } + + @Override + public int[] getEntitiesIDs() { + if (isSpawned()) { + if (touchSlime != null) { + return ArrayUtils.addAll(new int[] {nmsVehicle.getIdNMS(), nmsItem.getIdNMS()}, touchSlime.getEntitiesIDs()); + } else { + return new int[] {nmsVehicle.getIdNMS(), nmsItem.getIdNMS()}; + } + } else { + return new int[0]; + } + } + + public NMSItem getNmsItem() { + return nmsItem; + } + + public NMSEntityBase getNmsVehicle() { + return nmsVehicle; + } + + private double getItemOffset() { + return Offsets.ARMOR_STAND_WITH_ITEM; + } + + @Override + public String toString() { + return "CraftItemLine [itemStack=" + itemStack + ", pickupHandler=" + pickupHandler + "]"; + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/line/CraftTextLine.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/line/CraftTextLine.java index 54ff894f..79dfe8f7 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/line/CraftTextLine.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/line/CraftTextLine.java @@ -1,124 +1,138 @@ -package com.gmail.filoghost.holographicdisplays.object.line; - -import org.apache.commons.lang.ArrayUtils; -import org.bukkit.Location; -import org.bukkit.World; - -import com.gmail.filoghost.holographicdisplays.HolographicDisplays; -import com.gmail.filoghost.holographicdisplays.api.handler.TouchHandler; -import com.gmail.filoghost.holographicdisplays.api.line.TextLine; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSNameable; -import com.gmail.filoghost.holographicdisplays.object.CraftHologram; -import com.gmail.filoghost.holographicdisplays.placeholder.PlaceholdersManager; -import com.gmail.filoghost.holographicdisplays.util.Offsets; - -public class CraftTextLine extends CraftTouchableLine implements TextLine { - - private String text; - private NMSNameable nmsNameble; - - - public CraftTextLine(CraftHologram parent, String text) { - super(0.23, parent); - setText(text); - } - - - @Override - public String getText() { - return text; - } - - @Override - public void setText(String text) { - this.text = text; - - if (nmsNameble != null) { - if (text != null && !text.isEmpty()) { - nmsNameble.setCustomNameNMS(text); - if (getParent().isAllowPlaceholders()) { - PlaceholdersManager.trackIfNecessary(this); - } - } else { - nmsNameble.setCustomNameNMS(""); // It will not appear - if (getParent().isAllowPlaceholders()) { - PlaceholdersManager.untrack(this); - } - } - } - } - - @Override - public void setTouchHandler(TouchHandler touchHandler) { - - if (nmsNameble != null) { - - Location loc = nmsNameble.getBukkitEntityNMS().getLocation(); - super.setTouchHandler(touchHandler, loc.getWorld(), loc.getX(), loc.getY() - getTextOffset(), loc.getZ()); - - } else { - super.setTouchHandler(touchHandler, null, 0, 0, 0); - } - } - - @Override - public void spawn(World world, double x, double y, double z) { - super.spawn(world, x, y, z); - - nmsNameble = HolographicDisplays.getNMSManager().spawnNMSArmorStand(world, x, y + getTextOffset(), z, this); - - if (text != null && !text.isEmpty()) { - nmsNameble.setCustomNameNMS(text); - } - - nmsNameble.setLockTick(true); - } - - - @Override - public void despawn() { - super.despawn(); - - if (nmsNameble != null) { - nmsNameble.killEntityNMS(); - nmsNameble = null; - } - } - - - @Override - public void teleport(double x, double y, double z) { - super.teleport(x, y, z); - - if (nmsNameble != null) { - nmsNameble.setLocationNMS(x, y + getTextOffset(), z); - } - } - - @Override - public int[] getEntitiesIDs() { - if (isSpawned()) { - if (touchSlime != null) { - return ArrayUtils.add(touchSlime.getEntitiesIDs(), nmsNameble.getIdNMS()); - } else { - return new int[] {nmsNameble.getIdNMS()}; - } - } else { - return new int[0]; - } - } - - public NMSNameable getNmsNameble() { - return nmsNameble; - } - - private double getTextOffset() { - return Offsets.ARMOR_STAND_ALONE; - } - - @Override - public String toString() { - return "CraftTextLine [text=" + text + "]"; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.object.line; + +import org.apache.commons.lang.ArrayUtils; +import org.bukkit.Location; +import org.bukkit.World; + +import com.gmail.filoghost.holographicdisplays.HolographicDisplays; +import com.gmail.filoghost.holographicdisplays.api.handler.TouchHandler; +import com.gmail.filoghost.holographicdisplays.api.line.TextLine; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSNameable; +import com.gmail.filoghost.holographicdisplays.object.CraftHologram; +import com.gmail.filoghost.holographicdisplays.placeholder.PlaceholdersManager; +import com.gmail.filoghost.holographicdisplays.util.Offsets; + +public class CraftTextLine extends CraftTouchableLine implements TextLine { + + private String text; + private NMSNameable nmsNameble; + + + public CraftTextLine(CraftHologram parent, String text) { + super(0.23, parent); + setText(text); + } + + + @Override + public String getText() { + return text; + } + + @Override + public void setText(String text) { + this.text = text; + + if (nmsNameble != null) { + if (text != null && !text.isEmpty()) { + nmsNameble.setCustomNameNMS(text); + if (getParent().isAllowPlaceholders()) { + PlaceholdersManager.trackIfNecessary(this); + } + } else { + nmsNameble.setCustomNameNMS(""); // It will not appear + if (getParent().isAllowPlaceholders()) { + PlaceholdersManager.untrack(this); + } + } + } + } + + @Override + public void setTouchHandler(TouchHandler touchHandler) { + + if (nmsNameble != null) { + + Location loc = nmsNameble.getBukkitEntityNMS().getLocation(); + super.setTouchHandler(touchHandler, loc.getWorld(), loc.getX(), loc.getY() - getTextOffset(), loc.getZ()); + + } else { + super.setTouchHandler(touchHandler, null, 0, 0, 0); + } + } + + @Override + public void spawn(World world, double x, double y, double z) { + super.spawn(world, x, y, z); + + nmsNameble = HolographicDisplays.getNMSManager().spawnNMSArmorStand(world, x, y + getTextOffset(), z, this); + + if (text != null && !text.isEmpty()) { + nmsNameble.setCustomNameNMS(text); + } + + nmsNameble.setLockTick(true); + } + + + @Override + public void despawn() { + super.despawn(); + + if (nmsNameble != null) { + nmsNameble.killEntityNMS(); + nmsNameble = null; + } + } + + + @Override + public void teleport(double x, double y, double z) { + super.teleport(x, y, z); + + if (nmsNameble != null) { + nmsNameble.setLocationNMS(x, y + getTextOffset(), z); + } + } + + @Override + public int[] getEntitiesIDs() { + if (isSpawned()) { + if (touchSlime != null) { + return ArrayUtils.add(touchSlime.getEntitiesIDs(), nmsNameble.getIdNMS()); + } else { + return new int[] {nmsNameble.getIdNMS()}; + } + } else { + return new int[0]; + } + } + + public NMSNameable getNmsNameble() { + return nmsNameble; + } + + private double getTextOffset() { + return Offsets.ARMOR_STAND_ALONE; + } + + @Override + public String toString() { + return "CraftTextLine [text=" + text + "]"; + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/line/CraftTouchSlimeLine.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/line/CraftTouchSlimeLine.java index e0a3fd23..d7a64808 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/line/CraftTouchSlimeLine.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/line/CraftTouchSlimeLine.java @@ -1,105 +1,119 @@ -package com.gmail.filoghost.holographicdisplays.object.line; - -import org.bukkit.World; - -import com.gmail.filoghost.holographicdisplays.HolographicDisplays; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSSlime; -import com.gmail.filoghost.holographicdisplays.object.CraftHologram; -import com.gmail.filoghost.holographicdisplays.util.Offsets; - -/** - * A touch slime that can be applied to a line. - */ -public class CraftTouchSlimeLine extends CraftHologramLine { - - // The touchable piece associated with this piece - private CraftTouchableLine touchablePiece; - - private NMSSlime nmsSlime; - private NMSEntityBase nmsVehicle; - - - protected CraftTouchSlimeLine(CraftHologram parent, CraftTouchableLine touchablePiece) { - super(0.5, parent); - this.touchablePiece = touchablePiece; - } - - public CraftTouchableLine getTouchablePiece() { - return touchablePiece; - } - - - @Override - public void spawn(World world, double x, double y, double z) { - super.spawn(world, x, y, z); - - double offset = getSlimeOffset(); - - nmsSlime = HolographicDisplays.getNMSManager().spawnNMSSlime(world, x, y + offset, z, this); - nmsVehicle = HolographicDisplays.getNMSManager().spawnNMSArmorStand(world, x, y + offset, z, this); - - nmsSlime.setPassengerOfNMS(nmsVehicle); - - nmsSlime.setLockTick(true); - nmsVehicle.setLockTick(true); - } - - - @Override - public void despawn() { - super.despawn(); - - if (nmsSlime != null) { - nmsSlime.killEntityNMS(); - nmsSlime = null; - } - - if (nmsVehicle != null) { - nmsVehicle.killEntityNMS(); - nmsVehicle = null; - } - } - - - @Override - public void teleport(double x, double y, double z) { - - double offset = getSlimeOffset(); - - if (nmsVehicle != null) { - nmsVehicle.setLocationNMS(x, y + offset, z); - } - - if (nmsSlime != null) { - nmsSlime.setLocationNMS(x, y + offset, z); - } - } - - @Override - public int[] getEntitiesIDs() { - if (isSpawned()) { - return new int[] {nmsVehicle.getIdNMS(), nmsSlime.getIdNMS()}; - } else { - return new int[0]; - } - } - - public NMSSlime getNmsSlime() { - return nmsSlime; - } - - public NMSEntityBase getNmsVehicle() { - return nmsVehicle; - } - - private double getSlimeOffset() { - return Offsets.ARMOR_STAND_WITH_SLIME; - } - - @Override - public String toString() { - return "CraftTouchSlimeLine [touchablePiece=" + touchablePiece + "]"; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.object.line; + +import org.bukkit.World; + +import com.gmail.filoghost.holographicdisplays.HolographicDisplays; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSEntityBase; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSSlime; +import com.gmail.filoghost.holographicdisplays.object.CraftHologram; +import com.gmail.filoghost.holographicdisplays.util.Offsets; + +/** + * A touch slime that can be applied to a line. + */ +public class CraftTouchSlimeLine extends CraftHologramLine { + + // The touchable piece associated with this piece + private CraftTouchableLine touchablePiece; + + private NMSSlime nmsSlime; + private NMSEntityBase nmsVehicle; + + + protected CraftTouchSlimeLine(CraftHologram parent, CraftTouchableLine touchablePiece) { + super(0.5, parent); + this.touchablePiece = touchablePiece; + } + + public CraftTouchableLine getTouchablePiece() { + return touchablePiece; + } + + + @Override + public void spawn(World world, double x, double y, double z) { + super.spawn(world, x, y, z); + + double offset = getSlimeOffset(); + + nmsSlime = HolographicDisplays.getNMSManager().spawnNMSSlime(world, x, y + offset, z, this); + nmsVehicle = HolographicDisplays.getNMSManager().spawnNMSArmorStand(world, x, y + offset, z, this); + + nmsSlime.setPassengerOfNMS(nmsVehicle); + + nmsSlime.setLockTick(true); + nmsVehicle.setLockTick(true); + } + + + @Override + public void despawn() { + super.despawn(); + + if (nmsSlime != null) { + nmsSlime.killEntityNMS(); + nmsSlime = null; + } + + if (nmsVehicle != null) { + nmsVehicle.killEntityNMS(); + nmsVehicle = null; + } + } + + + @Override + public void teleport(double x, double y, double z) { + + double offset = getSlimeOffset(); + + if (nmsVehicle != null) { + nmsVehicle.setLocationNMS(x, y + offset, z); + } + + if (nmsSlime != null) { + nmsSlime.setLocationNMS(x, y + offset, z); + } + } + + @Override + public int[] getEntitiesIDs() { + if (isSpawned()) { + return new int[] {nmsVehicle.getIdNMS(), nmsSlime.getIdNMS()}; + } else { + return new int[0]; + } + } + + public NMSSlime getNmsSlime() { + return nmsSlime; + } + + public NMSEntityBase getNmsVehicle() { + return nmsVehicle; + } + + private double getSlimeOffset() { + return Offsets.ARMOR_STAND_WITH_SLIME; + } + + @Override + public String toString() { + return "CraftTouchSlimeLine [touchablePiece=" + touchablePiece + "]"; + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/line/CraftTouchableLine.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/line/CraftTouchableLine.java index 088e5a5b..92099866 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/line/CraftTouchableLine.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/object/line/CraftTouchableLine.java @@ -1,78 +1,92 @@ -package com.gmail.filoghost.holographicdisplays.object.line; - -import org.bukkit.World; - -import com.gmail.filoghost.holographicdisplays.api.handler.TouchHandler; -import com.gmail.filoghost.holographicdisplays.object.CraftHologram; - -/** - * Useful class that implements TouchablePiece. The downside is that subclasses must extend this, and cannot extend other classes. - * But all the current items are touchable. - */ -public abstract class CraftTouchableLine extends CraftHologramLine { - - protected CraftTouchSlimeLine touchSlime; - private TouchHandler touchHandler; - - - protected CraftTouchableLine(double height, CraftHologram parent) { - super(height, parent); - } - - - protected void setTouchHandler(TouchHandler touchHandler, World world, double x, double y, double z) { - this.touchHandler = touchHandler; - - if (touchHandler != null && touchSlime == null && world != null) { - // If the touch handler was null before and no entity has been spawned, spawn it now. - touchSlime = new CraftTouchSlimeLine(getParent(), this); - touchSlime.spawn(world, x, y + (getHeight() / 2.0 - touchSlime.getHeight() / 2.0), z); - - } else if (touchHandler == null && touchSlime != null) { - // Opposite case, the touch handler was not null and an entity was spawned, but now it's useless. - touchSlime.despawn(); - touchSlime = null; - } - } - - - public TouchHandler getTouchHandler() { - return this.touchHandler; - } - - - @Override - public void spawn(World world, double x, double y, double z) { - super.spawn(world, x, y, z); - - if (touchHandler != null) { - touchSlime = new CraftTouchSlimeLine(getParent(), this); - touchSlime.spawn(world, x, y + (getHeight() / 2.0 - touchSlime.getHeight() / 2.0), z); - } - } - - - @Override - public void despawn() { - super.despawn(); - - if (touchSlime != null) { - touchSlime.despawn(); - touchSlime = null; - } - } - - - @Override - public void teleport(double x, double y, double z) { - if (touchSlime != null) { - touchSlime.teleport(x, y + (getHeight() / 2.0 - touchSlime.getHeight() / 2.0), z); - } - } - - - public CraftTouchSlimeLine getTouchSlime() { - return touchSlime; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.object.line; + +import org.bukkit.World; + +import com.gmail.filoghost.holographicdisplays.api.handler.TouchHandler; +import com.gmail.filoghost.holographicdisplays.object.CraftHologram; + +/** + * Useful class that implements TouchablePiece. The downside is that subclasses must extend this, and cannot extend other classes. + * But all the current items are touchable. + */ +public abstract class CraftTouchableLine extends CraftHologramLine { + + protected CraftTouchSlimeLine touchSlime; + private TouchHandler touchHandler; + + + protected CraftTouchableLine(double height, CraftHologram parent) { + super(height, parent); + } + + + protected void setTouchHandler(TouchHandler touchHandler, World world, double x, double y, double z) { + this.touchHandler = touchHandler; + + if (touchHandler != null && touchSlime == null && world != null) { + // If the touch handler was null before and no entity has been spawned, spawn it now. + touchSlime = new CraftTouchSlimeLine(getParent(), this); + touchSlime.spawn(world, x, y + (getHeight() / 2.0 - touchSlime.getHeight() / 2.0), z); + + } else if (touchHandler == null && touchSlime != null) { + // Opposite case, the touch handler was not null and an entity was spawned, but now it's useless. + touchSlime.despawn(); + touchSlime = null; + } + } + + + public TouchHandler getTouchHandler() { + return this.touchHandler; + } + + + @Override + public void spawn(World world, double x, double y, double z) { + super.spawn(world, x, y, z); + + if (touchHandler != null) { + touchSlime = new CraftTouchSlimeLine(getParent(), this); + touchSlime.spawn(world, x, y + (getHeight() / 2.0 - touchSlime.getHeight() / 2.0), z); + } + } + + + @Override + public void despawn() { + super.despawn(); + + if (touchSlime != null) { + touchSlime.despawn(); + touchSlime = null; + } + } + + + @Override + public void teleport(double x, double y, double z) { + if (touchSlime != null) { + touchSlime.teleport(x, y + (getHeight() / 2.0 - touchSlime.getHeight() / 2.0), z); + } + } + + + public CraftTouchSlimeLine getTouchSlime() { + return touchSlime; + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/placeholder/AnimationsRegister.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/placeholder/AnimationsRegister.java index 57ddd7ac..9c4c6926 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/placeholder/AnimationsRegister.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/placeholder/AnimationsRegister.java @@ -1,88 +1,102 @@ -package com.gmail.filoghost.holographicdisplays.placeholder; - -import java.io.File; -import java.util.List; -import java.util.Map; -import java.util.logging.Level; - -import org.bukkit.plugin.Plugin; - -import com.gmail.filoghost.holographicdisplays.HolographicDisplays; -import com.gmail.filoghost.holographicdisplays.disk.StringConverter; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.FileUtils; -import com.gmail.filoghost.holographicdisplays.util.Utils; - -public class AnimationsRegister { - - // - private final static Map animations = Utils.newMap(); - - public static void loadAnimations(Plugin plugin) { - animations.clear(); - - File animationFolder = new File(plugin.getDataFolder(), "animations"); - if (!animationFolder.isDirectory()) { - animationFolder.mkdirs(); - plugin.saveResource("animations/example.txt", false); - return; - } - - for (File file : animationFolder.listFiles()) { - - try { - List lines = FileUtils.readLines(file); - if (lines.size() == 0) { - continue; - } - - double speed = 0.5; - boolean validSpeedFound = false; - - String firstLine = lines.get(0).trim(); - if (firstLine.toLowerCase().startsWith("speed:")) { - - // Do not consider it. - lines.remove(0); - - firstLine = firstLine.substring("speed:".length()).trim(); - - try { - speed = Double.parseDouble(firstLine); - validSpeedFound = true; - } catch (NumberFormatException e) { } - } - - if (!validSpeedFound) { - ConsoleLogger.log(Level.WARNING, "Could not find a valid 'speed: ' in the first line of the file '" + file.getName() + "'. Default speed of 0.5 seconds will be used."); - } - - if (lines.isEmpty()) { - lines.add("[No lines: " + file.getName() + "]"); - ConsoleLogger.log(Level.WARNING, "Could not find any line in '" + file.getName() + "' (excluding the speed). You should add at least one more line."); - } - - // Replace placeholders. - for (int i = 0; i < lines.size(); i++) { - lines.set(i, StringConverter.toReadableFormat(lines.get(i))); - } - - animations.put(file.getName(), new Placeholder(HolographicDisplays.getInstance(), file.getName(), speed, new CyclicPlaceholderReplacer(lines.toArray(new String[lines.size()])))); - ConsoleLogger.logDebug(Level.INFO, "Successfully loaded animation '" + file.getName() + "', speed = " + speed + "."); - - } catch (Exception e) { - ConsoleLogger.log(Level.SEVERE, "Couldn't load the file '" + file.getName() + "'!", e); - } - } - } - - - public static Map getAnimations() { - return animations; - } - - public static Placeholder getAnimation(String name) { - return animations.get(name); - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.placeholder; + +import java.io.File; +import java.util.List; +import java.util.Map; +import java.util.logging.Level; + +import org.bukkit.plugin.Plugin; + +import com.gmail.filoghost.holographicdisplays.HolographicDisplays; +import com.gmail.filoghost.holographicdisplays.disk.StringConverter; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.FileUtils; +import com.gmail.filoghost.holographicdisplays.util.Utils; + +public class AnimationsRegister { + + // + private final static Map animations = Utils.newMap(); + + public static void loadAnimations(Plugin plugin) { + animations.clear(); + + File animationFolder = new File(plugin.getDataFolder(), "animations"); + if (!animationFolder.isDirectory()) { + animationFolder.mkdirs(); + plugin.saveResource("animations/example.txt", false); + return; + } + + for (File file : animationFolder.listFiles()) { + + try { + List lines = FileUtils.readLines(file); + if (lines.size() == 0) { + continue; + } + + double speed = 0.5; + boolean validSpeedFound = false; + + String firstLine = lines.get(0).trim(); + if (firstLine.toLowerCase().startsWith("speed:")) { + + // Do not consider it. + lines.remove(0); + + firstLine = firstLine.substring("speed:".length()).trim(); + + try { + speed = Double.parseDouble(firstLine); + validSpeedFound = true; + } catch (NumberFormatException e) { } + } + + if (!validSpeedFound) { + ConsoleLogger.log(Level.WARNING, "Could not find a valid 'speed: ' in the first line of the file '" + file.getName() + "'. Default speed of 0.5 seconds will be used."); + } + + if (lines.isEmpty()) { + lines.add("[No lines: " + file.getName() + "]"); + ConsoleLogger.log(Level.WARNING, "Could not find any line in '" + file.getName() + "' (excluding the speed). You should add at least one more line."); + } + + // Replace placeholders. + for (int i = 0; i < lines.size(); i++) { + lines.set(i, StringConverter.toReadableFormat(lines.get(i))); + } + + animations.put(file.getName(), new Placeholder(HolographicDisplays.getInstance(), file.getName(), speed, new CyclicPlaceholderReplacer(lines.toArray(new String[lines.size()])))); + ConsoleLogger.logDebug(Level.INFO, "Successfully loaded animation '" + file.getName() + "', speed = " + speed + "."); + + } catch (Exception e) { + ConsoleLogger.log(Level.SEVERE, "Couldn't load the file '" + file.getName() + "'!", e); + } + } + } + + + public static Map getAnimations() { + return animations; + } + + public static Placeholder getAnimation(String name) { + return animations.get(name); + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/placeholder/CyclicPlaceholderReplacer.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/placeholder/CyclicPlaceholderReplacer.java index 2f99e4fc..92766bc0 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/placeholder/CyclicPlaceholderReplacer.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/placeholder/CyclicPlaceholderReplacer.java @@ -1,27 +1,41 @@ -package com.gmail.filoghost.holographicdisplays.placeholder; - -import com.gmail.filoghost.holographicdisplays.api.placeholder.PlaceholderReplacer; - -public class CyclicPlaceholderReplacer implements PlaceholderReplacer { - - String[] frames; - private int index; - - public CyclicPlaceholderReplacer(String[] frames) { - this.frames = frames; - index = 0; - } - - @Override - public String update() { - String result = frames[index]; - - index++; - if (index >= frames.length) { - index = 0; - } - - return result; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.placeholder; + +import com.gmail.filoghost.holographicdisplays.api.placeholder.PlaceholderReplacer; + +public class CyclicPlaceholderReplacer implements PlaceholderReplacer { + + String[] frames; + private int index; + + public CyclicPlaceholderReplacer(String[] frames) { + this.frames = frames; + index = 0; + } + + @Override + public String update() { + String result = frames[index]; + + index++; + if (index >= frames.length) { + index = 0; + } + + return result; + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/placeholder/DynamicLineData.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/placeholder/DynamicLineData.java index 5b5a29c1..42ffcaf1 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/placeholder/DynamicLineData.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/placeholder/DynamicLineData.java @@ -1,73 +1,87 @@ -package com.gmail.filoghost.holographicdisplays.placeholder; - -import java.util.Map; -import java.util.Set; - -import com.gmail.filoghost.holographicdisplays.api.placeholder.PlaceholderReplacer; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSNameable; -import com.gmail.filoghost.holographicdisplays.util.Utils; -import com.gmail.filoghost.holographicdisplays.util.Validator; - -public class DynamicLineData { - - private final NMSNameable entity; - private final String originalName; - - private Set placeholders; - private final Map animations; - private final Map replacers; - - public DynamicLineData(NMSNameable entity, String originalName) { - Validator.notNull(entity, "entity"); - - this.entity = entity; - this.originalName = originalName; - placeholders = Utils.newSet(); - animations = Utils.newMap(); - replacers = Utils.newMap(); - } - - public NMSNameable getEntity() { - return entity; - } - - public String getOriginalName() { - return originalName; - } - - public void setPlaceholders(Set placeholders) { - this.placeholders = placeholders; - } - - public Set getPlaceholders() { - return placeholders; - } - - public Map getReplacers() { - return replacers; - } - - public Map getAnimations() { - return animations; - } - - @Override - public int hashCode() { - return entity.hashCode(); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - DynamicLineData other = (DynamicLineData) obj; - return this.entity == other.entity; - } - - - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.placeholder; + +import java.util.Map; +import java.util.Set; + +import com.gmail.filoghost.holographicdisplays.api.placeholder.PlaceholderReplacer; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSNameable; +import com.gmail.filoghost.holographicdisplays.util.Utils; +import com.gmail.filoghost.holographicdisplays.util.Validator; + +public class DynamicLineData { + + private final NMSNameable entity; + private final String originalName; + + private Set placeholders; + private final Map animations; + private final Map replacers; + + public DynamicLineData(NMSNameable entity, String originalName) { + Validator.notNull(entity, "entity"); + + this.entity = entity; + this.originalName = originalName; + placeholders = Utils.newSet(); + animations = Utils.newMap(); + replacers = Utils.newMap(); + } + + public NMSNameable getEntity() { + return entity; + } + + public String getOriginalName() { + return originalName; + } + + public void setPlaceholders(Set placeholders) { + this.placeholders = placeholders; + } + + public Set getPlaceholders() { + return placeholders; + } + + public Map getReplacers() { + return replacers; + } + + public Map getAnimations() { + return animations; + } + + @Override + public int hashCode() { + return entity.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + DynamicLineData other = (DynamicLineData) obj; + return this.entity == other.entity; + } + + + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/placeholder/Placeholder.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/placeholder/Placeholder.java index 7c93d6ad..30b63f57 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/placeholder/Placeholder.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/placeholder/Placeholder.java @@ -1,88 +1,102 @@ -package com.gmail.filoghost.holographicdisplays.placeholder; - -import org.bukkit.plugin.Plugin; - -import com.gmail.filoghost.holographicdisplays.api.placeholder.PlaceholderReplacer; - -public class Placeholder { - - // The plugin that owns this placeholder. - private final Plugin owner; - - // The placeholder itself, something like {onlinePlayers}. Case sensitive! - private final String textPlaceholder; - - // How many tenths of second between each refresh. - private int tenthsToRefresh; - - // This is the current replacement for this placeholder. - private String currentReplacement; - - private PlaceholderReplacer replacer; - - public Placeholder(Plugin owner, String textPlaceholder, double refreshRate, PlaceholderReplacer replacer) { - this.owner = owner; - this.textPlaceholder = textPlaceholder; - this.tenthsToRefresh = refreshRate <= 0.1 ? 1 : (int) (refreshRate * 10.0); - this.replacer = replacer; - this.currentReplacement = ""; - } - - public Plugin getOwner() { - return owner; - } - - public int getTenthsToRefresh() { - return tenthsToRefresh; - } - - public void setTenthsToRefresh(int tenthsToRefresh) { - this.tenthsToRefresh = tenthsToRefresh; - } - - public String getTextPlaceholder() { - return textPlaceholder; - } - - public String getCurrentReplacement() { - return currentReplacement; - } - - public void setCurrentReplacement(String replacement) { - this.currentReplacement = replacement != null ? replacement : "null"; - } - - public PlaceholderReplacer getReplacer() { - return replacer; - } - - public void setReplacer(PlaceholderReplacer replacer) { - this.replacer = replacer; - } - - public void update() { - setCurrentReplacement(replacer.update()); - } - - - @Override - public boolean equals(Object obj) { - if (obj == null) { - return false; - } - - if (obj instanceof Placeholder) { - return ((Placeholder) obj).textPlaceholder.equals(this.textPlaceholder); - } - - return false; - } - - - @Override - public int hashCode() { - return textPlaceholder.hashCode(); - } - - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.placeholder; + +import org.bukkit.plugin.Plugin; + +import com.gmail.filoghost.holographicdisplays.api.placeholder.PlaceholderReplacer; + +public class Placeholder { + + // The plugin that owns this placeholder. + private final Plugin owner; + + // The placeholder itself, something like {onlinePlayers}. Case sensitive! + private final String textPlaceholder; + + // How many tenths of second between each refresh. + private int tenthsToRefresh; + + // This is the current replacement for this placeholder. + private String currentReplacement; + + private PlaceholderReplacer replacer; + + public Placeholder(Plugin owner, String textPlaceholder, double refreshRate, PlaceholderReplacer replacer) { + this.owner = owner; + this.textPlaceholder = textPlaceholder; + this.tenthsToRefresh = refreshRate <= 0.1 ? 1 : (int) (refreshRate * 10.0); + this.replacer = replacer; + this.currentReplacement = ""; + } + + public Plugin getOwner() { + return owner; + } + + public int getTenthsToRefresh() { + return tenthsToRefresh; + } + + public void setTenthsToRefresh(int tenthsToRefresh) { + this.tenthsToRefresh = tenthsToRefresh; + } + + public String getTextPlaceholder() { + return textPlaceholder; + } + + public String getCurrentReplacement() { + return currentReplacement; + } + + public void setCurrentReplacement(String replacement) { + this.currentReplacement = replacement != null ? replacement : "null"; + } + + public PlaceholderReplacer getReplacer() { + return replacer; + } + + public void setReplacer(PlaceholderReplacer replacer) { + this.replacer = replacer; + } + + public void update() { + setCurrentReplacement(replacer.update()); + } + + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + + if (obj instanceof Placeholder) { + return ((Placeholder) obj).textPlaceholder.equals(this.textPlaceholder); + } + + return false; + } + + + @Override + public int hashCode() { + return textPlaceholder.hashCode(); + } + + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/placeholder/PlaceholdersManager.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/placeholder/PlaceholdersManager.java index a8b82606..1c9319ed 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/placeholder/PlaceholdersManager.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/placeholder/PlaceholdersManager.java @@ -1,381 +1,395 @@ -package com.gmail.filoghost.holographicdisplays.placeholder; - -import java.util.Iterator; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; -import java.util.logging.Level; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import org.bukkit.Bukkit; -import org.bukkit.plugin.Plugin; - -import com.gmail.filoghost.holographicdisplays.api.placeholder.PlaceholderReplacer; -import com.gmail.filoghost.holographicdisplays.bridge.bungeecord.BungeeServerTracker; -import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSNameable; -import com.gmail.filoghost.holographicdisplays.object.line.CraftTextLine; -import com.gmail.filoghost.holographicdisplays.task.WorldPlayerCounterTask; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.Utils; - -public class PlaceholdersManager { - - private static long elapsedTenthsOfSecond; - protected static Set linesToUpdate = Utils.newSet(); - - private static final Pattern BUNGEE_ONLINE_PATTERN = makePlaceholderWithArgsPattern("online"); - private static final Pattern BUNGEE_MAX_PATTERN = makePlaceholderWithArgsPattern("max_players"); - private static final Pattern BUNGEE_MOTD_PATTERN = makePlaceholderWithArgsPattern("motd"); - private static final Pattern BUNGEE_MOTD_2_PATTERN = makePlaceholderWithArgsPattern("motd2"); - private static final Pattern BUNGEE_STATUS_PATTERN = makePlaceholderWithArgsPattern("status"); - private static final Pattern ANIMATION_PATTERN = makePlaceholderWithArgsPattern("animation"); - private static final Pattern WORLD_PATTERN = makePlaceholderWithArgsPattern("world"); - - private static Pattern makePlaceholderWithArgsPattern(String prefix) { - return Pattern.compile("(\\{" + Pattern.quote(prefix) + ":)(.+?)(\\})"); - } - - private static String extractArgumentFromPlaceholder(Matcher matcher) { - return matcher.group(2).trim(); - } - - - public static void load(Plugin plugin) { - - Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() { - - @Override - public void run() { - - for (Placeholder placeholder : PlaceholdersRegister.getPlaceholders()) { - if (elapsedTenthsOfSecond % placeholder.getTenthsToRefresh() == 0) { - try { - placeholder.update(); - } catch (Throwable t) { - ConsoleLogger.log(Level.WARNING, "The placeholder " + placeholder.getTextPlaceholder() + " registered by the plugin " + placeholder.getOwner().getName() + " generated an exception while updating. Please contact the author of " + placeholder.getOwner().getName(), t); - } - } - } - - for (Placeholder placeholder : AnimationsRegister.getAnimations().values()) { - if (elapsedTenthsOfSecond % placeholder.getTenthsToRefresh() == 0) { - placeholder.update(); - } - } - - Iterator iter = linesToUpdate.iterator(); - DynamicLineData currentLineData; - - while (iter.hasNext()) { - currentLineData = iter.next(); - - if (currentLineData.getEntity().isDeadNMS()) { - iter.remove(); - } else { - updatePlaceholders(currentLineData); - } - } - - elapsedTenthsOfSecond++; - } - - }, 2L, 2L); - } - - - public static void untrackAll() { - linesToUpdate.clear(); - } - - public static void untrack(CraftTextLine line) { - - if (line == null || !line.isSpawned()) { - return; - } - - Iterator iter = linesToUpdate.iterator(); - while (iter.hasNext()) { - DynamicLineData data = iter.next(); - if (data.getEntity() == line.getNmsNameble()) { - iter.remove(); - data.getEntity().setCustomNameNMS(data.getOriginalName()); - } - } - } - - public static void trackIfNecessary(CraftTextLine line) { - - NMSNameable nameableEntity = line.getNmsNameble(); - String name = line.getText(); - - if (nameableEntity == null) { - return; - } - - boolean updateName = false; - - if (name == null || name.isEmpty()) { - return; - } - - // Lazy initialization. - Set normalPlaceholders = null; - - Map bungeeReplacers = null; - - Map worldsOnlinePlayersReplacers = null; - Map animationsPlaceholders = null; - - Matcher matcher; - - for (Placeholder placeholder : PlaceholdersRegister.getPlaceholders()) { - - if (name.contains(placeholder.getTextPlaceholder())) { - - if (normalPlaceholders == null) { - normalPlaceholders = Utils.newSet(); - } - - normalPlaceholders.add(placeholder); - } - } - - - // Players in a world count pattern. - matcher = WORLD_PATTERN.matcher(name); - while (matcher.find()) { - - if (worldsOnlinePlayersReplacers == null) { - worldsOnlinePlayersReplacers = Utils.newMap(); - } - - final String worldName = extractArgumentFromPlaceholder(matcher); - worldsOnlinePlayersReplacers.put(matcher.group(), new PlaceholderReplacer() { - - @Override - public String update() { - return WorldPlayerCounterTask.getCount(worldName); - } - }); - } - - // BungeeCord online pattern. - matcher = BUNGEE_ONLINE_PATTERN.matcher(name); - while (matcher.find()) { - - if (bungeeReplacers == null) { - bungeeReplacers = Utils.newMap(); - } - - final String serverName = extractArgumentFromPlaceholder(matcher); - BungeeServerTracker.track(serverName); // Track this server. - - if (serverName.contains(",")) { - - String[] split = serverName.split(","); - for (int i = 0; i < split.length; i++) { - split[i] = split[i].trim(); - } - - final String[] serversToTrack = split; - - // Add it to tracked servers. - bungeeReplacers.put(matcher.group(), new PlaceholderReplacer() { - - @Override - public String update() { - int count = 0; - for (String serverToTrack : serversToTrack) { - count += BungeeServerTracker.getPlayersOnline(serverToTrack); - } - return String.valueOf(count); - } - }); - } else { - // Normal, single tracked server. - bungeeReplacers.put(matcher.group(), new PlaceholderReplacer() { - - @Override - public String update() { - return String.valueOf(BungeeServerTracker.getPlayersOnline(serverName)); - } - }); - } - } - - // BungeeCord max players pattern. - matcher = BUNGEE_MAX_PATTERN.matcher(name); - while (matcher.find()) { - - if (bungeeReplacers == null) { - bungeeReplacers = Utils.newMap(); - } - - final String serverName = extractArgumentFromPlaceholder(matcher); - BungeeServerTracker.track(serverName); // Track this server. - - // Add it to tracked servers. - bungeeReplacers.put(matcher.group(), new PlaceholderReplacer() { - - @Override - public String update() { - return BungeeServerTracker.getMaxPlayers(serverName); - } - }); - } - - // BungeeCord motd pattern. - matcher = BUNGEE_MOTD_PATTERN.matcher(name); - while (matcher.find()) { - - if (bungeeReplacers == null) { - bungeeReplacers = Utils.newMap(); - } - - final String serverName = extractArgumentFromPlaceholder(matcher); - BungeeServerTracker.track(serverName); // Track this server. - - // Add it to tracked servers. - bungeeReplacers.put(matcher.group(), new PlaceholderReplacer() { - - @Override - public String update() { - return BungeeServerTracker.getMotd1(serverName); - } - }); - } - - // BungeeCord motd (line 2) pattern. - matcher = BUNGEE_MOTD_2_PATTERN.matcher(name); - while (matcher.find()) { - - if (bungeeReplacers == null) { - bungeeReplacers = Utils.newMap(); - } - - final String serverName = extractArgumentFromPlaceholder(matcher); - BungeeServerTracker.track(serverName); // Track this server. - - // Add it to tracked servers. - bungeeReplacers.put(matcher.group(), new PlaceholderReplacer() { - - @Override - public String update() { - return BungeeServerTracker.getMotd2(serverName); - } - }); - } - - // BungeeCord status pattern. - matcher = BUNGEE_STATUS_PATTERN.matcher(name); - while (matcher.find()) { - - if (bungeeReplacers == null) { - bungeeReplacers = Utils.newMap(); - } - - final String serverName = extractArgumentFromPlaceholder(matcher); - BungeeServerTracker.track(serverName); // Track this server. - - // Add it to tracked servers. - bungeeReplacers.put(matcher.group(), new PlaceholderReplacer() { - - @Override - public String update() { - return BungeeServerTracker.getOnlineStatus(serverName); - } - }); - } - - - // Animation pattern. - matcher = ANIMATION_PATTERN.matcher(name); - while (matcher.find()) { - - String fileName = extractArgumentFromPlaceholder(matcher); - Placeholder animation = AnimationsRegister.getAnimation(fileName); - - // If exists... - if (animation != null) { - - if (animationsPlaceholders == null) { - animationsPlaceholders = Utils.newMap(); - } - - animationsPlaceholders.put(matcher.group(), animation); - - } else { - name = name.replace(matcher.group(), "[Animation not found: " + fileName + "]"); - updateName = true; - } - } - - if (Utils.isThereNonNull(normalPlaceholders, bungeeReplacers, worldsOnlinePlayersReplacers, animationsPlaceholders)) { - - DynamicLineData lineData = new DynamicLineData(nameableEntity, name); - - if (normalPlaceholders != null) { - lineData.setPlaceholders(normalPlaceholders); - } - - if (bungeeReplacers != null) { - lineData.getReplacers().putAll(bungeeReplacers); - } - - if (worldsOnlinePlayersReplacers != null) { - lineData.getReplacers().putAll(worldsOnlinePlayersReplacers); - } - - if (animationsPlaceholders != null) { - lineData.getAnimations().putAll(animationsPlaceholders); - } - - // It could be already tracked! - if (!linesToUpdate.add(lineData)) { - linesToUpdate.remove(lineData); - linesToUpdate.add(lineData); - } - - updatePlaceholders(lineData); - - } else { - - // The name needs to be updated anyways. - if (updateName) { - nameableEntity.setCustomNameNMS(name); - } - } - } - - - private static void updatePlaceholders(DynamicLineData lineData) { - - String oldCustomName = lineData.getEntity().getCustomNameNMS(); - String newCustomName = lineData.getOriginalName(); - - if (!lineData.getPlaceholders().isEmpty()) { - for (Placeholder placeholder : lineData.getPlaceholders()) { - newCustomName = newCustomName.replace(placeholder.getTextPlaceholder(), Utils.sanitize(placeholder.getCurrentReplacement())); - } - } - - if (!lineData.getReplacers().isEmpty()) { - for (Entry entry : lineData.getReplacers().entrySet()) { - newCustomName = newCustomName.replace(entry.getKey(), Utils.sanitize(entry.getValue().update())); - } - } - - if (!lineData.getAnimations().isEmpty()) { - for (Entry entry : lineData.getAnimations().entrySet()) { - newCustomName = newCustomName.replace(entry.getKey(), Utils.sanitize(entry.getValue().getCurrentReplacement())); - } - } - - // Update only if needed, don't send useless packets. - if (!oldCustomName.equals(newCustomName)) { - lineData.getEntity().setCustomNameNMS(newCustomName); - } - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.placeholder; + +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.logging.Level; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.bukkit.Bukkit; +import org.bukkit.plugin.Plugin; + +import com.gmail.filoghost.holographicdisplays.api.placeholder.PlaceholderReplacer; +import com.gmail.filoghost.holographicdisplays.bridge.bungeecord.BungeeServerTracker; +import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSNameable; +import com.gmail.filoghost.holographicdisplays.object.line.CraftTextLine; +import com.gmail.filoghost.holographicdisplays.task.WorldPlayerCounterTask; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.Utils; + +public class PlaceholdersManager { + + private static long elapsedTenthsOfSecond; + protected static Set linesToUpdate = Utils.newSet(); + + private static final Pattern BUNGEE_ONLINE_PATTERN = makePlaceholderWithArgsPattern("online"); + private static final Pattern BUNGEE_MAX_PATTERN = makePlaceholderWithArgsPattern("max_players"); + private static final Pattern BUNGEE_MOTD_PATTERN = makePlaceholderWithArgsPattern("motd"); + private static final Pattern BUNGEE_MOTD_2_PATTERN = makePlaceholderWithArgsPattern("motd2"); + private static final Pattern BUNGEE_STATUS_PATTERN = makePlaceholderWithArgsPattern("status"); + private static final Pattern ANIMATION_PATTERN = makePlaceholderWithArgsPattern("animation"); + private static final Pattern WORLD_PATTERN = makePlaceholderWithArgsPattern("world"); + + private static Pattern makePlaceholderWithArgsPattern(String prefix) { + return Pattern.compile("(\\{" + Pattern.quote(prefix) + ":)(.+?)(\\})"); + } + + private static String extractArgumentFromPlaceholder(Matcher matcher) { + return matcher.group(2).trim(); + } + + + public static void load(Plugin plugin) { + + Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() { + + @Override + public void run() { + + for (Placeholder placeholder : PlaceholdersRegister.getPlaceholders()) { + if (elapsedTenthsOfSecond % placeholder.getTenthsToRefresh() == 0) { + try { + placeholder.update(); + } catch (Throwable t) { + ConsoleLogger.log(Level.WARNING, "The placeholder " + placeholder.getTextPlaceholder() + " registered by the plugin " + placeholder.getOwner().getName() + " generated an exception while updating. Please contact the author of " + placeholder.getOwner().getName(), t); + } + } + } + + for (Placeholder placeholder : AnimationsRegister.getAnimations().values()) { + if (elapsedTenthsOfSecond % placeholder.getTenthsToRefresh() == 0) { + placeholder.update(); + } + } + + Iterator iter = linesToUpdate.iterator(); + DynamicLineData currentLineData; + + while (iter.hasNext()) { + currentLineData = iter.next(); + + if (currentLineData.getEntity().isDeadNMS()) { + iter.remove(); + } else { + updatePlaceholders(currentLineData); + } + } + + elapsedTenthsOfSecond++; + } + + }, 2L, 2L); + } + + + public static void untrackAll() { + linesToUpdate.clear(); + } + + public static void untrack(CraftTextLine line) { + + if (line == null || !line.isSpawned()) { + return; + } + + Iterator iter = linesToUpdate.iterator(); + while (iter.hasNext()) { + DynamicLineData data = iter.next(); + if (data.getEntity() == line.getNmsNameble()) { + iter.remove(); + data.getEntity().setCustomNameNMS(data.getOriginalName()); + } + } + } + + public static void trackIfNecessary(CraftTextLine line) { + + NMSNameable nameableEntity = line.getNmsNameble(); + String name = line.getText(); + + if (nameableEntity == null) { + return; + } + + boolean updateName = false; + + if (name == null || name.isEmpty()) { + return; + } + + // Lazy initialization. + Set normalPlaceholders = null; + + Map bungeeReplacers = null; + + Map worldsOnlinePlayersReplacers = null; + Map animationsPlaceholders = null; + + Matcher matcher; + + for (Placeholder placeholder : PlaceholdersRegister.getPlaceholders()) { + + if (name.contains(placeholder.getTextPlaceholder())) { + + if (normalPlaceholders == null) { + normalPlaceholders = Utils.newSet(); + } + + normalPlaceholders.add(placeholder); + } + } + + + // Players in a world count pattern. + matcher = WORLD_PATTERN.matcher(name); + while (matcher.find()) { + + if (worldsOnlinePlayersReplacers == null) { + worldsOnlinePlayersReplacers = Utils.newMap(); + } + + final String worldName = extractArgumentFromPlaceholder(matcher); + worldsOnlinePlayersReplacers.put(matcher.group(), new PlaceholderReplacer() { + + @Override + public String update() { + return WorldPlayerCounterTask.getCount(worldName); + } + }); + } + + // BungeeCord online pattern. + matcher = BUNGEE_ONLINE_PATTERN.matcher(name); + while (matcher.find()) { + + if (bungeeReplacers == null) { + bungeeReplacers = Utils.newMap(); + } + + final String serverName = extractArgumentFromPlaceholder(matcher); + BungeeServerTracker.track(serverName); // Track this server. + + if (serverName.contains(",")) { + + String[] split = serverName.split(","); + for (int i = 0; i < split.length; i++) { + split[i] = split[i].trim(); + } + + final String[] serversToTrack = split; + + // Add it to tracked servers. + bungeeReplacers.put(matcher.group(), new PlaceholderReplacer() { + + @Override + public String update() { + int count = 0; + for (String serverToTrack : serversToTrack) { + count += BungeeServerTracker.getPlayersOnline(serverToTrack); + } + return String.valueOf(count); + } + }); + } else { + // Normal, single tracked server. + bungeeReplacers.put(matcher.group(), new PlaceholderReplacer() { + + @Override + public String update() { + return String.valueOf(BungeeServerTracker.getPlayersOnline(serverName)); + } + }); + } + } + + // BungeeCord max players pattern. + matcher = BUNGEE_MAX_PATTERN.matcher(name); + while (matcher.find()) { + + if (bungeeReplacers == null) { + bungeeReplacers = Utils.newMap(); + } + + final String serverName = extractArgumentFromPlaceholder(matcher); + BungeeServerTracker.track(serverName); // Track this server. + + // Add it to tracked servers. + bungeeReplacers.put(matcher.group(), new PlaceholderReplacer() { + + @Override + public String update() { + return BungeeServerTracker.getMaxPlayers(serverName); + } + }); + } + + // BungeeCord motd pattern. + matcher = BUNGEE_MOTD_PATTERN.matcher(name); + while (matcher.find()) { + + if (bungeeReplacers == null) { + bungeeReplacers = Utils.newMap(); + } + + final String serverName = extractArgumentFromPlaceholder(matcher); + BungeeServerTracker.track(serverName); // Track this server. + + // Add it to tracked servers. + bungeeReplacers.put(matcher.group(), new PlaceholderReplacer() { + + @Override + public String update() { + return BungeeServerTracker.getMotd1(serverName); + } + }); + } + + // BungeeCord motd (line 2) pattern. + matcher = BUNGEE_MOTD_2_PATTERN.matcher(name); + while (matcher.find()) { + + if (bungeeReplacers == null) { + bungeeReplacers = Utils.newMap(); + } + + final String serverName = extractArgumentFromPlaceholder(matcher); + BungeeServerTracker.track(serverName); // Track this server. + + // Add it to tracked servers. + bungeeReplacers.put(matcher.group(), new PlaceholderReplacer() { + + @Override + public String update() { + return BungeeServerTracker.getMotd2(serverName); + } + }); + } + + // BungeeCord status pattern. + matcher = BUNGEE_STATUS_PATTERN.matcher(name); + while (matcher.find()) { + + if (bungeeReplacers == null) { + bungeeReplacers = Utils.newMap(); + } + + final String serverName = extractArgumentFromPlaceholder(matcher); + BungeeServerTracker.track(serverName); // Track this server. + + // Add it to tracked servers. + bungeeReplacers.put(matcher.group(), new PlaceholderReplacer() { + + @Override + public String update() { + return BungeeServerTracker.getOnlineStatus(serverName); + } + }); + } + + + // Animation pattern. + matcher = ANIMATION_PATTERN.matcher(name); + while (matcher.find()) { + + String fileName = extractArgumentFromPlaceholder(matcher); + Placeholder animation = AnimationsRegister.getAnimation(fileName); + + // If exists... + if (animation != null) { + + if (animationsPlaceholders == null) { + animationsPlaceholders = Utils.newMap(); + } + + animationsPlaceholders.put(matcher.group(), animation); + + } else { + name = name.replace(matcher.group(), "[Animation not found: " + fileName + "]"); + updateName = true; + } + } + + if (Utils.isThereNonNull(normalPlaceholders, bungeeReplacers, worldsOnlinePlayersReplacers, animationsPlaceholders)) { + + DynamicLineData lineData = new DynamicLineData(nameableEntity, name); + + if (normalPlaceholders != null) { + lineData.setPlaceholders(normalPlaceholders); + } + + if (bungeeReplacers != null) { + lineData.getReplacers().putAll(bungeeReplacers); + } + + if (worldsOnlinePlayersReplacers != null) { + lineData.getReplacers().putAll(worldsOnlinePlayersReplacers); + } + + if (animationsPlaceholders != null) { + lineData.getAnimations().putAll(animationsPlaceholders); + } + + // It could be already tracked! + if (!linesToUpdate.add(lineData)) { + linesToUpdate.remove(lineData); + linesToUpdate.add(lineData); + } + + updatePlaceholders(lineData); + + } else { + + // The name needs to be updated anyways. + if (updateName) { + nameableEntity.setCustomNameNMS(name); + } + } + } + + + private static void updatePlaceholders(DynamicLineData lineData) { + + String oldCustomName = lineData.getEntity().getCustomNameNMS(); + String newCustomName = lineData.getOriginalName(); + + if (!lineData.getPlaceholders().isEmpty()) { + for (Placeholder placeholder : lineData.getPlaceholders()) { + newCustomName = newCustomName.replace(placeholder.getTextPlaceholder(), Utils.sanitize(placeholder.getCurrentReplacement())); + } + } + + if (!lineData.getReplacers().isEmpty()) { + for (Entry entry : lineData.getReplacers().entrySet()) { + newCustomName = newCustomName.replace(entry.getKey(), Utils.sanitize(entry.getValue().update())); + } + } + + if (!lineData.getAnimations().isEmpty()) { + for (Entry entry : lineData.getAnimations().entrySet()) { + newCustomName = newCustomName.replace(entry.getKey(), Utils.sanitize(entry.getValue().getCurrentReplacement())); + } + } + + // Update only if needed, don't send useless packets. + if (!oldCustomName.equals(newCustomName)) { + lineData.getEntity().setCustomNameNMS(newCustomName); + } + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/placeholder/PlaceholdersRegister.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/placeholder/PlaceholdersRegister.java index f953549b..94560775 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/placeholder/PlaceholdersRegister.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/placeholder/PlaceholdersRegister.java @@ -1,114 +1,128 @@ -package com.gmail.filoghost.holographicdisplays.placeholder; - -import java.util.Date; -import java.util.Iterator; -import java.util.Set; - -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.plugin.Plugin; - -import com.gmail.filoghost.holographicdisplays.HolographicDisplays; -import com.gmail.filoghost.holographicdisplays.api.placeholder.PlaceholderReplacer; -import com.gmail.filoghost.holographicdisplays.disk.Configuration; -import com.gmail.filoghost.holographicdisplays.util.Utils; - -public class PlaceholdersRegister { - - private static final Set placeholders = Utils.newSet(); - - // Register the default placeholders statically. - static { - - register(new Placeholder(HolographicDisplays.getInstance(), "{online}", 1.0, new PlaceholderReplacer() { - - @Override - public String update() { - return String.valueOf(Bukkit.getOnlinePlayers().size()); - } - })); - - register(new Placeholder(HolographicDisplays.getInstance(), "{max_players}", 10.0, new PlaceholderReplacer() { - - @Override - public String update() { - return String.valueOf(Bukkit.getMaxPlayers()); - } - })); - - register(new Placeholder(HolographicDisplays.getInstance(), "{motd}", 60.0, new PlaceholderReplacer() { - - @Override - public String update() { - return Bukkit.getMotd(); - } - })); - - register(new Placeholder(HolographicDisplays.getInstance(), "{time}", 0.9, new PlaceholderReplacer() { - - @Override - public String update() { - return Configuration.timeFormat.format(new Date()); - } - })); - - register(new Placeholder(HolographicDisplays.getInstance(), "&u", 0.2, new CyclicPlaceholderReplacer(Utils.arrayToStrings( - ChatColor.RED, - ChatColor.GOLD, - ChatColor.YELLOW, - ChatColor.GREEN, - ChatColor.AQUA, - ChatColor.LIGHT_PURPLE - )))); - } - - - public static boolean register(Placeholder placeholder) { - if (placeholders.contains(placeholder)) { - return false; - } - - placeholders.add(placeholder); - return true; - } - - public static Set getTextPlaceholdersByPlugin(Plugin plugin) { - Set found = Utils.newSet(); - - for (Placeholder placeholder : placeholders) { - if (placeholder.getOwner().equals(plugin)) { - found.add(placeholder.getTextPlaceholder()); - } - } - - return found; - } - - public static boolean unregister(Plugin plugin, String textPlaceholder) { - - Iterator iter = placeholders.iterator(); - - while (iter.hasNext()) { - Placeholder placeholder = iter.next(); - - if (placeholder.getOwner().equals(plugin) && placeholder.getTextPlaceholder().equals(textPlaceholder)) { - iter.remove(); - - for (DynamicLineData data : PlaceholdersManager.linesToUpdate) { - if (data.getPlaceholders().contains(placeholder)) { - data.getPlaceholders().remove(placeholder); - } - } - - return true; - } - } - - return false; - } - - protected static Set getPlaceholders() { - return placeholders; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.placeholder; + +import java.util.Date; +import java.util.Iterator; +import java.util.Set; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.plugin.Plugin; + +import com.gmail.filoghost.holographicdisplays.HolographicDisplays; +import com.gmail.filoghost.holographicdisplays.api.placeholder.PlaceholderReplacer; +import com.gmail.filoghost.holographicdisplays.disk.Configuration; +import com.gmail.filoghost.holographicdisplays.util.Utils; + +public class PlaceholdersRegister { + + private static final Set placeholders = Utils.newSet(); + + // Register the default placeholders statically. + static { + + register(new Placeholder(HolographicDisplays.getInstance(), "{online}", 1.0, new PlaceholderReplacer() { + + @Override + public String update() { + return String.valueOf(Bukkit.getOnlinePlayers().size()); + } + })); + + register(new Placeholder(HolographicDisplays.getInstance(), "{max_players}", 10.0, new PlaceholderReplacer() { + + @Override + public String update() { + return String.valueOf(Bukkit.getMaxPlayers()); + } + })); + + register(new Placeholder(HolographicDisplays.getInstance(), "{motd}", 60.0, new PlaceholderReplacer() { + + @Override + public String update() { + return Bukkit.getMotd(); + } + })); + + register(new Placeholder(HolographicDisplays.getInstance(), "{time}", 0.9, new PlaceholderReplacer() { + + @Override + public String update() { + return Configuration.timeFormat.format(new Date()); + } + })); + + register(new Placeholder(HolographicDisplays.getInstance(), "&u", 0.2, new CyclicPlaceholderReplacer(Utils.arrayToStrings( + ChatColor.RED, + ChatColor.GOLD, + ChatColor.YELLOW, + ChatColor.GREEN, + ChatColor.AQUA, + ChatColor.LIGHT_PURPLE + )))); + } + + + public static boolean register(Placeholder placeholder) { + if (placeholders.contains(placeholder)) { + return false; + } + + placeholders.add(placeholder); + return true; + } + + public static Set getTextPlaceholdersByPlugin(Plugin plugin) { + Set found = Utils.newSet(); + + for (Placeholder placeholder : placeholders) { + if (placeholder.getOwner().equals(plugin)) { + found.add(placeholder.getTextPlaceholder()); + } + } + + return found; + } + + public static boolean unregister(Plugin plugin, String textPlaceholder) { + + Iterator iter = placeholders.iterator(); + + while (iter.hasNext()) { + Placeholder placeholder = iter.next(); + + if (placeholder.getOwner().equals(plugin) && placeholder.getTextPlaceholder().equals(textPlaceholder)) { + iter.remove(); + + for (DynamicLineData data : PlaceholdersManager.linesToUpdate) { + if (data.getPlaceholders().contains(placeholder)) { + data.getPlaceholders().remove(placeholder); + } + } + + return true; + } + } + + return false; + } + + protected static Set getPlaceholders() { + return placeholders; + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/task/BungeeCleanupTask.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/task/BungeeCleanupTask.java index 53d8f845..55c8d6f0 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/task/BungeeCleanupTask.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/task/BungeeCleanupTask.java @@ -1,37 +1,51 @@ -package com.gmail.filoghost.holographicdisplays.task; - -import java.util.Iterator; -import java.util.Map.Entry; -import java.util.concurrent.TimeUnit; -import java.util.logging.Level; - -import com.gmail.filoghost.holographicdisplays.bridge.bungeecord.BungeeServerInfo; -import com.gmail.filoghost.holographicdisplays.bridge.bungeecord.BungeeServerTracker; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; - -/** - * A task to remove unused server data in the server tracker. - */ -public class BungeeCleanupTask implements Runnable { - - private static final long MAX_INACTIVITY = TimeUnit.MINUTES.toMillis(10); - - @Override - public void run() { - - long now = System.currentTimeMillis(); - Iterator> iter = BungeeServerTracker.getTrackedServers().entrySet().iterator(); - - while (iter.hasNext()) { - Entry next = iter.next(); - long lastRequest = next.getValue().getLastRequest(); - - if (lastRequest != 0 && now - lastRequest > MAX_INACTIVITY) { - // Don't track that server anymore. - iter.remove(); - ConsoleLogger.logDebug(Level.INFO, "Removed bungee server \"" + next.getKey() + "\" from tracking due to inactivity."); - } - } - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.task; + +import java.util.Iterator; +import java.util.Map.Entry; +import java.util.concurrent.TimeUnit; +import java.util.logging.Level; + +import com.gmail.filoghost.holographicdisplays.bridge.bungeecord.BungeeServerInfo; +import com.gmail.filoghost.holographicdisplays.bridge.bungeecord.BungeeServerTracker; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; + +/** + * A task to remove unused server data in the server tracker. + */ +public class BungeeCleanupTask implements Runnable { + + private static final long MAX_INACTIVITY = TimeUnit.MINUTES.toMillis(10); + + @Override + public void run() { + + long now = System.currentTimeMillis(); + Iterator> iter = BungeeServerTracker.getTrackedServers().entrySet().iterator(); + + while (iter.hasNext()) { + Entry next = iter.next(); + long lastRequest = next.getValue().getLastRequest(); + + if (lastRequest != 0 && now - lastRequest > MAX_INACTIVITY) { + // Don't track that server anymore. + iter.remove(); + ConsoleLogger.logDebug(Level.INFO, "Removed bungee server \"" + next.getKey() + "\" from tracking due to inactivity."); + } + } + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/task/StartupLoadHologramsTask.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/task/StartupLoadHologramsTask.java index 931584d8..2e64b044 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/task/StartupLoadHologramsTask.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/task/StartupLoadHologramsTask.java @@ -1,15 +1,29 @@ -package com.gmail.filoghost.holographicdisplays.task; - -import com.gmail.filoghost.holographicdisplays.object.NamedHologram; -import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; - -public class StartupLoadHologramsTask implements Runnable { - - @Override - public void run() { - for (NamedHologram hologram : NamedHologramManager.getHolograms()) { - hologram.refreshAll(); - } - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.task; + +import com.gmail.filoghost.holographicdisplays.object.NamedHologram; +import com.gmail.filoghost.holographicdisplays.object.NamedHologramManager; + +public class StartupLoadHologramsTask implements Runnable { + + @Override + public void run() { + for (NamedHologram hologram : NamedHologramManager.getHolograms()) { + hologram.refreshAll(); + } + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/task/WorldPlayerCounterTask.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/task/WorldPlayerCounterTask.java index 798db927..b31c8bde 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/task/WorldPlayerCounterTask.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/task/WorldPlayerCounterTask.java @@ -1,37 +1,51 @@ -package com.gmail.filoghost.holographicdisplays.task; - -import java.util.List; -import java.util.Map; - -import org.bukkit.Bukkit; -import org.bukkit.World; -import org.bukkit.entity.Player; - -import com.gmail.filoghost.holographicdisplays.util.Utils; - -public class WorldPlayerCounterTask implements Runnable { - - private static Map worlds = Utils.newMap(); - - @Override - public void run() { - worlds.clear(); - - for (World world : Bukkit.getWorlds()) { - List players = world.getPlayers(); - int count = 0; - - for (Player player : players) { - if (!player.hasMetadata("NPC")) { - count++; - } - } - worlds.put(world.getName(), count); - } - } - - public static String getCount(String world) { - Integer count = worlds.get(world); - return count != null ? count.toString() : "[World \"" + world + "\" not found]"; - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.task; + +import java.util.List; +import java.util.Map; + +import org.bukkit.Bukkit; +import org.bukkit.World; +import org.bukkit.entity.Player; + +import com.gmail.filoghost.holographicdisplays.util.Utils; + +public class WorldPlayerCounterTask implements Runnable { + + private static Map worlds = Utils.newMap(); + + @Override + public void run() { + worlds.clear(); + + for (World world : Bukkit.getWorlds()) { + List players = world.getPlayers(); + int count = 0; + + for (Player player : players) { + if (!player.hasMetadata("NPC")) { + count++; + } + } + worlds.put(world.getName(), count); + } + } + + public static String getCount(String world) { + Integer count = worlds.get(world); + return count != null ? count.toString() : "[World \"" + world + "\" not found]"; + } +} diff --git a/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/exception/UnreadableImageException.java b/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/exception/UnreadableImageException.java index c32b5fa6..0a1b9787 100644 --- a/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/exception/UnreadableImageException.java +++ b/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/exception/UnreadableImageException.java @@ -1,7 +1,21 @@ -package com.gmail.filoghost.holographicdisplays.exception; - -public class UnreadableImageException extends Exception { - - private static final long serialVersionUID = 1L; - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.exception; + +public class UnreadableImageException extends Exception { + + private static final long serialVersionUID = 1L; + +} diff --git a/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/ConsoleLogger.java b/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/ConsoleLogger.java index f399558a..54964641 100644 --- a/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/ConsoleLogger.java +++ b/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/ConsoleLogger.java @@ -1,49 +1,63 @@ -package com.gmail.filoghost.holographicdisplays.util; - -import java.util.logging.Level; -import java.util.logging.Logger; - -import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; - -public class ConsoleLogger { - - private static boolean debug; - private static Logger logger; - - public static void setLogger(Logger logger) { - ConsoleLogger.logger = logger; - } - - public static void setDebugEnabled(boolean enabled) { - debug = enabled; - } - - public static void log(Level level, String msg, Throwable thrown) { - if (logger != null) { - logger.log(level, msg, thrown); - } - } - - public static void log(Level level, String msg) { - log(level, msg, null); - } - - public static void logDebug(Level level, String msg, Throwable thrown) { - if (debug) { - log(level, "[Debug] " + msg, thrown); - } - } - - public static void logDebug(Level level, String msg) { - logDebug(level, msg, null); - } - - public static void logDebugException(Throwable thrown) { - thrown.printStackTrace(); - } - - public static void handleSpawnFail(HologramLine parentPiece) { - logDebug(Level.WARNING, "Coulnd't spawn entity for this hologram: " + parentPiece.getParent().toString()); - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.util; + +import java.util.logging.Level; +import java.util.logging.Logger; + +import com.gmail.filoghost.holographicdisplays.api.line.HologramLine; + +public class ConsoleLogger { + + private static boolean debug; + private static Logger logger; + + public static void setLogger(Logger logger) { + ConsoleLogger.logger = logger; + } + + public static void setDebugEnabled(boolean enabled) { + debug = enabled; + } + + public static void log(Level level, String msg, Throwable thrown) { + if (logger != null) { + logger.log(level, msg, thrown); + } + } + + public static void log(Level level, String msg) { + log(level, msg, null); + } + + public static void logDebug(Level level, String msg, Throwable thrown) { + if (debug) { + log(level, "[Debug] " + msg, thrown); + } + } + + public static void logDebug(Level level, String msg) { + logDebug(level, msg, null); + } + + public static void logDebugException(Throwable thrown) { + thrown.printStackTrace(); + } + + public static void handleSpawnFail(HologramLine parentPiece) { + logDebug(Level.WARNING, "Coulnd't spawn entity for this hologram: " + parentPiece.getParent().toString()); + } + +} diff --git a/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/FileUtils.java b/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/FileUtils.java index 7e7eb475..bfa8fe63 100644 --- a/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/FileUtils.java +++ b/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/FileUtils.java @@ -1,93 +1,107 @@ -package com.gmail.filoghost.holographicdisplays.util; - -import java.awt.image.BufferedImage; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.net.URL; -import java.util.ArrayList; -import java.util.List; - -import javax.imageio.ImageIO; - -import com.gmail.filoghost.holographicdisplays.exception.UnreadableImageException; - - -public class FileUtils { - - public static List readLines(File file) throws IOException, Exception { - - if (!file.isFile()) { - throw new FileNotFoundException(file.getName()); - } - - BufferedReader br = null; - - try { - - List lines = new ArrayList(); - - if (!file.exists()) { - throw new FileNotFoundException(); - } - - br = new BufferedReader(new FileReader(file)); - String line = br.readLine(); - - while (line != null) { - lines.add(line); - line = br.readLine(); - } - - return lines; - - } finally { - if (br != null) { - try { - br.close(); - } catch (IOException e) { } - } - } - } - - public static BufferedImage readImage(File file) throws UnreadableImageException, IOException, Exception { - - if (!file.isFile()) { - throw new FileNotFoundException(file.getName()); - } - - BufferedImage image = ImageIO.read(file); - - if (image == null) { - throw new UnreadableImageException(); - } - - return image; - } - - public static BufferedImage readImage(URL url) throws UnreadableImageException, IOException, Exception { - - BufferedImage image = ImageIO.read(url); - - if (image == null) { - throw new UnreadableImageException(); - } - - return image; - } - - public static boolean isParentFolder(File folder, File file) throws IOException { - File iteratorFile = file.getCanonicalFile(); - folder = folder.getCanonicalFile(); - while ((iteratorFile = iteratorFile.getParentFile()) != null) { - if (iteratorFile.equals(folder)) { - return true; - } - } - - return false; - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.util; + +import java.awt.image.BufferedImage; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +import javax.imageio.ImageIO; + +import com.gmail.filoghost.holographicdisplays.exception.UnreadableImageException; + + +public class FileUtils { + + public static List readLines(File file) throws IOException, Exception { + + if (!file.isFile()) { + throw new FileNotFoundException(file.getName()); + } + + BufferedReader br = null; + + try { + + List lines = new ArrayList(); + + if (!file.exists()) { + throw new FileNotFoundException(); + } + + br = new BufferedReader(new FileReader(file)); + String line = br.readLine(); + + while (line != null) { + lines.add(line); + line = br.readLine(); + } + + return lines; + + } finally { + if (br != null) { + try { + br.close(); + } catch (IOException e) { } + } + } + } + + public static BufferedImage readImage(File file) throws UnreadableImageException, IOException, Exception { + + if (!file.isFile()) { + throw new FileNotFoundException(file.getName()); + } + + BufferedImage image = ImageIO.read(file); + + if (image == null) { + throw new UnreadableImageException(); + } + + return image; + } + + public static BufferedImage readImage(URL url) throws UnreadableImageException, IOException, Exception { + + BufferedImage image = ImageIO.read(url); + + if (image == null) { + throw new UnreadableImageException(); + } + + return image; + } + + public static boolean isParentFolder(File folder, File file) throws IOException { + File iteratorFile = file.getCanonicalFile(); + folder = folder.getCanonicalFile(); + while ((iteratorFile = iteratorFile.getParentFile()) != null) { + if (iteratorFile.equals(folder)) { + return true; + } + } + + return false; + } + +} diff --git a/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/ItemUtils.java b/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/ItemUtils.java index c52c1fd3..7b4c71da 100644 --- a/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/ItemUtils.java +++ b/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/ItemUtils.java @@ -1,106 +1,120 @@ -package com.gmail.filoghost.holographicdisplays.util; - -import java.util.HashMap; -import java.util.Map; -import java.util.regex.Pattern; - -import org.bukkit.ChatColor; -import org.bukkit.Material; - -public class ItemUtils { - - // This is used on hologram icons, to prevent vanilla items from merging with them. - public static final String ANTISTACK_LORE = ChatColor.BLACK.toString() + Math.random(); - - // A map with formatted materials (lowercase and without symbols) for fast access. - private static final Map NAMES_TO_MATERIALS = new HashMap(); - - // The chars that will be ignored when matching materials. - private static final Pattern STRIP_SPACING_SYMBOLS_PATTERN = Pattern.compile("[_ \\-]+"); - - static { - // Add default materials. - for (Material mat : Material.values()) { - NAMES_TO_MATERIALS.put(stripSpacingChars(mat.toString()).toLowerCase(), mat); - } - - // Only add aliases before 1.13. - if (!NMSVersion.isGreaterEqualThan(NMSVersion.v1_13_R1)) { - // Default material names are not intuitive and sometimes confusing. - addAlias("iron bar", "IRON_FENCE"); - addAlias("iron bars", "IRON_FENCE"); - addAlias("glass pane", "THIN_GLASS"); - addAlias("nether wart", "NETHER_STALK"); - addAlias("nether warts", "NETHER_STALK"); - addAlias("slab", "STEP"); - addAlias("double slab", "DOUBLE_STEP"); - addAlias("stone brick", "SMOOTH_BRICK"); - addAlias("stone bricks", "SMOOTH_BRICK"); - addAlias("stone stair", "SMOOTH_STAIRS"); - addAlias("stone stairs", "SMOOTH_STAIRS"); - addAlias("potato", "POTATO_ITEM"); - addAlias("carrot", "CARROT_ITEM"); - addAlias("brewing stand", "BREWING_STAND_ITEM"); - addAlias("cauldron", "CAULDRON_ITEM"); - addAlias("carrot on stick", "CARROT_STICK"); - addAlias("carrot on a stick", "CARROT_STICK"); - addAlias("cobblestone wall", "COBBLE_WALL"); - addAlias("wood slab", "WOOD_STEP"); - addAlias("double wood slab", "WOOD_DOUBLE_STEP"); - addAlias("repeater", "DIODE"); - addAlias("piston", "PISTON_BASE"); - addAlias("sticky piston", "PISTON_STICKY_BASE"); - addAlias("flower pot", "FLOWER_POT_ITEM"); - addAlias("wood showel", "WOOD_SPADE"); - addAlias("stone showel", "STONE_SPADE"); - addAlias("gold showel", "GOLD_SPADE"); - addAlias("iron showel", "IRON_SPADE"); - addAlias("diamond showel", "DIAMOND_SPADE"); - addAlias("steak", "COOKED_BEEF"); - addAlias("cooked porkchop", "GRILLED_PORK"); - addAlias("raw porkchop", "PORK"); - addAlias("hardened clay", "HARD_CLAY"); - addAlias("huge brown mushroom", "HUGE_MUSHROOM_1"); - addAlias("huge red mushroom", "HUGE_MUSHROOM_2"); - addAlias("mycelium", "MYCEL"); - addAlias("poppy", "RED_ROSE"); - addAlias("comparator", "REDSTONE_COMPARATOR"); - addAlias("skull", "SKULL_ITEM"); - addAlias("head", "SKULL_ITEM"); - addAlias("redstone torch", "REDSTONE_TORCH_ON"); - addAlias("redstone lamp", "REDSTONE_LAMP_OFF"); - addAlias("glistering melon", "SPECKLED_MELON"); - addAlias("gunpowder", "SULPHUR"); - addAlias("lilypad", "WATER_LILY"); - addAlias("command block", "COMMAND"); - addAlias("dye", "INK_SACK"); - } - } - - private static void addAlias(String alias, String material) { - try { - NAMES_TO_MATERIALS.put(stripSpacingChars(alias).toLowerCase(), Material.valueOf(material)); - } catch (IllegalArgumentException e) { - // Not found, do nothing. - } - } - - public static String stripSpacingChars(String input) { - return STRIP_SPACING_SYMBOLS_PATTERN.matcher(input).replaceAll(""); - } - - @SuppressWarnings("deprecation") - public static Material matchMaterial(String input) { - if (!NMSVersion.isGreaterEqualThan(NMSVersion.v1_13_R1)) { - // Before 1.13, allow IDs as materials. - try { - return Material.getMaterial(Integer.parseInt(input)); - } catch (NumberFormatException e) { - // Not a number, ignore and go on. - } - } - - return NAMES_TO_MATERIALS.get(stripSpacingChars(input).toLowerCase()); - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.util; + +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Pattern; + +import org.bukkit.ChatColor; +import org.bukkit.Material; + +public class ItemUtils { + + // This is used on hologram icons, to prevent vanilla items from merging with them. + public static final String ANTISTACK_LORE = ChatColor.BLACK.toString() + Math.random(); + + // A map with formatted materials (lowercase and without symbols) for fast access. + private static final Map NAMES_TO_MATERIALS = new HashMap(); + + // The chars that will be ignored when matching materials. + private static final Pattern STRIP_SPACING_SYMBOLS_PATTERN = Pattern.compile("[_ \\-]+"); + + static { + // Add default materials. + for (Material mat : Material.values()) { + NAMES_TO_MATERIALS.put(stripSpacingChars(mat.toString()).toLowerCase(), mat); + } + + // Only add aliases before 1.13. + if (!NMSVersion.isGreaterEqualThan(NMSVersion.v1_13_R1)) { + // Default material names are not intuitive and sometimes confusing. + addAlias("iron bar", "IRON_FENCE"); + addAlias("iron bars", "IRON_FENCE"); + addAlias("glass pane", "THIN_GLASS"); + addAlias("nether wart", "NETHER_STALK"); + addAlias("nether warts", "NETHER_STALK"); + addAlias("slab", "STEP"); + addAlias("double slab", "DOUBLE_STEP"); + addAlias("stone brick", "SMOOTH_BRICK"); + addAlias("stone bricks", "SMOOTH_BRICK"); + addAlias("stone stair", "SMOOTH_STAIRS"); + addAlias("stone stairs", "SMOOTH_STAIRS"); + addAlias("potato", "POTATO_ITEM"); + addAlias("carrot", "CARROT_ITEM"); + addAlias("brewing stand", "BREWING_STAND_ITEM"); + addAlias("cauldron", "CAULDRON_ITEM"); + addAlias("carrot on stick", "CARROT_STICK"); + addAlias("carrot on a stick", "CARROT_STICK"); + addAlias("cobblestone wall", "COBBLE_WALL"); + addAlias("wood slab", "WOOD_STEP"); + addAlias("double wood slab", "WOOD_DOUBLE_STEP"); + addAlias("repeater", "DIODE"); + addAlias("piston", "PISTON_BASE"); + addAlias("sticky piston", "PISTON_STICKY_BASE"); + addAlias("flower pot", "FLOWER_POT_ITEM"); + addAlias("wood showel", "WOOD_SPADE"); + addAlias("stone showel", "STONE_SPADE"); + addAlias("gold showel", "GOLD_SPADE"); + addAlias("iron showel", "IRON_SPADE"); + addAlias("diamond showel", "DIAMOND_SPADE"); + addAlias("steak", "COOKED_BEEF"); + addAlias("cooked porkchop", "GRILLED_PORK"); + addAlias("raw porkchop", "PORK"); + addAlias("hardened clay", "HARD_CLAY"); + addAlias("huge brown mushroom", "HUGE_MUSHROOM_1"); + addAlias("huge red mushroom", "HUGE_MUSHROOM_2"); + addAlias("mycelium", "MYCEL"); + addAlias("poppy", "RED_ROSE"); + addAlias("comparator", "REDSTONE_COMPARATOR"); + addAlias("skull", "SKULL_ITEM"); + addAlias("head", "SKULL_ITEM"); + addAlias("redstone torch", "REDSTONE_TORCH_ON"); + addAlias("redstone lamp", "REDSTONE_LAMP_OFF"); + addAlias("glistering melon", "SPECKLED_MELON"); + addAlias("gunpowder", "SULPHUR"); + addAlias("lilypad", "WATER_LILY"); + addAlias("command block", "COMMAND"); + addAlias("dye", "INK_SACK"); + } + } + + private static void addAlias(String alias, String material) { + try { + NAMES_TO_MATERIALS.put(stripSpacingChars(alias).toLowerCase(), Material.valueOf(material)); + } catch (IllegalArgumentException e) { + // Not found, do nothing. + } + } + + public static String stripSpacingChars(String input) { + return STRIP_SPACING_SYMBOLS_PATTERN.matcher(input).replaceAll(""); + } + + @SuppressWarnings("deprecation") + public static Material matchMaterial(String input) { + if (!NMSVersion.isGreaterEqualThan(NMSVersion.v1_13_R1)) { + // Before 1.13, allow IDs as materials. + try { + return Material.getMaterial(Integer.parseInt(input)); + } catch (NumberFormatException e) { + // Not a number, ignore and go on. + } + } + + return NAMES_TO_MATERIALS.get(stripSpacingChars(input).toLowerCase()); + } + +} diff --git a/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/NMSVersion.java b/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/NMSVersion.java index ab9fcba1..6a1f64e3 100644 --- a/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/NMSVersion.java +++ b/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/NMSVersion.java @@ -1,60 +1,74 @@ -package com.gmail.filoghost.holographicdisplays.util; - -/** - * The NMS version is the name of the main package under net.minecraft.server. - */ -public enum NMSVersion { - - v1_8_R1, - v1_8_R2, - v1_8_R3, - v1_9_R1, - v1_9_R2, - v1_10_R1, - v1_11_R1, - v1_12_R1, - v1_13_R1, - v1_13_R2; - - private static final NMSVersion CURRENT_VERSION = extractCurrentVersion(); - - - private static NMSVersion extractCurrentVersion() { - String nmsVersionName = VersionUtils.extractNMSVersion(); - - if (nmsVersionName != null) { - try { - return valueOf(nmsVersionName); - } catch (IllegalArgumentException e) { - return null; - } - } else { - return null; - } - } - - - public static boolean isValid() { - return CURRENT_VERSION != null; - } - - - public static NMSVersion getCurrent() { - if (CURRENT_VERSION == null) { - throw new IllegalStateException("Current version not set"); - } - return CURRENT_VERSION; - } - - - public static boolean isGreaterEqualThan(NMSVersion other) { - return getCurrent().ordinal() >= other.ordinal(); - } - - - public static boolean isBetween(NMSVersion from, NMSVersion to) { - return from.ordinal() <= getCurrent().ordinal() && getCurrent().ordinal() <= to.ordinal(); - } - - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.util; + +/** + * The NMS version is the name of the main package under net.minecraft.server. + */ +public enum NMSVersion { + + v1_8_R1, + v1_8_R2, + v1_8_R3, + v1_9_R1, + v1_9_R2, + v1_10_R1, + v1_11_R1, + v1_12_R1, + v1_13_R1, + v1_13_R2; + + private static final NMSVersion CURRENT_VERSION = extractCurrentVersion(); + + + private static NMSVersion extractCurrentVersion() { + String nmsVersionName = VersionUtils.extractNMSVersion(); + + if (nmsVersionName != null) { + try { + return valueOf(nmsVersionName); + } catch (IllegalArgumentException e) { + return null; + } + } else { + return null; + } + } + + + public static boolean isValid() { + return CURRENT_VERSION != null; + } + + + public static NMSVersion getCurrent() { + if (CURRENT_VERSION == null) { + throw new IllegalStateException("Current version not set"); + } + return CURRENT_VERSION; + } + + + public static boolean isGreaterEqualThan(NMSVersion other) { + return getCurrent().ordinal() >= other.ordinal(); + } + + + public static boolean isBetween(NMSVersion from, NMSVersion to) { + return from.ordinal() <= getCurrent().ordinal() && getCurrent().ordinal() <= to.ordinal(); + } + + +} diff --git a/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/Offsets.java b/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/Offsets.java index f3b979b6..0c62172e 100644 --- a/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/Offsets.java +++ b/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/Offsets.java @@ -1,51 +1,65 @@ -package com.gmail.filoghost.holographicdisplays.util; - -/** - * When spawning a hologram at a location, the top part of the first line should be exactly on that location. - * The second line is below the first, and so on. - */ -public class Offsets { - - public static final double - - // A single armor stand. - ARMOR_STAND_ALONE = getArmorStandAloneOffset(), - - // An armor stand holding an item. - ARMOR_STAND_WITH_ITEM = getArmorStandWithItemOffset(), - - // An armor stand holding a slime. - ARMOR_STAND_WITH_SLIME = getArmorStandWithSlimeOffset(); - - - private static double getArmorStandAloneOffset() { - if (NMSVersion.getCurrent() == NMSVersion.v1_8_R1) { - // When the NBT tag "Marker" was not implemented - return -1.25; - } else { - return -0.29; - } - } - - - private static double getArmorStandWithItemOffset() { - if (NMSVersion.getCurrent() == NMSVersion.v1_8_R1) { - // When the NBT tag "Marker" was not implemented - return -1.48; - } else { - return 0; - } - } - - - private static double getArmorStandWithSlimeOffset() { - if (NMSVersion.getCurrent() == NMSVersion.v1_8_R1) { - // When the NBT tag "Marker" was not implemented - return -1.48; - } else { - return 0; - } - } - - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.util; + +/** + * When spawning a hologram at a location, the top part of the first line should be exactly on that location. + * The second line is below the first, and so on. + */ +public class Offsets { + + public static final double + + // A single armor stand. + ARMOR_STAND_ALONE = getArmorStandAloneOffset(), + + // An armor stand holding an item. + ARMOR_STAND_WITH_ITEM = getArmorStandWithItemOffset(), + + // An armor stand holding a slime. + ARMOR_STAND_WITH_SLIME = getArmorStandWithSlimeOffset(); + + + private static double getArmorStandAloneOffset() { + if (NMSVersion.getCurrent() == NMSVersion.v1_8_R1) { + // When the NBT tag "Marker" was not implemented + return -1.25; + } else { + return -0.29; + } + } + + + private static double getArmorStandWithItemOffset() { + if (NMSVersion.getCurrent() == NMSVersion.v1_8_R1) { + // When the NBT tag "Marker" was not implemented + return -1.48; + } else { + return 0; + } + } + + + private static double getArmorStandWithSlimeOffset() { + if (NMSVersion.getCurrent() == NMSVersion.v1_8_R1) { + // When the NBT tag "Marker" was not implemented + return -1.48; + } else { + return 0; + } + } + + +} diff --git a/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/Utils.java b/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/Utils.java index 0b4a8ad3..5b857cf2 100644 --- a/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/Utils.java +++ b/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/Utils.java @@ -1,134 +1,148 @@ -package com.gmail.filoghost.holographicdisplays.util; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import org.bukkit.ChatColor; - -public class Utils extends Object { - - /** - * Converts a generic array to an array of Strings using the method toString(). - * @param array the array to convert - * @return the new generated array of Strings - */ - public static String[] arrayToStrings(Object... array) { - String[] result = new String[array.length]; - for (int i = 0; i < array.length; i++) { - result[i] = array[i] != null ? array[i].toString() : null; - } - - return result; - } - - - /** - * Convenience method to add colors to a string. - * @param text the text to colorize - * @return the colorized text, or null if text was null - */ - public static String addColors(String text) { - if (text == null) { - return null; - } - - return ChatColor.translateAlternateColorCodes('&', text); - } - - - public static boolean containsIgnoreCase(String toCheck, String content) { - return toCheck.toLowerCase().contains(content.toLowerCase()); - } - - - public static Map newMap() { - return new HashMap(); - } - - public static List newList() { - return new ArrayList(); - } - - public static Set newSet() { - return new HashSet(); - } - - - public static int floor(double num) { - int floor = (int) num; - return floor == num ? floor : floor - (int) (Double.doubleToRawLongBits(num) >>> 63); - } - - - public static double square(double num) { - return num * num; - } - - - public static String join(String[] elements, String separator, int startIndex, int endIndex) { - Validator.isTrue(startIndex >= 0 && startIndex < elements.length, "startIndex out of bounds"); - Validator.isTrue(endIndex >= 0 && endIndex <= elements.length, "endIndex out of bounds"); - Validator.isTrue(startIndex <= endIndex, "startIndex lower than endIndex"); - - StringBuilder result = new StringBuilder(); - - while (startIndex < endIndex) { - if (result.length() != 0) { - result.append(separator); - } - - if (elements[startIndex] != null) { - result.append(elements[startIndex]); - } - startIndex++; - } - - return result.toString(); - } - - public static String join(String[] elements, String separator) { - return join(elements, separator, 0, elements.length); - } - - public static String join(List elements, String separator, int startIndex, int size) { - return join(elements.toArray(new String[elements.size()]), separator, startIndex, size); - } - - public static String join(List elements, String separator) { - return join(elements, separator, 0, elements.size()); - } - - - public static String sanitize(String s) { - return s != null ? s : "null"; - } - - - public static boolean isThereNonNull(Object... objects) { - if (objects == null) { - return false; - } - - for (int i = 0; i < objects.length; i++) { - if (objects[i] != null) { - return true; - } - } - - return false; - } - - - public static boolean classExists(String className) { - try { - Class.forName(className); - return true; - } catch (Throwable t) { - return false; - } - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.util; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.bukkit.ChatColor; + +public class Utils extends Object { + + /** + * Converts a generic array to an array of Strings using the method toString(). + * @param array the array to convert + * @return the new generated array of Strings + */ + public static String[] arrayToStrings(Object... array) { + String[] result = new String[array.length]; + for (int i = 0; i < array.length; i++) { + result[i] = array[i] != null ? array[i].toString() : null; + } + + return result; + } + + + /** + * Convenience method to add colors to a string. + * @param text the text to colorize + * @return the colorized text, or null if text was null + */ + public static String addColors(String text) { + if (text == null) { + return null; + } + + return ChatColor.translateAlternateColorCodes('&', text); + } + + + public static boolean containsIgnoreCase(String toCheck, String content) { + return toCheck.toLowerCase().contains(content.toLowerCase()); + } + + + public static Map newMap() { + return new HashMap(); + } + + public static List newList() { + return new ArrayList(); + } + + public static Set newSet() { + return new HashSet(); + } + + + public static int floor(double num) { + int floor = (int) num; + return floor == num ? floor : floor - (int) (Double.doubleToRawLongBits(num) >>> 63); + } + + + public static double square(double num) { + return num * num; + } + + + public static String join(String[] elements, String separator, int startIndex, int endIndex) { + Validator.isTrue(startIndex >= 0 && startIndex < elements.length, "startIndex out of bounds"); + Validator.isTrue(endIndex >= 0 && endIndex <= elements.length, "endIndex out of bounds"); + Validator.isTrue(startIndex <= endIndex, "startIndex lower than endIndex"); + + StringBuilder result = new StringBuilder(); + + while (startIndex < endIndex) { + if (result.length() != 0) { + result.append(separator); + } + + if (elements[startIndex] != null) { + result.append(elements[startIndex]); + } + startIndex++; + } + + return result.toString(); + } + + public static String join(String[] elements, String separator) { + return join(elements, separator, 0, elements.length); + } + + public static String join(List elements, String separator, int startIndex, int size) { + return join(elements.toArray(new String[elements.size()]), separator, startIndex, size); + } + + public static String join(List elements, String separator) { + return join(elements, separator, 0, elements.size()); + } + + + public static String sanitize(String s) { + return s != null ? s : "null"; + } + + + public static boolean isThereNonNull(Object... objects) { + if (objects == null) { + return false; + } + + for (int i = 0; i < objects.length; i++) { + if (objects[i] != null) { + return true; + } + } + + return false; + } + + + public static boolean classExists(String className) { + try { + Class.forName(className); + return true; + } catch (Throwable t) { + return false; + } + } +} diff --git a/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/Validator.java b/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/Validator.java index 6b6405ff..7e93edfe 100644 --- a/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/Validator.java +++ b/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/Validator.java @@ -1,17 +1,31 @@ -package com.gmail.filoghost.holographicdisplays.util; - -public class Validator { - - public static void notNull(Object o, String name) { - if (o == null) { - throw new NullPointerException(name + " cannot be null"); - } - } - - public static void isTrue(boolean statement, String message) { - if (!statement) { - throw new IllegalArgumentException(message); - } - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.util; + +public class Validator { + + public static void notNull(Object o, String name) { + if (o == null) { + throw new NullPointerException(name + " cannot be null"); + } + } + + public static void isTrue(boolean statement, String message) { + if (!statement) { + throw new IllegalArgumentException(message); + } + } + +} diff --git a/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/VersionUtils.java b/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/VersionUtils.java index 876533aa..acc2f7cc 100644 --- a/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/VersionUtils.java +++ b/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/VersionUtils.java @@ -1,82 +1,96 @@ -package com.gmail.filoghost.holographicdisplays.util; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import org.bukkit.Bukkit; - -public class VersionUtils { - - private static final boolean IS_PAPER_SERVER = Bukkit.getName().equals("Paper"); - - - /** - * Paper contains some changes that require - */ - public static boolean isPaperServer() { - return IS_PAPER_SERVER; - } - - - /** - * This method uses a regex to get the NMS package part that changes with every update. - * Example: v1_8_R1 - * @return the NMS package part or null if not found. - */ - public static String extractNMSVersion() { - Matcher matcher = Pattern.compile("v\\d+_\\d+_R\\d+").matcher(Bukkit.getServer().getClass().getPackage().getName()); - if (matcher.find()) { - return matcher.group(); - } else { - return null; - } - } - - - /** - * @return 1 if reference > comparison, 0 if reference == comparison, -1 if reference < comparison - */ - private static int compare(String reference, String comparison) throws NumberFormatException { - String[] referenceSplit = reference.split("\\."); - String[] comparisonSplit = comparison.split("\\."); - - int longest = Math.max(referenceSplit.length, comparisonSplit.length); - - // Default value is 0 - int[] referenceNumbersArray = new int[longest]; - int[] comparisonNumbersArray = new int[longest]; - - for (int i = 0; i < referenceSplit.length; i++) { - referenceNumbersArray[i] = Integer.parseInt(referenceSplit[i]); - } - - for (int i = 0; i < comparisonSplit.length; i++) { - comparisonNumbersArray[i] = Integer.parseInt(comparisonSplit[i]); - } - - for (int i = 0; i < longest; i++) { - int diff = referenceNumbersArray[i] - comparisonNumbersArray[i]; - if (diff > 0) { - return 1; - } else if (diff < 0) { - return -1; - } - } - - return 0; - } - - - public static boolean isVersionGreaterEqual(String reference, String thanWhat) { - return compare(reference, thanWhat) >= 0; - } - - public static boolean isVersionLessEqual(String reference, String thanWhat) { - return compare(reference, thanWhat) <= 0; - } - - public static boolean isVersionBetweenEqual(String reference, String lowest, String highest) { - return isVersionGreaterEqual(reference, lowest) && isVersionLessEqual(reference, highest); - } - -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.util; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.bukkit.Bukkit; + +public class VersionUtils { + + private static final boolean IS_PAPER_SERVER = Bukkit.getName().equals("Paper"); + + + /** + * Paper contains some changes that require + */ + public static boolean isPaperServer() { + return IS_PAPER_SERVER; + } + + + /** + * This method uses a regex to get the NMS package part that changes with every update. + * Example: v1_8_R1 + * @return the NMS package part or null if not found. + */ + public static String extractNMSVersion() { + Matcher matcher = Pattern.compile("v\\d+_\\d+_R\\d+").matcher(Bukkit.getServer().getClass().getPackage().getName()); + if (matcher.find()) { + return matcher.group(); + } else { + return null; + } + } + + + /** + * @return 1 if reference > comparison, 0 if reference == comparison, -1 if reference < comparison + */ + private static int compare(String reference, String comparison) throws NumberFormatException { + String[] referenceSplit = reference.split("\\."); + String[] comparisonSplit = comparison.split("\\."); + + int longest = Math.max(referenceSplit.length, comparisonSplit.length); + + // Default value is 0 + int[] referenceNumbersArray = new int[longest]; + int[] comparisonNumbersArray = new int[longest]; + + for (int i = 0; i < referenceSplit.length; i++) { + referenceNumbersArray[i] = Integer.parseInt(referenceSplit[i]); + } + + for (int i = 0; i < comparisonSplit.length; i++) { + comparisonNumbersArray[i] = Integer.parseInt(comparisonSplit[i]); + } + + for (int i = 0; i < longest; i++) { + int diff = referenceNumbersArray[i] - comparisonNumbersArray[i]; + if (diff > 0) { + return 1; + } else if (diff < 0) { + return -1; + } + } + + return 0; + } + + + public static boolean isVersionGreaterEqual(String reference, String thanWhat) { + return compare(reference, thanWhat) >= 0; + } + + public static boolean isVersionLessEqual(String reference, String thanWhat) { + return compare(reference, thanWhat) <= 0; + } + + public static boolean isVersionBetweenEqual(String reference, String lowest, String highest) { + return isVersionGreaterEqual(reference, lowest) && isVersionLessEqual(reference, highest); + } + +} diff --git a/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/reflection/ReflectField.java b/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/reflection/ReflectField.java index 691ec5e2..63753108 100644 --- a/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/reflection/ReflectField.java +++ b/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/reflection/ReflectField.java @@ -1,3 +1,17 @@ +/* + * 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 . + */ package com.gmail.filoghost.holographicdisplays.util.reflection; import java.lang.reflect.Field; diff --git a/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/reflection/ReflectMethod.java b/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/reflection/ReflectMethod.java index 6d6e6efa..346b5d85 100644 --- a/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/reflection/ReflectMethod.java +++ b/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/reflection/ReflectMethod.java @@ -1,3 +1,17 @@ +/* + * 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 . + */ package com.gmail.filoghost.holographicdisplays.util.reflection; import java.lang.reflect.Method; diff --git a/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/reflection/ReflectionUtils.java b/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/reflection/ReflectionUtils.java index 30c8e76a..8f944dfb 100644 --- a/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/reflection/ReflectionUtils.java +++ b/Utils/src/main/java/com/gmail/filoghost/holographicdisplays/util/reflection/ReflectionUtils.java @@ -1,67 +1,81 @@ -package com.gmail.filoghost.holographicdisplays.util.reflection; - -import java.lang.StackWalker.StackFrame; -import java.util.function.Function; -import java.util.logging.Level; -import java.util.stream.Stream; - -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; -import com.gmail.filoghost.holographicdisplays.util.Utils; - -public class ReflectionUtils { - - private static final boolean HAS_JAVA9_STACKWALKER = Utils.classExists("java.lang.StackWalker"); - - private static final ReflectMethod GET_STACKTRACE_DEPTH_METHOD = new ReflectMethod(Throwable.class, "getStackTraceDepth"); - private static final ReflectMethod GET_STACKTRACE_ELEMENT_METHOD = new ReflectMethod(Throwable.class, "getStackTraceElement", int.class); - - private static boolean stackTraceError; - - - /** - * If you only need one stack trace element this is faster than Throwable.getStackTrace()[element], - * it doesn't generate the full stack trace (except as fallback). - */ - public static StackTraceElement getStackTraceElement(final int index) { - // Use the faster methods only if there hasn't been any error while executing them previously - if (!stackTraceError) { - try { - if (HAS_JAVA9_STACKWALKER) { - // Use the Java 9 StackWalker API - StackFrame result = StackWalker.getInstance().walk(new Function, StackFrame>() { - - @Override - public StackFrame apply(Stream stream) { - return stream.skip(index).limit(1).findFirst().orElse(null); - } - - }); - return result != null ? result.toStackTraceElement() : null; - - } else { - // Use reflection to avoid generating the full stack trace - Throwable dummyThrowable = new Throwable(); - int depth = GET_STACKTRACE_DEPTH_METHOD.invoke(dummyThrowable); - - if (index < depth) { - return GET_STACKTRACE_ELEMENT_METHOD.invoke(dummyThrowable, index); - } else { - return null; - } - } - } catch (Throwable t) { - ConsoleLogger.log(Level.WARNING, "Unable to get a stack trace element, please inform the developer. You will only see this error once and a fallback method will be used.", t); - stackTraceError = true; - } - } - - // Fallback slower method, generates the full stack trace (it should never be called if everything works as expected) - StackTraceElement[] fullStackTrace = new Throwable().getStackTrace(); - if (index < fullStackTrace.length) { - return fullStackTrace[index]; - } else { - return null; - } - - } -} +/* + * 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 . + */ +package com.gmail.filoghost.holographicdisplays.util.reflection; + +import java.lang.StackWalker.StackFrame; +import java.util.function.Function; +import java.util.logging.Level; +import java.util.stream.Stream; + +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import com.gmail.filoghost.holographicdisplays.util.Utils; + +public class ReflectionUtils { + + private static final boolean HAS_JAVA9_STACKWALKER = Utils.classExists("java.lang.StackWalker"); + + private static final ReflectMethod GET_STACKTRACE_DEPTH_METHOD = new ReflectMethod(Throwable.class, "getStackTraceDepth"); + private static final ReflectMethod GET_STACKTRACE_ELEMENT_METHOD = new ReflectMethod(Throwable.class, "getStackTraceElement", int.class); + + private static boolean stackTraceError; + + + /** + * If you only need one stack trace element this is faster than Throwable.getStackTrace()[element], + * it doesn't generate the full stack trace (except as fallback). + */ + public static StackTraceElement getStackTraceElement(final int index) { + // Use the faster methods only if there hasn't been any error while executing them previously + if (!stackTraceError) { + try { + if (HAS_JAVA9_STACKWALKER) { + // Use the Java 9 StackWalker API + StackFrame result = StackWalker.getInstance().walk(new Function, StackFrame>() { + + @Override + public StackFrame apply(Stream stream) { + return stream.skip(index).limit(1).findFirst().orElse(null); + } + + }); + return result != null ? result.toStackTraceElement() : null; + + } else { + // Use reflection to avoid generating the full stack trace + Throwable dummyThrowable = new Throwable(); + int depth = GET_STACKTRACE_DEPTH_METHOD.invoke(dummyThrowable); + + if (index < depth) { + return GET_STACKTRACE_ELEMENT_METHOD.invoke(dummyThrowable, index); + } else { + return null; + } + } + } catch (Throwable t) { + ConsoleLogger.log(Level.WARNING, "Unable to get a stack trace element, please inform the developer. You will only see this error once and a fallback method will be used.", t); + stackTraceError = true; + } + } + + // Fallback slower method, generates the full stack trace (it should never be called if everything works as expected) + StackTraceElement[] fullStackTrace = new Throwable().getStackTrace(); + if (index < fullStackTrace.length) { + return fullStackTrace[index]; + } else { + return null; + } + + } +}