Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit 72a45a3

Browse files
Add ChunkEvent (#176)
* Add ChunkEvent * Update patchwork-events-world/src/main/java/net/patchworkmc/impl/event/world/WorldEventsClient.java Co-authored-by: Glitch <glitchieproductionsofficial@gmail.com> Co-authored-by: Glitch <glitchieproductionsofficial@gmail.com>
1 parent 8437561 commit 72a45a3

5 files changed

Lines changed: 186 additions & 0 deletions

File tree

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.minecraftforge.event.world;
21+
22+
import net.minecraftforge.common.MinecraftForge;
23+
24+
import net.minecraft.world.IWorld;
25+
import net.minecraft.world.chunk.Chunk;
26+
27+
import net.patchworkmc.impl.event.world.WorldEvents;
28+
29+
/**
30+
* ChunkEvent is fired when an event involving a chunk occurs.<br>
31+
* If a method utilizes this {@link net.minecraftforge.eventbus.api.Event} as
32+
* its parameter, the method will receive every child event of this class.<br>
33+
* <br>
34+
* {@link #Chunk} contains the Chunk this event is affecting.<br>
35+
* <br>
36+
* All children of this event are fired on the
37+
* {@link MinecraftForge#EVENT_BUS}.<br>
38+
*/
39+
public class ChunkEvent extends WorldEvent {
40+
private final Chunk chunk;
41+
42+
public ChunkEvent(Chunk chunk) {
43+
super(WorldEvents.getWorldForChunk(chunk));
44+
this.chunk = chunk;
45+
}
46+
47+
public ChunkEvent(Chunk chunk, IWorld world) {
48+
super(world);
49+
this.chunk = chunk;
50+
}
51+
52+
public Chunk getChunk() {
53+
return chunk;
54+
}
55+
56+
/**
57+
* ChunkEvent.Load is fired when vanilla Minecraft attempts to load a Chunk into
58+
* the world.<br>
59+
* This event is fired during chunk loading in <br>
60+
* {@link ChunkProviderClient#loadChunk(int, int)}, <br>
61+
* Chunk.onChunkLoad(). <br>
62+
* <br>
63+
* This event is not {@link net.minecraftforge.eventbus.api.Cancelable}.<br>
64+
* <br>
65+
* This event does not have a result. {@link HasResult} <br>
66+
* <br>
67+
* This event is fired on the {@link MinecraftForge#EVENT_BUS}.<br>
68+
*/
69+
public static class Load extends ChunkEvent {
70+
public Load(Chunk chunk) {
71+
super(chunk);
72+
}
73+
}
74+
75+
/**
76+
* ChunkEvent.Unload is fired when vanilla Minecraft attempts to unload a Chunk
77+
* from the world.<br>
78+
* This event is fired during chunk unloading in <br>
79+
* Chunk.onChunkUnload(). <br>
80+
* <br>
81+
* This event is not {@link net.minecraftforge.eventbus.api.Cancelable}.<br>
82+
* <br>
83+
* This event does not have a result. {@link HasResult} <br>
84+
* <br>
85+
* This event is fired on the {@link MinecraftForge#EVENT_BUS}.<br>
86+
*/
87+
public static class Unload extends ChunkEvent {
88+
public Unload(Chunk chunk) {
89+
super(chunk);
90+
}
91+
}
92+
}
93+

patchwork-events-world/src/main/java/net/patchworkmc/impl/event/world/WorldEvents.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@
2222
import java.util.Collections;
2323
import java.util.List;
2424

25+
import javax.annotation.Nullable;
26+
2527
import net.minecraftforge.common.MinecraftForge;
2628
import net.minecraftforge.event.world.BlockEvent;
29+
import net.minecraftforge.event.world.ChunkEvent;
2730
import net.minecraftforge.event.world.ChunkWatchEvent;
2831
import net.minecraftforge.event.world.WorldEvent;
2932

@@ -39,10 +42,13 @@
3942
import net.minecraft.util.math.ChunkPos;
4043
import net.minecraft.world.IWorld;
4144
import net.minecraft.world.biome.Biome;
45+
import net.minecraft.world.chunk.Chunk;
46+
import net.minecraft.world.chunk.WorldChunk;
4247
import net.minecraft.world.dimension.DimensionType;
4348
import net.minecraft.world.level.LevelInfo;
4449

4550
import net.fabricmc.api.ModInitializer;
51+
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerChunkEvents;
4652
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerWorldEvents;
4753

4854
public class WorldEvents implements ModInitializer {
@@ -87,6 +93,16 @@ public static void fireChunkWatch(boolean watch, ServerPlayerEntity entity, Chun
8793
}
8894
}
8995

96+
/**
97+
* Called by ChunkEvent.
98+
* @return the IWorld instance holding the given chunk, null if not applicable.
99+
*/
100+
@Nullable
101+
public static IWorld getWorldForChunk(Chunk chunk) {
102+
// replaces IForgeChunk.getWorldForge()
103+
return chunk instanceof WorldChunk ? ((WorldChunk) chunk).getWorld() : null;
104+
}
105+
90106
@Override
91107
public void onInitialize() {
92108
ServerWorldEvents.LOAD.register((server, world) -> {
@@ -96,5 +112,10 @@ public void onInitialize() {
96112
onWorldLoad(world);
97113
}
98114
});
115+
116+
// Fire ChunkEvent.Load on server side, the other location is in MixinThreadedAnvilChunkStorage
117+
ServerChunkEvents.CHUNK_LOAD.register((server, chunk) -> MinecraftForge.EVENT_BUS.post(new ChunkEvent.Load(chunk)));
118+
// Fire ChunkEvent.Unload on server side
119+
ServerChunkEvents.CHUNK_UNLOAD.register((server, chunk) -> MinecraftForge.EVENT_BUS.post(new ChunkEvent.Unload(chunk)));
99120
}
100121
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.patchworkmc.impl.event.world;
21+
22+
import net.minecraftforge.common.MinecraftForge;
23+
import net.minecraftforge.event.world.ChunkEvent;
24+
25+
import net.fabricmc.api.ClientModInitializer;
26+
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientChunkEvents;
27+
28+
public class WorldEventsClient implements ClientModInitializer {
29+
@Override
30+
public void onInitializeClient() {
31+
///////////////////////////////////////////////
32+
/// ChunkEvent.Load and Unload on client side
33+
///////////////////////////////////////////////
34+
ClientChunkEvents.CHUNK_LOAD.register((server, chunk) -> MinecraftForge.EVENT_BUS.post(new ChunkEvent.Load(chunk)));
35+
ClientChunkEvents.CHUNK_UNLOAD.register((server, chunk) -> MinecraftForge.EVENT_BUS.post(new ChunkEvent.Unload(chunk)));
36+
// Fabric fires the chunk unload event after it's been removed from the client's chunk graph. This probably won't cause any issues.
37+
/* ClientChunkManager.unload(II)V
38+
* if (method_20181(chunk, x, z)) {
39+
* net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new net.minecraftforge.event.world.ChunkEvent.Unload(chunk));
40+
* this.chunks.method_20183(i, chunk, (WorldChunk)null);
41+
* ClientChunkEvents.CHUNK_UNLOAD
42+
* }
43+
*/
44+
}
45+
}

patchwork-events-world/src/main/java/net/patchworkmc/mixin/event/world/MixinThreadedAnvilChunkStorage.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,21 @@
2222
import org.spongepowered.asm.mixin.Mixin;
2323
import org.spongepowered.asm.mixin.Shadow;
2424
import org.spongepowered.asm.mixin.injection.At;
25+
import org.spongepowered.asm.mixin.injection.At.Shift;
2526
import org.spongepowered.asm.mixin.injection.Inject;
2627
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
28+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
29+
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
30+
import net.minecraftforge.common.MinecraftForge;
31+
import net.minecraftforge.event.world.ChunkEvent;
2732

33+
import net.minecraft.nbt.CompoundTag;
2834
import net.minecraft.network.Packet;
2935
import net.minecraft.server.network.ServerPlayerEntity;
3036
import net.minecraft.server.world.ServerWorld;
3137
import net.minecraft.server.world.ThreadedAnvilChunkStorage;
3238
import net.minecraft.util.math.ChunkPos;
39+
import net.minecraft.world.chunk.Chunk;
3340

3441
import net.patchworkmc.impl.event.world.WorldEvents;
3542

@@ -44,4 +51,16 @@ private void fireWatchEvents(ServerPlayerEntity player, ChunkPos pos, Packet<?>[
4451
WorldEvents.fireChunkWatch(withinViewDistance, player, pos, this.world);
4552
}
4653
}
54+
55+
// Lambda in "private CompletableFuture<Either<Chunk, Unloaded>> method_20619(ChunkPos chunkPos)"
56+
// chunk.setLastSaveTime(this.world.getTime());
57+
// + MinecraftForge.EVENT_BUS.post(new ChunkEvent.Load(chunk));
58+
// return Either.left(chunk);
59+
@SuppressWarnings("rawtypes")
60+
@Inject(method = "method_17256", locals = LocalCapture.CAPTURE_FAILHARD, at = @At(value = "INVOKE", shift = Shift.AFTER, ordinal = 0, target =
61+
"net/minecraft/world/chunk/Chunk.setLastSaveTime(J)V"))
62+
public void onServerChunkLoad(ChunkPos chunkPos, CallbackInfoReturnable cir, CompoundTag compoundTag, boolean bl, Chunk chunk) {
63+
// Fire ChunkEvent.Load on server side
64+
MinecraftForge.EVENT_BUS.post(new ChunkEvent.Load(chunk));
65+
}
4766
}

patchwork-events-world/src/main/resources/fabric.mod.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515
"depends": {
1616
"patchwork-api-base": "*"
1717
},
18+
"entrypoints": {
19+
"main": [
20+
"net.patchworkmc.impl.event.world.WorldEvents"
21+
],
22+
"client": [
23+
"net.patchworkmc.impl.event.world.WorldEventsClient"
24+
]
25+
},
1826
"mixins": [
1927
"patchwork-events-world.mixins.json"
2028
],

0 commit comments

Comments
 (0)