forked from neoforged/NeoForge
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[1.21.1] Add event for registering atlases for use with Material (neo…
- Loading branch information
Showing
5 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/main/java/net/neoforged/neoforge/client/event/RegisterMaterialAtlasesEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright (c) NeoForged and contributors | ||
* SPDX-License-Identifier: LGPL-2.1-only | ||
*/ | ||
|
||
package net.neoforged.neoforge.client.event; | ||
|
||
import java.util.Map; | ||
import net.minecraft.client.Minecraft; | ||
import net.minecraft.client.renderer.texture.TextureAtlas; | ||
import net.minecraft.client.resources.TextureAtlasHolder; | ||
import net.minecraft.client.resources.model.Material; | ||
import net.minecraft.client.resources.model.ModelManager; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.neoforged.bus.api.Event; | ||
import net.neoforged.bus.api.ICancellableEvent; | ||
import net.neoforged.fml.LogicalSide; | ||
import net.neoforged.fml.event.IModBusEvent; | ||
import org.jetbrains.annotations.ApiStatus; | ||
|
||
/** | ||
* Fired for registering {@linkplain TextureAtlas texture atlases} that will be used with {@link Material} or | ||
* other systems which retrieve the atlas via {@link Minecraft#getTextureAtlas(ResourceLocation)} or | ||
* {@link ModelManager#getAtlas(ResourceLocation)}. | ||
* <p> | ||
* If an atlas is registered via this event, then it must <b>NOT</b> be used through a {@link TextureAtlasHolder}. | ||
* <p> | ||
* This event fires during startup when the {@link ModelManager} is constructed. | ||
* <p> | ||
* This event is not {@linkplain ICancellableEvent cancellable}. | ||
* <p> | ||
* This event is fired on the mod-specific event bus, only on the {@linkplain LogicalSide#CLIENT logical client}. | ||
*/ | ||
public class RegisterMaterialAtlasesEvent extends Event implements IModBusEvent { | ||
private final Map<ResourceLocation, ResourceLocation> atlases; | ||
|
||
@ApiStatus.Internal | ||
public RegisterMaterialAtlasesEvent(Map<ResourceLocation, ResourceLocation> atlases) { | ||
this.atlases = atlases; | ||
} | ||
|
||
/** | ||
* Register a texture atlas with the given name and info location | ||
* | ||
* @param atlasLocation The name of the texture atlas | ||
* @param atlasInfoLocation The location of the atlas info JSON relative to the {@code atlases} directory | ||
*/ | ||
public void register(ResourceLocation atlasLocation, ResourceLocation atlasInfoLocation) { | ||
ResourceLocation oldAtlasInfoLoc = this.atlases.putIfAbsent(atlasLocation, atlasInfoLocation); | ||
if (oldAtlasInfoLoc != null) { | ||
throw new IllegalStateException(String.format( | ||
"Duplicate registration of atlas: %s (old info: %s, new info: %s)", | ||
atlasLocation, | ||
oldAtlasInfoLoc, | ||
atlasInfoLocation)); | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
tests/src/main/java/net/neoforged/neoforge/debug/client/TextureAtlasTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright (c) NeoForged and contributors | ||
* SPDX-License-Identifier: LGPL-2.1-only | ||
*/ | ||
|
||
package net.neoforged.neoforge.debug.client; | ||
|
||
import net.minecraft.client.Minecraft; | ||
import net.minecraft.client.renderer.texture.MissingTextureAtlasSprite; | ||
import net.minecraft.client.renderer.texture.TextureAtlasSprite; | ||
import net.minecraft.client.resources.model.Material; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.server.packs.resources.ResourceManagerReloadListener; | ||
import net.neoforged.api.distmarker.Dist; | ||
import net.neoforged.neoforge.client.event.RegisterClientReloadListenersEvent; | ||
import net.neoforged.neoforge.client.event.RegisterMaterialAtlasesEvent; | ||
import net.neoforged.testframework.DynamicTest; | ||
import net.neoforged.testframework.annotation.ForEachTest; | ||
import net.neoforged.testframework.annotation.TestHolder; | ||
|
||
@ForEachTest(side = Dist.CLIENT, groups = { "client.texture_atlas", "texture_atlas" }) | ||
public class TextureAtlasTests { | ||
@TestHolder(description = { "Tests that texture atlases intended for use with Material are correctly registered and loaded" }, enabledByDefault = true) | ||
static void testMaterialAtlas(final DynamicTest test) { | ||
String modId = test.createModId(); | ||
ResourceLocation atlasLoc = ResourceLocation.fromNamespaceAndPath(modId, "textures/atlas/material_test.png"); | ||
|
||
test.framework().modEventBus().addListener(RegisterMaterialAtlasesEvent.class, event -> { | ||
ResourceLocation infoLoc = ResourceLocation.fromNamespaceAndPath(modId, "material_test"); | ||
event.register(atlasLoc, infoLoc); | ||
}); | ||
|
||
test.framework().modEventBus().addListener(RegisterClientReloadListenersEvent.class, event -> { | ||
event.registerReloadListener((ResourceManagerReloadListener) manager -> { | ||
try { | ||
Minecraft.getInstance().getModelManager().getAtlas(atlasLoc); | ||
} catch (NullPointerException npe) { | ||
test.fail("Atlas was not registered"); | ||
return; | ||
} catch (Throwable t) { | ||
test.fail("Atlas lookup failed: " + t.getMessage()); | ||
return; | ||
} | ||
|
||
try { | ||
Material material = new Material(atlasLoc, ResourceLocation.withDefaultNamespace("block/stone")); | ||
TextureAtlasSprite sprite = material.sprite(); | ||
if (sprite.contents().name().equals(MissingTextureAtlasSprite.getLocation())) { | ||
test.fail("Expected sprite was not stitched"); | ||
return; | ||
} | ||
} catch (Throwable t) { | ||
test.fail("Sprite lookup via material failed: " + t.getMessage()); | ||
} | ||
|
||
test.pass(); | ||
}); | ||
}); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
tests/src/main/resources/assets/neotests_test_material_atlas/atlases/material_test.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"sources": [ | ||
{ | ||
"type": "minecraft:single", | ||
"resource": "minecraft:block/stone" | ||
} | ||
] | ||
} |