Skip to content

Commit

Permalink
more power to the chunk generator
Browse files Browse the repository at this point in the history
  • Loading branch information
valoeghese committed Apr 30, 2021
1 parent 7a44b37 commit 4cb9aa1
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 4 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,8 @@ This API is available under the MIT license. Feel free to learn from it and inco
## Running Tests
If your ide runs using gradle change settings to make your ide compile it itself so you can run the test modules.
If your ide doesn't have this feature get a better ide not a glorified text editor please kthx

## Subproject Versions
The convention I use for subproject versions is setting it to the last api version I updated them.
If this is stupid, please ping Valoeghese on discord to rant about it.

2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ org.gradle.jvmargs=-Xmx1G
loader_version=5ce86c8

# Mod Properties
mod_version = 1.0.3
mod_version = 1.0.4
maven_group = io.github.minecraftcursedlegacy
archives_base_name = cursed-legacy-api
2 changes: 1 addition & 1 deletion legacy-terrain-v1/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
archivesBaseName = 'legacy-terrain-v1'
version = getSubprojectVersion(project, '1.0.0')
version = getSubprojectVersion(project, '1.0.4')

moduleDependencies(project, 'legacy-api-base')
dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.minecraft.level.chunk.Chunk;
import net.minecraft.level.gen.OverworldCave;
import net.minecraft.level.source.LevelSource;
import net.minecraft.tile.Tile;
import net.minecraft.util.ProgressListener;

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

/**
* Whether the given x/z spawn coordinates are a valid player spawn position.
* @param x the block x position to spawn the player at.
* @param z the block z position to spawn the player at.
* @return whether the player is allowed to spawn here.
*/
public boolean isValidSpawnPos(int x, int z) {
int surfaceTile = this.level.method_152(x, z);
return surfaceTile == Tile.SAND.id;
}

@Override
public Chunk loadChunk(int x, int z) {
return this.getChunk(x, z);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2020 The Cursed Legacy Team.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package io.github.minecraftcursedlegacy.impl.terrain;

import net.minecraft.level.source.LevelSource;

/**
* Duck interface.
* Because a certain way they implemented cancellable head injects is very annoying.
* And messing with priorities didn't fix it.
*/
public interface InternalLevelSourceSetter {
/**
* Sets the internal api level source field on the class implementing this (i.e. Dimension).
* @param source the level source.
* @return the given source.
*/
LevelSource setInternalLevelSource(LevelSource source);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2020 The Cursed Legacy Team.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package io.github.minecraftcursedlegacy.mixin.terrain;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import io.github.minecraftcursedlegacy.api.terrain.ChunkGenerator;
import io.github.minecraftcursedlegacy.impl.terrain.InternalLevelSourceSetter;
import net.minecraft.level.dimension.Dimension;
import net.minecraft.level.source.LevelSource;

@Mixin(Dimension.class)
public class MixinDimension implements InternalLevelSourceSetter {
private LevelSource api_level_source;

@Inject(at = @At("HEAD"), method = "method_1770", cancellable = true)
private void onIsValidSpawnPos(int x, int z, CallbackInfoReturnable<Boolean> info) {
if (this.api_level_source instanceof ChunkGenerator) {
info.setReturnValue(((ChunkGenerator) this.api_level_source).isValidSpawnPos(x, z));
}
}

// This is better than manually making an accessor and case for every cache
@Inject(at = @At("RETURN"), method = "createLevelSource")
private void onCreateLevelSource(CallbackInfoReturnable<LevelSource> info) {
this.setInternalLevelSource(info.getReturnValue());
}

@Override
public LevelSource setInternalLevelSource(LevelSource source) {
return this.api_level_source = source;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"compatibilityLevel": "JAVA_8",
"mixins": [
"MixinBiome",
"MixinDimension",
"MixinNetherLevelSource",
"MixinOverworldLevelSource"
],
Expand Down
2 changes: 1 addition & 1 deletion legacy-worldtypes-v1/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
archivesBaseName = 'legacy-worldtypes-v1'
version = getSubprojectVersion(project, '1.0.0')
version = getSubprojectVersion(project, '1.0.4')

moduleDependencies(project, 'legacy-api-base', 'legacy-attached-data-v1', 'legacy-terrain-v1', 'legacy-translations-v0')
dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

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

if (type != WorldType.DEFAULT) { // only mess with non default in case another mod wants to mixin here for some reason
info.setReturnValue(type.createChunkGenerator(self.level, data.getOrCreateLoadedData(type.storesAdditionalData()))); // set the custom chunk generator
info.setReturnValue(((InternalLevelSourceSetter) this).setInternalLevelSource( // set the custom chunk generator
type.createChunkGenerator(self.level, data.getOrCreateLoadedData(type.storesAdditionalData()))
));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,10 @@ protected void shapeChunk(int chunkX, int chunkZ, byte[] tiles, Biome[] biomes)
protected void buildSurface(int chunkX, int chunkZ, byte[] tiles, Biome[] biomes) {
this.surface.buildSurface(chunkX, chunkZ, tiles, biomes);
}

@Override
public boolean isValidSpawnPos(int x, int z) {
int surfaceTile = this.level.method_152(x, z);
return surfaceTile == Tile.GRASS.id;
}
}

0 comments on commit 4cb9aa1

Please sign in to comment.