Skip to content

Commit

Permalink
start on custom decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
dags- committed Mar 20, 2020
1 parent aa8ead4 commit 79d48ac
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/main/java/com/terraforged/feature/fast/FastDecorator.java
@@ -0,0 +1,49 @@
package com.terraforged.feature.fast;

import com.mojang.datafixers.Dynamic;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IWorld;
import net.minecraft.world.gen.ChunkGenerator;
import net.minecraft.world.gen.feature.ConfiguredFeature;
import net.minecraft.world.gen.feature.Feature;
import net.minecraft.world.gen.feature.IFeatureConfig;
import net.minecraft.world.gen.placement.IPlacementConfig;
import net.minecraft.world.gen.placement.Placement;

import java.util.Random;
import java.util.function.Function;
import java.util.stream.Stream;

public abstract class FastDecorator<C extends IPlacementConfig> extends Placement<C> {

public FastDecorator(Function<Dynamic<?>, ? extends C> factory) {
super(factory);
}

public FastDecorator<C> name(String name) {
setRegistryName(name);
return this;
}

@Override
public final Stream<BlockPos> getPositions(IWorld world, ChunkGenerator<?> generator, Random random, C config, BlockPos pos) {
return Stream.empty();
}

@Override
public final <FC extends IFeatureConfig, F extends Feature<FC>> boolean place(IWorld world, ChunkGenerator<?> generator, Random random, BlockPos pos, C config, ConfiguredFeature<FC, F> feature) {
boolean result = false;
int count = getCount(config);
BlockPos.Mutable mutable = new BlockPos.Mutable();
for (int i = 0; i < count; i++) {
if (next(world, generator, random, config, pos, mutable)) {
result |= feature.place(world, generator, random, mutable);
}
}
return result;
}

protected abstract int getCount(C config);

protected abstract boolean next(IWorld world, ChunkGenerator<?> generator, Random random, C config, BlockPos pos, BlockPos.Mutable mutable);
}
@@ -0,0 +1,15 @@
package com.terraforged.feature.fast;

import net.minecraft.world.gen.placement.FrequencyConfig;

public abstract class FastFrequencyDecorator extends FastDecorator<FrequencyConfig> {

public FastFrequencyDecorator() {
super(FrequencyConfig::deserialize);
}

@Override
protected int getCount(FrequencyConfig config) {
return config.count;
}
}
22 changes: 22 additions & 0 deletions src/main/java/com/terraforged/feature/fast/impl/FastAtSurface.java
@@ -0,0 +1,22 @@
package com.terraforged.feature.fast.impl;

import com.terraforged.feature.fast.FastFrequencyDecorator;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IWorld;
import net.minecraft.world.gen.ChunkGenerator;
import net.minecraft.world.gen.GenerationSettings;
import net.minecraft.world.gen.Heightmap;
import net.minecraft.world.gen.placement.FrequencyConfig;

import java.util.Random;

public class FastAtSurface extends FastFrequencyDecorator {
@Override
protected boolean next(IWorld world, ChunkGenerator<? extends GenerationSettings> generator, Random random, FrequencyConfig config, BlockPos pos, BlockPos.Mutable mutable) {
int x = random.nextInt(16) + pos.getX();
int z = random.nextInt(16) + pos.getZ();
int y = world.getHeight(Heightmap.Type.MOTION_BLOCKING, x, z);
mutable.setPos(x, y, z);
return true;
}
}
@@ -0,0 +1,30 @@
package com.terraforged.feature.fast.impl;

import com.terraforged.feature.fast.FastDecorator;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IWorld;
import net.minecraft.world.gen.ChunkGenerator;
import net.minecraft.world.gen.placement.CountRangeConfig;

import java.util.Random;

public class FastCountRange extends FastDecorator<CountRangeConfig> {

public FastCountRange() {
super(CountRangeConfig::deserialize);
}

@Override
protected int getCount(CountRangeConfig config) {
return config.count;
}

@Override
protected boolean next(IWorld world, ChunkGenerator<?> generator, Random random, CountRangeConfig config, BlockPos pos, BlockPos.Mutable mutable) {
int x = random.nextInt(16) + pos.getX();
int z = random.nextInt(16) + pos.getZ();
int y = random.nextInt(config.maximum - config.topOffset) + config.bottomOffset;
mutable.setPos(x, y, z);
return true;
}
}
22 changes: 22 additions & 0 deletions src/main/java/com/terraforged/feature/fast/impl/FastTopSolid.java
@@ -0,0 +1,22 @@
package com.terraforged.feature.fast.impl;

import com.terraforged.feature.fast.FastFrequencyDecorator;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IWorld;
import net.minecraft.world.gen.ChunkGenerator;
import net.minecraft.world.gen.GenerationSettings;
import net.minecraft.world.gen.Heightmap;
import net.minecraft.world.gen.placement.FrequencyConfig;

import java.util.Random;

public class FastTopSolid extends FastFrequencyDecorator {
@Override
protected boolean next(IWorld world, ChunkGenerator<? extends GenerationSettings> generator, Random random, FrequencyConfig config, BlockPos pos, BlockPos.Mutable mutable) {
int i = random.nextInt(16) + pos.getX();
int j = random.nextInt(16) + pos.getZ();
int k = world.getHeight(Heightmap.Type.OCEAN_FLOOR_WG, i, j);
mutable.setPos(i, k, j);
return true;
}
}

0 comments on commit 79d48ac

Please sign in to comment.