Add ChunkWatchEvent (#464)

Signed-off-by: Sergey Shatunov <me@aur.rocks>
Co-authored-by: Max <maxh2709@gmail.com>
This commit is contained in:
Sergey Shatunov
2024-01-10 05:05:28 +08:00
committed by GitHub
parent fd7e62b2a9
commit e986c607c3
9 changed files with 227 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
/*
* This file is part of architectury.
* Copyright (C) 2020, 2021, 2022 architectury
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package dev.architectury.mixin.forge.minecraftforge;
import dev.architectury.event.events.common.ChunkWatchEvent;
import net.minecraft.server.level.ChunkMap;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.chunk.LevelChunk;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(ChunkMap.class)
public abstract class MixinChunkMap {
@Inject(method = "markChunkPendingToSend(Lnet/minecraft/server/level/ServerPlayer;Lnet/minecraft/world/level/chunk/LevelChunk;)V", at = @At("TAIL"))
private static void watch(ServerPlayer player, LevelChunk chunk, CallbackInfo ci) {
ChunkWatchEvent.WATCH.invoker().listen(chunk, player.serverLevel(), player);
}
@Inject(method = "dropChunk(Lnet/minecraft/server/level/ServerPlayer;Lnet/minecraft/world/level/ChunkPos;)V", at = @At("HEAD"))
private static void unwatch(ServerPlayer player, ChunkPos chunkPos, CallbackInfo ci) {
ChunkWatchEvent.UNWATCH.invoker().listen(chunkPos, player.serverLevel(), player);
}
}

View File

@@ -0,0 +1,38 @@
/*
* This file is part of architectury.
* Copyright (C) 2020, 2021, 2022 architectury
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package dev.architectury.mixin.forge.minecraftforge;
import dev.architectury.event.events.common.ChunkWatchEvent;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.network.PlayerChunkSender;
import net.minecraft.server.network.ServerGamePacketListenerImpl;
import net.minecraft.world.level.chunk.LevelChunk;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(PlayerChunkSender.class)
public abstract class MixinPlayerChunkSender {
@Inject(method = "sendChunk", at = @At("TAIL"))
private static void sendChunk(ServerGamePacketListenerImpl packetListener, ServerLevel level, LevelChunk chunk, CallbackInfo ci) {
ChunkWatchEvent.SENT.invoker().listen(chunk, level, packetListener.player);
}
}

View File

@@ -6,6 +6,8 @@
"client": [
],
"mixins": [
"minecraftforge.MixinChunkMap",
"minecraftforge.MixinPlayerChunkSender",
"minecraftforge.MixinChunkSerializer",
"minecraftforge.MixinEntitySpawnExtension"
],