2019-08-25 09:58:28 +00:00
|
|
|
/*
|
|
|
|
* WorldEdit, a Minecraft world manipulation toolkit
|
|
|
|
* Copyright (C) sk89q <http://www.sk89q.com>
|
|
|
|
* Copyright (C) WorldEdit team and contributors
|
|
|
|
*
|
2020-08-25 01:31:47 +00:00
|
|
|
* 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
|
2019-08-25 09:58:28 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2020-08-25 01:31:47 +00:00
|
|
|
* 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.
|
2019-08-25 09:58:28 +00:00
|
|
|
*
|
2020-08-25 01:31:47 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-08-25 09:58:28 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package com.sk89q.worldedit.extension.platform;
|
|
|
|
|
2019-11-20 00:11:54 +00:00
|
|
|
import com.boydti.fawe.object.exception.FaweException;
|
2019-11-23 04:31:48 +00:00
|
|
|
import com.boydti.fawe.object.task.AsyncNotifyQueue;
|
2019-11-20 00:11:54 +00:00
|
|
|
import com.boydti.fawe.util.TaskManager;
|
|
|
|
import com.sk89q.worldedit.WorldEditException;
|
2019-08-25 09:58:28 +00:00
|
|
|
import com.sk89q.worldedit.internal.cui.CUIEvent;
|
|
|
|
|
|
|
|
import java.io.File;
|
2019-11-20 00:11:54 +00:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
2019-08-25 09:58:28 +00:00
|
|
|
|
|
|
|
public abstract class AbstractNonPlayerActor implements Actor {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canDestroyBedrock() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isPlayer() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public File openFileOpenDialog(String[] extensions) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public File openFileSaveDialog(String[] extensions) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void dispatchCUIEvent(CUIEvent event) {
|
|
|
|
}
|
2019-11-20 00:11:54 +00:00
|
|
|
|
|
|
|
private final ConcurrentHashMap<String, Object> meta = new ConcurrentHashMap<>();
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Map<String, Object> getRawMeta() {
|
|
|
|
return meta;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Queue for async tasks
|
|
|
|
private AtomicInteger runningCount = new AtomicInteger();
|
2020-07-14 02:50:59 +00:00
|
|
|
private AsyncNotifyQueue asyncNotifyQueue = new AsyncNotifyQueue((thread, throwable) -> {
|
2019-11-20 00:11:54 +00:00
|
|
|
while (throwable.getCause() != null) {
|
|
|
|
throwable = throwable.getCause();
|
|
|
|
}
|
|
|
|
if (throwable instanceof WorldEditException) {
|
|
|
|
printError(throwable.getLocalizedMessage());
|
|
|
|
} else {
|
|
|
|
FaweException fe = FaweException.get(throwable);
|
|
|
|
if (fe != null) {
|
2019-11-21 19:56:10 +00:00
|
|
|
printError(fe.getComponent());
|
2019-11-20 00:11:54 +00:00
|
|
|
} else {
|
|
|
|
throwable.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2020-07-14 02:50:59 +00:00
|
|
|
* Run a task either async, or on the current thread.
|
2019-11-20 00:11:54 +00:00
|
|
|
*
|
2020-07-14 02:50:59 +00:00
|
|
|
* @param ifFree the task to run if free
|
2019-11-20 00:11:54 +00:00
|
|
|
* @param checkFree Whether to first check if a task is running
|
2020-07-14 02:50:59 +00:00
|
|
|
* @param async TODO Description
|
2019-11-20 00:11:54 +00:00
|
|
|
* @return false if the task was ran or queued
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public boolean runAction(Runnable ifFree, boolean checkFree, boolean async) {
|
|
|
|
if (checkFree) {
|
|
|
|
if (runningCount.get() != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Runnable wrapped = () -> {
|
|
|
|
try {
|
|
|
|
runningCount.addAndGet(1);
|
|
|
|
ifFree.run();
|
|
|
|
} finally {
|
|
|
|
runningCount.decrementAndGet();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if (async) {
|
2019-11-23 04:31:48 +00:00
|
|
|
asyncNotifyQueue.run(wrapped);
|
2019-11-20 00:11:54 +00:00
|
|
|
} else {
|
|
|
|
TaskManager.IMP.taskNow(wrapped, false);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2019-08-25 09:58:28 +00:00
|
|
|
}
|