From 1552a8e74bc92ffc395cbe361811f5f23e2a4f19 Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Fri, 11 Sep 2020 14:18:50 +0100 Subject: [PATCH] Add option to change lighting behaviour in queues --- .../bukkit/queue/BukkitQueueCoordinator.java | 26 +++++++-- .../core/configuration/Settings.java | 6 ++ .../core/queue/BasicQueueCoordinator.java | 15 ++++- .../core/queue/DelegateQueueCoordinator.java | 16 +++++- .../plotsquared/core/queue/LightingMode.java | 57 +++++++++++++++++++ .../core/queue/QueueCoordinator.java | 12 ++++ 6 files changed, 126 insertions(+), 6 deletions(-) create mode 100644 Core/src/main/java/com/plotsquared/core/queue/LightingMode.java diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/queue/BukkitQueueCoordinator.java b/Bukkit/src/main/java/com/plotsquared/bukkit/queue/BukkitQueueCoordinator.java index ad5f66c3f..e9b463a9b 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/queue/BukkitQueueCoordinator.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/queue/BukkitQueueCoordinator.java @@ -63,7 +63,8 @@ import java.util.function.Consumer; public class BukkitQueueCoordinator extends BasicQueueCoordinator { - private final SideEffectSet sideEffectSet; + private final SideEffectSet noSideEffectSet; + private final SideEffectSet lightingSideEffectSet; private org.bukkit.World bukkitWorld; @Inject private ChunkCoordinatorBuilderFactory chunkCoordinatorBuilderFactory; @Inject private ChunkCoordinatorFactory chunkCoordinatorFactory; @@ -71,7 +72,8 @@ public class BukkitQueueCoordinator extends BasicQueueCoordinator { @Inject public BukkitQueueCoordinator(@Nonnull World world) { super(world); - sideEffectSet = SideEffectSet.none().with(SideEffect.LIGHTING, SideEffect.State.OFF).with(SideEffect.NEIGHBORS, SideEffect.State.OFF); + noSideEffectSet = SideEffectSet.none().with(SideEffect.LIGHTING, SideEffect.State.OFF).with(SideEffect.NEIGHBORS, SideEffect.State.OFF); + lightingSideEffectSet = SideEffectSet.none().with(SideEffect.NEIGHBORS, SideEffect.State.OFF); } @Override public BlockState getBlock(int x, int y, int z) { @@ -177,7 +179,7 @@ public class BukkitQueueCoordinator extends BasicQueueCoordinator { localChunk.getTiles().forEach(((blockVector3, tag) -> { try { BaseBlock block = getWorld().getBlock(blockVector3).toBaseBlock(tag); - getWorld().setBlock(blockVector3, block, sideEffectSet); + getWorld().setBlock(blockVector3, block, noSideEffectSet); } catch (WorldEditException ignored) { StateWrapper sw = new StateWrapper(tag); sw.restoreTag(getWorld().getName(), blockVector3.getX(), blockVector3.getY(), blockVector3.getZ()); @@ -205,7 +207,23 @@ public class BukkitQueueCoordinator extends BasicQueueCoordinator { */ private void setWorldBlock(int x, int y, int z, @Nonnull BaseBlock block, @Nonnull BlockVector2 blockVector2) { try { - getWorld().setBlock(BlockVector3.at(x, y, z), block, sideEffectSet); + BlockVector3 loc = BlockVector3.at(x, y, z); + boolean lighting = false; + switch (getLightingMode()) { + case NONE: + break; + case PLACEMENT: + lighting = block.getBlockType().getMaterial().getLightValue() > 0; + break; + case REPLACEMENT: + lighting = block.getBlockType().getMaterial().getLightValue() > 0 + || getWorld().getBlock(loc).getBlockType().getMaterial().getLightValue() > 0; + break; + default: + // Can only be "all" + lighting = true; + } + getWorld().setBlock(loc, block, lighting ? lightingSideEffectSet : noSideEffectSet); } catch (WorldEditException ignored) { // Fallback to not so nice method BlockData blockData = BukkitAdapter.adapt(block); diff --git a/Core/src/main/java/com/plotsquared/core/configuration/Settings.java b/Core/src/main/java/com/plotsquared/core/configuration/Settings.java index 53edf3864..aa1a1e512 100644 --- a/Core/src/main/java/com/plotsquared/core/configuration/Settings.java +++ b/Core/src/main/java/com/plotsquared/core/configuration/Settings.java @@ -543,6 +543,12 @@ public class Settings extends Config { @Comment({"Time to wait in ms before beginning to notify player or console of progress.", "Prevent needless notification of progress for short queues."}) public static int NOTIFY_WAIT = 5000; + @Comment({"How lighitng should be handled by the queue. Modes:", + " - 0 - Do not do any lighting (fastest)", + " - 1 - Only execute lighting where blocks with light values are placed", + " - 2 - Only execute lighting where blocks with light values are placed or removed/replaced", + " - 3 - Always execute lighting (slowest)"}) + public static int LIGHTING_MODE = 0; } @Comment("Settings related to tab completion") diff --git a/Core/src/main/java/com/plotsquared/core/queue/BasicQueueCoordinator.java b/Core/src/main/java/com/plotsquared/core/queue/BasicQueueCoordinator.java index f2d436962..7b57146fa 100644 --- a/Core/src/main/java/com/plotsquared/core/queue/BasicQueueCoordinator.java +++ b/Core/src/main/java/com/plotsquared/core/queue/BasicQueueCoordinator.java @@ -25,6 +25,7 @@ */ package com.plotsquared.core.queue; +import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.queue.subscriber.ProgressSubscriber; import com.plotsquared.core.util.PatternUtil; import com.sk89q.jnbt.CompoundTag; @@ -55,6 +56,7 @@ public abstract class BasicQueueCoordinator extends QueueCoordinator { private final World world; private final ConcurrentHashMap blockChunks = new ConcurrentHashMap<>(); private final List readRegion = new ArrayList<>(); + private final List progressSubscribers = new ArrayList<>(); private long modified; private LocalChunk lastWrappedChunk; private int lastX = Integer.MIN_VALUE; @@ -68,7 +70,7 @@ public abstract class BasicQueueCoordinator extends QueueCoordinator { private Consumer consumer = null; private boolean unloadAfter = true; private Runnable whenDone; - private List progressSubscribers = new ArrayList<>(); + @Nullable private LightingMode lightingMode = LightingMode.valueOf(Settings.QUEUE.LIGHTING_MODE); public BasicQueueCoordinator(@Nonnull World world) { super(world); @@ -265,6 +267,17 @@ public abstract class BasicQueueCoordinator extends QueueCoordinator { this.progressSubscribers.add(progressSubscriber); } + @Override @Nonnull public final LightingMode getLightingMode() { + if (lightingMode == null) { + return LightingMode.valueOf(Settings.QUEUE.LIGHTING_MODE); + } + return this.lightingMode; + } + + @Override public final void setLightingMode(@Nullable LightingMode mode) { + this.lightingMode = mode; + } + @Override public Runnable getCompleteTask() { return this.whenDone; } diff --git a/Core/src/main/java/com/plotsquared/core/queue/DelegateQueueCoordinator.java b/Core/src/main/java/com/plotsquared/core/queue/DelegateQueueCoordinator.java index 21dee819f..8783d494f 100644 --- a/Core/src/main/java/com/plotsquared/core/queue/DelegateQueueCoordinator.java +++ b/Core/src/main/java/com/plotsquared/core/queue/DelegateQueueCoordinator.java @@ -25,6 +25,7 @@ */ package com.plotsquared.core.queue; +import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.queue.subscriber.ProgressSubscriber; import com.sk89q.jnbt.CompoundTag; import com.sk89q.worldedit.entity.Entity; @@ -191,7 +192,7 @@ public class DelegateQueueCoordinator extends QueueCoordinator { } } - @Nullable @Override public Consumer getChunkConsumer() { + @Override @Nullable public Consumer getChunkConsumer() { if (parent != null) { return parent.getChunkConsumer(); } @@ -210,6 +211,19 @@ public class DelegateQueueCoordinator extends QueueCoordinator { } } + @Override @Nonnull public LightingMode getLightingMode() { + if (parent != null) { + return parent.getLightingMode(); + } + return LightingMode.valueOf(Settings.QUEUE.LIGHTING_MODE); + } + + @Override public void setLightingMode(@Nullable LightingMode mode) { + if (parent != null) { + parent.setLightingMode(mode); + } + } + @Override @Nonnull public List getReadChunks() { if (parent != null) { return parent.getReadChunks(); diff --git a/Core/src/main/java/com/plotsquared/core/queue/LightingMode.java b/Core/src/main/java/com/plotsquared/core/queue/LightingMode.java new file mode 100644 index 000000000..221bd7b64 --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/queue/LightingMode.java @@ -0,0 +1,57 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * PlotSquared plot management system for Minecraft + * Copyright (C) ${year} IntellectualSites + * + * 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 + * (at your option) any later version. + * + * 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. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.plotsquared.core.queue; + +import java.util.HashMap; +import java.util.Map; + +public enum LightingMode { + + NONE(0), PLACEMENT(1), REPLACEMENT(2), ALL(3); + + private static final Map map = new HashMap<>(); + + static { + for (LightingMode mode : LightingMode.values()) { + map.put(mode.mode, mode); + } + } + + private final int mode; + + LightingMode(int mode) { + this.mode = mode; + } + + public static LightingMode valueOf(int mode) { + return map.get(mode); + } + + public int getMode() { + return mode; + } +} diff --git a/Core/src/main/java/com/plotsquared/core/queue/QueueCoordinator.java b/Core/src/main/java/com/plotsquared/core/queue/QueueCoordinator.java index d40efce4b..3580a9dbe 100644 --- a/Core/src/main/java/com/plotsquared/core/queue/QueueCoordinator.java +++ b/Core/src/main/java/com/plotsquared/core/queue/QueueCoordinator.java @@ -359,6 +359,18 @@ public abstract class QueueCoordinator { */ public abstract void addProgressSubscriber(@Nonnull ProgressSubscriber progressSubscriber); + /** + * Get the {@link LightingMode} to be used when setting blocks + */ + @Nonnull public abstract LightingMode getLightingMode(); + + /** + * Set the {@link LightingMode} to be used when setting blocks + * + * @param mode lighting mode. Null to use default. + */ + public abstract void setLightingMode(@Nullable LightingMode mode); + /** * Fill a cuboid between two positions with a BlockState *