Code Patterns

Copy-paste examples for common plugin tasks. Commands, events, ECS, GUI, and more.

← Back to Patterns
moderation

Mute System

Timed mute system with JSON persistence. Automatically cleans up expired mutes.

Example Code

java
public class MuteTracker extends BlockingDiskFile {
    private List<Mute> muteTracker = new ArrayList<>();

    public MuteTracker() {
        super(Path.of("MyPlugin/Mute.json"));
    }

    public void addMute(Mute mute) {
        this.fileLock.writeLock().lock();
        this.muteTracker.removeIf(m -> m.target().equals(mute.target()));
        this.muteTracker.add(mute);
        this.fileLock.writeLock().unlock();
        this.syncSave();
    }

    public List<Mute> getMutes() {
        // Auto-cleanup expired mutes
        if (this.muteTracker.removeIf(m -> m.until().isBefore(Instant.now()))) {
            this.syncSave();
        }
        return muteTracker;
    }

    public boolean isMuted(UUID uuid) {
        var playerMute = getMutes().stream()
            .filter(m -> m.target().equals(uuid))
            .findFirst().orElse(null);
        return playerMute != null && playerMute.until().isAfter(Instant.now());
    }

    public record Mute(UUID target, UUID mutedBy, Instant until, String reason) {}
}