From b1f99fd6fe3a283259452c14ca43b1f8f20537b9 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Thu, 4 Mar 2021 16:52:43 -0300 Subject: [PATCH 1/4] refactored code and organized little things. --- .idea/.gitignore | 3 + .idea/DisplayAPI.iml | 9 + .idea/compiler.xml | 16 + .idea/encodings.xml | 7 + .idea/jarRepositories.xml | 20 ++ .idea/misc.xml | 18 + .idea/modules.xml | 8 + .idea/uiDesigner.xml | 124 +++++++ .idea/vcs.xml | 6 + DisplayAPI.iml | 33 ++ pom.xml | 65 ++++ .../listeners/ChunkUnloadListener.java | 15 - .../java}/fr/watch54/displays/Plugin.java | 10 +- .../watch54/displays/holograms/Hologram.java | 123 ++++--- .../holograms/client}/HologramClient.java | 311 +++++++++--------- .../holograms/server}/HologramServer.java | 199 +++++------ .../watch54/displays/interfaces/Action.java | 18 +- .../fr/watch54/displays/interfaces/Text.java | 14 +- .../displays/listeners/Listeners.java} | 123 +++---- .../displays/managers/HologramManager.java | 208 ++++++------ .../watch54/displays/tasks/HologramTask.java | 66 ++-- .../main/java/resources}/plugin.yml | 6 +- 22 files changed, 840 insertions(+), 562 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/DisplayAPI.iml create mode 100644 .idea/compiler.xml create mode 100644 .idea/encodings.xml create mode 100644 .idea/jarRepositories.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/uiDesigner.xml create mode 100644 .idea/vcs.xml create mode 100644 DisplayAPI.iml create mode 100644 pom.xml delete mode 100644 src/fr/watch54/displays/listeners/ChunkUnloadListener.java rename src/{ => main/java}/fr/watch54/displays/Plugin.java (95%) rename src/{ => main/java}/fr/watch54/displays/holograms/Hologram.java (94%) rename src/{fr/watch54/displays/holograms => main/java/fr/watch54/displays/holograms/client}/HologramClient.java (96%) rename src/{fr/watch54/displays/holograms => main/java/fr/watch54/displays/holograms/server}/HologramServer.java (94%) rename src/{ => main/java}/fr/watch54/displays/interfaces/Action.java (93%) rename src/{ => main/java}/fr/watch54/displays/interfaces/Text.java (92%) rename src/{fr/watch54/displays/listeners/InteractListener.java => main/java/fr/watch54/displays/listeners/Listeners.java} (55%) rename src/{ => main/java}/fr/watch54/displays/managers/HologramManager.java (85%) rename src/{ => main/java}/fr/watch54/displays/tasks/HologramTask.java (95%) rename {resources => src/main/java/resources}/plugin.yml (96%) diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/DisplayAPI.iml b/.idea/DisplayAPI.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/DisplayAPI.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..c5a4ea7 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..eeecc00 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..d5a44fd --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/DisplayAPI.iml b/DisplayAPI.iml new file mode 100644 index 0000000..b478335 --- /dev/null +++ b/DisplayAPI.iml @@ -0,0 +1,33 @@ + + + + + + + SPIGOT + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..2a9b242 --- /dev/null +++ b/pom.xml @@ -0,0 +1,65 @@ + + + 4.0.0 + + br.com.redelegit + DisplayAPI + 1.0-SNAPSHOT + + DisplayAPI + + + 1.8 + UTF-8 + + + + clean package + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + ${java.version} + ${java.version} + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + + package + + shade + + + false + + + + + + + + src/main/resources + true + + + + + + + org.spigotmc + spigot-api + 1.8.8-R0.1-SNAPSHOT + system + C:/server/spigot-1.8.8-R0.1-SNAPSHOT-latest.jar + + + + \ No newline at end of file diff --git a/src/fr/watch54/displays/listeners/ChunkUnloadListener.java b/src/fr/watch54/displays/listeners/ChunkUnloadListener.java deleted file mode 100644 index 9df93e8..0000000 --- a/src/fr/watch54/displays/listeners/ChunkUnloadListener.java +++ /dev/null @@ -1,15 +0,0 @@ -package fr.watch54.displays.listeners; - -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.world.ChunkUnloadEvent; - -public class ChunkUnloadListener implements Listener { - - @EventHandler - public void onChunkUnload(ChunkUnloadEvent event){ - event.setCancelled(true); - - } - -} diff --git a/src/fr/watch54/displays/Plugin.java b/src/main/java/fr/watch54/displays/Plugin.java similarity index 95% rename from src/fr/watch54/displays/Plugin.java rename to src/main/java/fr/watch54/displays/Plugin.java index 1cdc791..132b819 100644 --- a/src/fr/watch54/displays/Plugin.java +++ b/src/main/java/fr/watch54/displays/Plugin.java @@ -1,5 +1,5 @@ -package fr.watch54.displays; - -import org.bukkit.plugin.java.JavaPlugin; - -public class Plugin extends JavaPlugin {} +package fr.watch54.displays; + +import org.bukkit.plugin.java.JavaPlugin; + +public class Plugin extends JavaPlugin {} diff --git a/src/fr/watch54/displays/holograms/Hologram.java b/src/main/java/fr/watch54/displays/holograms/Hologram.java similarity index 94% rename from src/fr/watch54/displays/holograms/Hologram.java rename to src/main/java/fr/watch54/displays/holograms/Hologram.java index 3de95c0..47e6439 100644 --- a/src/fr/watch54/displays/holograms/Hologram.java +++ b/src/main/java/fr/watch54/displays/holograms/Hologram.java @@ -1,65 +1,58 @@ -package fr.watch54.displays.holograms; - -import fr.watch54.displays.interfaces.Action; -import fr.watch54.displays.interfaces.Text; -import org.bukkit.Location; - -import java.util.List; - -public abstract class Hologram { - - protected List textList; - protected Location location; - protected boolean spawned; - - public Hologram(List textList, Location location){ - this.textList = textList; - this.location = location; - this.spawned = false; - - } - - public List getTextList(){ - return textList; - - } - - public void setTextList(List textList){ - this.textList = textList; - - } - - public Location getLocation(){ - return location; - - } - - public void setLocation(Location location){ - this.location = location; - - } - - public boolean isSpawned(){ - return spawned; - - } - - public void setSpawned(boolean spawned){ - this.spawned = spawned; - - } - - public abstract void display(); - - public abstract void update(); - - public abstract void remove(); - - public abstract void interact(Action action); - - public void teleport(Location location){ - this.update(); - - } - -} +package fr.watch54.displays.holograms; + +import fr.watch54.displays.interfaces.Action; +import fr.watch54.displays.interfaces.Text; +import org.bukkit.Location; + +import java.util.List; + +public abstract class Hologram { + + protected List textList; + protected Location location; + protected boolean spawned; + + public Hologram(List textList, Location location){ + this.textList = textList; + this.location = location; + this.spawned = false; + } + + public List getTextList(){ + return textList; + } + + public void setTextList(List textList){ + this.textList = textList; + } + + public Location getLocation(){ + return location; + + } + + public void setLocation(Location location){ + this.location = location; + } + + public boolean isSpawned(){ + return spawned; + } + + public void setSpawned(boolean spawned){ + this.spawned = spawned; + } + + public abstract void display(); + + public abstract void update(); + + public abstract void remove(); + + public abstract void interact(Action action); + + public void teleport(Location location){ + this.update(); + } + +} diff --git a/src/fr/watch54/displays/holograms/HologramClient.java b/src/main/java/fr/watch54/displays/holograms/client/HologramClient.java similarity index 96% rename from src/fr/watch54/displays/holograms/HologramClient.java rename to src/main/java/fr/watch54/displays/holograms/client/HologramClient.java index 4e00f95..c319c0d 100644 --- a/src/fr/watch54/displays/holograms/HologramClient.java +++ b/src/main/java/fr/watch54/displays/holograms/client/HologramClient.java @@ -1,155 +1,156 @@ -package fr.watch54.displays.holograms; - -import fr.watch54.displays.interfaces.Action; -import fr.watch54.displays.interfaces.Text; -import io.netty.channel.ChannelDuplexHandler; -import io.netty.channel.ChannelHandlerContext; -import io.netty.channel.ChannelPipeline; -import net.minecraft.server.v1_8_R3.EntityArmorStand; -import net.minecraft.server.v1_8_R3.PacketPlayInUseEntity; -import net.minecraft.server.v1_8_R3.PacketPlayOutEntityDestroy; -import net.minecraft.server.v1_8_R3.PacketPlayOutSpawnEntityLiving; -import org.bukkit.Location; -import org.bukkit.craftbukkit.v1_8_R3.CraftWorld; -import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer; -import org.bukkit.entity.Player; - -import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class HologramClient extends Hologram { - - private Player player; - private Map entityArmorStandTextMap; - - public HologramClient(Player player, List textList, Location location){ - super(textList, location); - this.player = player; - this.entityArmorStandTextMap = new HashMap<>(); - - } - - @Override - public void display(){ - - if(player == null) throw new NullPointerException("Player cannot be null!"); - if(this.getTextList() == null) throw new NullPointerException("Texts cannot be null!"); - if(this.getLocation() == null) throw new NullPointerException("Location cannot be null!"); - - Location locationClone = this.getLocation().clone(); - - CraftPlayer craftPlayer = (CraftPlayer) player; - CraftWorld craftWorld = (CraftWorld) locationClone.getWorld(); - - for(Text text : this.getTextList()){ - - EntityArmorStand armorStand = new EntityArmorStand(craftWorld.getHandle(), locationClone.getX(), locationClone.getY(), locationClone.getZ()); - armorStand.setGravity(false); - armorStand.setInvisible(true); - armorStand.setCustomNameVisible(true); - armorStand.setCustomName(text.getText()); - armorStand.fireTicks = Integer.MAX_VALUE; - - entityArmorStandTextMap.put(armorStand, text); - locationClone.add(0, -0.3D, 0); - - PacketPlayOutSpawnEntityLiving entityLiving = new PacketPlayOutSpawnEntityLiving(armorStand); - craftPlayer.getHandle().playerConnection.sendPacket(entityLiving); - - this.setSpawned(true); - - } - - } - - @Override - public void update(){ - this.remove(); - this.display(); - - } - - @Override - public void remove(){ - - CraftPlayer craftPlayer = (CraftPlayer) player; - - List armorStandClone = new ArrayList<>(entityArmorStandTextMap.keySet()); - for(EntityArmorStand armorStand : armorStandClone){ - - PacketPlayOutEntityDestroy entityDestroy = new PacketPlayOutEntityDestroy(armorStand.getId()); - craftPlayer.getHandle().playerConnection.sendPacket(entityDestroy); - entityArmorStandTextMap.clear(); - - this.setSpawned(false); - - } - } - - @Override - public void interact(Action action){ - - final int[] id = {0}; - ChannelDuplexHandler channelDuplexHandler = new ChannelDuplexHandler(){ - - @Override - public void channelRead(ChannelHandlerContext channelHandlerContext, Object packet) throws Exception { - - if(packet instanceof PacketPlayInUseEntity){ - - PacketPlayInUseEntity useEntity = (PacketPlayInUseEntity) packet; - - if (useEntity.a() == PacketPlayInUseEntity.EnumEntityUseAction.ATTACK) return; - - Field entityIDField = useEntity.getClass().getDeclaredField("a"); - entityIDField.setAccessible(true); - - for(EntityArmorStand entityArmorStand : entityArmorStandTextMap.keySet()){ - - if(entityIDField.get(useEntity).equals(entityArmorStand.getId())){ - action.execute(player); - id[0] = entityArmorStand.getId(); - break; - - } - - } - - } - - super.channelRead(channelHandlerContext, packet); - - } - - }; - - ChannelPipeline channelPipeline = ((CraftPlayer) player).getHandle().playerConnection.networkManager.channel.pipeline(); - channelPipeline.addBefore("packet_handler", player.getName() + "/" + id[0], channelDuplexHandler); - - } - - @Override - public void teleport(Location location){ - - this.setLocation(location); - Location locationClone = location.clone(); - - entityArmorStandTextMap.keySet().forEach(armorStand -> { - armorStand.setPosition(locationClone.getX(), location.getY(), location.getZ()); - locationClone.add(0, -0.3D, 0); - - }); - - } - - @Override - public void setTextList(List textList){ - this.textList = textList; - this.update(); - - } - -} +package fr.watch54.displays.holograms.client; + +import fr.watch54.displays.holograms.Hologram; +import fr.watch54.displays.interfaces.Action; +import fr.watch54.displays.interfaces.Text; +import io.netty.channel.ChannelDuplexHandler; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelPipeline; +import net.minecraft.server.v1_8_R3.EntityArmorStand; +import net.minecraft.server.v1_8_R3.PacketPlayInUseEntity; +import net.minecraft.server.v1_8_R3.PacketPlayOutEntityDestroy; +import net.minecraft.server.v1_8_R3.PacketPlayOutSpawnEntityLiving; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_8_R3.CraftWorld; +import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer; +import org.bukkit.entity.Player; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class HologramClient extends Hologram { + + private Player player; + private Map entityArmorStandTextMap; + + public HologramClient(Player player, List textList, Location location){ + super(textList, location); + this.player = player; + this.entityArmorStandTextMap = new HashMap<>(); + + } + + @Override + public void display(){ + + if(player == null) throw new NullPointerException("Player cannot be null!"); + if(this.getTextList() == null) throw new NullPointerException("Texts cannot be null!"); + if(this.getLocation() == null) throw new NullPointerException("Location cannot be null!"); + + Location locationClone = this.getLocation().clone(); + + CraftPlayer craftPlayer = (CraftPlayer) player; + CraftWorld craftWorld = (CraftWorld) locationClone.getWorld(); + + for(Text text : this.getTextList()){ + + EntityArmorStand armorStand = new EntityArmorStand(craftWorld.getHandle(), locationClone.getX(), locationClone.getY(), locationClone.getZ()); + armorStand.setGravity(false); + armorStand.setInvisible(true); + armorStand.setCustomNameVisible(true); + armorStand.setCustomName(text.getText()); + armorStand.fireTicks = Integer.MAX_VALUE; + + entityArmorStandTextMap.put(armorStand, text); + locationClone.add(0, -0.3D, 0); + + PacketPlayOutSpawnEntityLiving entityLiving = new PacketPlayOutSpawnEntityLiving(armorStand); + craftPlayer.getHandle().playerConnection.sendPacket(entityLiving); + + this.setSpawned(true); + + } + + } + + @Override + public void update(){ + this.remove(); + this.display(); + + } + + @Override + public void remove(){ + + CraftPlayer craftPlayer = (CraftPlayer) player; + + List armorStandClone = new ArrayList<>(entityArmorStandTextMap.keySet()); + for(EntityArmorStand armorStand : armorStandClone){ + + PacketPlayOutEntityDestroy entityDestroy = new PacketPlayOutEntityDestroy(armorStand.getId()); + craftPlayer.getHandle().playerConnection.sendPacket(entityDestroy); + entityArmorStandTextMap.clear(); + + this.setSpawned(false); + + } + } + + @Override + public void interact(Action action){ + + final int[] id = {0}; + ChannelDuplexHandler channelDuplexHandler = new ChannelDuplexHandler(){ + + @Override + public void channelRead(ChannelHandlerContext channelHandlerContext, Object packet) throws Exception { + + if(packet instanceof PacketPlayInUseEntity){ + + PacketPlayInUseEntity useEntity = (PacketPlayInUseEntity) packet; + + if (useEntity.a() == PacketPlayInUseEntity.EnumEntityUseAction.ATTACK) return; + + Field entityIDField = useEntity.getClass().getDeclaredField("a"); + entityIDField.setAccessible(true); + + for(EntityArmorStand entityArmorStand : entityArmorStandTextMap.keySet()){ + + if(entityIDField.get(useEntity).equals(entityArmorStand.getId())){ + action.execute(player); + id[0] = entityArmorStand.getId(); + break; + + } + + } + + } + + super.channelRead(channelHandlerContext, packet); + + } + + }; + + ChannelPipeline channelPipeline = ((CraftPlayer) player).getHandle().playerConnection.networkManager.channel.pipeline(); + channelPipeline.addBefore("packet_handler", player.getName() + "/" + id[0], channelDuplexHandler); + + } + + @Override + public void teleport(Location location){ + + this.setLocation(location); + Location locationClone = location.clone(); + + entityArmorStandTextMap.keySet().forEach(armorStand -> { + armorStand.setPosition(locationClone.getX(), location.getY(), location.getZ()); + locationClone.add(0, -0.3D, 0); + + }); + + } + + @Override + public void setTextList(List textList){ + this.textList = textList; + this.update(); + + } + +} diff --git a/src/fr/watch54/displays/holograms/HologramServer.java b/src/main/java/fr/watch54/displays/holograms/server/HologramServer.java similarity index 94% rename from src/fr/watch54/displays/holograms/HologramServer.java rename to src/main/java/fr/watch54/displays/holograms/server/HologramServer.java index 23c2266..627feaa 100644 --- a/src/fr/watch54/displays/holograms/HologramServer.java +++ b/src/main/java/fr/watch54/displays/holograms/server/HologramServer.java @@ -1,99 +1,100 @@ -package fr.watch54.displays.holograms; - -import fr.watch54.displays.interfaces.Action; -import fr.watch54.displays.interfaces.Text; -import org.bukkit.Location; -import org.bukkit.World; -import org.bukkit.entity.ArmorStand; -import org.bukkit.entity.Entity; -import org.bukkit.entity.EntityType; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class HologramServer extends Hologram { - - private Map armorStandTextMap; - private Action action; - - public HologramServer(List textList, Location location){ - super(textList, location); - this.armorStandTextMap = new HashMap<>(); - - } - - @Override - public void display(){ - - if(this.getTextList() == null) throw new NullPointerException("Texts cannot be null!"); - if(this.getLocation() == null) throw new NullPointerException("Location cannot be null!"); - - Location locationClone = this.getLocation().clone(); - World world = locationClone.getWorld(); - - for(Text text : this.getTextList()){ - - ArmorStand armorStand = (ArmorStand) world.spawnEntity(locationClone, EntityType.ARMOR_STAND); - armorStand.setGravity(false); - armorStand.setVisible(false); - armorStand.setCustomNameVisible(true); - armorStand.setCustomName(text.getText()); - - armorStandTextMap.put(armorStand, text); - locationClone.add(0, -0.3D, 0); - - this.setSpawned(true); - - } - - } - - @Override - public void update(){ - armorStandTextMap.keySet().forEach(armorStand -> armorStand.setCustomName(armorStandTextMap.get(armorStand).getText())); - - } - - @Override - public void remove(){ - armorStandTextMap.keySet().forEach(Entity::remove); - armorStandTextMap.clear(); - this.setSpawned(false); - - } - - @Override - public void interact(Action action){ - this.action = action; - - } - - @Override - public void teleport(Location location){ - - this.setLocation(location); - Location locationClone = location.clone(); - - armorStandTextMap.keySet().forEach(armorStand -> { - armorStand.teleport(locationClone); - locationClone.add(0, -0.3D, 0); - - }); - - } - - @Override - public void setTextList(List textList){ - this.textList = textList; - this.remove(); - this.display(); - - } - - public Action getAction(){ - return action; - - } - -} +package fr.watch54.displays.holograms.server; + +import fr.watch54.displays.holograms.Hologram; +import fr.watch54.displays.interfaces.Action; +import fr.watch54.displays.interfaces.Text; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.entity.ArmorStand; +import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class HologramServer extends Hologram { + + private Map armorStandTextMap; + private Action action; + + public HologramServer(List textList, Location location){ + super(textList, location); + this.armorStandTextMap = new HashMap<>(); + + } + + @Override + public void display(){ + + if(this.getTextList() == null) throw new NullPointerException("Texts cannot be null!"); + if(this.getLocation() == null) throw new NullPointerException("Location cannot be null!"); + + Location locationClone = this.getLocation().clone(); + World world = locationClone.getWorld(); + + for(Text text : this.getTextList()){ + + ArmorStand armorStand = (ArmorStand) world.spawnEntity(locationClone, EntityType.ARMOR_STAND); + armorStand.setGravity(false); + armorStand.setVisible(false); + armorStand.setCustomNameVisible(true); + armorStand.setCustomName(text.getText()); + + armorStandTextMap.put(armorStand, text); + locationClone.add(0, -0.3D, 0); + + this.setSpawned(true); + + } + + } + + @Override + public void update(){ + armorStandTextMap.keySet().forEach(armorStand -> armorStand.setCustomName(armorStandTextMap.get(armorStand).getText())); + + } + + @Override + public void remove(){ + armorStandTextMap.keySet().forEach(Entity::remove); + armorStandTextMap.clear(); + this.setSpawned(false); + + } + + @Override + public void interact(Action action){ + this.action = action; + + } + + @Override + public void teleport(Location location){ + + this.setLocation(location); + Location locationClone = location.clone(); + + armorStandTextMap.keySet().forEach(armorStand -> { + armorStand.teleport(locationClone); + locationClone.add(0, -0.3D, 0); + + }); + + } + + @Override + public void setTextList(List textList){ + this.textList = textList; + this.remove(); + this.display(); + + } + + public Action getAction(){ + return action; + + } + +} diff --git a/src/fr/watch54/displays/interfaces/Action.java b/src/main/java/fr/watch54/displays/interfaces/Action.java similarity index 93% rename from src/fr/watch54/displays/interfaces/Action.java rename to src/main/java/fr/watch54/displays/interfaces/Action.java index 8eafe2e..d70f1cd 100644 --- a/src/fr/watch54/displays/interfaces/Action.java +++ b/src/main/java/fr/watch54/displays/interfaces/Action.java @@ -1,9 +1,9 @@ -package fr.watch54.displays.interfaces; - -import org.bukkit.entity.Player; - -public interface Action { - - void execute(Player player); - -} +package fr.watch54.displays.interfaces; + +import org.bukkit.entity.Player; + +public interface Action { + + void execute(Player player); + +} diff --git a/src/fr/watch54/displays/interfaces/Text.java b/src/main/java/fr/watch54/displays/interfaces/Text.java similarity index 92% rename from src/fr/watch54/displays/interfaces/Text.java rename to src/main/java/fr/watch54/displays/interfaces/Text.java index 419ec0c..85c424c 100644 --- a/src/fr/watch54/displays/interfaces/Text.java +++ b/src/main/java/fr/watch54/displays/interfaces/Text.java @@ -1,7 +1,7 @@ -package fr.watch54.displays.interfaces; - -public interface Text { - - String getText(); - -} +package fr.watch54.displays.interfaces; + +public interface Text { + + String getText(); + +} diff --git a/src/fr/watch54/displays/listeners/InteractListener.java b/src/main/java/fr/watch54/displays/listeners/Listeners.java similarity index 55% rename from src/fr/watch54/displays/listeners/InteractListener.java rename to src/main/java/fr/watch54/displays/listeners/Listeners.java index d16142b..724290c 100644 --- a/src/fr/watch54/displays/listeners/InteractListener.java +++ b/src/main/java/fr/watch54/displays/listeners/Listeners.java @@ -1,59 +1,64 @@ -package fr.watch54.displays.listeners; - -import fr.watch54.displays.holograms.Hologram; -import fr.watch54.displays.holograms.HologramServer; -import fr.watch54.displays.interfaces.Action; -import fr.watch54.displays.managers.HologramManager; -import org.bukkit.block.Block; -import org.bukkit.entity.ArmorStand; -import org.bukkit.entity.Entity; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerInteractAtEntityEvent; - -import java.util.Optional; - -public class InteractListener implements Listener { - - private HologramManager hologramManager; - - public InteractListener(HologramManager hologramManager){ - this.hologramManager = hologramManager; - - } - - @EventHandler - public void onInteract(PlayerInteractAtEntityEvent event){ - - Player player = event.getPlayer(); - Entity entity = event.getRightClicked(); - - if(entity instanceof ArmorStand){ - - Block block = entity.getLocation().getBlock(); - - if(hologramManager.containsHologram(block)){ - - Optional optionalHologram = hologramManager.getHologram(block); - - optionalHologram.ifPresent(hologram -> { - - if(hologram instanceof HologramServer){ - - HologramServer server = (HologramServer) hologram; - Action action = server.getAction(); - - if(action != null) action.execute(player); - - } - - }); - - } - - } - - } - -} +package fr.watch54.displays.listeners; + +import fr.watch54.displays.holograms.Hologram; +import fr.watch54.displays.holograms.server.HologramServer; +import fr.watch54.displays.interfaces.Action; +import fr.watch54.displays.managers.HologramManager; +import org.bukkit.block.Block; +import org.bukkit.entity.ArmorStand; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerInteractAtEntityEvent; +import org.bukkit.event.world.ChunkUnloadEvent; + +import java.util.Optional; + +/** + * Copyright (C) gameszaum, all rights reserved, unauthorized + * utlization or copy of this file, is strictly prohibited and + * liable to civil and criminal penalties, the project 'DisplayAPI' + * is privated and the re-sale without contact with me (gameszaum) is not allowed. + */ +public class Listeners implements Listener { + + private HologramManager hologramManager; + + public Listeners(HologramManager hologramManager) { + this.hologramManager = hologramManager; + } + + @EventHandler + public void onInteract(PlayerInteractAtEntityEvent event) { + Player player = event.getPlayer(); + Entity entity = event.getRightClicked(); + + if (entity instanceof ArmorStand) { + + Block block = entity.getLocation().getBlock(); + + if (hologramManager.containsHologram(block)) { + + Optional optionalHologram = hologramManager.getHologram(block); + + optionalHologram.ifPresent(hologram -> { + + if (hologram instanceof HologramServer) { + + HologramServer server = (HologramServer) hologram; + Action action = server.getAction(); + + if (action != null) action.execute(player); + } + }); + } + } + } + + @EventHandler + public void onChunkUnload(ChunkUnloadEvent event) { + event.setCancelled(true); + } + +} diff --git a/src/fr/watch54/displays/managers/HologramManager.java b/src/main/java/fr/watch54/displays/managers/HologramManager.java similarity index 85% rename from src/fr/watch54/displays/managers/HologramManager.java rename to src/main/java/fr/watch54/displays/managers/HologramManager.java index 5da096b..307dd53 100644 --- a/src/fr/watch54/displays/managers/HologramManager.java +++ b/src/main/java/fr/watch54/displays/managers/HologramManager.java @@ -1,110 +1,98 @@ -package fr.watch54.displays.managers; - -import fr.watch54.displays.holograms.Hologram; -import fr.watch54.displays.holograms.HologramClient; -import fr.watch54.displays.holograms.HologramServer; -import fr.watch54.displays.interfaces.Text; -import fr.watch54.displays.listeners.ChunkUnloadListener; -import fr.watch54.displays.listeners.InteractListener; -import fr.watch54.displays.tasks.HologramTask; -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.block.Block; -import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; - -public class HologramManager { - - private Plugin plugin; - private Map hologramMap; - - public HologramManager(Plugin plugin){ - this.plugin = plugin; - this.hologramMap = new HashMap<>(); - Bukkit.getPluginManager().registerEvents(new InteractListener(this), plugin); - Bukkit.getPluginManager().registerEvents(new ChunkUnloadListener(), plugin); - - } - - public HologramServer createServer(List textList, Location location, boolean refresh){ - - Block block = location.getBlock(); - if(this.containsHologram(block)) return (HologramServer) hologramMap.get(block); - - HologramServer hologramServer = new HologramServer(textList, location); - Bukkit.getScheduler().runTaskLater(plugin, hologramServer::display, 20); - - hologramMap.put(location.getBlock(), hologramServer); - - HologramTask hologramTask = new HologramTask(hologramServer, this, refresh); - hologramTask.runTaskTimer(plugin, 20, 20); - - return hologramServer; - - } - - public HologramClient createClient(Player player, List textList, Location location, boolean refresh){ - - HologramClient hologram = new HologramClient(player, textList, location); - Bukkit.getScheduler().runTaskLater(plugin, hologram::display, 20); - - hologramMap.put(location.getBlock(), hologram); - - HologramTask hologramTask = new HologramTask(hologram, this, refresh); - hologramTask.runTaskTimer(plugin, 20, 20); - - return hologram; - - } - - public void teleport(Hologram hologram, Location location){ - - if(!hologramMap.containsValue(hologram)) return; - - hologramMap.entrySet().removeIf(entry -> entry.getValue().equals(hologram)); - hologramMap.put(location.getBlock(), hologram); - hologram.teleport(location); - - } - - public boolean containsHologram(Block block){ - return hologramMap.keySet().stream().anyMatch(blockMap -> block.getX() == blockMap.getX() && block.getZ() == blockMap.getZ()); - - } - - public Optional getHologram(Block block){ - - Optional blockFinal = hologramMap - .keySet() - .stream() - .filter(blockMap -> blockMap.getX() == block.getX() && blockMap.getZ() == block.getZ()) - .findFirst(); - - Hologram hologram = null; - - if(blockFinal.isPresent()) hologram = hologramMap.get(blockFinal.get()); - return Optional.ofNullable(hologram); - - } - - public void remove(Hologram hologram){ - - if(!hologramMap.containsValue(hologram)) return; - - hologram.remove(); - hologramMap.entrySet().removeIf(entry -> entry.getValue().equals(hologram)); - - } - - public void clear(){ - hologramMap.values().forEach(Hologram::remove); - hologramMap.clear(); - - } - -} +package fr.watch54.displays.managers; + +import fr.watch54.displays.holograms.Hologram; +import fr.watch54.displays.holograms.client.HologramClient; +import fr.watch54.displays.holograms.server.HologramServer; +import fr.watch54.displays.interfaces.Text; +import fr.watch54.displays.listeners.Listeners; +import fr.watch54.displays.tasks.HologramTask; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.block.Block; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +public class HologramManager { + + private Plugin plugin; + private Map hologramMap; + + public HologramManager(Plugin plugin){ + this.plugin = plugin; + this.hologramMap = new HashMap<>(); + + Bukkit.getPluginManager().registerEvents(new Listeners(this), plugin); + } + + public HologramServer createServer(List textList, Location location, boolean refresh){ + Block block = location.getBlock(); + if(this.containsHologram(block)) return (HologramServer) hologramMap.get(block); + + HologramServer hologramServer = new HologramServer(textList, location); + Bukkit.getScheduler().runTaskLater(plugin, hologramServer::display, 20); + + hologramMap.put(location.getBlock(), hologramServer); + + HologramTask hologramTask = new HologramTask(hologramServer, this, refresh); + hologramTask.runTaskTimer(plugin, 20, 20); + + return hologramServer; + + } + + public HologramClient createClient(Player player, List textList, Location location, boolean refresh){ + HologramClient hologram = new HologramClient(player, textList, location); + Bukkit.getScheduler().runTaskLater(plugin, hologram::display, 20); + + hologramMap.put(location.getBlock(), hologram); + + HologramTask hologramTask = new HologramTask(hologram, this, refresh); + hologramTask.runTaskTimer(plugin, 20, 20); + + return hologram; + + } + + public void teleport(Hologram hologram, Location location){ + if(!hologramMap.containsValue(hologram)) return; + + hologramMap.entrySet().removeIf(entry -> entry.getValue().equals(hologram)); + hologramMap.put(location.getBlock(), hologram); + hologram.teleport(location); + } + + public boolean containsHologram(Block block){ + return hologramMap.keySet().stream().anyMatch(blockMap -> block.getX() == blockMap.getX() && block.getZ() == blockMap.getZ()); + } + + public Optional getHologram(Block block){ + Optional blockFinal = hologramMap + .keySet() + .stream() + .filter(blockMap -> blockMap.getX() == block.getX() && blockMap.getZ() == block.getZ()) + .findFirst(); + + Hologram hologram = null; + + if(blockFinal.isPresent()) hologram = hologramMap.get(blockFinal.get()); + return Optional.ofNullable(hologram); + } + + public void remove(Hologram hologram){ + if(!hologramMap.containsValue(hologram)) return; + + hologram.remove(); + hologramMap.entrySet().removeIf(entry -> entry.getValue().equals(hologram)); + } + + public void clear(){ + hologramMap.values().forEach(Hologram::remove); + hologramMap.clear(); + } + +} diff --git a/src/fr/watch54/displays/tasks/HologramTask.java b/src/main/java/fr/watch54/displays/tasks/HologramTask.java similarity index 95% rename from src/fr/watch54/displays/tasks/HologramTask.java rename to src/main/java/fr/watch54/displays/tasks/HologramTask.java index 79d2645..a82546f 100644 --- a/src/fr/watch54/displays/tasks/HologramTask.java +++ b/src/main/java/fr/watch54/displays/tasks/HologramTask.java @@ -1,35 +1,31 @@ -package fr.watch54.displays.tasks; - -import fr.watch54.displays.managers.HologramManager; -import fr.watch54.displays.holograms.Hologram; -import org.bukkit.Bukkit; -import org.bukkit.scheduler.BukkitRunnable; - -public class HologramTask extends BukkitRunnable { - - private Hologram hologram; - private HologramManager hologramManager; - private boolean refresh; - - public HologramTask(Hologram hologram, HologramManager hologramManager, boolean refresh){ - this.hologram = hologram; - this.hologramManager = hologramManager; - this.refresh = refresh; - - } - - @Override - public void run(){ - - if(Bukkit.getOnlinePlayers().size() < 1 && hologram.isSpawned()){ - cancel(); - hologramManager.remove(hologram); - return; - - } - - if(refresh) hologram.update(); - - } - -} +package fr.watch54.displays.tasks; + +import fr.watch54.displays.managers.HologramManager; +import fr.watch54.displays.holograms.Hologram; +import org.bukkit.Bukkit; +import org.bukkit.scheduler.BukkitRunnable; + +public class HologramTask extends BukkitRunnable { + + private Hologram hologram; + private HologramManager hologramManager; + private boolean refresh; + + public HologramTask(Hologram hologram, HologramManager hologramManager, boolean refresh){ + this.hologram = hologram; + this.hologramManager = hologramManager; + this.refresh = refresh; + + } + + @Override + public void run(){ + if(Bukkit.getOnlinePlayers().size() < 1 && hologram.isSpawned()){ + cancel(); + hologramManager.remove(hologram); + return; + } + if(refresh) hologram.update(); + } + +} diff --git a/resources/plugin.yml b/src/main/java/resources/plugin.yml similarity index 96% rename from resources/plugin.yml rename to src/main/java/resources/plugin.yml index 67f73e3..01eeef7 100644 --- a/resources/plugin.yml +++ b/src/main/java/resources/plugin.yml @@ -1,4 +1,4 @@ -name: HologramDisplays -author: Watch54 -main: fr.watch54.displays.Plugin +name: HologramDisplays +author: Watch54 +main: fr.watch54.displays.Plugin version: 1.0.0 \ No newline at end of file From 922a4ee1daf6e864e13f0feeb81c61ac86c78a73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Vila=C3=A7a?= Date: Wed, 10 Mar 2021 22:21:39 -0300 Subject: [PATCH 2/4] refactored HologramClient interact method, get all holograms method created. --- .../holograms/client/HologramClient.java | 41 +++++-------------- .../displays/managers/HologramManager.java | 5 ++- 2 files changed, 14 insertions(+), 32 deletions(-) diff --git a/src/main/java/fr/watch54/displays/holograms/client/HologramClient.java b/src/main/java/fr/watch54/displays/holograms/client/HologramClient.java index c319c0d..2063899 100644 --- a/src/main/java/fr/watch54/displays/holograms/client/HologramClient.java +++ b/src/main/java/fr/watch54/displays/holograms/client/HologramClient.java @@ -3,19 +3,15 @@ import fr.watch54.displays.holograms.Hologram; import fr.watch54.displays.interfaces.Action; import fr.watch54.displays.interfaces.Text; -import io.netty.channel.ChannelDuplexHandler; -import io.netty.channel.ChannelHandlerContext; -import io.netty.channel.ChannelPipeline; import net.minecraft.server.v1_8_R3.EntityArmorStand; -import net.minecraft.server.v1_8_R3.PacketPlayInUseEntity; import net.minecraft.server.v1_8_R3.PacketPlayOutEntityDestroy; import net.minecraft.server.v1_8_R3.PacketPlayOutSpawnEntityLiving; import org.bukkit.Location; import org.bukkit.craftbukkit.v1_8_R3.CraftWorld; import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer; import org.bukkit.entity.Player; +import org.bukkit.util.BlockIterator; -import java.lang.reflect.Field; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -30,12 +26,10 @@ public HologramClient(Player player, List textList, Location location){ super(textList, location); this.player = player; this.entityArmorStandTextMap = new HashMap<>(); - } @Override public void display(){ - if(player == null) throw new NullPointerException("Player cannot be null!"); if(this.getTextList() == null) throw new NullPointerException("Texts cannot be null!"); if(this.getLocation() == null) throw new NullPointerException("Location cannot be null!"); @@ -46,7 +40,6 @@ public void display(){ CraftWorld craftWorld = (CraftWorld) locationClone.getWorld(); for(Text text : this.getTextList()){ - EntityArmorStand armorStand = new EntityArmorStand(craftWorld.getHandle(), locationClone.getX(), locationClone.getY(), locationClone.getZ()); armorStand.setGravity(false); armorStand.setInvisible(true); @@ -61,21 +54,17 @@ public void display(){ craftPlayer.getHandle().playerConnection.sendPacket(entityLiving); this.setSpawned(true); - } - } @Override public void update(){ this.remove(); this.display(); - } @Override public void remove(){ - CraftPlayer craftPlayer = (CraftPlayer) player; List armorStandClone = new ArrayList<>(entityArmorStandTextMap.keySet()); @@ -86,21 +75,23 @@ public void remove(){ entityArmorStandTextMap.clear(); this.setSpawned(false); - } } @Override - public void interact(Action action){ - + public void interact(Action action) { + BlockIterator iter = new BlockIterator(player, 3); + while (iter.hasNext()) { + if (iter.next().equals(location.getBlock())) { + action.execute(player); + } + } + /* final int[] id = {0}; ChannelDuplexHandler channelDuplexHandler = new ChannelDuplexHandler(){ - @Override public void channelRead(ChannelHandlerContext channelHandlerContext, Object packet) throws Exception { - if(packet instanceof PacketPlayInUseEntity){ - PacketPlayInUseEntity useEntity = (PacketPlayInUseEntity) packet; if (useEntity.a() == PacketPlayInUseEntity.EnumEntityUseAction.ATTACK) return; @@ -109,41 +100,29 @@ public void channelRead(ChannelHandlerContext channelHandlerContext, Object pack entityIDField.setAccessible(true); for(EntityArmorStand entityArmorStand : entityArmorStandTextMap.keySet()){ - if(entityIDField.get(useEntity).equals(entityArmorStand.getId())){ action.execute(player); id[0] = entityArmorStand.getId(); break; - } - } - } - super.channelRead(channelHandlerContext, packet); - } - }; - ChannelPipeline channelPipeline = ((CraftPlayer) player).getHandle().playerConnection.networkManager.channel.pipeline(); - channelPipeline.addBefore("packet_handler", player.getName() + "/" + id[0], channelDuplexHandler); - + channelPipeline.addBefore("packet_handler", player.getName() + "/" + id[0], channelDuplexHandler);*/ } @Override public void teleport(Location location){ - this.setLocation(location); Location locationClone = location.clone(); entityArmorStandTextMap.keySet().forEach(armorStand -> { armorStand.setPosition(locationClone.getX(), location.getY(), location.getZ()); locationClone.add(0, -0.3D, 0); - }); - } @Override diff --git a/src/main/java/fr/watch54/displays/managers/HologramManager.java b/src/main/java/fr/watch54/displays/managers/HologramManager.java index 307dd53..51c90e6 100644 --- a/src/main/java/fr/watch54/displays/managers/HologramManager.java +++ b/src/main/java/fr/watch54/displays/managers/HologramManager.java @@ -90,9 +90,12 @@ public void remove(Hologram hologram){ hologramMap.entrySet().removeIf(entry -> entry.getValue().equals(hologram)); } - public void clear(){ + public void clear() { hologramMap.values().forEach(Hologram::remove); hologramMap.clear(); } + public Map getHologramMap() { + return hologramMap; + } } From c02cac97cb8c37dab27db1cbeb1fb1608603ede1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Vila=C3=A7a?= Date: Tue, 16 Mar 2021 19:36:36 -0300 Subject: [PATCH 3/4] refactored hologram interact method and optimized code. --- .idea/encodings.xml | 1 - DisplayAPI.iml | 2 +- pom.xml | 2 +- .../watch54/displays/holograms/Hologram.java | 9 ++- .../holograms/client/HologramClient.java | 41 ++++++------- .../holograms/server/HologramServer.java | 40 +++--------- .../watch54/displays/listeners/Listeners.java | 61 +++++++++++++------ .../displays/managers/HologramManager.java | 24 ++++---- .../watch54/displays/tasks/HologramTask.java | 1 - src/main/java/resources/plugin.yml | 2 +- 10 files changed, 97 insertions(+), 86 deletions(-) diff --git a/.idea/encodings.xml b/.idea/encodings.xml index aa00ffa..63e9001 100644 --- a/.idea/encodings.xml +++ b/.idea/encodings.xml @@ -2,6 +2,5 @@ - \ No newline at end of file diff --git a/DisplayAPI.iml b/DisplayAPI.iml index b478335..ef0295c 100644 --- a/DisplayAPI.iml +++ b/DisplayAPI.iml @@ -23,7 +23,7 @@ - + diff --git a/pom.xml b/pom.xml index 2a9b242..8f93b08 100644 --- a/pom.xml +++ b/pom.xml @@ -58,7 +58,7 @@ spigot-api 1.8.8-R0.1-SNAPSHOT system - C:/server/spigot-1.8.8-R0.1-SNAPSHOT-latest.jar + D:/dependencies/spigot-1.8.8-R0.1-SNAPSHOT-latest.jar diff --git a/src/main/java/fr/watch54/displays/holograms/Hologram.java b/src/main/java/fr/watch54/displays/holograms/Hologram.java index 47e6439..def45c3 100644 --- a/src/main/java/fr/watch54/displays/holograms/Hologram.java +++ b/src/main/java/fr/watch54/displays/holograms/Hologram.java @@ -11,6 +11,7 @@ public abstract class Hologram { protected List textList; protected Location location; protected boolean spawned; + protected Action action; public Hologram(List textList, Location location){ this.textList = textList; @@ -49,10 +50,16 @@ public void setSpawned(boolean spawned){ public abstract void remove(); - public abstract void interact(Action action); + public void interact(Action action) { + this.action = action; + } public void teleport(Location location){ this.update(); } + public Action getAction() { + return action; + } + } diff --git a/src/main/java/fr/watch54/displays/holograms/client/HologramClient.java b/src/main/java/fr/watch54/displays/holograms/client/HologramClient.java index 2063899..2856657 100644 --- a/src/main/java/fr/watch54/displays/holograms/client/HologramClient.java +++ b/src/main/java/fr/watch54/displays/holograms/client/HologramClient.java @@ -22,24 +22,24 @@ public class HologramClient extends Hologram { private Player player; private Map entityArmorStandTextMap; - public HologramClient(Player player, List textList, Location location){ + public HologramClient(Player player, List textList, Location location) { super(textList, location); this.player = player; this.entityArmorStandTextMap = new HashMap<>(); } @Override - public void display(){ - if(player == null) throw new NullPointerException("Player cannot be null!"); - if(this.getTextList() == null) throw new NullPointerException("Texts cannot be null!"); - if(this.getLocation() == null) throw new NullPointerException("Location cannot be null!"); + public void display() { + if (player == null) throw new NullPointerException("Player cannot be null!"); + if (this.getTextList() == null) throw new NullPointerException("Texts cannot be null!"); + if (this.getLocation() == null) throw new NullPointerException("Location cannot be null!"); Location locationClone = this.getLocation().clone(); CraftPlayer craftPlayer = (CraftPlayer) player; CraftWorld craftWorld = (CraftWorld) locationClone.getWorld(); - for(Text text : this.getTextList()){ + for (Text text : this.getTextList()) { EntityArmorStand armorStand = new EntityArmorStand(craftWorld.getHandle(), locationClone.getX(), locationClone.getY(), locationClone.getZ()); armorStand.setGravity(false); armorStand.setInvisible(true); @@ -58,17 +58,17 @@ public void display(){ } @Override - public void update(){ + public void update() { this.remove(); this.display(); } @Override - public void remove(){ + public void remove() { CraftPlayer craftPlayer = (CraftPlayer) player; List armorStandClone = new ArrayList<>(entityArmorStandTextMap.keySet()); - for(EntityArmorStand armorStand : armorStandClone){ + for (EntityArmorStand armorStand : armorStandClone) { PacketPlayOutEntityDestroy entityDestroy = new PacketPlayOutEntityDestroy(armorStand.getId()); craftPlayer.getHandle().playerConnection.sendPacket(entityDestroy); @@ -78,15 +78,8 @@ public void remove(){ } } - @Override + /*@Override public void interact(Action action) { - BlockIterator iter = new BlockIterator(player, 3); - while (iter.hasNext()) { - if (iter.next().equals(location.getBlock())) { - action.execute(player); - } - } - /* final int[] id = {0}; ChannelDuplexHandler channelDuplexHandler = new ChannelDuplexHandler(){ @Override @@ -111,11 +104,11 @@ public void channelRead(ChannelHandlerContext channelHandlerContext, Object pack } }; ChannelPipeline channelPipeline = ((CraftPlayer) player).getHandle().playerConnection.networkManager.channel.pipeline(); - channelPipeline.addBefore("packet_handler", player.getName() + "/" + id[0], channelDuplexHandler);*/ - } + channelPipeline.addBefore("packet_handler", player.getName() + "/" + id[0], channelDuplexHandler); + }*/ @Override - public void teleport(Location location){ + public void teleport(Location location) { this.setLocation(location); Location locationClone = location.clone(); @@ -126,10 +119,16 @@ public void teleport(Location location){ } @Override - public void setTextList(List textList){ + public void setTextList(List textList) { this.textList = textList; this.update(); + } + public Player getPlayer() { + return player; } + public void setPlayer(Player player) { + this.player = player; + } } diff --git a/src/main/java/fr/watch54/displays/holograms/server/HologramServer.java b/src/main/java/fr/watch54/displays/holograms/server/HologramServer.java index 627feaa..a460fea 100644 --- a/src/main/java/fr/watch54/displays/holograms/server/HologramServer.java +++ b/src/main/java/fr/watch54/displays/holograms/server/HologramServer.java @@ -16,25 +16,22 @@ public class HologramServer extends Hologram { private Map armorStandTextMap; - private Action action; - public HologramServer(List textList, Location location){ + public HologramServer(List textList, Location location) { super(textList, location); this.armorStandTextMap = new HashMap<>(); - } @Override - public void display(){ + public void display() { - if(this.getTextList() == null) throw new NullPointerException("Texts cannot be null!"); - if(this.getLocation() == null) throw new NullPointerException("Location cannot be null!"); + if (this.getTextList() == null) throw new NullPointerException("Texts cannot be null!"); + if (this.getLocation() == null) throw new NullPointerException("Location cannot be null!"); Location locationClone = this.getLocation().clone(); World world = locationClone.getWorld(); - for(Text text : this.getTextList()){ - + for (Text text : this.getTextList()) { ArmorStand armorStand = (ArmorStand) world.spawnEntity(locationClone, EntityType.ARMOR_STAND); armorStand.setGravity(false); armorStand.setVisible(false); @@ -45,56 +42,37 @@ public void display(){ locationClone.add(0, -0.3D, 0); this.setSpawned(true); - } - } @Override - public void update(){ + public void update() { armorStandTextMap.keySet().forEach(armorStand -> armorStand.setCustomName(armorStandTextMap.get(armorStand).getText())); - } @Override - public void remove(){ + public void remove() { armorStandTextMap.keySet().forEach(Entity::remove); armorStandTextMap.clear(); this.setSpawned(false); - } @Override - public void interact(Action action){ - this.action = action; - - } - - @Override - public void teleport(Location location){ - + public void teleport(Location location) { this.setLocation(location); Location locationClone = location.clone(); armorStandTextMap.keySet().forEach(armorStand -> { armorStand.teleport(locationClone); locationClone.add(0, -0.3D, 0); - }); - } @Override - public void setTextList(List textList){ + public void setTextList(List textList) { this.textList = textList; this.remove(); this.display(); - - } - - public Action getAction(){ - return action; - } } diff --git a/src/main/java/fr/watch54/displays/listeners/Listeners.java b/src/main/java/fr/watch54/displays/listeners/Listeners.java index 724290c..de6b07d 100644 --- a/src/main/java/fr/watch54/displays/listeners/Listeners.java +++ b/src/main/java/fr/watch54/displays/listeners/Listeners.java @@ -1,19 +1,22 @@ package fr.watch54.displays.listeners; -import fr.watch54.displays.holograms.Hologram; +import fr.watch54.displays.holograms.client.HologramClient; import fr.watch54.displays.holograms.server.HologramServer; -import fr.watch54.displays.interfaces.Action; import fr.watch54.displays.managers.HologramManager; +import org.bukkit.Location; import org.bukkit.block.Block; import org.bukkit.entity.ArmorStand; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerInteractAtEntityEvent; +import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.world.ChunkUnloadEvent; -import java.util.Optional; +import java.util.ArrayList; +import java.util.List; /** * Copyright (C) gameszaum, all rights reserved, unauthorized @@ -29,27 +32,37 @@ public Listeners(HologramManager hologramManager) { this.hologramManager = hologramManager; } - @EventHandler - public void onInteract(PlayerInteractAtEntityEvent event) { + + @EventHandler(priority = EventPriority.MONITOR) + public void clientSide(PlayerInteractEvent event) { + if (hologramManager.getHologramMap().size() > 0 && hologramManager.getHologramMap().values().stream().anyMatch(hologram -> hologram instanceof HologramClient)) { + for (Block block : getNearbyBlocks(event.getPlayer(), 4)) { + if (hologramManager.containsHologram(block)) { + hologramManager.getHologram(block).filter(hologram -> hologram instanceof HologramClient).map(hologram -> (HologramClient) hologram).ifPresent(hologram -> { + if (hologram.getPlayer().getName().equals(event.getPlayer().getName())) { + if (hologram.getAction() != null) { + hologram.getAction().execute(event.getPlayer()); + } + } + }); + return; + } + } + } + } + + @EventHandler(priority = EventPriority.MONITOR) + public void serverSide(PlayerInteractAtEntityEvent event) { Player player = event.getPlayer(); Entity entity = event.getRightClicked(); if (entity instanceof ArmorStand) { - Block block = entity.getLocation().getBlock(); if (hologramManager.containsHologram(block)) { - - Optional optionalHologram = hologramManager.getHologram(block); - - optionalHologram.ifPresent(hologram -> { - - if (hologram instanceof HologramServer) { - - HologramServer server = (HologramServer) hologram; - Action action = server.getAction(); - - if (action != null) action.execute(player); + hologramManager.getHologram(block).filter(hologram -> hologram instanceof HologramServer).map(hologram -> (HologramServer) hologram).ifPresent(hologram -> { + if (hologram.getAction() != null) { + hologram.getAction().execute(player); } }); } @@ -61,4 +74,18 @@ public void onChunkUnload(ChunkUnloadEvent event) { event.setCancelled(true); } + private List getNearbyBlocks(Player player, int radius) { + List blocks = new ArrayList<>(); + Location location = player.getLocation(); + + for (int x = location.getBlockX() - radius; x <= location.getBlockX() + radius; x++) { + for (int y = location.getBlockY() - radius; y <= location.getBlockY() + radius; y++) { + for (int z = location.getBlockZ() - radius; z <= location.getBlockZ() + radius; z++) { + blocks.add(location.getWorld().getBlockAt(x, y, z)); + } + } + } + return blocks; + } + } diff --git a/src/main/java/fr/watch54/displays/managers/HologramManager.java b/src/main/java/fr/watch54/displays/managers/HologramManager.java index 51c90e6..c83cc39 100644 --- a/src/main/java/fr/watch54/displays/managers/HologramManager.java +++ b/src/main/java/fr/watch54/displays/managers/HologramManager.java @@ -22,16 +22,16 @@ public class HologramManager { private Plugin plugin; private Map hologramMap; - public HologramManager(Plugin plugin){ + public HologramManager(Plugin plugin) { this.plugin = plugin; this.hologramMap = new HashMap<>(); Bukkit.getPluginManager().registerEvents(new Listeners(this), plugin); } - public HologramServer createServer(List textList, Location location, boolean refresh){ + public HologramServer createServer(List textList, Location location, boolean refresh) { Block block = location.getBlock(); - if(this.containsHologram(block)) return (HologramServer) hologramMap.get(block); + if (this.containsHologram(block)) return (HologramServer) hologramMap.get(block); HologramServer hologramServer = new HologramServer(textList, location); Bukkit.getScheduler().runTaskLater(plugin, hologramServer::display, 20); @@ -45,7 +45,7 @@ public HologramServer createServer(List textList, Location location, boole } - public HologramClient createClient(Player player, List textList, Location location, boolean refresh){ + public HologramClient createClient(Player player, List textList, Location location, boolean refresh) { HologramClient hologram = new HologramClient(player, textList, location); Bukkit.getScheduler().runTaskLater(plugin, hologram::display, 20); @@ -58,19 +58,19 @@ public HologramClient createClient(Player player, List textList, Location } - public void teleport(Hologram hologram, Location location){ - if(!hologramMap.containsValue(hologram)) return; + public void teleport(Hologram hologram, Location location) { + if (!hologramMap.containsValue(hologram)) return; hologramMap.entrySet().removeIf(entry -> entry.getValue().equals(hologram)); hologramMap.put(location.getBlock(), hologram); hologram.teleport(location); } - public boolean containsHologram(Block block){ + public boolean containsHologram(Block block) { return hologramMap.keySet().stream().anyMatch(blockMap -> block.getX() == blockMap.getX() && block.getZ() == blockMap.getZ()); } - public Optional getHologram(Block block){ + public Optional getHologram(Block block) { Optional blockFinal = hologramMap .keySet() .stream() @@ -79,12 +79,14 @@ public Optional getHologram(Block block){ Hologram hologram = null; - if(blockFinal.isPresent()) hologram = hologramMap.get(blockFinal.get()); + if (blockFinal.isPresent()) { + hologram = hologramMap.get(blockFinal.get()); + } return Optional.ofNullable(hologram); } - public void remove(Hologram hologram){ - if(!hologramMap.containsValue(hologram)) return; + public void remove(Hologram hologram) { + if (!hologramMap.containsValue(hologram)) return; hologram.remove(); hologramMap.entrySet().removeIf(entry -> entry.getValue().equals(hologram)); diff --git a/src/main/java/fr/watch54/displays/tasks/HologramTask.java b/src/main/java/fr/watch54/displays/tasks/HologramTask.java index a82546f..ca728c3 100644 --- a/src/main/java/fr/watch54/displays/tasks/HologramTask.java +++ b/src/main/java/fr/watch54/displays/tasks/HologramTask.java @@ -15,7 +15,6 @@ public HologramTask(Hologram hologram, HologramManager hologramManager, boolean this.hologram = hologram; this.hologramManager = hologramManager; this.refresh = refresh; - } @Override diff --git a/src/main/java/resources/plugin.yml b/src/main/java/resources/plugin.yml index 01eeef7..1f2ee22 100644 --- a/src/main/java/resources/plugin.yml +++ b/src/main/java/resources/plugin.yml @@ -1,4 +1,4 @@ name: HologramDisplays -author: Watch54 +authors: [Watch54, gameszaum] main: fr.watch54.displays.Plugin version: 1.0.0 \ No newline at end of file From 434e2d13e721fe31c311e0b5517867666be375bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Vila=C3=A7a?= <34211090+gameszaum@users.noreply.github.com> Date: Thu, 18 Mar 2021 04:34:22 -0300 Subject: [PATCH 4/4] Update README.md --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 3996f62..e6b99ca 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # DisplayAPI Display API, a library to create holograms on Minecraft servers (1.8.8 ONLY) -**You must add the API as your project dependencies and add this to plugins folder** +**You must add the API as your project dependencies and use the scope compile** ## Features: - For now, only 1.8.8 because i'm using some reflections @@ -15,7 +15,6 @@ Display API, a library to create holograms on Minecraft servers (1.8.8 ONLY) - Make it no blinking on client-side - Make it multi-version - Add ProtocolLib as dependencies for safe-use - - Add Gradle/Maven dependences for this project ## Examples: