Skip to content

Commit

Permalink
Wrappers: Add CTWrapper class
Browse files Browse the repository at this point in the history
  • Loading branch information
camnwalter authored and mattco98 committed Jun 22, 2023
1 parent 099295e commit 90453a0
Show file tree
Hide file tree
Showing 30 changed files with 441 additions and 444 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ object ChatLib {

// helper method to make sure player exists before putting something in chat
fun checkPlayerExists(out: String): Boolean {
if (Player.getPlayer() == null) {
if (Player.toMC() == null) {
out.printToConsole()
return false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ object Renderer {
*
* Takes a parameter with the following options:
* - player: The player entity to draw. Can be a [PlayerMP] or [AbstractClientPlayerEntity].
* Defaults to Player.getPlayer()
* Defaults to Player.toMC()
* - x: The x position on the screen to render the player
* - y: The y position on the screen to render the player
* - size: The size of the rendered player
Expand All @@ -527,8 +527,8 @@ object Renderer {
fun drawPlayer(obj: NativeObject) {
val entity = obj["player"].let {
it as? AbstractClientPlayerEntity
?: ((it as? PlayerMP)?.entity as? AbstractClientPlayerEntity)
?: Player.getPlayer()
?: ((it as? PlayerMP)?.toMC() as? AbstractClientPlayerEntity)
?: Player.toMC()
?: return
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.chattriggers.ctjs.minecraft.objects

import com.chattriggers.ctjs.CTJS
import com.chattriggers.ctjs.minecraft.wrappers.CTWrapper
import com.chattriggers.ctjs.minecraft.wrappers.Player
import com.chattriggers.ctjs.minecraft.wrappers.World
import com.chattriggers.ctjs.mixins.AbstractSoundInstanceAccessor
Expand Down Expand Up @@ -266,7 +267,7 @@ class Sound(private val config: NativeObject) {
}
}

enum class Category(private val mcValue: SoundCategory) {
enum class Category(override val mcValue: SoundCategory) : CTWrapper<SoundCategory> {
MASTER(SoundCategory.MASTER),
MUSIC(SoundCategory.MUSIC),
RECORDS(SoundCategory.RECORDS),
Expand All @@ -278,8 +279,6 @@ class Sound(private val config: NativeObject) {
AMBIENT(SoundCategory.AMBIENT),
VOICE(SoundCategory.VOICE);

fun toMC() = mcValue

companion object {
@JvmStatic
fun fromMC(mcValue: SoundCategory) = values().first { it.mcValue == mcValue }
Expand All @@ -294,12 +293,10 @@ class Sound(private val config: NativeObject) {
}
}

enum class AttenuationType(private val mcValue: MCAttenuationType) {
enum class AttenuationType(override val mcValue: MCAttenuationType) : CTWrapper<MCAttenuationType> {
NONE(MCAttenuationType.NONE),
LINEAR(MCAttenuationType.LINEAR);

fun toMC() = mcValue

companion object {
@JvmStatic
fun fromMC(mcValue: MCAttenuationType) = values().first { it.mcValue == mcValue }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.chattriggers.ctjs.minecraft.wrappers

interface CTWrapper<MCClass> {
val mcValue: MCClass

fun toMC(): MCClass = mcValue
}
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ abstract class Client {
*/
@JvmStatic
fun close() {
Player.getPlayer()?.closeScreen()
Player.toMC()?.closeScreen()
}
}

Expand Down
77 changes: 40 additions & 37 deletions src/main/kotlin/com/chattriggers/ctjs/minecraft/wrappers/Player.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,41 @@ import gg.essential.universal.wrappers.message.UTextComponent
import net.minecraft.client.network.ClientPlayerEntity
import java.util.*

object Player {
object Player : CTWrapper<ClientPlayerEntity?> {
override val mcValue get() = UMinecraft.getMinecraft().player

/**
* Gets Minecraft's EntityPlayerSP object representing the user
*
* @return The Minecraft EntityPlayerSP object representing the user
*/
@Deprecated("Use toMC", ReplaceWith("toMC()"))
@JvmStatic
fun getPlayer(): ClientPlayerEntity? = UMinecraft.getMinecraft().player
fun getPlayer() = toMC()

@JvmStatic
fun getTeam(): Team? = Scoreboard.getScoreboard()?.getPlayerTeam(getName())?.let(::Team)
fun getTeam(): Team? = Scoreboard.toMC()?.getPlayerTeam(getName())?.let(::Team)

@JvmStatic
fun asPlayerMP(): PlayerMP? = getPlayer()?.let(::PlayerMP)
fun asPlayerMP(): PlayerMP? = toMC()?.let(::PlayerMP)

@JvmStatic
fun getX(): Double = getPlayer()?.x ?: 0.0
fun getX(): Double = toMC()?.x ?: 0.0

@JvmStatic
fun getY(): Double = getPlayer()?.y ?: 0.0
fun getY(): Double = toMC()?.y ?: 0.0

@JvmStatic
fun getZ(): Double = getPlayer()?.z ?: 0.0
fun getZ(): Double = toMC()?.z ?: 0.0

@JvmStatic
fun getLastX(): Double = getPlayer()?.lastRenderX ?: 0.0
fun getLastX(): Double = toMC()?.lastRenderX ?: 0.0

@JvmStatic
fun getLastY(): Double = getPlayer()?.lastRenderY ?: 0.0
fun getLastY(): Double = toMC()?.lastRenderY ?: 0.0

@JvmStatic
fun getLastZ(): Double = getPlayer()?.lastRenderZ ?: 0.0
fun getLastZ(): Double = toMC()?.lastRenderZ ?: 0.0

@JvmStatic
fun getRenderX(): Double = getLastX() + (getX() - getLastX()) * Renderer.partialTicks
Expand All @@ -61,7 +64,7 @@ object Player {
* @return the player's x motion
*/
@JvmStatic
fun getMotionX(): Double = getPlayer()?.velocity?.x ?: 0.0
fun getMotionX(): Double = toMC()?.velocity?.x ?: 0.0

/**
* Gets the player's y motion.
Expand All @@ -70,7 +73,7 @@ object Player {
* @return the player's y motion
*/
@JvmStatic
fun getMotionY(): Double = getPlayer()?.velocity?.y ?: 0.0
fun getMotionY(): Double = toMC()?.velocity?.y ?: 0.0

/**
* Gets the player's z motion.
Expand All @@ -79,23 +82,23 @@ object Player {
* @return the player's z motion
*/
@JvmStatic
fun getMotionZ(): Double = getPlayer()?.velocity?.z ?: 0.0
fun getMotionZ(): Double = toMC()?.velocity?.z ?: 0.0

/**
* Gets the player's camera pitch.
*
* @return the player's camera pitch
*/
@JvmStatic
fun getPitch(): Double = UMath.wrapAngleTo180(getPlayer()?.pitch?.toDouble() ?: 0.0)
fun getPitch(): Double = UMath.wrapAngleTo180(toMC()?.pitch?.toDouble() ?: 0.0)

/**
* Gets the player's camera yaw.
*
* @return the player's camera yaw
*/
@JvmStatic
fun getYaw(): Double = UMath.wrapAngleTo180(getPlayer()?.yaw?.toDouble() ?: 0.0)
fun getYaw(): Double = UMath.wrapAngleTo180(toMC()?.yaw?.toDouble() ?: 0.0)

// TODO(breaking): Remove getRawYaw (completely useless method)

Expand All @@ -118,16 +121,16 @@ object Player {
fun getUUID(): UUID = UMinecraft.getMinecraft().session.profile.id

@JvmStatic
fun getHP(): Float = getPlayer()?.health ?: 0f
fun getHP(): Float = toMC()?.health ?: 0f

@JvmStatic
fun getHunger(): Int = getPlayer()?.hungerManager?.foodLevel ?: 0
fun getHunger(): Int = toMC()?.hungerManager?.foodLevel ?: 0

@JvmStatic
fun getSaturation(): Float = getPlayer()?.hungerManager?.saturationLevel ?: 0f
fun getSaturation(): Float = toMC()?.hungerManager?.saturationLevel ?: 0f

@JvmStatic
fun getArmorPoints(): Int = getPlayer()?.armor ?: 0
fun getArmorPoints(): Int = toMC()?.armor ?: 0

/**
* Gets the player's air level.
Expand All @@ -139,18 +142,18 @@ object Player {
* @return the player's air level
*/
@JvmStatic
fun getAirLevel(): Int = getPlayer()?.air ?: 0
fun getAirLevel(): Int = toMC()?.air ?: 0

@JvmStatic
fun getXPLevel(): Int = getPlayer()?.experienceLevel ?: 0
fun getXPLevel(): Int = toMC()?.experienceLevel ?: 0

@JvmStatic
fun getXPProgress(): Float = getPlayer()?.experienceProgress ?: 0f
fun getXPProgress(): Float = toMC()?.experienceProgress ?: 0f

@JvmStatic
fun getBiome(): String {
val pos = getPlayer()?.blockPos ?: return ""
val biomeEntry = World.getWorld()?.getBiome(pos) ?: return ""
val pos = toMC()?.blockPos ?: return ""
val biomeEntry = World.toMC()?.getBiome(pos) ?: return ""

return biomeEntry.key.get().value.path
}
Expand All @@ -161,27 +164,27 @@ object Player {
* @return the light level at the player's current position
*/
@JvmStatic
fun getLightLevel(): Int = World.getWorld()?.getLightLevel(getPlayer()?.blockPos) ?: 0
fun getLightLevel(): Int = World.toMC()?.getLightLevel(toMC()?.blockPos) ?: 0

@JvmStatic
fun isMoving(): Boolean = getPlayer()?.movementSpeed?.let { it != 0f } ?: false
fun isMoving(): Boolean = toMC()?.movementSpeed?.let { it != 0f } ?: false

@JvmStatic
fun isSneaking(): Boolean = getPlayer()?.isSneaking ?: false
fun isSneaking(): Boolean = toMC()?.isSneaking ?: false

@JvmStatic
fun isSprinting(): Boolean = getPlayer()?.isSprinting ?: false
fun isSprinting(): Boolean = toMC()?.isSprinting ?: false

/**
* Checks if player can be pushed by water.
*
* @return true if the player is flying, false otherwise
*/
@JvmStatic
fun isFlying(): Boolean = getPlayer()?.abilities?.flying ?: false
fun isFlying(): Boolean = toMC()?.abilities?.flying ?: false

@JvmStatic
fun isSleeping(): Boolean = getPlayer()?.isSleeping ?: false
fun isSleeping(): Boolean = toMC()?.isSleeping ?: false

/**
* Gets the direction the player is facing.
Expand All @@ -191,7 +194,7 @@ object Player {
*/
@JvmStatic
fun facing(): String {
if (getPlayer() == null) return ""
if (toMC() == null) return ""

val yaw = getYaw()

Expand All @@ -215,7 +218,7 @@ object Player {
* @return a list of the active [PotionEffect]s
*/
@JvmStatic
fun getActivePotionEffects(): List<PotionEffect> = getPlayer()?.activeStatusEffects?.values?.map(::PotionEffect).orEmpty()
fun getActivePotionEffects(): List<PotionEffect> = toMC()?.activeStatusEffects?.values?.map(::PotionEffect).orEmpty()

/**
* Gets the current object that the player is looking at,
Expand All @@ -235,7 +238,7 @@ object Player {
* @return the current held [Item]
*/
@JvmStatic
fun getHeldItem(): Item? = getPlayer()?.inventory?.selectedSlot?.let {
fun getHeldItem(): Item? = toMC()?.inventory?.selectedSlot?.let {
getInventory()?.getStackInSlot(it)
}

Expand All @@ -246,7 +249,7 @@ object Player {
*/
@JvmStatic
fun setHeldItemIndex(index: Int) {
getPlayer()?.inventory?.selectedSlot = index
toMC()?.inventory?.selectedSlot = index
}

/**
Expand All @@ -255,15 +258,15 @@ object Player {
* @return the current index
*/
@JvmStatic
fun getHeldItemIndex(): Int = getPlayer()?.inventory?.selectedSlot ?: -1
fun getHeldItemIndex(): Int = toMC()?.inventory?.selectedSlot ?: -1

/**
* Gets the inventory of the player, i.e. the inventory accessed by 'e'.
*
* @return the player's inventory
*/
@JvmStatic
fun getInventory(): Inventory? = getPlayer()?.inventory?.let(::Inventory)
fun getInventory(): Inventory? = toMC()?.inventory?.let(::Inventory)

/**
* Gets the display name for the player,
Expand Down Expand Up @@ -305,7 +308,7 @@ object Player {
// * @return the currently opened container
// */
// @JvmStatic
// fun getContainer(): Inventory? = getPlayer()?.openContainer?.let(::Inventory)
// fun getContainer(): Inventory? = toMC()?.openContainer?.let(::Inventory)

// /**
// * Draws the player in the GUI
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ import gg.essential.universal.wrappers.message.UTextComponent
import net.minecraft.scoreboard.ScoreboardObjective
import net.minecraft.scoreboard.ScoreboardPlayerScore

object Scoreboard {
object Scoreboard : CTWrapper<MCScoreboard?> {
private var needsUpdate = true
private var scoreboardNames = mutableListOf<Score>()
private var scoreboardTitle = UTextComponent("")
private var shouldRender = true

override val mcValue get() = World.toMC()?.scoreboard

@Deprecated("Use toMC", ReplaceWith("toMC()"))
@JvmStatic
fun getScoreboard(): MCScoreboard? = World.getWorld()?.scoreboard
fun getScoreboard() = toMC()

@JvmStatic
fun getSidebar(): ScoreboardObjective? = getScoreboard()?.getObjectiveForSlot(1)
fun getSidebar(): ScoreboardObjective? = toMC()?.getObjectiveForSlot(1)

// TODO(breaking): Remove getScoreboardTitle

Expand Down Expand Up @@ -99,7 +102,7 @@ object Scoreboard {
*/
@JvmStatic
fun setLine(score: Int, line: String, override: Boolean) {
val scoreboard = getScoreboard() ?: return
val scoreboard = toMC() ?: return
val sidebarObjective = getSidebar() ?: return

val scores = scoreboard.getAllPlayerScores(sidebarObjective)
Expand Down Expand Up @@ -127,7 +130,7 @@ object Scoreboard {
scoreboardNames.clear()
scoreboardTitle = UTextComponent("")

val scoreboard = getScoreboard() ?: return
val scoreboard = toMC() ?: return
val sidebarObjective = getSidebar() ?: return
scoreboardTitle = UTextComponent(sidebarObjective.displayName)

Expand All @@ -139,23 +142,23 @@ object Scoreboard {
needsUpdate = true
}

class Score(val score: ScoreboardPlayerScore) {
class Score(override val mcValue: ScoreboardPlayerScore) : CTWrapper<ScoreboardPlayerScore> {
/**
* Gets the score point value for this score,
* i.e. the number on the right of the board
*
* @return the actual point value
*/
fun getPoints(): Int = score.score
fun getPoints(): Int = mcValue.score

/**
* Gets the display string of this score
*
* @return the display name
*/
fun getName() = UTextComponent(MCTeam.decorateName(
getScoreboard()!!.getTeam(score.playerName),
UTextComponent(score.playerName),
Scoreboard.toMC()!!.getTeam(mcValue.playerName),
UTextComponent(mcValue.playerName),
)).formattedText

override fun toString(): String = getName()
Expand Down
Loading

0 comments on commit 90453a0

Please sign in to comment.