diff --git a/src/main/java/ru/dbotthepony/mc/otm/matter/MatterGrid.java b/src/main/java/ru/dbotthepony/mc/otm/matter/MatterGrid.java index c4bae03ed..ab02f4552 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/matter/MatterGrid.java +++ b/src/main/java/ru/dbotthepony/mc/otm/matter/MatterGrid.java @@ -23,10 +23,10 @@ public class MatterGrid implements IMatterGridListener { private final Set cells = new HashSet<>(); private final Set listeners = new HashSet<>(); - private static final Set> discovering_neighbours = new HashSet<>(); + private static final WeakHashMap>> discovering_neighbours = new WeakHashMap<>(); public static void scheduleDiscoverNeighbours(IMatterGridCell cell, BlockPos pos, Level level) { - discovering_neighbours.add(() -> !cell.isValidMatterCell() || cell.connectOrCreateMatterGrid(pos, level, true)); + discovering_neighbours.computeIfAbsent(level, (key) -> new HashSet<>()).add(() -> !cell.isValidMatterCell() || cell.connectOrCreateMatterGrid(pos, level, true)); } @SubscribeEvent @@ -40,11 +40,16 @@ public class MatterGrid implements IMatterGridListener { } @SubscribeEvent - public static void discoverNeighbours(TickEvent.ServerTickEvent event) { - if (event.phase == TickEvent.Phase.END && discovering_neighbours.size() != 0) { + public static void discoverNeighbours(TickEvent.WorldTickEvent event) { + if (event.phase != TickEvent.Phase.START) + return; + + var set = discovering_neighbours.get(event.world); + + if (set != null && set.size() != 0) { ArrayList> invalid = new ArrayList<>(); - for (Supplier cell : discovering_neighbours) { + for (Supplier cell : set) { if (cell.get()) { invalid.add(cell); } @@ -52,9 +57,13 @@ public class MatterGrid implements IMatterGridListener { if (invalid.size() != 0) { for (Supplier cell : invalid) { - discovering_neighbours.remove(cell); + set.remove(cell); } } + + if (set.size() == 0) { + discovering_neighbours.remove(event.world); + } } }