Skip to content

Commit 6ce93f5

Browse files
authored
Update for changes to Chunk API
Include some implementation for the Chunk interface (still need to finish volume streams)
1 parent 0a441bf commit 6ce93f5

File tree

9 files changed

+309
-33
lines changed

9 files changed

+309
-33
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* This file is part of Sponge, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) SpongePowered <https://www.spongepowered.org>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package org.spongepowered.common.util;
26+
27+
import net.minecraft.util.Mth;
28+
import net.minecraft.world.level.chunk.ChunkBiomeContainer;
29+
import org.checkerframework.checker.nullness.qual.Nullable;
30+
import org.spongepowered.api.world.biome.Biome;
31+
import org.spongepowered.common.accessor.world.level.chunk.ChunkBiomeContainerAccessor;
32+
33+
public final class ChunkUtil {
34+
35+
private ChunkUtil() {
36+
}
37+
38+
public static boolean setBiome(final @Nullable ChunkBiomeContainer biomeContainer,
39+
final int x, final int y, final int z, final Biome biome) {
40+
if (biomeContainer == null) {
41+
return false;
42+
}
43+
final net.minecraft.world.level.biome.Biome[] biomes = ((ChunkBiomeContainerAccessor) biomeContainer).accessor$biomes();
44+
45+
final int maskedX = x & ChunkBiomeContainer.HORIZONTAL_MASK;
46+
final int maskedY = Mth.clamp(y, 0, ChunkBiomeContainer.VERTICAL_MASK);
47+
final int maskedZ = z & ChunkBiomeContainer.HORIZONTAL_MASK;
48+
49+
final int WIDTH_BITS = ChunkBiomeContainerAccessor.accessor$WIDTH_BITS();
50+
final int posKey = maskedY << WIDTH_BITS + WIDTH_BITS | maskedZ << WIDTH_BITS | maskedX;
51+
biomes[posKey] = (net.minecraft.world.level.biome.Biome) (Object) biome;
52+
53+
return true;
54+
}
55+
56+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* This file is part of Sponge, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) SpongePowered <https://www.spongepowered.org>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package org.spongepowered.common.mixin.api.mcp.world.level.chunk;
26+
27+
import net.minecraft.world.level.chunk.ChunkAccess;
28+
import net.minecraft.world.level.chunk.ChunkBiomeContainer;
29+
import net.minecraft.world.level.chunk.ChunkStatus;
30+
import org.spongepowered.api.entity.Entity;
31+
import org.spongepowered.api.world.biome.Biome;
32+
import org.spongepowered.api.world.chunk.ChunkState;
33+
import org.spongepowered.api.world.chunk.ProtoChunk;
34+
import org.spongepowered.asm.mixin.Mixin;
35+
import org.spongepowered.asm.mixin.Shadow;
36+
import org.spongepowered.common.util.ChunkUtil;
37+
38+
import javax.annotation.Nullable;
39+
40+
@Mixin(ChunkAccess.class)
41+
public interface ChunkAccessMixin_API<P extends ProtoChunk<P>> extends ProtoChunk<P> {
42+
43+
// @formatter:on
44+
@Shadow ChunkStatus shadow$getStatus();
45+
@Shadow @Nullable ChunkBiomeContainer shadow$getBiomes();
46+
@Shadow void shadow$addEntity(net.minecraft.world.entity.Entity entity);
47+
// @formatter:off
48+
49+
@Override
50+
default void addEntity(final Entity entity) {
51+
this.shadow$addEntity((net.minecraft.world.entity.Entity) entity);
52+
}
53+
54+
@Override
55+
default ChunkState state() {
56+
return (ChunkState) this.shadow$getStatus();
57+
}
58+
59+
@Override
60+
default boolean isEmpty() {
61+
return this.shadow$getStatus() == ChunkStatus.EMPTY;
62+
}
63+
64+
@Override
65+
default boolean setBiome(final int x, final int y, final int z, final Biome biome) {
66+
return ChunkUtil.setBiome(this.shadow$getBiomes(), x, y, z, biome);
67+
}
68+
69+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* This file is part of Sponge, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) SpongePowered <https://www.spongepowered.org>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package org.spongepowered.common.mixin.api.mcp.world.level.chunk;
26+
27+
import net.minecraft.world.level.chunk.ChunkStatus;
28+
import org.spongepowered.api.world.chunk.ChunkState;
29+
import org.spongepowered.asm.mixin.Mixin;
30+
import org.spongepowered.asm.mixin.Shadow;
31+
32+
@Mixin(ChunkStatus.class)
33+
public abstract class ChunkStatusMixin_API implements ChunkState {
34+
35+
@Shadow public abstract boolean isOrAfter(ChunkStatus param0);
36+
37+
@Override
38+
public boolean isAfter(final ChunkState state) {
39+
return !this.equals(state) && this.isOrAfter((ChunkStatus) state);
40+
}
41+
42+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* This file is part of Sponge, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) SpongePowered <https://www.spongepowered.org>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package org.spongepowered.common.mixin.api.mcp.world.level.chunk;
26+
27+
import net.minecraft.world.level.chunk.EmptyLevelChunk;
28+
import org.spongepowered.api.world.biome.Biome;
29+
import org.spongepowered.asm.mixin.Mixin;
30+
31+
@Mixin(EmptyLevelChunk.class)
32+
public abstract class EmptyLevelChunkMixin_API extends LevelChunkMixin_API {
33+
34+
@Override
35+
public boolean setBiome(final int x, final int y, final int z, final Biome biome) {
36+
return false;
37+
}
38+
39+
}

