Skip to content

Commit

Permalink
Refined logic improvements (#1)
Browse files Browse the repository at this point in the history
* Refine logic and add more configuration options
* Thanks to Pugzy for the new logic
* Ensure multiple matches can be supported

Signed-off-by: applenick <applenick@users.noreply.github.com>
  • Loading branch information
applenick authored Mar 9, 2022
1 parent 92afdd3 commit 903547d
Show file tree
Hide file tree
Showing 8 changed files with 261 additions and 122 deletions.
25 changes: 19 additions & 6 deletions src/main/java/tc/oc/occ/idly/Idly.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,45 @@
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;
import tc.oc.occ.idly.api.BaseIdlyAPI;
import tc.oc.occ.idly.api.IdlyAPI;

public class Idly extends JavaPlugin {

private static Idly plugin;
private BukkitAudiences adventure;
private BukkitCommandManager commands;
private IdlyConfig config;
private IdlyManager manager;
private IdlyAPI api;

@Override
public void onEnable() {
plugin = this;
this.saveDefaultConfig();
this.reloadConfig();
this.config = new IdlyConfig(this.getConfig());
this.commands = new BukkitCommandManager(this);
this.adventure = BukkitAudiences.create(this);
this.api = new BaseIdlyAPI();
this.manager = new IdlyManager(this);

this.commands.registerDependency(IdlyConfig.class, config);
this.commands.registerCommand(new IdlyCommand());

this.getServer().getPluginManager().registerEvents(new IdlyListener(manager), this);
this.getServer().getPluginManager().registerEvents(new IdlyListener(manager, config), this);
}

public static Idly get() {
return plugin;
}

public void setAPI(IdlyAPI api) {
this.api = api;
}

public IdlyAPI getAPI() {
return api;
}

public IdlyConfig getIdlyConfig() {
Expand All @@ -35,9 +53,4 @@ public IdlyConfig getIdlyConfig() {
public Audience getViewer(CommandSender sender) {
return adventure.sender(sender);
}

// TODO: command to view last movement for players
// TODO: ignore permission bypass
// TODO: eee

}
75 changes: 67 additions & 8 deletions src/main/java/tc/oc/occ/idly/IdlyConfig.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package tc.oc.occ.idly;

import java.time.Duration;
import org.bukkit.ChatColor;
import org.bukkit.configuration.Configuration;

public class IdlyConfig {

private boolean enabled;
private int afkDelay;
private int kickDelay;
private boolean kickMode;
private int participantDelay;
private int observerDelay;
private int warningDuration;
private int warningFrequency;
private boolean requireMatchRunning;
private boolean preciseMovement;
private boolean movementCheck;
private boolean chatCheck;
private String kickMessage;

public IdlyConfig(Configuration config) {
reload(config);
Expand All @@ -16,17 +26,66 @@ public boolean isEnabled() {
return enabled;
}

public int getAfkDelay() {
return afkDelay;
public boolean isKickMode() {
return kickMode;
}

public int getKickDelay() {
return kickDelay;
public int getParticipantDelay() {
return participantDelay;
}

public int getObserverDelay() {
return observerDelay;
}

public int getWarningDuration() {
return warningDuration;
}

public int getWarningFrequency() {
return warningFrequency;
}

public boolean isRequireMatchRunning() {
return requireMatchRunning;
}

public boolean isPreciseMovement() {
return preciseMovement;
}

public boolean isMovementCheck() {
return movementCheck;
}

public boolean isChatCheck() {
return chatCheck;
}

public String getKickMessage() {
return ChatColor.translateAlternateColorCodes('&', kickMessage);
}

public void reload(Configuration config) {
this.enabled = config.getBoolean("enabled");
this.afkDelay = config.getInt("afk-delay");
this.kickDelay = config.getInt("kick-delay");
this.kickMode = config.getBoolean("kick-mode");
this.participantDelay = config.getInt("participant-delay");
this.observerDelay = config.getInt("observer-delay");
this.warningDuration = config.getInt("warning-duration");
this.warningFrequency = config.getInt("warning-frequency");
this.requireMatchRunning = config.getBoolean("require-match-running");
this.preciseMovement = config.getBoolean("precise-movement");
this.movementCheck = config.getBoolean("checks.movement");
this.chatCheck = config.getBoolean("checks.chat");
this.kickMessage = config.getString("kick-message");

this.participantDelay = convertSettingTime(participantDelay);
this.observerDelay = convertSettingTime(observerDelay);
this.warningFrequency = convertSettingTime(warningFrequency);
this.warningDuration = convertSettingTime(warningDuration);
}

private int convertSettingTime(int value) {
return (int) Duration.ofSeconds(value).toMillis() / 50;
}
}
34 changes: 26 additions & 8 deletions src/main/java/tc/oc/occ/idly/IdlyListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,42 @@
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import tc.oc.pgm.api.match.event.MatchPhaseChangeEvent;

public class IdlyListener implements Listener {

private IdlyConfig config;
private IdlyManager manager;

public IdlyListener(IdlyManager plugin) {
this.manager = plugin;
public IdlyListener(IdlyManager manager, IdlyConfig config) {
this.manager = manager;
this.config = config;
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerMoveEvent(PlayerMoveEvent event) {
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerJoin(PlayerJoinEvent event) {
this.manager.logMovement(event.getPlayer());
}

@EventHandler
public void onMatchStatusChange(MatchPhaseChangeEvent event) {
this.manager.reset();
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerChatEvent(AsyncPlayerChatEvent event) {
if (config.isChatCheck()) {
this.manager.logMovement(event.getPlayer());
}
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerMoveEvent(PlayerMoveEvent event) {
if (config.isMovementCheck()) {
boolean wasPitchYawMoved =
event.getFrom().getYaw() != event.getTo().getYaw()
|| event.getFrom().getPitch() != event.getTo().getPitch();

if (config.isPreciseMovement() && !wasPitchYawMoved) return;

this.manager.logMovement(event.getPlayer());
}
}
}
Loading

0 comments on commit 903547d

Please sign in to comment.