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 2 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,101 @@
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))
);
}

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 {
addPlant(BlockBase.GRASS.getDefaultState(), new GrassStructure(), 1);
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't this replace the vanilla generation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it replaces vanilla generator since calling API cancels vanilla code

Copy link
Member

Choose a reason for hiding this comment

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

Flowers no longer generate because of this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now it should be fine

2023-10-30_16 15 20

Copy link
Member

Choose a reason for hiding this comment

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

Don't really understand why only grass generator is replaced out of all bonemeal generators

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you will not override it then:

  • If event is not cancellable custom plant will generate with too hight density and override vanilla grass. If its generation chance will be forced to be low it will be not possible to generate hight density plants
  • If event is cancellable then adding custom plant will stop any grass from spawning

Basically adding these generators just makes things simple - you get a complete control over custom plant density together with grass, and it will generate patches almost identical to vanilla

Copy link
Member

Choose a reason for hiding this comment

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

When you say "it will generate patches almost identical to vanilla", do you mean with custom plants or without?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Both, by default it will generate vanilla grass and flowers in vanilla way, but you can add more flowers/grasses (or even structures) to generation pool, and they can have weight even larger than vanilla grass

Copy link
Member

Choose a reason for hiding this comment

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

I'm kind of hesitant of overriding vanilla generation like this. Though, to be fair, the original code does use ItemBase#rand which uses a different seed each run, so it probably won't affect anything.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That produces a result equal to vanilla code. It is possible to use more injects into vanilla code, but there will be no difference except worse performance and troubles for mods to mixin above that

}
}
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