Skip to content

Commit 4cb9aa1

Browse files
committed
more power to the chunk generator
1 parent 7a44b37 commit 4cb9aa1

File tree

10 files changed

+128
-4
lines changed

10 files changed

+128
-4
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,8 @@ This API is available under the MIT license. Feel free to learn from it and inco
3838
## Running Tests
3939
If your ide runs using gradle change settings to make your ide compile it itself so you can run the test modules.
4040
If your ide doesn't have this feature get a better ide not a glorified text editor please kthx
41+
42+
## Subproject Versions
43+
The convention I use for subproject versions is setting it to the last api version I updated them.
44+
If this is stupid, please ping Valoeghese on discord to rant about it.
45+

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ org.gradle.jvmargs=-Xmx1G
66
loader_version=5ce86c8
77

88
# Mod Properties
9-
mod_version = 1.0.3
9+
mod_version = 1.0.4
1010
maven_group = io.github.minecraftcursedlegacy
1111
archives_base_name = cursed-legacy-api

legacy-terrain-v1/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
archivesBaseName = 'legacy-terrain-v1'
2-
version = getSubprojectVersion(project, '1.0.0')
2+
version = getSubprojectVersion(project, '1.0.4')
33

44
moduleDependencies(project, 'legacy-api-base')
55
dependencies {

legacy-terrain-v1/src/main/java/io/github/minecraftcursedlegacy/api/terrain/ChunkGenerator.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import net.minecraft.level.chunk.Chunk;
88
import net.minecraft.level.gen.OverworldCave;
99
import net.minecraft.level.source.LevelSource;
10+
import net.minecraft.tile.Tile;
1011
import net.minecraft.util.ProgressListener;
1112

1213
/**
@@ -55,6 +56,17 @@ protected int getIndex(int localX, int y, int localZ) {
5556
protected void generateCarvers(int chunkX, int chunkZ, byte[] tiles, Biome[] biomes) {
5657
}
5758

59+
/**
60+
* Whether the given x/z spawn coordinates are a valid player spawn position.
61+
* @param x the block x position to spawn the player at.
62+
* @param z the block z position to spawn the player at.
63+
* @return whether the player is allowed to spawn here.
64+
*/
65+
public boolean isValidSpawnPos(int x, int z) {
66+
int surfaceTile = this.level.method_152(x, z);
67+
return surfaceTile == Tile.SAND.id;
68+
}
69+
5870
@Override
5971
public Chunk loadChunk(int x, int z) {
6072
return this.getChunk(x, z);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2020 The Cursed Legacy Team.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining
5+
* a copy of this software and associated documentation files (the
6+
* "Software"), to deal in the Software without restriction, including
7+
* without limitation the rights to use, copy, modify, merge, publish,
8+
* distribute, sublicense, and/or sell copies of the Software, and to
9+
* permit persons to whom the Software is furnished to do so, subject to
10+
* the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be
13+
* included in all copies or substantial portions of the Software.
14+
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
24+
package io.github.minecraftcursedlegacy.impl.terrain;
25+
26+
import net.minecraft.level.source.LevelSource;
27+
28+
/**
29+
* Duck interface.
30+
* Because a certain way they implemented cancellable head injects is very annoying.
31+
* And messing with priorities didn't fix it.
32+
*/
33+
public interface InternalLevelSourceSetter {
34+
/**
35+
* Sets the internal api level source field on the class implementing this (i.e. Dimension).
36+
* @param source the level source.
37+
* @return the given source.
38+
*/
39+
LevelSource setInternalLevelSource(LevelSource source);
40+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) 2020 The Cursed Legacy Team.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining
5+
* a copy of this software and associated documentation files (the
6+
* "Software"), to deal in the Software without restriction, including
7+
* without limitation the rights to use, copy, modify, merge, publish,
8+
* distribute, sublicense, and/or sell copies of the Software, and to
9+
* permit persons to whom the Software is furnished to do so, subject to
10+
* the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be
13+
* included in all copies or substantial portions of the Software.
14+
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
24+
package io.github.minecraftcursedlegacy.mixin.terrain;
25+
26+
import org.spongepowered.asm.mixin.Mixin;
27+
import org.spongepowered.asm.mixin.injection.At;
28+
import org.spongepowered.asm.mixin.injection.Inject;
29+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
30+
31+
import io.github.minecraftcursedlegacy.api.terrain.ChunkGenerator;
32+
import io.github.minecraftcursedlegacy.impl.terrain.InternalLevelSourceSetter;
33+
import net.minecraft.level.dimension.Dimension;
34+
import net.minecraft.level.source.LevelSource;
35+
36+
@Mixin(Dimension.class)
37+
public class MixinDimension implements InternalLevelSourceSetter {
38+
private LevelSource api_level_source;
39+
40+
@Inject(at = @At("HEAD"), method = "method_1770", cancellable = true)
41+
private void onIsValidSpawnPos(int x, int z, CallbackInfoReturnable<Boolean> info) {
42+
if (this.api_level_source instanceof ChunkGenerator) {
43+
info.setReturnValue(((ChunkGenerator) this.api_level_source).isValidSpawnPos(x, z));
44+
}
45+
}
46+
47+
// This is better than manually making an accessor and case for every cache
48+
@Inject(at = @At("RETURN"), method = "createLevelSource")
49+
private void onCreateLevelSource(CallbackInfoReturnable<LevelSource> info) {
50+
this.setInternalLevelSource(info.getReturnValue());
51+
}
52+
53+
@Override
54+
public LevelSource setInternalLevelSource(LevelSource source) {
55+
return this.api_level_source = source;
56+
}
57+
}

legacy-terrain-v1/src/main/resources/legacy-terrain.mixins.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"compatibilityLevel": "JAVA_8",
55
"mixins": [
66
"MixinBiome",
7+
"MixinDimension",
78
"MixinNetherLevelSource",
89
"MixinOverworldLevelSource"
910
],

legacy-worldtypes-v1/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
archivesBaseName = 'legacy-worldtypes-v1'
2-
version = getSubprojectVersion(project, '1.0.0')
2+
version = getSubprojectVersion(project, '1.0.4')
33

44
moduleDependencies(project, 'legacy-api-base', 'legacy-attached-data-v1', 'legacy-terrain-v1', 'legacy-translations-v0')
55
dependencies {

legacy-worldtypes-v1/src/main/java/io/github/minecraftcursedlegacy/mixin/worldtype/MixinDimension.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import io.github.minecraftcursedlegacy.api.attacheddata.v1.DataManager;
3333
import io.github.minecraftcursedlegacy.api.worldtype.WorldType;
34+
import io.github.minecraftcursedlegacy.impl.terrain.InternalLevelSourceSetter;
3435
import io.github.minecraftcursedlegacy.impl.worldtype.WorldTypeData;
3536
import io.github.minecraftcursedlegacy.impl.worldtype.WorldTypeImpl;
3637
import net.minecraft.level.LevelProperties;
@@ -60,7 +61,9 @@ private void api_createLevelSource(CallbackInfoReturnable<LevelSource> info) {
6061
WorldType type = WorldType.getById(data.getTypeId()); // retrieve the world type
6162

6263
if (type != WorldType.DEFAULT) { // only mess with non default in case another mod wants to mixin here for some reason
63-
info.setReturnValue(type.createChunkGenerator(self.level, data.getOrCreateLoadedData(type.storesAdditionalData()))); // set the custom chunk generator
64+
info.setReturnValue(((InternalLevelSourceSetter) this).setInternalLevelSource( // set the custom chunk generator
65+
type.createChunkGenerator(self.level, data.getOrCreateLoadedData(type.storesAdditionalData()))
66+
));
6467
}
6568
}
6669
}

legacy-worldtypes-v1/src/test/java/io/github/minecraftcursedlegacy/test/RandomChunkGenerator.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,10 @@ protected void shapeChunk(int chunkX, int chunkZ, byte[] tiles, Biome[] biomes)
4242
protected void buildSurface(int chunkX, int chunkZ, byte[] tiles, Biome[] biomes) {
4343
this.surface.buildSurface(chunkX, chunkZ, tiles, biomes);
4444
}
45+
46+
@Override
47+
public boolean isValidSpawnPos(int x, int z) {
48+
int surfaceTile = this.level.method_152(x, z);
49+
return surfaceTile == Tile.GRASS.id;
50+
}
4551
}

0 commit comments

Comments
 (0)