diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ae48d15..7c64eb9d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/changes.md b/changes.md index 8847f375..fde0d03f 100644 --- a/changes.md +++ b/changes.md @@ -1 +1 @@ -- Use mutable BlockPos when checking tree to improve a bit performances. +- Delay breaking leaves by a few ticks making (fixes #6) diff --git a/gradle.properties b/gradle.properties index 70acb445..5d171dbb 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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 diff --git a/src/main/java/fr/raksrinana/fallingtree/FallingTree.java b/src/main/java/fr/raksrinana/fallingtree/FallingTree.java index a8532662..4b4d5c97 100644 --- a/src/main/java/fr/raksrinana/fallingtree/FallingTree.java +++ b/src/main/java/fr/raksrinana/fallingtree/FallingTree.java @@ -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(){ diff --git a/src/main/java/fr/raksrinana/fallingtree/ForgeEventSubscriber.java b/src/main/java/fr/raksrinana/fallingtree/ForgeEventSubscriber.java index 80482ed2..608a5e82 100644 --- a/src/main/java/fr/raksrinana/fallingtree/ForgeEventSubscriber.java +++ b/src/main/java/fr/raksrinana/fallingtree/ForgeEventSubscriber.java @@ -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; @@ -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 scheduledLeavesBreaking = new ConcurrentSet<>(); + @SubscribeEvent public static void onBlockBreakEvent(@Nonnull BlockEvent.BreakEvent event){ if(!event.isCanceled() && !event.getWorld().isRemote()){ @@ -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(); @@ -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 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(); + } + } + } + } } diff --git a/src/main/java/fr/raksrinana/fallingtree/ScheduledLeafBreak.java b/src/main/java/fr/raksrinana/fallingtree/ScheduledLeafBreak.java index 9cfefb4e..987ec39b 100644 --- a/src/main/java/fr/raksrinana/fallingtree/ScheduledLeafBreak.java +++ b/src/main/java/fr/raksrinana/fallingtree/ScheduledLeafBreak.java @@ -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()); + } } diff --git a/update.json b/update.json index 4ac5e83b..afa86c14 100644 --- a/update.json +++ b/update.json @@ -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" } } \ No newline at end of file