Skip to content

Commit

Permalink
Rename and fix custom sprite source test
Browse files Browse the repository at this point in the history
  • Loading branch information
Minecraftschurli committed Nov 28, 2023
1 parent 9eb5980 commit a35d9d2
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import java.util.Optional;
import net.minecraft.client.renderer.texture.*;
import net.minecraft.client.renderer.texture.SpriteContents;
import net.minecraft.client.renderer.texture.SpriteTicker;
import net.minecraft.client.renderer.texture.atlas.SpriteSource;
import net.minecraft.client.renderer.texture.atlas.SpriteSourceType;
import net.minecraft.client.resources.metadata.animation.FrameSize;
Expand All @@ -21,6 +22,7 @@
import net.minecraft.server.packs.resources.ResourceMetadata;
import net.minecraft.util.RandomSource;
import net.minecraft.world.item.Item;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.common.Mod;
import net.neoforged.fml.javafmlmod.FMLJavaModLoadingContext;
import net.neoforged.fml.loading.FMLLoader;
Expand All @@ -29,39 +31,41 @@
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;

@Mod(CustomTASTest.MOD_ID)
public class CustomTASTest {
@Mod(CustomSpriteSourceTest.MOD_ID)
public class CustomSpriteSourceTest {
private static final boolean ENABLED = true;
static final String MOD_ID = "custom_tas_test";
static final String MOD_ID = "custom_sprite_source_test";
private static final DeferredRegister.Items ITEMS = DeferredRegister.createItems(MOD_ID);
private static final Holder<Item> TEST_ITEM = ITEMS.registerSimpleItem("test_item");

public CustomTASTest() {
if (ENABLED) {
if (FMLLoader.getDist().isClient()) {
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::registerTextureAtlasSpriteLoaders);
}
ITEMS.register(FMLJavaModLoadingContext.get().getModEventBus());
public CustomSpriteSourceTest() {
if (!ENABLED) return;
IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
if (FMLLoader.getDist().isClient()) {
modEventBus.addListener(this::registerTextureAtlasSpriteLoaders);
}
ITEMS.register(modEventBus);
}

private static SpriteSourceType tasLoader;

private void registerTextureAtlasSpriteLoaders(RegisterSpriteSourceTypesEvent event) {
tasLoader = event.register(new ResourceLocation(MOD_ID, "tas_loader"), TasLoader.CODEC);
tasLoader = event.register(new ResourceLocation(MOD_ID, "custom_sprite_source"), CustomSpriteSource.CODEC);
}

private record TasLoader(ResourceLocation id) implements SpriteSource {
private record CustomSpriteSource(ResourceLocation id) implements SpriteSource {
private static final Logger LOGGER = LogUtils.getLogger();
private static final Codec<TasLoader> CODEC = RecordCodecBuilder.create(inst -> inst.group(
ResourceLocation.CODEC.fieldOf("id").forGetter(TasLoader::id)).apply(inst, TasLoader::new));
private static final Codec<CustomSpriteSource> CODEC = RecordCodecBuilder.create(inst -> inst.group(
ResourceLocation.CODEC.fieldOf("id").forGetter(CustomSpriteSource::id)
).apply(inst, CustomSpriteSource::new));

@Override
public void run(ResourceManager manager, Output output) {
ResourceLocation resourcelocation = TEXTURE_ID_CONVERTER.idToFile(this.id());
ResourceLocation id = this.id();
ResourceLocation resourcelocation = TEXTURE_ID_CONVERTER.idToFile(id);
Optional<Resource> optional = manager.getResource(resourcelocation);
if (optional.isPresent()) {
output.add(this.id(), spriteResourceLoader -> spriteResourceLoader.loadSprite(resourcelocation, optional.get(), this::loadContents));
output.add(id, spriteResourceLoader -> spriteResourceLoader.loadSprite(id, optional.get(), CustomSpriteContents::new));
} else {
LOGGER.warn("Missing sprite: {}", resourcelocation);
}
Expand All @@ -72,35 +76,30 @@ public SpriteSourceType type() {
return tasLoader;
}

public SpriteContents loadContents(ResourceLocation name, FrameSize frameSize, NativeImage image, ResourceMetadata metadata) {
final class TASSpriteContents extends SpriteContents {

public TASSpriteContents(ResourceLocation p_249787_, FrameSize p_251031_, NativeImage p_252131_, ResourceMetadata p_294742_) {
super(p_249787_, p_251031_, p_252131_, p_294742_);
}
static final class CustomSpriteContents extends SpriteContents {

@Override
public @NotNull SpriteTicker createTicker() {
return new Ticker();
}
public CustomSpriteContents(ResourceLocation p_249787_, FrameSize p_251031_, NativeImage p_252131_, ResourceMetadata p_294742_) {
super(p_249787_, p_251031_, p_252131_, p_294742_);
}

class Ticker implements SpriteTicker {
final RandomSource random = RandomSource.create();
@Override
public @NotNull SpriteTicker createTicker() {
return new Ticker();
}

@Override
public void tickAndUpload(int p_248847_, int p_250486_) {
TASSpriteContents.this.byMipLevel[0].fillRect(0, 0, 16, 16, 0xFF000000 | random.nextInt(0xFFFFFF));
TASSpriteContents.this.uploadFirstFrame(p_248847_, p_250486_);
}
class Ticker implements SpriteTicker {
final RandomSource random = RandomSource.create();

@Override
public void close() {
@Override
public void tickAndUpload(int p_248847_, int p_250486_) {
CustomSpriteContents.this.byMipLevel[0].fillRect(0, 0, 16, 16, 0xFF000000 | random.nextInt(0xFFFFFF));
CustomSpriteContents.this.uploadFirstFrame(p_248847_, p_250486_);
}

}
@Override
public void close() {
}
}

return new TASSpriteContents(name, frameSize, image, metadata);
}
}
}
2 changes: 1 addition & 1 deletion tests/src/main/resources/META-INF/mods.toml
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ modId="fishing_bobber_event_test"
[[mods]]
modId="custom_respawn_test"
[[mods]]
modId="custom_tas_test"
modId="custom_sprite_source_test"
[[mods]]
modId="dimension_settings_test"
[[mods]]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"parent": "minecraft:item/handheld",
"textures": {
"layer0": "custom_tas_test:item/test_item"
"layer0": "custom_sprite_source_test:test_item"
}
}
}

This file was deleted.

8 changes: 8 additions & 0 deletions tests/src/main/resources/assets/minecraft/atlases/blocks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"sources": [
{
"type": "custom_sprite_source_test:custom_sprite_source",
"id": "custom_sprite_source_test:test_item"
}
]
}

0 comments on commit a35d9d2

Please sign in to comment.