Skip to content

Commit

Permalink
Implement original refund, change PacketType
Browse files Browse the repository at this point in the history
  • Loading branch information
bibi-reden committed Jul 22, 2024
1 parent 1c1e61a commit 1412cdc
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 50 deletions.
28 changes: 11 additions & 17 deletions src/main/kotlin/com/bibireden/playerex/PlayerEX.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bibireden.playerex

import com.bibireden.data_attributes.api.attribute.EntityAttributeSupplier
import com.bibireden.data_attributes.api.factory.DefaultAttributeFactory
import com.bibireden.playerex.api.PlayerEXAPI
import com.bibireden.playerex.api.attribute.DefaultAttributeImpl
Expand All @@ -12,6 +13,7 @@ import com.bibireden.playerex.factory.*
import com.bibireden.playerex.networking.NetworkingChannels
import com.bibireden.playerex.networking.NetworkingPackets
import com.bibireden.playerex.networking.registerServerbound
import com.bibireden.playerex.networking.types.UpdatePacketType
import eu.pb4.placeholders.api.Placeholders
import net.fabricmc.api.ModInitializer
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback
Expand All @@ -34,24 +36,16 @@ object PlayerEX : ModInitializer {
fun id(path: String) = Identifier.of(MOD_ID, path)!!

override fun onInitialize() {
NetworkingChannels.MODIFY.registerServerbound(NetworkingPackets.Update::class) { (type, refs), ctx ->
val player = ctx.player
val server = ctx.player.server

val component = PlayerEXComponents.PLAYER_DATA.get(player)

// todo: needs to be redone to support many packed attributes... depending on amount, refundable and skill-points have to be adjusted...
// if (type.applyIfValid(server, player, component)) {
// for ((id, value) in refs) {
// Registries.ATTRIBUTE[id]?.let { attr ->
// DataAttributesAPI.getValue(attr, player).ifPresent { component.add(attr, it) }
// }
// }
// }
NetworkingChannels.MODIFY.registerServerbound(NetworkingPackets.Update::class) { (type, id, amount), ctx ->
EntityAttributeSupplier(id).get()?.let {
when (type) {
UpdatePacketType.Skill -> PlayerEXComponents.PLAYER_DATA.get(ctx.player).skillUp(it, amount)
UpdatePacketType.Refund -> PlayerEXComponents.PLAYER_DATA.get(ctx.player).refund(it, amount)
}
}
}
NetworkingChannels.MODIFY.registerServerbound(NetworkingPackets.Level::class) { (levelsToAdd), ctx ->
if (levelsToAdd <= 0) return@registerServerbound
PlayerEXComponents.PLAYER_DATA.get(ctx.player).levelUp(levelsToAdd)
NetworkingChannels.MODIFY.registerServerbound(NetworkingPackets.Level::class) { (amount), ctx ->
PlayerEXComponents.PLAYER_DATA.get(ctx.player).levelUp(amount)
}

CommandRegistrationCallback.EVENT.register(PlayerEXCommands::register)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,7 @@ interface IPlayerDataComponent : Component {
* If not possible, it will return `false`.
* This can be changed by setting `true` to the [override] argument. */
fun skillUp(skill: EntityAttribute, amount: Int, override: Boolean = false): Boolean

/** Refunds skill points based on the amount provided and how much [refundablePoints] the player currently has. */
fun refund(skill: EntityAttribute, amount: Int): Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,13 @@ class PlayerDataComponent(
}.orElse(false)
}

override fun refund(skill: EntityAttribute, amount: Int): Boolean {
if (amount <= 0 || this.refundablePoints < amount) return false
this.addRefundablePoints(-1)
this.addSkillPoints(1)
return true
}

override val skillPoints: Int
get() = this._skillPoints

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.bibireden.playerex.networking

import com.bibireden.playerex.networking.types.AttributePacketType
import com.bibireden.playerex.networking.types.UpdatePacketType
import com.bibireden.playerex.networking.types.NotificationType
import net.minecraft.entity.attribute.EntityAttribute
import net.minecraft.util.Identifier


Expand All @@ -14,14 +13,14 @@ object NetworkingPackets {
data class Notify(val type: NotificationType)

/**
* Updates the provided attribute(s) with an associated [Double] value.
* Possibility will be dictated based on the [AttributePacketType]
* Updates the provided attribute with an associated [Double] value.
* Possibility will be dictated based on the [UpdatePacketType]
*
* todo: make it add the amounts given, not set.
* todo: also, ensure they SHOULD be able to do this, based on the amount of skill points.
*/
@JvmRecord
data class Update(val type: AttributePacketType, val refs: Map<Identifier, Double>)
data class Update(val type: UpdatePacketType, val id: Identifier, val amount: Int)

/** Packet specifically for handling leveling events. Provides the amount of levels the client intends to increase by. */
@JvmRecord
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.bibireden.playerex.networking.types

enum class UpdatePacketType {
Skill,
Refund;
}

0 comments on commit 1412cdc

Please sign in to comment.