Skip to content

Commit

Permalink
Merge pull request #7 from timinc-cobble/tmetcalfe89/findsparkle-cmd
Browse files Browse the repository at this point in the history
added findsparkle command
  • Loading branch information
tmetcalfe89 authored Aug 16, 2024
2 parents e5b5d42 + 8be2b20 commit 24849a6
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import com.cobblemon.mod.common.api.Priority
import com.cobblemon.mod.common.api.events.CobblemonEvents
import com.cobblemon.mod.common.api.storage.player.PlayerDataExtensionRegistry
import net.fabricmc.api.ModInitializer
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback
import net.minecraft.util.Identifier
import us.timinc.mc.cobblemon.shinysparkles.blocks.ShinySparklesBlocks
import us.timinc.mc.cobblemon.shinysparkles.blocks.blockentities.BlockEntities
import us.timinc.mc.cobblemon.shinysparkles.commands.Commands
import us.timinc.mc.cobblemon.shinysparkles.config.ConfigBuilder
import us.timinc.mc.cobblemon.shinysparkles.config.ShinySparklesConfig
import us.timinc.mc.cobblemon.shinysparkles.spawning.ShinySparkleSoulStealer
Expand All @@ -21,6 +23,7 @@ object ShinySparkles : ModInitializer {
PlayerDataExtensionRegistry.register(SparklesData.NAME, SparklesData::class.java)
BlockEntities.register()
ShinySparklesBlocks.register()
CommandRegistrationCallback.EVENT.register(Commands::register)
CobblemonEvents.POKEMON_ENTITY_SPAWN.subscribe(Priority.LOWEST) { ShinySparkleSoulStealer.possiblyStealSoul(it) }
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package us.timinc.mc.cobblemon.shinysparkles.commands

import com.mojang.brigadier.CommandDispatcher
import net.minecraft.server.command.ServerCommandSource

abstract class CommandExecutor {
abstract fun register(dispatcher: CommandDispatcher<ServerCommandSource>)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package us.timinc.mc.cobblemon.shinysparkles.commands

import com.mojang.brigadier.CommandDispatcher
import net.minecraft.command.CommandRegistryAccess
import net.minecraft.server.command.CommandManager
import net.minecraft.server.command.ServerCommandSource

object Commands {
fun register(
dispatcher: CommandDispatcher<ServerCommandSource>,
registry: CommandRegistryAccess,
registrationEnvironment: CommandManager.RegistrationEnvironment
) {
FindSparkle.register(dispatcher)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package us.timinc.mc.cobblemon.shinysparkles.commands

import com.mojang.brigadier.context.CommandContext
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.server.command.ServerCommandSource
import net.minecraft.text.Text
import us.timinc.mc.cobblemon.shinysparkles.ShinySparkles
import us.timinc.mc.cobblemon.shinysparkles.store.player.SparklesData

object FindSparkle : PlayerCommandExecutor(listOf("findsparkle")) {
override fun check(ctx: CommandContext<ServerCommandSource>, player: PlayerEntity): Int {
if (!ShinySparkles.config.enableFindCommand) {
player.sendMessage(Text.translatable("shinysparkles.msg.commanddisabled"), true)
return 0
}

val prevSparklesData = SparklesData.getFromPlayer(player)
val pos = prevSparklesData.pos ?: run {
player.sendMessage(Text.translatable("shinysparkles.msg.nosparkleforyou"), true)
return 0
}
player.sendMessage(Text.literal("${pos.x}, ${pos.y}, ${pos.z}"), true)
return 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package us.timinc.mc.cobblemon.shinysparkles.commands

import com.mojang.brigadier.CommandDispatcher
import com.mojang.brigadier.context.CommandContext
import net.minecraft.command.argument.EntityArgumentType
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.server.command.CommandManager.argument
import net.minecraft.server.command.CommandManager.literal
import net.minecraft.server.command.ServerCommandSource
import us.timinc.mc.cobblemon.shinysparkles.ShinySparkles.MOD_ID

abstract class PlayerCommandExecutor(private val path: List<String>) : CommandExecutor() {
override fun register(dispatcher: CommandDispatcher<ServerCommandSource>) {
val reversedPath = path.reversed()
var lastLink = literal(reversedPath.first())
.then(argument("player", EntityArgumentType.player()).executes(::withPlayer))
.executes(::withoutPlayer)

for (name in reversedPath.drop(1)) {
val nextLink = literal(name)
nextLink.then(lastLink)
lastLink = nextLink
}

dispatcher.register(literal(MOD_ID).then(lastLink))
}

fun withPlayer(ctx: CommandContext<ServerCommandSource>): Int {
return check(
ctx, EntityArgumentType.getPlayer(ctx, "player")
)
}

fun withoutPlayer(ctx: CommandContext<ServerCommandSource>): Int {
return ctx.source.player?.let { player ->
check(
ctx, player
)
} ?: 0
}

abstract fun check(ctx: CommandContext<ServerCommandSource>, player: PlayerEntity): Int
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ class ShinySparklesConfig {
val searchAllowance: Int = 3
val cancelMultiples: Boolean = false
val debug: Boolean = false
val enableFindCommand: Boolean = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ import com.cobblemon.mod.common.entity.pokemon.PokemonEntity
import net.minecraft.block.Blocks
import net.minecraft.util.math.BlockPos
import net.minecraft.util.math.Direction
import us.timinc.mc.cobblemon.shinysparkles.ShinySparkles
import us.timinc.mc.cobblemon.shinysparkles.ShinySparkles.config
import us.timinc.mc.cobblemon.shinysparkles.ShinySparkles.debug
import us.timinc.mc.cobblemon.shinysparkles.blocks.ShinySparkle
import us.timinc.mc.cobblemon.shinysparkles.blocks.ShinySparklesBlocks
import us.timinc.mc.cobblemon.shinysparkles.blocks.blockentities.ShinySparkleBlockEntity
import us.timinc.mc.cobblemon.shinysparkles.store.player.SparklesData
Expand Down Expand Up @@ -42,8 +40,8 @@ object ShinySparkleSoulStealer {
val world = spawnEvent.ctx.world
val pos = spawnEvent.ctx.position

var targetPos : BlockPos.Mutable = pos.mutableCopy()
var searchAllowance : Int = config.searchAllowance
var targetPos: BlockPos.Mutable = pos.mutableCopy()
var searchAllowance: Int = config.searchAllowance
var currentOffset = 0
while (world.getBlockState(targetPos).block != Blocks.AIR && searchAllowance > 0) {
if (world.getBlockState(targetPos).block != Blocks.WATER) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/assets/shinysparkles/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"shinysparkles.block.sparkle.msg.notyours": "The sparkle calls out for %d.",
"shinysparkles.msg.emptyperson": "someone else"
"shinysparkles.msg.emptyperson": "someone else",
"shinysparkles.msg.commanddisabled": "This command is disabled.",
"shinysparkles.msg.nosparkleforyou": "You do not currently have a shiny sparkle."
}

0 comments on commit 24849a6

Please sign in to comment.