-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate
MenuComponent
to registry, and make UI a helluva lot cleaner
- Loading branch information
1 parent
5240c97
commit 9f56dc0
Showing
10 changed files
with
275 additions
and
129 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
src/client/java/com/bibireden/playerex/registry/AttributesMenuRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.bibireden.playerex.registry; | ||
|
||
import com.bibireden.playerex.ui.components.MenuComponent; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public final class AttributesMenuRegistry { | ||
@NotNull | ||
private static final List<Class<? extends MenuComponent>> ENTRIES = new ArrayList<>(); | ||
|
||
/** | ||
* Registers a {@link MenuComponent} to the registry, | ||
* which will be applied to the {@link com.bibireden.playerex.ui.PlayerEXScreen} as a page. | ||
*/ | ||
public static void register(@NotNull Class<? extends MenuComponent> menu) { ENTRIES.add(menu); } | ||
|
||
@NotNull | ||
public static List<Class<? extends MenuComponent>> get() { | ||
return ENTRIES; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/client/kotlin/com/bibireden/playerex/ui/components/MenuComponent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.bibireden.playerex.ui.components | ||
|
||
import com.bibireden.playerex.components.player.IPlayerDataComponent | ||
import com.bibireden.playerex.ui.PlayerEXScreen | ||
import io.wispforest.owo.ui.container.FlowLayout | ||
import io.wispforest.owo.ui.core.OwoUIAdapter | ||
import io.wispforest.owo.ui.core.Sizing | ||
import io.wispforest.owo.util.EventSource | ||
import io.wispforest.owo.util.EventStream | ||
import net.minecraft.client.network.ClientPlayerEntity | ||
import net.minecraft.entity.attribute.EntityAttribute | ||
import org.jetbrains.annotations.ApiStatus | ||
|
||
/** | ||
* A component meant to be used in the **PlayerEX** screen. | ||
* | ||
* This allows for other mods to create their own custom logic, | ||
* and have the benefits of a unique instance that is attached to the primary mod. | ||
*/ | ||
@ApiStatus.OverrideOnly | ||
abstract class MenuComponent(horizontalSizing: Sizing, verticalSizing: Sizing, algorithm: Algorithm) : FlowLayout(horizontalSizing, verticalSizing, algorithm) { | ||
val onLevelUpdatedEvents = OnLevelUpdated.stream | ||
val onAttributeUpdatedEvents = OnAttributeUpdated.stream | ||
|
||
val onLevelUpdated: EventSource<OnLevelUpdated> = onLevelUpdatedEvents.source() | ||
val onAttributeUpdated: EventSource<OnAttributeUpdated> = onAttributeUpdatedEvents.source() | ||
|
||
/** When the [PlayerEXScreen] is ready to be constructed, this function (if the component is registered) will be called.*/ | ||
abstract fun build(player: ClientPlayerEntity, adapter: OwoUIAdapter<FlowLayout>, component: IPlayerDataComponent) | ||
|
||
fun interface OnLevelUpdated { | ||
fun onLevelUpdated(level: Int) | ||
|
||
companion object { | ||
val stream: EventStream<OnLevelUpdated> get() = EventStream { subscribers -> | ||
OnLevelUpdated { level -> subscribers.forEach { it.onLevelUpdated(level) } } | ||
} | ||
} | ||
} | ||
|
||
fun interface OnAttributeUpdated { | ||
fun onAttributeUpdated(attribute: EntityAttribute, level: Double) | ||
|
||
companion object { | ||
val stream: EventStream<OnAttributeUpdated> get() = EventStream { subscribers -> | ||
OnAttributeUpdated { attribute, value -> subscribers.forEach { it.onAttributeUpdated(attribute, value) } } | ||
} | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/client/kotlin/com/bibireden/playerex/ui/components/labels/AttributeButtonComponent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.bibireden.playerex.ui.components.labels | ||
|
||
import com.bibireden.data_attributes.api.DataAttributesAPI | ||
import com.bibireden.playerex.components.player.IPlayerDataComponent | ||
import com.bibireden.playerex.ext.id | ||
import com.bibireden.playerex.networking.NetworkingChannels | ||
import com.bibireden.playerex.networking.NetworkingPackets | ||
import com.bibireden.playerex.ui.PlayerEXScreen | ||
import com.bibireden.playerex.ui.childById | ||
import io.wispforest.owo.ui.component.ButtonComponent | ||
import io.wispforest.owo.ui.component.TextBoxComponent | ||
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( | ||
Text.literal(type.symbol), | ||
{ | ||
// reference text-box to get needed value to send to server | ||
it.parent()?.childById(TextBoxComponent::class, "entry:${attribute.id}")?.let { box -> | ||
val amount = box.text.toDoubleOrNull() ?: return@let | ||
val points = type.getPointsFromComponent(component) | ||
|
||
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())) | ||
} | ||
} | ||
} | ||
) { | ||
init { | ||
renderer(Renderer.flat(Colors.BLACK, Colors.BLACK, Colors.BLACK)) | ||
sizing(Sizing.fixed(12), Sizing.fixed(12)) | ||
} | ||
} |
Oops, something went wrong.