Skip to content

Commit

Permalink
Backport 1.15 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Rakambda committed Feb 16, 2020
1 parent b156db3 commit 41893b0
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 31 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## [1.14.4-2.0.3] - 2020-02-16
- Backport 1.15 features
- Add an option in the config to instantly break leaves (defaults to false).
- Added an option to force destroy leaves withing a certain radius. This will be applied from one of the top most log blocks and will destroy all leaves within it (including leaves that shouldn't despawn because another tree is too close, or because they've been placed by a player).

## [1.14.4-1.1.0] - 2019-12-16
- Automatically support all logs by default (now use the blacklist if you want to exclude some logs)
- Automatically support all axes by default (now use the blacklist if you want to exclude some tools)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ Several options are available in the config file:
- max\_log\_count: The maximum number of log a tree can be mad of (if more the mod won't apply).
- preserve_tools: If this option is enabled your tool won't be broken by chopping down a big tree, it'll instead be left with 1 of durability.
- reverse_sneaking: If this option is enabled you'll need to sneak in order to break the whole tree.
- break_leaves: If this is set to true, leaves will despawn instantly when a tree is broken (even if not cut in one shot).
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ curseforge {
changelogType = 'markdown'
addGameVersion "${project.mc_version}"
changelog = getLatestChangeLog()
releaseType = 'beta'
releaseType = "${project.curseforge_release_type}"
mainArtifact(jar) {
displayName = "${project.archivesBaseName}-${project.mc_version}-${project.version}"
}
Expand Down
5 changes: 3 additions & 2 deletions changes.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
- Automatically support all logs by default (now use the blacklist if you want to exclude some logs)
- Automatically support all axes by default (now use the blacklist if you want to exclude some tools)
- Backport 1.15 features
- Add an option in the config to instantly break leaves (defaults to false).
- Added an option to force destroy leaves withing a certain radius. This will be applied from one of the top most log blocks and will destroy all leaves within it (including leaves that shouldn't despawn because another tree is too close, or because they've been placed by a player).
7 changes: 4 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ changelogUrl = https://github.com/RakSrinaNa/FallingTree/blob/1.14.4/CHANGELOG.m

mc_version = 1.14.4

forge_version = 1.14.4-28.1.107
mcp_channel = snapshot
mcp_mappings = 20191216-1.14.3
forge_version = 1.14.4-28.2.1
mcp_channel = stable
mcp_mappings = 58-1.14.4

curseforge_project_id=349559
curseforge_release_type=release

doDeobfJar = true
doSourceJar = true
4 changes: 2 additions & 2 deletions src/main/java/fr/raksrinana/fallingtree/FallingTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
public class FallingTree{
public static final String MOD_ID = "falling_tree";
public static final String MOD_NAME = "Falling Tree";
public static final String VERSION = "1.1.0";
public static final Logger LOGGER = LogManager.getLogger(MOD_NAME);
public static final String VERSION = "2.0.3";
public static final Logger LOGGER = LogManager.getLogger(MOD_ID);

public FallingTree(){
ModLoadingContext.get().registerConfig(ModConfig.Type.COMMON, Config.COMMON_SPEC);
Expand Down
59 changes: 55 additions & 4 deletions src/main/java/fr/raksrinana/fallingtree/ForgeEventSubscriber.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,29 @@

import fr.raksrinana.fallingtree.config.Config;
import fr.raksrinana.fallingtree.tree.TreeHandler;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.tags.BlockTags;
import net.minecraft.util.Direction;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.LogicalSide;
import net.minecraftforge.fml.common.Mod;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.Iterator;
import java.util.Optional;
import java.util.concurrent.ConcurrentLinkedQueue;

@Mod.EventBusSubscriber(modid = FallingTree.MOD_ID, bus = Mod.EventBusSubscriber.Bus.FORGE)
public final class ForgeEventSubscriber{
public static final Logger LOGGER = LogManager.getLogger(FallingTree.MOD_NAME);
private static Collection<LeavesBlockToBreak> leavesToRemove = new ConcurrentLinkedQueue<>();

@SubscribeEvent
public static void onBlockBreakEvent(@Nonnull BlockEvent.BreakEvent event){
Expand All @@ -35,12 +45,53 @@ public static void onBlockBreakEvent(@Nonnull BlockEvent.BreakEvent event){
}

private static boolean isPlayerInRightState(PlayerEntity player){
if(player.abilities.isCreativeMode){
if(player.abilities.isCreativeMode && !FallingTree.isDevBuild()){
return false;
}
if(Config.COMMON.reverseSneaking.get() != player.isSneaking()){
return false;
}
return TreeHandler.canPlayerBreakTree(player);
}

@SubscribeEvent
public static void onNeighborNotifyEvent(BlockEvent.NeighborNotifyEvent event){
if(Config.COMMON.breakLeaves.get() && !event.getWorld().isRemote()){
World world = (World) event.getWorld();
BlockState eventState = event.getState();
Block eventBlock = eventState.getBlock();
BlockPos eventPos = event.getPos();
if(eventBlock.isAir(eventState, world, eventPos)){
for(Direction facing : event.getNotifiedSides()){
BlockPos neighborPos = eventPos.offset(facing);
if(world.isBlockLoaded(neighborPos)){
BlockState neighborState = event.getWorld().getBlockState(neighborPos);
if(BlockTags.LEAVES.contains(neighborState.getBlock())){
leavesToRemove.add(new LeavesBlockToBreak((World) world, neighborPos));
}
}
}
}
}
}

@SubscribeEvent
public static void onServerTick(TickEvent.ServerTickEvent event){
if(event.side == LogicalSide.SERVER && event.phase == TickEvent.Phase.END){
Iterator<LeavesBlockToBreak> iterator = leavesToRemove.iterator();
while(iterator.hasNext()){
LeavesBlockToBreak leavesBlockToBreak = iterator.next();
iterator.remove();
Optional.ofNullable(leavesBlockToBreak.getWorld()).ifPresent(world -> {
if(world.isBlockLoaded(leavesBlockToBreak.getBlockPos())){
BlockState state = world.getBlockState(leavesBlockToBreak.getBlockPos());
if(BlockTags.LEAVES.contains(state.getBlock())){
state.tick(world, leavesBlockToBreak.getBlockPos(), world.getRandom());
state.randomTick(world, leavesBlockToBreak.getBlockPos(), world.getRandom());
}
}
});
}
}
}
}
22 changes: 21 additions & 1 deletion src/main/java/fr/raksrinana/fallingtree/LeavesBlockToBreak.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
package fr.raksrinana.fallingtree;

public class LeavesBlockToBreak{}
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IWorld;
import net.minecraft.world.World;

public class LeavesBlockToBreak{
private final World world;
private final BlockPos blockPos;

public LeavesBlockToBreak(World world, BlockPos blockPos){
this.world = world;
this.blockPos = blockPos;
}

public World getWorld(){
return world;
}

public BlockPos getBlockPos(){
return blockPos;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public class CommonConfig{
public final ForgeConfigSpec.BooleanValue ignoreDurabilityLoss;
public final ForgeConfigSpec.BooleanValue preserveTools;
public final ForgeConfigSpec.BooleanValue reverseSneaking;
public final ForgeConfigSpec.BooleanValue breakLeaves;
public final ForgeConfigSpec.IntValue maxTreeSize;
public final ForgeConfigSpec.IntValue forceBreakLeavesRadius;


public CommonConfig(ForgeConfigSpec.Builder builder){
Expand All @@ -32,6 +34,8 @@ public CommonConfig(ForgeConfigSpec.Builder builder){
maxTreeSize = builder.comment("The maximum size of a tree. If there's more logs than this value the tree won't be cut.").defineInRange("max_log_count", 100, 1, Integer.MAX_VALUE);
preserveTools = builder.comment("When set to true, when a tree is broken and the tool is about to break we will just break one block and not the whole tree.").define("preserve_tools", false);
reverseSneaking = builder.comment("When set to true, a tree will only be chopped down if the player is sneaking").define("reverse_sneaking", false);
breakLeaves = builder.comment("When set to true, leaves that should naturally break will be broken instantly").define("break_leaves", false);
forceBreakLeavesRadius = builder.comment("Radius to force break leaves. If another tree is still holding the leaves they'll still be broken. If the leaves are persistent (placed by player) they'll also be destroyed. The radius is applied from one of the top most log blocks. break_leaves must be activated for this to take effect.").defineInRange("force_break_leaves_radius", 0, 0, 10);
}

public Stream<Block> getWhitelistedLogs(){
Expand Down
16 changes: 7 additions & 9 deletions src/main/java/fr/raksrinana/fallingtree/tree/Tree.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@

import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IWorld;
import net.minecraft.world.World;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;
import java.util.*;
import java.util.stream.Stream;

public class Tree{
private final IWorld world;
private final World world;
private final Set<BlockPos> logs;
private final BlockPos hitPos;

public Tree(@Nonnull IWorld world, @Nonnull BlockPos blockPos){
public Tree(@Nonnull World world, @Nonnull BlockPos blockPos){
this.world = world;
this.hitPos = blockPos;
this.logs = new LinkedHashSet<>();
Expand All @@ -37,12 +35,12 @@ public BlockPos getHitPos(){
}

@Nonnull
public IWorld getWorld(){
public World getWorld(){
return this.world;
}

@Nonnull
public Stream<BlockPos> getLogs(){
return this.logs.stream();
public Collection<BlockPos> getLogs(){
return this.logs;
}
}
34 changes: 27 additions & 7 deletions src/main/java/fr/raksrinana/fallingtree/tree/TreeHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

import fr.raksrinana.fallingtree.config.Config;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.AxeItem;
import net.minecraft.item.ItemStack;
import net.minecraft.tags.BlockTags;
import net.minecraft.tags.Tag;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IWorld;
import net.minecraft.world.World;
import net.minecraftforge.common.Tags;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.*;
Expand All @@ -36,7 +35,7 @@ public static Optional<Tree> getTree(@Nonnull IWorld world, @Nonnull BlockPos bl
Queue<BlockPos> toAnalyzePos = new LinkedList<>();
Set<BlockPos> analyzedPos = new HashSet<>();
Block logBlock = world.getBlockState(blockPos).getBlock();
Tree tree = new Tree(world, blockPos);
Tree tree = new Tree((World) world, blockPos);
toAnalyzePos.add(blockPos);
while(!toAnalyzePos.isEmpty()){
BlockPos analyzingPos = toAnalyzePos.remove();
Expand All @@ -52,12 +51,13 @@ public static Optional<Tree> getTree(@Nonnull IWorld world, @Nonnull BlockPos bl
@Nonnull
private static Collection<BlockPos> neighborLogs(@Nonnull IWorld world, @Nonnull Block logBlock, @Nonnull BlockPos blockPos, @Nonnull Collection<BlockPos> analyzedPos){
List<BlockPos> neighborLogs = new LinkedList<>();
final BlockPos.MutableBlockPos checkPos = new BlockPos.MutableBlockPos();
for(int x = -1; x <= 1; x++){
for(int z = -1; z <= 1; z++){
for(int y = -1; y <= 1; y++){
BlockPos pos = blockPos.add(x, y, z);
if(!analyzedPos.contains(pos) && isSameLog(world, pos, logBlock)){
neighborLogs.add(blockPos.add(x, y, z));
checkPos.setPos(blockPos.getX() + x, blockPos.getY() + y, blockPos.getZ() + z);
if(!analyzedPos.contains(checkPos) && isSameLog(world, checkPos, logBlock)){
neighborLogs.add(checkPos.toImmutable());
}
}
}
Expand All @@ -78,12 +78,32 @@ public static boolean destroy(@Nonnull Tree tree, @Nonnull PlayerEntity player,
if(toolUsesLeft < 1){
return false;
}
tree.getLogs().limit(toolUsesLeft).forEachOrdered(logBlock -> {
final boolean isFullyBroken = Config.COMMON.ignoreDurabilityLoss.get() || (tool.getMaxDamage() - tool.getDamage()) >= tree.getLogCount();
tree.getLogs().stream().limit(toolUsesLeft).forEachOrdered(logBlock -> {
if(!Config.COMMON.ignoreDurabilityLoss.get() && tree.getWorld() instanceof World){
tool.onBlockDestroyed((World) tree.getWorld(), tree.getWorld().getBlockState(logBlock), logBlock, player);
}
tree.getWorld().destroyBlock(logBlock, true);
});
if(isFullyBroken){
final int radius = Config.COMMON.forceBreakLeavesRadius.get();
if(radius > 0){
tree.getLogs().stream().max(Comparator.comparingInt(BlockPos::getY)).ifPresent(topLog -> {
BlockPos.MutableBlockPos checkPos = new BlockPos.MutableBlockPos();
for(int dx = -radius; dx < radius; dx++){
for(int dy = -radius; dy < radius; dy++){
for(int dz = -radius; dz < radius; dz++){
checkPos.setPos(topLog.getX() + dx, topLog.getY() + dy, topLog.getZ() + dz);
final BlockState checkState = tree.getWorld().getBlockState(checkPos);
if(BlockTags.LEAVES.contains(checkState.getBlock())){
tree.getWorld().destroyBlock(checkPos, true);
}
}
}
}
});
}
}
return true;
}

Expand Down
6 changes: 4 additions & 2 deletions update.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
"homepage": "https://github.com/RakSrinaNa/FallingTree",
"changelog": "https://github.com/RakSrinaNa/FallingTree/blob/1.14.4/CHANGELOG.md",
"1.14.4": {
"2.0.3": "https://github.com/RakSrinaNa/FallingTree/blob/1.14.4/CHANGELOG.md",
"1.1.0": "https://github.com/RakSrinaNa/FallingTree/blob/1.14.4/CHANGELOG.md",
"1.0.4": "https://github.com/RakSrinaNa/FallingTree/blob/1.14.4/CHANGELOG.md",
"1.0.3": "https://github.com/RakSrinaNa/FallingTree/blob/1.14.4/CHANGELOG.md",
"1.0.2": "https://github.com/RakSrinaNa/FallingTree/blob/1.14.4/CHANGELOG.md",
"1.0.1": "https://github.com/RakSrinaNa/FallingTree/blob/1.14.4/CHANGELOG.md",
"1.0.0": "https://github.com/RakSrinaNa/FallingTree/blob/1.14.4/CHANGELOG.md"
},
"promos": {
"1.14.4-latest": "1.0.4",
"1.14.4-recommended": "1.0.4"
"1.14.4-latest": "2.0.3",
"1.14.4-recommended": "2.0.3"
}
}

0 comments on commit 41893b0

Please sign in to comment.