Bonemeal util & weighted list
This commit is contained in:
parent
017d663af6
commit
b80a7b3630
2 changed files with 94 additions and 0 deletions
60
src/main/java/ru/bclib/util/BonemealUtil.java
Normal file
60
src/main/java/ru/bclib/util/BonemealUtil.java
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
package ru.bclib.util;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
import com.google.common.collect.Maps;
|
||||||
|
|
||||||
|
import net.minecraft.resources.ResourceLocation;
|
||||||
|
import net.minecraft.world.level.block.Block;
|
||||||
|
|
||||||
|
public class BonemealUtil {
|
||||||
|
private static final Map<ResourceLocation, Map<Block, WeightedList<Block>>> GRASS_BIOMES = Maps.newHashMap();
|
||||||
|
private static final Map<Block, WeightedList<Block>> GRASS_TYPES = Maps.newHashMap();
|
||||||
|
|
||||||
|
public static void addBonemealGrass(Block terrain, Block plant) {
|
||||||
|
addBonemealGrass(terrain, plant, 1F);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void addBonemealGrass(Block terrain, Block plant, float chance) {
|
||||||
|
WeightedList<Block> list = GRASS_TYPES.get(terrain);
|
||||||
|
if (list == null) {
|
||||||
|
list = new WeightedList<Block>();
|
||||||
|
GRASS_TYPES.put(terrain, list);
|
||||||
|
}
|
||||||
|
list.add(plant, chance);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void addBonemealGrass(ResourceLocation biome, Block terrain, Block plant) {
|
||||||
|
addBonemealGrass(biome, terrain, plant, 1F);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void addBonemealGrass(ResourceLocation biome, Block terrain, Block plant, float chance) {
|
||||||
|
Map<Block, WeightedList<Block>> map = GRASS_BIOMES.get(biome);
|
||||||
|
if (map == null) {
|
||||||
|
map = Maps.newHashMap();
|
||||||
|
GRASS_BIOMES.put(biome, map);
|
||||||
|
}
|
||||||
|
WeightedList<Block> list = map.get(terrain);
|
||||||
|
if (list == null) {
|
||||||
|
list = new WeightedList<Block>();
|
||||||
|
map.put(terrain, list);
|
||||||
|
}
|
||||||
|
list.add(plant, chance);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Block getGrass(ResourceLocation biomeID, Block terrain, Random random) {
|
||||||
|
Map<Block, WeightedList<Block>> map = GRASS_BIOMES.get(biomeID);
|
||||||
|
WeightedList<Block> list = null;
|
||||||
|
if (map != null) {
|
||||||
|
list = map.get(terrain);
|
||||||
|
if (list == null) {
|
||||||
|
list = GRASS_TYPES.get(terrain);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
list = GRASS_TYPES.get(terrain);
|
||||||
|
}
|
||||||
|
return list == null ? null : list.get(random);
|
||||||
|
}
|
||||||
|
}
|
34
src/main/java/ru/bclib/util/WeightedList.java
Normal file
34
src/main/java/ru/bclib/util/WeightedList.java
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
package ru.bclib.util;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class WeightedList<T> {
|
||||||
|
private final List<Float> weights = new ArrayList<Float>();
|
||||||
|
private final List<T> values = new ArrayList<T>();
|
||||||
|
private float maxWeight;
|
||||||
|
|
||||||
|
public void add(T value, float weight) {
|
||||||
|
maxWeight += weight;
|
||||||
|
weights.add(maxWeight);
|
||||||
|
values.add(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public T get(Random random) {
|
||||||
|
if (maxWeight < 1) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
float weight = random.nextFloat() * maxWeight;
|
||||||
|
for (int i = 0; i < weights.size(); i++) {
|
||||||
|
if (weight <= weights.get(i)) {
|
||||||
|
return values.get(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isEmpty() {
|
||||||
|
return maxWeight == 0;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue