mirror of
https://github.com/GTNewHorizons/ForestryMC.git
synced 2026-11-20 03:59:45 +00:00
Harvest wood from top to bottom to help leaf decay
This commit is contained in:
1 parent
752a221fe8
commit
bc2c80365c
6 files changed
+69
-32
No files matched your search
@@ -5,10 +5,12 @@
|
||||
******************************************************************************/
|
||||
package forestry.api.farming;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.Collection;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
|
||||
public interface ICrop {
|
||||
|
||||
@@ -20,4 +22,6 @@ public interface ICrop {
|
||||
@Nullable
|
||||
Collection<ItemStack> harvest();
|
||||
|
||||
@Nonnull
|
||||
BlockPos getPosition();
|
||||
}
|
||||
@@ -14,7 +14,6 @@ import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
@@ -29,6 +28,7 @@ import net.minecraftforge.event.terraingen.TerrainGen;
|
||||
import forestry.api.world.ITreeGenData;
|
||||
import forestry.arboriculture.tiles.TileTreeContainer;
|
||||
import forestry.core.tiles.TileUtil;
|
||||
import forestry.core.utils.TopDownBlockPosComparator;
|
||||
import forestry.core.worldgen.WorldGenBase;
|
||||
|
||||
public abstract class WorldGenArboriculture extends WorldGenBase {
|
||||
@@ -62,12 +62,7 @@ public abstract class WorldGenArboriculture extends WorldGenBase {
|
||||
|
||||
if (genPos != null) {
|
||||
List<BlockPos> branchEnds = new ArrayList<>(generateTrunk(world, rand, wood, genPos));
|
||||
Collections.sort(branchEnds, new Comparator<BlockPos>() {
|
||||
@Override
|
||||
public int compare(BlockPos o1, BlockPos o2) {
|
||||
return Integer.compare(o2.getY(), o1.getY());
|
||||
}
|
||||
});
|
||||
Collections.sort(branchEnds, TopDownBlockPosComparator.INSTANCE);
|
||||
generateLeaves(world, rand, leaf, branchEnds, genPos);
|
||||
generateExtras(world, rand, genPos);
|
||||
return true;
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package forestry.core.utils;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
|
||||
public class TopDownBlockPosComparator implements Comparator<BlockPos> {
|
||||
public static final TopDownBlockPosComparator INSTANCE = new TopDownBlockPosComparator();
|
||||
|
||||
private TopDownBlockPosComparator() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compare(BlockPos o1, BlockPos o2) {
|
||||
return Integer.compare(o2.getY(), o1.getY());
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@
|
||||
******************************************************************************/
|
||||
package forestry.farming.logic;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.Collection;
|
||||
|
||||
@@ -24,7 +25,7 @@ public abstract class Crop implements ICrop {
|
||||
private final World world;
|
||||
protected final BlockPos position;
|
||||
|
||||
protected Crop(World world, BlockPos position) {
|
||||
protected Crop(@Nonnull World world, @Nonnull BlockPos position) {
|
||||
this.world = world;
|
||||
this.position = position;
|
||||
}
|
||||
@@ -43,4 +44,9 @@ public abstract class Crop implements ICrop {
|
||||
return harvestBlock(world, position);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public BlockPos getPosition() {
|
||||
return position;
|
||||
}
|
||||
}
|
||||
@@ -102,15 +102,26 @@ public class FarmLogicArboreal extends FarmLogicHomogeneous {
|
||||
}
|
||||
|
||||
// get all crops of the same type that are connected to the first one
|
||||
Stack<BlockPos> candidates = new Stack<>();
|
||||
candidates.add(position);
|
||||
Stack<BlockPos> knownCropPositions = new Stack<>();
|
||||
knownCropPositions.add(position);
|
||||
|
||||
Set<BlockPos> seen = new HashSet<>();
|
||||
Set<BlockPos> checkedBlocks = new HashSet<>();
|
||||
Stack<ICrop> crops = new Stack<>();
|
||||
|
||||
while (!candidates.empty()) {
|
||||
BlockPos candidate = candidates.pop();
|
||||
processHarvestBlock(world, candidate, farmable, crops, candidates, seen);
|
||||
while (!knownCropPositions.empty()) {
|
||||
BlockPos knownCropPos = knownCropPositions.pop();
|
||||
for (BlockPos candidate : BlockPos.getAllInBox(knownCropPos.add(-1, -1, -1), knownCropPos.add(1, 1, 1))) {
|
||||
if (!checkedBlocks.contains(candidate)) {
|
||||
checkedBlocks.add(candidate);
|
||||
|
||||
IBlockState blockState = world.getBlockState(candidate);
|
||||
ICrop crop = farmable.getCropAt(world, candidate, blockState);
|
||||
if (crop != null) {
|
||||
crops.push(crop);
|
||||
knownCropPositions.push(candidate);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return crops;
|
||||
@@ -127,21 +138,6 @@ public class FarmLogicArboreal extends FarmLogicHomogeneous {
|
||||
return null;
|
||||
}
|
||||
|
||||
private static void processHarvestBlock(World world, BlockPos position, IFarmable farmable, Stack<ICrop> crops, Stack<BlockPos> candidates, Set<BlockPos> seen) {
|
||||
for (BlockPos candidate : BlockPos.getAllInBox(position.add(-1, -1, -1), position.add(1, 1, 1))) {
|
||||
if (!seen.contains(candidate)) {
|
||||
seen.add(candidate);
|
||||
|
||||
IBlockState blockState = world.getBlockState(candidate);
|
||||
ICrop crop = farmable.getCropAt(world, candidate, blockState);
|
||||
if (crop != null) {
|
||||
crops.push(crop);
|
||||
candidates.push(candidate);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean maintainGermlings(World world, IFarmHousing farmHousing, BlockPos pos, FarmDirection direction, int extent) {
|
||||
for (int i = 0; i < extent; i++) {
|
||||
|
||||
@@ -18,9 +18,11 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.EnumMap;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@@ -74,6 +76,7 @@ import forestry.core.network.DataInputStreamForestry;
|
||||
import forestry.core.network.DataOutputStreamForestry;
|
||||
import forestry.core.tiles.ILiquidTankTile;
|
||||
import forestry.core.utils.PlayerUtil;
|
||||
import forestry.core.utils.TopDownBlockPosComparator;
|
||||
import forestry.core.utils.Translator;
|
||||
import forestry.farming.FarmHelper;
|
||||
import forestry.farming.FarmTarget;
|
||||
@@ -114,7 +117,7 @@ public class FarmController extends RectangularMultiblockControllerBase implemen
|
||||
private int allowedExtent = 0;
|
||||
|
||||
private IFarmLogic harvestProvider; // The farm logic which supplied the pending crops.
|
||||
private final Stack<ICrop> pendingCrops = new Stack<>();
|
||||
private final List<ICrop> pendingCrops = new LinkedList<>();
|
||||
private final Stack<ItemStack> pendingProduce = new Stack<>();
|
||||
|
||||
private Stage stage = Stage.CULTIVATE;
|
||||
@@ -469,8 +472,9 @@ public class FarmController extends RectangularMultiblockControllerBase implemen
|
||||
|
||||
// Cull queued crops.
|
||||
if (!pendingCrops.isEmpty()) {
|
||||
if (cullCrop(pendingCrops.peek(), harvestProvider)) {
|
||||
pendingCrops.pop();
|
||||
ICrop first = pendingCrops.get(0);
|
||||
if (cullCrop(first, harvestProvider)) {
|
||||
pendingCrops.remove(0);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
@@ -505,6 +509,7 @@ public class FarmController extends RectangularMultiblockControllerBase implemen
|
||||
farmWorkStatus.didWork = !harvested.isEmpty();
|
||||
if (!harvested.isEmpty()) {
|
||||
pendingCrops.addAll(harvested);
|
||||
Collections.sort(pendingCrops, TopDownICropComparator.INSTANCE);
|
||||
harvestProvider = logic;
|
||||
}
|
||||
} else if (stage == Stage.CULTIVATE) {
|
||||
@@ -870,4 +875,17 @@ public class FarmController extends RectangularMultiblockControllerBase implemen
|
||||
public String toString() {
|
||||
return Objects.toStringHelper(this).add("stage", stage).add("logic", farmLogics.toString()).toString();
|
||||
}
|
||||
|
||||
private static class TopDownICropComparator implements Comparator<ICrop> {
|
||||
public static final TopDownICropComparator INSTANCE = new TopDownICropComparator();
|
||||
|
||||
private TopDownICropComparator() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compare(ICrop o1, ICrop o2) {
|
||||
return TopDownBlockPosComparator.INSTANCE.compare(o1.getPosition(), o2.getPosition());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user