140 lines
4.0 KiB
Java
140 lines
4.0 KiB
Java
package com.rpserver.jailplugin.managers;
|
|
|
|
import com.rpserver.jailplugin.JailPlugin;
|
|
import org.bukkit.configuration.file.FileConfiguration;
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.potion.PotionEffect;
|
|
import org.bukkit.potion.PotionEffectType;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.util.HashSet;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
import java.util.UUID;
|
|
import java.util.stream.Collectors;
|
|
|
|
public class CuffManager {
|
|
|
|
private final JailPlugin plugin;
|
|
private final Set<UUID> cuffedPlayers;
|
|
private File dataFile;
|
|
private FileConfiguration dataConfig;
|
|
|
|
public CuffManager(JailPlugin plugin) {
|
|
this.plugin = plugin;
|
|
this.cuffedPlayers = new HashSet<>();
|
|
loadData();
|
|
}
|
|
|
|
public boolean isCuffed(Player player) {
|
|
return cuffedPlayers.contains(player.getUniqueId());
|
|
}
|
|
|
|
public boolean isCuffed(UUID uuid) {
|
|
return cuffedPlayers.contains(uuid);
|
|
}
|
|
|
|
public void cuff(Player player) {
|
|
cuffedPlayers.add(player.getUniqueId());
|
|
applySlowEffect(player);
|
|
saveData();
|
|
}
|
|
|
|
public void uncuff(Player player) {
|
|
cuffedPlayers.remove(player.getUniqueId());
|
|
removeSlowEffect(player);
|
|
saveData();
|
|
|
|
plugin.getLeadManager().unlead(player);
|
|
}
|
|
|
|
private void applySlowEffect(Player player) {
|
|
double speedModifier = plugin.getConfig().getDouble("cuff.speed-modifier", 0.5);
|
|
int amplifier = (int) ((1.0 - speedModifier) * 4); // 0.5 speed = Slowness 2
|
|
|
|
player.addPotionEffect(new PotionEffect(
|
|
PotionEffectType.SLOWNESS,
|
|
Integer.MAX_VALUE,
|
|
amplifier,
|
|
false,
|
|
false,
|
|
true
|
|
));
|
|
}
|
|
|
|
private void removeSlowEffect(Player player) {
|
|
player.removePotionEffect(PotionEffectType.SLOWNESS);
|
|
}
|
|
|
|
public boolean isCommandBlocked(String command) {
|
|
if (!plugin.getConfig().getBoolean("cuff.block-commands", true)) {
|
|
return false;
|
|
}
|
|
|
|
String cmd = command.toLowerCase().split(" ")[0];
|
|
for (String allowed : plugin.getConfig().getStringList("cuff.allowed-commands")) {
|
|
if (cmd.equalsIgnoreCase(allowed) || cmd.equalsIgnoreCase(allowed.substring(1))) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public double getCuffDistance() {
|
|
return plugin.getConfig().getDouble("cuff.distance", 5.0);
|
|
}
|
|
|
|
public Set<UUID> getCuffedPlayers() {
|
|
return new HashSet<>(cuffedPlayers);
|
|
}
|
|
|
|
public void applyEffectOnJoin(Player player) {
|
|
if (isCuffed(player)) {
|
|
applySlowEffect(player);
|
|
}
|
|
}
|
|
|
|
private void loadData() {
|
|
dataFile = new File(plugin.getDataFolder(), "cuffed.yml");
|
|
|
|
if (!dataFile.exists()) {
|
|
try {
|
|
plugin.getDataFolder().mkdirs();
|
|
dataFile.createNewFile();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
dataConfig = YamlConfiguration.loadConfiguration(dataFile);
|
|
|
|
if (dataConfig.contains("cuffed-players")) {
|
|
List<String> uuidStrings = dataConfig.getStringList("cuffed-players");
|
|
for (String uuidString : uuidStrings) {
|
|
try {
|
|
UUID uuid = UUID.fromString(uuidString);
|
|
cuffedPlayers.add(uuid);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void saveData() {
|
|
List<String> uuidStrings = cuffedPlayers.stream()
|
|
.map(UUID::toString)
|
|
.collect(Collectors.toList());
|
|
|
|
dataConfig.set("cuffed-players", uuidStrings);
|
|
|
|
try {
|
|
dataConfig.save(dataFile);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|