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

Commit 657afda

Browse files
authored
Add LootTableLoadEvent, methods on LootPool/LootTable (#113)
* Add LootTableLoadEvent, methods on LootPool/LootTable The mixin to LootManager is messy, and I'm open to alternative ways to doing it, even if it involves scrapping the 'forge way' of doing it and doing it a different way. For now, I'm going to get this up so that bikeshedding can happen. I'm using the "net.patchworkmc.api.*.Forge<classname>" pattern for interfaces that describe public methods added by Forge for use by mods in this commit. This is up for bikeshedding. This is untested other than starting up Minecraft and opening a world. * Fix LocalCapture.PRINT that I forgot about * Implement FMLServerStoppedEvent * Dispatch from the god classes for the events added in this PR * prevent memory leak * fixup! checkstype * Make slightly less hacky! The idea is to add an inject in the same place as the method we would be redirecting (but are instead cancelling) so that we can capture locals, and grab the builder that we would otherwise be missing. The downside is that we are effectively overwriting the lambda that vanilla uses in favor of our own. * Make the implicit "overwrite" explicit. * Make requested changes. Mainly adding Override annotations to methods for interfaces, but a few other things as well.
1 parent 0316d31 commit 657afda

File tree

22 files changed

+895
-2
lines changed

22 files changed

+895
-2
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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.fml.event.server;
21+
22+
import net.minecraft.server.MinecraftServer;
23+
24+
/**
25+
* Called after {@link FMLServerStoppingEvent} when the server has completely shut down.
26+
* Called immediately before shutting down, on the dedicated server, and before returning
27+
* to the main menu on the client.
28+
*
29+
* @author cpw
30+
*/
31+
public class FMLServerStoppedEvent extends ServerLifecycleEvent {
32+
public FMLServerStoppedEvent(MinecraftServer server) {
33+
super(server);
34+
}
35+
}

patchwork-events-lifecycle/src/main/java/net/minecraftforge/fml/server/ServerLifecycleHooks.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
package net.minecraftforge.fml.server;
2121

22+
import java.util.concurrent.CountDownLatch;
23+
2224
import net.minecraft.server.MinecraftServer;
2325

2426
import net.patchworkmc.impl.event.lifecycle.LifecycleEvents;
@@ -28,6 +30,7 @@
2830
*/
2931
public class ServerLifecycleHooks {
3032
public static MinecraftServer currentServer;
33+
public static volatile CountDownLatch exitLatch = null;
3134

3235
public static MinecraftServer getCurrentServer() {
3336
return currentServer;
@@ -48,4 +51,8 @@ public static boolean handleServerStarting(final MinecraftServer server) {
4851
public static void handleServerStarted(final MinecraftServer server) {
4952
LifecycleEvents.handleServerStarted(server);
5053
}
54+
55+
public static void handleServerStopped(final MinecraftServer server) {
56+
LifecycleEvents.handleServerStopped(server);
57+
}
5158
}

patchwork-events-lifecycle/src/main/java/net/patchworkmc/impl/event/lifecycle/LifecycleEvents.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package net.patchworkmc.impl.event.lifecycle;
2121

2222
import java.nio.file.Path;
23+
import java.util.concurrent.CountDownLatch;
2324

2425
import net.minecraftforge.common.MinecraftForge;
2526
import net.minecraftforge.event.TickEvent;
@@ -30,6 +31,7 @@
3031
import net.minecraftforge.fml.event.server.FMLServerAboutToStartEvent;
3132
import net.minecraftforge.fml.event.server.FMLServerStartedEvent;
3233
import net.minecraftforge.fml.event.server.FMLServerStartingEvent;
34+
import net.minecraftforge.fml.event.server.FMLServerStoppedEvent;
3335
import net.minecraftforge.fml.loading.FileUtils;
3436
import net.minecraftforge.fml.server.ServerLifecycleHooks;
3537

@@ -93,6 +95,18 @@ public static void handleLoadComplete() {
9395
loadCompleteCallback.run();
9496
}
9597

98+
public static void handleServerStopped(final MinecraftServer server) {
99+
MinecraftForge.EVENT_BUS.post(new FMLServerStoppedEvent(server));
100+
ServerLifecycleHooks.currentServer = null;
101+
LogicalSidedProvider.setServer(null);
102+
CountDownLatch latch = ServerLifecycleHooks.exitLatch;
103+
104+
if (latch != null) {
105+
latch.countDown();
106+
ServerLifecycleHooks.exitLatch = null;
107+
}
108+
}
109+
96110
@Override
97111
public void onInitialize() {
98112
WorldTickCallback.EVENT.register(world -> fireWorldTickEvent(TickEvent.Phase.END, world));
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.mixin.event.lifecycle;
21+
22+
import org.spongepowered.asm.mixin.Mixin;
23+
import org.spongepowered.asm.mixin.injection.At;
24+
import org.spongepowered.asm.mixin.injection.Inject;
25+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
26+
27+
import net.minecraft.server.MinecraftServer;
28+
29+
import net.patchworkmc.impl.event.lifecycle.LifecycleEvents;
30+
31+
@Mixin(MinecraftServer.class)
32+
public class MixinMinecraftServer {
33+
@Inject(method = "run", at = @At(value = "INVOKE", target = "net/minecraft/server/MinecraftServer.exit ()V"))
34+
private void serverStoppedHook(CallbackInfo ci) {
35+
LifecycleEvents.handleServerStopped((MinecraftServer) (Object) this);
36+
}
37+
}

patchwork-god-classes/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ dependencies {
77
compile project(path: ':patchwork-events-entity', configuration: 'dev')
88
compile project(path: ':patchwork-events-lifecycle', configuration: 'dev')
99
compile project(path: ':patchwork-events-rendering', configuration: 'dev')
10+
compile project(path: ':patchwork-loot', configuration: 'dev')
1011
}

patchwork-god-classes/src/main/java/net/minecraftforge/common/ForgeHooks.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
import javax.annotation.Nullable;
2525

26+
import com.google.gson.Gson;
27+
import com.google.gson.JsonObject;
2628
import net.minecraftforge.event.ForgeEventFactory;
2729
import net.minecraftforge.eventbus.api.Event;
2830

@@ -33,12 +35,16 @@
3335
import net.minecraft.entity.damage.DamageSource;
3436
import net.minecraft.entity.mob.MobEntity;
3537
import net.minecraft.entity.player.PlayerEntity;
38+
import net.minecraft.loot.LootManager;
39+
import net.minecraft.loot.LootTable;
3640
import net.minecraft.util.ActionResult;
3741
import net.minecraft.util.Hand;
42+
import net.minecraft.util.Identifier;
3843
import net.minecraft.world.IWorld;
3944
import net.minecraft.world.MobSpawnerLogic;
4045

4146
import net.patchworkmc.impl.event.entity.EntityEvents;
47+
import net.patchworkmc.impl.loot.LootHooks;
4248

4349
/*
4450
* Note: this class is intended for mod use only, to dispatch to the implementations kept in their own modules.
@@ -98,4 +104,13 @@ public static boolean onLivingDrops(LivingEntity entity, DamageSource source, Co
98104
public static boolean onPlayerAttackTarget(PlayerEntity player, Entity target) {
99105
return EntityEvents.attackEntity(player, target);
100106
}
107+
108+
@Nullable
109+
public static LootTable loadLootTable(Gson gson, Identifier name, JsonObject data, boolean custom, LootManager lootTableManager) {
110+
return LootHooks.loadLootTable(gson, name, data, custom, lootTableManager);
111+
}
112+
113+
public static String readPoolName(JsonObject json) {
114+
return LootHooks.readPoolName(json);
115+
}
101116
}

patchwork-god-classes/src/main/java/net/minecraftforge/event/ForgeEventFactory.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,16 @@
2828
import net.minecraft.entity.SpawnType;
2929
import net.minecraft.entity.mob.MobEntity;
3030
import net.minecraft.entity.player.PlayerEntity;
31+
import net.minecraft.loot.LootManager;
32+
import net.minecraft.loot.LootTable;
33+
import net.minecraft.util.Identifier;
3134
import net.minecraft.world.IWorld;
3235
import net.minecraft.world.MobSpawnerLogic;
3336
import net.minecraft.world.World;
3437

3538
import net.patchworkmc.impl.capability.CapabilityEvents;
3639
import net.patchworkmc.impl.event.entity.EntityEvents;
40+
import net.patchworkmc.impl.event.loot.LootEvents;
3741

3842
/*
3943
* Note: this class is intended for mod use only, to dispatch to the implementations kept in their own modules.
@@ -65,4 +69,8 @@ public static void onPlayerFall(PlayerEntity player, float distance, float multi
6569
public static boolean doSpecialSpawn(MobEntity entity, World world, float x, float y, float z, MobSpawnerLogic spawner, SpawnType spawnReason) {
6670
return EntityEvents.doSpecialSpawn(entity, world, x, y, z, spawner, spawnReason);
6771
}
72+
73+
public static LootTable loadLootTable(Identifier name, LootTable table, LootManager lootTableManager) {
74+
return LootEvents.loadLootTable(name, table, lootTableManager);
75+
}
6876
}

patchwork-god-classes/src/main/resources/fabric.mod.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"patchwork-fml": "*",
2020
"patchwork-capabilities": "*",
2121
"patchwork-events-lifecycle": "*",
22-
"patchwork-events-rendering": "*"
22+
"patchwork-events-rendering": "*",
23+
"patchwork-loot": "*"
2324
},
2425
"custom": {
2526
"modmenu:api": true,

patchwork-loot/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
archivesBaseName = "patchwork-loot"
22
version = getSubprojectVersion(project, "0.2.0")
3+
4+
dependencies {
5+
compile project(path: ':patchwork-fml', configuration: 'dev')
6+
}
7+
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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;
21+
22+
import net.minecraftforge.eventbus.api.Event;
23+
24+
import net.minecraft.loot.LootManager;
25+
import net.minecraft.loot.LootTable;
26+
import net.minecraft.util.Identifier;
27+
28+
/**
29+
* Event fired when a LootTable json is loaded from json.
30+
* This event is fired whenever resources are loaded, or when the server starts.
31+
* This event will NOT be fired for LootTables loaded from the world folder, these are
32+
* considered configurations files and should not be modified by mods.
33+
*
34+
* <p>Canceling the event will make it load a empty loot table.</p>
35+
*/
36+
public class LootTableLoadEvent extends Event {
37+
private final Identifier name;
38+
private LootTable table;
39+
private LootManager lootTableManager;
40+
41+
public LootTableLoadEvent(Identifier name, LootTable table, LootManager lootTableManager) {
42+
this.name = name;
43+
this.table = table;
44+
this.lootTableManager = lootTableManager;
45+
}
46+
47+
public Identifier getName() {
48+
return this.name;
49+
}
50+
51+
public LootTable getTable() {
52+
return this.table;
53+
}
54+
55+
public void setTable(LootTable table) {
56+
this.table = table;
57+
}
58+
59+
public LootManager getLootTableManager() {
60+
return this.lootTableManager;
61+
}
62+
63+
@Override
64+
public boolean isCancelable() {
65+
return true;
66+
}
67+
}

0 commit comments

Comments
 (0)