Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bonemeal API #78

Merged
merged 7 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package net.modificationstation.sltest.bonemeal;

import net.mine_diver.unsafeevents.listener.EventListener;
import net.mine_diver.unsafeevents.listener.ListenerPriority;
import net.minecraft.block.BlockBase;
import net.modificationstation.stationapi.api.bonemeal.BonemealAPI;
import net.modificationstation.stationapi.api.event.registry.BlockRegistryEvent;

public class BonemealListener {
@EventListener(priority = ListenerPriority.LOW)
public void registerItems(BlockRegistryEvent event) {
BonemealAPI.addPlant(BlockBase.SAND.getDefaultState(), BlockBase.BOOKSHELF.getDefaultState(), 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package net.modificationstation.sltest.mixin;

import net.minecraft.block.BlockBase;
import net.minecraft.block.Obsidian;
import net.minecraft.level.Level;
import net.minecraft.level.biome.Biome;
import net.minecraft.level.dimension.DimensionData;
import net.minecraft.level.gen.BiomeSource;
import net.modificationstation.stationapi.api.block.BlockState;
import net.modificationstation.stationapi.api.block.StationBlock;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;

@Mixin(Obsidian.class)
public abstract class MixinObsidian implements StationBlock {
@Override
public boolean onBonemealUse(Level level, int x, int y, int z, BlockState state) {
level.setBlockState(x, y, z, BlockBase.LOG.getDefaultState());
System.out.println(x + " " + y + " " + z);
return true;
}
}
3 changes: 2 additions & 1 deletion src/test/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"net.modificationstation.sltest.tileentity.TileEntityListener",
"net.modificationstation.sltest.item.tool.ToolListener",
"net.modificationstation.sltest.datafixer.DataFixerListener",
"net.modificationstation.sltest.worldgen.TestWorldgenListener"
"net.modificationstation.sltest.worldgen.TestWorldgenListener",
"net.modificationstation.sltest.bonemeal.BonemealListener"
],
"stationapi:event_bus_client": [
"net.modificationstation.sltest.gui.GuiListener",
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/sltest.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"BlockBaseAccessor",
"MixinLevel",
"MixinNetherLevelSource",
"MixinObsidian",
"OverworldTestMixin"
],
"server": [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package net.modificationstation.stationapi.api.util.collection;

import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class WeightedList<T> {
private final List<T> objects = new ArrayList<>();
private final IntList weights = new IntArrayList();
private int maxWeight;

public void add(T object, int weight) {
objects.add(object);
maxWeight += weight;
weights.add(maxWeight);
}

public void clear() {
objects.clear();
weights.clear();
maxWeight = 0;
}

public T get(Random random) {
if (maxWeight == 0 || objects.isEmpty()) return null;
int weight = random.nextInt(maxWeight);
for (int i = 0; i < objects.size(); i++) {
if (weight < weights.getInt(i)) return objects.get(i);
}
return objects.get(0);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.modificationstation.stationapi.api.block;

import net.minecraft.block.BlockBase;
import net.minecraft.level.Level;
import net.modificationstation.stationapi.api.registry.Identifier;
import net.modificationstation.stationapi.api.registry.ModID;
import net.modificationstation.stationapi.api.registry.RemappableRawIdHolder;
Expand All @@ -15,4 +16,8 @@ default BlockBase setTranslationKey(ModID modID, String translationKey) {
default BlockBase setTranslationKey(Identifier translationKey) {
return Util.assertImpl();
}

default boolean onBonemealUse(Level level, int x, int y, int z, BlockState state) {
return false;
}
}
1 change: 1 addition & 0 deletions station-items-v0/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ version = getSubprojectVersion(project, "1.0.0")

moduleDependencies(project,
'station-api-base',
'station-maths-v0',
'station-registry-api-v0',
'station-flattening-v0',
'station-blocks-v0',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package net.modificationstation.stationapi.api.bonemeal;

import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap;
import net.minecraft.block.BlockBase;
import net.minecraft.level.Level;
import net.minecraft.level.structure.Structure;
import net.modificationstation.stationapi.api.block.BlockState;
import net.modificationstation.stationapi.api.registry.BlockRegistry;
import net.modificationstation.stationapi.api.tag.TagKey;
import net.modificationstation.stationapi.api.util.collection.WeightedList;
import net.modificationstation.stationapi.api.util.math.Direction;

import java.util.Map;
import java.util.Random;

public class BonemealAPI {
private static final Map<BlockState, WeightedList<Structure>> PLACERS = new Reference2ObjectOpenHashMap<>();

public static void addPlant(BlockState ground, BlockState plant, int weight) {
addPlant(ground, new SimpleStateStructure(plant), weight);
}

public static void addPlant(BlockState ground, Structure plant, int weight) {
PLACERS.computeIfAbsent(ground, g -> new WeightedList<>()).add(plant, weight);
}

public static void addPlant(TagKey<BlockBase> ground, BlockState plant, int weight) {
BlockRegistry.INSTANCE.forEach(blockBase ->
blockBase.getStateManager().getStates().stream().filter(state -> state.isIn(ground)).forEach(state -> addPlant(state, plant, weight))
);
}

public static void addPlant(TagKey<BlockBase> ground, Structure plant, int weight) {
BlockRegistry.INSTANCE.forEach(blockBase ->
blockBase.getStateManager().getStates().stream().filter(state -> state.isIn(ground)).forEach(state -> addPlant(state, plant, weight))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caching states from tags like this is probably not a very good idea, tags are reloadable after all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, original idea was to add plants to all blocks with tag, like "nether_ground" or similar things. I can adapt code to work with additional internal tag storage, that can solve this issue

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, now it should support both reloadable tags and states

);
}

public static boolean generate(Level level, int x, int y, int z, BlockState state, int side) {
WeightedList<Structure> structures = PLACERS.get(state);
if (structures == null) return false;
Random random = level.rand;
Direction offset = Direction.byId(side);
structures.get(random).generate(
level,
random,
x + offset.getOffsetX(),
y + offset.getOffsetY(),
z + offset.getOffsetZ()
);
for (byte i = 0; i < 127; i++) {
int px = x + random.nextInt(7) - 3;
int py = y + random.nextInt(5) - 2;
int pz = z + random.nextInt(7) - 3;
state = level.getBlockState(px, py, pz);
structures = PLACERS.get(state);
if (structures == null) continue;
structures.get(random).generate(level, random, px, py + 1, pz);
}
return true;
}

private static class SimpleStateStructure extends Structure {
private final BlockState state;

private SimpleStateStructure(BlockState state) {
this.state = state;
}

@Override
public boolean generate(Level level, Random random, int x, int y, int z) {
BlockState levelState = level.getBlockState(x, y, z);
if (!levelState.isAir() && !levelState.getMaterial().isLiquid()) return false;
if (state.getBlock().canPlaceAt(level, x, y, z)) {
level.setBlockState(x, y, z, state);
return true;
}
return false;
}
}

private static class GrassStructure extends Structure {
private static final BlockState STATE = BlockBase.TALLGRASS.getDefaultState();

@Override
public boolean generate(Level level, Random random, int x, int y, int z) {
BlockState levelState = level.getBlockState(x, y, z);
if (!levelState.isAir() && !levelState.getMaterial().isLiquid()) return false;
if (STATE.getBlock().canPlaceAt(level, x, y, z)) {
level.setBlockState(x, y, z, STATE);
level.setTileMeta(x, y, z, 1);
return true;
}
return false;
}
}

static {
BlockState grass = BlockBase.GRASS.getDefaultState();
addPlant(grass, new GrassStructure(), 10);
addPlant(grass, BlockBase.DANDELION.getDefaultState(), 1);
addPlant(grass, BlockBase.ROSE.getDefaultState(), 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package net.modificationstation.stationapi.mixin.item;

import net.minecraft.entity.player.PlayerBase;
import net.minecraft.item.Dye;
import net.minecraft.item.ItemInstance;
import net.minecraft.level.Level;
import net.modificationstation.stationapi.api.block.BlockState;
import net.modificationstation.stationapi.api.bonemeal.BonemealAPI;
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;

@Mixin(Dye.class)
public class MixinDye {
@Inject(method = "useOnTile", at = @At(
value = "INVOKE",
target = "Lnet/minecraft/level/Level;getTileId(III)I",
ordinal = 0
), cancellable = true)
private void onBonemealUse(ItemInstance item, PlayerBase player, Level level, int x, int y, int z, int side, CallbackInfoReturnable<Boolean> info) {
BlockState state = level.getBlockState(x, y, z);
if (state.getBlock().onBonemealUse(level, x, y, z, state)) {
level.method_202(x, y, z, x, y, z);
info.setReturnValue(true);
item.count--;
return;
}
if (BonemealAPI.generate(level, x, y, z, state, side)) {
level.method_202(x - 8, y - 8, z - 8, x + 8, y + 8, z + 8);
info.setReturnValue(true);
item.count--;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"mixins": [
"MixinBlockBase",
"MixinContainerBase",
"MixinDye",
"MixinInventoryUpdate0x68S2CPacket",
"MixinItemBase",
"MixinItemInstance",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
package net.modificationstation.stationapi.impl.worldgen;

import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import net.minecraft.level.biome.Biome;
import net.minecraft.level.biome.Forest;
import net.minecraft.level.biome.Hell;
import net.minecraft.level.biome.Rainforest;
import net.minecraft.level.biome.Sky;
import net.minecraft.level.biome.SparseBiome;
import net.minecraft.level.biome.Swampland;
import net.minecraft.level.biome.Taiga;
import net.modificationstation.stationapi.api.worldgen.biome.ClimateBiomeProvider;

import java.util.Collection;
import java.util.Set;

public class OverworldBiomeProviderImpl extends ClimateBiomeProvider {
private static final OverworldBiomeProviderImpl INSTANCE = new OverworldBiomeProviderImpl();

Expand All @@ -20,6 +31,23 @@ protected Biome getBiome(float temperature, float wetness) {
Biome biome = super.getBiome(temperature, wetness);
return biome == null ? Biome.getBiome(temperature, wetness) : biome;
}

@Override
public Collection<Biome> getBiomes() {
Collection<Biome> biomes = super.getBiomes();
biomes.add(Biome.RAINFOREST);
biomes.add(Biome.SWAMPLAND);
biomes.add(Biome.SEASONAL_FOREST);
biomes.add(Biome.FOREST);
biomes.add(Biome.SAVANNA);
biomes.add(Biome.SHRUBLAND);
biomes.add(Biome.TAIGA);
biomes.add(Biome.DESERT);
biomes.add(Biome.PLAINS);
biomes.add(Biome.ICE_DESERT);
biomes.add(Biome.TUNDRA);
return biomes;
}

public static OverworldBiomeProviderImpl getInstance() {
return INSTANCE;
Expand Down