Created
May 22, 2019 18:53
-
-
Save Amperthorpe/c3eb28300b0a6690b05c0e0684282847 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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