Skip to content

Commit

Permalink
better patch for silent switch
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Aug 8, 2024
1 parent 940ce81 commit e5433fc
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 197 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package me.xginko.aef.modules.combat;

import com.cryptomorin.xseries.XEntityType;
import me.xginko.aef.modules.AEFModule;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemHeldEvent;
import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.event.player.PlayerQuitEvent;

import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

public class SilentSwapDelay extends AEFModule implements Listener {

private final Map<UUID, PlayerData> playerDataMap;
private final long swapDelayMillis;

public SilentSwapDelay() {
super("combat.crystal-aura.silent-swap-delay");
this.playerDataMap = new ConcurrentHashMap<>();
this.swapDelayMillis = config.getLong(configPath + ".min-swap-delay-millis", 50L,
"The delay in millis a player cant swap hotbar items after placing\n" +
"a block, clicking a block (for example to place a crystal) or\n" +
"damaging an entity. (50 ms = 1 tick)");
}

@Override
public void enable() {
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}

@Override
public boolean shouldEnable() {
return config.getBoolean(configPath + ".enable", false);
}

@Override
public void disable() {
HandlerList.unregisterAll(this);
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onPlayerItemHeld(PlayerItemHeldEvent event) { // Fired when a hot bar item selection changes
PlayerData playerData = playerDataMap.computeIfAbsent(event.getPlayer().getUniqueId(), PlayerData::new);

if (playerData.lastAttackEntityTimeMillis.get() + swapDelayMillis > System.currentTimeMillis()) {
event.setCancelled(true);
return;
}

if (playerData.lastInteractBlockTimeMillis.get() + swapDelayMillis > System.currentTimeMillis()) {
event.setCancelled(true);
return;
}

if (playerData.lastPlaceBlockTimeMillis.get() + swapDelayMillis > System.currentTimeMillis()) {
event.setCancelled(true);
}
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
private void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
if (event.getDamager().getType() != XEntityType.PLAYER.get()) return;

playerDataMap.computeIfAbsent(event.getDamager().getUniqueId(), PlayerData::new)
.lastAttackEntityTimeMillis.set(System.currentTimeMillis());
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
private void onPlayerInteract(PlayerInteractEvent event) {
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return;

playerDataMap.computeIfAbsent(event.getPlayer().getUniqueId(), PlayerData::new)
.lastInteractBlockTimeMillis.set(System.currentTimeMillis());
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
private void onBlockPlace(BlockPlaceEvent event) {
playerDataMap.computeIfAbsent(event.getPlayer().getUniqueId(), PlayerData::new)
.lastPlaceBlockTimeMillis.set(System.currentTimeMillis());
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
private void onPlayerQuit(PlayerQuitEvent event) {
playerDataMap.remove(event.getPlayer().getUniqueId());
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
private void onPlayerKicked(PlayerKickEvent event) {
playerDataMap.remove(event.getPlayer().getUniqueId());
}

private static class PlayerData {

public final UUID uuid;
public final AtomicLong lastInteractBlockTimeMillis, lastAttackEntityTimeMillis, lastPlaceBlockTimeMillis;

public PlayerData(UUID uuid) {
this.uuid = uuid;
this.lastInteractBlockTimeMillis = new AtomicLong();
this.lastAttackEntityTimeMillis = new AtomicLong();
this.lastPlaceBlockTimeMillis = new AtomicLong();
}
}
}

This file was deleted.

Loading

0 comments on commit e5433fc

Please sign in to comment.