Compare commits

...
6 Commits
12 changed files with 150 additions and 66 deletions

No files matched your search

+1 -1
View File
@@ -21,7 +21,7 @@ buildscript {
apply plugin: 'scala'
apply plugin: 'forge'
version = "1.4.4"
version = "1.4.5"
if (project.hasProperty('buildnum')) {
project.version = project.version + '.' + project.buildnum
+18
View File
@@ -0,0 +1,18 @@
bdlib.multiblock.face.north=北方
bdlib.multiblock.face.south=南方
bdlib.multiblock.face.east=东方
bdlib.multiblock.face.west=西方
bdlib.multiblock.face.up=顶部
bdlib.multiblock.face.down=底部
bdlib.multiblock.disabled=停用
bdlib.multiblock.incomplete=这台多方块结构机器未搭建完成, 它至少需要安装一个:
bdlib.multiblock.notconnected=未连接有效机器
bdlib.multiblock.toomanyoutputs=连接的输出接口过多, 无法连接新的接口
bdlib.rsmode.always=一直启用
bdlib.rsmode.never=一直停用
bdlib.rsmode.rs_on=接收到红石信号时启用
bdlib.rsmode.rs_off=接收到红石信号时停用
bdlib.label.empty=空
+10 -1
View File
@@ -11,7 +11,7 @@ package net.bdew.lib
import cpw.mods.fml.common.Mod
import cpw.mods.fml.common.Mod.EventHandler
import cpw.mods.fml.common.event.{FMLPreInitializationEvent, FMLServerStartingEvent}
import cpw.mods.fml.common.event.{FMLPreInitializationEvent, FMLServerStartingEvent, FMLServerStoppingEvent}
import net.bdew.lib.multiblock.network.NetHandler
import net.minecraft.command.CommandHandler
import org.apache.logging.log4j.Logger
@@ -26,6 +26,9 @@ object BdLib {
def logWarnException(msg: String, t: Throwable, args: Any*) = log.warn(msg.format(args: _*), t)
def logErrorException(msg: String, t: Throwable, args: Any*) = log.error(msg.format(args: _*), t)
val onServerStarting = Event[FMLServerStartingEvent]
val onServerStopping = Event[FMLServerStoppingEvent]
@EventHandler
def preInit(ev: FMLPreInitializationEvent) {
log = ev.getModLog
@@ -37,5 +40,11 @@ object BdLib {
def serverStarting(event: FMLServerStartingEvent) {
val commandHandler = event.getServer.getCommandManager.asInstanceOf[CommandHandler]
commandHandler.registerCommand(new CommandDumpRegistry)
onServerStarting.trigger(event)
}
@EventHandler
def serverStopping(event: FMLServerStoppingEvent) {
onServerStopping.trigger(event)
}
}
+4
View File
@@ -86,6 +86,10 @@ object Misc {
def asInstanceOpt[T](v: Any, cls: Class[T]) =
if (cls.isInstance(v)) Some(v.asInstanceOf[T]) else None
// Same thing but in a way that's easier to use with map, etc.
def asInstanceOpt[T](cls: Class[T])(v: Any) =
if (cls.isInstance(v)) Some(v.asInstanceOf[T]) else None
def getNeighbourTile[T](origin: TileEntity, dir: ForgeDirection, cls: Class[T]) =
Option(origin.getWorldObj.getTileEntity(origin.xCoord + dir.offsetX,
origin.yCoord + dir.offsetY, origin.zCoord + dir.offsetZ)) flatMap (asInstanceOpt(_, cls))
@@ -0,0 +1,16 @@
/*
* Copyright (c) bdew, 2013 - 2014
* https://github.com/bdew/bdlib
*
* 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
* http://bdew.net/minecraft-mod-public-license/
*/
package net.bdew.lib.gui.widgets
import net.bdew.lib.gui._
case class WidgetSimpleIcon(rect: Rect, icon: Texture) extends Widget {
override def draw(mouse: Point) = parent.drawTexture(rect, icon)
}
@@ -9,8 +9,8 @@
package net.bdew.lib.multiblock.network
import net.bdew.lib.network.Message
import net.bdew.lib.network.BaseMessage
abstract class MsgOutputCfg extends Message {
abstract class MsgOutputCfg extends BaseMessage[NetHandler.type] {
val output: Int
}
@@ -9,4 +9,4 @@
package net.bdew.lib.network
abstract class Message extends Serializable
abstract class BaseMessage[CH <: NetChannel] extends Serializable
+35 -4
View File
@@ -13,18 +13,49 @@ import java.util
import cpw.mods.fml.common.network.{FMLEmbeddedChannel, FMLOutboundHandler, NetworkRegistry}
import cpw.mods.fml.relauncher.Side
import io.netty.channel.{ChannelFutureListener, ChannelHandler}
import io.netty.channel.{ChannelFutureListener, ChannelHandler, ChannelHandlerContext, SimpleChannelInboundHandler}
import net.bdew.lib.{BdLib, Misc}
import net.minecraft.entity.player.EntityPlayerMP
import net.minecraft.network.NetHandlerPlayServer
class NetChannel(val name: String) {
var channels: util.EnumMap[Side, FMLEmbeddedChannel] = null
type Message = BaseMessage[this.type]
object ServerHandler extends SimpleChannelInboundHandler[Message] {
def channelRead0(ctx: ChannelHandlerContext, msg: Message) {
try {
val player = ctx.channel().attr(NetworkRegistry.NET_HANDLER).get.asInstanceOf[NetHandlerPlayServer].playerEntity
if (serverChain.isDefinedAt(msg, player))
serverChain(msg, player)
else
BdLib.logWarn("Unable to handle message from user %s: %s", player.getDisplayName, msg)
} catch {
case e: Throwable => BdLib.logErrorException("Error handling packet", e)
}
}
}
object ClientHandler extends SimpleChannelInboundHandler[Message] {
def channelRead0(ctx: ChannelHandlerContext, msg: Message) {
try {
if (clientChain.isDefinedAt(msg))
clientChain(msg)
else
BdLib.logWarn("Unable to handle message from server: %s", msg)
} catch {
case e: Throwable => BdLib.logErrorException("Error handling packet", e)
}
}
}
def init() {
if (channels != null) sys.error("Attempted to initalize a channel twice (%s)".format(name))
channels = NetworkRegistry.INSTANCE.newChannel(name, new SerializedMessageCodec)
BdLib.logInfo("Initialized network channel '%s' for mod '%s'", name, Misc.getActiveModId)
addHandler(Side.SERVER, new NetChannelServerHandler(this))
addHandler(Side.CLIENT, new NetChannelClientHandler(this))
addHandler(Side.SERVER, ServerHandler)
addHandler(Side.CLIENT, ClientHandler)
}
var clientChain = PartialFunction.empty[Message, Unit]
@@ -38,7 +69,7 @@ class NetChannel(val name: String) {
private def addHandler(side: Side, handler: ChannelHandler) {
val ch = channels.get(side)
val name = ch.findChannelHandlerNameForType(classOf[SerializedMessageCodec])
val name = ch.findChannelHandlerNameForType(classOf[SerializedMessageCodec[this.type]])
ch.pipeline().addAfter(name, side + "Handler", handler)
}
@@ -1,26 +0,0 @@
/*
* Copyright (c) bdew, 2013 - 2014
* https://github.com/bdew/bdlib
*
* 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/bdlib/master/MMPL-1.0.txt
*/
package net.bdew.lib.network
import io.netty.channel.{ChannelHandlerContext, SimpleChannelInboundHandler}
import net.bdew.lib.BdLib
class NetChannelClientHandler(ch: NetChannel) extends SimpleChannelInboundHandler[Message] {
def channelRead0(ctx: ChannelHandlerContext, msg: Message) {
try {
if (ch.clientChain.isDefinedAt(msg))
ch.clientChain(msg)
else
BdLib.logWarn("Unable to handle message from server: %s", msg)
} catch {
case e: Throwable => BdLib.logErrorException("Error handling packet", e)
}
}
}
@@ -1,29 +0,0 @@
/*
* Copyright (c) bdew, 2013 - 2014
* https://github.com/bdew/bdlib
*
* 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/bdlib/master/MMPL-1.0.txt
*/
package net.bdew.lib.network
import cpw.mods.fml.common.network.NetworkRegistry
import io.netty.channel.{ChannelHandlerContext, SimpleChannelInboundHandler}
import net.bdew.lib.BdLib
import net.minecraft.network.NetHandlerPlayServer
class NetChannelServerHandler(ch: NetChannel) extends SimpleChannelInboundHandler[Message] {
def channelRead0(ctx: ChannelHandlerContext, msg: Message) {
try {
val player = ctx.channel().attr(NetworkRegistry.NET_HANDLER).get.asInstanceOf[NetHandlerPlayServer].playerEntity
if (ch.serverChain.isDefinedAt(msg, player))
ch.serverChain(msg, player)
else
BdLib.logWarn("Unable to handle message from user %s: %s", player.getDisplayName, msg)
} catch {
case e: Throwable => BdLib.logErrorException("Error handling packet", e)
}
}
}
@@ -21,8 +21,8 @@ import io.netty.handler.codec.MessageToMessageCodec
import net.bdew.lib.BdLib
@Sharable
class SerializedMessageCodec extends MessageToMessageCodec[FMLProxyPacket, Message] {
def encode(ctx: ChannelHandlerContext, msg: Message, out: util.List[AnyRef]) {
class SerializedMessageCodec[T <: NetChannel] extends MessageToMessageCodec[FMLProxyPacket, BaseMessage[T]] {
def encode(ctx: ChannelHandlerContext, msg: BaseMessage[T], out: util.List[AnyRef]) {
val buff = Unpooled.buffer()
val writer = new ObjectOutputStream(new ByteBufOutputStream(buff))
writer.writeObject(msg)
+61
View File
@@ -0,0 +1,61 @@
/*
* Copyright (c) bdew, 2013 - 2014
* https://github.com/bdew/bdlib
*
* 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
* http://bdew.net/minecraft-mod-public-license/
*/
package net.bdew.lib.player
import cpw.mods.fml.common.FMLCommonHandler
import cpw.mods.fml.common.eventhandler.SubscribeEvent
import cpw.mods.fml.common.gameevent.PlayerEvent._
import net.bdew.lib.BdLib
import net.minecraft.entity.player.EntityPlayer
import scala.collection.mutable
class PlayerCache[T] extends mutable.Map[EntityPlayer, T] {
var map = Map.empty[EntityPlayer, T]
BdLib.onServerStarting.listen(x => map = map.empty)
BdLib.onServerStopping.listen(x => map = map.empty)
FMLCommonHandler.instance().bus().register(this)
def updateIfNeeded(p: EntityPlayer, v: T): Boolean = {
if (map.get(p).contains(v)) {
false
} else {
map += p -> v
true
}
}
def reset(p: EntityPlayer) {
if (map.contains(p)) map -= p
}
override def get(p: EntityPlayer) = map.get(p)
override def +=(kv: (EntityPlayer, T)) = {
map += kv
this
}
override def -=(key: EntityPlayer) = {
map -= key
this
}
override def iterator = map.iterator
@SubscribeEvent
def hadlePlayerLogout(ev: PlayerLoggedOutEvent) = reset(ev.player)
@SubscribeEvent
def hadlePlayerChangedDimension(ev: PlayerChangedDimensionEvent) = reset(ev.player)
@SubscribeEvent
def hadlePlayerRespawn(ev: PlayerRespawnEvent) = reset(ev.player)
}