src/mixins/java/org/spongepowered/common/mixin/api/mcp/world/level/chunk/LevelChunkMixin_API.java

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,20 @@
3939
import org.spongepowered.api.block.BlockState;
4040
import org.spongepowered.api.block.entity.BlockEntity;
4141
import org.spongepowered.api.entity.Entity;
42+
import org.spongepowered.api.util.Ticks;
4243
import org.spongepowered.api.world.biome.Biome;
4344
import org.spongepowered.api.world.chunk.Chunk;
4445
import org.spongepowered.api.world.volume.stream.StreamOptions;
4546
import org.spongepowered.api.world.volume.stream.VolumeStream;
4647
import org.spongepowered.asm.mixin.Final;
48+
import org.spongepowered.asm.mixin.Implements;
49+
import org.spongepowered.asm.mixin.Interface;
4750
import org.spongepowered.asm.mixin.Intrinsic;
4851
import org.spongepowered.asm.mixin.Mixin;
4952
import org.spongepowered.asm.mixin.Shadow;
5053
import org.spongepowered.common.accessor.world.level.chunk.ChunkBiomeContainerAccessor;
54+
import org.spongepowered.common.util.ChunkUtil;
55+
import org.spongepowered.common.util.SpongeTicks;
5156
import org.spongepowered.common.world.volume.VolumeStreamUtils;
5257
import org.spongepowered.common.world.volume.buffer.biome.ObjectArrayMutableBiomeBuffer;
5358
import org.spongepowered.common.world.volume.buffer.block.ArrayMutableBlockBuffer;
@@ -61,6 +66,7 @@
6166
import java.util.stream.Stream;
6267

