mirror of
https://github.com/CarmJos/TimeForFlight.git
synced 2026-06-04 15:18:16 +08:00
项目完成,首次提交
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
package cc.carm.plugin.timeforflight;
|
||||
|
||||
import cc.carm.plugin.timeforflight.commands.TimeForFlightCommand;
|
||||
import cc.carm.plugin.timeforflight.commands.ToggleFlyCommand;
|
||||
import cc.carm.plugin.timeforflight.managers.ConfigManager;
|
||||
import cc.carm.plugin.timeforflight.managers.DataManager;
|
||||
import cc.carm.plugin.timeforflight.listeners.PlayerListener;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class Main extends JavaPlugin {
|
||||
|
||||
private static Main instance;
|
||||
|
||||
public static Main getInstance() {
|
||||
return Main.instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
instance = this;
|
||||
|
||||
logInfo("加载配置文件中");
|
||||
ConfigManager.loadConfig();
|
||||
|
||||
logInfo("注册指令");
|
||||
Main.getInstance().getCommand("TimeForFlight").setExecutor(new TimeForFlightCommand());
|
||||
Main.getInstance().getCommand("ToggleFly").setExecutor(new ToggleFlyCommand());
|
||||
|
||||
logInfo("注册监听器");
|
||||
Bukkit.getPluginManager().registerEvents(new PlayerListener(),this);
|
||||
|
||||
logInfo("初始化数据管理");
|
||||
DataManager.init();
|
||||
}
|
||||
|
||||
public static void logInfo(String message) {
|
||||
Main.getInstance().getLogger().log(Level.INFO, message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
package cc.carm.plugin.timeforflight.commands;
|
||||
|
||||
import cc.carm.plugin.timeforflight.models.UserData;
|
||||
import cc.carm.plugin.timeforflight.utils.MessageParser;
|
||||
import cc.carm.plugin.timeforflight.enums.Permissions;
|
||||
import cc.carm.plugin.timeforflight.managers.DataManager;
|
||||
import cc.carm.plugin.timeforflight.utils.TimeFormat;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabCompleter;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.util.StringUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 管理员管理、查看玩家飞行时间的指令
|
||||
*/
|
||||
public class TimeForFlightCommand implements CommandExecutor, TabCompleter {
|
||||
|
||||
public static boolean help(CommandSender sender) {
|
||||
sendMessage(sender, "&6&l限时飞行 &7管理指令帮助");
|
||||
sendMessage(sender, "&8- &fset <玩家> <秒数> &7设置玩家的飞行时间 ");
|
||||
sendMessage(sender, "&8- &fadd <玩家> <秒数> &7添加玩家的飞行时间 ");
|
||||
sendMessage(sender, "&8- &fremove <玩家> <秒数> &7移除玩家的飞行时间 ");
|
||||
sendMessage(sender, "&8- &fget <玩家> &7查看玩家的飞行时间 ");
|
||||
sendMessage(sender, "&8- &fclear <玩家> &7清空玩家的飞行时间 ");
|
||||
sendMessage(sender, "&8- &fhelp &7查看此帮助 ");
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean noPerm(CommandSender sender) {
|
||||
sender.sendMessage(MessageParser.parseColor("&c抱歉!&7但您没有这么做的权限。"));
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void sendMessage(CommandSender sender, String message) {
|
||||
sender.sendMessage(MessageParser.parseColor(message));
|
||||
}
|
||||
|
||||
public static void sendMessage(Player player, String message) {
|
||||
player.sendMessage(MessageParser.parseColor(message));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String s, String[] args) {
|
||||
if (!sender.hasPermission(Permissions.ADMIN.toString())) return noPerm(sender);
|
||||
if (args.length > 3 || args.length < 2) return help(sender);
|
||||
String aim = args[0].toLowerCase();
|
||||
if (args.length == 2) {
|
||||
if (aim.equalsIgnoreCase("clear")) {
|
||||
OfflinePlayer target = Bukkit.getOfflinePlayer(args[1]);
|
||||
UserData targetData = DataManager.getData(target.getUniqueId());
|
||||
if (targetData.isFileLoaded() && targetData.getRemainTime() > 0) {
|
||||
targetData.setTime(0);
|
||||
}
|
||||
DataManager.unloadData(target.getUniqueId());
|
||||
sendMessage(sender, "&f已清空玩家 " + target.getName() + " 的飞行时间。");
|
||||
return true;
|
||||
} else if (aim.equalsIgnoreCase("get")) {
|
||||
OfflinePlayer target = Bukkit.getOfflinePlayer(args[1]);
|
||||
UserData targetData = DataManager.getData(target.getUniqueId());
|
||||
if (targetData.isFileLoaded() && targetData.getRemainTime() > 0) {
|
||||
sendMessage(sender, "&f玩家" + target.getName() + " 的剩余飞行时间为 " + TimeFormat.getTimeString(targetData.getRemainTime()) + "。");
|
||||
} else {
|
||||
sendMessage(sender, "&f玩家" + target.getName() + " 的剩余飞行时间为 " + TimeFormat.getTimeString(0) + "。");
|
||||
}
|
||||
DataManager.unloadData(target.getUniqueId());
|
||||
|
||||
return true;
|
||||
} else {
|
||||
return help(sender);
|
||||
}
|
||||
|
||||
} else if (args.length == 3) {
|
||||
if (aim.equalsIgnoreCase("add") || aim.equalsIgnoreCase("remove") || aim.equalsIgnoreCase("set")) {
|
||||
int value;
|
||||
try {
|
||||
value = Integer.parseInt(args[2]);
|
||||
} catch (Exception ignore) {
|
||||
sendMessage(sender, "请填写正确的数字。");
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
OfflinePlayer target = Bukkit.getOfflinePlayer(args[1]);
|
||||
UserData targetData = DataManager.getData(target.getUniqueId());
|
||||
|
||||
if (aim.equalsIgnoreCase("add")) {
|
||||
targetData.addTime(value);
|
||||
sendMessage(sender, "成功为玩家 " + target.getName() + " 添加飞行时间 " + TimeFormat.getTimeString(value) + "。");
|
||||
} else if (aim.equalsIgnoreCase("remove")) {
|
||||
targetData.removeTime(value);
|
||||
sendMessage(sender, "成功为玩家 " + target.getName() + " 移除飞行时间 " + TimeFormat.getTimeString(value) + "。");
|
||||
} else if (aim.equalsIgnoreCase("set")) {
|
||||
targetData.setTime(value);
|
||||
sendMessage(sender, "成功设置玩家 " + target.getName() + " 的飞行时间 " + TimeFormat.getTimeString(value) + "。");
|
||||
}
|
||||
DataManager.unloadData(target.getUniqueId());
|
||||
return true;
|
||||
} else {
|
||||
return help(sender);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
|
||||
if (!sender.hasPermission(Permissions.ADMIN.toString())) {
|
||||
return ImmutableList.of();
|
||||
}
|
||||
switch (args.length) {
|
||||
case 1: {
|
||||
List<String> completions = new ArrayList<>();
|
||||
List<String> strings = new ArrayList<>();
|
||||
strings.add("help");
|
||||
strings.add("set");
|
||||
strings.add("add");
|
||||
strings.add("remove");
|
||||
strings.add("clear");
|
||||
for (String s : strings) {
|
||||
if (StringUtil.startsWithIgnoreCase(s, args[0].toLowerCase())) {
|
||||
completions.add(s);
|
||||
}
|
||||
}
|
||||
return completions;
|
||||
}
|
||||
case 2: {
|
||||
String aim = args[1];
|
||||
if (aim.equalsIgnoreCase("add")
|
||||
|| aim.equalsIgnoreCase("remove")
|
||||
|| aim.equalsIgnoreCase("set")
|
||||
|| aim.equalsIgnoreCase("clear")) {
|
||||
List<String> completions = new ArrayList<>();
|
||||
for (Player pl : Bukkit.getOnlinePlayers()) {
|
||||
if (StringUtil.startsWithIgnoreCase(pl.getName(), args[1].toLowerCase())) {
|
||||
completions.add(pl.getName());
|
||||
}
|
||||
}
|
||||
return completions;
|
||||
}
|
||||
}
|
||||
default:
|
||||
return ImmutableList.of();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
package cc.carm.plugin.timeforflight.commands;
|
||||
|
||||
import cc.carm.plugin.timeforflight.models.UserData;
|
||||
import cc.carm.plugin.timeforflight.utils.MessageParser;
|
||||
import cc.carm.plugin.timeforflight.enums.Permissions;
|
||||
import cc.carm.plugin.timeforflight.managers.DataManager;
|
||||
import cc.carm.plugin.timeforflight.utils.TimeFormat;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
/**
|
||||
* 开关飞行的指令
|
||||
*/
|
||||
public class ToggleFlyCommand implements CommandExecutor {
|
||||
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String s, String[] args) {
|
||||
if (!(sender instanceof Player)) {
|
||||
sender.sendMessage(MessageParser.parseColor("该指令只允许玩家使用。"));
|
||||
sender.sendMessage(MessageParser.parseColor("您可以输入 /togglefly <玩家名> 设置某个玩家的飞行状态。"));
|
||||
return true;
|
||||
}
|
||||
|
||||
Player player = (Player) sender;
|
||||
|
||||
UserData userData = DataManager.getData(player.getUniqueId());
|
||||
|
||||
if (args.length == 0 || !sender.isOp()) {
|
||||
if (player.getAllowFlight()) {
|
||||
// 在玩家开启飞行的状态之下
|
||||
if (player.hasPermission(Permissions.ALLOW_FLIGHT.toString()) || player.hasPermission(Permissions.UNLIMITED.toString())) {
|
||||
userData.stopFly(player);
|
||||
player.sendMessage(MessageParser.parseColor("§f已为您关闭飞行状态,剩余飞行时长 " + userData.getRemainTimeString() + " 。"));
|
||||
return true;
|
||||
} else {
|
||||
sender.sendMessage(MessageParser.parseColor("&c抱歉!&f但您没有使用该指令的权限。"));
|
||||
}
|
||||
} else {
|
||||
//在玩家没有开启飞行的状态下
|
||||
if (player.hasPermission(Permissions.UNLIMITED.toString())) {
|
||||
player.setAllowFlight(true);
|
||||
player.sendMessage(MessageParser.parseColor("&f您现在可以飞行了!"));
|
||||
return true;
|
||||
} else if (player.hasPermission(Permissions.ALLOW_FLIGHT.toString())) {
|
||||
if (userData.canFly()) {
|
||||
userData.startFlyTask(player);
|
||||
player.sendMessage(MessageParser.parseColor("&f您现在可以飞行了!"));
|
||||
player.sendMessage(MessageParser.parseColor("&f剩余飞行时长 &a" + TimeFormat.getTimeString(userData.getRemainTime())));
|
||||
return true;
|
||||
} else {
|
||||
player.sendMessage(MessageParser.parseColor("&c您的飞行时长不足,无法开启飞行。"));
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
player.sendMessage(MessageParser.parseColor("&c抱歉!&f但您没有使用该指令的权限。"));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} else if (args.length == 1) {
|
||||
if (!sender.isOp()) {
|
||||
sender.sendMessage(MessageParser.parseColor("&c抱歉!&f但您没有使用该指令的权限。"));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Player target = Bukkit.getPlayer(args[0]);
|
||||
if (target == null) {
|
||||
sender.sendMessage("玩家 " + args[0] + " 不在线。");
|
||||
return true;
|
||||
}
|
||||
UserData targetData = DataManager.getData(target.getUniqueId());
|
||||
|
||||
if (target.getAllowFlight()) {
|
||||
//在目标玩家正在飞行的状态下。
|
||||
if (target.hasPermission(Permissions.ALLOW_FLIGHT.toString()) || target.hasPermission(Permissions.UNLIMITED.toString())) {
|
||||
userData.stopFly(target);
|
||||
target.sendMessage(MessageParser.parseColor("§f已为您关闭飞行状态,剩余飞行时长 " + userData.getRemainTimeString() + " 。"));
|
||||
return true;
|
||||
} else {
|
||||
sender.sendMessage(MessageParser.parseColor("&c抱歉!&f但您没有使用该指令的权限。"));
|
||||
}
|
||||
} else {
|
||||
//在目标玩家没有飞行的状态下
|
||||
if (target.hasPermission(Permissions.UNLIMITED.toString())) {
|
||||
|
||||
target.setAllowFlight(true);
|
||||
target.sendMessage(MessageParser.parseColor("&f您现在可以飞行了!"));
|
||||
sender.sendMessage(MessageParser.parseColor("已为玩家 " + target.getName() + " 开启飞行。"));
|
||||
return true;
|
||||
} else if (target.hasPermission(Permissions.ALLOW_FLIGHT.toString())) {
|
||||
if (targetData.canFly()) {
|
||||
targetData.startFlyTask(target);
|
||||
target.sendMessage(MessageParser.parseColor("&f您现在可以飞行了!"));
|
||||
target.sendMessage(MessageParser.parseColor("&f剩余飞行时长 &a" + TimeFormat.getTimeString(userData.getRemainTime())));
|
||||
sender.sendMessage(MessageParser.parseColor("已为玩家 " + target.getName() + " 开启飞行。"));
|
||||
|
||||
return true;
|
||||
} else {
|
||||
target.sendMessage(MessageParser.parseColor("&c您的飞行时长不足,无法开启飞行。"));
|
||||
sender.sendMessage(MessageParser.parseColor("玩家 " + target.getName() + " 飞行时间不足。"));
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
sender.sendMessage(MessageParser.parseColor("玩家 " + target.getName() + " 没有飞行权限。"));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package cc.carm.plugin.timeforflight.enums;
|
||||
|
||||
public enum Permissions {
|
||||
UNLIMITED("timeforflight.unlimited"),
|
||||
ALLOW_FLIGHT("timeforflight.allowflight"),
|
||||
GET_TIME("timeforflight.getflighttime"),
|
||||
ADMIN("timeforflight.admin");
|
||||
|
||||
String permissionNode;
|
||||
|
||||
Permissions(String permissionNode) {
|
||||
this.permissionNode = permissionNode;
|
||||
}
|
||||
|
||||
|
||||
public String getPermission() {
|
||||
return permissionNode;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package cc.carm.plugin.timeforflight.listeners;
|
||||
|
||||
import cc.carm.plugin.timeforflight.managers.DataManager;
|
||||
import cc.carm.plugin.timeforflight.models.UserData;
|
||||
import cc.carm.plugin.timeforflight.enums.Permissions;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
public class PlayerListener implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onJoin(PlayerJoinEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
UserData playerCache = DataManager.loadData(player.getUniqueId());
|
||||
if (player.hasPermission(Permissions.GET_TIME.toString())
|
||||
&& !player.hasPermission(Permissions.UNLIMITED.toString())) {
|
||||
playerCache.startGivenTask(player);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@EventHandler
|
||||
public void onQuit(PlayerQuitEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
UserData playerCache = DataManager.getData(player.getUniqueId());
|
||||
|
||||
player.setAllowFlight(false);
|
||||
player.setFlying(false);
|
||||
|
||||
DataManager.unloadData(player.getUniqueId());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package cc.carm.plugin.timeforflight.managers;
|
||||
|
||||
import cc.carm.plugin.timeforflight.utils.MessageParser;
|
||||
import cc.carm.plugin.timeforflight.Main;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class ConfigManager {
|
||||
|
||||
|
||||
private static FileConfiguration configuration;
|
||||
|
||||
public static void loadConfig() {
|
||||
|
||||
Main.getInstance().saveDefaultConfig();
|
||||
Main.getInstance().reloadConfig();
|
||||
|
||||
configuration = Main.getInstance().getConfig();
|
||||
|
||||
}
|
||||
|
||||
public static int getTimeInterval() {
|
||||
return getConfiguration().getInt("settings.interval", 600);
|
||||
}
|
||||
|
||||
public static int getTimeGiven() {
|
||||
return getConfiguration().getInt("settings.giveTime", 60);
|
||||
}
|
||||
|
||||
public static boolean isAlertEnabled() {
|
||||
return getConfiguration().getBoolean("settings.alert.enable", true);
|
||||
}
|
||||
|
||||
/**
|
||||
* # - 变量: %(interval) 间隔时间
|
||||
* # - 变量: %(time) 赠送时间
|
||||
*
|
||||
* @return 通知消息
|
||||
*/
|
||||
public static String getAlertMessage() {
|
||||
return MessageParser.parseColor(Objects.requireNonNull(getConfiguration().getString(
|
||||
"settings.alert.message",
|
||||
"&7您刚刚完成了一次在线 &f%(interval) 秒&7,获增了 &f%(time) 秒&7飞行时间。"
|
||||
)))
|
||||
.replace("%(interval)", Integer.toString(getTimeInterval()))
|
||||
.replace("%(time)", Integer.toString(getTimeGiven()));
|
||||
}
|
||||
|
||||
public static FileConfiguration getConfiguration() {
|
||||
return configuration;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package cc.carm.plugin.timeforflight.managers;
|
||||
|
||||
import cc.carm.plugin.timeforflight.models.UserData;
|
||||
import cc.carm.plugin.timeforflight.Main;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.*;
|
||||
|
||||
public class DataManager {
|
||||
private static File userdataFolder;
|
||||
|
||||
/**
|
||||
* 通过这个Map缓存玩家的数据
|
||||
*/
|
||||
public static Map<UUID, UserData> userDataCaches = new HashMap<>();
|
||||
|
||||
public static void init() {
|
||||
userdataFolder = new File(Main.getInstance().getDataFolder() + File.separator + "userdata");
|
||||
if (!userdataFolder.isDirectory() || !userdataFolder.exists()) {
|
||||
userdataFolder.mkdir();
|
||||
}
|
||||
}
|
||||
|
||||
public static UserData loadData(UUID uuid) {
|
||||
UserData prefixCache = new UserData(uuid);
|
||||
|
||||
userDataCaches.put(uuid, prefixCache);
|
||||
|
||||
return prefixCache;
|
||||
}
|
||||
|
||||
|
||||
public static UserData getData(UUID uuid) {
|
||||
return userDataCaches.getOrDefault(uuid, loadData(uuid));
|
||||
}
|
||||
|
||||
public static void unloadData(UUID uuid) {
|
||||
if (isDataLoaded(uuid)) {
|
||||
UserData data = getData(uuid);
|
||||
data.stopTasks();
|
||||
data.saveData();
|
||||
|
||||
userDataCaches.remove(uuid);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static boolean isDataLoaded(UUID uuid) {
|
||||
return userDataCaches.containsKey(uuid);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断一个UUID是否有已保存的数据
|
||||
*
|
||||
* @param uuid 判断的UUID
|
||||
* @return 是否已有数据
|
||||
*/
|
||||
public static boolean hasData(UUID uuid) {
|
||||
return Arrays.stream(Objects.requireNonNull(userdataFolder.listFiles()))
|
||||
.anyMatch(file -> file.getName().startsWith(uuid.toString()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
package cc.carm.plugin.timeforflight.models;
|
||||
|
||||
import cc.carm.plugin.timeforflight.Main;
|
||||
import cc.carm.plugin.timeforflight.enums.Permissions;
|
||||
import cc.carm.plugin.timeforflight.managers.ConfigManager;
|
||||
import cc.carm.plugin.timeforflight.utils.MessageParser;
|
||||
import cc.carm.plugin.timeforflight.utils.TimeFormat;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
public class UserData {
|
||||
|
||||
|
||||
UUID uuid;
|
||||
|
||||
private File dataFile;
|
||||
private FileConfiguration data;
|
||||
|
||||
boolean fileLoaded;
|
||||
|
||||
private int remainTime;
|
||||
|
||||
private BukkitRunnable giveTask;
|
||||
private BukkitRunnable flyTask;
|
||||
|
||||
|
||||
public UserData(UUID uuid) {
|
||||
this.uuid = uuid;
|
||||
File userdatasFolder = new File(Main.getInstance().getDataFolder() + "/userdata");
|
||||
if (!userdatasFolder.isDirectory() || !userdatasFolder.exists()) {
|
||||
userdatasFolder.mkdir();
|
||||
}
|
||||
this.dataFile = new File(userdatasFolder, this.uuid + ".yml");
|
||||
this.remainTime = 0;
|
||||
|
||||
|
||||
this.fileLoaded = dataFile.exists();
|
||||
if (fileLoaded) {
|
||||
this.data = YamlConfiguration.loadConfiguration(dataFile);
|
||||
|
||||
readData();
|
||||
}
|
||||
}
|
||||
|
||||
public void startFlyTask(Player player) {
|
||||
player.setAllowFlight(true);
|
||||
flyTask = new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (canFly()) {
|
||||
removeTime(1);
|
||||
if (getRemainTime() == 10) {
|
||||
player.sendMessage(MessageParser.parseColor("§c注意!§7您的飞行剩余时间仅剩10秒,请您注意安全!"));
|
||||
}
|
||||
} else {
|
||||
player.sendMessage(MessageParser.parseColor("&7您已经用尽剩余的飞行时长,已为您关闭飞行状态。"));
|
||||
stopFly(player);
|
||||
}
|
||||
}
|
||||
};
|
||||
flyTask.runTaskTimer(Main.getInstance(), 0L, 20L);
|
||||
|
||||
}
|
||||
|
||||
public void stopFly(Player player) {
|
||||
if (this.flyTask != null) {
|
||||
this.flyTask.cancel();
|
||||
}
|
||||
player.setAllowFlight(false);
|
||||
player.setFlying(false);
|
||||
}
|
||||
|
||||
public void readData() {
|
||||
this.remainTime = getData().getInt("remainTime");
|
||||
}
|
||||
|
||||
public void addTime(int remainTime) {
|
||||
this.remainTime += remainTime;
|
||||
}
|
||||
|
||||
public void removeTime(int time) {
|
||||
this.remainTime -= time;
|
||||
}
|
||||
public void setTime(int time){
|
||||
this.remainTime = time;
|
||||
}
|
||||
|
||||
public void addTimeInConfig() {
|
||||
addTime(ConfigManager.getTimeGiven());
|
||||
}
|
||||
|
||||
public boolean canFly() {
|
||||
return getRemainTime() > 0;
|
||||
}
|
||||
|
||||
|
||||
public BukkitRunnable getGiveTask() {
|
||||
return giveTask;
|
||||
}
|
||||
|
||||
public void startGivenTask(Player player) {
|
||||
giveTask = new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (!player.isOnline()
|
||||
|| !player.hasPermission(Permissions.GET_TIME.toString())
|
||||
|| player.hasPermission(Permissions.UNLIMITED.toString())) {
|
||||
cancel();
|
||||
}
|
||||
addTimeInConfig();
|
||||
if (ConfigManager.isAlertEnabled()) {
|
||||
player.sendMessage(ConfigManager.getAlertMessage());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
getGiveTask().runTaskTimerAsynchronously(Main.getInstance(),
|
||||
ConfigManager.getTimeInterval() * 20L,
|
||||
ConfigManager.getTimeInterval() * 20L);
|
||||
}
|
||||
|
||||
public void stopTasks() {
|
||||
if (getGiveTask() != null) {
|
||||
getGiveTask().cancel();
|
||||
}
|
||||
if (this.flyTask != null) {
|
||||
this.flyTask.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isFileLoaded() {
|
||||
return fileLoaded;
|
||||
}
|
||||
|
||||
public File getDataFile() {
|
||||
return dataFile;
|
||||
}
|
||||
|
||||
public FileConfiguration getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public int getRemainTime() {
|
||||
return remainTime;
|
||||
}
|
||||
|
||||
private void checkFile() {
|
||||
if (!dataFile.exists()) {
|
||||
try {
|
||||
dataFile.createNewFile();
|
||||
} catch (IOException ex) {
|
||||
Bukkit.getLogger().info("Could not load file " + "/userdata/" + "yml" + ex);
|
||||
}
|
||||
}
|
||||
if (!isFileLoaded()) {
|
||||
this.data = YamlConfiguration.loadConfiguration(dataFile);
|
||||
this.fileLoaded = true;
|
||||
}
|
||||
}
|
||||
|
||||
public String getRemainTimeString() {
|
||||
return TimeFormat.getTimeString(getRemainTime());
|
||||
}
|
||||
|
||||
public void saveData() {
|
||||
checkFile();
|
||||
getData().set("remainTime", getRemainTime());
|
||||
try {
|
||||
getData().save(dataFile);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package cc.carm.plugin.timeforflight.utils;
|
||||
|
||||
public class MessageParser {
|
||||
|
||||
public static String parseColor(String text) {
|
||||
return text.replaceAll("&", "§").replace("§§", "&");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package cc.carm.plugin.timeforflight.utils;
|
||||
|
||||
public class TimeFormat {
|
||||
|
||||
|
||||
public static String getTimeString(int time) {
|
||||
int temp;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
temp = time / 60 / 60 % 60;
|
||||
sb.append((temp < 10) ? "0" + temp + ":" : "" + temp + ":");
|
||||
|
||||
temp = time % 3600 / 60;
|
||||
sb.append((temp < 10) ? "0" + temp + ":" : "" + temp + ":");
|
||||
|
||||
temp = time % 3600 % 60;
|
||||
sb.append((temp < 10) ? "0" + temp : "" + temp);
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user