Skip to content

Commit

Permalink
Fix many bugs, now ready for release
Browse files Browse the repository at this point in the history
  • Loading branch information
viral32111 committed Jun 8, 2023
1 parent 422b215 commit ae82dfc
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 49 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ fabric_language_kotlin_version = 1.9.4+kotlin.1.8.21
events_version = 0.3.2

# Mod properties
mod_version = 0.1.0
mod_version = 1.0.0
maven_group = com.viral32111
archives_base_name = progression
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ public void gainExperience( int experience, CallbackInfo info ) {

GainExperienceCallback.Companion.getEVENT().invoker().interact( player, experience );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ fun blockPortalTravel( player: ServerPlayerEntity, destinationWorld: ServerWorld

if ( destinationDimension == World.NETHER && state.experienceCounter <= configuration.experienceThresholds.nether ) {
player.sendMessage( Text.of( configuration.chatMessages.nether ), false )
LOGGER.info( "Preventing player '${ player.displayName }' (${ player.uuidAsString }) from entering the nether as experience progress is only ${ state.experienceCounter } / ${ configuration.experienceThresholds.nether }." )
LOGGER.info( "Preventing player '${ player.displayName.string }' (${ player.uuidAsString }) from entering the nether as experience progress is only ${ state.experienceCounter } / ${ configuration.experienceThresholds.nether }." )
return ActionResult.FAIL
} else if ( destinationDimension == World.END && state.experienceCounter <= configuration.experienceThresholds.end ) {
player.sendMessage( Text.of( configuration.chatMessages.end ), false )
LOGGER.info( "Preventing player '${ player.displayName }' (${ player.uuidAsString }) from entering the end as experience progress is only ${ state.experienceCounter } / ${ configuration.experienceThresholds.end }." )
LOGGER.info( "Preventing player '${ player.displayName.string }' (${ player.uuidAsString }) from entering the end as experience progress is only ${ state.experienceCounter } / ${ configuration.experienceThresholds.end }." )
return ActionResult.FAIL
}

Expand Down
75 changes: 54 additions & 21 deletions src/main/kotlin/com/viral32111/progression/Progression.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,25 @@ import kotlinx.serialization.json.Json
import net.fabricmc.api.DedicatedServerModInitializer
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents
import net.fabricmc.fabric.api.event.player.UseBlockCallback
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents
import net.fabricmc.loader.api.FabricLoader
import net.minecraft.block.Block
import net.minecraft.block.EndPortalBlock
import net.minecraft.block.EndPortalFrameBlock
import net.minecraft.entity.boss.BossBar
import net.minecraft.entity.boss.ServerBossBar
import net.minecraft.item.Items
import net.minecraft.server.command.CommandManager.literal
import net.minecraft.text.Text
import net.minecraft.util.ActionResult
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.nio.file.StandardOpenOption
import kotlin.io.path.createDirectory
import kotlin.io.path.notExists
import kotlin.io.path.readText
import kotlin.io.path.writeText
import net.minecraft.server.command.CommandManager.*

@Suppress( "UNUSED" )
class Progression: DedicatedServerModInitializer {
Expand All @@ -45,13 +51,27 @@ class Progression: DedicatedServerModInitializer {
var state = ProgressionState()

val bossBar: ServerBossBar = ServerBossBar(
Text.of( configuration.bossBarTitles.nether ),
Text.of( configuration.bossBarTitles.nether ), // Assume Nether is not unlocked yet
BossBar.Color.GREEN,
BossBar.Style.PROGRESS
)

fun isNetherUnlocked() = state.experienceCounter >= configuration.experienceThresholds.nether
fun isEndUnlocked() = state.experienceCounter >= configuration.experienceThresholds.end
private fun isNetherUnlocked() = state.experienceCounter >= configuration.experienceThresholds.nether
private fun isEndUnlocked() = state.experienceCounter >= configuration.experienceThresholds.end

fun updateBossBar() {
if ( !isNetherUnlocked() ) {
bossBar.name = Text.of( configuration.bossBarTitles.nether )
bossBar.isVisible = state.experienceCounter < configuration.experienceThresholds.nether
bossBar.percent = state.experienceCounter.toFloat() / configuration.experienceThresholds.nether
} else if ( isNetherUnlocked() && !isEndUnlocked() ) {
bossBar.name = Text.of( configuration.bossBarTitles.end )
bossBar.isVisible = state.experienceCounter < configuration.experienceThresholds.end
bossBar.percent = state.experienceCounter.toFloat() / configuration.experienceThresholds.end
} else {
bossBar.isVisible = false
}
}
}

override fun onInitializeServer() {
Expand All @@ -60,23 +80,7 @@ class Progression: DedicatedServerModInitializer {
configuration = loadConfigurationFile()

registerCallbackListeners()

// https://www.fabricmc.net/wiki/tutorial:event_index
ServerLifecycleEvents.SERVER_STARTING.register { server ->
state = ProgressionState.getProgressionState( server )
LOGGER.info( "Acquired persistent state from server (experience counter: ${ state.experienceCounter }, players hiding progress bar: ${ state.playersHidingProgressBar.size })." )
}

registerCommands()

ServerPlayConnectionEvents.JOIN.register { handler, _, server ->
//val state = ProgressionState.getProgressionState( server )
val player = handler.player

if ( !state.playersHidingProgressBar.contains( player.uuid ) ) {
bossBar.addPlayer( player )
}
}
}

private fun loadConfigurationFile(): Configuration {
Expand Down Expand Up @@ -118,7 +122,36 @@ class Progression: DedicatedServerModInitializer {
EnterPortalCallback.EVENT.register( ::blockPortalTravel )
GainExperienceCallback.EVENT.register( ::updateExperienceProgress )

LOGGER.info( "Registered callback listeners for mixins." );
// https://www.fabricmc.net/wiki/tutorial:event_index
ServerLifecycleEvents.SERVER_STARTED.register { server ->
state = ProgressionState.getProgressionState( server ) ?: throw RuntimeException( "Persistent state manager for Overworld is null" )
LOGGER.info( "Acquired persistent state from server (experience counter: ${ state.experienceCounter }, players hiding progress bar: ${ state.playersHidingProgressBar.size })." )

updateBossBar()
}

ServerPlayConnectionEvents.JOIN.register { handler, _, _ ->
//val state = ProgressionState.getProgressionState( server )
val player = handler.player

if ( !state.playersHidingProgressBar.contains( player.uuid ) ) {
bossBar.addPlayer( player )
}
}

UseBlockCallback.EVENT.register { player, world, hand, hitResult ->
val blockState = world.getBlockState( hitResult.blockPos )

if ( !isEndUnlocked() && blockState.block is EndPortalFrameBlock && player.getStackInHand( hand ).item == Items.ENDER_EYE ) {
player.sendMessage( Text.of( configuration.chatMessages.end ), false )
LOGGER.info( "Preventing player '${ player.displayName.string }' (${ player.uuidAsString }) from using end portal frame as experience progress is only ${ state.experienceCounter } / ${ configuration.experienceThresholds.end }." )
return@register ActionResult.FAIL
}

return@register ActionResult.PASS
}

LOGGER.info( "Registered callback listeners." );
}

private fun registerCommands() {
Expand Down
18 changes: 12 additions & 6 deletions src/main/kotlin/com/viral32111/progression/ProgressionState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ class ProgressionState : PersistentState() {
private const val KEY_EXPERIENCE_COUNTER = "experienceCounter"
private const val KEY_PLAYERS_HIDING_BOSS_BAR = "playersHidingProgressBar"

fun getProgressionState( server: MinecraftServer ): ProgressionState {
val persistentStateManager = server.overworld.persistentStateManager
return persistentStateManager.getOrCreate( ::createFromNbt, { ProgressionState() }, MOD_ID )
fun getProgressionState( server: MinecraftServer ): ProgressionState? {
val persistentStateManager = server.overworld?.persistentStateManager
return persistentStateManager?.getOrCreate( ::createFromNbt, { ProgressionState() }, MOD_ID )
}

private fun createFromNbt( nbt: NbtCompound ): ProgressionState {
Expand All @@ -28,9 +28,15 @@ class ProgressionState : PersistentState() {
progressionState.experienceCounter = nbt.getInt( KEY_EXPERIENCE_COUNTER )

val playersHidingProgressBarList = nbt.getList( KEY_PLAYERS_HIDING_BOSS_BAR, NbtElement.STRING_TYPE.toInt() )
for ( index: Int in 0 .. playersHidingProgressBarList.size ) {
val playerUUID = UUID.fromString( playersHidingProgressBarList.getString( index ) )
progressionState.playersHidingProgressBar.add( playerUUID )
for ( index: Int in 0 until playersHidingProgressBarList.size ) {
val uuidAsString = playersHidingProgressBarList.getString( index )
if ( uuidAsString.isEmpty() ) {
LOGGER.warn( "Skipping empty player UUID '${ uuidAsString }' from players hiding progress bar list!" )
} else {
val playerUUID = UUID.fromString( playersHidingProgressBarList.getString( index ) )
progressionState.playersHidingProgressBar.add( playerUUID )
LOGGER.info( "Progress bar will be hidden for player with UUID '${ playerUUID }'." )
}
}

LOGGER.info( "Created persistent state." )
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@
package com.viral32111.progression

import com.viral32111.progression.Progression.Companion.LOGGER
import com.viral32111.progression.Progression.Companion.bossBar
import com.viral32111.progression.Progression.Companion.configuration
import com.viral32111.progression.Progression.Companion.isEndUnlocked
import com.viral32111.progression.Progression.Companion.isNetherUnlocked
import com.viral32111.progression.Progression.Companion.state
import com.viral32111.progression.Progression.Companion.updateBossBar
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.text.Text
import net.minecraft.util.ActionResult

fun updateExperienceProgress( player: PlayerEntity, experience: Int ): ActionResult {
state.experienceCounter += experience
state.markDirty()

LOGGER.info( "Incremented global experience counter to ${ state.experienceCounter } due to player '${ player.displayName }' (${ player.uuidAsString }) gaining $experience experience." )
LOGGER.info( "Incremented global experience counter to ${ state.experienceCounter } due to player '${ player.displayName.string }' (${ player.uuidAsString }) gaining $experience experience." )

if ( !isNetherUnlocked() ) {
bossBar.name = Text.of( configuration.bossBarTitles.nether )
bossBar.isVisible = state.experienceCounter < configuration.experienceThresholds.nether
bossBar.percent = state.experienceCounter.toFloat() / configuration.experienceThresholds.nether
} else if ( isNetherUnlocked() && !isEndUnlocked() ) {
bossBar.name = Text.of( configuration.bossBarTitles.end )
bossBar.isVisible = state.experienceCounter < configuration.experienceThresholds.end
bossBar.percent = state.experienceCounter.toFloat() / configuration.experienceThresholds.end
} else {
bossBar.isVisible = false
}
updateBossBar()

return ActionResult.PASS
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import kotlinx.serialization.Serializable

@Serializable
data class ExperienceThresholds(
@Required @SerialName( "nether" ) val nether: Int = 10000,
@Required @SerialName( "end" ) val end: Int = 20000
@Required @SerialName( "nether" ) val nether: Int = 25000,
@Required @SerialName( "end" ) val end: Int = 75000
)

0 comments on commit ae82dfc

Please sign in to comment.