generated from FabricMC/fabric-example-mod
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from valoeghese/tsuchimikado_motoharu
chunk gen events
- Loading branch information
Showing
13 changed files
with
226 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/java/io/github/fabriccommunity/events/mixin/world/MixinChunkGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.github.fabriccommunity.events.mixin.world; | ||
|
||
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.CallbackInfo; | ||
|
||
import io.github.fabriccommunity.events.world.WorldGenEvents; | ||
import net.minecraft.world.ChunkRegion; | ||
import net.minecraft.world.gen.StructureAccessor; | ||
import net.minecraft.world.gen.chunk.ChunkGenerator; | ||
|
||
/** | ||
* @author Valoeghese | ||
*/ | ||
@Mixin(ChunkGenerator.class) | ||
public class MixinChunkGenerator { | ||
@Inject(at = @At("HEAD"), method = "generateFeatures", cancellable = true) | ||
private void onStartGenerateFeatures(ChunkRegion region, StructureAccessor accessor, CallbackInfo info) { | ||
if (WorldGenEvents.GENERATE_FEATURES_START.invoker().onStartGenerateFeatures((ChunkGenerator) (Object) this, region, accessor)) { | ||
info.cancel(); | ||
} | ||
} | ||
|
||
@Inject(at = @At("RETURN"), method = "generateFeatures") | ||
private void onEndGenerateFeatures(ChunkRegion region, StructureAccessor accessor, CallbackInfo info) { | ||
WorldGenEvents.GENERATE_FEATURES_END.invoker().onEndGenerateFeatures((ChunkGenerator) (Object) this, region, accessor); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/io/github/fabriccommunity/events/util/SpawnPosGetter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package io.github.fabriccommunity.events.util; | ||
|
||
import net.minecraft.entity.EntityType; | ||
import net.minecraft.util.math.BlockPos; | ||
import net.minecraft.world.WorldView; | ||
|
||
/** | ||
* Used by {@link WorldGenEvents.POPULATE_ENTITIES} as an accessible way to get the entity spawn pos for given x/z coordinates. | ||
* @author Valoeghese | ||
*/ | ||
@FunctionalInterface | ||
public interface SpawnPosGetter { | ||
BlockPos getEntitySpawnPos(WorldView world, EntityType<?> entityType, int x, int z); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/test/java/io/github/fabriccommunity/events/test/ChunkGenTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package io.github.fabriccommunity.events.test; | ||
|
||
import io.github.fabriccommunity.events.world.WorldGenEvents; | ||
import net.fabricmc.api.ModInitializer; | ||
import net.minecraft.block.Blocks; | ||
import net.minecraft.entity.EntityType; | ||
import net.minecraft.entity.SpawnReason; | ||
import net.minecraft.entity.passive.VillagerEntity; | ||
import net.minecraft.util.math.BlockPos; | ||
import net.minecraft.world.biome.Biome; | ||
|
||
public class ChunkGenTest implements ModInitializer { | ||
public static final boolean ENABLED = true; | ||
|
||
@Override | ||
public void onInitialize() { | ||
if (ENABLED) { | ||
WorldGenEvents.GENERATE_FEATURES_START.register((generator, region, structures) -> { | ||
if (random(region.getCenterChunkX(), region.getCenterChunkZ(), (int) region.getSeed(), 0b111) == 0) { | ||
return true; // cancel further event processing | ||
} | ||
|
||
return false; | ||
}); | ||
|
||
WorldGenEvents.GENERATE_FEATURES_END.register((generator, region, structures) -> { | ||
int x = region.getCenterChunkX() * 16; | ||
int z = region.getCenterChunkZ() * 16; | ||
BlockPos.Mutable e = new BlockPos.Mutable(x, 0, z); | ||
|
||
for (int y = 128; y >= 32; --y) { | ||
e.setY(y); | ||
region.setBlockState(e, Blocks.AIR.getDefaultState(), 3); | ||
} | ||
}); | ||
|
||
WorldGenEvents.POPULATE_ENTITIES.register((world, biome, chunkX, chunkZ, random, spawnPosGetter) -> { | ||
if (biome.getCategory() != Biome.Category.OCEAN) { | ||
int target = random.nextInt(3); | ||
|
||
for (int i = 0; i < target; ++i) { | ||
int ex = (chunkX << 4) + random.nextInt(16); | ||
int ez = (chunkZ << 4) + random.nextInt(16); | ||
|
||
VillagerEntity villager = EntityType.VILLAGER.create(world.toServerWorld()); | ||
BlockPos pos = spawnPosGetter.getEntitySpawnPos(world, villager.getType(), ex, ez); | ||
villager.refreshPositionAndAngles(pos, random.nextFloat() * 360.0f, 0.0f); | ||
villager.headYaw = villager.yaw; | ||
villager.bodyYaw = villager.yaw; | ||
villager.initialize(world, world.getLocalDifficulty(pos), SpawnReason.STRUCTURE, null, null); | ||
world.spawnEntityAndPassengers(villager); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
|
||
private static int random(int x, int y, int seed, int mask) { | ||
seed = 375462423 * seed + 672456235; | ||
seed += x; | ||
seed = 375462423 * seed + 672456235; | ||
seed += y; | ||
seed = 375462423 * seed + 672456235; | ||
return seed & mask; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters