From ae82dfc9f65bc5e5171012833b3baca8a10f8c3b Mon Sep 17 00:00:00 2001 From: viral32111 <19510403+viral32111@users.noreply.github.com> Date: Thu, 8 Jun 2023 10:29:13 +0100 Subject: [PATCH] Fix many bugs, now ready for release --- gradle.properties | 2 +- .../mixin/GainExperienceMixin.java | 1 + .../progression/BlockPortalTravel.kt | 4 +- .../com/viral32111/progression/Progression.kt | 75 +++++++++++++------ .../progression/ProgressionState.kt | 18 +++-- .../progression/UpdateExperienceProgress.kt | 20 +---- .../config/ExperienceThresholds.kt | 4 +- 7 files changed, 75 insertions(+), 49 deletions(-) diff --git a/gradle.properties b/gradle.properties index 2120f85..0279a05 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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 diff --git a/src/main/java/com/viral32111/progression/mixin/GainExperienceMixin.java b/src/main/java/com/viral32111/progression/mixin/GainExperienceMixin.java index deb07db..35db228 100644 --- a/src/main/java/com/viral32111/progression/mixin/GainExperienceMixin.java +++ b/src/main/java/com/viral32111/progression/mixin/GainExperienceMixin.java @@ -16,4 +16,5 @@ public void gainExperience( int experience, CallbackInfo info ) { GainExperienceCallback.Companion.getEVENT().invoker().interact( player, experience ); } + } diff --git a/src/main/kotlin/com/viral32111/progression/BlockPortalTravel.kt b/src/main/kotlin/com/viral32111/progression/BlockPortalTravel.kt index 0bdb8ab..3a3f9e6 100644 --- a/src/main/kotlin/com/viral32111/progression/BlockPortalTravel.kt +++ b/src/main/kotlin/com/viral32111/progression/BlockPortalTravel.kt @@ -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 } diff --git a/src/main/kotlin/com/viral32111/progression/Progression.kt b/src/main/kotlin/com/viral32111/progression/Progression.kt index e9f1002..8ff3441 100644 --- a/src/main/kotlin/com/viral32111/progression/Progression.kt +++ b/src/main/kotlin/com/viral32111/progression/Progression.kt @@ -11,11 +11,18 @@ 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 @@ -23,7 +30,6 @@ 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 { @@ -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() { @@ -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 { @@ -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() { diff --git a/src/main/kotlin/com/viral32111/progression/ProgressionState.kt b/src/main/kotlin/com/viral32111/progression/ProgressionState.kt index 46c2d96..afd4462 100644 --- a/src/main/kotlin/com/viral32111/progression/ProgressionState.kt +++ b/src/main/kotlin/com/viral32111/progression/ProgressionState.kt @@ -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 { @@ -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." ) diff --git a/src/main/kotlin/com/viral32111/progression/UpdateExperienceProgress.kt b/src/main/kotlin/com/viral32111/progression/UpdateExperienceProgress.kt index ac147a4..92648d2 100644 --- a/src/main/kotlin/com/viral32111/progression/UpdateExperienceProgress.kt +++ b/src/main/kotlin/com/viral32111/progression/UpdateExperienceProgress.kt @@ -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 } diff --git a/src/main/kotlin/com/viral32111/progression/config/ExperienceThresholds.kt b/src/main/kotlin/com/viral32111/progression/config/ExperienceThresholds.kt index 47b0953..6e0c2b1 100644 --- a/src/main/kotlin/com/viral32111/progression/config/ExperienceThresholds.kt +++ b/src/main/kotlin/com/viral32111/progression/config/ExperienceThresholds.kt @@ -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 )