Skip to content

Commit

Permalink
Shift down nether trees
Browse files Browse the repository at this point in the history
  • Loading branch information
Rakambda committed Sep 29, 2020
1 parent e528037 commit e523490
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 79 deletions.
70 changes: 26 additions & 44 deletions src/main/java/fr/raksrinana/fallingtree/tree/Tree.java
Original file line number Diff line number Diff line change
@@ -1,75 +1,57 @@
package fr.raksrinana.fallingtree.tree;

import fr.raksrinana.fallingtree.utils.TreePart;
import fr.raksrinana.fallingtree.utils.TreePartType;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import java.util.*;
import java.util.stream.Stream;
import java.util.stream.Collectors;

public class Tree{
private final World world;
private final Set<BlockPos> logs;
private final Set<BlockPos> warts;
private final Set<TreePart> parts;
private final BlockPos hitPos;

public Tree(World world, BlockPos blockPos){
this.world = world;
this.hitPos = blockPos;
this.logs = new LinkedHashSet<>();
this.warts = new LinkedHashSet<>();
this.parts = new LinkedHashSet<>();
}

public void addLog(BlockPos blockPos){
if(Objects.nonNull(blockPos)){
this.logs.add(blockPos);
}
public void addPart(TreePart treePart){
this.parts.add(treePart);
}

public void addWart(BlockPos blockPos){
if(Objects.nonNull(blockPos)){
this.warts.add(blockPos);
}
public Optional<TreePart> getLastSequencePart(){
return getParts().stream()
.max(Comparator.comparingInt(TreePart::getSequence));
}

public void addPart(TreePart treePart, BlockPos blockPos){
switch(treePart){
case LOG:
addLog(blockPos);
break;
case WART:
addWart(blockPos);
break;
}
public Collection<TreePart> getWarts(){
return getParts().stream()
.filter(part -> part.getTreePartType() == TreePartType.WART)
.collect(Collectors.toSet());
}

public Collection<BlockPos> getWarts(){
return this.warts;
}

public Optional<BlockPos> getTopMostFurthestPart(){
return getTopMostPart().flatMap(topMost -> logs.stream()
.filter(log -> Objects.equals(log.getY(), topMost.getY()))
.max(Comparator.comparingInt(this::getDistanceFromHit)));
public Collection<TreePart> getLogs(){
return getParts().stream()
.filter(part -> part.getTreePartType() == TreePartType.LOG)
.collect(Collectors.toSet());
}

private Optional<BlockPos> getTopMostPart(){
return getAllPartsStream().max(Comparator.comparingInt(BlockPos::getY));
return getParts().stream()
.map(TreePart::getBlockPos)
.max(Comparator.comparingInt(BlockPos::getY));
}

public Optional<BlockPos> getTopMostLog(){
return getLogs().stream().max(Comparator.comparingInt(BlockPos::getY));
}

private Stream<BlockPos> getAllPartsStream(){
return Stream.concat(getLogs().stream(), getWarts().stream());
}

public int getDistanceFromHit(BlockPos pos){
return Math.abs(hitPos.getX() - pos.getX()) + Math.abs(hitPos.getY() - pos.getY()) + Math.abs(hitPos.getZ() - pos.getZ());
return getLogs().stream()
.map(TreePart::getBlockPos)
.max(Comparator.comparingInt(BlockPos::getY));
}

public int getLogCount(){
return this.logs.size();
return this.getLogs().size();
}

public BlockPos getHitPos(){
Expand All @@ -80,7 +62,7 @@ public World getWorld(){
return this.world;
}

public Collection<BlockPos> getLogs(){
return this.logs;
public Collection<TreePart> getParts(){
return this.parts;
}
}
65 changes: 36 additions & 29 deletions src/main/java/fr/raksrinana/fallingtree/tree/TreeHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import fr.raksrinana.fallingtree.config.Config;
import fr.raksrinana.fallingtree.utils.ToAnalyzePos;
import fr.raksrinana.fallingtree.utils.TreePart;
import fr.raksrinana.fallingtree.utils.TreePartType;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
Expand All @@ -27,10 +27,10 @@ public static Optional<Tree> getTree(World world, BlockPos originPos){
Queue<ToAnalyzePos> toAnalyzePos = new PriorityQueue<>();
Set<ToAnalyzePos> analyzedPos = new HashSet<>();
Tree tree = new Tree(world, originPos);
toAnalyzePos.add(new ToAnalyzePos(originPos, originBlock, originPos, originBlock, TreePart.LOG));
toAnalyzePos.add(new ToAnalyzePos(originPos, originBlock, originPos, originBlock, TreePartType.LOG, 0));
while(!toAnalyzePos.isEmpty()){
ToAnalyzePos analyzingPos = toAnalyzePos.remove();
tree.addPart(analyzingPos.getTreePart(), analyzingPos.getCheckPos());
tree.addPart(analyzingPos.toTreePart());
analyzedPos.add(analyzingPos);
Collection<ToAnalyzePos> nearbyPos = neighborLogs(world, originPos, originBlock, analyzingPos, analyzedPos);
nearbyPos.removeAll(analyzedPos);
Expand Down Expand Up @@ -68,7 +68,7 @@ private static Collection<ToAnalyzePos> neighborLogs(World world, BlockPos origi
for(int y = -1; y <= 1; y++){
checkPos.setPos(blockPos.getX() + x, blockPos.getY() + y, blockPos.getZ() + z);
Block checkBlock = world.getBlockState(checkPos).getBlock();
ToAnalyzePos analyzed = new ToAnalyzePos(blockPos, parent.getCheckBlock(), checkPos.toImmutable(), checkBlock, getTreePart(checkBlock));
ToAnalyzePos analyzed = new ToAnalyzePos(blockPos, parent.getCheckBlock(), checkPos.toImmutable(), checkBlock, getTreePart(checkBlock), parent.getSequence() + 1);
if(!analyzedPos.contains(analyzed) && shouldIncludeInChain(originPos, originBlock, parent, analyzed)){
neighborLogs.add(analyzed);
}
Expand All @@ -79,22 +79,22 @@ private static Collection<ToAnalyzePos> neighborLogs(World world, BlockPos origi
return neighborLogs;
}

private static TreePart getTreePart(Block checkBlock){
private static TreePartType getTreePart(Block checkBlock){
if(isLogBlock(checkBlock)){
return TreePart.LOG;
return TreePartType.LOG;
}
if(isNetherWartOrShroomlight(checkBlock)){
return TreePart.WART;
return TreePartType.WART;
}
return TreePart.OTHER;
return TreePartType.OTHER;
}

private static boolean shouldIncludeInChain(BlockPos originPos, Block originBlock, ToAnalyzePos parent, ToAnalyzePos check){
if(parent.getTreePart() == TreePart.LOG && isSameTree(originBlock, check)){
if(parent.getTreePartType() == TreePartType.LOG && isSameTree(originBlock, check)){
return true;
}
if(Config.COMMON.getTreesConfiguration().isBreakNetherTreeWarts()){
if(check.getTreePart() == TreePart.WART){
if(check.getTreePartType() == TreePartType.WART){
BlockPos checkBlockPos = check.getCheckPos();
int dx = Math.abs(originPos.getX() - checkBlockPos.getX());
int dz = Math.abs(originPos.getZ() - checkBlockPos.getZ());
Expand All @@ -106,7 +106,7 @@ private static boolean shouldIncludeInChain(BlockPos originPos, Block originBloc

private static boolean isSameTree(Block parentLogBlock, ToAnalyzePos check){
if(Config.COMMON.getTreesConfiguration().isAllowMixedLogs()){
return check.getTreePart() == TreePart.LOG;
return check.getTreePartType() == TreePartType.LOG;
}
else{
return check.getCheckBlock().equals(parentLogBlock);
Expand All @@ -128,25 +128,30 @@ public static boolean destroyInstant(@Nonnull Tree tree, @Nonnull PlayerEntity p
}
}
final boolean isTreeFullyBroken = damageMultiplicand == 0 || rawWeightedUsesLeft >= tree.getLogCount();
tree.getLogs().stream().limit((int) rawWeightedUsesLeft).forEachOrdered(logBlock -> {
final BlockState logState = world.getBlockState(logBlock);
player.addStat(Stats.ITEM_USED.get(logState.getBlock().asItem()));
logState.getBlock().harvestBlock(world, player, logBlock, logState, world.getTileEntity(logBlock), tool);
world.destroyBlock(logBlock, false);
});
tree.getLogs().stream()
.limit((int) rawWeightedUsesLeft)
.map(TreePart::getBlockPos)
.forEachOrdered(logBlockPos -> {
final BlockState logState = world.getBlockState(logBlockPos);
player.addStat(Stats.ITEM_USED.get(logState.getBlock().asItem()));
logState.getBlock().harvestBlock(world, player, logBlockPos, logState, world.getTileEntity(logBlockPos), tool);
world.removeBlock(logBlockPos, false);
});
int toolDamage = (damageMultiplicand * (int) Math.min(tree.getLogCount(), rawWeightedUsesLeft)) - 1;
if(toolDamage > 0){
tool.damageItem(toolDamage, player, (entity) -> {});
}
if(isTreeFullyBroken){
tree.getWarts().forEach(wartPos -> {
final BlockState wartState = world.getBlockState(wartPos);
wartState.getBlock().harvestBlock(world, player, wartPos, wartState, world.getTileEntity(wartPos), tool);
world.removeBlock(wartPos, false);
});
tree.getWarts().stream()
.map(TreePart::getBlockPos)
.forEach(wartPos -> {
final BlockState wartState = world.getBlockState(wartPos);
wartState.getBlock().harvestBlock(world, player, wartPos, wartState, world.getTileEntity(wartPos), tool);
world.removeBlock(wartPos, false);
});
final int radius = Config.COMMON.getTreesConfiguration().getLeavesBreakingForceRadius();
if(radius > 0){
tree.getLogs().stream().max(Comparator.comparingInt(BlockPos::getY)).ifPresent(topLog -> {
tree.getTopMostLog().ifPresent(topLog -> {
BlockPos.Mutable checkPos = new BlockPos.Mutable();
for(int dx = -radius; dx < radius; dx++){
for(int dy = -radius; dy < radius; dy++){
Expand Down Expand Up @@ -178,12 +183,14 @@ public static boolean destroyShift(@Nonnull Tree tree, @Nonnull PlayerEntity pla
return false;
}
}
tree.getTopMostFurthestPart().ifPresent(logBlock -> {
final BlockState logState = world.getBlockState(logBlock);
player.addStat(Stats.ITEM_USED.get(logState.getBlock().asItem()));
logState.getBlock().harvestBlock(world, player, tree.getHitPos(), logState, world.getTileEntity(logBlock), tool);
world.destroyBlock(logBlock, false);
});
tree.getLastSequencePart()
.map(TreePart::getBlockPos)
.ifPresent(logBlock -> {
final BlockState logState = world.getBlockState(logBlock);
player.addStat(Stats.ITEM_USED.get(logState.getBlock().asItem()));
logState.getBlock().harvestBlock(world, player, tree.getHitPos(), logState, world.getTileEntity(logBlock), tool);
world.removeBlock(logBlock, false);
});
int toolDamage = damageMultiplicand;
if(toolDamage > 0){
tool.damageItem(toolDamage, player, (entity) -> {});
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/fr/raksrinana/fallingtree/tree/TreePart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package fr.raksrinana.fallingtree.tree;

import fr.raksrinana.fallingtree.utils.TreePartType;
import net.minecraft.util.math.BlockPos;

public class TreePart{
private final BlockPos blockPos;
private final TreePartType treePartType;
private final int sequence;

public TreePart(BlockPos blockPos, TreePartType treePartType, int sequence){
this.blockPos = blockPos;
this.treePartType = treePartType;
this.sequence = sequence;
}

public BlockPos getBlockPos(){
return blockPos;
}

public int getSequence(){
return sequence;
}

public TreePartType getTreePartType(){
return treePartType;
}
}
21 changes: 16 additions & 5 deletions src/main/java/fr/raksrinana/fallingtree/utils/ToAnalyzePos.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.raksrinana.fallingtree.utils;

import fr.raksrinana.fallingtree.tree.TreePart;
import net.minecraft.block.Block;
import net.minecraft.util.math.BlockPos;
import java.util.Objects;
Expand All @@ -9,21 +10,27 @@ public class ToAnalyzePos implements Comparable<ToAnalyzePos>{
private final Block parentBlock;
private final BlockPos checkPos;
private final Block checkBlock;
private final TreePart treePart;
private final TreePartType treePartType;
private final int sequence;

public ToAnalyzePos(BlockPos parentPos, Block parentBlock, BlockPos checkPos, Block checkBlock, TreePart treePart){
public ToAnalyzePos(BlockPos parentPos, Block parentBlock, BlockPos checkPos, Block checkBlock, TreePartType treePartType, int sequence){
this.parentPos = parentPos;
this.parentBlock = parentBlock;
this.checkPos = checkPos;
this.checkBlock = checkBlock;
this.treePart = treePart;
this.treePartType = treePartType;
this.sequence = sequence;
}

@Override
public int compareTo(ToAnalyzePos o){
return 0;
}

public TreePart toTreePart(){
return new TreePart(getCheckPos(), getTreePartType(), sequence);
}

public Block getCheckBlock(){
return checkBlock;
}
Expand All @@ -40,8 +47,12 @@ public BlockPos getCheckPos(){
return checkPos;
}

public TreePart getTreePart(){
return treePart;
public TreePartType getTreePartType(){
return treePartType;
}

public int getSequence(){
return sequence;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package fr.raksrinana.fallingtree.utils;

public enum TreePart{
public enum TreePartType{
LOG, WART, OTHER
}

0 comments on commit e523490

Please sign in to comment.