-
Notifications
You must be signed in to change notification settings - Fork 20
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
mineLdiver
merged 7 commits into
ModificationStation:master
from
paulevsGitch:bonemeal-api
Nov 3, 2023
Merged
Bonemeal API #78
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
50290a0
Bonemeal API
paulevsGitch 4193406
Reindented code. Fixed missing station-maths-v0 dependency
mineLdiver 301d4da
Fixed missing flowers
paulevsGitch 75fdb6c
Merge remote-tracking branch 'origin/bonemeal-api' into bonemeal-api
paulevsGitch bb75d75
Added missing overworld biomes
paulevsGitch a8f82dc
Reloadable tag support
paulevsGitch a82d8f5
Reindented code
mineLdiver File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
src/test/java/net/modificationstation/sltest/bonemeal/BonemealListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/test/java/net/modificationstation/sltest/mixin/MixinObsidian.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...se/src/main/java/net/modificationstation/stationapi/api/util/collection/WeightedList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
...n-items-v0/src/main/java/net/modificationstation/stationapi/api/bonemeal/BonemealAPI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
station-items-v0/src/main/java/net/modificationstation/stationapi/mixin/item/MixinDye.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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--; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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:
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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