Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
alex2276564 committed Oct 11, 2024
1 parent 6607223 commit e399dc8
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 34 deletions.
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ repositories {

dependencies {
compileOnly "com.destroystokyo.paper:paper-api:1.16.5-R0.1-SNAPSHOT"

compileOnly 'org.projectlombok:lombok:1.18.34'
annotationProcessor 'org.projectlombok:lombok:1.18.34'

testCompileOnly 'org.projectlombok:lombok:1.18.34'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.34'
}

def targetJavaVersion = 16
Expand Down
26 changes: 19 additions & 7 deletions src/main/java/uz/alex2276564/leverlock/LeverLock.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
package uz.alex2276564.leverlock;

import lombok.Getter;
import org.bukkit.plugin.java.JavaPlugin;
import uz.alex2276564.leverlock.commands.ReloadCommand;
import uz.alex2276564.leverlock.listeners.PlayerLeverClickListener;
import uz.alex2276564.leverlock.utils.ConfigManager;

public final class LeverLock extends JavaPlugin {
private PlayerLeverClickListener leverClickListener;
@Getter
private static LeverLock instance;

@Override
public void onEnable() {
saveDefaultConfig();
leverClickListener = new PlayerLeverClickListener(this);
getServer().getPluginManager().registerEvents(leverClickListener, this);
getCommand("leverlockreload").setExecutor(new ReloadCommand(this));
instance = this;
registerListeners();
registerCommands();
loadUtils();
}

public void reloadListenerConfig() {
leverClickListener.reloadConfig();
private void registerListeners() {
getServer().getPluginManager().registerEvents(new PlayerLeverClickListener(this), this);
}

private void registerCommands() {
getCommand("leverlockreload").setExecutor(new ReloadCommand());
}

private void loadUtils() {
ConfigManager.reload();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,20 @@
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import uz.alex2276564.leverlock.LeverLock;
import uz.alex2276564.leverlock.utils.ConfigManager;

public class ReloadCommand implements CommandExecutor {
private final LeverLock plugin;

public ReloadCommand(LeverLock plugin) {
this.plugin = plugin;
}

@Override
public boolean onCommand(CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
String permission = "leverlock.reload";

if (!sender.hasPermission(permission)) {
sender.sendMessage("§cYou do not have permission to use this command. Missing permission: §e" + permission);
return true;
}

plugin.reloadConfig();
plugin.reloadListenerConfig();
ConfigManager.reload();
sender.sendMessage("§aLeverLock configuration successfully reloaded.");
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uz.alex2276564.leverlock.listeners;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
Expand All @@ -8,26 +9,22 @@
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.plugin.java.JavaPlugin;
import uz.alex2276564.leverlock.LeverLock;
import uz.alex2276564.leverlock.utils.ConfigManager;

import java.time.Duration;
import java.time.Instant;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class PlayerLeverClickListener implements Listener {
private Duration cooldownDuration;
private final JavaPlugin plugin;
private static final Material TARGET_BLOCK = Material.LEVER;
private long cleanupInterval;
private String cooldownMessage;

private final Map<Player, Instant> cooldownMap = new ConcurrentHashMap<>();
private final JavaPlugin plugin;

public PlayerLeverClickListener(JavaPlugin plugin) {
public PlayerLeverClickListener(LeverLock plugin) {
this.plugin = plugin;
this.cooldownDuration = Duration.ofSeconds(plugin.getConfig().getLong("cooldown.duration", 1));
this.cleanupInterval = plugin.getConfig().getLong("cleanup.interval", 300) * 20; // convert to ticks
this.cooldownMessage = plugin.getConfig().getString("message.cooldown", "§cYou are interacting with the lever too quickly!");
startCleanupTask();
}

Expand All @@ -40,29 +37,23 @@ public void onPlayerInteract(PlayerInteractEvent event) {
Instant currentTime = Instant.now();
Instant lastInteractionTime = cooldownMap.getOrDefault(player, Instant.EPOCH);

if (Duration.between(lastInteractionTime, currentTime).compareTo(cooldownDuration) < 0) {
if (Duration.between(lastInteractionTime, currentTime).compareTo(ConfigManager.getCooldownDuration()) < 0) {
event.setCancelled(true);
player.sendMessage(cooldownMessage);
player.sendMessage(ConfigManager.getCooldownMessage());
} else {
cooldownMap.put(player, currentTime);
}
}
}

private void startCleanupTask() {
plugin.getServer().getScheduler().runTaskTimer(plugin, this::cleanupCooldowns, cleanupInterval, cleanupInterval);
Bukkit.getScheduler().runTaskTimer(plugin, this::cleanupCooldowns, ConfigManager.getCleanupInterval(), ConfigManager.getCleanupInterval());
}

private void cleanupCooldowns() {
Instant now = Instant.now();
cooldownMap.entrySet().removeIf(entry ->
!entry.getKey().isOnline() || Duration.between(entry.getValue(), now).compareTo(cooldownDuration) > 0
!entry.getKey().isOnline() || Duration.between(entry.getValue(), now).compareTo(ConfigManager.getCooldownDuration()) > 0
);
}

public void reloadConfig() {
this.cooldownDuration = Duration.ofSeconds(plugin.getConfig().getLong("cooldown.duration", 1));
this.cleanupInterval = plugin.getConfig().getLong("cleanup.interval", 300) * 20;
this.cooldownMessage = plugin.getConfig().getString("message.cooldown", "§cYou are interacting with the lever too quickly!");
}
}
37 changes: 37 additions & 0 deletions src/main/java/uz/alex2276564/leverlock/utils/ConfigManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package uz.alex2276564.leverlock.utils;

import lombok.Getter;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.Plugin;
import uz.alex2276564.leverlock.LeverLock;

import java.time.Duration;

public class ConfigManager {
private static FileConfiguration config;
@Getter
private static Duration cooldownDuration;
@Getter
private static long cleanupInterval;
@Getter
private static String cooldownMessage;

public static void reload() {
Plugin plugin = LeverLock.getInstance();

plugin.saveDefaultConfig();
plugin.reloadConfig();
config = plugin.getConfig();
loadConfig();
}

public static void loadConfig() {
cooldownDuration = Duration.ofSeconds(config.getLong("cooldown.duration", 1));
cleanupInterval = config.getLong("cleanup.interval", 300) * 20; // convert to ticks
cooldownMessage = config.getString("message.cooldown", "§cYou are interacting with the lever too quickly!");
}

private ConfigManager() {
throw new IllegalStateException("Utility class");
}
}

0 comments on commit e399dc8

Please sign in to comment.