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

Commit a2c372e

Browse files
rikka0w0TheGlitch76cittyinthecloud
authored
Impl ModelBakeEvent, ModelRegistryEvent and some ModelLoader methods (#106)
* Impl ModelBakeEvent, ModelRegistryEvent and some ModelLoader methods * Apply suggestions from code review Co-authored-by: Glitch <glitchieproductionsofficial@gmail.com> * Rename AbstractModelLoader to SpecialModelProvider * Simplify MixinModelLoader * Add comments * Apply suggestions from code review Co-authored-by: famous1622 <8428080+famous1622@users.noreply.github.com> * Update ModelEventDispatcher.java * Better mixin target Co-authored-by: Glitch <glitchieproductionsofficial@gmail.com> Co-authored-by: famous1622 <8428080+famous1622@users.noreply.github.com>
1 parent 90ba4b0 commit a2c372e

File tree

15 files changed

+505
-0
lines changed

15 files changed

+505
-0
lines changed

patchwork-dispatcher/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ dependencies {
66
compile project(path: ':patchwork-registries', configuration: 'dev')
77
compile project(path: ':patchwork-events-lifecycle', configuration: 'dev')
88
compile project(path: ':patchwork-events-rendering', configuration: 'dev')
9+
compile project(path: ':patchwork-model-loader', configuration: 'dev')
910
}

patchwork-dispatcher/src/main/java/net/patchworkmc/impl/Patchwork.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import net.patchworkmc.api.ForgeInitializer;
5555
import net.patchworkmc.impl.event.lifecycle.LifecycleEvents;
5656
import net.patchworkmc.impl.event.render.RenderEvents;
57+
import net.patchworkmc.impl.modelloader.ModelEventDispatcher;
5758
import net.patchworkmc.impl.registries.RegistryEventDispatcher;
5859

5960
public class Patchwork implements ModInitializer {
@@ -127,6 +128,7 @@ public void onInitialize() {
127128
dispatch(mods, FMLCommonSetupEvent::new);
128129

129130
DistExecutor.runWhenOn(Dist.CLIENT, () -> () -> {
131+
ModelEventDispatcher.fireModelRegistryEvent();
130132
dispatch(mods, container -> new FMLClientSetupEvent(MinecraftClient::getInstance, container));
131133
RenderEvents.registerEventDispatcher(event -> dispatch(mods, event));
132134
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
archivesBaseName = "patchwork-model-loader"
2+
version = getSubprojectVersion(project, "0.1.0")
3+
4+
dependencies {
5+
compile project(path: ':patchwork-fml', configuration: 'dev')
6+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.client.event;
21+
22+
import java.util.Map;
23+
24+
import net.minecraftforge.client.model.ModelLoader;
25+
import net.minecraftforge.eventbus.api.Event;
26+
27+
import net.minecraft.client.render.model.BakedModel;
28+
import net.minecraft.client.render.model.BakedModelManager;
29+
import net.minecraft.util.Identifier;
30+
31+
/**
32+
* Fired when the BakedModelManager is notified of the resource manager reloading.
33+
* Called after model registry is setup, but before it's passed to
34+
* BlockModelShapes.
35+
*/
36+
public class ModelBakeEvent extends Event {
37+
private final BakedModelManager modelManager;
38+
private final Map<Identifier, BakedModel> modelRegistry;
39+
private final ModelLoader modelLoader;
40+
41+
public ModelBakeEvent(BakedModelManager modelManager, Map<Identifier, BakedModel> modelRegistry,
42+
ModelLoader modelLoader) {
43+
this.modelManager = modelManager;
44+
this.modelRegistry = modelRegistry;
45+
this.modelLoader = modelLoader;
46+
}
47+
48+
public BakedModelManager getModelManager() {
49+
return modelManager;
50+
}
51+
52+
public Map<Identifier, BakedModel> getModelRegistry() {
53+
return modelRegistry;
54+
}
55+
56+
public ModelLoader getModelLoader() {
57+
return modelLoader;
58+
}
59+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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.client.event;
21+
22+
import net.minecraftforge.eventbus.api.Event;
23+
24+
/**
25+
* Fired when the {@link net.minecraftforge.client.model.ModelLoader} is ready
26+
* to receive registrations.
27+
*/
28+
public class ModelRegistryEvent extends Event {
29+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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.client.model;
21+
22+
import java.util.HashMap;
23+
import java.util.HashSet;
24+
import java.util.Map;
25+
import java.util.Set;
26+
27+
import javax.annotation.Nullable;
28+
29+
import org.apache.logging.log4j.LogManager;
30+
import org.apache.logging.log4j.Logger;
31+
import org.apache.logging.log4j.Marker;
32+
import org.apache.logging.log4j.MarkerManager;
33+
34+
import net.minecraft.client.color.block.BlockColors;
35+
import net.minecraft.client.render.model.BakedModel;
36+
import net.minecraft.client.texture.SpriteAtlasTexture;
37+
import net.minecraft.client.util.ModelIdentifier;
38+
import net.minecraft.resource.ResourceManager;
39+
import net.minecraft.util.Identifier;
40+
import net.minecraft.util.profiler.Profiler;
41+
42+
import net.patchworkmc.impl.modelloader.SpecialModelProvider;
43+
44+
public class ModelLoader extends net.minecraft.client.render.model.ModelLoader implements SpecialModelProvider {
45+
private static final Marker MODELLOADING = MarkerManager.getMarker("MODELLOADING");
46+
private static Set<Identifier> specialModels = new HashSet<>();
47+
private static final Logger LOGGER = LogManager.getLogger();
48+
private final Map<Identifier, Exception> loadingExceptions = new HashMap<>();
49+
private boolean isLoading = false;
50+
private static ModelLoader instance;
51+
52+
@Nullable
53+
public static ModelLoader instance() {
54+
return instance;
55+
}
56+
57+
public boolean isLoading() {
58+
return isLoading;
59+
}
60+
61+
public ModelLoader(ResourceManager resourceManager, SpriteAtlasTexture spriteAtlas, BlockColors blockColors,
62+
Profiler profiler) {
63+
super(resourceManager, spriteAtlas, blockColors, profiler);
64+
}
65+
66+
/**
67+
* Indicate to vanilla that it should load and bake the given model, even if no
68+
* blocks or items use it. This is useful if e.g. you have baked models only for
69+
* entity renderers. Call during
70+
* {@link net.minecraftforge.client.event.ModelRegistryEvent}
71+
*
72+
* @param rl The model, either {@link ModelResourceLocation} to point to a
73+
* blockstate variant, or plain {@link ResourceLocation} to point
74+
* directly to a json in the models folder.
75+
*/
76+
public static void addSpecialModel(Identifier rl) {
77+
specialModels.add(rl);
78+
}
79+
80+
@Override
81+
public Set<Identifier> getSpecialModels() {
82+
return specialModels;
83+
}
84+
85+
/**
86+
* Internal, do not use.
87+
*/
88+
public void onPostBakeEvent(Map<Identifier, BakedModel> modelRegistry) {
89+
BakedModel missingModel = modelRegistry.get(MISSING);
90+
91+
for (Map.Entry<Identifier, Exception> entry : loadingExceptions.entrySet()) {
92+
// ignoring pure Identifier arguments, all things we care about pass
93+
// ModelIdentifier
94+
if (entry.getKey() instanceof ModelIdentifier) {
95+
LOGGER.debug(MODELLOADING, "Model {} failed to load: {}", entry.getKey().toString(),
96+
entry.getValue().getLocalizedMessage());
97+
final ModelIdentifier location = (ModelIdentifier) entry.getKey();
98+
final BakedModel model = modelRegistry.get(location);
99+
100+
if (model == null) {
101+
modelRegistry.put(location, missingModel);
102+
}
103+
}
104+
}
105+
106+
loadingExceptions.clear();
107+
isLoading = false;
108+
}
109+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.modelloader;
21+
22+
import java.util.Map;
23+
24+
import net.minecraftforge.client.event.ModelBakeEvent;
25+
import net.minecraftforge.client.event.ModelRegistryEvent;
26+
import net.minecraftforge.client.model.ModelLoader;
27+
import net.minecraftforge.fml.ModLoader;
28+
29+
import net.minecraft.client.render.model.BakedModel;
30+
import net.minecraft.client.render.model.BakedModelManager;
31+
import net.minecraft.util.Identifier;
32+
33+
public class ModelEventDispatcher {
34+
/**
35+
* In Forge, ModelRegistryEvent is fired in parallel with FMLClientSetupEvent.
36+
* Here we fire ModelRegistryEvent before FMLClientSetupEvent.
37+
* The official forge does not set the ModLoadingContext here, so this should be fine.
38+
*/
39+
public static void fireModelRegistryEvent() {
40+
ModLoader.get().postEvent(new ModelRegistryEvent());
41+
}
42+
43+
public static void onModelBake(BakedModelManager modelManager, Map<Identifier, BakedModel> modelRegistry, ModelLoader modelLoader) {
44+
ModLoader.get().postEvent(new ModelBakeEvent(modelManager, modelRegistry, modelLoader));
45+
modelLoader.onPostBakeEvent(modelRegistry);
46+
}
47+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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.modelloader;
21+
22+
public class Signatures {
23+
public static final String Profiler_swap = "net/minecraft/util/profiler/Profiler.swap(Ljava/lang/String;)V";
24+
25+
public static final String ModelLoader_new = "("
26+
+ "Lnet/minecraft/resource/ResourceManager;"
27+
+ "Lnet/minecraft/client/texture/SpriteAtlasTexture;"
28+
+ "Lnet/minecraft/client/color/block/BlockColors;"
29+
+ "Lnet/minecraft/util/profiler/Profiler;"
30+
+ ")"
31+
+ "Lnet/minecraft/client/render/model/ModelLoader;";
32+
33+
public static final String ModelLoader_addModel = "net/minecraft/client/render/model/ModelLoader.addModel(Lnet/minecraft/client/util/ModelIdentifier;)V";
34+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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.modelloader;
21+
22+
import java.util.Set;
23+
24+
import net.minecraft.util.Identifier;
25+
26+
public interface SpecialModelProvider {
27+
default Set<Identifier> getSpecialModels() {
28+
return java.util.Collections.emptySet();
29+
}
30+
}

0 commit comments

Comments
 (0)