New bonemeal API (consumers, allows to add any plant/structure)

This commit is contained in:
Frank 2022-05-27 17:13:59 +02:00
parent d985792cae
commit 5d970a27ba
2 changed files with 114 additions and 59 deletions

View file

@ -1,21 +1,25 @@
package org.betterx.bclib.api;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.betterx.bclib.util.BlocksHelper;
import org.betterx.bclib.util.WeightedList;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
public class BonemealAPI {
private static final Map<ResourceLocation, Map<Block, WeightedList<Block>>> WATER_GRASS_BIOMES = Maps.newHashMap();
private static final Map<ResourceLocation, Map<Block, WeightedList<Block>>> LAND_GRASS_BIOMES = Maps.newHashMap();
private static final Map<Block, WeightedList<Block>> WATER_GRASS_TYPES = Maps.newHashMap();
private static final Map<Block, WeightedList<Block>> LAND_GRASS_TYPES = Maps.newHashMap();
private static final Map<ResourceLocation, Map<Block, WeightedList<BiConsumer<Level, BlockPos>>>> WATER_GRASS_BIOMES = Maps.newHashMap();
private static final Map<ResourceLocation, Map<Block, WeightedList<BiConsumer<Level, BlockPos>>>> LAND_GRASS_BIOMES = Maps.newHashMap();
private static final Map<Block, WeightedList<BiConsumer<Level, BlockPos>>> WATER_GRASS_TYPES = Maps.newHashMap();
private static final Map<Block, WeightedList<BiConsumer<Level, BlockPos>>> LAND_GRASS_TYPES = Maps.newHashMap();
private static final Map<Block, Block> SPREADABLE_BLOCKS = Maps.newHashMap();
private static final Set<Block> TERRAIN_TO_SPREAD = Sets.newHashSet();
private static final Set<Block> TERRAIN = Sets.newHashSet();
@ -39,21 +43,33 @@ public class BonemealAPI {
}
public static void addLandGrass(Block plant, Block... terrain) {
addLandGrass(makeConsumer(plant), terrain);
}
public static void addLandGrass(BiConsumer<Level, BlockPos> plant, Block... terrain) {
for (Block block : terrain) {
addLandGrass(plant, block, 1F);
}
}
public static void addLandGrass(ResourceLocation biome, Block plant, Block... terrain) {
addLandGrass(biome, makeConsumer(plant), terrain);
}
public static void addLandGrass(ResourceLocation biome, BiConsumer<Level, BlockPos> plant, Block... terrain) {
for (Block block : terrain) {
addLandGrass(biome, plant, block, 1F);
}
}
public static void addLandGrass(Block plant, Block terrain, float chance) {
WeightedList<Block> list = LAND_GRASS_TYPES.get(terrain);
addLandGrass(makeConsumer(plant), terrain, chance);
}
public static void addLandGrass(BiConsumer<Level, BlockPos> plant, Block terrain, float chance) {
WeightedList<BiConsumer<Level, BlockPos>> list = LAND_GRASS_TYPES.get(terrain);
if (list == null) {
list = new WeightedList<Block>();
list = new WeightedList<>();
LAND_GRASS_TYPES.put(terrain, list);
}
TERRAIN.add(terrain);
@ -61,14 +77,21 @@ public class BonemealAPI {
}
public static void addLandGrass(ResourceLocation biome, Block plant, Block terrain, float chance) {
Map<Block, WeightedList<Block>> map = LAND_GRASS_BIOMES.get(biome);
addLandGrass(biome, makeConsumer(plant), terrain, chance);
}
public static void addLandGrass(ResourceLocation biome,
BiConsumer<Level, BlockPos> plant,
Block terrain,
float chance) {
Map<Block, WeightedList<BiConsumer<Level, BlockPos>>> map = LAND_GRASS_BIOMES.get(biome);
if (map == null) {
map = Maps.newHashMap();
LAND_GRASS_BIOMES.put(biome, map);
}
WeightedList<Block> list = map.get(terrain);
WeightedList<BiConsumer<Level, BlockPos>> list = map.get(terrain);
if (list == null) {
list = new WeightedList<Block>();
list = new WeightedList<>();
map.put(terrain, list);
}
TERRAIN.add(terrain);
@ -76,21 +99,33 @@ public class BonemealAPI {
}
public static void addWaterGrass(Block plant, Block... terrain) {
addWaterGrass(makeConsumer(plant), terrain);
}
public static void addWaterGrass(BiConsumer<Level, BlockPos> plant, Block... terrain) {
for (Block block : terrain) {
addWaterGrass(plant, block, 1F);
}
}
public static void addWaterGrass(ResourceLocation biome, Block plant, Block... terrain) {
addWaterGrass(biome, makeConsumer(plant), terrain);
}
public static void addWaterGrass(ResourceLocation biome, BiConsumer<Level, BlockPos> plant, Block... terrain) {
for (Block block : terrain) {
addWaterGrass(biome, plant, block, 1F);
}
}
public static void addWaterGrass(Block plant, Block terrain, float chance) {
WeightedList<Block> list = WATER_GRASS_TYPES.get(terrain);
addWaterGrass(makeConsumer(plant), terrain, chance);
}
public static void addWaterGrass(BiConsumer<Level, BlockPos> plant, Block terrain, float chance) {
WeightedList<BiConsumer<Level, BlockPos>> list = WATER_GRASS_TYPES.get(terrain);
if (list == null) {
list = new WeightedList<Block>();
list = new WeightedList<>();
WATER_GRASS_TYPES.put(terrain, list);
}
TERRAIN.add(terrain);
@ -98,23 +133,32 @@ public class BonemealAPI {
}
public static void addWaterGrass(ResourceLocation biome, Block plant, Block terrain, float chance) {
Map<Block, WeightedList<Block>> map = WATER_GRASS_BIOMES.get(biome);
addWaterGrass(biome, makeConsumer(plant), terrain, chance);
}
public static void addWaterGrass(ResourceLocation biome,
BiConsumer<Level, BlockPos> plant,
Block terrain,
float chance) {
Map<Block, WeightedList<BiConsumer<Level, BlockPos>>> map = WATER_GRASS_BIOMES.get(biome);
if (map == null) {
map = Maps.newHashMap();
WATER_GRASS_BIOMES.put(biome, map);
}
WeightedList<Block> list = map.get(terrain);
WeightedList<BiConsumer<Level, BlockPos>> list = map.get(terrain);
if (list == null) {
list = new WeightedList<Block>();
list = new WeightedList<>();
map.put(terrain, list);
}
TERRAIN.add(terrain);
list.add(plant, chance);
}
public static Block getLandGrass(ResourceLocation biomeID, Block terrain, RandomSource random) {
Map<Block, WeightedList<Block>> map = LAND_GRASS_BIOMES.get(biomeID);
WeightedList<Block> list = null;
public static BiConsumer<Level, BlockPos> getLandGrass(ResourceLocation biomeID,
Block terrain,
RandomSource random) {
Map<Block, WeightedList<BiConsumer<Level, BlockPos>>> map = LAND_GRASS_BIOMES.get(biomeID);
WeightedList<BiConsumer<Level, BlockPos>> list;
if (map != null) {
list = map.get(terrain);
if (list == null) {
@ -126,9 +170,11 @@ public class BonemealAPI {
return list == null ? null : list.get(random);
}
public static Block getWaterGrass(ResourceLocation biomeID, Block terrain, RandomSource random) {
Map<Block, WeightedList<Block>> map = WATER_GRASS_BIOMES.get(biomeID);
WeightedList<Block> list = null;
public static BiConsumer<Level, BlockPos> getWaterGrass(ResourceLocation biomeID,
Block terrain,
RandomSource random) {
Map<Block, WeightedList<BiConsumer<Level, BlockPos>>> map = WATER_GRASS_BIOMES.get(biomeID);
WeightedList<BiConsumer<Level, BlockPos>> list;
if (map != null) {
list = map.get(terrain);
if (list == null) {
@ -139,4 +185,8 @@ public class BonemealAPI {
}
return list == null ? null : list.get(random);
}
private static BiConsumer<Level, BlockPos> makeConsumer(Block block) {
return (level, pos) -> BlocksHelper.setWithoutUpdate(level, pos, block);
}
}

View file

@ -17,20 +17,23 @@ import org.betterx.bclib.api.biomes.BiomeAPI;
import org.betterx.bclib.util.BlocksHelper;
import org.betterx.bclib.util.MHelper;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import java.util.function.BiConsumer;
@Mixin(BoneMealItem.class)
public class BoneMealItemMixin {
private static final MutableBlockPos bclib_BLOCK_POS = new MutableBlockPos();
@Unique
private static final MutableBlockPos BCLIB_BLOCK_POS = new MutableBlockPos();
@Inject(method = "useOn", at = @At("HEAD"), cancellable = true)
private void bclib_onUse(UseOnContext context, CallbackInfoReturnable<InteractionResult> info) {
Level world = context.getLevel();
BlockPos blockPos = context.getClickedPos();
if (!world.isClientSide) {
BlockPos offseted = blockPos.relative(context.getClickedFace());
if (!world.isClientSide()) {
if (BonemealAPI.isTerrain(world.getBlockState(blockPos).getBlock())) {
boolean consume = false;
if (BonemealAPI.isSpreadableTerrain(world.getBlockState(blockPos).getBlock())) {
@ -61,22 +64,23 @@ public class BoneMealItemMixin {
}
}
private boolean bclib_growLandGrass(Level world, BlockPos pos) {
@Unique
private boolean bclib_growLandGrass(Level level, BlockPos pos) {
int y1 = pos.getY() + 3;
int y2 = pos.getY() - 3;
boolean result = false;
for (int i = 0; i < 64; i++) {
int x = (int) (pos.getX() + world.random.nextGaussian() * 2);
int z = (int) (pos.getZ() + world.random.nextGaussian() * 2);
bclib_BLOCK_POS.setX(x);
bclib_BLOCK_POS.setZ(z);
for (byte i = 0; i < 64; i++) {
int x = (int) (pos.getX() + level.random.nextGaussian() * 2);
int z = (int) (pos.getZ() + level.random.nextGaussian() * 2);
BCLIB_BLOCK_POS.setX(x);
BCLIB_BLOCK_POS.setZ(z);
for (int y = y1; y >= y2; y--) {
bclib_BLOCK_POS.setY(y);
BlockPos down = bclib_BLOCK_POS.below();
if (world.isEmptyBlock(bclib_BLOCK_POS) && !world.isEmptyBlock(down)) {
BlockState grass = bclib_getLandGrassState(world, down);
BCLIB_BLOCK_POS.setY(y);
BlockPos down = BCLIB_BLOCK_POS.below();
if (level.isEmptyBlock(BCLIB_BLOCK_POS) && !level.isEmptyBlock(down)) {
BiConsumer<Level, BlockPos> grass = bclib_getLandGrassState(level, down);
if (grass != null) {
BlocksHelper.setWithoutUpdate(world, bclib_BLOCK_POS, grass);
grass.accept(level, BCLIB_BLOCK_POS);
result = true;
}
break;
@ -86,23 +90,24 @@ public class BoneMealItemMixin {
return result;
}
private boolean bclib_growWaterGrass(Level world, BlockPos pos) {
@Unique
private boolean bclib_growWaterGrass(Level level, BlockPos pos) {
int y1 = pos.getY() + 3;
int y2 = pos.getY() - 3;
boolean result = false;
for (int i = 0; i < 64; i++) {
int x = (int) (pos.getX() + world.random.nextGaussian() * 2);
int z = (int) (pos.getZ() + world.random.nextGaussian() * 2);
bclib_BLOCK_POS.setX(x);
bclib_BLOCK_POS.setZ(z);
for (byte i = 0; i < 64; i++) {
int x = (int) (pos.getX() + level.random.nextGaussian() * 2);
int z = (int) (pos.getZ() + level.random.nextGaussian() * 2);
BCLIB_BLOCK_POS.setX(x);
BCLIB_BLOCK_POS.setZ(z);
for (int y = y1; y >= y2; y--) {
bclib_BLOCK_POS.setY(y);
BlockPos down = bclib_BLOCK_POS.below();
if (BlocksHelper.isFluid(world.getBlockState(bclib_BLOCK_POS)) && !BlocksHelper.isFluid(world.getBlockState(
BCLIB_BLOCK_POS.setY(y);
BlockPos down = BCLIB_BLOCK_POS.below();
if (BlocksHelper.isFluid(level.getBlockState(BCLIB_BLOCK_POS)) && !BlocksHelper.isFluid(level.getBlockState(
down))) {
BlockState grass = bclib_getWaterGrassState(world, down);
BiConsumer<Level, BlockPos> grass = bclib_getWaterGrassState(level, down);
if (grass != null) {
BlocksHelper.setWithoutUpdate(world, bclib_BLOCK_POS, grass);
grass.accept(level, BCLIB_BLOCK_POS);
result = true;
}
break;
@ -112,26 +117,25 @@ public class BoneMealItemMixin {
return result;
}
private BlockState bclib_getLandGrassState(Level world, BlockPos pos) {
BlockState state = world.getBlockState(pos);
Block block = state.getBlock();
block = BonemealAPI.getLandGrass(BiomeAPI.getBiomeID(world.getBiome(pos)), block, world.getRandom());
return block == null ? null : block.defaultBlockState();
@Unique
private BiConsumer<Level, BlockPos> bclib_getLandGrassState(Level level, BlockPos pos) {
BlockState state = level.getBlockState(pos);
return BonemealAPI.getLandGrass(BiomeAPI.getBiomeID(level.getBiome(pos)), state.getBlock(), level.getRandom());
}
private BlockState bclib_getWaterGrassState(Level world, BlockPos pos) {
BlockState state = world.getBlockState(pos);
Block block = state.getBlock();
block = BonemealAPI.getWaterGrass(BiomeAPI.getBiomeID(world.getBiome(pos)), block, world.getRandom());
return block == null ? null : block.defaultBlockState();
@Unique
private BiConsumer<Level, BlockPos> bclib_getWaterGrassState(Level level, BlockPos pos) {
BlockState state = level.getBlockState(pos);
return BonemealAPI.getWaterGrass(BiomeAPI.getBiomeID(level.getBiome(pos)), state.getBlock(), level.getRandom());
}
private BlockState bclib_getSpreadable(Level world, BlockPos pos) {
Vec3i[] offsets = MHelper.getOffsets(world.getRandom());
BlockState center = world.getBlockState(pos);
@Unique
private BlockState bclib_getSpreadable(Level level, BlockPos pos) {
Vec3i[] offsets = MHelper.getOffsets(level.getRandom());
BlockState center = level.getBlockState(pos);
for (Vec3i dir : offsets) {
BlockPos p = pos.offset(dir);
BlockState state = world.getBlockState(p);
BlockState state = level.getBlockState(p);
Block terrain = BonemealAPI.getSpreadable(state.getBlock());
if (center.is(terrain)) {
if (bclib_haveSameProperties(state, center)) {
@ -145,6 +149,7 @@ public class BoneMealItemMixin {
return null;
}
@Unique
private boolean bclib_haveSameProperties(BlockState state1, BlockState state2) {
Property<?>[] properties1 = state1.getProperties().toArray(new Property[0]);
Property<?>[] properties2 = state2.getProperties().toArray(new Property[0]);