Skip to content

Instantly share code, notes, and snippets.

@Amperthorpe
Created May 22, 2019 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Amperthorpe/c3eb28300b0a6690b05c0e0684282847 to your computer and use it in GitHub Desktop.
Save Amperthorpe/c3eb28300b0a6690b05c0e0684282847 to your computer and use it in GitHub Desktop.
package com.alleluid.littleopener
import com.alleluid.littleopener.common.blocks.blockopener.TileOpener
import io.netty.buffer.ByteBuf
import net.minecraft.util.math.BlockPos
import net.minecraftforge.fml.common.network.simpleimpl.IMessage
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext
import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper
import net.minecraftforge.fml.relauncher.Side
object PacketHandler {
@JvmStatic
val INSTANCE = SimpleNetworkWrapper(MOD_ID)
@JvmStatic
var uid = 0
@JvmStatic
fun registerMessages() {
INSTANCE.registerMessage(SaveCoordsMessage.SaveCoordsMessageHandler::class.java, SaveCoordsMessage::class.java, uid++, Side.SERVER)
}
}
class SaveCoordsMessage(var toSendXTE: Int, var toSendYTE: Int, var toSendZTE: Int,
var toSendXLT: Int, var toSendYLT: Int, var toSendZLT: Int) : IMessage {
constructor(blockPosTE: BlockPos, blockPosLT: BlockPos) :
this(blockPosTE.x, blockPosTE.y, blockPosTE.z,
blockPosLT.x, blockPosLT.y, blockPosLT.z)
override fun fromBytes(buf: ByteBuf?) {
buf?.writeInt(toSendXTE)
buf?.writeInt(toSendYTE)
buf?.writeInt(toSendZTE)
buf?.writeInt(toSendXLT)
buf?.writeInt(toSendYLT)
buf?.writeInt(toSendZLT)
}
override fun toBytes(buf: ByteBuf?) {
toSendXTE = buf?.readInt() ?: Int.MIN_VALUE
toSendYTE = buf?.readInt() ?: Int.MIN_VALUE
toSendZTE = buf?.readInt() ?: Int.MIN_VALUE
toSendXLT = buf?.readInt() ?: Int.MIN_VALUE
toSendYLT = buf?.readInt() ?: Int.MIN_VALUE
toSendZLT = buf?.readInt() ?: Int.MIN_VALUE
}
class SaveCoordsMessageHandler : IMessageHandler<SaveCoordsMessage, IMessage> {
override fun onMessage(message: SaveCoordsMessage?, ctx: MessageContext?): IMessage? {
if (message != null && ctx != null) {
//Check for values as a result of nulls
if (listOf(message.toSendXTE, message.toSendYTE, message.toSendZTE,
message.toSendXLT, message.toSendYLT, message.toSendZLT)
.contains(Int.MIN_VALUE))
return null
val posTE = BlockPos(message.toSendXTE, message.toSendYTE, message.toSendZTE)
val posLT = BlockPos(message.toSendXLT, message.toSendYLT, message.toSendZLT)
println("posTE:$posTE | posLT:$posLT")
val serverWorld = ctx.serverHandler.player.serverWorld
serverWorld.addScheduledTask {
val opener = serverWorld.getTileEntity(posTE) as TileOpener
opener.targetPos = posLT
}
}
return null
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment