Skip to content

Commit cf065ee

Browse files
committed
Rename ChunkSectionMask and clean it up
1 parent 6648b87 commit cf065ee

8 files changed

Lines changed: 48 additions & 61 deletions

File tree

worldedit-bukkit/adapters/adapter-1.21.3/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/v1_21_3/wna/PaperweightNativeChunk.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import com.google.common.base.Throwables;
2424
import com.sk89q.worldedit.bukkit.adapter.impl.v1_21_3.PaperweightAdapter;
2525
import com.sk89q.worldedit.bukkit.adapter.impl.v1_21_3.StaticRefraction;
26-
import com.sk89q.worldedit.internal.util.collection.ChunkSectionMask;
26+
import com.sk89q.worldedit.internal.util.collection.ChunkSectionPosSet;
2727
import com.sk89q.worldedit.internal.wna.NativeBlockState;
2828
import com.sk89q.worldedit.internal.wna.NativeChunk;
2929
import com.sk89q.worldedit.internal.wna.NativeChunkSection;
@@ -125,7 +125,7 @@ public NativeBlockState getBlockState(NativePosition blockPos) {
125125
}
126126

127127
@Override
128-
public void markSectionChanged(int index, ChunkSectionMask changed) {
128+
public void markSectionChanged(int index, ChunkSectionPosSet changed) {
129129
ServerChunkCache serverChunkCache = (ServerChunkCache) delegate.getLevel().getChunkSource();
130130
ChunkHolder holder;
131131
try {
@@ -152,9 +152,9 @@ public void markSectionChanged(int index, ChunkSectionMask changed) {
152152
Throwables.throwIfUnchecked(e);
153153
throw new RuntimeException(e);
154154
}
155-
changedBlocksPerSection[index] = new ShortOpenHashSet(changed.asShortCollection());
155+
changedBlocksPerSection[index] = new ShortOpenHashSet(changed.asSectionPosEncodedShorts());
156156
} else {
157-
changedBlocksPerSection[index].addAll(changed.asShortCollection());
157+
changedBlocksPerSection[index].addAll(changed.asSectionPosEncodedShorts());
158158
}
159159
// Trick to get the holder into the broadcast set
160160
serverChunkCache.onChunkReadyToSend(holder);
@@ -207,7 +207,7 @@ public NativeChunkSection getChunkSection(int index) {
207207
}
208208

209209
@Override
210-
public NativeChunkSection setChunkSection(int index, NativeChunkSection section, ChunkSectionMask modifiedBlocks) {
210+
public NativeChunkSection setChunkSection(int index, NativeChunkSection section, ChunkSectionPosSet modifiedBlocks) {
211211
Preconditions.checkPositionIndex(index, delegate.getSectionsCount());
212212
LevelChunkSection[] chunkSections = delegate.getSections();
213213
var oldSection = new PaperweightNativeChunkSection(chunkSections[index]);

worldedit-bukkit/adapters/adapter-1.21.4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/v1_21_4/wna/PaperweightNativeChunk.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import com.google.common.base.Throwables;
2424
import com.sk89q.worldedit.bukkit.adapter.impl.v1_21_4.PaperweightAdapter;
2525
import com.sk89q.worldedit.bukkit.adapter.impl.v1_21_4.StaticRefraction;
26-
import com.sk89q.worldedit.internal.util.collection.ChunkSectionMask;
26+
import com.sk89q.worldedit.internal.util.collection.ChunkSectionPosSet;
2727
import com.sk89q.worldedit.internal.wna.NativeBlockState;
2828
import com.sk89q.worldedit.internal.wna.NativeChunk;
2929
import com.sk89q.worldedit.internal.wna.NativeChunkSection;
@@ -125,7 +125,7 @@ public NativeBlockState getBlockState(NativePosition blockPos) {
125125
}
126126

127127
@Override
128-
public void markSectionChanged(int index, ChunkSectionMask changed) {
128+
public void markSectionChanged(int index, ChunkSectionPosSet changed) {
129129
ServerChunkCache serverChunkCache = (ServerChunkCache) delegate.getLevel().getChunkSource();
130130
ChunkHolder holder;
131131
try {
@@ -152,9 +152,9 @@ public void markSectionChanged(int index, ChunkSectionMask changed) {
152152
Throwables.throwIfUnchecked(e);
153153
throw new RuntimeException(e);
154154
}
155-
changedBlocksPerSection[index] = new ShortOpenHashSet(changed.asShortCollection());
155+
changedBlocksPerSection[index] = new ShortOpenHashSet(changed.asSectionPosEncodedShorts());
156156
} else {
157-
changedBlocksPerSection[index].addAll(changed.asShortCollection());
157+
changedBlocksPerSection[index].addAll(changed.asSectionPosEncodedShorts());
158158
}
159159
// Trick to get the holder into the broadcast set
160160
serverChunkCache.onChunkReadyToSend(holder);
@@ -207,7 +207,7 @@ public NativeChunkSection getChunkSection(int index) {
207207
}
208208

209209
@Override
210-
public NativeChunkSection setChunkSection(int index, NativeChunkSection section, ChunkSectionMask modifiedBlocks) {
210+
public NativeChunkSection setChunkSection(int index, NativeChunkSection section, ChunkSectionPosSet modifiedBlocks) {
211211
Preconditions.checkPositionIndex(index, delegate.getSectionsCount());
212212
LevelChunkSection[] chunkSections = delegate.getSections();
213213
var oldSection = new PaperweightNativeChunkSection(chunkSections[index]);

worldedit-core/src/main/java/com/sk89q/worldedit/extent/world/internal/SectionBufferingExtent.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import com.sk89q.worldedit.extent.world.SideEffectExtent;
2828
import com.sk89q.worldedit.function.operation.Operation;
2929
import com.sk89q.worldedit.function.operation.RunContext;
30-
import com.sk89q.worldedit.internal.util.collection.ChunkSectionMask;
30+
import com.sk89q.worldedit.internal.util.collection.ChunkSectionPosSet;
3131
import com.sk89q.worldedit.internal.wna.NativeBlockState;
3232
import com.sk89q.worldedit.internal.wna.NativeChunk;
3333
import com.sk89q.worldedit.internal.wna.NativeChunkSection;
@@ -60,7 +60,7 @@ private static long getTopSectionKey(BlockVector3 sectionPos) {
6060

6161
private record SectionData(
6262
NativeChunkSection section,
63-
ChunkSectionMask modified
63+
ChunkSectionPosSet modified
6464
) {
6565
}
6666

@@ -139,7 +139,7 @@ public <B extends BlockStateHolder<B>> boolean setBlock(BlockVector3 location, B
139139
}
140140
data = new SectionData(
141141
nativeWorld.getChunk(sectionPos.x(), sectionPos.z()).getChunkSection(sectionPos.y()).copy(),
142-
new ChunkSectionMask()
142+
new ChunkSectionPosSet()
143143
);
144144
sectionTable.computeIfAbsent(getTopSectionKey(sectionPos), k -> new Int2ObjectArrayMap<>())
145145
.put(sectionPos.y(), data);
@@ -196,7 +196,7 @@ private void finishOperation() {
196196
record SectionDataWithOld(
197197
NativeChunkSection section,
198198
NativeChunkSection oldSection,
199-
ChunkSectionMask modified
199+
ChunkSectionPosSet modified
200200
) {
201201
}
202202

worldedit-core/src/main/java/com/sk89q/worldedit/internal/util/collection/ChunkSectionMask.java renamed to worldedit-core/src/main/java/com/sk89q/worldedit/internal/util/collection/ChunkSectionPosSet.java

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,17 @@
2626
import java.util.BitSet;
2727

2828
/**
29-
* A mask for a chunk section.
29+
* A set of positions in a chunk section.
30+
*
31+
* <p>
32+
* This has a defined order based on the order of the bits in the internal encoding. It is not guaranteed to be stable
33+
* between Minecraft versions, but it is stable within a single one.
34+
* </p>
3035
*/
31-
public class ChunkSectionMask {
32-
public static int index(int x, int y, int z) {
36+
public final class ChunkSectionPosSet {
37+
private static int index(int x, int y, int z) {
3338
// Each value is 0-15, so 4 bits
34-
// NOTE: This order specifically matches the order used by SectionPos in Minecraft, do not change unless they do
39+
// NOTE: This encoding specifically matches the encoding used by SectionPos in Minecraft, do not change unless they do
3540
return (x << 8) | (z << 4) | y;
3641
}
3742

@@ -42,57 +47,39 @@ public interface PosConsumer {
4247

4348
private final BitSet mask = new BitSet(4096);
4449

45-
public boolean isSet(int x, int y, int z) {
46-
return mask.get(index(x, y, z));
47-
}
48-
4950
public void set(int x, int y, int z) {
5051
mask.set(index(x, y, z));
5152
}
5253

53-
public void clear(int x, int y, int z) {
54-
mask.clear(index(x, y, z));
55-
}
56-
57-
public void clear() {
58-
mask.clear();
59-
}
60-
61-
public void setAll() {
62-
mask.set(0, 4096);
63-
}
64-
6554
public void forEach(PosConsumer consumer) {
6655
for (int i = mask.nextSetBit(0); i >= 0; i = mask.nextSetBit(i + 1)) {
6756
consumer.apply((i >> 8) & 0xF, i & 0xF, (i >> 4) & 0xF);
6857
}
6958
}
7059

71-
public int cardinality() {
72-
return mask.cardinality();
73-
}
74-
7560
/**
76-
* {@return a view of this mask as a short collection} Used for updating MC internals.
61+
* {@return a view of this set as a short collection} These shorts match those used by {@code SectionPos}.
7762
*/
78-
public ShortCollection asShortCollection() {
63+
public ShortCollection asSectionPosEncodedShorts() {
7964
return new AbstractShortCollection() {
8065
@Override
8166
public ShortIterator iterator() {
8267
return new ShortIterator() {
8368
private int next = mask.nextSetBit(0);
8469

85-
@Override public short nextShort() {
70+
@Override
71+
public short nextShort() {
8672
if (!hasNext()) {
8773
throw new IllegalStateException();
8874
}
89-
// Uses the fact that we share the order with SectionPos to efficiently map
75+
// Uses the fact that we share the encoding with SectionPos to efficiently map
9076
short value = (short) next;
9177
next = mask.nextSetBit(next + 1);
9278
return value;
9379
}
9480

95-
@Override public boolean hasNext() {
81+
@Override
82+
public boolean hasNext() {
9683
return next >= 0;
9784
}
9885
};

worldedit-core/src/main/java/com/sk89q/worldedit/internal/wna/NativeChunk.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
package com.sk89q.worldedit.internal.wna;
2121

22-
import com.sk89q.worldedit.internal.util.collection.ChunkSectionMask;
22+
import com.sk89q.worldedit.internal.util.collection.ChunkSectionPosSet;
2323

2424
import javax.annotation.Nullable;
2525

@@ -38,7 +38,7 @@ public interface NativeChunk {
3838
@Nullable
3939
NativeBlockState setBlockState(NativePosition blockPos, NativeBlockState newState, boolean update);
4040

41-
void markSectionChanged(int index, ChunkSectionMask changed);
41+
void markSectionChanged(int index, ChunkSectionPosSet changed);
4242

4343
void updateHeightmaps();
4444

@@ -60,12 +60,12 @@ public interface NativeChunk {
6060
* Replaces a chunk section in the given chunk. This method is also responsible for updating heightmaps
6161
* and creating block entities, to keep consistency with {@link #setBlockState(NativePosition, NativeBlockState, boolean)}
6262
* (the method we used to use). This is usually easily done by calling
63-
* {@link WNASharedImpl#postChunkSectionReplacement(NativeChunk, int, NativeChunkSection, NativeChunkSection, ChunkSectionMask)}.
63+
* {@link WNASharedImpl#postChunkSectionReplacement(NativeChunk, int, NativeChunkSection, NativeChunkSection, ChunkSectionPosSet)}.
6464
*
6565
* @param index the index, from 0 to the max height divided by 16
6666
* @param section the new chunk section
67-
* @param modifiedBlocks the mask of modified blocks
67+
* @param modifiedBlocks the set of modified blocks
6868
* @return the old chunk section
6969
*/
70-
NativeChunkSection setChunkSection(int index, NativeChunkSection section, ChunkSectionMask modifiedBlocks);
70+
NativeChunkSection setChunkSection(int index, NativeChunkSection section, ChunkSectionPosSet modifiedBlocks);
7171
}

worldedit-core/src/main/java/com/sk89q/worldedit/internal/wna/WNASharedImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
package com.sk89q.worldedit.internal.wna;
2121

2222
import com.sk89q.worldedit.WorldEditException;
23-
import com.sk89q.worldedit.internal.util.collection.ChunkSectionMask;
23+
import com.sk89q.worldedit.internal.util.collection.ChunkSectionPosSet;
2424
import com.sk89q.worldedit.math.BlockVector3;
2525
import com.sk89q.worldedit.util.SideEffect;
2626
import com.sk89q.worldedit.util.SideEffectSet;
@@ -167,11 +167,11 @@ public static void markAndNotifyBlock(
167167
* @param index the replaced section index
168168
* @param oldSection the old section
169169
* @param newSection the new section
170-
* @param modifiedBlocks the mask of modified blocks
170+
* @param modifiedBlocks the set of modified blocks
171171
*/
172172
public static void postChunkSectionReplacement(
173173
NativeChunk chunk, int index, NativeChunkSection oldSection, NativeChunkSection newSection,
174-
ChunkSectionMask modifiedBlocks
174+
ChunkSectionPosSet modifiedBlocks
175175
) {
176176
modifiedBlocks.forEach((secX, secY, secZ) -> {
177177
NativeBlockState oldState = oldSection.getBlock(secX, secY, secZ);

worldedit-fabric/src/main/java/com/sk89q/worldedit/fabric/mixin/MixinNativeChunk.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import com.google.common.base.Preconditions;
2323
import com.sk89q.worldedit.fabric.internal.ExtendedChunk;
24-
import com.sk89q.worldedit.internal.util.collection.ChunkSectionMask;
24+
import com.sk89q.worldedit.internal.util.collection.ChunkSectionPosSet;
2525
import com.sk89q.worldedit.internal.wna.NativeBlockState;
2626
import com.sk89q.worldedit.internal.wna.NativeChunk;
2727
import com.sk89q.worldedit.internal.wna.NativeChunkSection;
@@ -121,15 +121,15 @@ public MixinNativeChunk(ChunkPos chunkPos, UpgradeData upgradeData, LevelHeightA
121121
);
122122
}
123123

124-
public void nc$markSectionChanged(int index, ChunkSectionMask changed) {
124+
public void nc$markSectionChanged(int index, ChunkSectionPosSet changed) {
125125
ServerChunkCache serverChunkCache = (ServerChunkCache) getLevel().getChunkSource();
126126
ChunkHolder holder = serverChunkCache.getVisibleChunkIfPresent(getPos().toLong());
127127
if (holder != null) {
128128
if (holder.changedBlocksPerSection[index] == null) {
129129
holder.hasChangedSections = true;
130-
holder.changedBlocksPerSection[index] = new ShortOpenHashSet(changed.asShortCollection());
130+
holder.changedBlocksPerSection[index] = new ShortOpenHashSet(changed.asSectionPosEncodedShorts());
131131
} else {
132-
holder.changedBlocksPerSection[index].addAll(changed.asShortCollection());
132+
holder.changedBlocksPerSection[index].addAll(changed.asSectionPosEncodedShorts());
133133
}
134134
// Trick to get the holder into the broadcast set
135135
((ServerChunkCache) getLevel().getChunkSource()).onChunkReadyToSend(holder);
@@ -171,7 +171,7 @@ public MixinNativeChunk(ChunkPos chunkPos, UpgradeData upgradeData, LevelHeightA
171171
return (NativeChunkSection) getSection(index);
172172
}
173173

174-
public NativeChunkSection nc$setChunkSection(int index, NativeChunkSection section, ChunkSectionMask modifiedBlocks) {
174+
public NativeChunkSection nc$setChunkSection(int index, NativeChunkSection section, ChunkSectionPosSet modifiedBlocks) {
175175
Preconditions.checkPositionIndex(index, getSectionsCount());
176176
LevelChunkSection[] chunkSections = getSections();
177177
var oldSection = (NativeChunkSection) chunkSections[index];

worldedit-neoforge/src/main/java/com/sk89q/worldedit/neoforge/mixin/MixinNativeChunk.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
package com.sk89q.worldedit.neoforge.mixin;
2121

2222
import com.google.common.base.Preconditions;
23-
import com.sk89q.worldedit.internal.util.collection.ChunkSectionMask;
23+
import com.sk89q.worldedit.internal.util.collection.ChunkSectionPosSet;
2424
import com.sk89q.worldedit.internal.wna.NativeBlockState;
2525
import com.sk89q.worldedit.internal.wna.NativeChunk;
2626
import com.sk89q.worldedit.internal.wna.NativeChunkSection;
@@ -121,15 +121,15 @@ public MixinNativeChunk(ChunkPos chunkPos, UpgradeData upgradeData, LevelHeightA
121121
);
122122
}
123123

124-
public void nc$markSectionChanged(int index, ChunkSectionMask changed) {
124+
public void nc$markSectionChanged(int index, ChunkSectionPosSet changed) {
125125
ServerChunkCache serverChunkCache = (ServerChunkCache) getLevel().getChunkSource();
126126
ChunkHolder holder = serverChunkCache.getVisibleChunkIfPresent(getPos().toLong());
127127
if (holder != null) {
128128
if (holder.changedBlocksPerSection[index] == null) {
129129
holder.hasChangedSections = true;
130-
holder.changedBlocksPerSection[index] = new ShortOpenHashSet(changed.asShortCollection());
130+
holder.changedBlocksPerSection[index] = new ShortOpenHashSet(changed.asSectionPosEncodedShorts());
131131
} else {
132-
holder.changedBlocksPerSection[index].addAll(changed.asShortCollection());
132+
holder.changedBlocksPerSection[index].addAll(changed.asSectionPosEncodedShorts());
133133
}
134134
// Trick to get the holder into the broadcast set
135135
((ServerChunkCache) getLevel().getChunkSource()).onChunkReadyToSend(holder);
@@ -171,7 +171,7 @@ public MixinNativeChunk(ChunkPos chunkPos, UpgradeData upgradeData, LevelHeightA
171171
return (NativeChunkSection) getSection(index);
172172
}
173173

174-
public NativeChunkSection nc$setChunkSection(int index, NativeChunkSection section, ChunkSectionMask modifiedBlocks) {
174+
public NativeChunkSection nc$setChunkSection(int index, NativeChunkSection section, ChunkSectionPosSet modifiedBlocks) {
175175
Preconditions.checkPositionIndex(index, getSectionsCount());
176176
LevelChunkSection[] chunkSections = getSections();
177177
var oldSection = (NativeChunkSection) chunkSections[index];

0 commit comments

Comments
 (0)