Skip to content

Commit

Permalink
Polished Label rendering stuff, added accessibility feature
Browse files Browse the repository at this point in the history
  • Loading branch information
AV306 committed Dec 28, 2023
1 parent 5f1807d commit dd51327
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 41 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ dependencies {
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"

// CompleteConfig
// why is there nothing in here
modImplementation "com.github.Lortseam.completeconfig:base:${project.complete_config_version}"
modImplementation "com.github.Lortseam.completeconfig:gui-cloth:${project.complete_config_version}"
//modImplementation "com.github.shedaniel:cloth-config:v10-SNAPSHOT"
Expand Down
10 changes: 5 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

# Fabric Properties
# check these on https://fabricmc.net/versions.html
minecraft_version = 1.20.2
yarn_mappings = 1.20.2+build.4
loader_version = 0.14.24
minecraft_version = 1.20.1
yarn_mappings = 1.20.1+build.10
loader_version = 0.15.3

# Fabric api
fabric_version = 0.90.4+1.20.2
fabric_version = 0.91.0+1.20.1

# Mod Properties
mod_version = 4.3.0+1.20.2
mod_version = 4.4.0+1.20.1
maven_group = me.av306
archives_base_name = xenon

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/me/av306/xenon/Xenon.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import me.av306.xenon.event.ClientWorldEvents;
import me.av306.xenon.feature.*;
import me.av306.xenon.features.*;
import me.av306.xenon.features.accessibility.*;
import me.av306.xenon.features.chat.*;
import me.av306.xenon.features.movement.*;
import me.av306.xenon.features.render.*;
Expand All @@ -19,7 +20,6 @@
import net.fabricmc.loader.api.ModContainer;
import net.fabricmc.loader.api.Version;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
Expand Down Expand Up @@ -139,6 +139,7 @@ private void initFeatures()
new FullBrightFeature();
if ( FullKeyboardGroup.enable ) new FullKeyboardFeature();
new HealthDisplayFeature();
new LabelFeature();
new MultiQuickChatFeature();
new ProximityRadarFeature();
new QuickChatFeature();
Expand Down
27 changes: 23 additions & 4 deletions src/main/java/me/av306/xenon/event/EntityRendererEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.client.render.entity.model.EntityModel;
import net.minecraft.entity.Entity;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;

public interface EntityRendererEvents
{
Event<GetLabelText> RENDER_LABEL_TEXT = EventFactory.createArrayBacked(
Event<GetLabelText> GET_LABEL_TEXT = EventFactory.createArrayBacked(
GetLabelText.class,
(listeners) -> (entity, text) ->
{
Expand All @@ -25,15 +23,36 @@ public interface EntityRendererEvents
}
);

Event<GetHasLabel> GET_HAS_LABEL = EventFactory.createArrayBacked(
GetHasLabel.class,
(listeners) -> (entity) ->
{
for ( GetHasLabel listener : listeners )
{
ActionResult result = listener.onGetHasLabel( entity );

if ( result != ActionResult.PASS ) return result;
}

return ActionResult.PASS;
}
);

@FunctionalInterface
interface GetLabelText<T extends Entity>
{
ActionResult onGetLabelText( T entity, Text text );
}

@FunctionalInterface
interface GetHasLabel<T extends Entity>
{
ActionResult onGetHasLabel( T entity );
}

class EventData
{
public static Text LABEL_TEXT_OVERRIDE;
public static Text LABEL_TEXT_OVERRIDE = null;
public static boolean SHOULD_OVERRIDE_LABEL_TEXT = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.client.render.entity.model.EntityModel;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.util.ActionResult;

public interface MobEntityRendererEvents
{
Event<RenderLabelTextEvent> RENDER_LABEL_TEXT = EventFactory.createArrayBacked(
RenderLabelTextEvent.class,
Event<GetHasLabel> GET_HAS_LABEL = EventFactory.createArrayBacked(
GetHasLabel.class,
(listeners) -> (entity) ->
{
for ( RenderLabelTextEvent listener : listeners )
for ( GetHasLabel listener : listeners )
{
ActionResult result = listener.onGetHasLabel( entity );

Expand All @@ -24,7 +23,7 @@ public interface MobEntityRendererEvents
);

@FunctionalInterface
interface RenderLabelTextEvent<T extends MobEntity>
interface GetHasLabel<T extends MobEntity>
{
ActionResult onGetHasLabel( T entity );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package me.av306.xenon.features.accessibility;

import me.av306.xenon.event.EntityRendererEvents;
import me.av306.xenon.event.MobEntityRendererEvents;
import me.av306.xenon.feature.IToggleableFeature;
import net.minecraft.entity.Entity;
import net.minecraft.util.ActionResult;

public class LabelFeature extends IToggleableFeature
{
public LabelFeature()
{
super( "Labels", "label", "lb" );

EntityRendererEvents.GET_HAS_LABEL.register( this::onEntityRendererGetHasLabel );
MobEntityRendererEvents.GET_HAS_LABEL.register( this::onEntityRendererGetHasLabel );
}

private ActionResult onEntityRendererGetHasLabel( Entity entity )
{
return this.isEnabled ? ActionResult.SUCCESS : ActionResult.PASS;
}

@Override
protected void onEnable()
{

}

@Override
protected void onDisable()
{

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,38 @@
// FIXME: Make this adhere to my convention that feature-specific code should not be in mixins
public class HealthDisplayFeature extends IToggleableFeature
{
private static HealthDisplayFeature INSTANCE;
public static HealthDisplayFeature getInstance() { return INSTANCE; }
public HealthDisplayFeature()
{
super( "HealthDisplay", "hd", "healthdisp" );

// HudRenderCallback.EVENT.register( this::onHudRender );
INSTANCE = this;
MobEntityRendererEvents.GET_HAS_LABEL.register( this::shouldForceEntityNameplate );

MobEntityRendererEvents.RENDER_LABEL_TEXT.register( this::shouldForceEntityNameplate );

EntityRendererEvents.RENDER_LABEL_TEXT.register( this::getLabelText );
EntityRendererEvents.GET_LABEL_TEXT.register( this::getLabelText );
}

private ActionResult getLabelText( Entity entity, Text text )
{
if ( !this.isEnabled ) return ActionResult.PASS;

try
if ( this.isEnabled )
{
LivingEntity livingEntity = (LivingEntity) entity;

EntityRendererEvents.EventData.LABEL_TEXT_OVERRIDE = text.copy().append( String.format( " §c(%.2f❤)§r", livingEntity.getHealth() ) );
try
{
LivingEntity livingEntity = (LivingEntity) entity;

EntityRendererEvents.EventData.LABEL_TEXT_OVERRIDE = text.copy().append( String.format( " §c(%.2f❤)§r", livingEntity.getHealth() ) );
}
catch ( ClassCastException ignored )
{
// Play nicely with Labels
EntityRendererEvents.EventData.LABEL_TEXT_OVERRIDE = text;
}
}
catch ( ClassCastException ignored ) {}

return ActionResult.PASS;
}

private ActionResult shouldForceEntityNameplate( MobEntity mobEntity )
{
return this.isEnabled ? ActionResult.FAIL : ActionResult.PASS;
return this.isEnabled ? ActionResult.SUCCESS : ActionResult.PASS;
}

@Override
Expand Down
23 changes: 16 additions & 7 deletions src/main/java/me/av306/xenon/mixin/EntityRendererMixin.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package me.av306.xenon.mixin;

import me.av306.xenon.event.EntityRendererEvents;
import me.av306.xenon.features.render.HealthDisplayFeature;
import net.minecraft.entity.Entity;
import net.minecraft.text.Text;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.entity.EntityRenderer;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.util.ActionResult;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin( EntityRenderer.class )
public class EntityRendererMixin<T extends Entity>
{
/*@Inject(
@Inject(
method = "hasLabel(Lnet/minecraft/entity/Entity;)Z",
at = @At( "HEAD" ),
cancellable = true
Expand All @@ -23,9 +25,13 @@ private void onGetHasLabel( T entity, CallbackInfoReturnable<Boolean> cir )
{
// This only works on dropped entities, living entities use MobEntityRenderer which overrides this method
// You can use this to make dropped items have names. Hmmm...
// TODO: Make this a feature :D
cir.setReturnValue( false );
}*/
ActionResult result = EntityRendererEvents.GET_HAS_LABEL.invoker().onGetHasLabel( entity );
if ( result == ActionResult.SUCCESS )
cir.setReturnValue( true );

else if ( result == ActionResult.FAIL )
cir.setReturnValue( false );
}

@ModifyVariable(
argsOnly = true,
Expand All @@ -39,8 +45,11 @@ private Text modifyLabelText( Text text, T entity, Text textOther, MatrixStack m
{
//if ( !HealthDisplayFeature.getInstance().getIsEnabled() ) return text1;

EntityRendererEvents.RENDER_LABEL_TEXT.invoker().onGetLabelText( entity, text );
// Do the thing to give features a chance to set the event data
EntityRendererEvents.GET_LABEL_TEXT.invoker().onGetLabelText( entity, text );

Text textOverride = EntityRendererEvents.EventData.LABEL_TEXT_OVERRIDE;

return EntityRendererEvents.EventData.SHOULD_OVERRIDE_LABEL_TEXT ? EntityRendererEvents.EventData.LABEL_TEXT_OVERRIDE : text;
return EntityRendererEvents.EventData.SHOULD_OVERRIDE_LABEL_TEXT && textOverride != null ? EntityRendererEvents.EventData.LABEL_TEXT_OVERRIDE : text;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ public class MobEntityRendererMixin<T extends MobEntity, M extends EntityModel<T
)
private void onGetHasLabel( T entity, CallbackInfoReturnable<Boolean> cir )
{
// Don't block name tags when disabled
//if ( HealthDisplayFeature.getInstance().shouldForceEntityNameplate() )
//cir.setReturnValue( true );
// Decide whether to force nameplates, block them or do nothing
ActionResult result = MobEntityRendererEvents.GET_HAS_LABEL.invoker().onGetHasLabel( entity );

if ( MobEntityRendererEvents.RENDER_LABEL_TEXT.invoker().onGetHasLabel( entity ) == ActionResult.FAIL )
if ( result == ActionResult.SUCCESS )
cir.setReturnValue( true );
else if ( result == ActionResult.FAIL )
cir.setReturnValue( false );
}
}

0 comments on commit dd51327

Please sign in to comment.