mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-04-07 00:06:10 +00:00
Merge remote-tracking branch 'upstream/master' into noSystemOut
This commit is contained in:
76
src/main/java/com/volmit/iris/util/scheduling/jobs/Job.java
Normal file
76
src/main/java/com/volmit/iris/util/scheduling/jobs/Job.java
Normal file
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Bukkit Servers
|
||||
* Copyright (c) 2021 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.volmit.iris.util.scheduling.jobs;
|
||||
|
||||
import com.volmit.iris.util.collection.KList;
|
||||
import com.volmit.iris.util.format.Form;
|
||||
import com.volmit.iris.util.plugin.VolmitSender;
|
||||
import com.volmit.iris.util.scheduling.J;
|
||||
import com.volmit.iris.util.scheduling.PrecisionStopwatch;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public interface Job
|
||||
{
|
||||
String getName();
|
||||
|
||||
void execute();
|
||||
|
||||
void completeWork();
|
||||
|
||||
int getTotalWork();
|
||||
|
||||
default int getWorkRemaining()
|
||||
{
|
||||
return getTotalWork() - getWorkCompleted();
|
||||
}
|
||||
|
||||
int getWorkCompleted();
|
||||
|
||||
default String getProgressString()
|
||||
{
|
||||
return Form.pc(getProgress(), 0);
|
||||
}
|
||||
|
||||
default double getProgress()
|
||||
{
|
||||
return (double)getWorkCompleted() / (double)getTotalWork();
|
||||
}
|
||||
|
||||
default void execute(VolmitSender sender)
|
||||
{
|
||||
PrecisionStopwatch p = PrecisionStopwatch.start();
|
||||
CompletableFuture<?> f = J.afut(this::execute);
|
||||
int c = J.ar(() -> {
|
||||
if(sender.isPlayer())
|
||||
{
|
||||
sender.sendProgress(getProgress(), getName());
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
sender.sendMessage(getName() + ": " + getProgressString());
|
||||
}
|
||||
}, sender.isPlayer() ? 0 : 20);
|
||||
f.whenComplete((fs, ff) -> {
|
||||
J.car(c);
|
||||
sender.sendMessage("Completed " + getName() + " in " + Form.duration(p.getMilliseconds(), 1));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Bukkit Servers
|
||||
* Copyright (c) 2021 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.volmit.iris.util.scheduling.jobs;
|
||||
|
||||
import com.volmit.iris.util.collection.KList;
|
||||
|
||||
public class JobCollection implements Job {
|
||||
private final String name;
|
||||
private String status;
|
||||
private final KList<Job> jobs;
|
||||
|
||||
public JobCollection(String name, Job... jobs)
|
||||
{
|
||||
this(name, new KList<>(jobs));
|
||||
}
|
||||
|
||||
public JobCollection(String name, KList<Job> jobs)
|
||||
{
|
||||
this.name = name;
|
||||
status = null;
|
||||
this.jobs = new KList<>(jobs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return status == null ? name : (name + " 》" + status);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
for(Job i : jobs)
|
||||
{
|
||||
status = i.getName();
|
||||
i.execute();
|
||||
}
|
||||
|
||||
status = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void completeWork() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTotalWork() {
|
||||
return jobs.stream().mapToInt(Job::getTotalWork).sum();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWorkCompleted() {
|
||||
return jobs.stream().mapToInt(Job::getWorkCompleted).sum();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Bukkit Servers
|
||||
* Copyright (c) 2021 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.volmit.iris.util.scheduling.jobs;
|
||||
|
||||
import com.volmit.iris.util.collection.KList;
|
||||
|
||||
public abstract class QueueJob<T> implements Job {
|
||||
private final KList<T> queue;
|
||||
private int totalWork;
|
||||
private int completed;
|
||||
|
||||
public QueueJob()
|
||||
{
|
||||
totalWork = 0;
|
||||
completed = 0;
|
||||
queue = new KList<>();
|
||||
}
|
||||
|
||||
public void queue(T t)
|
||||
{
|
||||
queue.add(t);
|
||||
totalWork++;
|
||||
}
|
||||
|
||||
public void queue(KList<T> f)
|
||||
{
|
||||
queue.addAll(f);
|
||||
totalWork += f.size();
|
||||
}
|
||||
|
||||
public abstract void execute(T t);
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
totalWork = queue.size();
|
||||
while(queue.isNotEmpty())
|
||||
{
|
||||
execute(queue.pop());
|
||||
completeWork();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void completeWork() {
|
||||
completed++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTotalWork() {
|
||||
return totalWork;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWorkCompleted() {
|
||||
return completed;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Bukkit Servers
|
||||
* Copyright (c) 2021 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.volmit.iris.util.scheduling.jobs;
|
||||
|
||||
public class SingleJob implements Job{
|
||||
private boolean done;
|
||||
private final String name;
|
||||
private final Runnable runnable;
|
||||
|
||||
public SingleJob(String name, Runnable runnable)
|
||||
{
|
||||
this.name = name;
|
||||
done = false;
|
||||
this.runnable = runnable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
runnable.run();
|
||||
completeWork();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void completeWork() {
|
||||
done = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTotalWork() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWorkCompleted() {
|
||||
return done ? 1 : 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user