-
-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: Adds drink XP feature #220
Open
kaique-lisboa
wants to merge
1
commit into
lucaargolo:1.20
Choose a base branch
from
kaique-lisboa:feature/drink-xp-from-tank
base: 1.20
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
77 changes: 77 additions & 0 deletions
77
src/main/kotlin/io/github/lucaargolo/kibe/utils/XpUtils.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,77 @@ | ||
package io.github.lucaargolo.kibe.utils | ||
|
||
import io.github.lucaargolo.kibe.MOD_CONFIG | ||
import io.github.lucaargolo.kibe.fluids.LIQUID_XP | ||
import io.github.lucaargolo.kibe.fluids.miscellaneous.LiquidXpFluid | ||
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidConstants | ||
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant | ||
import net.fabricmc.fabric.api.transfer.v1.storage.Storage | ||
import net.fabricmc.fabric.api.transfer.v1.storage.base.SingleVariantStorage | ||
import net.fabricmc.fabric.api.transfer.v1.transaction.Transaction | ||
import net.minecraft.entity.player.PlayerEntity | ||
import net.minecraft.sound.SoundCategory | ||
import net.minecraft.sound.SoundEvents | ||
import net.minecraft.util.ActionResult | ||
import net.minecraft.util.Hand | ||
import kotlin.math.min | ||
|
||
|
||
object XpUtils { | ||
|
||
const val MB_PER_XP = FluidConstants.BUCKET / 100 | ||
|
||
private fun isTankValidXpDonor(tank: Storage<FluidVariant>) = | ||
tank is SingleVariantStorage<FluidVariant> && tank.variant.fluid is LiquidXpFluid | ||
|
||
|
||
fun expToReachNextLvl(nextLevelExp: Int, progress: Float): Int { | ||
return nextLevelExp - (nextLevelExp * progress).toInt() | ||
} | ||
|
||
fun convertXpToMilibuckets(xp: Long) = MB_PER_XP * xp | ||
|
||
fun convertMilibucketsToXp(milibuckets: Long) = milibuckets / MB_PER_XP | ||
|
||
fun donateXpAction(player: PlayerEntity, tank: SingleVariantStorage<FluidVariant>): ActionResult { | ||
if(tank.amount < MB_PER_XP) return ActionResult.FAIL | ||
|
||
val xpToNextLevel = expToReachNextLvl(player.nextLevelExperience, player.experienceProgress) | ||
|
||
val liquidXpToExtract = min( | ||
convertXpToMilibuckets(xpToNextLevel.toLong()), | ||
tank.amount | ||
) | ||
|
||
Transaction.openOuter().also { transaction -> | ||
var extractedAmount = -1L | ||
tank.extract(FluidVariant.of(LIQUID_XP), liquidXpToExtract, transaction).let { extractedAmount = it } | ||
transaction.addCloseCallback { _, result -> | ||
if(result.wasAborted()) { | ||
return@addCloseCallback | ||
} | ||
player.addExperience(convertMilibucketsToXp(liquidXpToExtract).toInt()) | ||
if(extractedAmount > 0) { | ||
player.world.playSound( | ||
null, | ||
player.blockPos, | ||
SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, | ||
SoundCategory.AMBIENT, | ||
.5f, | ||
player.world.random.nextBetween(3,9).toFloat() / 10) | ||
} | ||
} | ||
transaction.commit() | ||
} | ||
|
||
return ActionResult.success(true) | ||
} | ||
|
||
fun canPlayerDrinkXp(tank: Storage<FluidVariant>, player: PlayerEntity, hand: Hand): Boolean { | ||
return MOD_CONFIG.miscellaneousModule.xpTankDrinkOnRightClick && | ||
player.mainHandStack.isEmpty && | ||
hand == Hand.MAIN_HAND && | ||
isTankValidXpDonor(tank) | ||
|
||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
very important step, validating on transaction success ensures that the XP was actually drained from tank before adding the experience levels to player.
XpDrain
andXpShowerBlockEntity
should usewasAborted
orwasCommited
methods of Transaction as well, as of today, if the fluid transfer fails for whatever reason, the XP will be drained / created either way