offhand switch cooldown

Hello, is there possible way to add cooldown to switching offhands, please let me know if there is. Thanks!
12 Replies
Admincraft Meta
Thanks for asking your question!
Make sure to provide as much helpful information as possible such as logs/what you tried and what your exact issue is
Make sure to mark solved when issue is solved!!!
/close !close !solved !answered
Requested by itzerpandx#0
Discount Milk
Discount Milk2y ago
Probably if you write a plugin for it.
ItzErpandX
ItzErpandXOP2y ago
i just did but there's some error that is spamming in console Can you help me?
[19:35:30 ERROR]: Could not pass event PlayerSwapHandItemsEvent to switchoffhandcooldown v1.0-SNAPSHOT
java.lang.NoClassDefFoundError: poisonbox/itzerpandx/erpand/OffhandCooldownPlugin$1
at poisonbox.itzerpandx.erpand.OffhandCooldownPlugin.onPlayerSwapHandItems(OffhandCooldownPlugin.java:40) ~[erpand-1.0-SNAPSHOT.jar:?]
at com.destroystokyo.paper.event.executor.asm.generated.GeneratedEventExecutor210.execute(Unknown Source) ~[?:?]
at org.bukkit.plugin.EventExecutor$2.execute(EventExecutor.java:77) ~[paper-api-1.20.1-R0.1-SNAPSHOT.jar:?]
at co.aikar.timings.TimedEventExecutor.execute(TimedEventExecutor.java:81) ~[paper-api-1.20.1-R0.1-SNAPSHOT.jar:git-Paper-82]
at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[paper-api-1.20.1-R0.1-SNAPSHOT.jar:?]
at io.papermc.paper.plugin.manager.PaperEventManager.callEvent(PaperEventManager.java:54) ~[paper-1.20.1.jar:git-Paper-82]
at io.papermc.paper.plugin.manager.PaperPluginManagerImpl.callEvent(PaperPluginManagerImpl.java:126) ~[paper-1.20.1.jar:git-Paper-82]
at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:615) ~[paper-api-1.20.1-R0.1-SNAPSHOT.jar:?]
at net.minecraft.server.network.ServerGamePacketListenerImpl.handlePlayerAction(ServerGamePacketListenerImpl.java:1810) ~[?:?]
at net.minecraft.network.protocol.game.ServerboundPlayerActionPacket.handle(ServerboundPlayerActionPacket.java:42) ~[?:?]
at net.minecraft.network.protocol.game.ServerboundPlayerActionPacket.a(ServerboundPlayerActionPacket.java:15) ~[?:?]
at net.minecraft.network.protocol.PacketUtils.lambda$ensureRunningOnSameThread$0(PacketUtils.java:51) ~[?:?]
at net.minecraft.server.TickTask.run(TickTask.java:18) ~[paper-1.20.1.jar:git-Paper-82]
at net.minecraft.util.thread.BlockableEventLoop.doRunTask(BlockableEventLoop.java:153) ~[?:?]
at net.minecraft.util.thread.ReentrantBlockableEventLoop.doRunTask(ReentrantBlockableEventLoop.java:24) ~[?:?]
at net.minecraft.server.MinecraftServer.doRunTask(MinecraftServer.java:1339) ~[paper-1.20.1.jar:git-Paper-82]
at net.minecraft.server.MinecraftServer.d(MinecraftServer.java:197) ~[paper-1.20.1.jar:git-Paper-82]
at net.minecraft.util.thread.BlockableEventLoop.pollTask(BlockableEventLoop.java:126) ~[?:?]
at net.minecraft.server.MinecraftServer.pollTaskInternal(MinecraftServer.java:1316) ~[paper-1.20.1.jar:git-Paper-82]
at net.minecraft.server.MinecraftServer.pollTask(MinecraftServer.java:1309) ~[paper-1.20.1.jar:git-Paper-82]
at net.minecraft.util.thread.BlockableEventLoop.managedBlock(BlockableEventLoop.java:136) ~[?:?]
at net.minecraft.server.MinecraftServer.waitUntilNextTick(MinecraftServer.java:1287) ~[paper-1.20.1.jar:git-Paper-82]
at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:1175) ~[paper-1.20.1.jar:git-Paper-82]
at net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:318) ~[paper-1.20.1.jar:git-Paper-82]
at java.lang.Thread.run(Thread.java:833) ~[?:?]
Caused by: java.lang.ClassNotFoundException: poisonbox.itzerpandx.erpand.OffhandCooldownPlugin$1
... 25 more
[19:35:30 ERROR]: Could not pass event PlayerSwapHandItemsEvent to switchoffhandcooldown v1.0-SNAPSHOT
java.lang.NoClassDefFoundError: poisonbox/itzerpandx/erpand/OffhandCooldownPlugin$1
at poisonbox.itzerpandx.erpand.OffhandCooldownPlugin.onPlayerSwapHandItems(OffhandCooldownPlugin.java:40) ~[erpand-1.0-SNAPSHOT.jar:?]
at com.destroystokyo.paper.event.executor.asm.generated.GeneratedEventExecutor210.execute(Unknown Source) ~[?:?]
at org.bukkit.plugin.EventExecutor$2.execute(EventExecutor.java:77) ~[paper-api-1.20.1-R0.1-SNAPSHOT.jar:?]
at co.aikar.timings.TimedEventExecutor.execute(TimedEventExecutor.java:81) ~[paper-api-1.20.1-R0.1-SNAPSHOT.jar:git-Paper-82]
at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[paper-api-1.20.1-R0.1-SNAPSHOT.jar:?]
at io.papermc.paper.plugin.manager.PaperEventManager.callEvent(PaperEventManager.java:54) ~[paper-1.20.1.jar:git-Paper-82]
at io.papermc.paper.plugin.manager.PaperPluginManagerImpl.callEvent(PaperPluginManagerImpl.java:126) ~[paper-1.20.1.jar:git-Paper-82]
at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:615) ~[paper-api-1.20.1-R0.1-SNAPSHOT.jar:?]
at net.minecraft.server.network.ServerGamePacketListenerImpl.handlePlayerAction(ServerGamePacketListenerImpl.java:1810) ~[?:?]
at net.minecraft.network.protocol.game.ServerboundPlayerActionPacket.handle(ServerboundPlayerActionPacket.java:42) ~[?:?]
at net.minecraft.network.protocol.game.ServerboundPlayerActionPacket.a(ServerboundPlayerActionPacket.java:15) ~[?:?]
at net.minecraft.network.protocol.PacketUtils.lambda$ensureRunningOnSameThread$0(PacketUtils.java:51) ~[?:?]
at net.minecraft.server.TickTask.run(TickTask.java:18) ~[paper-1.20.1.jar:git-Paper-82]
at net.minecraft.util.thread.BlockableEventLoop.doRunTask(BlockableEventLoop.java:153) ~[?:?]
at net.minecraft.util.thread.ReentrantBlockableEventLoop.doRunTask(ReentrantBlockableEventLoop.java:24) ~[?:?]
at net.minecraft.server.MinecraftServer.doRunTask(MinecraftServer.java:1339) ~[paper-1.20.1.jar:git-Paper-82]
at net.minecraft.server.MinecraftServer.d(MinecraftServer.java:197) ~[paper-1.20.1.jar:git-Paper-82]
at net.minecraft.util.thread.BlockableEventLoop.pollTask(BlockableEventLoop.java:126) ~[?:?]
at net.minecraft.server.MinecraftServer.pollTaskInternal(MinecraftServer.java:1316) ~[paper-1.20.1.jar:git-Paper-82]
at net.minecraft.server.MinecraftServer.pollTask(MinecraftServer.java:1309) ~[paper-1.20.1.jar:git-Paper-82]
at net.minecraft.util.thread.BlockableEventLoop.managedBlock(BlockableEventLoop.java:136) ~[?:?]
at net.minecraft.server.MinecraftServer.waitUntilNextTick(MinecraftServer.java:1287) ~[paper-1.20.1.jar:git-Paper-82]
at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:1175) ~[paper-1.20.1.jar:git-Paper-82]
at net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:318) ~[paper-1.20.1.jar:git-Paper-82]
at java.lang.Thread.run(Thread.java:833) ~[?:?]
Caused by: java.lang.ClassNotFoundException: poisonbox.itzerpandx.erpand.OffhandCooldownPlugin$1
... 25 more
That's the error
ProGamingDk
ProGamingDk2y ago
did u use chatgpt to make it
ItzErpandX
ItzErpandXOP2y ago
yeah ;-; in chatgpt we trust xD
ProGamingDk
ProGamingDk2y ago
-.-
ItzErpandX
ItzErpandXOP2y ago
should i send the code ;-;
ProGamingDk
ProGamingDk2y ago
well that would be a start and just dont use chatgpt kek
ItzErpandX
ItzErpandXOP2y ago
package com.example.offhandcooldown;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerSwapHandItemsEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class OffhandCooldownPlugin extends JavaPlugin implements Listener {

private Map<UUID, Long> cooldowns = new HashMap<>();

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

@EventHandler
public void onPlayerSwapHandItems(PlayerSwapHandItemsEvent event) {
Player player = event.getPlayer();
UUID playerId = player.getUniqueId();

if (hasCooldown(playerId)) {
// Player is still on cooldown
player.sendMessage("You're on cooldown. Please wait.");
event.setCancelled(true);
return;
}

// Apply cooldown and perform actions
applyCooldown(playerId);
// Perform your offhand switch actions here

// Schedule cooldown removal after 10 seconds
new BukkitRunnable() {
@Override
public void run() {
cooldowns.remove(playerId);
}
}.runTaskLater(this, 20 * 10); // 10 seconds
}

private boolean hasCooldown(UUID playerId) {
return cooldowns.containsKey(playerId) && cooldowns.get(playerId) > System.currentTimeMillis();
}

private void applyCooldown(UUID playerId) {
cooldowns.put(playerId, System.currentTimeMillis() + 10000); // 10 seconds cooldown
}
}
package com.example.offhandcooldown;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerSwapHandItemsEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class OffhandCooldownPlugin extends JavaPlugin implements Listener {

private Map<UUID, Long> cooldowns = new HashMap<>();

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

@EventHandler
public void onPlayerSwapHandItems(PlayerSwapHandItemsEvent event) {
Player player = event.getPlayer();
UUID playerId = player.getUniqueId();

if (hasCooldown(playerId)) {
// Player is still on cooldown
player.sendMessage("You're on cooldown. Please wait.");
event.setCancelled(true);
return;
}

// Apply cooldown and perform actions
applyCooldown(playerId);
// Perform your offhand switch actions here

// Schedule cooldown removal after 10 seconds
new BukkitRunnable() {
@Override
public void run() {
cooldowns.remove(playerId);
}
}.runTaskLater(this, 20 * 10); // 10 seconds
}

private boolean hasCooldown(UUID playerId) {
return cooldowns.containsKey(playerId) && cooldowns.get(playerId) > System.currentTimeMillis();
}

private void applyCooldown(UUID playerId) {
cooldowns.put(playerId, System.currentTimeMillis() + 10000); // 10 seconds cooldown
}
}
that's the code xD
Lunaiskey
Lunaiskey2y ago
can i ask why you want to add a cooldown to the switching an item between main hand and off hand?
ItzErpandX
ItzErpandXOP2y ago
because in my server there's glitch that allows you to move trough cobwebs like you have noslow hack
Discount Milk
Discount Milk2y ago
You should probably figure out what's enabling that glitch and then report it to the developer

Did you find this page helpful?