Skip to content

Commit

Permalink
Fix following holograms gets deleted bug
Browse files Browse the repository at this point in the history
Use HolographicDisplays' workaround for a ProtocolLib until it gets fixed
  • Loading branch information
Blueyescat committed Aug 29, 2019
1 parent 92d8234 commit 0f4f8a0
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 52 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
lib/

## Gradle
.gradle
build/
Expand Down
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group 'me.blueyescat.skriptholo'
version '1.0.1'
version '1.0.2'

sourceCompatibility = 1.8
compileJava.options.encoding = 'UTF-8'
Expand All @@ -25,10 +25,11 @@ repositories {
}

dependencies {
implementation 'org.spigotmc:spigot-api:1.14.3-R0.1-SNAPSHOT'
implementation 'org.spigotmc:spigot-api:1.14.4-R0.1-SNAPSHOT'
implementation 'com.github.SkriptLang:Skript:dev36'
implementation 'com.gmail.filoghost.holographicdisplays:holographicdisplays-api:2.3.2'
implementation 'com.comphenix.protocol:ProtocolLib:4.4.0'
compile fileTree(include: ['*.jar'], dir: 'lib')
}

processResources {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package me.blueyescat.skriptholo;

import java.util.List;
import java.util.Map;

import org.bukkit.Bukkit;
Expand All @@ -9,9 +8,11 @@
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.scheduler.BukkitRunnable;

import ch.njol.skript.Skript;
import ch.njol.skript.util.Direction;

import com.comphenix.protocol.PacketType;
Expand All @@ -20,19 +21,24 @@
import com.comphenix.protocol.events.ListenerPriority;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketEvent;
import com.destroystokyo.paper.event.entity.EntityRemoveFromWorldEvent;
import com.gmail.filoghost.holographicdisplays.HolographicDisplays;
import com.gmail.filoghost.holographicdisplays.api.Hologram;

import me.blueyescat.skriptholo.skript.effects.EffCreateHologram;
import me.blueyescat.skriptholo.util.Utils;

public class Listeners implements Listener {
public class FollowingHologramListeners implements Listener {

private final static boolean entityRemoveEventExists = Skript.classExists("com.destroystokyo.paper.event.entity.EntityRemoveFromWorldEvent");

public static void start() {
if (SkriptHolo.startedFollowingHologramTasks)
return;
SkriptHolo.startedFollowingHologramTasks = true;

Bukkit.getPluginManager().registerEvents(new Listeners(), SkriptHolo.getInstance());
Bukkit.getPluginManager().registerEvents(new FollowingHologramListeners(), SkriptHolo.getInstance());
if (entityRemoveEventExists)
Bukkit.getPluginManager().registerEvents(new FollowingHologramListeners.EntityRemoveListener(), SkriptHolo.getInstance());

ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();

Expand All @@ -53,7 +59,14 @@ public void onPacketSending(PacketEvent event) {
continue;
}
Player player = event.getPlayer();
Entity entity = event.getPacket().getEntityModifier(event).getValues().get(0);
Entity entity;
try {
entity = event.getPacket().getEntityModifier(event).read(0);
} catch (Exception e) {
// Use HolographicDisplays' workaround for the ProtocolLib bug
entity = HolographicDisplays.getNMSManager()
.getEntityFromID(player.getWorld(), event.getPacket().getIntegers().read(0));
}
if (player.equals(entity) && !holo.getVisibilityManager().isVisibleTo(player))
continue;
Location location = entity.getLocation().clone();
Expand All @@ -63,36 +76,29 @@ public void onPacketSending(PacketEvent event) {
}
});

protocolManager.addPacketListener(
new PacketAdapter(SkriptHolo.getInstance(), ListenerPriority.NORMAL, PacketType.Play.Server.ENTITY_DESTROY) {
@Override
public void onPacketSending(PacketEvent event) {
for (int entityID : event.getPacket().getIntegerArrays().read(0))
SkriptHolo.deleteFollowingHolograms(entityID);
}
});

new BukkitRunnable() {
@SuppressWarnings("unchecked")
@Override
public void run() {
for (Object o : SkriptHolo.followingHologramsEntities.entrySet()) {
Map.Entry entry = (Map.Entry) o;
Entity entity = (Entity) entry.getKey();
if (!entity.isValid()) {
for (Hologram holo : (List<Hologram>) entry.getValue()) {
if (!holo.isDeleted())
holo.delete();
if (holo.equals(EffCreateHologram.lastCreated))
EffCreateHologram.lastCreated = null;
SkriptHolo.followingHologramsList.remove(holo);
if (!entityRemoveEventExists) {
protocolManager.addPacketListener(
new PacketAdapter(SkriptHolo.getInstance(), ListenerPriority.NORMAL, PacketType.Play.Server.ENTITY_DESTROY) {
@Override
public void onPacketSending(PacketEvent event) {
for (int entityID : event.getPacket().getIntegerArrays().read(0)) {
if (SkriptHolo.followingHolograms.containsKey(entityID)) {
Utils.cleanFollowingHolograms();
return;
}
}
}
SkriptHolo.followingHologramsEntities.remove(entity);
SkriptHolo.followingHolograms.remove(entity.getEntityId());
}
});

new BukkitRunnable() {
@Override
public void run() {
Utils.cleanFollowingHolograms();
}
}
}.runTaskTimerAsynchronously(SkriptHolo.getInstance(), 60, 0);
}.runTaskTimerAsynchronously(SkriptHolo.getInstance(), 0, 20 * 5);

Bukkit.getPluginManager().registerEvents(new FollowingHologramListeners.EntityDeathListener(), SkriptHolo.getInstance());
}
}

@EventHandler
Expand All @@ -119,4 +125,22 @@ public void onPlayerMove(PlayerMoveEvent event) {
}
}

static class EntityRemoveListener implements Listener {

@EventHandler
public void onEntityRemove(EntityRemoveFromWorldEvent event) {
Utils.deleteFollowingHolograms(event.getEntity().getEntityId());
}

}

static class EntityDeathListener implements Listener {

@EventHandler
public void onEntityDeath(EntityDeathEvent event) {
Utils.deleteFollowingHolograms(event.getEntity().getEntityId());
}

}

}
12 changes: 0 additions & 12 deletions src/main/java/me/blueyescat/skriptholo/SkriptHolo.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.gmail.filoghost.holographicdisplays.api.Hologram;

import me.blueyescat.skriptholo.util.Metrics;
import me.blueyescat.skriptholo.util.Utils;

public class SkriptHolo extends JavaPlugin implements Listener {

Expand Down Expand Up @@ -61,17 +60,6 @@ public void onEnable() {
getLogger().info("Finished loading!");
}

static void deleteFollowingHolograms(int entityID) {
Map<Hologram, Direction[]> holoMap = followingHolograms.get(entityID);
if (holoMap == null || holoMap.isEmpty())
return;
for (Object o : holoMap.entrySet()) {
Map.Entry entry = (Map.Entry) o;
Hologram holo = (Hologram) entry.getKey();
Utils.deleteHologram(entityID, holo);
}
}

public static SkriptAddon getAddonInstance() {
if (addonInstance == null)
addonInstance = Skript.registerAddon(getInstance());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public class CondIsHologramVisibleByDefault extends Condition {

@SuppressWarnings("unchecked")
@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
if (!Utils.hasPlugin("ProtocolLib")) {
Skript.error("The hologram visibility feature requires ProtocolLib");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import com.gmail.filoghost.holographicdisplays.api.HologramsAPI;
import com.gmail.filoghost.holographicdisplays.api.line.HologramLine;

import me.blueyescat.skriptholo.Listeners;
import me.blueyescat.skriptholo.FollowingHologramListeners;
import me.blueyescat.skriptholo.SkriptHolo;
import me.blueyescat.skriptholo.util.Utils;

Expand Down Expand Up @@ -139,7 +139,7 @@ protected void execute(Event e) {
if (isFollowing) {
assert entity != null;
Utils.makeHologramStartFollowing(holo, entity, offset);
Listeners.start();
FollowingHologramListeners.start();
}
if (variable != null)
variable.change(e, CollectionUtils.array(holo), (variable.isList() ? ChangeMode.ADD : ChangeMode.SET));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import com.gmail.filoghost.holographicdisplays.api.Hologram;

import me.blueyescat.skriptholo.Listeners;
import me.blueyescat.skriptholo.FollowingHologramListeners;
import me.blueyescat.skriptholo.util.Utils;

@Name("Following Hologram")
Expand Down Expand Up @@ -78,7 +78,7 @@ protected void execute(Event e) {
assert entity != null;
Utils.makeHologramStartFollowing(holo, entity, offset);
}
Listeners.start();
FollowingHologramListeners.start();
} else {
for (Hologram holo : holograms.getArray(e)) {
if (Utils.isFollowingHologram(holo))
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/me/blueyescat/skriptholo/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.gmail.filoghost.holographicdisplays.api.line.ItemLine;
import com.gmail.filoghost.holographicdisplays.api.line.TouchableLine;

import me.blueyescat.skriptholo.SkriptHolo;
import me.blueyescat.skriptholo.SkriptHolo;
import me.blueyescat.skriptholo.skript.effects.EffCreateHologram;

Expand Down Expand Up @@ -80,6 +81,36 @@ public static void deleteHologram(Hologram... holograms) {
deleteHologram(null, holograms);
}

public static void deleteFollowingHolograms(int entityID) {
Map<Hologram, Direction[]> holoMap = SkriptHolo.followingHolograms.get(entityID);
if (holoMap == null || holoMap.isEmpty())
return;
for (Object o : holoMap.entrySet()) {
Map.Entry entry = (Map.Entry) o;
Hologram holo = (Hologram) entry.getKey();
Utils.deleteHologram(entityID, holo);
}
}

@SuppressWarnings("unchecked")
public static void cleanFollowingHolograms() {
for (Object o : SkriptHolo.followingHologramsEntities.entrySet()) {
Map.Entry entry = (Map.Entry) o;
Entity entity = (Entity) entry.getKey();
if (!entity.isValid()) {
for (Hologram holo : (List<Hologram>) entry.getValue()) {
if (!holo.isDeleted())
holo.delete();
if (holo.equals(EffCreateHologram.lastCreated))
EffCreateHologram.lastCreated = null;
SkriptHolo.followingHologramsList.remove(holo);
}
SkriptHolo.followingHologramsEntities.remove(entity);
SkriptHolo.followingHolograms.remove(entity.getEntityId());
}
}
}

public static Location offsetLocation(Location loc, Direction... directions) {
for (Direction d : directions)
loc = d.getRelative(loc);
Expand Down

0 comments on commit 0f4f8a0

Please sign in to comment.