Compare commits
12
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cc403cf68b | ||
|
|
9cb0c66c93 | ||
|
|
0d0774069f | ||
|
|
a4aadb9950 | ||
|
|
b637afb3a3 | ||
|
|
7e4154f57d | ||
|
|
610055f979 | ||
|
|
935572b7f6 | ||
|
|
254d57fac3 | ||
|
|
07658afbc1 | ||
|
|
5e6f48f152 | ||
|
|
14221f095e |
No files matched your search
@@ -1,3 +1,8 @@
|
||||
.gradle
|
||||
.idea
|
||||
.project
|
||||
.settings/
|
||||
build
|
||||
run
|
||||
.classpath
|
||||
bin/
|
||||
@@ -1,8 +1,17 @@
|
||||
## WorldEdit Tick Spreader
|
||||
## WETS-OG
|
||||
|
||||
Spread WorldEdit operations across multiple ticks.
|
||||
A soft fork of [WETS](https://github.com/emilyy-dev/wets) maintained for [TrueOG](https://true-og.net/).
|
||||
|
||||
That's all. Download on [modrinth](https://modrinth.com/mod/wets).
|
||||
**Changes:**
|
||||
|
||||
- Changed API version to 1.19.4.
|
||||
- Removed Fabric support.
|
||||
- Modified project structure to build with TrueOG's gradle configuration.
|
||||
- Messages are colored using MiniMessage.
|
||||
- Added a customizable prefix.
|
||||
- Added a customizable default blocks per tick (set to 100).
|
||||
|
||||
Spreads WorldEdit operations across multiple ticks.
|
||||
|
||||
Semi-configurable, single command `/wets` (`/worldedit-tick-spreader`), one permission (`wets`), two possible arguments:
|
||||
* `/wets (sorted | not-sorted)` - whether blocks will be placed in a sorted arrangement or not. Sorted is default.
|
||||
@@ -14,5 +23,4 @@ Sorted placing and block count per tick, affected by commands, are per-player.
|
||||
## License
|
||||
Project licensed under the MIT license (also known as Expat license).
|
||||
|
||||
The Fabric build of this project redistributes
|
||||
[lucko/fabric-permissions-api](https://github.com/lucko/fabric-permissions-api) which is licensed under the MIT license.
|
||||
The original project which supports Fabric API can be found [here](https://github.com/emilyy-dev/wets).
|
||||
+78
-27
@@ -1,36 +1,87 @@
|
||||
import java.io.BufferedOutputStream
|
||||
import java.io.FileOutputStream
|
||||
import java.nio.file.Files
|
||||
import java.util.zip.ZipEntry
|
||||
import java.util.zip.ZipOutputStream
|
||||
|
||||
plugins {
|
||||
id("com.github.johnrengelman.shadow") version "7.1.2" apply false
|
||||
id("fabric-loom") version "1.0-SNAPSHOT" apply false
|
||||
id("org.jetbrains.kotlin.jvm") version "1.9.23"
|
||||
id("com.github.johnrengelman.shadow") version "8.1.1"
|
||||
id("eclipse")
|
||||
}
|
||||
|
||||
subprojects {
|
||||
apply(plugin = "java")
|
||||
group = "net.trueog.wets-og" // Declare bundle identifier.
|
||||
version = "1.2"
|
||||
|
||||
val apiVersion = "1.19"
|
||||
|
||||
tasks.named<ProcessResources>("processResources") {
|
||||
val props = mapOf(
|
||||
"version" to version,
|
||||
"apiVersion" to apiVersion,
|
||||
)
|
||||
|
||||
filesMatching("plugin.yml") {
|
||||
expand(props)
|
||||
}
|
||||
}
|
||||
|
||||
repositories {
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
maven("https://maven.enginehub.org/repo/")
|
||||
}
|
||||
|
||||
dependencies {
|
||||
"implementation"("com.sk89q.worldedit:worldedit-core:7.2.13")
|
||||
"implementation"("it.unimi.dsi:fastutil:8.5.9")
|
||||
"compileOnly"("org.jetbrains:annotations:23.0.0")
|
||||
}
|
||||
|
||||
tasks {
|
||||
named<JavaCompile>("compileJava") {
|
||||
options.release.set(17)
|
||||
maven {
|
||||
url = uri("https://repo.purpurmc.org/snapshots")
|
||||
}
|
||||
maven {
|
||||
url = uri("https://maven.enginehub.org/repo") // Get WorldEdit API from EngineHub Repository.
|
||||
}
|
||||
|
||||
named<ProcessResources>("processResources") {
|
||||
inputs.property("version", this@subprojects.version)
|
||||
filesNotMatching("**/icon.png") {
|
||||
expand("version" to this@subprojects.version)
|
||||
}
|
||||
}
|
||||
|
||||
withType<Jar> {
|
||||
metaInf.from(rootProject.file("LICENSE")).into("ar.emily/wets")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compileOnly("org.purpurmc.purpur:purpur-api:1.19.4-R0.1-SNAPSHOT")
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0")
|
||||
compileOnly("com.sk89q.worldedit:worldedit-bukkit:7.2.14")
|
||||
}
|
||||
|
||||
tasks.withType<AbstractArchiveTask>().configureEach {
|
||||
isPreserveFileTimestamps = false
|
||||
isReproducibleFileOrder = true
|
||||
}
|
||||
|
||||
tasks.shadowJar {
|
||||
minimize()
|
||||
}
|
||||
|
||||
tasks.shadowJar.configure {
|
||||
archiveClassifier.set("")
|
||||
from("LICENSE") {
|
||||
into("/")
|
||||
}
|
||||
}
|
||||
|
||||
tasks.jar {
|
||||
dependsOn("shadowJar")
|
||||
}
|
||||
|
||||
tasks.jar.configure {
|
||||
archiveClassifier.set("part")
|
||||
}
|
||||
|
||||
tasks.withType<JavaCompile>().configureEach {
|
||||
options.compilerArgs.add("-parameters")
|
||||
options.encoding = "UTF-8"
|
||||
options.forkOptions.executable = File(options.forkOptions.javaHome, "bin/javac").path
|
||||
}
|
||||
|
||||
kotlin {
|
||||
jvmToolchain(17)
|
||||
}
|
||||
|
||||
java {
|
||||
toolchain {
|
||||
languageVersion = JavaLanguageVersion.of(17)
|
||||
vendor = JvmVendorSpec.GRAAL_VM
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
plugins {
|
||||
id("com.github.johnrengelman.shadow")
|
||||
}
|
||||
|
||||
repositories {
|
||||
maven("https://repo.papermc.io/repository/maven-public/")
|
||||
}
|
||||
|
||||
dependencies {
|
||||
// this is all set up the wrong way around but shadowJar is special
|
||||
implementation("io.papermc.paper:paper-api:1.19.3-R0.1-SNAPSHOT")
|
||||
implementation("com.sk89q.worldedit:worldedit-bukkit:7.2.13")
|
||||
shadow(project(":wets-common")) { isTransitive = false }
|
||||
}
|
||||
|
||||
tasks {
|
||||
shadowJar {
|
||||
configurations = listOf(project.configurations["shadow"])
|
||||
archiveBaseName.set("WETS")
|
||||
archiveClassifier.set("bukkit")
|
||||
}
|
||||
|
||||
build { dependsOn(shadowJar) }
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
package ar.emily.wets.bukkit;
|
||||
|
||||
import ar.emily.wets.common.AbstractScheduler;
|
||||
import ar.emily.wets.common.WESpread;
|
||||
import com.sk89q.worldedit.bukkit.BukkitAdapter;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.util.StringUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public final class WESpreadPlugin extends JavaPlugin implements Listener {
|
||||
|
||||
private static final List<String> COMPLETIONS = List.of("sorted", "not-sorted");
|
||||
|
||||
private final WESpread plugin = new WESpread(new AbstractScheduler(getServer()::getCurrentTick, task -> getServer().getScheduler().runTaskTimer(this, task, 1L, 1L)));
|
||||
|
||||
@EventHandler
|
||||
public void on(final PlayerQuitEvent event) {
|
||||
this.plugin.playerLogout(event.getPlayer().getUniqueId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoad() {
|
||||
this.plugin.load();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
getServer().getPluginManager().registerEvents(this, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
this.plugin.flush();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(
|
||||
final @NotNull CommandSender sender,
|
||||
final @NotNull Command command,
|
||||
final @NotNull String label,
|
||||
final String @NotNull [] args
|
||||
) {
|
||||
this.plugin.command(BukkitAdapter.adapt(sender), Arrays.asList(args));
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull List<String> onTabComplete(
|
||||
final @NotNull CommandSender sender,
|
||||
final @NotNull Command command,
|
||||
final @NotNull String alias,
|
||||
final String @NotNull [] args
|
||||
) {
|
||||
return args.length == 1 ? StringUtil.copyPartialMatches(args[0], COMPLETIONS, new ArrayList<>(1)) : List.of();
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
|
||||
@@ -1,79 +0,0 @@
|
||||
package ar.emily.wets.common;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Iterator;
|
||||
import java.util.Queue;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.IntSupplier;
|
||||
|
||||
public class AbstractScheduler implements Scheduler {
|
||||
|
||||
private final IntSupplier currentTickGetter;
|
||||
private final Queue<AbstractTask> pendingTasks = new ArrayDeque<>();
|
||||
private final Queue<AbstractTask> tasks = new ArrayDeque<>();
|
||||
|
||||
public AbstractScheduler(final IntSupplier currentTickGetter, final Consumer<Runnable> underlyingScheduler) {
|
||||
this.currentTickGetter = currentTickGetter;
|
||||
underlyingScheduler.accept(() -> {
|
||||
processPendingTasks();
|
||||
processTasks();
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void runPeriodically(final Consumer<Task> action, final long initialDelay, final long period) {
|
||||
this.pendingTasks.add(new AbstractTask(action, this.currentTickGetter.getAsInt(), initialDelay, period));
|
||||
}
|
||||
|
||||
public void flush() {
|
||||
this.tasks.addAll(this.pendingTasks);
|
||||
this.pendingTasks.clear();
|
||||
|
||||
final Iterator<AbstractTask> it = this.tasks.iterator();
|
||||
while (it.hasNext()) {
|
||||
final AbstractTask task = it.next();
|
||||
task.task.accept(it::remove);
|
||||
}
|
||||
|
||||
this.tasks.clear();
|
||||
}
|
||||
|
||||
private void processPendingTasks() {
|
||||
final int currentTick = this.currentTickGetter.getAsInt();
|
||||
final Iterator<AbstractTask> it = this.pendingTasks.iterator();
|
||||
while (it.hasNext()) {
|
||||
final AbstractTask task = it.next();
|
||||
if (task.registerTick + task.initialDelay >= currentTick) {
|
||||
this.tasks.add(task);
|
||||
task.registerTick = currentTick;
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void processTasks() {
|
||||
final int currentTick = this.currentTickGetter.getAsInt();
|
||||
final Iterator<AbstractTask> it = this.tasks.iterator();
|
||||
while (it.hasNext()) {
|
||||
final AbstractTask task = it.next();
|
||||
if ((currentTick - task.registerTick) % task.period == 0) {
|
||||
task.task.accept(it::remove);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static final class AbstractTask {
|
||||
|
||||
final Consumer<Task> task;
|
||||
final long initialDelay;
|
||||
final long period;
|
||||
long registerTick;
|
||||
|
||||
AbstractTask(final Consumer<Task> task, final long registerTick, final long initialDelay, final long period) {
|
||||
this.task = task;
|
||||
this.registerTick = registerTick;
|
||||
this.initialDelay = Math.min(initialDelay, 1);
|
||||
this.period = Math.min(period, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package ar.emily.wets.common;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public interface Scheduler {
|
||||
|
||||
void flush();
|
||||
void runPeriodically(Consumer<Task> action, long initialDelay, long period);
|
||||
|
||||
@FunctionalInterface
|
||||
interface Task {
|
||||
|
||||
void cancel();
|
||||
}
|
||||
}
|
||||
@@ -1,174 +0,0 @@
|
||||
package ar.emily.wets.common;
|
||||
|
||||
import com.sk89q.worldedit.EditSession;
|
||||
import com.sk89q.worldedit.WorldEdit;
|
||||
import com.sk89q.worldedit.WorldEditException;
|
||||
import com.sk89q.worldedit.entity.Player;
|
||||
import com.sk89q.worldedit.event.extent.EditSessionEvent;
|
||||
import com.sk89q.worldedit.extension.platform.Actor;
|
||||
import com.sk89q.worldedit.extent.AbstractBufferingExtent;
|
||||
import com.sk89q.worldedit.extent.Extent;
|
||||
import com.sk89q.worldedit.function.operation.Operation;
|
||||
import com.sk89q.worldedit.function.operation.RunContext;
|
||||
import com.sk89q.worldedit.math.BlockVector3;
|
||||
import com.sk89q.worldedit.util.collection.BlockMap;
|
||||
import com.sk89q.worldedit.util.eventbus.Subscribe;
|
||||
import com.sk89q.worldedit.util.formatting.text.Component;
|
||||
import com.sk89q.worldedit.util.formatting.text.TextComponent;
|
||||
import com.sk89q.worldedit.util.formatting.text.format.TextColor;
|
||||
import com.sk89q.worldedit.world.block.BaseBlock;
|
||||
import com.sk89q.worldedit.world.block.BlockStateHolder;
|
||||
import it.unimi.dsi.fastutil.objects.Object2LongMap;
|
||||
import it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.function.LongSupplier;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public final class WESpread {
|
||||
|
||||
private static final Component PLAYERS_ONLY = TextComponent.of("This command can only be ran by players", TextColor.RED);
|
||||
private static final Component INVALID_ARGUMENT = TextComponent.of("Blocks per tick must be a valid positive number", TextColor.RED);
|
||||
private static final Component COMMAND_USAGE = TextComponent.of("Usage: /wets (<blocks per tick> | sorted | not-sorted)");
|
||||
private static final Component COMMAND_SORTED = TextComponent.of("Block placement of new operations will now be sorted");
|
||||
private static final Component COMMAND_NOT_SORTED = TextComponent.of("Block placement of new operations will now be unsorted");
|
||||
private static final Component COMMAND_BPT_UPDATED = TextComponent.of("Blocks per tick placement updated");
|
||||
|
||||
private final Scheduler scheduler;
|
||||
private final Object2LongMap<UUID> blocksPerTickMap = new Object2LongOpenHashMap<>();
|
||||
private final Set<UUID> actorsWhosePlacementIsNotSorted = new HashSet<>();
|
||||
private long defaultBlocksPerTick = 1;
|
||||
|
||||
public WESpread(final Scheduler scheduler) {
|
||||
this.scheduler = scheduler;
|
||||
}
|
||||
|
||||
public void playerLogout(final UUID id) {
|
||||
this.blocksPerTickMap.removeLong(id);
|
||||
this.actorsWhosePlacementIsNotSorted.remove(id);
|
||||
}
|
||||
|
||||
public void flush() {
|
||||
this.defaultBlocksPerTick = Long.MAX_VALUE;
|
||||
this.blocksPerTickMap.clear();
|
||||
this.actorsWhosePlacementIsNotSorted.clear();
|
||||
this.scheduler.flush();
|
||||
}
|
||||
|
||||
public void load() {
|
||||
WorldEdit.getInstance().getEventBus().register(new Object() {
|
||||
@Subscribe
|
||||
public void on(final EditSessionEvent event) {
|
||||
final @Nullable Actor actor = event.getActor();
|
||||
if (event.getStage() == EditSession.Stage.BEFORE_CHANGE && actor != null) {
|
||||
event.setExtent(new SchedulingExtent(event.getExtent(), actor.getUniqueId()));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void command(final Actor source, final List<String> args) {
|
||||
if (args.size() != 1) {
|
||||
source.print(COMMAND_USAGE);
|
||||
return;
|
||||
}
|
||||
if (!(source instanceof Player player)) {
|
||||
source.print(PLAYERS_ONLY);
|
||||
return;
|
||||
}
|
||||
|
||||
final String arg = args.iterator().next();
|
||||
if ("sorted".equals(arg)) {
|
||||
this.actorsWhosePlacementIsNotSorted.remove(player.getUniqueId());
|
||||
source.print(COMMAND_SORTED);
|
||||
return;
|
||||
} else if ("not-sorted".equals(arg)) {
|
||||
this.actorsWhosePlacementIsNotSorted.add(player.getUniqueId());
|
||||
source.print(COMMAND_NOT_SORTED);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
long blocksPerTick = Long.parseLong(arg);
|
||||
if (blocksPerTick < 0) { blocksPerTick = Long.MAX_VALUE; }
|
||||
this.blocksPerTickMap.put(player.getUniqueId(), blocksPerTick);
|
||||
source.print(COMMAND_BPT_UPDATED);
|
||||
} catch (final NumberFormatException exception) {
|
||||
source.print(INVALID_ARGUMENT);
|
||||
source.print(COMMAND_USAGE);
|
||||
}
|
||||
}
|
||||
|
||||
private final class SchedulingExtent extends AbstractBufferingExtent {
|
||||
|
||||
private final BlockMap<BaseBlock> buffer = BlockMap.createForBaseBlock();
|
||||
private final LongSupplier blocksPerTick;
|
||||
private final boolean sorted;
|
||||
|
||||
private SchedulingExtent(final Extent delegate, final UUID playerId) {
|
||||
super(delegate);
|
||||
this.sorted = !WESpread.this.actorsWhosePlacementIsNotSorted.contains(playerId);
|
||||
this.blocksPerTick = () -> WESpread.this.blocksPerTickMap.getOrDefault(playerId, WESpread.this.defaultBlocksPerTick);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @Nullable BaseBlock getBufferedFullBlock(final BlockVector3 position) {
|
||||
return this.buffer.computeIfAbsent(position, getExtent()::getFullBlock);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends BlockStateHolder<T>> boolean setBlock(final BlockVector3 position, final T block) {
|
||||
this.buffer.remove(position); // workaround silly WorldEdit bug, see https://github.com/emilyy-dev/wets/issues/1
|
||||
this.buffer.put(position, block.toBaseBlock());
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Operation commitBefore() {
|
||||
final Iterator<Map.Entry<BlockVector3, BaseBlock>> it;
|
||||
{
|
||||
Stream<Map.Entry<BlockVector3, BaseBlock>> stream = this.buffer.entrySet().stream();
|
||||
if (this.sorted) { stream = stream.sorted(Map.Entry.comparingByKey(BlockVector3.sortByCoordsYzx())); }
|
||||
it = stream.iterator();
|
||||
}
|
||||
|
||||
return new Operation() {
|
||||
boolean started = false;
|
||||
|
||||
@Override
|
||||
public Operation resume(final RunContext run) {
|
||||
//noinspection PointlessBooleanExpression
|
||||
if (this.started == false && it.hasNext()) {
|
||||
this.started = true;
|
||||
WESpread.this.scheduler.runPeriodically(this::putBlock, 1L, 1L);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private void putBlock(final Scheduler.Task task) {
|
||||
try {
|
||||
for (long i = 0; it.hasNext() && i < SchedulingExtent.this.blocksPerTick.getAsLong(); ++i) {
|
||||
final var entry = it.next();
|
||||
setDelegateBlock(entry.getKey(), entry.getValue());
|
||||
}
|
||||
} catch (final WorldEditException exception) {
|
||||
throw new RuntimeException(exception.getMessage(), exception);
|
||||
} finally {
|
||||
if (!it.hasNext()) { task.cancel(); }
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancel() {
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
plugins {
|
||||
id("fabric-loom")
|
||||
}
|
||||
|
||||
repositories {
|
||||
maven("https://oss.sonatype.org/content/repositories/snapshots/")
|
||||
}
|
||||
|
||||
dependencies {
|
||||
minecraft("com.mojang:minecraft:1.19.3")
|
||||
mappings(loom.officialMojangMappings())
|
||||
modImplementation("net.fabricmc:fabric-loader:0.14.11")
|
||||
setOf(
|
||||
"fabric-command-api-v2",
|
||||
"fabric-lifecycle-events-v1"
|
||||
)
|
||||
.forEach { modImplementation(fabricApi.module(it, "0.72.0+1.19.3")) }
|
||||
|
||||
modImplementation("com.sk89q.worldedit:worldedit-fabric-mc1.19.3:7.2.13")
|
||||
modImplementation("me.lucko:fabric-permissions-api:0.2-SNAPSHOT") { include(this) }
|
||||
implementation(project(":wets-common")) { include(this) }
|
||||
}
|
||||
|
||||
tasks {
|
||||
remapJar {
|
||||
archiveBaseName.set("WETS")
|
||||
archiveClassifier.set("fabric")
|
||||
}
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
package ar.emily.wets.fabric;
|
||||
|
||||
import ar.emily.wets.common.AbstractScheduler;
|
||||
import ar.emily.wets.common.WESpread;
|
||||
import com.mojang.brigadier.Command;
|
||||
import com.mojang.brigadier.CommandDispatcher;
|
||||
import com.mojang.brigadier.context.CommandContext;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import com.mojang.brigadier.tree.LiteralCommandNode;
|
||||
import com.sk89q.worldedit.fabric.FabricAdapter;
|
||||
import me.lucko.fabric.api.permissions.v0.Permissions;
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
|
||||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
|
||||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
|
||||
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import static com.mojang.brigadier.arguments.LongArgumentType.getLong;
|
||||
import static com.mojang.brigadier.arguments.LongArgumentType.longArg;
|
||||
import static net.minecraft.commands.Commands.LEVEL_GAMEMASTERS;
|
||||
import static net.minecraft.commands.Commands.argument;
|
||||
import static net.minecraft.commands.Commands.literal;
|
||||
|
||||
public final class WESpreadMod implements ModInitializer {
|
||||
|
||||
private static final List<String> SORTED = List.of("sorted");
|
||||
private static final List<String> NOT_SORTED = List.of("not-sorted");
|
||||
|
||||
private WESpread plugin;
|
||||
|
||||
@Override
|
||||
public void onInitialize() {
|
||||
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> command(dispatcher));
|
||||
ServerLifecycleEvents.SERVER_STARTING.register(server -> {
|
||||
this.plugin = new WESpread(new AbstractScheduler(server::getTickCount, task -> ServerTickEvents.END_SERVER_TICK.register(s -> task.run())));
|
||||
this.plugin.load();
|
||||
});
|
||||
ServerLifecycleEvents.SERVER_STOPPING.register(server -> this.plugin.flush());
|
||||
ServerPlayConnectionEvents.DISCONNECT.register((packetListener, server) ->
|
||||
this.plugin.playerLogout(packetListener.getPlayer().getUUID())
|
||||
);
|
||||
}
|
||||
|
||||
private int executeCommand(final CommandContext<CommandSourceStack> ctx, final List<String> args) throws CommandSyntaxException {
|
||||
this.plugin.command(FabricAdapter.adaptPlayer(ctx.getSource().getPlayerOrException()), args);
|
||||
return Command.SINGLE_SUCCESS;
|
||||
}
|
||||
|
||||
private void command(final CommandDispatcher<CommandSourceStack> dispatcher) {
|
||||
final Predicate<CommandSourceStack> permissionRequirement = Permissions.require("wets", LEVEL_GAMEMASTERS);
|
||||
final LiteralCommandNode<CommandSourceStack> wetsCommand = dispatcher.register(
|
||||
literal("worldedit-tick-spreader")
|
||||
.requires(permissionRequirement)
|
||||
.then(literal("sorted").executes(ctx -> executeCommand(ctx, SORTED)))
|
||||
.then(literal("not-sorted").executes(ctx -> executeCommand(ctx, NOT_SORTED)))
|
||||
.then(argument("blocks-per-tick", longArg()).executes(ctx ->
|
||||
executeCommand(ctx, List.of(String.valueOf(getLong(ctx, "blocks-per-tick"))))
|
||||
))
|
||||
);
|
||||
|
||||
dispatcher.register(literal("wets").requires(permissionRequirement).redirect(wetsCommand));
|
||||
}
|
||||
}
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 19 KiB |
@@ -1,31 +0,0 @@
|
||||
{
|
||||
"schemaVersion": 1,
|
||||
"id": "worldedit-tick-spreader",
|
||||
"version": "${version}",
|
||||
|
||||
"icon": "assets/worldedit-tick-spreader/icon.png",
|
||||
"name": "WorldEdit Tick Spreader",
|
||||
"authors": [
|
||||
"emilyy-dev"
|
||||
],
|
||||
"contact": {
|
||||
"sources": "https://github.com/emilyy-dev/wets",
|
||||
"issues": "https://github.com/emilyy-dev/wets/issues"
|
||||
},
|
||||
|
||||
"license": "MIT",
|
||||
"environment": "*",
|
||||
"entrypoints": {
|
||||
"main": [
|
||||
"ar.emily.wets.fabric.WESpreadMod"
|
||||
]
|
||||
},
|
||||
|
||||
"depends": {
|
||||
"fabric-permissions-api-v0": "*",
|
||||
"fabricloader": "*",
|
||||
"java": ">=17",
|
||||
"minecraft": "1.19.3",
|
||||
"worldedit": "*"
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -1,4 +1,5 @@
|
||||
org.gradle.jvmargs=-Xmx1G
|
||||
org.gradle.parallel=false
|
||||
org.gradle.caching=false
|
||||
group=ar.emily
|
||||
version=0.0.2
|
||||
version=0.0.4
|
||||
Vendored
BIN
Binary file not shown.
+3
-1
@@ -1,5 +1,7 @@
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
|
||||
networkTimeout=10000
|
||||
validateDistributionUrl=true
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
@@ -55,7 +55,7 @@
|
||||
# Darwin, MinGW, and NonStop.
|
||||
#
|
||||
# (3) This script is generated from the Groovy template
|
||||
# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
|
||||
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
|
||||
# within the Gradle project.
|
||||
#
|
||||
# You can find Gradle at https://github.com/gradle/gradle/.
|
||||
@@ -80,13 +80,11 @@ do
|
||||
esac
|
||||
done
|
||||
|
||||
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
|
||||
|
||||
APP_NAME="Gradle"
|
||||
# This is normally unused
|
||||
# shellcheck disable=SC2034
|
||||
APP_BASE_NAME=${0##*/}
|
||||
|
||||
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
||||
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
|
||||
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit
|
||||
|
||||
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||
MAX_FD=maximum
|
||||
@@ -133,22 +131,29 @@ location of your Java installation."
|
||||
fi
|
||||
else
|
||||
JAVACMD=java
|
||||
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
if ! command -v java >/dev/null 2>&1
|
||||
then
|
||||
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
|
||||
Please set the JAVA_HOME variable in your environment to match the
|
||||
location of your Java installation."
|
||||
fi
|
||||
fi
|
||||
|
||||
# Increase the maximum file descriptors if we can.
|
||||
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
|
||||
case $MAX_FD in #(
|
||||
max*)
|
||||
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
|
||||
# shellcheck disable=SC2039,SC3045
|
||||
MAX_FD=$( ulimit -H -n ) ||
|
||||
warn "Could not query maximum file descriptor limit"
|
||||
esac
|
||||
case $MAX_FD in #(
|
||||
'' | soft) :;; #(
|
||||
*)
|
||||
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
|
||||
# shellcheck disable=SC2039,SC3045
|
||||
ulimit -n "$MAX_FD" ||
|
||||
warn "Could not set maximum file descriptor limit to $MAX_FD"
|
||||
esac
|
||||
@@ -193,11 +198,15 @@ if "$cygwin" || "$msys" ; then
|
||||
done
|
||||
fi
|
||||
|
||||
# Collect all arguments for the java command;
|
||||
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
|
||||
# shell script including quotes and variable substitutions, so put them in
|
||||
# double quotes to make sure that they get re-expanded; and
|
||||
# * put everything else in single quotes, so that it's not re-expanded.
|
||||
|
||||
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
||||
|
||||
# Collect all arguments for the java command:
|
||||
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
|
||||
# and any embedded shellness will be escaped.
|
||||
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
|
||||
# treated as '${Hostname}' itself on the command line.
|
||||
|
||||
set -- \
|
||||
"-Dorg.gradle.appname=$APP_BASE_NAME" \
|
||||
|
||||
Vendored
+11
-10
@@ -26,6 +26,7 @@ if "%OS%"=="Windows_NT" setlocal
|
||||
|
||||
set DIRNAME=%~dp0
|
||||
if "%DIRNAME%"=="" set DIRNAME=.
|
||||
@rem This is normally unused
|
||||
set APP_BASE_NAME=%~n0
|
||||
set APP_HOME=%DIRNAME%
|
||||
|
||||
@@ -42,11 +43,11 @@ set JAVA_EXE=java.exe
|
||||
%JAVA_EXE% -version >NUL 2>&1
|
||||
if %ERRORLEVEL% equ 0 goto execute
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
echo. 1>&2
|
||||
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
|
||||
echo. 1>&2
|
||||
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
|
||||
echo location of your Java installation. 1>&2
|
||||
|
||||
goto fail
|
||||
|
||||
@@ -56,11 +57,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
||||
|
||||
if exist "%JAVA_EXE%" goto execute
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
echo. 1>&2
|
||||
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
|
||||
echo. 1>&2
|
||||
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
|
||||
echo location of your Java installation. 1>&2
|
||||
|
||||
goto fail
|
||||
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
rootProject.name = "WETS-OG"
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
rootProject.name = "worldedit-spread-placing"
|
||||
|
||||
pluginManagement {
|
||||
repositories {
|
||||
maven("https://maven.fabricmc.net/")
|
||||
mavenCentral()
|
||||
gradlePluginPortal()
|
||||
}
|
||||
}
|
||||
|
||||
listOf("common", "bukkit", "fabric").forEach {
|
||||
include(it)
|
||||
project(":$it").name = "wets-$it"
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package ar.emily.wets.bukkit;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Iterator;
|
||||
import java.util.Queue;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.IntSupplier;
|
||||
|
||||
public final class AbstractScheduler implements Scheduler {
|
||||
|
||||
private final IntSupplier currentTickGetter;
|
||||
private final Queue<AbstractTask> pendingTasks = new ArrayDeque<>();
|
||||
private final Queue<AbstractTask> tasks = new ArrayDeque<>();
|
||||
private final Consumer<Runnable> underlyingScheduler;
|
||||
|
||||
public AbstractScheduler(final IntSupplier currentTickGetter, final Consumer<Runnable> underlyingScheduler) {
|
||||
this.currentTickGetter = currentTickGetter;
|
||||
this.underlyingScheduler = underlyingScheduler;
|
||||
}
|
||||
|
||||
public void setup() {
|
||||
this.underlyingScheduler.accept(() -> {
|
||||
processPendingTasks();
|
||||
processTasks();
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void runPeriodically(final Consumer<Task> action, final long initialDelay, final long period) {
|
||||
this.pendingTasks.add(new AbstractTask(action, this.currentTickGetter.getAsInt(), initialDelay, period));
|
||||
}
|
||||
|
||||
public void flush() {
|
||||
this.tasks.addAll(this.pendingTasks);
|
||||
this.pendingTasks.clear();
|
||||
|
||||
final Iterator<AbstractTask> it = this.tasks.iterator();
|
||||
while (it.hasNext()) {
|
||||
final AbstractTask task = it.next();
|
||||
task.task.accept(it::remove);
|
||||
}
|
||||
|
||||
this.tasks.clear();
|
||||
}
|
||||
|
||||
private void processPendingTasks() {
|
||||
final int currentTick = this.currentTickGetter.getAsInt();
|
||||
final Iterator<AbstractTask> it = this.pendingTasks.iterator();
|
||||
while (it.hasNext()) {
|
||||
final AbstractTask task = it.next();
|
||||
if (task.registerTick + task.initialDelay >= currentTick) {
|
||||
this.tasks.add(task);
|
||||
task.registerTick = currentTick;
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void processTasks() {
|
||||
final int currentTick = this.currentTickGetter.getAsInt();
|
||||
final Iterator<AbstractTask> it = this.tasks.iterator();
|
||||
while (it.hasNext()) {
|
||||
final AbstractTask task = it.next();
|
||||
if ((currentTick - task.registerTick) % task.period == 0) {
|
||||
task.task.accept(it::remove);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static final class AbstractTask {
|
||||
|
||||
final Consumer<Task> task;
|
||||
final long initialDelay;
|
||||
final long period;
|
||||
long registerTick;
|
||||
|
||||
AbstractTask(final Consumer<Task> task, final long registerTick, final long initialDelay, final long period) {
|
||||
this.task = task;
|
||||
this.registerTick = registerTick;
|
||||
this.initialDelay = Math.min(initialDelay, 1);
|
||||
this.period = Math.min(period, 1);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package ar.emily.wets.bukkit;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public interface Scheduler {
|
||||
|
||||
void flush();
|
||||
void runPeriodically(Consumer<Task> action, long initialDelay, long period);
|
||||
|
||||
@FunctionalInterface
|
||||
interface Task {
|
||||
|
||||
void cancel();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package ar.emily.wets.bukkit;
|
||||
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
|
||||
public class Utils {
|
||||
|
||||
public static String legacySerializerAnyCase(String subject) {
|
||||
|
||||
int count = 0;
|
||||
// Count the number of '&' characters to determine the size of the array
|
||||
for (char c : subject.toCharArray()) {
|
||||
|
||||
if (c == '&') {
|
||||
|
||||
count++;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Create an array to store the positions of '&' characters
|
||||
int[] positions = new int[count];
|
||||
int index = 0;
|
||||
// Find the positions of '&' characters and store in the array
|
||||
for (int i = 0; i < subject.length(); i++) {
|
||||
|
||||
if (subject.charAt(i) == '&') {
|
||||
|
||||
if (isUpperBukkitCode(subject.charAt(i + 1))) {
|
||||
|
||||
subject = replaceCharAtIndex(subject, (i + 1), Character.toLowerCase(subject.charAt(i + 1)));
|
||||
|
||||
}
|
||||
|
||||
positions[index++] = i;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
String serializedText = LegacyComponentSerializer.legacySection().serialize(LegacyComponentSerializer.legacyAmpersand().deserialize(subject));
|
||||
|
||||
return serializedText;
|
||||
|
||||
}
|
||||
|
||||
private static boolean isUpperBukkitCode(char input) {
|
||||
|
||||
char[] bukkitColorCodes = {'A', 'B', 'C', 'D', 'E', 'F', 'K', 'L', 'M', 'N', 'O', 'R'};
|
||||
boolean match = false;
|
||||
|
||||
// Loop through each character in the array.
|
||||
for (char c : bukkitColorCodes) {
|
||||
// Check if the current character in the array is equal to the input character.
|
||||
if (c == input) {
|
||||
|
||||
match = true;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return match;
|
||||
|
||||
}
|
||||
|
||||
private static String replaceCharAtIndex(String original, int index, char newChar) {
|
||||
|
||||
// Check if the index is valid
|
||||
if (index >= 0 && index < original.length()) {
|
||||
|
||||
// Create a new string with the replaced character
|
||||
return original.substring(0, index) + newChar + original.substring(index + 1);
|
||||
|
||||
}
|
||||
|
||||
// If the index is invalid, return the original string
|
||||
return original;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package ar.emily.wets.bukkit;
|
||||
|
||||
import static java.lang.invoke.MethodHandles.byteArrayViewVarHandle;
|
||||
|
||||
import java.lang.invoke.VarHandle;
|
||||
import java.nio.ByteOrder;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.UUID;
|
||||
|
||||
// why am I overcomplicating this?
|
||||
final class V5UUID {
|
||||
|
||||
private static final VarHandle AS_LONG_ARRAY = byteArrayViewVarHandle(long[].class, ByteOrder.BIG_ENDIAN);
|
||||
private static final MessageDigest MD;
|
||||
|
||||
static {
|
||||
try {
|
||||
MD = MessageDigest.getInstance("SHA-1");
|
||||
} catch (final NoSuchAlgorithmException ex) {
|
||||
// shouldn't throw, SHA-1 is a standard algorithm that is required by Java SE
|
||||
// https://docs.oracle.com/en/java/javase/17/docs/specs/security/standard-names.html
|
||||
throw new ExceptionInInitializerError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
// MessageDigest isn't thread safe, that's why it's being cloned
|
||||
private static MessageDigest md() {
|
||||
try {
|
||||
return (MessageDigest) MD.clone();
|
||||
} catch (final CloneNotSupportedException ex) {
|
||||
// shouldn't throw
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
// use V5 UUID as to not collide with V3 or V4 UUIDs, which are used by the game
|
||||
static UUID create(final byte[] bs) {
|
||||
final var md = md();
|
||||
md.update(bs);
|
||||
final byte[] digest = md.digest();
|
||||
digest[6] &= 0x0f; // clear version
|
||||
digest[6] |= 0x50; // set version 5
|
||||
digest[8] &= 0x3f; // clear variant
|
||||
digest[8] |= (byte) 0x80; // set variant 1
|
||||
return new UUID(
|
||||
(long) AS_LONG_ARRAY.get(digest, 0),
|
||||
(long) AS_LONG_ARRAY.get(digest, 8)
|
||||
);
|
||||
}
|
||||
|
||||
private V5UUID() {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,215 @@
|
||||
package ar.emily.wets.bukkit;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.function.LongSupplier;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.bukkit.configuration.InvalidConfigurationException;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import com.sk89q.worldedit.EditSession;
|
||||
import com.sk89q.worldedit.WorldEdit;
|
||||
import com.sk89q.worldedit.WorldEditException;
|
||||
import com.sk89q.worldedit.event.extent.EditSessionEvent;
|
||||
import com.sk89q.worldedit.event.platform.PlatformUnreadyEvent;
|
||||
import com.sk89q.worldedit.extension.platform.Actor;
|
||||
import com.sk89q.worldedit.extent.AbstractBufferingExtent;
|
||||
import com.sk89q.worldedit.extent.Extent;
|
||||
import com.sk89q.worldedit.function.operation.Operation;
|
||||
import com.sk89q.worldedit.function.operation.RunContext;
|
||||
import com.sk89q.worldedit.math.BlockVector3;
|
||||
import com.sk89q.worldedit.util.Identifiable;
|
||||
import com.sk89q.worldedit.util.collection.BlockMap;
|
||||
import com.sk89q.worldedit.util.eventbus.EventHandler;
|
||||
import com.sk89q.worldedit.util.eventbus.Subscribe;
|
||||
import com.sk89q.worldedit.util.formatting.text.Component;
|
||||
import com.sk89q.worldedit.util.formatting.text.TextComponent;
|
||||
import com.sk89q.worldedit.world.block.BaseBlock;
|
||||
import com.sk89q.worldedit.world.block.BlockStateHolder;
|
||||
|
||||
import it.unimi.dsi.fastutil.objects.Object2LongMap;
|
||||
import it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap;
|
||||
|
||||
public final class WESpread {
|
||||
|
||||
public static final UUID NON_PLAYER_ACTOR_ID =
|
||||
V5UUID.create(WESpread.class.descriptorString().getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
private AbstractScheduler scheduler;
|
||||
|
||||
File configFile = new File("plugins/WETS-OG/config.yml");
|
||||
|
||||
public static FileConfiguration convertFileToConfig(File file) {
|
||||
FileConfiguration config = YamlConfiguration.loadConfiguration(file);
|
||||
|
||||
try {
|
||||
config.load(file);
|
||||
} catch (IOException | InvalidConfigurationException error) {
|
||||
error.printStackTrace();
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
FileConfiguration configFileYAML = convertFileToConfig(configFile);
|
||||
|
||||
public WESpread(AbstractScheduler scheduler) {
|
||||
this.scheduler = scheduler;
|
||||
}
|
||||
|
||||
private final Component INVALID_ARGUMENT = TextComponent.of(Utils.legacySerializerAnyCase(Utils.legacySerializerAnyCase(configFileYAML.getString("ChatPrefix") + "&cERROR: Blocks per tick must be a valid positive number!")));
|
||||
private final Component COMMAND_USAGE = TextComponent.of(Utils.legacySerializerAnyCase(configFileYAML.getString("ChatPrefix") + "&6Usage: &e/wets (<blocks per tick> | sorted | not-sorted)"));
|
||||
private final Component COMMAND_SORTED = TextComponent.of(Utils.legacySerializerAnyCase(configFileYAML.getString("ChatPrefix") + "&6Block placement of new operations will now be &esorted&6."));
|
||||
private final Component COMMAND_NOT_SORTED = TextComponent.of(Utils.legacySerializerAnyCase(configFileYAML.getString("ChatPrefix") + "&6Block placement of new operations will now be &eunsorted&6."));
|
||||
private final Component COMMAND_BPT_UPDATED = TextComponent.of(Utils.legacySerializerAnyCase(configFileYAML.getString("ChatPrefix") + "&aBlocks per tick placement set to: "));
|
||||
|
||||
private final Object2LongMap<UUID> blocksPerTickMap = new Object2LongOpenHashMap<>();
|
||||
private final Set<UUID> actorsWhosePlacementIsNotSorted = new HashSet<>();
|
||||
private long defaultBlocksPerTick = configFileYAML.getInt("DefaultBlocksPerTick");
|
||||
|
||||
public void playerLogout(final UUID id) {
|
||||
this.blocksPerTickMap.removeLong(id);
|
||||
this.actorsWhosePlacementIsNotSorted.remove(id);
|
||||
}
|
||||
|
||||
public void load() {
|
||||
WorldEdit.getInstance().getEventBus().register(this);
|
||||
}
|
||||
|
||||
// We really really really want to flush the queue before WorldEdit itself unloads.
|
||||
@Subscribe(priority = EventHandler.Priority.VERY_EARLY)
|
||||
public void on(final PlatformUnreadyEvent event) {
|
||||
WorldEdit.getInstance().getEventBus().unregister(this);
|
||||
this.defaultBlocksPerTick = Long.MAX_VALUE;
|
||||
this.blocksPerTickMap.clear();
|
||||
this.actorsWhosePlacementIsNotSorted.clear();
|
||||
this.scheduler.flush();
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void on(final EditSessionEvent event) {
|
||||
if (event.getStage() == EditSession.Stage.BEFORE_CHANGE) {
|
||||
final UUID actorId =
|
||||
Optional.ofNullable(event.getActor())
|
||||
// use a set ID for non-player actors, such as console or potentially API users
|
||||
.filter(Actor::isPlayer)
|
||||
.map(Identifiable::getUniqueId)
|
||||
.orElse(NON_PLAYER_ACTOR_ID);
|
||||
event.setExtent(new SchedulingExtent(event.getExtent(), actorId));
|
||||
}
|
||||
}
|
||||
|
||||
public void command(final Actor source, final List<String> args) {
|
||||
if (args.size() != 1) {
|
||||
source.print(COMMAND_USAGE);
|
||||
return;
|
||||
}
|
||||
|
||||
final UUID id = source.isPlayer() ? source.getUniqueId() : NON_PLAYER_ACTOR_ID;
|
||||
final String arg = args.iterator().next();
|
||||
if ("sorted".equals(arg)) {
|
||||
this.actorsWhosePlacementIsNotSorted.remove(id);
|
||||
source.print(COMMAND_SORTED);
|
||||
return;
|
||||
} else if ("not-sorted".equals(arg)) {
|
||||
this.actorsWhosePlacementIsNotSorted.add(id);
|
||||
source.print(COMMAND_NOT_SORTED);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
long blocksPerTick = Long.parseLong(arg);
|
||||
if (blocksPerTick < 0) { blocksPerTick = Long.MAX_VALUE; }
|
||||
this.blocksPerTickMap.put(id, blocksPerTick);
|
||||
configFileYAML.set("DefaultBlocksPerTick", blocksPerTick);
|
||||
try {
|
||||
configFileYAML.save(configFile);
|
||||
}
|
||||
catch (IOException error) {
|
||||
error.printStackTrace();
|
||||
}
|
||||
source.print(COMMAND_BPT_UPDATED.append(TextComponent.of(Utils.legacySerializerAnyCase("&e" + String.valueOf(blocksPerTick)))));
|
||||
}
|
||||
catch (final NumberFormatException exception) {
|
||||
source.printError(INVALID_ARGUMENT);
|
||||
source.print(COMMAND_USAGE);
|
||||
}
|
||||
}
|
||||
|
||||
private final class SchedulingExtent extends AbstractBufferingExtent {
|
||||
|
||||
private final BlockMap<BaseBlock> buffer = BlockMap.createForBaseBlock();
|
||||
private final LongSupplier blocksPerTick;
|
||||
private final boolean sorted;
|
||||
|
||||
private SchedulingExtent(final Extent delegate, final UUID actor) {
|
||||
super(delegate);
|
||||
this.sorted = ! WESpread.this.actorsWhosePlacementIsNotSorted.contains(actor);
|
||||
this.blocksPerTick = () -> WESpread.this.blocksPerTickMap.getOrDefault(actor, WESpread.this.defaultBlocksPerTick);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @Nullable BaseBlock getBufferedFullBlock(final BlockVector3 position) {
|
||||
return this.buffer.computeIfAbsent(position, getExtent()::getFullBlock);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends BlockStateHolder<T>> boolean setBlock(final BlockVector3 position, final T block) {
|
||||
this.buffer.remove(position); // workaround silly WorldEdit bug, see https://github.com/emilyy-dev/wets/issues/1
|
||||
this.buffer.put(position, block.toBaseBlock());
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Operation commitBefore() {
|
||||
final Iterator<Map.Entry<BlockVector3, BaseBlock>> it;
|
||||
{
|
||||
Stream<Map.Entry<BlockVector3, BaseBlock>> stream = this.buffer.entrySet().stream();
|
||||
if (this.sorted) { stream = stream.sorted(Map.Entry.comparingByKey(BlockVector3.sortByCoordsYzx())); }
|
||||
it = stream.iterator();
|
||||
}
|
||||
|
||||
return new Operation() {
|
||||
boolean started = false;
|
||||
|
||||
@Override
|
||||
public Operation resume(final RunContext run) {
|
||||
if (! this.started && it.hasNext()) {
|
||||
this.started = true;
|
||||
WESpread.this.scheduler.runPeriodically(this::putBlock, 1L, 1L);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private void putBlock(final Scheduler.Task task) {
|
||||
try {
|
||||
for (long i = 0; it.hasNext() && i < SchedulingExtent.this.blocksPerTick.getAsLong(); ++i) {
|
||||
final var entry = it.next();
|
||||
setDelegateBlock(entry.getKey(), entry.getValue());
|
||||
}
|
||||
} catch (final WorldEditException exception) {
|
||||
throw new RuntimeException(exception);
|
||||
} finally {
|
||||
if (! it.hasNext()) { task.cancel(); }
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancel() {
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package ar.emily.wets.bukkit;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.util.StringUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import com.sk89q.worldedit.bukkit.BukkitAdapter;
|
||||
|
||||
public final class WESpreadPlugin extends JavaPlugin implements Listener {
|
||||
|
||||
private static final List<String> COMPLETIONS = List.of("sorted", "not-sorted");
|
||||
|
||||
private final AbstractScheduler scheduler =
|
||||
new AbstractScheduler(
|
||||
getServer()::getCurrentTick,
|
||||
task -> getServer().getScheduler().runTaskTimer(this, task, 1L, 1L)
|
||||
);
|
||||
private final WESpread wetsSpread = new WESpread(this.scheduler);
|
||||
|
||||
@EventHandler
|
||||
public void on(final PlayerQuitEvent event) {
|
||||
this.wetsSpread.playerLogout(event.getPlayer().getUniqueId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoad() {
|
||||
this.wetsSpread.load();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
saveDefaultConfig();
|
||||
this.scheduler.setup();
|
||||
getServer().getPluginManager().registerEvents(this, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(
|
||||
final @NotNull CommandSender sender,
|
||||
final @NotNull Command command,
|
||||
final @NotNull String label,
|
||||
final String @NotNull [] args
|
||||
) {
|
||||
this.wetsSpread.command(BukkitAdapter.adapt(sender), Arrays.asList(args));
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull List<String> onTabComplete(
|
||||
final @NotNull CommandSender sender,
|
||||
final @NotNull Command command,
|
||||
final @NotNull String alias,
|
||||
final String @NotNull [] args
|
||||
) {
|
||||
return args.length == 1 ? StringUtil.copyPartialMatches(args[0], COMPLETIONS, new ArrayList<>(1)) : List.of();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
# Set the chat prefix (using bukkit color codes).
|
||||
ChatPrefix: "&7[&eWETS&f-&4OG&7] "
|
||||
# Set the amount of blocks to update per tick (high values may cause lag).
|
||||
DefaultBlocksPerTick: 100
|
||||
@@ -1,8 +1,8 @@
|
||||
name: WorldEditTickSpreader
|
||||
name: WETS-OG
|
||||
version: ${version}
|
||||
website: https://github.com/emilyy-dev/wets
|
||||
website: https://github.com/NotAlexNoyle/WETS-OG
|
||||
main: ar.emily.wets.bukkit.WESpreadPlugin
|
||||
api-version: 1.13
|
||||
api-version: 1.19
|
||||
depend:
|
||||
- WorldEdit
|
||||
|
||||
Reference in new issue
Block a user