6368
@Mixin(net.minecraft.world.level.chunk.LevelChunk.class)
69+
@Implements(@Interface(iface = Chunk.class, prefix = "chunk$"))
6470
public abstract class LevelChunkMixin_API implements Chunk {
6571

6672
//@formatter:off
@@ -69,37 +75,22 @@ public abstract class LevelChunkMixin_API implements Chunk {
6975
@Shadow @Final private ChunkPos chunkPos;
7076
@Shadow @Final private Level level;
7177

72-
@Shadow public abstract void shadow$setInhabitedTime(long p_177415_1_);
78+
@Shadow public abstract boolean shadow$isEmpty();
7379
//@formatter:on
7480

7581
@Override
7682
public boolean setBiome(final int x, final int y, final int z, final Biome biome) {
77-
final net.minecraft.world.level.biome.Biome[] biomes = ((ChunkBiomeContainerAccessor) this.biomes).accessor$biomes();
78-
79-
int maskedX = x & ChunkBiomeContainer.HORIZONTAL_MASK;
80-
int maskedY = Mth.clamp(y, 0, ChunkBiomeContainer.VERTICAL_MASK);
81-
int maskedZ = z & ChunkBiomeContainer.HORIZONTAL_MASK;
82-
83-
final int WIDTH_BITS = ChunkBiomeContainerAccessor.accessor$WIDTH_BITS();
84-
final int posKey = maskedY << WIDTH_BITS + WIDTH_BITS | maskedZ << WIDTH_BITS | maskedX;
85-
biomes[posKey] = (net.minecraft.world.level.biome.Biome) (Object) biome;
86-
87-
return true;
88-
}
89-
90-
@Intrinsic
91-
public long impl$getInhabitedTime() {
92-
return this.inhabitedTime;
83+
return ChunkUtil.setBiome(this.biomes, x, y, z, biome);
9384
}
9485

95-
@Intrinsic
96-
public void impl$setInhabitedTime(long newInhabitedTime) {
97-
this.shadow$setInhabitedTime(newInhabitedTime);
86+
@Override
87+
public Ticks inhabitedTime() {
88+
return new SpongeTicks(this.inhabitedTime);
9889
}
9990

10091
@Override
101-
public long inhabitedTime() {
102-
return this.inhabitedTime;
92+
public void setInhabitedTime(final Ticks newInhabitedTime) {
93+
this.inhabitedTime = newInhabitedTime.ticks();
10394
}
10495

10596
@Override
@@ -110,23 +101,28 @@ public Vector3i chunkPosition() {
110101
@Override
111102
public double regionalDifficultyFactor() {
112103
return new DifficultyInstance(this.level.getDifficulty(), this.level.getDayTime(),
113-
this.inhabitedTime(), this.level.getMoonBrightness()).getEffectiveDifficulty();
104+
this.inhabitedTime().ticks(), this.level.getMoonBrightness()).getEffectiveDifficulty();
114105
}
115106

116107
@Override
117108
public double regionalDifficultyPercentage() {
118109
return new DifficultyInstance(this.level.getDifficulty(), this.level.getDayTime(),
119-
this.inhabitedTime(), this.level.getMoonBrightness()).getSpecialMultiplier();
110+
this.inhabitedTime().ticks(), this.level.getMoonBrightness()).getSpecialMultiplier();
120111
}
121112

122113
@Override
123114
public org.spongepowered.api.world.World<?, ?> world() {
124115
return ((org.spongepowered.api.world.World<?, ?>) this.level);
125116
}
126117

118+
@Intrinsic
119+
public boolean chunk$isEmpty() {
120+
return this.shadow$isEmpty();
121+
}
122+
127123
@Override
128124
public VolumeStream<Chunk, Entity> entityStream(
129-
Vector3i min, Vector3i max, StreamOptions options
125+
final Vector3i min, final Vector3i max, final StreamOptions options
130126
) {
131127
VolumeStreamUtils.validateStreamArgs(
132128
Objects.requireNonNull(min, "min"), Objects.requireNonNull(max, "max"),
@@ -165,7 +161,7 @@ public VolumeStream<Chunk, Entity> entityStream(
165161

166162
@Override
167163
public VolumeStream<Chunk, BlockState> blockStateStream(
168-
Vector3i min, Vector3i max, StreamOptions options
164+
final Vector3i min, final Vector3i max, final StreamOptions options
169165
) {
170166
VolumeStreamUtils.validateStreamArgs(Objects.requireNonNull(min, "min"), Objects.requireNonNull(max, "max"),
171167
Objects.requireNonNull(options, "options"));
@@ -206,7 +202,7 @@ public VolumeStream<Chunk, BlockState> blockStateStream(
206202

207203
@Override
208204
public VolumeStream<Chunk, BlockEntity> blockEntityStream(
209-
Vector3i min, Vector3i max, StreamOptions options
205+
final Vector3i min, final Vector3i max, final StreamOptions options
210206
) {
211207
VolumeStreamUtils.validateStreamArgs(Objects.requireNonNull(min, "min"), Objects.requireNonNull(max, "max"),
212208
Objects.requireNonNull(options, "options"));
@@ -242,13 +238,13 @@ public VolumeStream<Chunk, BlockEntity> blockEntityStream(
242238
);
243239
}
244240

245-
private Stream<Map.Entry<BlockPos, net.minecraft.world.level.block.entity.BlockEntity>> impl$getBlockEntitiesStream(ChunkAccess chunk) {
241+
private Stream<Map.Entry<BlockPos, net.minecraft.world.level.block.entity.BlockEntity>> impl$getBlockEntitiesStream(final ChunkAccess chunk) {
246242
return chunk instanceof LevelChunk ? ((LevelChunk) chunk).getBlockEntities().entrySet().stream() : Stream.empty();
247243
}
248244

249245
@Override
250246
public VolumeStream<Chunk, Biome> biomeStream(
251-
Vector3i min, Vector3i max, StreamOptions options
247+
final Vector3i min, final Vector3i max, final StreamOptions options
252248
) {
253249
VolumeStreamUtils.validateStreamArgs(Objects.requireNonNull(min, "min"), Objects.requireNonNull(max, "max"),
254250
Objects.requireNonNull(options, "options"));

0 commit comments

Comments
 (0)