Skip to content

Commit

Permalink
use packetlistener for illegal checks
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Mar 9, 2024
1 parent 77925ab commit f0beac0
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package me.moomoo.anarchyexploitfixes.events;

import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.inventory.EquipmentSlot;

public class PacketPlayerAttackEntityEvent extends Event implements Cancellable {

private static final HandlerList handlers = new HandlerList();
private boolean isCancelled = false;

private final Player player;
private final EquipmentSlot hand;

public PacketPlayerAttackEntityEvent(Player player, EquipmentSlot hand) {
this.player = player;
this.hand = hand;
}

public PacketPlayerAttackEntityEvent(boolean isAsync, Player player, EquipmentSlot hand) {
super(isAsync);
this.player = player;
this.hand = hand;
}

public Player getPlayer() {
return player;
}

public EquipmentSlot getHand() {
return hand;
}

@Override
public void setCancelled(boolean cancel) {
isCancelled = cancel;
}

@Override
public boolean isCancelled() {
return isCancelled;
}

@Override
public HandlerList getHandlers() {
return handlers;
}

public static HandlerList getHandlerList() {
return handlers;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package me.moomoo.anarchyexploitfixes.listener;

import java.util.HashSet;
import java.util.Set;

public interface AEFListener {

boolean shouldEnable();
void enable();
void disable();

Set<AEFListener> LISTENERS = new HashSet<>();

static void reloadListeners() {
LISTENERS.forEach(AEFListener::disable);
LISTENERS.clear();

LISTENERS.add(new InteractEntityPacketListener());

for (AEFListener listener : LISTENERS) {
if (listener.shouldEnable()) {
listener.enable();
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package me.moomoo.anarchyexploitfixes.listener;

import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.event.PacketListenerAbstract;
import com.github.retrooper.packetevents.event.PacketListenerPriority;
import com.github.retrooper.packetevents.event.PacketReceiveEvent;
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientInteractEntity;
import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
import me.moomoo.anarchyexploitfixes.events.PacketPlayerAttackEntityEvent;
import org.bukkit.entity.Player;
import org.bukkit.inventory.EquipmentSlot;

public final class InteractEntityPacketListener extends PacketListenerAbstract implements AEFListener {

public InteractEntityPacketListener() {
super(PacketListenerPriority.HIGHEST);
}

@Override
public boolean shouldEnable() {
return !AnarchyExploitFixes.getConfiguration().packet_events_disabled;
}

@Override
public void enable() {
PacketEvents.getAPI().getEventManager().registerListener(this);
}

@Override
public void disable() {
PacketEvents.getAPI().getEventManager().unregisterListener(this);
}

@Override
public void onPacketReceive(PacketReceiveEvent event) {
if (event.getPacketType() != PacketType.Play.Client.INTERACT_ENTITY) return;
if (event.isCancelled()) return;

WrapperPlayClientInteractEntity packet = new WrapperPlayClientInteractEntity(event);
if (packet.getAction() != WrapperPlayClientInteractEntity.InteractAction.ATTACK) return;

final PacketPlayerAttackEntityEvent attackEvent = new PacketPlayerAttackEntityEvent(
true,
(Player) event.getPlayer(),
EquipmentSlot.valueOf(packet.getHand().name())
);

if (!attackEvent.callEvent()) {
event.setCancelled(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
import me.moomoo.anarchyexploitfixes.enums.AEFPermission;
import me.moomoo.anarchyexploitfixes.enums.ItemLegality;
import me.moomoo.anarchyexploitfixes.events.PacketPlayerAttackEntityEvent;
import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule;
import me.moomoo.anarchyexploitfixes.utils.CachingPermTool;
import org.bukkit.entity.EntityType;
Expand All @@ -25,8 +26,21 @@ public interface IllegalItemModule extends AnarchyExploitFixesModule, Listener {
ItemLegality determineLegality(ItemStack itemStack);
void handleItem(ItemStack itemStack, ItemLegality checkResult);

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
default void onAttack(PacketPlayerAttackEntityEvent event) {
final Player player = event.getPlayer();
if (player == null) return;
final ItemStack item = player.getInventory().getItem(event.getHand());
final ItemLegality legality = determineLegality(item);
if (legality != ItemLegality.LEGAL && !CachingPermTool.hasPermission(bypassPermission(), player)) {
event.setCancelled(true);
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
default void onAttack(EntityDamageByEntityEvent event) {
// Only use this as fallback
if (!AnarchyExploitFixes.getConfiguration().packet_events_disabled) return;
// Modifying this event in any way won't do anything on 1.12
if (AnarchyExploitFixes.getMCVersion() <= 12) return;
if (event.getDamager().getType() != EntityType.PLAYER) return;
Expand Down

0 comments on commit f0beac0

Please sign in to comment.