Skip to content

Commit

Permalink
huge refactoring for easy to use API #24 and undergarden support
Browse files Browse the repository at this point in the history
- still needs testing before release
  • Loading branch information
Terrails committed May 4, 2024
1 parent b40b967 commit d8157da
Show file tree
Hide file tree
Showing 59 changed files with 1,440 additions and 1,188 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
id "architectury-plugin" version "3.4-SNAPSHOT"
id "dev.architectury.loom" version "1.5-SNAPSHOT" apply false
id "dev.architectury.loom" version "1.6-SNAPSHOT" apply false
id "com.modrinth.minotaur" version "2.+" apply false
id 'net.darkhax.curseforgegradle' version '1.1.+' apply false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ public class CColorfulHearts {
public static final String MOD_NAME = "Colorful Hearts";
public static final Logger LOGGER = LogManager.getLogger(MOD_NAME);

public static final ResourceLocation SPRITE_NAME = new ResourceLocation(CColorfulHearts.MOD_ID, "colored_hearts");
public static ResourceLocation location(String path) {
return new ResourceLocation(CColorfulHearts.MOD_ID, path);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,26 @@

import dev.architectury.injectables.annotations.ExpectPlatform;
import net.minecraft.client.gui.GuiGraphics;
import org.jetbrains.annotations.Contract;
import terrails.colorfulhearts.api.event.HeartRenderEvent;
import terrails.colorfulhearts.heart.CHeartType;
import terrails.colorfulhearts.api.event.HeartRegistry;
import terrails.colorfulhearts.api.heart.drawing.StatusEffectHeart;

public class LoaderExpectPlatform {

/**
* Returns the lowercase name of current modloader
* @return modloader name
*/
@Contract
@ExpectPlatform
public static String getLoader() { throw new AssertionError(); }

/**
* Applies changes to night-config's FileConfig
* Technically not needed if autosave were to be enabled
*/
@Contract
@ExpectPlatform
public static void applyConfig() { throw new AssertionError(); }

Expand All @@ -22,29 +30,57 @@ public class LoaderExpectPlatform {
* Currently only possible with Fabric via ObjectShare
* @return if hardcore textures should be used even if not in a hardcore world
*/
@Contract
@ExpectPlatform
public static boolean forcedHardcoreHearts() { throw new AssertionError(); }

/**
* Event to register custom hearts. Current use is for status effect type hearts
* @param registry heart registry
*/
@Contract
@ExpectPlatform
public static void heartRegistryEvent(HeartRegistry registry) {
throw new AssertionError();
}

/**
* Called before health renderer draws anything on screen
* @param guiGraphics used by the renderer
* @param x position of the hearts
* @param y position of the hearts
* @param blinking eligible hearts should blink
* @param hardcore hearts should be of hardcore type
* @param effectHeart type of effect heart, null otherwise
* @return the event with modified values
*/
@Contract
@ExpectPlatform
public static HeartRenderEvent.Pre preRenderEvent(
GuiGraphics guiGraphics, int x, int y,
boolean blinking, boolean hardcore,
CHeartType healthType, CHeartType absorbingType
) {
public static HeartRenderEvent.Pre preRenderEvent(GuiGraphics guiGraphics, int x, int y, boolean blinking, boolean hardcore, StatusEffectHeart effectHeart) {
throw new AssertionError();
}

/**
* Called after health renderer finishes drawing
* @param guiGraphics used by renderer
* @param x final position of the hearts
* @param y final position of the hearts
* @param blinking eligible hearts blink
* @param hardcore hearts are of hardcore type
* @param effectHeart type of effect heart, null otherwise
*/
@Contract
@ExpectPlatform
public static void postRenderEvent(
GuiGraphics guiGraphics, int x, int y,
boolean blinking, boolean hardcore,
CHeartType healthType, CHeartType absorbingType
) {
public static void postRenderEvent(GuiGraphics guiGraphics, int x, int y, boolean blinking, boolean hardcore, StatusEffectHeart effectHeart) {
throw new AssertionError();
}

/**
* Just an empty event used to notify about in-game changes from the Config Screen
*/
@Contract
@ExpectPlatform
public static void heartChangeEvent() {
public static void heartUpdateEvent() {
throw new AssertionError();
}
}
10 changes: 0 additions & 10 deletions common/src/main/java/terrails/colorfulhearts/Utils.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package terrails.colorfulhearts.api.event;

import terrails.colorfulhearts.api.heart.Hearts;
import terrails.colorfulhearts.api.heart.drawing.StatusEffectHeart;

public class HeartRegistry {

public <T extends StatusEffectHeart> T registerStatusEffectHeart(T heart) {
Hearts.STATUS_EFFECT_HEARTS.put(heart.getId(), heart);
return heart;
}
}
Original file line number Diff line number Diff line change
@@ -1,68 +1,75 @@
package terrails.colorfulhearts.api.event;

import net.minecraft.client.gui.GuiGraphics;
import terrails.colorfulhearts.heart.CHeartType;
import terrails.colorfulhearts.api.heart.drawing.StatusEffectHeart;

import java.util.Optional;

/**
* A set of events useful to render any overlays
* A set of events useful to control the health renderer and render overlays
*/
public class HeartRenderEvent {

/**
* Event executed before health renderer does anything
* Called before health renderer draws anything on screen
* Can be used to do any tweaks to the rendering
*/
public static class Pre extends HeartRenderEvent {

private boolean cancelled = false;

public Pre(
GuiGraphics guiGraphics, int x, int y,
boolean blinking, boolean hardcore,
CHeartType healthType, CHeartType absorbingType
) {
super(guiGraphics, x, y, blinking, hardcore, healthType, absorbingType);
public Pre(GuiGraphics guiGraphics, int x, int y, boolean blinking, boolean hardcore, StatusEffectHeart effectHeart) {
super(guiGraphics, x, y, blinking, hardcore, effectHeart);
}

public void cancel() {
this.cancelled = true;
}

public boolean isCancelled() {
return this.cancelled;
}

public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}

public void setBlinking(boolean blinking) {
this.blinking = blinking;
}
public void setHardcore(boolean hardcore) {
this.hardcore = hardcore;
}
public void setEffectHeart(StatusEffectHeart heart) {
this.effectHeart = heart;
}
}

/**
* Event executed after health renderer finished
* Called after health renderer finishes drawing
* Can be used to render additional overlays on top of health
*/
public static class Post extends HeartRenderEvent {

public Post(
GuiGraphics guiGraphics, int x, int y,
boolean blinking, boolean hardcore,
CHeartType healthType, CHeartType absorbingType
) {
super(guiGraphics, x, y, blinking, hardcore, healthType, absorbingType);
public Post(GuiGraphics guiGraphics, int x, int y, boolean blinking, boolean hardcore, StatusEffectHeart effectHeart) {
super(guiGraphics, x, y, blinking, hardcore, effectHeart);
}
}

private final GuiGraphics guiGraphics;
private int x, y;
private boolean blinking, hardcore;
private CHeartType healthType, absorbingType;
protected final GuiGraphics guiGraphics;
protected int x, y;
protected boolean blinking, hardcore;
protected StatusEffectHeart effectHeart;

public HeartRenderEvent(
GuiGraphics guiGraphics, int x, int y,
boolean blinking, boolean hardcore,
CHeartType healthType, CHeartType absorbingType
) {
public HeartRenderEvent(GuiGraphics guiGraphics, int x, int y, boolean blinking, boolean hardcore, StatusEffectHeart effectHeart) {
this.guiGraphics = guiGraphics;
this.x = x;
this.y = y;
this.blinking = blinking;
this.hardcore = hardcore;
this.healthType = healthType;
this.absorbingType = absorbingType;
this.effectHeart = effectHeart;
}

public GuiGraphics getGuiGraphics() {
Expand All @@ -72,48 +79,18 @@ public GuiGraphics getGuiGraphics() {
public int getX() {
return x;
}

public void setX(int x) {
this.x = x;
}

public int getY() {
return y;
}

public void setY(int y) {
this.y = y;
}

public boolean isBlinking() {
return blinking;
}

public void setBlinking(boolean blinking) {
this.blinking = blinking;
}

public boolean isHardcore() {
return hardcore;
}

public void setHardcore(boolean hardcore) {
this.hardcore = hardcore;
}

public CHeartType getHealthType() {
return healthType;
}

public void setHealthType(CHeartType healthType) {
this.healthType = healthType;
}

public CHeartType getAbsorbingType() {
return absorbingType;
}

public void setAbsorbingType(CHeartType absorbingType) {
this.absorbingType = absorbingType;
public Optional<StatusEffectHeart> getEffectHeart() {
return Optional.ofNullable(effectHeart);
}
}
32 changes: 32 additions & 0 deletions common/src/main/java/terrails/colorfulhearts/api/heart/Hearts.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package terrails.colorfulhearts.api.heart;

import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Player;
import terrails.colorfulhearts.CColorfulHearts;
import terrails.colorfulhearts.api.heart.drawing.HeartDrawing;
import terrails.colorfulhearts.api.heart.drawing.SpriteHeartDrawing;
import terrails.colorfulhearts.api.heart.drawing.StatusEffectHeart;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public class Hearts {

public static final HeartDrawing CONTAINER = SpriteHeartDrawing.build(CColorfulHearts.location("container")).finish(
new ResourceLocation("hud/heart/container"), new ResourceLocation("hud/heart/container_blinking"),
new ResourceLocation("colorfulhearts", "heart/container_half"), new ResourceLocation("colorfulhearts", "heart/container_half_blinking"),
new ResourceLocation("hud/heart/container_hardcore"), new ResourceLocation("hud/heart/container_hardcore_blinking"),
new ResourceLocation("colorfulhearts", "heart/container_hardcore_half"), new ResourceLocation("colorfulhearts", "heart/container_hardcore_half_blinking")
);

public static List<HeartDrawing> COLORED_HEALTH_HEARTS;
public static List<HeartDrawing> COLORED_ABSORPTION_HEARTS;
public static Map<ResourceLocation, StatusEffectHeart> STATUS_EFFECT_HEARTS = new HashMap<>();

public static Optional<StatusEffectHeart> getEffectHeartForPlayer(Player player) {
return STATUS_EFFECT_HEARTS.values().stream().filter(heart -> heart.shouldDraw(player)).findFirst();
}

}

0 comments on commit d8157da

Please sign in to comment.