diff --git a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/NMSRelighterFactory.java b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/NMSRelighterFactory.java new file mode 100644 index 000000000..0a2f31a03 --- /dev/null +++ b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/NMSRelighterFactory.java @@ -0,0 +1,18 @@ +package com.boydti.fawe.bukkit; + +import com.boydti.fawe.beta.IQueueChunk; +import com.boydti.fawe.beta.IQueueExtent; +import com.boydti.fawe.beta.implementation.lighting.NMSRelighter; +import com.boydti.fawe.beta.implementation.lighting.Relighter; +import com.boydti.fawe.beta.implementation.lighting.RelighterFactory; +import com.boydti.fawe.config.Settings; +import com.boydti.fawe.object.RelightMode; +import com.sk89q.worldedit.world.World; + +public class NMSRelighterFactory implements RelighterFactory { + @Override + public Relighter createRelighter(RelightMode relightMode, World world, IQueueExtent queue) { + return new NMSRelighter(queue, Settings.IMP.LIGHTING.DO_HEIGHTMAPS, + relightMode != null ? relightMode : RelightMode.valueOf(Settings.IMP.LIGHTING.MODE)); + } +} diff --git a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_5/TuinityRelighterFactory_1_16_5.java b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_5/TuinityRelighterFactory_1_16_5.java new file mode 100644 index 000000000..90eeef210 --- /dev/null +++ b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_5/TuinityRelighterFactory_1_16_5.java @@ -0,0 +1,20 @@ +package com.boydti.fawe.bukkit.adapter.mc1_16_5; + +import com.boydti.fawe.beta.IQueueChunk; +import com.boydti.fawe.beta.IQueueExtent; +import com.boydti.fawe.beta.implementation.lighting.NullRelighter; +import com.boydti.fawe.beta.implementation.lighting.Relighter; +import com.boydti.fawe.beta.implementation.lighting.RelighterFactory; +import com.boydti.fawe.object.RelightMode; +import com.sk89q.worldedit.world.World; +import org.bukkit.Bukkit; +import org.bukkit.craftbukkit.v1_16_R3.CraftWorld; + +public class TuinityRelighterFactory_1_16_5 implements RelighterFactory { + @Override + public Relighter createRelighter(RelightMode relightMode, World world, IQueueExtent queue) { + org.bukkit.World w = Bukkit.getWorld(world.getName()); + if (w == null) return NullRelighter.INSTANCE; + return new TuinityRelighter_1_16_5(((CraftWorld) w).getHandle(), queue); + } +} diff --git a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_5/TuinityRelighter_1_16_5.java b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_5/TuinityRelighter_1_16_5.java new file mode 100644 index 000000000..080d9af25 --- /dev/null +++ b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_5/TuinityRelighter_1_16_5.java @@ -0,0 +1,125 @@ +package com.boydti.fawe.bukkit.adapter.mc1_16_5; + +import com.boydti.fawe.Fawe; +import com.boydti.fawe.beta.IQueueChunk; +import com.boydti.fawe.beta.IQueueExtent; +import com.boydti.fawe.beta.implementation.chunk.ChunkHolder; +import com.boydti.fawe.beta.implementation.lighting.Relighter; +import com.boydti.fawe.util.TaskManager; +import net.minecraft.server.v1_16_R3.BlockPosition; +import net.minecraft.server.v1_16_R3.ChunkCoordIntPair; +import net.minecraft.server.v1_16_R3.MCUtil; +import net.minecraft.server.v1_16_R3.WorldServer; + +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; +import java.lang.reflect.Method; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.concurrent.locks.ReentrantLock; +import java.util.function.Consumer; +import java.util.function.IntConsumer; + +public class TuinityRelighter_1_16_5 implements Relighter { + + private static final IntConsumer nothingIntConsumer = i -> {}; + + private final WorldServer world; + private final MethodHandle relight; + private final ReentrantLock lock = new ReentrantLock(); + + private final IQueueExtent queue; + + public TuinityRelighter_1_16_5(WorldServer world, IQueueExtent queue) { + this.queue = queue; + MethodHandle methodHandle = null; + try { + Method relightMethod = world.getChunkProvider().getLightEngine().getClass().getMethod( + "relight", + Set.class, + Consumer.class, + IntConsumer.class + ); + methodHandle = MethodHandles.lookup().unreflect(relightMethod); + } catch (NoSuchMethodException | IllegalAccessException e) { + e.printStackTrace(); + } + this.relight = methodHandle; + this.world = world; + } + + @Override + public boolean addChunk(int cx, int cz, byte[] skipReason, int bitmask) { + List chunks = MCUtil.getSpiralOutChunks(new BlockPosition(cx << 4, 0, cz << 4), 1); + TaskManager.IMP.task(() -> + { + try { + relight.invoke(world.getChunkProvider().getLightEngine(), + new HashSet<>(chunks), + (Consumer) coord -> sendChunk(cx, cz), // send chunk after lighting was done + nothingIntConsumer + ); + } catch (Throwable throwable) { + throwable.printStackTrace(); + } + } + ); + return true; + } + + private void sendChunk(int cx, int cz) { + ChunkHolder chunk = (ChunkHolder) queue.getOrCreateChunk(cx, cz); + Fawe.imp().getPlatformAdapter().sendChunk(chunk.getOrCreateGet(), -1, false); + } + + @Override + public void addLightUpdate(int x, int y, int z) { + + } + + @Override + public void fixLightingSafe(boolean sky) { + + } + + @Override + public void clear() { + + } + + @Override + public void removeLighting() { + + } + + @Override + public void fixBlockLighting() { + + } + + @Override + public void fixSkyLighting() { + + } + + @Override + public boolean isEmpty() { + return true; + } + + @Override + public ReentrantLock getLock() { + return this.lock; + } + + @Override + public boolean isFinished() { + return true; + } + + @Override + public void close() throws Exception { + + } +} diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitServerInterface.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitServerInterface.java index ce2c246e3..187a18b69 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitServerInterface.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitServerInterface.java @@ -19,6 +19,9 @@ package com.sk89q.worldedit.bukkit; +import com.boydti.fawe.beta.implementation.lighting.RelighterFactory; +import com.boydti.fawe.bukkit.NMSRelighterFactory; +import com.boydti.fawe.bukkit.adapter.mc1_16_5.TuinityRelighterFactory_1_16_5; import com.google.common.collect.Sets; import com.sk89q.bukkit.util.CommandInfo; import com.sk89q.bukkit.util.CommandRegistration; @@ -60,6 +63,7 @@ public class BukkitServerInterface extends AbstractPlatform implements MultiUser public final WorldEditPlugin plugin; private final CommandRegistration dynamicCommands; private final LazyReference watchdog; + private final RelighterFactory religherFactory; private boolean hookingEvents; public BukkitServerInterface(WorldEditPlugin plugin, Server server) { @@ -74,6 +78,14 @@ public class BukkitServerInterface extends AbstractPlatform implements MultiUser } return null; }); + RelighterFactory tempFactory; + try { + Class.forName("com.tuinity.tuinity.config.TuinityConfig"); + tempFactory = new TuinityRelighterFactory_1_16_5(); + } catch (ClassNotFoundException e) { + tempFactory = new NMSRelighterFactory(); + } + this.religherFactory = tempFactory; } CommandRegistration getDynamicCommands() { @@ -244,6 +256,11 @@ public class BukkitServerInterface extends AbstractPlatform implements MultiUser return SUPPORTED_SIDE_EFFECTS; } + @Override + public RelighterFactory getRelighterFactory() { + return this.religherFactory; + } + public void unregisterCommands() { dynamicCommands.unregisterCommands(); } diff --git a/worldedit-cli/src/main/java/com/sk89q/worldedit/cli/CLIPlatform.java b/worldedit-cli/src/main/java/com/sk89q/worldedit/cli/CLIPlatform.java index d620c4da9..ef22e1b57 100644 --- a/worldedit-cli/src/main/java/com/sk89q/worldedit/cli/CLIPlatform.java +++ b/worldedit-cli/src/main/java/com/sk89q/worldedit/cli/CLIPlatform.java @@ -19,6 +19,8 @@ package com.sk89q.worldedit.cli; +import com.boydti.fawe.beta.implementation.lighting.NullRelighter; +import com.boydti.fawe.beta.implementation.lighting.RelighterFactory; import com.google.common.collect.ImmutableSet; import com.sk89q.worldedit.entity.Player; import com.sk89q.worldedit.extension.platform.AbstractPlatform; @@ -160,6 +162,11 @@ class CLIPlatform extends AbstractPlatform { return ImmutableSet.of(); } + @Override + public RelighterFactory getRelighterFactory() { + return (_a, _b, _c) -> NullRelighter.INSTANCE; + } + public void addWorld(World world) { worlds.add(world); } diff --git a/worldedit-core/src/main/java/com/boydti/fawe/FaweAPI.java b/worldedit-core/src/main/java/com/boydti/fawe/FaweAPI.java index 30dedb3f0..9b1305658 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/FaweAPI.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/FaweAPI.java @@ -2,7 +2,7 @@ package com.boydti.fawe; import com.boydti.fawe.beta.IQueueChunk; import com.boydti.fawe.beta.IQueueExtent; -import com.boydti.fawe.beta.implementation.lighting.NMSRelighter; +import com.boydti.fawe.beta.implementation.lighting.Relighter; import com.boydti.fawe.beta.implementation.queue.ParallelQueueExtent; import com.boydti.fawe.config.Settings; import com.boydti.fawe.object.RegionWrapper; @@ -35,6 +35,7 @@ import com.sk89q.worldedit.util.Location; import com.sk89q.worldedit.util.formatting.text.Component; import com.sk89q.worldedit.world.World; +import javax.annotation.Nullable; import java.io.File; import java.io.IOException; import java.net.URL; @@ -45,7 +46,6 @@ import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.UUID; -import javax.annotation.Nullable; /** * The FaweAPI class offers a few useful functions.
@@ -344,24 +344,29 @@ public class FaweAPI { } } - NMSRelighter relighter = new NMSRelighter(queue, Settings.IMP.LIGHTING.DO_HEIGHTMAPS); - for (int x = minX; x <= maxX; x++) { - for (int z = minZ; z <= maxZ; z++) { - relighter.addChunk(x, z, null, 65535); - count++; + try (Relighter relighter = WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.WORLD_EDITING) + .getRelighterFactory() + .createRelighter(mode, world, queue)) { + + for (int x = minX; x <= maxX; x++) { + for (int z = minZ; z <= maxZ; z++) { + relighter.addChunk(x, z, null, 65535); + count++; + } } - } - if (mode != RelightMode.NONE) { - if (Settings.IMP.LIGHTING.REMOVE_FIRST) { - relighter.removeAndRelight(true); + if (mode != RelightMode.NONE) { + if (Settings.IMP.LIGHTING.REMOVE_FIRST) { + relighter.removeAndRelight(true); + } else { + relighter.fixSkyLighting(); + relighter.fixBlockLighting(); + } } else { - relighter.fixSkyLighting(); - relighter.fixBlockLighting(); + relighter.removeLighting(); } - } else { - relighter.removeLighting(); + } catch (Exception ignored) { + } - relighter.flush(); return count; } diff --git a/worldedit-core/src/main/java/com/boydti/fawe/beta/implementation/lighting/NMSRelighter.java b/worldedit-core/src/main/java/com/boydti/fawe/beta/implementation/lighting/NMSRelighter.java index ecde93ef5..dab5face3 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/beta/implementation/lighting/NMSRelighter.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/beta/implementation/lighting/NMSRelighter.java @@ -823,7 +823,8 @@ public class NMSRelighter implements Relighter { } } - public synchronized void flush() { + @Override + public synchronized void close() { Iterator> iter = chunksToSend.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = iter.next(); diff --git a/worldedit-core/src/main/java/com/boydti/fawe/beta/implementation/lighting/NullRelighter.java b/worldedit-core/src/main/java/com/boydti/fawe/beta/implementation/lighting/NullRelighter.java index 1c345df1b..8798e8023 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/beta/implementation/lighting/NullRelighter.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/beta/implementation/lighting/NullRelighter.java @@ -58,4 +58,9 @@ public class NullRelighter implements Relighter { public boolean isFinished() { return true; } + + @Override + public void close() throws Exception { + + } } diff --git a/worldedit-core/src/main/java/com/boydti/fawe/beta/implementation/lighting/Relighter.java b/worldedit-core/src/main/java/com/boydti/fawe/beta/implementation/lighting/Relighter.java index 61fbc2d87..bdf776c0f 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/beta/implementation/lighting/Relighter.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/beta/implementation/lighting/Relighter.java @@ -2,7 +2,7 @@ package com.boydti.fawe.beta.implementation.lighting; import java.util.concurrent.locks.ReentrantLock; -public interface Relighter { +public interface Relighter extends AutoCloseable { /** * Add a chunk to be relit when {@link Relighter#removeLighting} etc are called. diff --git a/worldedit-core/src/main/java/com/boydti/fawe/beta/implementation/lighting/RelighterFactory.java b/worldedit-core/src/main/java/com/boydti/fawe/beta/implementation/lighting/RelighterFactory.java new file mode 100644 index 000000000..c944a039d --- /dev/null +++ b/worldedit-core/src/main/java/com/boydti/fawe/beta/implementation/lighting/RelighterFactory.java @@ -0,0 +1,12 @@ +package com.boydti.fawe.beta.implementation.lighting; + +import com.boydti.fawe.beta.IQueueChunk; +import com.boydti.fawe.beta.IQueueExtent; +import com.boydti.fawe.object.RelightMode; +import com.sk89q.worldedit.world.World; + +@FunctionalInterface +public interface RelighterFactory { + + Relighter createRelighter(RelightMode relightMode, World world, IQueueExtent queue); +} diff --git a/worldedit-core/src/main/java/com/boydti/fawe/util/EditSessionBuilder.java b/worldedit-core/src/main/java/com/boydti/fawe/util/EditSessionBuilder.java index f2062d50c..670f7d84b 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/util/EditSessionBuilder.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/util/EditSessionBuilder.java @@ -34,6 +34,7 @@ import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.entity.Player; import com.sk89q.worldedit.event.extent.EditSessionEvent; +import com.sk89q.worldedit.extension.platform.Capability; import com.sk89q.worldedit.extent.AbstractDelegateExtent; import com.sk89q.worldedit.extent.Extent; import com.sk89q.worldedit.extent.inventory.BlockBag; @@ -405,8 +406,9 @@ public class EditSessionBuilder { } // There's no need to do lighting (and it'll also just be a pain to implement) if we're not placing chunks if (placeChunks && ((relightMode != null && relightMode != RelightMode.NONE) || (relightMode == null && Settings.IMP.LIGHTING.MODE > 0))) { - relighter = new NMSRelighter(queue, Settings.IMP.LIGHTING.DO_HEIGHTMAPS, - relightMode != null ? relightMode : RelightMode.valueOf(Settings.IMP.LIGHTING.MODE)); + relighter = WorldEdit.getInstance().getPlatformManager() + .queryCapability(Capability.WORLD_EDITING) + .getRelighterFactory().createRelighter(relightMode, world, queue); extent.addProcessor(new RelightProcessor(relighter)); } else { relighter = NullRelighter.INSTANCE; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/Platform.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/Platform.java index 709492a97..2b7a6d792 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/Platform.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/Platform.java @@ -19,6 +19,7 @@ package com.sk89q.worldedit.extension.platform; +import com.boydti.fawe.beta.implementation.lighting.RelighterFactory; import com.sk89q.worldedit.LocalConfiguration; import com.sk89q.worldedit.entity.Player; import com.sk89q.worldedit.internal.util.NonAbstractForCompatibility; @@ -206,4 +207,6 @@ public interface Platform extends Keyed { * @return A set of supported side effects */ Set getSupportedSideEffects(); + + RelighterFactory getRelighterFactory(); }