Skip to content
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.eternalcode.core.feature.jail;

import org.bukkit.Location;
import org.jetbrains.annotations.Nullable;

import java.time.Duration;
import java.time.Instant;
import java.util.UUID;
Expand All @@ -10,12 +13,19 @@ public class JailedPlayer {
private final Instant detainedAt;
private final Duration prisonTime;
private final String detainedBy;
@Nullable
private final Location lastLocation;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Location -> Position, We don't keep the location because it creates memory leaks and has a lot of world dependencies

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok but I think I will be obliged to use another serializer for Position class in database?


public JailedPlayer(UUID player, Instant detainedAt, Duration prisonTime, String detainedBy) {
this(player, detainedAt, prisonTime, detainedBy, null);
}

public JailedPlayer(UUID player, Instant detainedAt, Duration prisonTime, String lockedUpBy) {
public JailedPlayer(UUID player, Instant detainedAt, Duration prisonTime, String detainedBy, @Nullable Location lastLocation) {
this.player = player;
this.detainedAt = detainedAt;
this.prisonTime = prisonTime;
this.detainedBy = lockedUpBy;
this.detainedBy = detainedBy;
this.lastLocation = lastLocation;
}

public UUID getPlayerUniqueId() {
Expand All @@ -34,6 +44,11 @@ public Duration getPrisonTime() {
return this.prisonTime;
}

@Nullable
public Location getLastLocation() {
return this.lastLocation;
}

public boolean isPrisonExpired() {
return this.detainedAt.plus(this.prisonTime).isBefore(Instant.now());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.eternalcode.core.configuration.migrations;

import static eu.okaeri.configs.migrate.ConfigMigrationDsl.move;
import eu.okaeri.configs.migrate.builtin.NamedMigration;

class Migration_0009_Rename_allowed_to_restricted_jail_commands extends NamedMigration {

Migration_0009_Rename_allowed_to_restricted_jail_commands() {
super("Rename allowed to restricted jail commands",
move("jail.allowedCommands", "jail.restrictedCommands")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a jak to działa skoro mamy jailSection a nie jail

);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.eternalcode.core.configuration.migrations;

import eu.okaeri.configs.migrate.builtin.NamedMigration;

import static eu.okaeri.configs.migrate.ConfigMigrationDsl.move;

public class Migration_0010_Move_jail_to_dedicated_section extends NamedMigration {

Migration_0010_Move_jail_to_dedicated_section() {
super(
"Move jail to dedicated section",

move("jailSection.jailLocationSet", "jail.locationSet"),
move("jailSection.jailLocationRemove", "jail.locationRemove"),
move("jailSection.jailLocationNotSet", "jail.locationNotSet"),
move("jailSection.jailLocationOverride", "jail.locationOverride"),

move("jailSection.jailDetainBroadcast", "jail.detainBroadcast"),
move("jailSection.jailDetainPrivate", "jail.detainPrivate"),
move("jailSection.jailDetainCountdown", "jail.detainCountdown"),
move("jailSection.jailDetainOverride", "jail.detainOverride"),
move("jailSection.jailDetainAdmin", "jail.detainAdmin"),

move("jailSection.jailReleaseBroadcast", "jail.releaseBroadcast"),
move("jailSection.jailReleasePrivate", "jail.releasePrivate"),
move("jailSection.jailReleaseAll", "jail.releaseAll"),
move("jailSection.jailReleaseNoPlayers", "jail.releaseNoPlayers"),
move("jailSection.jailIsNotPrisoner", "jail.isNotPrisoner"),

move("jailSection.jailListHeader", "jail.listHeader"),
move("jailSection.jailListEmpty", "jail.listEmpty"),
move("jailSection.jailListPlayerEntry", "jail.listPlayerEntry"),

move("jailSection.jailCannotUseCommand", "jail.cannotUseCommand")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public class Migrations {
new Migration_0003_Move_tprp_to_dedicated_section(),
new Migration_0006_Move_alert_to_broadcast_section(),
new Migration_0007_Move_clear_to_dedicated_section(),
new Migration_0008_Move_repair_to_dedicated_section()
new Migration_0008_Move_repair_to_dedicated_section(),
new Migration_0009_Rename_allowed_to_restricted_jail_commands(),
};

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.eternalcode.core.feature.jail;

import static com.eternalcode.core.feature.jail.JailPermissionConstant.JAIL_BYPASS_PERMISSION;
import static com.eternalcode.core.feature.jail.JailPermissionConstant.JAIL_DETAIN_PERMISSION;
import static com.eternalcode.core.feature.jail.JailPermissionConstant.JAIL_LIST_PERMISSION;
import static com.eternalcode.core.feature.jail.JailPermissionConstant.JAIL_RELEASE_PERMISSION;
import static com.eternalcode.core.feature.jail.JailPermissionConstant.JAIL_SETUP_PERMISSION;

import com.eternalcode.annotations.scan.command.DescriptionDocs;
import com.eternalcode.annotations.scan.permission.PermissionDocs;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.notice.NoticeService;
import com.eternalcode.core.util.DurationUtil;
Expand All @@ -11,21 +16,16 @@
import dev.rollczi.litecommands.annotations.context.Context;
import dev.rollczi.litecommands.annotations.execute.Execute;
import dev.rollczi.litecommands.annotations.permission.Permission;
import java.time.Duration;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.entity.Player;

import java.time.Duration;

@Command(name = "jail")
@PermissionDocs(
name = "Jail Bypass",
permission = JailCommand.JAIL_BYPASS,
description = "Permission allows to bypass jail punishment"
)
@Permission(JAIL_BYPASS_PERMISSION)
class JailCommand {

static final String JAIL_BYPASS = "eternalcore.jail.bypass";

private final JailService jailService;
private final NoticeService noticeService;
private final JailSettings jailSettings;
Expand All @@ -41,7 +41,7 @@ class JailCommand {

@Async
@Execute(name = "setup")
@Permission("eternalcore.jail.setup")
@Permission(JAIL_SETUP_PERMISSION)
@DescriptionDocs(description = "Define jail spawn area")
void executeJailSetup(@Context Player player) {
Location location = player.getLocation();
Expand All @@ -51,15 +51,15 @@ void executeJailSetup(@Context Player player) {

this.noticeService.create()
.notice(translation -> (isLastJailSet
? translation.jailSection().jailLocationOverride()
: translation.jailSection().jailLocationSet()))
? translation.jail().locationOverride()
: translation.jail().locationSet()))
.player(player.getUniqueId())
.send();
}

@Async
@Execute(name = "setup")
@Permission("eternalcore.jail.setup")
@Permission(JAIL_SETUP_PERMISSION)
@DescriptionDocs(description = "Define jail spawn area", arguments = "<location>")
void executeJailSetup(@Context Player player, @Arg Location location) {
boolean isLastJailSet = this.jailService.getJailAreaLocation().isPresent();
Expand All @@ -69,15 +69,15 @@ void executeJailSetup(@Context Player player, @Arg Location location) {

this.noticeService.create()
.notice(translation -> (isLastJailSet
? translation.jailSection().jailLocationOverride()
: translation.jailSection().jailLocationSet()))
? translation.jail().locationOverride()
: translation.jail().locationSet()))
.player(player.getUniqueId())
.send();
}

@Async
@Execute(name = "remove")
@Permission("eternalcore.jail.setup")
@Permission(JAIL_SETUP_PERMISSION)
@DescriptionDocs(description = "Remove jail spawn area")
void executeJailRemove(@Context Player player) {
if (this.isPrisonAvailable(player)) {
Expand All @@ -87,36 +87,36 @@ void executeJailRemove(@Context Player player) {
this.jailService.removeJailArea();

this.noticeService.create()
.notice(translation -> translation.jailSection().jailLocationRemove())
.notice(translation -> translation.jail().locationRemoved())
.player(player.getUniqueId())
.send();
}

@Execute(name = "detain")
@Permission("eternalcore.jail.detain")
@Permission(JAIL_DETAIN_PERMISSION)
@DescriptionDocs(description = "Detain self")
void executeJailDetainSelf(@Context Player player) {
this.executeJailDetainForTime(player, player, this.jailSettings.defaultJailDuration());
}

@Execute(name = "detain")
@Permission("eternalcore.jail.detain")
@Permission(JAIL_DETAIN_PERMISSION)
@DescriptionDocs(description = "Detain a player", arguments = "<player>")
void executeJailDetain(@Context Player player, @Arg Player target) {
this.executeJailDetainForTime(player, target, this.jailSettings.defaultJailDuration());
}

@Execute(name = "detain")
@Permission("eternalcore.jail.detain")
@Permission(JAIL_DETAIN_PERMISSION)
@DescriptionDocs(description = "Detain a player for some time", arguments = "<player> <time>")
void executeJailDetainForTime(@Context Player player, @Arg Player target, @Arg Duration duration) {
if (this.isPrisonAvailable(player)) {
return;
}

if (target.hasPermission(JAIL_BYPASS)) {
if (target.hasPermission(JAIL_BYPASS_PERMISSION)) {
this.noticeService.create()
.notice(translation -> translation.jailSection().jailDetainAdmin())
.notice(translation -> translation.jail().detainAdmin())
.placeholder("{PLAYER}", target.getName())
.player(player.getUniqueId())
.send();
Expand All @@ -127,7 +127,7 @@ void executeJailDetainForTime(@Context Player player, @Arg Player target, @Arg D

if (isPlayerJailed) {
this.noticeService.create()
.notice(translation -> translation.jailSection().jailDetainOverride())
.notice(translation -> translation.jail().detainOverride())
.placeholder("{PLAYER}", target.getName())
.player(player.getUniqueId())
.send();
Expand All @@ -136,31 +136,31 @@ void executeJailDetainForTime(@Context Player player, @Arg Player target, @Arg D
this.jailService.detainPlayer(target, player, duration);

this.noticeService.create()
.notice(translation -> translation.jailSection().jailDetainBroadcast())
.notice(translation -> translation.jail().detainBroadcast())
.placeholder("{PLAYER}", target.getName())
.all()
.send();

this.noticeService.create()
.notice(translation -> translation.jailSection().jailDetainPrivate())
.notice(translation -> translation.jail().detainPrivate())
.player(target.getUniqueId())
.send();
}

@Execute(name = "release")
@Permission("eternalcore.jail.release")
@Permission(JAIL_RELEASE_PERMISSION)
@DescriptionDocs(description = "Release self from jail")
void executeJailReleaseSelf(@Context Player player) {
this.executeJailRelease(player, player);
}

@Execute(name = "release")
@Permission("eternalcore.jail.release")
@Permission(JAIL_RELEASE_PERMISSION)
@DescriptionDocs(description = "Release a player from jail", arguments = "<player>")
void executeJailRelease(@Context Player player, @Arg Player target) {
if (!this.jailService.isPlayerJailed(target.getUniqueId())) {
this.noticeService.create()
.notice(translation -> translation.jailSection().jailIsNotPrisoner())
.notice(translation -> translation.jail().isNotPrisoner())
.placeholder("{PLAYER}", target.getName())
.player(player.getUniqueId())
.send();
Expand All @@ -170,24 +170,24 @@ void executeJailRelease(@Context Player player, @Arg Player target) {
this.jailService.releasePlayer(target);

this.noticeService.create()
.notice(translation -> translation.jailSection().jailReleasePrivate())
.notice(translation -> translation.jail().releasePrivate())
.player(target.getUniqueId())
.send();

this.noticeService.create()
.notice(translation -> translation.jailSection().jailReleaseBroadcast())
.notice(translation -> translation.jail().releaseBroadcast())
.placeholder("{PLAYER}", target.getName())
.all()
.send();
}

@Execute(name = "release -all", aliases = { "release *" })
@Permission("eternalcore.jail.release")
@Permission(JAIL_RELEASE_PERMISSION)
@DescriptionDocs(description = "Release all players from jail")
void executeJailReleaseAll(@Context Player player) {
if (this.jailService.getJailedPlayers().isEmpty()) {
this.noticeService.create()
.notice(translation -> translation.jailSection().jailReleaseNoPlayers())
.notice(translation -> translation.jail().releaseNoPlayers())
.player(player.getUniqueId())
.send();
return;
Expand All @@ -196,31 +196,31 @@ void executeJailReleaseAll(@Context Player player) {
this.jailService.releaseAllPlayers();

this.noticeService.create()
.notice(translation -> translation.jailSection().jailReleaseAll())
.notice(translation -> translation.jail().releaseAll())
.all()
.send();
}

@Execute(name = "list")
@Permission("eternalcore.jail.list")
@Permission(JAIL_LIST_PERMISSION)
@DescriptionDocs(description = "List all jailed players")
void executeJailList(@Context Player player) {
if (this.jailService.getJailedPlayers().isEmpty()) {
this.noticeService.create()
.notice(translation -> translation.jailSection().jailListEmpty())
.notice(translation -> translation.jail().listEmpty())
.player(player.getUniqueId())
.send();
return;
}

this.noticeService.create()
.notice(translation -> translation.jailSection().jailListHeader())
.notice(translation -> translation.jail().listHeader())
.player(player.getUniqueId())
.send();

for (JailedPlayer jailedPlayer : this.jailService.getJailedPlayers()) {
this.noticeService.create()
.notice(translation -> translation.jailSection().jailListPlayerEntry())
.notice(translation -> translation.jail().listPlayerEntry())
.placeholder("{PLAYER}", this.server.getOfflinePlayer(jailedPlayer.getPlayerUniqueId()).getName())
.placeholder("{REMAINING_TIME}", DurationUtil.format(jailedPlayer.getRemainingTime(), true))
.placeholder("{DETAINED_BY}", jailedPlayer.getDetainedBy())
Expand All @@ -232,7 +232,7 @@ void executeJailList(@Context Player player) {
private boolean isPrisonAvailable(Player player) {
if (this.jailService.getJailAreaLocation().isEmpty()) {
this.noticeService.create()
.notice(translation -> translation.jailSection().jailLocationNotSet())
.notice(translation -> translation.jail().locationNotSet())
.player(player.getUniqueId())
.send();
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.eternalcode.core.feature.jail;

public enum JailCommandRestrictionType {
WHITELIST,
BLACKLIST
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public class JailConfig extends OkaeriConfig implements JailSettings {
@Comment("# Default jail duration, set if no duration is specified")
public Duration defaultJailDuration = Duration.ofMinutes(30);

@Comment("# Allowed commands in jail")
public Set<String> allowedCommands = Set.of("help", "msg", "r", "tell", "me", "helpop");
@Comment("# Command restriction type, either WHITELIST or BLACKLIST")
public JailCommandRestrictionType restrictionType = JailCommandRestrictionType.WHITELIST;

@Comment("# Restricted commands for jailed players")
public Set<String> restrictedCommands = Set.of("help", "msg", "r", "tell", "me", "helpop");
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,20 @@ public void onPlayerPreCommand(PlayerCommandPreprocessEvent event) {
}

String command = event.getMessage().split(" ")[0].substring(1);
Set<String> restrictedCommands = this.jailSettings.restrictedCommands();
JailCommandRestrictionType restrictionType = this.jailSettings.restrictionType();

if (this.jailSettings.allowedCommands().contains(command)) {
boolean shouldBlockCommand = switch (restrictionType) {
case WHITELIST -> !restrictedCommands.contains(command);
case BLACKLIST -> restrictedCommands.contains(command);
};

if (!shouldBlockCommand) {
return;
}

this.noticeService.create()
.notice(translation -> translation.jailSection().jailCannotUseCommand())
.notice(translation -> translation.jail().cannotUseCommand())
.player(player.getUniqueId())
.send();

Expand Down
Loading