Added Outlaw change timeout
This commit is contained in:
parent
a3fe1bf737
commit
71c413673d
@ -0,0 +1,7 @@
|
||||
package eu.mhsl.craftattack.spawn.appliances.outlawed;
|
||||
|
||||
public class OutlawChangeNotPermitted extends Exception {
|
||||
public OutlawChangeNotPermitted(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
package eu.mhsl.craftattack.spawn.appliances.outlawed;
|
||||
|
||||
public class OutlawForcedException extends Exception {
|
||||
public OutlawForcedException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
@ -15,11 +15,11 @@ import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.WeakHashMap;
|
||||
import java.util.*;
|
||||
|
||||
public class Outlawed extends Appliance {
|
||||
public final int timeoutInMs = 1000*60*60*6;
|
||||
private final Map<UUID, Long> timeouts = new HashMap<>();
|
||||
public enum Status {
|
||||
DISABLED,
|
||||
VOLUNTARILY,
|
||||
@ -28,6 +28,7 @@ public class Outlawed extends Appliance {
|
||||
|
||||
private final Map<Player, Status> playerStatusMap = new WeakHashMap<>();
|
||||
private final String voluntarilyEntry = "voluntarily";
|
||||
|
||||
public Outlawed() {
|
||||
super("outlawed");
|
||||
Bukkit.getScheduler().runTaskTimerAsynchronously(
|
||||
@ -48,12 +49,17 @@ public class Outlawed extends Appliance {
|
||||
);
|
||||
}
|
||||
|
||||
public void switchLawStatus(Player player) throws OutlawForcedException {
|
||||
public void switchLawStatus(Player player) throws OutlawChangeNotPermitted {
|
||||
if(getLawStatus(player).equals(Status.FORCED)) {
|
||||
throw new OutlawForcedException("Dein Vogelfreistatus wurde als Strafe auferlegt und kann daher nicht verändert werden.");
|
||||
throw new OutlawChangeNotPermitted("Dein Vogelfreistatus wurde als Strafe auferlegt und kann daher nicht verändert werden.");
|
||||
}
|
||||
|
||||
if(isTimeout(player)) {
|
||||
throw new OutlawChangeNotPermitted("Du kannst deinen Vogelfreistatus nicht so schnell wechseln. Bitte warte einige Stunden bevor du umschaltest!");
|
||||
}
|
||||
|
||||
setLawStatus(player, isOutlawed(player) ? Status.DISABLED : Status.VOLUNTARILY);
|
||||
setTimeout(player);
|
||||
}
|
||||
|
||||
public void updateForcedStatus(Player player, boolean forced) {
|
||||
@ -61,14 +67,13 @@ public class Outlawed extends Appliance {
|
||||
}
|
||||
|
||||
public Status getLawStatus(Player player) {
|
||||
Status status = playerStatusMap.computeIfAbsent(player, p -> {
|
||||
return playerStatusMap.computeIfAbsent(player, p -> {
|
||||
if(localConfig().getStringList(voluntarilyEntry).contains(p.getUniqueId().toString())) return Status.VOLUNTARILY;
|
||||
return Status.DISABLED;
|
||||
});
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setLawStatus(Player player, Status status) {
|
||||
private void setLawStatus(Player player, Status status) {
|
||||
playerStatusMap.put(player, status);
|
||||
Main.instance().getAppliance(DisplayName.class).update(player);
|
||||
|
||||
@ -87,6 +92,14 @@ public class Outlawed extends Appliance {
|
||||
return getLawStatus(player) != Status.DISABLED;
|
||||
}
|
||||
|
||||
private boolean isTimeout(Player player) {
|
||||
return timeouts.get(player.getUniqueId()) < System.currentTimeMillis() - timeoutInMs;
|
||||
}
|
||||
|
||||
private void setTimeout(Player player) {
|
||||
timeouts.put(player.getUniqueId(), System.currentTimeMillis());
|
||||
}
|
||||
|
||||
public Component getStatusDescription(Status status) {
|
||||
return switch (status) {
|
||||
case DISABLED -> Component.text("Vogelfreistatus inaktiv: ", NamedTextColor.GREEN)
|
||||
|
@ -20,7 +20,7 @@ public class OutlawedCommand extends ApplianceCommand.PlayerChecked<Outlawed> {
|
||||
getAppliance()
|
||||
.getStatusDescription(getAppliance().getLawStatus(getPlayer()))
|
||||
);
|
||||
} catch (OutlawForcedException e) {
|
||||
} catch (OutlawChangeNotPermitted e) {
|
||||
sender.sendMessage(Component.text(e.getMessage(), NamedTextColor.RED));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user