Skip to content

Commit

Permalink
Delay breaking leaves by a few ticks, fixes #6
Browse files Browse the repository at this point in the history
  • Loading branch information
Rakambda committed Feb 29, 2020
1 parent 324d6c3 commit d1c9b8d
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## [1.15.2-2.0.4] - 2020-02-29
- Delay breaking leaves by a few ticks making (fixes #6)

## [1.15.2-2.0.3] - 2020-01-28
- 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). (#5)

Expand Down
2 changes: 1 addition & 1 deletion changes.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
- Use mutable BlockPos when checking tree to improve a bit performances.
- Delay breaking leaves by a few ticks making (fixes #6)
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ changelogUrl = https://github.com/RakSrinaNa/FallingTree/blob/1.15.2/CHANGELOG.m

mc_version = 1.15.2

forge_version = 1.15.2-31.1.12
forge_version = 1.15.2-31.1.18
mcp_channel = snapshot
mcp_mappings = 20200216-1.15.1
mcp_mappings = 20200229-1.15.1

curseforge_project_id=349559
curseforge_release_type=release
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/fr/raksrinana/fallingtree/FallingTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
public class FallingTree{
public static final String MOD_ID = "falling_tree";
public static final String MOD_NAME = "Falling Tree";
public static final String VERSION = "2.0.3";
public static final String VERSION = "2.0.4";
public static final Logger LOGGER = LogManager.getLogger(MOD_ID);

public FallingTree(){
Expand Down
42 changes: 39 additions & 3 deletions src/main/java/fr/raksrinana/fallingtree/ForgeEventSubscriber.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import fr.raksrinana.fallingtree.config.Config;
import fr.raksrinana.fallingtree.tree.TreeHandler;
import io.netty.util.internal.ConcurrentSet;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
Expand All @@ -10,15 +11,21 @@
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.IWorld;
import net.minecraft.world.server.ServerWorld;
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 javax.annotation.Nonnull;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

@Mod.EventBusSubscriber(modid = FallingTree.MOD_ID, bus = Mod.EventBusSubscriber.Bus.FORGE)
public final class ForgeEventSubscriber{
private static final Set<ScheduledLeafBreak> scheduledLeavesBreaking = new ConcurrentSet<>();

@SubscribeEvent
public static void onBlockBreakEvent(@Nonnull BlockEvent.BreakEvent event){
if(!event.isCanceled() && !event.getWorld().isRemote()){
Expand Down Expand Up @@ -50,7 +57,7 @@ private static boolean isPlayerInRightState(PlayerEntity player){
@SubscribeEvent
public static void onNeighborNotifyEvent(BlockEvent.NeighborNotifyEvent event){
if(Config.COMMON.breakLeaves.get() && !event.getWorld().isRemote()){
IWorld world = event.getWorld();
ServerWorld world = (ServerWorld) event.getWorld();
BlockState eventState = event.getState();
Block eventBlock = eventState.getBlock();
BlockPos eventPos = event.getPos();
Expand All @@ -60,11 +67,40 @@ public static void onNeighborNotifyEvent(BlockEvent.NeighborNotifyEvent event){
if(world.isBlockLoaded(neighborPos)){
BlockState neighborState = event.getWorld().getBlockState(neighborPos);
if(BlockTags.LEAVES.contains(neighborState.getBlock())){
neighborState.getBlock().randomTick(neighborState, (ServerWorld) world, neighborPos, world.getRandom());
scheduledLeavesBreaking.add(new ScheduledLeafBreak(world, neighborPos, 4));
}
}
}
}
}
}

@SubscribeEvent
public static void onServerTick(TickEvent.ServerTickEvent event){
if(event.side == LogicalSide.SERVER && event.phase == TickEvent.Phase.END){
Iterator<ScheduledLeafBreak> leavesBreak = scheduledLeavesBreaking.iterator();
while(leavesBreak.hasNext()){
ScheduledLeafBreak scheduledLeafBreak = leavesBreak.next();
ServerWorld world = scheduledLeafBreak.getWorld();
if(scheduledLeafBreak.getRemainingTicks() <= 0){
if(world.isBlockLoaded(scheduledLeafBreak.getBlockPos())){
BlockState state = world.getBlockState(scheduledLeafBreak.getBlockPos());
Block block = state.getBlock();
if(BlockTags.LEAVES.contains(block)){
block.randomTick(state, world, scheduledLeafBreak.getBlockPos(), world.getRandom());
}
else{
leavesBreak.remove();
}
}
else{
leavesBreak.remove();
}
}
else{
scheduledLeafBreak.tick();
}
}
}
}
}
46 changes: 44 additions & 2 deletions src/main/java/fr/raksrinana/fallingtree/ScheduledLeafBreak.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,50 @@
package fr.raksrinana.fallingtree;

import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IWorld;
import net.minecraft.world.server.ServerWorld;
import java.util.Objects;

public class ScheduledLeafBreak{
public ScheduledLeafBreak(IWorld world, BlockPos neighborPos){}
private final ServerWorld world;
private final BlockPos blockPos;
private int remainingTicks;

public ScheduledLeafBreak(ServerWorld world, BlockPos blockPos, int remainingTicks){
this.world = world;
this.blockPos = blockPos;
this.remainingTicks = remainingTicks;
}

public void tick(){
this.remainingTicks--;
}

public int getRemainingTicks(){
return remainingTicks;
}

public BlockPos getBlockPos(){
return blockPos;
}

public ServerWorld getWorld(){
return world;
}

@Override
public boolean equals(Object o){
if(this == o){
return true;
}
if(!(o instanceof ScheduledLeafBreak)){
return false;
}
ScheduledLeafBreak that = (ScheduledLeafBreak) o;
return Objects.equals(getWorld(), that.getWorld()) && Objects.equals(getBlockPos(), that.getBlockPos());
}

@Override
public int hashCode(){
return Objects.hash(getWorld(), getBlockPos());
}
}
5 changes: 3 additions & 2 deletions update.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
"homepage": "https://github.com/RakSrinaNa/FallingTree",
"changelog": "https://github.com/RakSrinaNa/FallingTree/blob/1.15.2/CHANGELOG.md",
"1.15.2": {
"2.0.4": "https://github.com/RakSrinaNa/FallingTree/blob/1.15.2/CHANGELOG.md",
"2.0.3": "https://github.com/RakSrinaNa/FallingTree/blob/1.15.2/CHANGELOG.md",
"2.0.2": "https://github.com/RakSrinaNa/FallingTree/blob/1.15.2/CHANGELOG.md"
},
"promos": {
"1.15.2-latest": "2.0.3",
"1.15.2-recommended": "2.0.3"
"1.15.2-latest": "2.0.4",
"1.15.2-recommended": "2.0.4"
}
}

0 comments on commit d1c9b8d

Please sign in to comment.