Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Lora4967 committed Mar 30, 2024
1 parent 68d0990 commit 3ceeb53
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 9 deletions.
73 changes: 67 additions & 6 deletions src/main/java/me/earthme/millacat/hook/ServerThreadingHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import com.google.common.collect.Maps;
import it.unimi.dsi.fastutil.Pair;
import it.unimi.dsi.fastutil.longs.Long2BooleanLinkedOpenHashMap;
import it.unimi.dsi.fastutil.longs.Long2BooleanMap;
import it.unimi.dsi.fastutil.longs.Long2BooleanOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet;
import me.earthme.millacat.concurrent.SplittingTraverseTask;
import me.earthme.millacat.concurrent.thread.TickForkJoinWorker;
import me.earthme.millacat.concurrent.thread.TickThreadImpl;
Expand All @@ -21,6 +25,7 @@
import net.minecraft.world.ticks.ScheduledTick;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bukkit.Chunk;
import org.bukkit.craftbukkit.v1_18_R2.SpigotTimings;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -128,11 +133,11 @@ public static void callGlobalTileEntityTickTask(ServerLevel levelIn){
}

final AtomicInteger taskCounter = worldTaskCount.get(levelIn);
for (Map.Entry<Long, List<Locatable>> split : groupByChunk(levelIn.blockEntityTickers).entrySet()){
for (List<Locatable> split : remerge(groupByChunk(levelIn.blockEntityTickers))){
taskCounter.getAndIncrement();
tileEntityThreadPool.execute(()->{
try {
for (Locatable l : split.getValue()){
for (Locatable l : split){
final TickingBlockEntity tickingblockentity = ((TickingBlockEntity) l);

if (tickingblockentity.isRemoved()) {
Expand Down Expand Up @@ -170,6 +175,62 @@ public static Map<Long, List<Locatable>> groupByChunk(Collection<? extends Locat
return dataList.stream().collect(Collectors.groupingBy(Locatable::getChunkKey, LinkedHashMap::new, Collectors.toList()));
}

public static List<ChunkPos> getNeighbors(ChunkPos pos) {
List<ChunkPos> neighbors = new ArrayList<>();
for (int dx = -1; dx <= 1; dx++) {
for (int dz = -1; dz <= 1; dz++) {
if (dx != 0 || dz != 0) {
neighbors.add(new ChunkPos(pos.x + dx, pos.z + dz));
}
}
}
return neighbors;
}

public static Collection<List<Locatable>> remerge(Map<Long, List<Locatable>> map) {
Map<Long, List<Locatable>> aggregatedMap = new HashMap<>();
Set<Long> visited = new HashSet<>();

for (Long chunkPos : map.keySet()) {
if (visited.contains(chunkPos)) {
continue; // 如果已访问过该 Chunk,则跳过
}

List<Locatable> locatables = new ArrayList<>();
Queue<ChunkPos> queue = new LinkedList<>();
queue.offer(new ChunkPos(chunkPos));
visited.add(chunkPos);

while (!queue.isEmpty()) {
ChunkPos currentPos = queue.poll();
locatables.addAll(map.get(chunkPos));

for (ChunkPos neighbor : getNeighbors(currentPos)) {
Long neighborKey = neighbor.toLong();
if (!visited.contains(neighborKey) && map.containsKey(neighborKey) &&
distance(currentPos,neighbor) <= 2) {
queue.offer(neighbor);
visited.add(neighborKey);
}
}
}

long newKey = (chunkPos >> 32) << 32 | (chunkPos & 0xffffffffL); // 更新 key 为最左上角的 ChunkPos
aggregatedMap.put(newKey, locatables);
}


return aggregatedMap.values();
}


private static double distance(ChunkPos point1, ChunkPos point2) {
int deltaX = point2.x - point1.x;
int deltaZ = point2.z - point1.z;

return Math.sqrt(deltaX * deltaX + deltaZ * deltaZ);
}

public static <T> void postRunScheduledTicks(LevelTicks<T> ticks, BiConsumer<BlockPos, T> action){
final List<ScheduledTick<T>> scheduledTicks = new ArrayList<>();
final AtomicInteger taskCounter = worldTaskCount.get(ticks.level);
Expand All @@ -191,11 +252,11 @@ public static <T> void postRunScheduledTicks(LevelTicks<T> ticks, BiConsumer<Blo

final List<Runnable> toRun = new ArrayList<>();

for (Map.Entry<Long, List<Locatable>> split : groupByChunk(scheduledTicks).entrySet()){
for (List<Locatable> split : remerge(groupByChunk(scheduledTicks))){
taskCounter.getAndIncrement();
toRun.add(() -> {
try {
for (Locatable l : split.getValue()){
for (Locatable l : split){
final ScheduledTick<T> scheduledTick = ((ScheduledTick<T>) l);

action.accept(scheduledTick.pos(), scheduledTick.type());
Expand All @@ -215,11 +276,11 @@ public static void postEntityTicKTask(ServerLevel level){
final AtomicInteger taskCounter = worldTaskCount.get(level);
final List<Runnable> toRun = new ArrayList<>();

for (Map.Entry<Long, List<Locatable>> split : groupByChunk(level.entityTickList.entities).entrySet()){
for (List<Locatable> split : remerge(groupByChunk(level.entityTickList.entities))){
taskCounter.getAndIncrement();
toRun.add(() -> {
try {
for (Locatable entityInO : split.getValue()){
for (Locatable entityInO : split){
Entity entityIn = ((Entity) entityInO);
if (!entityIn.isRemoved()) {
if (level.shouldDiscardEntity(entityIn)) {
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/net/minecraftforge/common/ForgeHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -434,10 +434,7 @@ public static void onLivingJump(LivingEntity entity)
@Nullable
public static ItemEntity onPlayerTossEvent(@Nonnull Player player, @Nonnull ItemStack item, boolean includeName)
{
player.captureDrops(Lists.newArrayList());
ItemEntity ret = player.drop(item, false, includeName);
player.captureDrops(null);

if (ret == null)
return null;

Expand Down

0 comments on commit 3ceeb53

Please sign in to comment.