Skip to content

Commit

Permalink
Add button enabled/disabled, add level to nametags
Browse files Browse the repository at this point in the history
  • Loading branch information
bibi-reden committed Jul 25, 2024
1 parent 95c34db commit d246af4
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 164 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.bibireden.playerex.mixin;

import com.bibireden.data_attributes.api.DataAttributesAPI;
import com.bibireden.playerex.PlayerEX;
import com.bibireden.playerex.api.attribute.PlayerEXAttributes;
import com.llamalad7.mixinextras.sugar.Local;
import net.minecraft.client.render.entity.EntityRenderer;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.text.Text;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyArg;

import java.util.Optional;

@Mixin(EntityRenderer.class)
abstract class EntityRendererMixin<T extends Entity> {
@Unique
private boolean playerex$shouldRenderLevel() { return PlayerEX.CONFIG.getShowLevelOnNameplates(); }

@ModifyArg(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/entity/EntityRenderer;renderLabelIfPresent(Lnet/minecraft/entity/Entity;Lnet/minecraft/text/Text;Lnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider;I)V"), index = 1)
private Text playerex$renderLabelIfPresent(Text text, @Local(argsOnly = true) Entity entity) {
if (playerex$shouldRenderLevel() && entity instanceof PlayerEntity livingEntity) {
Optional<Double> maybeLevel = DataAttributesAPI.getValue(PlayerEXAttributes.LEVEL, livingEntity);
if (maybeLevel.isPresent()) {
text = text.copy().append(" ").append(Text.translatable("playerex.ui.nameplate.level", maybeLevel.get().intValue()).styled((style) -> style.withColor(0xFFAA00)));
}
}
return text;
}
}

This file was deleted.

15 changes: 10 additions & 5 deletions src/client/kotlin/com/bibireden/playerex/ui/PlayerEXScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import com.bibireden.playerex.networking.types.UpdatePacketType
import com.bibireden.playerex.registry.AttributesMenuRegistry
import com.bibireden.playerex.ui.components.MenuComponent
import com.bibireden.playerex.ui.components.MenuComponent.OnLevelUpdated
import com.bibireden.playerex.ui.components.buttons.AttributeButtonComponent
import com.bibireden.playerex.ui.util.Colors
import com.bibireden.playerex.util.PlayerEXUtil
import io.wispforest.owo.ui.base.BaseUIModelScreen
import io.wispforest.owo.ui.component.ButtonComponent
Expand Down Expand Up @@ -57,25 +59,28 @@ class PlayerEXScreen : BaseUIModelScreen<FlowLayout>(FlowLayout::class.java, Dat

this.uiAdapter.rootComponent.forEachDescendant { descendant ->
if (descendant is MenuComponent) descendant.onLevelUpdatedEvents.sink().onLevelUpdated(level)
if (descendant is AttributeButtonComponent) descendant.refresh()
}
}

/** Whenever any attribute is updated, this will be called. */
fun onAttributeUpdated(attribute: EntityAttribute, value: Double) {
this.uiAdapter.rootComponent.forEachDescendant { descendant ->
if (descendant is MenuComponent) descendant.onAttributeUpdatedEvents.sink().onAttributeUpdated(attribute, value)
if (descendant is AttributeButtonComponent) descendant.refresh()
}
updatePointsAvailable()
}

private fun updatePointsAvailable() {
this.uiAdapter.rootComponent.childById(LabelComponent::class, "points_available")?.apply {
text(Text.translatable("playerex.ui.main.skill_points_available", playerComponent.skillPoints)
.formatted(when (playerComponent.skillPoints) {
0 -> Formatting.GRAY
else -> Formatting.YELLOW
}
))
.styled {
it.withColor(when (playerComponent.skillPoints) {
0 -> Colors.GRAY
else -> Colors.SATURATED_BLUE
})
})
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.bibireden.playerex.ui.PlayerEXScreen.AttributeButtonComponentType
import com.bibireden.playerex.ui.childById
import com.bibireden.playerex.ui.components.buttons.AttributeButtonComponent
import com.bibireden.playerex.ui.components.labels.AttributeLabelComponent
import com.bibireden.playerex.ui.util.Colors
import io.wispforest.owo.ui.component.Components
import io.wispforest.owo.ui.component.LabelComponent
import io.wispforest.owo.ui.container.Containers
Expand All @@ -29,39 +30,40 @@ import net.minecraft.util.Formatting
private val StackingBehavior.symbol: String
get() = if (this == StackingBehavior.Add) "+" else "×"

class AttributeComponent(val attribute: EntityAttribute, val player: PlayerEntity, component: IPlayerDataComponent) : FlowLayout(Sizing.fill(35), Sizing.fixed(18), Algorithm.HORIZONTAL) {
class AttributeComponent(private val attribute: EntityAttribute, private val player: PlayerEntity, component: IPlayerDataComponent) : FlowLayout(Sizing.fill(35), Sizing.fixed(18), Algorithm.HORIZONTAL) {
fun refresh() {
// todo: allow data_attributes to have API funcs for obtaining this data.
val entries = DataAttributesClient.MANAGER.data.functions[attribute.id]
if (!entries.isNullOrEmpty()) {
this.childById(LabelComponent::class, "${this.attribute.id}:label")?.tooltip(
Text.translatable("playerex.ui.main.modified_attributes").also { text ->
text.append("\n")
text.append(Text.literal(attribute.id.toString()).formatted(Formatting.DARK_GRAY))
text.append("\n\n")
entries.forEach { function ->
val childAttribute = EntityAttributeSupplier(function.id).get() ?: return@forEach
val formula = (childAttribute as IEntityAttribute).`data_attributes$formula`()
text.append("\n")
text.append(Text.literal(attribute.id.toString()).formatted(Formatting.DARK_GRAY))
text.append("\n\n")
entries.forEach { function ->
val childAttribute = EntityAttributeSupplier(function.id).get() ?: return@forEach
val formula = (childAttribute as IEntityAttribute).`data_attributes$formula`()

text.apply {
append(Text.translatable(childAttribute.translationKey).styled { it.withColor(0x6EBAE5) })
append(" [")
append(Text.literal(formula.name.uppercase()).styled { it.withColor(if (formula == StackingFormula.Flat) 0xEDCD76 else 0xD63042) })
append("] ")
append(Text.literal(function.behavior.symbol).styled { it.withColor(0x48D19B) })
append(Text.literal("${function.value}"))
append(
Text.literal(
String.format(
" (%.2f)\n",
DataAttributesAPI.getValue(EntityAttributeSupplier(function.id), player).orElse(0.0)
)
).formatted(Formatting.GRAY)
)
formatted(Formatting.ITALIC)
text.apply {
append(Text.translatable(childAttribute.translationKey).styled { it.withColor(Colors.SATURATED_BLUE) })
append(" [")
append(Text.literal(formula.name.uppercase()).styled { it.withColor(if (formula == StackingFormula.Flat) Colors.SANDY else Colors.IMPISH_RED) })
append("] ")
append(Text.literal(function.behavior.symbol).styled { it.withColor(Colors.DARK_GREEN) })
append(Text.literal("${function.value}"))
append(
Text.literal(
String.format(
" (%.2f)\n",
DataAttributesAPI.getValue(childAttribute, player).orElse(0.0)
)
).formatted(Formatting.GRAY)
)
formatted(Formatting.ITALIC)
}
}
}
})
)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bibireden.playerex.ui.components.buttons

import com.bibireden.data_attributes.api.DataAttributesAPI
import com.bibireden.data_attributes.api.attribute.IEntityAttribute
import com.bibireden.playerex.components.player.IPlayerDataComponent
import com.bibireden.playerex.ext.id
import com.bibireden.playerex.networking.NetworkingChannels
Expand All @@ -13,9 +14,8 @@ import io.wispforest.owo.ui.core.Sizing
import net.minecraft.entity.attribute.EntityAttribute
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.text.Text
import net.minecraft.util.Colors

class AttributeButtonComponent(attribute: EntityAttribute, player: PlayerEntity, component: IPlayerDataComponent, type: PlayerEXScreen.AttributeButtonComponentType) : ButtonComponent(
class AttributeButtonComponent(private val attribute: EntityAttribute, private val player: PlayerEntity, private val component: IPlayerDataComponent, private val type: PlayerEXScreen.AttributeButtonComponentType) : ButtonComponent(
Text.literal(type.symbol),
{
// reference text-box to get needed value to send to server
Expand All @@ -25,14 +25,30 @@ class AttributeButtonComponent(attribute: EntityAttribute, player: PlayerEntity,

if (points < amount) return@let // invalid, not enough points.

DataAttributesAPI.getValue(attribute, player).ifPresent { NetworkingChannels.MODIFY.clientHandle().send(
NetworkingPackets.Update(type.packet, attribute.id, amount.toInt()))
DataAttributesAPI.getValue(attribute, player).ifPresent {
NetworkingChannels.MODIFY.clientHandle().send(NetworkingPackets.Update(type.packet, attribute.id, amount.toInt()))
}
}
}
) {
init {
renderer(Renderer.flat(Colors.BLACK, Colors.BLACK, Colors.BLACK))
sizing(Sizing.fixed(12), Sizing.fixed(12))
refresh()
}

fun refresh() {
DataAttributesAPI.getValue(attribute, player).ifPresent { value ->
var active = true
val max = (attribute as IEntityAttribute).`data_attributes$max`();
when (type) {
PlayerEXScreen.AttributeButtonComponentType.Add -> {
active = component.skillPoints > 0 && max > value
}
PlayerEXScreen.AttributeButtonComponentType.Remove -> {
active = component.refundablePoints > 0 && value > 0
}
}
this.active(active)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.bibireden.playerex.ui.components.labels
import com.bibireden.data_attributes.api.DataAttributesAPI
import com.bibireden.data_attributes.api.attribute.IEntityAttribute
import com.bibireden.playerex.ext.id
import com.bibireden.playerex.ui.util.Colors
import io.wispforest.owo.ui.component.LabelComponent
import io.wispforest.owo.ui.core.Sizing
import io.wispforest.owo.ui.core.VerticalAlignment
Expand All @@ -12,7 +13,9 @@ import net.minecraft.text.Text
import net.minecraft.util.Formatting

private fun createTextFromAttribute(attribute: EntityAttribute, player: PlayerEntity) = Text.literal("(")
.append(Text.literal("${DataAttributesAPI.getValue(attribute, player).map(Double::toInt).orElse(0)}").formatted(Formatting.GOLD))
.append(Text.literal("${DataAttributesAPI.getValue(attribute, player).map(Double::toInt).orElse(0)}").styled {
it.withColor(Colors.GOLD)
})
.append("/${(attribute as IEntityAttribute).`data_attributes$max`().toInt()})")

class AttributeLabelComponent(private val attribute: EntityAttribute, private val player: PlayerEntity) : LabelComponent(createTextFromAttribute(attribute, player)) {
Expand Down
10 changes: 10 additions & 0 deletions src/client/kotlin/com/bibireden/playerex/ui/util/Colors.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.bibireden.playerex.ui.util

object Colors {
const val GRAY = 0x9C9C9C
const val GOLD = 0xF0C25E
const val SATURATED_BLUE = 0x6EBAE5
const val SANDY = 0xEDCD76
const val IMPISH_RED = 0xD63042
const val DARK_GREEN = 0x48D19B
}
2 changes: 1 addition & 1 deletion src/client/resources/playerex.client.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"package": "com.bibireden.playerex.mixin",
"compatibilityLevel": "JAVA_17",
"client": [
"LivingEntityRendererMixin"
"EntityRendererMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down
Loading

0 comments on commit d246af4

Please sign in to comment.