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

Commit 90ba4b0

Browse files
authored
Implement ForgeEventFactory.gatherCapabilities (#129)
* Move implementation of gatherCapabilities to CapabilityEvents * Dispatch ForgeEventFactory.gatherCapabilities to CapabilityEvents * Remove <T extends ICapabilityProvider> restriction on ForgeEventFactory
1 parent 3f504ca commit 90ba4b0

File tree

6 files changed

+65
-21
lines changed

6 files changed

+65
-21
lines changed

patchwork-capabilities/src/main/java/net/minecraftforge/common/capabilities/CapabilityProvider.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@
2323
import javax.annotation.Nullable;
2424
import javax.annotation.ParametersAreNonnullByDefault;
2525

26-
import net.minecraftforge.common.MinecraftForge;
2726
import net.minecraftforge.common.util.LazyOptional;
28-
import net.minecraftforge.event.AttachCapabilitiesEvent;
2927

3028
import net.minecraft.nbt.CompoundTag;
3129
import net.minecraft.util.math.Direction;
3230

31+
import net.patchworkmc.impl.capability.CapabilityEvents;
32+
3333
@ParametersAreNonnullByDefault
3434
public abstract class CapabilityProvider<B> implements ICapabilityProvider {
3535
protected final Class<B> baseClass;
@@ -45,14 +45,7 @@ public final void gatherCapabilities() {
4545
}
4646

4747
public void gatherCapabilities(@Nullable ICapabilityProvider parent) {
48-
AttachCapabilitiesEvent<B> event = new AttachCapabilitiesEvent<>(baseClass, (B) this);
49-
MinecraftForge.EVENT_BUS.post(event);
50-
51-
if (!event.getCapabilities().isEmpty() || parent != null) {
52-
capabilities = new CapabilityDispatcher(event.getCapabilities(), event.getListeners(), parent);
53-
} else {
54-
capabilities = null;
55-
}
48+
capabilities = CapabilityEvents.gatherCapabilities(baseClass, this, parent);
5649
}
5750

5851
public final @Nullable CapabilityDispatcher getCapabilities() {

patchwork-capabilities/src/main/java/net/patchworkmc/impl/capability/BaseCapabilityProvider.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@
2121

2222
import javax.annotation.Nullable;
2323

24-
import net.minecraftforge.common.MinecraftForge;
25-
import net.minecraftforge.common.capabilities.CapabilityDispatcher;
2624
import net.minecraftforge.common.capabilities.CapabilityProvider;
2725
import net.minecraftforge.common.capabilities.ICapabilityProvider;
28-
import net.minecraftforge.event.AttachCapabilitiesEvent;
2926

3027
public class BaseCapabilityProvider<T> extends CapabilityProvider<T> {
3128
private final T provider;
@@ -37,13 +34,6 @@ public BaseCapabilityProvider(Class<T> baseClass, T provider) {
3734

3835
@Override
3936
public void gatherCapabilities(@Nullable ICapabilityProvider parent) {
40-
AttachCapabilitiesEvent<T> event = new AttachCapabilitiesEvent<>(baseClass, provider);
41-
MinecraftForge.EVENT_BUS.post(event);
42-
43-
if (!event.getCapabilities().isEmpty() || parent != null) {
44-
capabilities = new CapabilityDispatcher(event.getCapabilities(), event.getListeners(), parent);
45-
} else {
46-
capabilities = null;
47-
}
37+
capabilities = CapabilityEvents.gatherCapabilities(baseClass, provider, parent);
4838
}
4939
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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.capability;
21+
22+
import javax.annotation.Nullable;
23+
24+
import net.minecraftforge.common.MinecraftForge;
25+
import net.minecraftforge.common.capabilities.CapabilityDispatcher;
26+
import net.minecraftforge.common.capabilities.ICapabilityProvider;
27+
import net.minecraftforge.event.AttachCapabilitiesEvent;
28+
29+
public class CapabilityEvents {
30+
// This is less restrictive than Forge's implementation, since patchwork can't make vanilla extend stuff at random.
31+
@SuppressWarnings("unchecked")
32+
@Nullable
33+
public static <T> CapabilityDispatcher gatherCapabilities(Class<? extends T> type, T provider, @Nullable ICapabilityProvider parent) {
34+
AttachCapabilitiesEvent<T> event = new AttachCapabilitiesEvent<T>((Class<T>) type, provider);
35+
MinecraftForge.EVENT_BUS.post(event);
36+
37+
if (!event.getCapabilities().isEmpty() || parent != null) {
38+
return new CapabilityDispatcher(event.getCapabilities(), event.getListeners(), parent);
39+
} else {
40+
return null;
41+
}
42+
}
43+
}

patchwork-god-classes/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ version = getSubprojectVersion(project, "0.1.0")
33

44
dependencies {
55
compile project(path: ':patchwork-fml', configuration: 'dev')
6+
compile project(path: ':patchwork-capabilities', configuration: 'dev')
67
compile project(path: ':patchwork-events-lifecycle', configuration: 'dev')
78
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,25 @@
1919

2020
package net.minecraftforge.event;
2121

22+
import javax.annotation.Nullable;
23+
24+
import net.minecraftforge.common.capabilities.CapabilityDispatcher;
25+
import net.minecraftforge.common.capabilities.ICapabilityProvider;
26+
27+
import net.patchworkmc.impl.capability.CapabilityEvents;
28+
2229
/*
2330
* Note: this class is intended for mod use only, to dispatch to the implementations kept in their own modules.
2431
* Do not keep implementation details here, methods should be thin wrappers around methods in other modules.
2532
*/
2633
public class ForgeEventFactory {
34+
@Nullable
35+
public static <T> CapabilityDispatcher gatherCapabilities(Class<? extends T> type, T provider) {
36+
return gatherCapabilities(type, provider, null);
37+
}
38+
39+
@Nullable
40+
public static <T> CapabilityDispatcher gatherCapabilities(Class<? extends T> type, T provider, @Nullable ICapabilityProvider parent) {
41+
return CapabilityEvents.gatherCapabilities(type, provider, parent);
42+
}
2743
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"depends": {
1818
"fabricloader": ">=0.8.4",
1919
"patchwork-fml": "*",
20+
"patchwork-capabilities": "*",
2021
"patchwork-events-lifecycle": "*"
2122
},
2223
"custom": {

0 commit comments

Comments
 (0)