Skip to content

Commit

Permalink
beehive ifarmland. instant update on click for honey
Browse files Browse the repository at this point in the history
  • Loading branch information
eerussianguy committed Sep 23, 2022
1 parent 0d848a6 commit 86566ee
Showing 1 changed file with 8 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import java.util.Arrays;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Function;

import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
Expand All @@ -23,17 +21,15 @@
import com.eerussianguy.firmalife.common.FLHelpers;
import com.eerussianguy.firmalife.common.FLTags;
import com.eerussianguy.firmalife.common.blocks.FLBeehiveBlock;
import com.eerussianguy.firmalife.common.blocks.greenhouse.LargePlanterBlock;
import com.eerussianguy.firmalife.common.capabilities.bee.BeeAbility;
import com.eerussianguy.firmalife.common.capabilities.bee.BeeCapability;
import com.eerussianguy.firmalife.common.capabilities.bee.IBee;
import com.eerussianguy.firmalife.common.container.BeehiveContainer;
import net.dries007.tfc.common.blockentities.FarmlandBlockEntity;
import net.dries007.tfc.common.blockentities.TFCBlockEntities;
import net.dries007.tfc.common.blockentities.IFarmland;
import net.dries007.tfc.common.blockentities.TickableInventoryBlockEntity;
import net.dries007.tfc.common.blocks.soil.ConnectedGrassBlock;
import net.dries007.tfc.common.blocks.soil.DirtBlock;
import net.dries007.tfc.common.blocks.soil.FarmlandBlock;
import net.dries007.tfc.util.Helpers;
import net.dries007.tfc.util.calendar.Calendars;
import net.dries007.tfc.util.calendar.ICalendar;
Expand Down Expand Up @@ -278,6 +274,7 @@ public int takeHoney(int amount)
{
final int take = Math.min(amount, honey);
honey -= take;
updateState();
markForSync();
return take;
}
Expand All @@ -293,28 +290,15 @@ private void tickPosition(BlockPos pos, BlockState state, @Nullable IBee bee)
if (bee != null)
{
final Block block = state.getBlock();
final boolean soil = block instanceof FarmlandBlock;
final boolean planter = block instanceof LargePlanterBlock;

if (planter || soil)
if (level.getBlockEntity(pos) instanceof IFarmland farmland)
{
float cropAffinity = bee.getAbility(BeeAbility.CROP_AFFINITY); // 0 -> 10 scale
float nitrogen = level.random.nextFloat() * cropAffinity * 0.1f; // 0 -> 1 scale
float potassium = level.random.nextFloat() * cropAffinity * 0.1f;
float phosphorous = level.random.nextFloat() * cropAffinity * 0.1f;
float cap = (cropAffinity - 1) * 0.5f; // max that can possibly be set by bee fertilization, 0 -> 4.5 scale

if (soil)
{
level.getBlockEntity(pos, TFCBlockEntities.FARMLAND.get()).ifPresent(farmland -> receiveNutrients(farmland::getNutrient, farmland::setNutrient, cap, nitrogen, phosphorous, potassium));
}
else
{
if (level.getBlockEntity(pos) instanceof LargePlanterBlockEntity planterEntity)
{
receiveNutrients(planterEntity::getNutrient, planterEntity::setNutrient, cap, nitrogen, phosphorous, potassium);
}
}
receiveNutrients(farmland, cap, nitrogen, phosphorous, potassium);
}

final int restore = bee.getAbility(BeeAbility.NATURE_RESTORATION);
Expand Down Expand Up @@ -342,11 +326,11 @@ else if (state.isAir() && level.getBlockState(pos.below()).getBlock() instanceof
}
}

private void receiveNutrients(Function<FarmlandBlockEntity.NutrientType, Float> getter, BiConsumer<FarmlandBlockEntity.NutrientType, Float> setter, float cap, float nitrogen, float phosphorous, float potassium)
private void receiveNutrients(IFarmland farmland, float cap, float nitrogen, float phosphorous, float potassium)
{
float n = getter.apply(N); if (n < cap) setter.accept(N, Math.min(n + nitrogen, cap));
float p = getter.apply(N); if (p < cap) setter.accept(P, Math.min(p + phosphorous, cap));
float k = getter.apply(K); if (k < cap) setter.accept(K, Math.min(k + potassium, cap));
float n = farmland.getNutrient(N); if (n < cap) farmland.setNutrient(N, Math.min(n + nitrogen, cap));
float p = farmland.getNutrient(N); if (p < cap) farmland.setNutrient(P, Math.min(p + phosphorous, cap));
float k = farmland.getNutrient(K); if (k < cap) farmland.setNutrient(K, Math.min(k + potassium, cap));
}

public void updateState()
Expand Down

0 comments on commit 86566ee

Please sign in to comment.