mirror of
https://github.com/GTNewHorizons/bdlib.git
synced 2026-11-13 00:37:59 +00:00
Added experimental network stuff
This commit is contained in:
1 parent
4754c209ae
commit
005c300d8f
5 files changed
+168
-4
No files matched your search
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2014
|
||||
* https://github.com/bdew/teleporter
|
||||
*
|
||||
* This mod is distributed under the terms of the Minecraft Mod Public
|
||||
* License 1.0, or MMPL. Please check the contents of the license located in
|
||||
* https://raw.github.com/bdew/teleporter/master/MMPL-1.0.txt
|
||||
*/
|
||||
|
||||
package net.bdew.lib.network
|
||||
|
||||
import io.netty.handler.codec.MessageToMessageCodec
|
||||
import cpw.mods.fml.common.network.internal.FMLProxyPacket
|
||||
import net.minecraft.nbt.{CompressedStreamTools, NBTTagCompound}
|
||||
import io.netty.channel.ChannelHandlerContext
|
||||
import java.util
|
||||
import java.io.{DataInputStream, DataOutputStream}
|
||||
import io.netty.buffer.{ByteBufInputStream, ByteBufOutputStream, Unpooled}
|
||||
import cpw.mods.fml.common.network.NetworkRegistry
|
||||
import io.netty.channel.ChannelHandler.Sharable
|
||||
import net.bdew.lib.BdLib
|
||||
|
||||
@Sharable
|
||||
class NBTMessageToMessageCodec extends MessageToMessageCodec[FMLProxyPacket, NBTTagCompound] {
|
||||
def encode(ctx: ChannelHandlerContext, msg: NBTTagCompound, out: util.List[AnyRef]) {
|
||||
val buff = Unpooled.buffer()
|
||||
val writer = new DataOutputStream(new ByteBufOutputStream(buff))
|
||||
CompressedStreamTools.write(msg, writer)
|
||||
val pkt = new FMLProxyPacket(buff, ctx.channel.attr(NetworkRegistry.FML_CHANNEL).get)
|
||||
out.add(pkt)
|
||||
}
|
||||
def decode(ctx: ChannelHandlerContext, msg: FMLProxyPacket, out: util.List[AnyRef]) {
|
||||
try {
|
||||
val stream = new DataInputStream(new ByteBufInputStream(msg.payload()))
|
||||
out.add(CompressedStreamTools.read(stream))
|
||||
} catch {
|
||||
case e: Throwable => BdLib.log.error("Error decoding packet", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2014
|
||||
* https://github.com/bdew/teleporter
|
||||
*
|
||||
* This mod is distributed under the terms of the Minecraft Mod Public
|
||||
* License 1.0, or MMPL. Please check the contents of the license located in
|
||||
* https://raw.github.com/bdew/teleporter/master/MMPL-1.0.txt
|
||||
*/
|
||||
|
||||
package net.bdew.lib.network
|
||||
|
||||
import cpw.mods.fml.common.network.{FMLOutboundHandler, NetworkRegistry}
|
||||
import cpw.mods.fml.relauncher.Side
|
||||
import net.minecraft.nbt.NBTTagCompound
|
||||
import net.minecraft.entity.player.EntityPlayerMP
|
||||
import io.netty.channel.{ChannelHandler, ChannelFutureListener}
|
||||
|
||||
class NetChannel(val name: String) {
|
||||
val channels = NetworkRegistry.INSTANCE.newChannel(name, new NBTMessageToMessageCodec)
|
||||
|
||||
var serverHandlers = Map.empty[String, (NBTTagCompound, EntityPlayerMP) => Unit]
|
||||
var clientHandlers = Map.empty[String, (NBTTagCompound) => Unit]
|
||||
|
||||
def regServerHandler(id: String, callable: (NBTTagCompound, EntityPlayerMP) => Unit) =
|
||||
if (serverHandlers.contains(id))
|
||||
sys.error("Command name already in use: %s".format(name))
|
||||
else
|
||||
serverHandlers += (id -> callable)
|
||||
|
||||
def regClientHandler(id: String, callable: (NBTTagCompound) => Unit) =
|
||||
if (clientHandlers.contains(id))
|
||||
sys.error("Command name already in use: %s".format(name))
|
||||
else
|
||||
clientHandlers += (id -> callable)
|
||||
|
||||
addHandler(Side.SERVER, new NetChannelServerHandler(this))
|
||||
addHandler(Side.CLIENT, new NetChannelClientHandler(this))
|
||||
|
||||
def addHandler(side: Side, handler: ChannelHandler) {
|
||||
val ch = channels.get(side)
|
||||
val name = ch.findChannelHandlerNameForType(classOf[NBTMessageToMessageCodec])
|
||||
ch.pipeline().addAfter(name, side + "Handler", handler)
|
||||
}
|
||||
|
||||
def sendToAll(message: NBTTagCompound) {
|
||||
channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.ALL)
|
||||
channels.get(Side.SERVER).writeAndFlush(message).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE)
|
||||
}
|
||||
|
||||
def sendTo(message: NBTTagCompound, player: EntityPlayerMP) {
|
||||
channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.PLAYER)
|
||||
channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(player)
|
||||
channels.get(Side.SERVER).writeAndFlush(message).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE)
|
||||
}
|
||||
|
||||
def sendToAllAround(message: NBTTagCompound, point: NetworkRegistry.TargetPoint) {
|
||||
channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.ALLAROUNDPOINT)
|
||||
channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(point)
|
||||
channels.get(Side.SERVER).writeAndFlush(message).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE)
|
||||
}
|
||||
|
||||
def sendToDimension(message: NBTTagCompound, dimensionId: Int) {
|
||||
channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.DIMENSION)
|
||||
channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(new Integer(dimensionId))
|
||||
channels.get(Side.SERVER).writeAndFlush(message).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE)
|
||||
}
|
||||
|
||||
def sendToServer(message: NBTTagCompound) {
|
||||
channels.get(Side.CLIENT).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.TOSERVER)
|
||||
channels.get(Side.CLIENT).writeAndFlush(message).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2014
|
||||
* https://github.com/bdew/teleporter
|
||||
*
|
||||
* This mod is distributed under the terms of the Minecraft Mod Public
|
||||
* License 1.0, or MMPL. Please check the contents of the license located in
|
||||
* https://raw.github.com/bdew/teleporter/master/MMPL-1.0.txt
|
||||
*/
|
||||
|
||||
package net.bdew.lib.network
|
||||
|
||||
import io.netty.channel.{ChannelHandlerContext, SimpleChannelInboundHandler}
|
||||
import net.minecraft.nbt.NBTTagCompound
|
||||
import net.bdew.lib.BdLib
|
||||
|
||||
class NetChannelClientHandler(ch: NetChannel) extends SimpleChannelInboundHandler[NBTTagCompound] {
|
||||
def channelRead0(ctx: ChannelHandlerContext, msg: NBTTagCompound) {
|
||||
val cmd = msg.getString("_cmd")
|
||||
ch.clientHandlers.getOrElse(cmd, {
|
||||
BdLib.log.error("Unknown command: '%s' from server".format(cmd))
|
||||
return
|
||||
})(msg)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2014
|
||||
* https://github.com/bdew/teleporter
|
||||
*
|
||||
* This mod is distributed under the terms of the Minecraft Mod Public
|
||||
* License 1.0, or MMPL. Please check the contents of the license located in
|
||||
* https://raw.github.com/bdew/teleporter/master/MMPL-1.0.txt
|
||||
*/
|
||||
|
||||
package net.bdew.lib.network
|
||||
|
||||
import io.netty.channel.{ChannelHandlerContext, SimpleChannelInboundHandler}
|
||||
import net.minecraft.nbt.NBTTagCompound
|
||||
import cpw.mods.fml.common.network.NetworkRegistry
|
||||
import net.minecraft.network.NetHandlerPlayServer
|
||||
import net.bdew.lib.BdLib
|
||||
|
||||
class NetChannelServerHandler(ch: NetChannel) extends SimpleChannelInboundHandler[NBTTagCompound] {
|
||||
def channelRead0(ctx: ChannelHandlerContext, msg: NBTTagCompound) {
|
||||
val cmd = msg.getString("_cmd")
|
||||
val nh = ctx.channel().attr(NetworkRegistry.NET_HANDLER).get.asInstanceOf[NetHandlerPlayServer].playerEntity
|
||||
ch.serverHandlers.getOrElse(cmd, {
|
||||
BdLib.log.error("Unknown command: '%s' from '%s'".format(cmd, nh.getCommandSenderName))
|
||||
return
|
||||
})(msg, nh)
|
||||
}
|
||||
}
|
||||
@@ -159,7 +159,7 @@ class RecipeLoader {
|
||||
case _ => Seq(getConcreteStack(s))
|
||||
}
|
||||
|
||||
def notNull[T](v:T, err: => String) = if (v == null) error(err) else v
|
||||
def notNull[T](v: T, err: => String) = if (v == null) error(err) else v
|
||||
|
||||
/**
|
||||
* Returns an ItemStack that match a reference
|
||||
@@ -178,11 +178,11 @@ class RecipeLoader {
|
||||
return s
|
||||
case StackMacro(ch) => getConcreteStack(currCharMap(ch), cnt)
|
||||
case StackGeneric(mod, id) =>
|
||||
notNull(GameRegistry.findItemStack(mod, id, 1),"Stack not found %s:%s".format(mod, id))
|
||||
notNull(GameRegistry.findItemStack(mod, id, 1), "Stack not found %s:%s".format(mod, id))
|
||||
case StackBlock(mod, id, meta) =>
|
||||
new ItemStack(notNull(GameRegistry.findBlock(mod, id),"Block not found %s:%s".format(mod, id)), 1, meta)
|
||||
new ItemStack(notNull(GameRegistry.findBlock(mod, id), "Block not found %s:%s".format(mod, id)), 1, meta)
|
||||
case StackItem(mod, id, meta) =>
|
||||
new ItemStack(notNull(GameRegistry.findItem(mod, id),"Item not found %s:%s".format(mod, id)), 1, meta)
|
||||
new ItemStack(notNull(GameRegistry.findItem(mod, id), "Item not found %s:%s".format(mod, id)), 1, meta)
|
||||
case StackReflect(cls, field, meta) => reflectStack(cls, field, meta, cnt)
|
||||
case StackGetter(cls, method, param, meta) => reflectStackGetter(cls, method, param, meta, cnt)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user