Skip to content

Commit

Permalink
Fix tree felling helper memory leak (#2677)
Browse files Browse the repository at this point in the history
  • Loading branch information
omergunr100 authored Jan 4, 2025
1 parent 1f2159e commit f5bdec0
Showing 1 changed file with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.minecraftforge.fml.LogicalSide;
import net.minecraftforge.fml.common.Mod;

import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;

import java.util.*;
Expand All @@ -29,7 +30,7 @@ public class TreeFellingHelper {
private final Deque<BlockPos> orderedBlocks;
private int tick;

public static Set<TreeFellingHelper> helpers = new HashSet<>();
public static final List<TreeFellingHelper> helpers = ObjectArrayList.of();

private TreeFellingHelper(ServerPlayer player, ItemStack tool, Deque<BlockPos> orderedBlocks) {
this.player = player;
Expand Down Expand Up @@ -83,12 +84,15 @@ public static void fellTree(ItemStack stack, Level level, BlockState origin, Blo

@SubscribeEvent
public static void onWorldTick(TickEvent.LevelTickEvent event) {
if (event.phase == TickEvent.Phase.START && event.side == LogicalSide.SERVER) {
for (var helper : helpers) {
if (event.phase == TickEvent.Phase.START && event.side == LogicalSide.SERVER && !helpers.isEmpty()) {
var iterator = helpers.iterator();
while (iterator.hasNext()) {
var helper = iterator.next();
if (event.level == helper.player.level()) {
if (helper.orderedBlocks.isEmpty() || helper.tool.isEmpty() ||
!(hasBehaviorsTag(helper.player.getMainHandItem()) &&
getBehaviorsTag(helper.player.getMainHandItem()).getBoolean(TREE_FELLING_KEY))) {
iterator.remove();
continue;
}
if (helper.tick % ConfigHolder.INSTANCE.tools.treeFellingDelay == 0)
Expand Down

0 comments on commit f5bdec0

Please sign in to comment.