From b237ddf0767c032db6c1a0af1d02572dfaec9920 Mon Sep 17 00:00:00 2001 From: Jordan Date: Tue, 10 May 2022 09:11:12 +0100 Subject: [PATCH] Cherry pick upstream commits to fix SideEffectSet (#1737) * Fix SideEffectSet.none() being the default set (#1620) * Fix SideEffectSet.none() being the default set * Remove the unnecessary private empty constructor for SideEffectSet * Back to ImmutableMap.of * Speed up Fast Reorder Mode and make it default (#1653) * Speed up Fast Reorder Mode and make it default * Make non-exposed side effects internal * Docs on isExposed Co-authored-by: Matthew Miller --- .../command/argument/SideEffectConverter.java | 4 ++- .../extent/world/SideEffectExtent.java | 10 +++++- .../internal/wna/WorldNativeAccess.java | 6 ++-- .../com/sk89q/worldedit/util/SideEffect.java | 33 +++++++++++++++---- .../sk89q/worldedit/util/SideEffectSet.java | 8 +++-- .../formatting/component/SideEffectBox.java | 27 ++++++++------- 6 files changed, 63 insertions(+), 25 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/SideEffectConverter.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/SideEffectConverter.java index 7008499ab..4098669ed 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/SideEffectConverter.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/SideEffectConverter.java @@ -59,7 +59,9 @@ public class SideEffectConverter implements ArgumentConverter { @Override public List getSuggestions(String input, InjectedValueAccess context) { - return limitByPrefix(getSideEffects().stream().map(sideEffect -> sideEffect.name().toLowerCase(Locale.US)), input); + return limitByPrefix(getSideEffects().stream() + .filter(SideEffect::isExposed) + .map(sideEffect -> sideEffect.name().toLowerCase(Locale.US)), input); } @Override diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/world/SideEffectExtent.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/world/SideEffectExtent.java index 1eacb1b4a..5a90aebbb 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/world/SideEffectExtent.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/world/SideEffectExtent.java @@ -32,10 +32,13 @@ import com.sk89q.worldedit.world.World; import com.sk89q.worldedit.world.block.BlockState; import com.sk89q.worldedit.world.block.BlockStateHolder; +import java.util.Arrays; import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Set; +import java.util.function.Function; +import java.util.stream.Collectors; import static com.google.common.base.Preconditions.checkNotNull; @@ -50,6 +53,11 @@ public class SideEffectExtent extends AbstractDelegateExtent { private SideEffectSet sideEffectSet = SideEffectSet.defaults(); private boolean postEditSimulation; + private static final SideEffectSet INTERNAL_NONE = new SideEffectSet( + Arrays.stream(SideEffect.values()) + .collect(Collectors.toMap(Function.identity(), state -> SideEffect.State.OFF)) + ); + /** * Create a new instance. * @@ -86,7 +94,7 @@ public class SideEffectExtent extends AbstractDelegateExtent { positions.put(location, world.getBlock(location)); } - return world.setBlock(location, block, postEditSimulation ? SideEffectSet.none() : sideEffectSet); + return world.setBlock(location, block, postEditSimulation ? INTERNAL_NONE : sideEffectSet); } public boolean commitRequired() { diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/internal/wna/WorldNativeAccess.java b/worldedit-core/src/main/java/com/sk89q/worldedit/internal/wna/WorldNativeAccess.java index 1bd487028..769773b96 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/internal/wna/WorldNativeAccess.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/internal/wna/WorldNativeAccess.java @@ -174,7 +174,7 @@ public interface WorldNativeAccess { if (isChunkTicking(chunk)) { if (sideEffectSet.shouldApply(SideEffect.ENTITY_AI)) { notifyBlockUpdate(chunk, pos, oldState, newState); - } else { + } else if (sideEffectSet.shouldApply(SideEffect.NETWORK)) { // If we want to skip entity AI, just mark the block for sending markBlockChanged(chunk, pos); } @@ -190,7 +190,9 @@ public interface WorldNativeAccess { } // Seems used only for PoI updates - onBlockStateChange(pos, oldState, newState); + if (sideEffectSet.shouldApply(SideEffect.POI_UPDATE)) { + onBlockStateChange(pos, oldState, blockState1); + } } //FAWE start diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/SideEffect.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/SideEffect.java index e2793cd9f..3c3910be7 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/SideEffect.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/SideEffect.java @@ -22,21 +22,31 @@ package com.sk89q.worldedit.util; import java.util.Locale; public enum SideEffect { - LIGHTING(State.ON), - NEIGHBORS(State.ON), - UPDATE(State.ON), - VALIDATION(State.OFF), - ENTITY_AI(State.OFF), - EVENTS(State.OFF); + LIGHTING(State.ON, true), + NEIGHBORS(State.ON, true), + UPDATE(State.ON, true), + VALIDATION(State.OFF, true), + ENTITY_AI(State.OFF, true), + EVENTS(State.OFF, true), + /** + * Internal use only. + */ + POI_UPDATE(State.ON, false), + /** + * Internal use only. + */ + NETWORK(State.ON, false); private final String displayName; private final String description; private final State defaultValue; + private final boolean exposed; - SideEffect(State defaultValue) { + SideEffect(State defaultValue, boolean exposed) { this.displayName = "worldedit.sideeffect." + this.name().toLowerCase(Locale.US); this.description = "worldedit.sideeffect." + this.name().toLowerCase(Locale.US) + ".description"; this.defaultValue = defaultValue; + this.exposed = exposed; } public String getDisplayName() { @@ -51,6 +61,15 @@ public enum SideEffect { return this.defaultValue; } + /** + * Determines if this side effect is considered API. + * + * @return if the side effect is exposed via API + */ + public boolean isExposed() { + return exposed; + } + public enum State { OFF, ON, diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/SideEffectSet.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/SideEffectSet.java index 04169effb..6fdc61f08 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/SideEffectSet.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/SideEffectSet.java @@ -31,10 +31,12 @@ import java.util.stream.Collectors; public class SideEffectSet { - private static final SideEffectSet DEFAULT = new SideEffectSet( - Arrays.stream(SideEffect.values()).collect(Collectors.toMap(Function.identity(), SideEffect::getDefaultValue)) + private static final SideEffectSet DEFAULT = new SideEffectSet(); + private static final SideEffectSet NONE = new SideEffectSet( + Arrays.stream(SideEffect.values()) + .filter(SideEffect::isExposed) + .collect(Collectors.toMap(Function.identity(), state -> SideEffect.State.OFF)) ); - private static final SideEffectSet NONE = new SideEffectSet(); private final Map sideEffects; private final Set appliedSideEffects; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/SideEffectBox.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/SideEffectBox.java index c3d340807..3aac83cf8 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/SideEffectBox.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/SideEffectBox.java @@ -23,8 +23,11 @@ import com.fastasyncworldedit.core.configuration.Caption; import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.util.SideEffect; import com.sk89q.worldedit.util.SideEffectSet; +import com.sk89q.worldedit.util.concurrency.LazyReference; +import com.sk89q.worldedit.util.formatting.WorldEditText; import com.sk89q.worldedit.util.formatting.text.Component; import com.sk89q.worldedit.util.formatting.text.TextComponent; +import com.sk89q.worldedit.util.formatting.text.TranslatableComponent; import com.sk89q.worldedit.util.formatting.text.event.ClickEvent; import com.sk89q.worldedit.util.formatting.text.event.HoverEvent; import com.sk89q.worldedit.util.formatting.text.format.TextColor; @@ -36,21 +39,23 @@ import java.util.stream.Collectors; public class SideEffectBox extends PaginationBox { - private static List sideEffects; + private static final LazyReference> SIDE_EFFECTS = LazyReference.from(() -> + WorldEdit.getInstance().getPlatformManager().getSupportedSideEffects() + .stream() + .filter(SideEffect::isExposed) + .sorted(Comparator.comparing(effect -> + WorldEditText.reduceToText( + TranslatableComponent.of(effect.getDisplayName()), + Locale.US + ) + )) + .collect(Collectors.toList()) + ); private final SideEffectSet sideEffectSet; private static List getSideEffects() { - //FAWE start - if (sideEffects == null) { - sideEffects = WorldEdit.getInstance().getPlatformManager().getSupportedSideEffects() - .stream() - .sorted(Comparator.comparing(Enum::name)) - .collect(Collectors.toList()); - } - //FAWE end - - return sideEffects; + return SIDE_EFFECTS.getValue(); } public SideEffectBox(SideEffectSet sideEffectSet) {