CobbleSync Network Handler
This commit is contained in:
@@ -1,15 +1,25 @@
|
||||
package co.sirblob
|
||||
|
||||
import com.cobblemon.mod.common.Cobblemon
|
||||
import net.fabricmc.api.ModInitializer
|
||||
import org.slf4j.LoggerFactory
|
||||
|
||||
import com.cobblemon.mod.common.NetworkManager
|
||||
|
||||
object CobbleSync : ModInitializer {
|
||||
|
||||
const val ID = "cobblesync"
|
||||
|
||||
private val logger = LoggerFactory.getLogger("cobblesync")
|
||||
|
||||
override fun onInitialize() {
|
||||
val networkManager: NetworkManager = Cobblemon.implementation.networkManager
|
||||
|
||||
override fun onInitialize() {
|
||||
// This code runs as soon as Minecraft is in a mod-load-ready state.
|
||||
// However, some things (like resources) may still be uninitialized.
|
||||
// Proceed with mild caution.
|
||||
logger.info("Hello Fabric world!")
|
||||
logger.info("Hello CobbleSync world!")
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -16,7 +16,7 @@ object CommandHandler {
|
||||
fun registerCommands() {
|
||||
CommandRegistrationCallback.EVENT.register { dispatcher, registryAccess, environment ->
|
||||
// Register your commands here
|
||||
MyCommand.register(dispatcher)
|
||||
// MyCommand.register(dispatcher)
|
||||
// Example: AnotherCommand.register(dispatcher)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,100 +0,0 @@
|
||||
package co.sirblob.commands
|
||||
|
||||
import com.mojang.brigadier.Command
|
||||
import com.mojang.brigadier.CommandDispatcher
|
||||
import com.mojang.brigadier.arguments.IntegerArgumentType
|
||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder
|
||||
import com.mojang.brigadier.builder.RequiredArgumentBuilder
|
||||
import com.mojang.brigadier.context.CommandContext
|
||||
import net.minecraft.server.commands.ServerCommandSource
|
||||
import net.minecraft.server.commands.CommandManager.* // Required for literal() and argument()
|
||||
import net.minecraft.text.Text
|
||||
|
||||
|
||||
// TEMPLATE CODE DOESN"T WORK BUT MEH
|
||||
|
||||
/**
|
||||
* A template for a simple command.
|
||||
*
|
||||
* This command can be invoked in-game using:
|
||||
* /mycommand
|
||||
* /mycommand <number>
|
||||
*/
|
||||
object SaveParty {
|
||||
|
||||
private const val COMMAND_NAME = "mycommand" // The base name of the command
|
||||
|
||||
/**
|
||||
* Registers the command and its subcommands/arguments.
|
||||
*
|
||||
* @param dispatcher The command dispatcher used to register commands.
|
||||
*/
|
||||
fun register(dispatcher: CommandDispatcher<>) {
|
||||
// Create the literal argument builder for the base command
|
||||
// A literal argument is a fixed string that must be typed by the user.
|
||||
val commandBuilder = literal<ServerCommandSource>(COMMAND_NAME)
|
||||
.executes(::runBaseCommand) // Action to perform if only /mycommand is typed
|
||||
.then(
|
||||
// Define an argument that follows the base command.
|
||||
// In this case, we expect an integer argument named "number".
|
||||
argument("number", IntegerArgumentType.integer())
|
||||
.executes(::runWithNumberArgument) // Action to perform if /mycommand <number> is typed
|
||||
// You can add more subcommands or arguments here
|
||||
// .then(literal<ServerCommandSource>("subcommand").executes(::runSubCommand))
|
||||
)
|
||||
|
||||
// Register the command
|
||||
dispatcher.register(commandBuilder)
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes when the base command is called (e.g., /mycommand).
|
||||
*
|
||||
* @param context The command context, providing information about the command's execution.
|
||||
* @return An integer indicating command success (typically 1 for success, 0 for failure).
|
||||
*/
|
||||
private fun runBaseCommand(context: CommandContext<ServerCommandSource>): Int {
|
||||
val source = context.source
|
||||
source.sendFeedback({ Text.literal("You ran the base MyCommand!") }, false) // Send feedback to the player
|
||||
// false means it won't be broadcast to other ops
|
||||
|
||||
// You can add more logic here, like interacting with the game world, player, etc.
|
||||
// For example, to get the player who executed the command:
|
||||
// val player = source.player
|
||||
// player?.sendMessage(Text.literal("Hello from the command!"), false)
|
||||
|
||||
return Command.SINGLE_SUCCESS // Indicates the command was successful
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes when the command is called with a number argument (e.g., /mycommand 123).
|
||||
*
|
||||
* @param context The command context, providing information about the command's execution.
|
||||
* @return An integer indicating command success.
|
||||
*/
|
||||
private fun runWithNumberArgument(context: CommandContext<ServerCommandSource>): Int {
|
||||
val source = context.source
|
||||
val number = IntegerArgumentType.getInteger(context, "number") // Get the value of the "number" argument
|
||||
|
||||
source.sendFeedback({ Text.literal("MyCommand received number: $number") }, false)
|
||||
|
||||
// Add logic that uses the number argument
|
||||
if (number > 100) {
|
||||
source.sendFeedback({ Text.literal("That's a big number!") }, false)
|
||||
} else {
|
||||
source.sendFeedback({ Text.literal("That's a reasonable number.") }, false)
|
||||
}
|
||||
|
||||
return Command.SINGLE_SUCCESS
|
||||
}
|
||||
|
||||
/*
|
||||
// Example of another subcommand function
|
||||
private fun runSubCommand(context: CommandContext<ServerCommandSource>): Int {
|
||||
val source = context.source
|
||||
source.sendFeedback({ Text.literal("You ran the 'subcommand' of MyCommand!") }, false)
|
||||
return Command.SINGLE_SUCCESS
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
26
src/main/kotlin/co/sirblob/network/CobbleSyncNetwork.kt
Normal file
26
src/main/kotlin/co/sirblob/network/CobbleSyncNetwork.kt
Normal file
@@ -0,0 +1,26 @@
|
||||
package co.sirblob.network
|
||||
|
||||
import co.sirblob.network.handler.OpenPCHandler
|
||||
import co.sirblob.network.packet.OpenPCPacket
|
||||
import com.cobblemon.mod.common.net.PacketRegisterInfo
|
||||
|
||||
object CobbleSyncNetwork {
|
||||
|
||||
val s2cPayloads = generateS2CPacketInfoList()
|
||||
val c2sPayloads = generateC2SPacketInfoList()
|
||||
|
||||
private fun generateS2CPacketInfoList(): List<PacketRegisterInfo<*>> {
|
||||
val list = mutableListOf<PacketRegisterInfo<*>>()
|
||||
|
||||
return list
|
||||
}
|
||||
|
||||
private fun generateC2SPacketInfoList(): List<PacketRegisterInfo<*>> {
|
||||
val list = mutableListOf<PacketRegisterInfo<*>>()
|
||||
|
||||
list.add(PacketRegisterInfo(OpenPCPacket.ID, OpenPCPacket::decode, OpenPCHandler))
|
||||
|
||||
return list
|
||||
}
|
||||
|
||||
}
|
||||
18
src/main/kotlin/co/sirblob/network/handler/OpenPCHandler.kt
Normal file
18
src/main/kotlin/co/sirblob/network/handler/OpenPCHandler.kt
Normal file
@@ -0,0 +1,18 @@
|
||||
package co.sirblob.network.handler
|
||||
|
||||
import co.sirblob.network.packet.OpenPCPacket
|
||||
import com.cobblemon.mod.common.api.net.ServerNetworkPacketHandler
|
||||
import com.cobblemon.mod.common.util.pc
|
||||
import net.minecraft.server.MinecraftServer
|
||||
import net.minecraft.server.level.ServerPlayer
|
||||
|
||||
object OpenPCHandler : ServerNetworkPacketHandler<OpenPCPacket> {
|
||||
override fun handle(packet: OpenPCPacket, server: MinecraftServer, player: ServerPlayer) {
|
||||
server.execute {
|
||||
val pc = player.pc()
|
||||
com.cobblemon.mod.common.net.messages.client.storage.pc.OpenPCPacket(pc.uuid, 0).sendToPlayer(player)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package co.sirblob.network.packet
|
||||
|
||||
import co.sirblob.CobbleSync
|
||||
import com.cobblemon.mod.common.api.net.NetworkPacket
|
||||
import net.minecraft.server.level.ServerPlayer
|
||||
|
||||
interface CobbleSyncNetworkPacket<T : NetworkPacket<T>> : NetworkPacket<T> {
|
||||
|
||||
override fun sendToServer() {
|
||||
CobbleSync.networkManager.sendToServer(this)
|
||||
}
|
||||
|
||||
override fun sendToPlayer(player: ServerPlayer) {
|
||||
CobbleSync.networkManager.sendPacketToPlayer(player, this)
|
||||
}
|
||||
|
||||
}
|
||||
20
src/main/kotlin/co/sirblob/network/packet/OpenPCPacket.kt
Normal file
20
src/main/kotlin/co/sirblob/network/packet/OpenPCPacket.kt
Normal file
@@ -0,0 +1,20 @@
|
||||
package co.sirblob.network.packet
|
||||
|
||||
import co.sirblob.CobbleSync
|
||||
import net.minecraft.network.RegistryFriendlyByteBuf
|
||||
import net.minecraft.resources.ResourceLocation
|
||||
|
||||
class OpenPCPacket: CobbleSyncNetworkPacket<OpenPCPacket> {
|
||||
|
||||
companion object {
|
||||
val ID = ResourceLocation.fromNamespaceAndPath("open_pc", CobbleSync.ID)
|
||||
fun decode(buffer: RegistryFriendlyByteBuf): OpenPCPacket = OpenPCPacket()
|
||||
}
|
||||
|
||||
override val id = ID
|
||||
|
||||
override fun encode(buffer: RegistryFriendlyByteBuf) {
|
||||
// TO SOMETHING
|
||||
}
|
||||
|
||||
}
|
||||
3
src/main/resources/assets/cobblesync/lang/en_us.json
Normal file
3
src/main/resources/assets/cobblesync/lang/en_us.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"key.cobblesync.open_pc.desc": "Open PC Storage"
|
||||
}
|
||||
@@ -5,7 +5,7 @@
|
||||
"name": "CobbleSync",
|
||||
"description": "This is an example description! Tell everyone what your mod is about!",
|
||||
"authors": [
|
||||
"Me!"
|
||||
"Sir_Blob_"
|
||||
],
|
||||
"contact": {
|
||||
"homepage": "https://fabricmc.net/",
|
||||
|
||||
Reference in New Issue
Block a user