Skip to content
Draft
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
@@ -1,22 +1,33 @@
package com.simibubi.create.content.equipment;


import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap;
import net.createmod.catnip.levelWrappers.PlacementSimulationServerLevel;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.item.BoneMealItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.BonemealableBlock;
import net.minecraft.world.level.block.MangrovePropaguleBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;

import java.util.Map;

public class TreeFertilizerItem extends Item {
private static final Map<ResourceKey<Level>, TreesDreamWorld> WORLD_CACHE = new Reference2ObjectOpenHashMap<>();

public static void clearCache() {
WORLD_CACHE.clear();
}

public TreeFertilizerItem(Properties properties) {
super(properties);
Expand All @@ -38,8 +49,11 @@ public InteractionResult useOn(UseOnContext context) {
return InteractionResult.SUCCESS;
}

ServerLevel level = (ServerLevel) context.getLevel();

BlockPos saplingPos = context.getClickedPos();
TreesDreamWorld world = new TreesDreamWorld((ServerLevel) context.getLevel(), saplingPos);
TreesDreamWorld world = WORLD_CACHE.computeIfAbsent(level.dimension(), k -> new TreesDreamWorld(level));
world.initializeSoil(level.getBlockState(saplingPos.below()));

for (BlockPos pos : BlockPos.betweenClosed(-1, 0, -1, 1, 0, 1)) {
if (context.getLevel()
Expand Down Expand Up @@ -90,17 +104,19 @@ private BlockState withStage(BlockState original, int stage) {
}

private static class TreesDreamWorld extends PlacementSimulationServerLevel {
private final BlockState soil;
private BlockState soil;

protected TreesDreamWorld(ServerLevel wrapped, BlockPos saplingPos) {
protected TreesDreamWorld(ServerLevel wrapped) {
super(wrapped);
BlockState stateUnderSapling = wrapped.getBlockState(saplingPos.below());
}

public void initializeSoil(BlockState stateUnderSapling) {
// Tree features don't seem to succeed with mud as soil
if (stateUnderSapling.is(BlockTags.DIRT))
stateUnderSapling = Blocks.DIRT.defaultBlockState();

soil = stateUnderSapling;
blocksAdded.clear();
}

@Override
Expand All @@ -117,5 +133,4 @@ public boolean setBlock(BlockPos pos, BlockState newState, int flags) {
return super.setBlock(pos, newState, flags);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.simibubi.create.content.contraptions.actors.trainControls.ControlsServerHandler;
import com.simibubi.create.content.contraptions.minecart.CouplingPhysics;
import com.simibubi.create.content.contraptions.minecart.capability.CapabilityMinecartController;
import com.simibubi.create.content.equipment.TreeFertilizerItem;
import com.simibubi.create.content.equipment.toolbox.ToolboxHandler;
import com.simibubi.create.content.equipment.wrench.WrenchItem;
import com.simibubi.create.content.equipment.zapper.ZapperInteractionHandler;
Expand Down Expand Up @@ -165,6 +166,7 @@ public static void onUnloadWorld(LevelEvent.Unload event) {
Create.TORQUE_PROPAGATOR.onUnloadWorld(world);
WorldAttached.invalidateWorld(world);
CobbleGenOptimisation.invalidateWorld(world);
TreeFertilizerItem.clearCache();
}

@SubscribeEvent
Expand Down