1.任务的暂停、恢复;

2.任务遇到错误(材料不足 耐久不足)自动暂停;
3.进度条动态显示
4.优化了放置和破坏的顺序
5.优化了队列切换速度
This commit is contained in:
张宇衡 2023-06-06 11:14:18 +08:00
parent da74686f66
commit 3f6629eef8
7 changed files with 123 additions and 42 deletions

View File

@ -6,7 +6,7 @@
<groupId>site.deercloud</groupId> <groupId>site.deercloud</groupId>
<artifactId>LiteWorldEdit</artifactId> <artifactId>LiteWorldEdit</artifactId>
<version>1.5.13-SNAPSHOT</version> <version>1.6.4</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>LiteWorldEdit</name> <name>LiteWorldEdit</name>

View File

@ -146,7 +146,7 @@ public class Commands implements TabExecutor {
} else if (Objects.equals(args[0], "cancel")) { } else if (Objects.equals(args[0], "cancel")) {
if (sender instanceof Player) { if (sender instanceof Player) {
Player player = (Player) sender; Player player = (Player) sender;
LiteWorldEdit.instance.getCache().deleteAllJobsOfPlayer(player); LiteWorldEdit.instance.getCache().getQueueOf(player).cancel();
sender.sendMessage("已取消。"); sender.sendMessage("已取消。");
} else { } else {
sender.sendMessage("该命令只能由玩家执行。"); sender.sendMessage("该命令只能由玩家执行。");
@ -164,6 +164,22 @@ public class Commands implements TabExecutor {
LiteWorldEdit.instance.reloadConfig(); LiteWorldEdit.instance.reloadConfig();
sender.sendMessage("已重载配置文件。"); sender.sendMessage("已重载配置文件。");
} }
} else if (Objects.equals(args[0], "pause")) {
if (sender instanceof Player) {
Player player = (Player) sender;
LiteWorldEdit.instance.getCache().getQueueOf(player).pause();
sender.sendMessage("已暂停。");
} else {
sender.sendMessage("该命令只能由玩家执行。");
}
} else if (Objects.equals(args[0], "resume")) {
if (sender instanceof Player) {
Player player = (Player) sender;
LiteWorldEdit.instance.getCache().getQueueOf(player).resume();
sender.sendMessage("已恢复。");
} else {
sender.sendMessage("该命令只能由玩家执行。");
}
} else { } else {
sender.sendMessage("参数错误。"); sender.sendMessage("参数错误。");
} }
@ -173,7 +189,7 @@ public class Commands implements TabExecutor {
@Override @Override
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) { public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
if (args.length == 1) { if (args.length == 1) {
return Arrays.asList("point", "p", "points", "fill", "empty", "cancel", "help", "reload"); return Arrays.asList("point", "p", "points", "fill", "empty", "cancel", "pause", "resume", "help", "reload");
} else if (args.length == 2) { } else if (args.length == 2) {
switch (args[1]) { switch (args[1]) {
case "point": case "point":
@ -195,6 +211,8 @@ public class Commands implements TabExecutor {
sender.sendMessage(ChatColor.GREEN + "/lwe fill [点序号A] [点序号B] - (在AB点对角线间放置方块 - 需要手持被放置的方块)"); sender.sendMessage(ChatColor.GREEN + "/lwe fill [点序号A] [点序号B] - (在AB点对角线间放置方块 - 需要手持被放置的方块)");
sender.sendMessage(ChatColor.GREEN + "/lwe empty [点序号A] [点序号B] - (破坏AB点对角线间方块 - 需要拥有下届合金镐)"); sender.sendMessage(ChatColor.GREEN + "/lwe empty [点序号A] [点序号B] - (破坏AB点对角线间方块 - 需要拥有下届合金镐)");
sender.sendMessage(ChatColor.GREEN + "/lwe cancel - 取消所有任务"); sender.sendMessage(ChatColor.GREEN + "/lwe cancel - 取消所有任务");
sender.sendMessage(ChatColor.GREEN + "/lwe pause - 暂停工作");
sender.sendMessage(ChatColor.GREEN + "/lwe resume - 恢复工作");
} }
static public boolean out_of_region(Point A, Point B) { static public boolean out_of_region(Point A, Point B) {

View File

@ -10,8 +10,8 @@ import site.deercloud.liteworldedit.Managers.Point;
public class Empty { public class Empty {
public static void empty(Player player, World world, Point p1, Point p2) { public static void empty(Player player, World world, Point p1, Point p2) {
// 经验扣除与校验 // 经验扣除与校验
for (int x = Math.min(p1.x, p2.x); x <= Math.max(p1.x, p2.x); x++) { for (int y = Math.max(p1.y, p2.y); y >= Math.min(p1.y, p2.y); y--) {
for (int y = Math.min(p1.y, p2.y); y <= Math.max(p1.y, p2.y); y++) { for (int x = Math.min(p1.x, p2.x); x <= Math.max(p1.x, p2.x); x++) {
for (int z = Math.min(p1.z, p2.z); z <= Math.max(p1.z, p2.z); z++) { for (int z = Math.min(p1.z, p2.z); z <= Math.max(p1.z, p2.z); z++) {
Location location = new Location(world, (double) x, (double) y, (double) z); Location location = new Location(world, (double) x, (double) y, (double) z);
Remove remove_job = new Remove(location, player); Remove remove_job = new Remove(location, player);

View File

@ -12,8 +12,8 @@ import site.deercloud.liteworldedit.Managers.Point;
public class Fill { public class Fill {
public static void fill(Player player, World world, Point p1, Point p2, Material block) { public static void fill(Player player, World world, Point p1, Point p2, Material block) {
for (int x = Math.min(p1.x, p2.x); x <= Math.max(p1.x, p2.x); x++) { for (int y = Math.min(p1.y, p2.y); y <= Math.max(p1.y, p2.y); y++) {
for (int y = Math.max(p1.y, p2.y); y >= Math.min(p1.y, p2.y); y--) { for (int x = Math.min(p1.x, p2.x); x <= Math.max(p1.x, p2.x); x++) {
for (int z = Math.min(p1.z, p2.z); z <= Math.max(p1.z, p2.z); z++) { for (int z = Math.min(p1.z, p2.z); z <= Math.max(p1.z, p2.z); z++) {
Location location = new Location(world, (double) x, (double) y, (double) z); Location location = new Location(world, (double) x, (double) y, (double) z);
Place place_job = new Place(location, player, block); Place place_job = new Place(location, player, block);

View File

@ -43,8 +43,8 @@ public final class LiteWorldEdit extends JavaPlugin {
} }
} else { } else {
Player player = job.get_creator(); Player player = job.get_creator();
player.sendTitle("§c错误 任务已取消", "§c" + re.getMessage(), 10, 70, 20); player.sendTitle("§c错误 任务已自动暂停", "§c" + re.getMessage(), 10, 70, 20);
_cache.deleteAllJobsOfPlayer(player); _cache.getQueueOf(player).pause();
return; return;
} }
} }

View File

@ -1,26 +1,19 @@
package site.deercloud.liteworldedit.Managers; package site.deercloud.liteworldedit.Managers;
import org.bukkit.Bukkit;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import site.deercloud.liteworldedit.Jobs.Job; import site.deercloud.liteworldedit.Jobs.Job;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map; import java.util.Map;
public class Cache { public class Cache {
private final Map<String, Map<Integer, Point>> _points; private final Map<String, Map<Integer, Point>> _points;
private final Map<String, LinkedList<Job>> _jobs; private final Map<String, JobQueue> _jobs;
private String _last_jobs_player; // 上一次被获取任务的玩家uuid private String _last_jobs_player; // 上一次被获取任务的玩家uuid
private final Map<String, BossBar> _bars;
public Cache() { public Cache() {
_points = new HashMap<String, Map<Integer, Point>>(); _points = new HashMap<String, Map<Integer, Point>>();
_jobs = new HashMap<String, LinkedList<Job>>(); _jobs = new HashMap<String, JobQueue>();
_bars = new HashMap<String, BossBar>();
} }
public boolean addPoint(Player player, Integer index, Point point) { public boolean addPoint(Player player, Integer index, Point point) {
@ -36,7 +29,7 @@ public class Cache {
public void addJob(Player player, Job job) { public void addJob(Player player, Job job) {
if (!_jobs.containsKey(player.getUniqueId().toString())) { if (!_jobs.containsKey(player.getUniqueId().toString())) {
_jobs.put(player.getUniqueId().toString(), new LinkedList<Job>()); _jobs.put(player.getUniqueId().toString(), new JobQueue(player));
} }
_jobs.get(player.getUniqueId().toString()).add(job); _jobs.get(player.getUniqueId().toString()).add(job);
} }
@ -49,39 +42,19 @@ public class Cache {
if (!_jobs.containsKey(player)) { if (!_jobs.containsKey(player)) {
return null; return null;
} }
if (_jobs.get(player).isEmpty()) { return _jobs.get(player).pop();
_jobs.remove(player);
return null;
}
Job job = _jobs.get(player).pop();
updateBarOfPlayer(job.get_creator());
return job;
} }
public Map<Integer, Point> getPoints(Player player) { public Map<Integer, Point> getPoints(Player player) {
return _points.get(player.getUniqueId().toString()); return _points.get(player.getUniqueId().toString());
} }
public void deleteAllJobsOfPlayer(Player player) { public JobQueue getQueueOf(Player player) {
_jobs.get(player.getUniqueId().toString()).clear(); return _jobs.get(player.getUniqueId().toString());
updateBarOfPlayer(player);
} }
public void deletePlayerCache(Player player) { public void deletePlayerCache(Player player) {
// _points.remove(player.getUniqueId().toString());
_jobs.remove(player.getUniqueId().toString()); _jobs.remove(player.getUniqueId().toString());
_bars.remove(player.getUniqueId().toString());
}
public void updateBarOfPlayer(Player player) {
if (!_bars.containsKey(player.getUniqueId().toString())) {
_bars.put(player.getUniqueId().toString(), Bukkit.createBossBar("§a§lLiteWorldEdit 施工进度", BarColor.GREEN, BarStyle.SOLID));
_bars.get(player.getUniqueId().toString()).addPlayer(player);
}
BossBar bar = _bars.get(player.getUniqueId().toString());
bar.setProgress(1);
bar.setTitle("§a§lLiteWorldEdit 施工进度 剩余任务: " + _jobs.get(player.getUniqueId().toString()).size());
bar.setVisible(_jobs.get(player.getUniqueId().toString()).size() != 0);
} }
private String getNextPlayer() { private String getNextPlayer() {
@ -95,6 +68,10 @@ public class Cache {
boolean found = false; boolean found = false;
for (String key : _jobs.keySet()) { for (String key : _jobs.keySet()) {
if (found) { if (found) {
// 忽略没有任务的玩家
if (_jobs.get(key).size() == 0) {
continue;
}
_last_jobs_player = key; _last_jobs_player = key;
return key; return key;
} }

View File

@ -0,0 +1,86 @@
package site.deercloud.liteworldedit.Managers;
import org.bukkit.Bukkit;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar;
import org.bukkit.entity.Player;
import site.deercloud.liteworldedit.Jobs.Job;
import java.util.LinkedList;
public class JobQueue {
private final LinkedList<Job> queue = new LinkedList<Job>();
private final BossBar bar = Bukkit.createBossBar("§a§lLiteWorldEdit", BarColor.GREEN, BarStyle.SOLID);
boolean paused = false;
int total = 0;
int ms_last_pop;
Player player;
public JobQueue(Player player) {
this.player = player;
this.bar.addPlayer(player);
}
public void add(Job job) {
queue.add(job);
total++;
}
public Job pop() {
if (queue.isEmpty()) {
total = 0;
return null;
}
if (paused) {
return null;
}
Job job = queue.pop();
updateBarProgressBar();
ms_last_pop = (int) System.currentTimeMillis();
return job;
}
public int size() {
return queue.size();
}
public boolean isEmpty() {
return queue.isEmpty();
}
public void pause() {
bar.setColor(BarColor.YELLOW);
bar.setTitle("§e§lLiteWorldEdit [暂停] 剩余任务: " + queue.size());
paused = true;
}
public void resume() {
bar.setColor(BarColor.GREEN);
paused = false;
}
public void cancel() {
queue.clear();
total = 0;
bar.setVisible(false);
paused = false;
}
private void updateBarProgressBar() {
bar.setProgress((total - queue.size()) * 1.0 / total);
int delta_time_ms = (int) System.currentTimeMillis() - ms_last_pop;
int time_s_remaining = queue.size() * delta_time_ms / 1000;
int time_m_remaining = time_s_remaining / 60;
int time_s_remaining_mod = time_s_remaining % 60;
bar.setTitle("§a§lLiteWorldEdit [正在运行] 剩余任务: " + queue.size() + " 预计剩余时间: " + time_m_remaining + "" + time_s_remaining_mod + "");
bar.setVisible(queue.size() != 0);
}
}