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

Add a can_villager_compost field to the compostables datamap #1136

Merged
merged 5 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all 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,26 @@
--- a/net/minecraft/world/entity/ai/behavior/WorkAtComposter.java
+++ b/net/minecraft/world/entity/ai/behavior/WorkAtComposter.java
@@ -40,18 +_,18 @@

int i = 20;
int j = 10;
- int[] aint = new int[COMPOSTABLE_ITEMS.size()];
+ it.unimi.dsi.fastutil.objects.Reference2IntMap<Item> amounts = new it.unimi.dsi.fastutil.objects.Reference2IntOpenHashMap<>(COMPOSTABLE_ITEMS.size() * 2); // Neo: let's assume that there's roughly twice more villager compostables than in vanilla
SimpleContainer simplecontainer = p_24794_.getInventory();
int k = simplecontainer.getContainerSize();
BlockState blockstate = p_24796_;

for (int l = k - 1; l >= 0 && i > 0; l--) {
ItemStack itemstack = simplecontainer.getItem(l);
- int i1 = COMPOSTABLE_ITEMS.indexOf(itemstack.getItem());
- if (i1 != -1) {
+ var compostable = itemstack.getItemHolder().getData(net.neoforged.neoforge.registries.datamaps.builtin.NeoForgeDataMaps.COMPOSTABLES);
+ if (compostable != null && compostable.canVillagerCompost()) {
int j1 = itemstack.getCount();
- int k1 = aint[i1] + j1;
- aint[i1] = k1;
+ int k1 = amounts.getInt(itemstack.getItem()) + j1;
+ amounts.put(itemstack.getItem(), k1);
int l1 = Math.min(Math.min(k1 - 10, i), j1);
if (l1 > 0) {
i -= l1;
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"chance": 0.65
},
"minecraft:beetroot_seeds": {
"can_villager_compost": true,
"chance": 0.3
},
"minecraft:big_dripleaf": {
Expand Down Expand Up @@ -295,6 +296,7 @@
"chance": 0.65
},
"minecraft:wheat_seeds": {
"can_villager_compost": true,
"chance": 0.3
},
"minecraft:white_tulip": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import it.unimi.dsi.fastutil.objects.Reference2IntMap;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
Expand All @@ -20,8 +21,10 @@
import net.minecraft.util.random.Weight;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.ai.behavior.GiveGiftToHero;
import net.minecraft.world.entity.ai.behavior.WorkAtComposter;
import net.minecraft.world.entity.animal.Parrot;
import net.minecraft.world.entity.npc.VillagerProfession;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.ComposterBlock;
import net.minecraft.world.level.block.entity.AbstractFurnaceBlockEntity;
import net.minecraft.world.level.gameevent.GameEvent;
Expand All @@ -46,7 +49,8 @@ public NeoForgeDataMapsProvider(PackOutput packOutput, CompletableFuture<HolderL
@Override
protected void gather() {
final var compostables = builder(NeoForgeDataMaps.COMPOSTABLES);
ComposterBlock.COMPOSTABLES.forEach((item, chance) -> compostables.add(item.asItem().builtInRegistryHolder(), new Compostable(chance), false));
final List<Item> villagerCompostables = ObfuscationReflectionHelper.getPrivateValue(WorkAtComposter.class, null, "COMPOSTABLE_ITEMS");
ComposterBlock.COMPOSTABLES.forEach((item, chance) -> compostables.add(item.asItem().builtInRegistryHolder(), new Compostable(chance, villagerCompostables.contains(item.asItem())), false));

final var fuels = builder(NeoForgeDataMaps.FURNACE_FUELS);
AbstractFurnaceBlockEntity.buildFuels((value, time) -> value.ifLeft(item -> fuels.add(item.builtInRegistryHolder(), new FurnaceFuel(time), false))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,24 @@
/**
* Data map value for {@linkplain NeoForgeDataMaps#COMPOSTABLES compostables}.
*
* @param chance the chance that a compost is successful
* @param chance the chance that a compost is successful
* @param canVillagerCompost whether farmer villagers can compost the item
*/
public record Compostable(float chance) {
public record Compostable(float chance, boolean canVillagerCompost) {

public static final Codec<Compostable> CHANCE_CODEC = Codec.floatRange(0f, 1f)
.xmap(Compostable::new, Compostable::chance);
public static final Codec<Compostable> CODEC = Codec.withAlternative(
RecordCodecBuilder.create(in -> in.group(
Codec.floatRange(0f, 1f).fieldOf("chance").forGetter(Compostable::chance)).apply(in, Compostable::new)),
Codec.floatRange(0f, 1f).fieldOf("chance").forGetter(Compostable::chance),
Codec.BOOL.optionalFieldOf("can_villager_compost", false).forGetter(Compostable::canVillagerCompost)).apply(in, Compostable::new)),
CHANCE_CODEC);
/**
* Constructs a {@link Compostable} that cannot be composted by farmer villagers.
*
* @param chance the chance that a compost is successful
*/
public Compostable(float chance) {
this(chance, false);
}
}
Loading