Skip to content

Commit

Permalink
Add bingo:sprite icon type
Browse files Browse the repository at this point in the history
Resolves #15

Example:
"icon": {
  "type": "bingo:sprite",
  "sprite": "sprite_icon:smiley",
  "fallback": {
    "count": 1,
    "id": "yellow_dye"
  }
}
  • Loading branch information
Gaming32 committed Nov 25, 2024
1 parent a5a5f99 commit 642b21d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ public static void setup() {
IconRenderers.register(GoalIconType.INSTRUMENT_CYCLE, new InstrumentCycleIconRenderer());
IconRenderers.register(GoalIconType.ITEM, (icon, graphics, x, y) -> graphics.renderFakeItem(icon.item(), x, y));
IconRenderers.register(GoalIconType.ITEM_TAG_CYCLE, new ItemTagCycleIconRenderer());
IconRenderers.register(GoalIconType.SPRITE, new SpriteIconRenderer());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.github.gaming32.bingo.client.icons;

import io.github.gaming32.bingo.data.icons.SpriteIcon;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.renderer.RenderType;

public class SpriteIconRenderer implements IconRenderer<SpriteIcon> {
@Override
public void render(SpriteIcon icon, GuiGraphics graphics, int x, int y) {
final var sprite = icon.sprite().withPrefix("bingo/icon/");
graphics.blitSprite(RenderType::guiTextured, sprite, x, y, 16, 16);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public interface GoalIconType<I extends GoalIcon> {
RegistryValue<GoalIconType<InstrumentCycleIcon>> INSTRUMENT_CYCLE = register("instrument_cycle", InstrumentCycleIcon.CODEC, InstrumentCycleIcon.STREAM_CODEC);
RegistryValue<GoalIconType<ItemIcon>> ITEM = register("item", ItemIcon.CODEC, ItemIcon.STREAM_CODEC);
RegistryValue<GoalIconType<ItemTagCycleIcon>> ITEM_TAG_CYCLE = register("item_tag_cycle", ItemTagCycleIcon.CODEC, ItemTagCycleIcon.STREAM_CODEC);
RegistryValue<GoalIconType<SpriteIcon>> SPRITE = register("sprite", SpriteIcon.CODEC, SpriteIcon.STREAM_CODEC);

MapCodec<I> codec();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.github.gaming32.bingo.data.icons;

import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import io.github.gaming32.bingo.util.BingoCodecs;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;

public record SpriteIcon(ResourceLocation sprite, ItemStack fallback) implements GoalIcon.WithoutContext {
public static final MapCodec<SpriteIcon> CODEC = RecordCodecBuilder.mapCodec(
instance -> instance.group(
ResourceLocation.CODEC.fieldOf("sprite").forGetter(SpriteIcon::sprite),
BingoCodecs.LENIENT_ITEM_STACK.fieldOf("fallback").forGetter(SpriteIcon::fallback)
).apply(instance, SpriteIcon::new)
);
public static final StreamCodec<RegistryFriendlyByteBuf, SpriteIcon> STREAM_CODEC = StreamCodec.composite(
ResourceLocation.STREAM_CODEC, SpriteIcon::sprite,
ItemStack.STREAM_CODEC, SpriteIcon::fallback,
SpriteIcon::new
);

@Override
public ItemStack getFallback() {
return fallback;
}

@Override
public GoalIconType<?> type() {
return GoalIconType.SPRITE.get();
}
}

0 comments on commit 642b21d

Please sign in to comment.