Skip to content

Commit

Permalink
[1.5.7] - Fix clicking on entities from other plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
Luncaaa committed Nov 22, 2024
1 parent 812bc65 commit 50321ea
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 108 deletions.
Original file line number Diff line number Diff line change
@@ -1,47 +1,8 @@
package me.lucaaa.advanceddisplays.nms_common;

import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.event.inventory.ClickType;

import javax.annotation.Nonnull;

@SuppressWarnings("unused")
public class InternalEntityClickEvent extends Event {
private static final HandlerList HANDLER_LIST = new HandlerList();
private final Player player;
private final ClickType clickType;
private final int interactionId;

public InternalEntityClickEvent(Player player, ClickType clickType, int interactionId) {
super(true); // So that players do not get kicked when the event is called.
this.player = player;
this.clickType = clickType;
this.interactionId = interactionId;
}

@Override
@Nonnull
public HandlerList getHandlers() {
return HANDLER_LIST;
}

public static HandlerList getHandlerList() {
return HANDLER_LIST;
}

public Player getPlayer() {
return player;
}

public ClickType getClickType() {
return clickType;
}

public int getInteractionId() {
return interactionId;
}
public record InternalEntityClickEvent(ClickType clickType, int interactionId) {

public static ClickType getClickTypeFromPacket(boolean isSneaking, int clickTypeNumber) {
ClickType clickType;
Expand All @@ -52,4 +13,4 @@ public static ClickType getClickTypeFromPacket(boolean isSneaking, int clickType
}
return clickType;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public InternalEntityClickEvent getClickEvent(Player player, Object anyPacket) {

ClickType clickType = InternalEntityClickEvent.getClickTypeFromPacket(packet.isUsingSecondaryAction(), clickTypeNumber);

return new InternalEntityClickEvent(player, clickType, interactionId);
return new InternalEntityClickEvent(clickType, interactionId);

} catch (NoSuchFieldException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
Logger.logError(java.util.logging.Level.SEVERE, "An error occurred while handling a click on a display: ", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public InternalEntityClickEvent getClickEvent(Player player, Object anyPacket) {

ClickType clickType = InternalEntityClickEvent.getClickTypeFromPacket(packet.isUsingSecondaryAction(), clickTypeNumber);

return new InternalEntityClickEvent(player, clickType, interactionId);
return new InternalEntityClickEvent(clickType, interactionId);

} catch (NoSuchFieldException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
Logger.logError(java.util.logging.Level.SEVERE, "An error occurred while handling a click on a display: ", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public InternalEntityClickEvent getClickEvent(Player player, Object anyPacket) {

ClickType clickType = InternalEntityClickEvent.getClickTypeFromPacket(packet.isUsingSecondaryAction(), clickTypeNumber);

return new InternalEntityClickEvent(player, clickType, interactionId);
return new InternalEntityClickEvent(clickType, interactionId);

} catch (NoSuchFieldException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
Logger.logError(java.util.logging.Level.SEVERE, "An error occurred while handling a click on a display: ", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public InternalEntityClickEvent getClickEvent(Player player, Object anyPacket) {

ClickType clickType = InternalEntityClickEvent.getClickTypeFromPacket(packet.isUsingSecondaryAction(), clickTypeNumber);

return new InternalEntityClickEvent(player, clickType, interactionId);
return new InternalEntityClickEvent(clickType, interactionId);

} catch (NoSuchFieldException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
Logger.logError(java.util.logging.Level.SEVERE, "An error occurred while handling a click on a display: ", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public InternalEntityClickEvent getClickEvent(Player player, Object anyPacket) {

ClickType clickType = InternalEntityClickEvent.getClickTypeFromPacket(packet.isUsingSecondaryAction(), clickTypeNumber);

return new InternalEntityClickEvent(player, clickType, interactionId);
return new InternalEntityClickEvent(clickType, interactionId);

} catch (NoSuchFieldException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
Logger.logError(java.util.logging.Level.SEVERE, "An error occurred while handling a click on a display: ", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public InternalEntityClickEvent getClickEvent(Player player, Object anyPacket) {

ClickType clickType = InternalEntityClickEvent.getClickTypeFromPacket(packet.isUsingSecondaryAction(), clickTypeNumber);

return new InternalEntityClickEvent(player, clickType, interactionId);
return new InternalEntityClickEvent(clickType, interactionId);

} catch (NoSuchFieldException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
Logger.logError(java.util.logging.Level.SEVERE, "An error occurred while handling a click on a display: ", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public InternalEntityClickEvent getClickEvent(Player player, Object anyPacket) {

ClickType clickType = InternalEntityClickEvent.getClickTypeFromPacket(packet.isUsingSecondaryAction(), clickTypeNumber);

return new InternalEntityClickEvent(player, clickType, interactionId);
return new InternalEntityClickEvent(clickType, interactionId);

} catch (NoSuchFieldException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
Logger.logError(java.util.logging.Level.SEVERE, "An error occurred while handling a click on a display: ", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ public void onEnable() {

// Register events.
getServer().getPluginManager().registerEvents(new PlayerEventsListener(this), this);
getServer().getPluginManager().registerEvents(new InternalEntityClickListener(this), this);
getServer().getPluginManager().registerEvents(new InventoryEventsListener(this), this);

// Registers the main command and adds tab completions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ public void setVisibility(Visibility visibility, Player player) {

@Override
public boolean isVisibleByPlayer(Player player) {
if (!player.getWorld().equals(display.getLocation().getWorld())) return false;

boolean def = globalVisibility == Visibility.SHOW;
boolean individual = individualVis.containsKey(player.getName()) && individualVis.get(player.getName()) == Visibility.SHOW;
boolean perm = (display.getPermission().equalsIgnoreCase("none") || player.hasPermission(display.getPermission())) && (display.getHidePermission().equalsIgnoreCase("none") || !player.hasPermission(display.getHidePermission()));
boolean inRange = display.getViewDistance() <= 0.0 || player.getLocation().distance(display.getLocation()) <= display.getViewDistance();
boolean inWorld = display.getLocation().getWorld() == player.getLocation().getWorld();

return (def || individual) && perm && inRange && inWorld;
return (def || individual) && perm && inRange;
}

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import me.lucaaa.advanceddisplays.AdvancedDisplays;
import me.lucaaa.advanceddisplays.api.actions.ClickType;
import me.lucaaa.advanceddisplays.displays.ADBaseDisplay;
import me.lucaaa.advanceddisplays.nms_common.InternalEntityClickEvent;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

public class PlayerPacketManager extends ChannelDuplexHandler {
private final AdvancedDisplays plugin;
public static final String IDENTIFIER = "ad_packet_manager";
private final Player player;
private boolean pastInteraction = false;

public PlayerPacketManager(AdvancedDisplays plugin, Player player) {
this.plugin = plugin;
Expand All @@ -19,12 +22,44 @@ public PlayerPacketManager(AdvancedDisplays plugin, Player player) {

@Override
public void channelRead(ChannelHandlerContext ctx, Object packet) throws Exception {
super.channelRead(ctx, packet);

InternalEntityClickEvent clickEvent = plugin.getPacketsManager().getPackets().getClickEvent(player, packet);
if (clickEvent == null) return;

ClickType clickType = ClickType.getFromBukkit(clickEvent.clickType());

if (clickEvent != null) {
Bukkit.getPluginManager().callEvent(clickEvent);
// Because the event is fired twice, the first time the event is run and the variable is set to "true".
// The second time, when the variable is true, the event will be ignored.
if (clickType == ClickType.RIGHT || clickType == ClickType.SHIFT_RIGHT) {
if (pastInteraction) {
pastInteraction = false;
return;
} else {
pastInteraction = true;
}

/* Old System
if (pastInteraction > 0) {
long now = System.currentTimeMillis();
if (now - pastInteraction <= 500) {
pastInteraction = 0;
return;
}
} else {
pastInteraction = System.currentTimeMillis();
} */
}

super.channelRead(ctx, packet);
ADBaseDisplay display = plugin.getInteractionsManager().getDisplay(clickEvent.interactionId());
if (display == null) return;

// Run sync to prevent errors
new BukkitRunnable() {
@Override
public void run() {
display.runActions(player,clickType);
}
}.runTask(plugin);
}
}
}

0 comments on commit 50321ea

Please sign in to comment.