Update ViaVersion, use CompletableFuture

This commit is contained in:
creeper123123321 2018-10-02 19:33:00 -03:00
parent f69490179b
commit 2f88e44e12
5 changed files with 71 additions and 67 deletions

View File

@ -40,7 +40,7 @@ configurations {
}
dependencies {
shade ('us.myles:viaversion:1.5.1') {
shade ('us.myles:viaversion:1.5.2') {
transitive = false
changing = true
}

View File

@ -40,14 +40,11 @@ import org.spongepowered.asm.mixin.Mixins;
import us.myles.ViaVersion.ViaManager;
import us.myles.ViaVersion.api.Via;
import java.util.concurrent.ThreadFactory;
public class ViaRift implements InitializationListener {
public static int fakeServerVersion = -1;
public static final Logger LOGGER = LogManager.getLogger();
public static final java.util.logging.Logger JLOGGER = new JLoggerToLog4j(LOGGER);
public static final ThreadFactory THREAD_FACTORY = new NamedThreadFactory("ViaRift");
public static final EventLoop EVENT_LOOP = new DefaultEventLoop(THREAD_FACTORY);
public static final EventLoop EVENT_LOOP = new DefaultEventLoop(new NamedThreadFactory("ViaRift"));
@Override
public void onInitialization() {
MixinBootstrap.init();

View File

@ -27,7 +27,7 @@ package com.github.creeper123123321.viarift.platform;
import com.github.creeper123123321.viarift.ViaRift;
import com.github.creeper123123321.viarift.protocol.Interceptor;
import com.github.creeper123123321.viarift.util.FutureTaskId;
import com.github.creeper123123321.viarift.util.ThreadTaskId;
import com.github.creeper123123321.viarift.util.ManagedBlockerRunnable;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.ViaAPI;
@ -46,6 +46,8 @@ import us.myles.viaversion.libs.gson.JsonObject;
import java.io.File;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
@ -74,43 +76,62 @@ public class VRPlatform implements ViaPlatform {
@Override
public TaskId runAsync(Runnable runnable) {
Thread t = ViaRift.THREAD_FACTORY.newThread(runnable);
t.start();
return new ThreadTaskId(t);
return new FutureTaskId(
CompletableFuture
.runAsync(() -> {
try {
ForkJoinPool.managedBlock(new ManagedBlockerRunnable(runnable));
} catch (InterruptedException e) {
e.printStackTrace();
}
}).exceptionally(ex -> {
if (ex != null) ex.printStackTrace();
return null;
})
);
}
@Override
public TaskId runSync(Runnable runnable) {
return new FutureTaskId(ViaRift.EVENT_LOOP.submit(runnable).addListener(future -> {
if (!future.isSuccess()) {
future.cause().printStackTrace();
}
}));
return new FutureTaskId(
CompletableFuture
.runAsync(runnable)
.exceptionally(ex -> {
if (ex != null) ex.printStackTrace();
return null;
})
);
}
@Override
public TaskId runSync(Runnable runnable, Long ticks) {
return new FutureTaskId(ViaRift.EVENT_LOOP.schedule(runnable, ticks * 50, TimeUnit.SECONDS).addListener(future -> {
if (!future.isSuccess()) {
future.cause().printStackTrace();
}
}));
return new FutureTaskId(
ViaRift.EVENT_LOOP
.schedule(runnable, ticks * 50, TimeUnit.SECONDS)
.addListener(future -> {
if (!future.isSuccess()) {
future.cause().printStackTrace();
}
})
);
}
@Override
public TaskId runRepeatingSync(Runnable runnable, Long ticks) {
return new FutureTaskId(ViaRift.EVENT_LOOP.scheduleAtFixedRate(runnable, 0, ticks * 50, TimeUnit.SECONDS).addListener(future -> {
if (!future.isSuccess()) {
future.cause().printStackTrace();
}
}));
return new FutureTaskId(
ViaRift.EVENT_LOOP
.scheduleAtFixedRate(runnable, 0, ticks * 50, TimeUnit.SECONDS)
.addListener(future -> {
if (!future.isSuccess()) {
future.cause().printStackTrace();
}
})
);
}
@Override
public void cancelTask(TaskId taskId) {
if (taskId instanceof ThreadTaskId) {
((ThreadTaskId) taskId).getObject().interrupt();
} else if (taskId instanceof FutureTaskId) {
if (taskId instanceof FutureTaskId) {
((FutureTaskId) taskId).getObject().cancel(false);
}
}
@ -186,4 +207,5 @@ public class VRPlatform implements ViaPlatform {
public boolean isOldClientsAllowed() {
return true;
}
}

View File

@ -0,0 +1,25 @@
package com.github.creeper123123321.viarift.util;
import java.util.concurrent.ForkJoinPool;
public class ManagedBlockerRunnable implements ForkJoinPool.ManagedBlocker {
private final Runnable runnable;
private boolean release;
public ManagedBlockerRunnable(Runnable runnable) {
this.runnable = runnable;
release = false;
}
@Override
public boolean block() {
runnable.run();
release = true;
return true;
}
@Override
public boolean isReleasable() {
return release;
}
}

View File

@ -1,40 +0,0 @@
/*
* MIT License
*
* Copyright (c) 2018 creeper123123321 and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.github.creeper123123321.viarift.util;
import us.myles.ViaVersion.api.platform.TaskId;
public class ThreadTaskId implements TaskId {
private Thread object;
public ThreadTaskId(Thread object) {
this.object = object;
}
@Override
public Thread getObject() {
return object;
}
}