EventLoop for tasks

This commit is contained in:
creeper123123321 2018-08-21 14:19:18 -03:00
parent b1775b5e75
commit dc7c4d34fa
6 changed files with 40 additions and 66 deletions

View File

@ -4,6 +4,8 @@ import com.github.creeper123123321.viarift.platform.VRInjector;
import com.github.creeper123123321.viarift.platform.VRLoader;
import com.github.creeper123123321.viarift.platform.VRPlatform;
import com.github.creeper123123321.viarift.util.JLoggerToLog4j;
import io.netty.channel.DefaultEventLoop;
import io.netty.channel.EventLoop;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dimdev.riftloader.listener.InitializationListener;
@ -16,6 +18,7 @@ public class ViaRift implements InitializationListener {
public static int fakeServerVersion = 393; // TODO
public static final Logger LOGGER = LogManager.getLogger();
public static final java.util.logging.Logger JLOGGER = new JLoggerToLog4j(LOGGER);
public static final EventLoop EVENT_LOOP = new DefaultEventLoop();
@Override
public void onInitialization() {
MixinBootstrap.init();

View File

@ -1,8 +1,8 @@
package com.github.creeper123123321.viarift.platform;
import com.github.creeper123123321.viarift.ViaRift;
import com.github.creeper123123321.viarift.util.DelayedRunnable;
import com.github.creeper123123321.viarift.util.LoopRunnable;
import com.github.creeper123123321.viarift.util.FutureTaskId;
import com.github.creeper123123321.viarift.util.ThreadTaskId;
import net.minecraft.client.Minecraft;
import net.minecraft.util.text.TextComponentString;
import us.myles.ViaVersion.api.ViaAPI;
@ -15,6 +15,7 @@ import us.myles.ViaVersion.sponge.VersionInfo;
import us.myles.viaversion.libs.gson.JsonObject;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
public class VRPlatform implements ViaPlatform {
@ -42,34 +43,30 @@ public class VRPlatform implements ViaPlatform {
public TaskId runAsync(Runnable runnable) {
Thread t = new Thread(runnable, "ViaRift Async Task");
t.start();
return new VRTaskId(t);
return new ThreadTaskId(t);
}
@Override
public TaskId runSync(Runnable runnable) {
Thread t = new Thread(runnable, "ViaRift Sync Task");
t.start();
return new VRTaskId(t);
return new FutureTaskId(ViaRift.EVENT_LOOP.submit(runnable));
}
@Override
public TaskId runSync(Runnable runnable, Long aLong) {
Thread t = new Thread(new DelayedRunnable(runnable, aLong * 50), "ViaRift Sync Delayed Task");
t.start();
return new VRTaskId(t);
public TaskId runSync(Runnable runnable, Long ticks) {
return new FutureTaskId(ViaRift.EVENT_LOOP.schedule(runnable, ticks * 50, TimeUnit.SECONDS));
}
@Override
public TaskId runRepeatingSync(Runnable runnable, Long aLong) {
Thread t = new Thread(new LoopRunnable(runnable, aLong * 50), "ViaRift Sync Repeating Task");
t.start();
return new VRTaskId(t);
public TaskId runRepeatingSync(Runnable runnable, Long ticks) {
return new FutureTaskId(ViaRift.EVENT_LOOP.scheduleAtFixedRate(runnable, 0, ticks * 50, TimeUnit.SECONDS));
}
@Override
public void cancelTask(TaskId taskId) {
if (taskId instanceof VRTaskId) {
((VRTaskId) taskId).getObject().interrupt();
if (taskId instanceof ThreadTaskId) {
((ThreadTaskId) taskId).getObject().interrupt();
} else if (taskId instanceof FutureTaskId) {
((FutureTaskId) taskId).getObject().cancel(false);
}
}

View File

@ -1,21 +0,0 @@
package com.github.creeper123123321.viarift.util;
public class DelayedRunnable implements Runnable {
private Runnable runnable;
private long delay;
public DelayedRunnable(Runnable runnable, long delay) {
this.runnable = runnable;
this.delay = delay;
}
@Override
public void run() {
try {
Thread.sleep(delay);
} catch (InterruptedException ignored) {
return;
}
runnable.run();
}
}

View File

@ -0,0 +1,18 @@
package com.github.creeper123123321.viarift.util;
import us.myles.ViaVersion.api.platform.TaskId;
import java.util.concurrent.Future;
public class FutureTaskId implements TaskId {
private Future<?> object;
public FutureTaskId(Future<?> object) {
this.object = object;
}
@Override
public Future<?> getObject() {
return object;
}
}

View File

@ -1,23 +0,0 @@
package com.github.creeper123123321.viarift.util;
public class LoopRunnable implements Runnable {
private Runnable runnable;
private long interval;
public LoopRunnable(Runnable runnable, long interval) {
this.runnable = runnable;
this.interval = interval;
}
@Override
public void run() {
while (true) {
runnable.run();
try {
Thread.sleep(interval);
} catch (InterruptedException ignored) {
return;
}
}
}
}

View File

@ -1,16 +1,16 @@
package com.github.creeper123123321.viarift.platform;
package com.github.creeper123123321.viarift.util;
import us.myles.ViaVersion.api.platform.TaskId;
public class VRTaskId implements TaskId {
public class ThreadTaskId implements TaskId {
private Thread object;
public ThreadTaskId(Thread object) {
this.object = object;
}
@Override
public Thread getObject() {
return object;
}
public VRTaskId(Thread object) {
this.object = object;
}
}