Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wirelesshub #24

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions src/main/resources/assets/ae2stuff/lang/en_US.lang
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ tile.ae2stuff.Encoder.name=Pattern Encoder
tile.ae2stuff.Grower.name=Crystal Growth Chamber
tile.ae2stuff.Inscriber.name=Advanced Inscriber
tile.ae2stuff.Wireless.name=Wireless Connector
tile.ae2stuff.WirelessHub.name=Hub

item.ae2stuff.WirelessKit.name=Wireless Setup Kit
item.ae2stuff.AdvWirelessKit.name=Advanced Wireless Setup Kit
Expand Down
8 changes: 6 additions & 2 deletions src/main/scala/net/bdew/ae2stuff/items/AdvWirelessKit.scala
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,14 @@ object AdvWirelessKit
Color.RED
)
)
} else if (tile.isHub && other.isHub) {
player.addChatMessage(
L("ae2stuff.wireless.tool.failed").setColor(Color.RED)
)
} else {
// Player can modify both sides - unlink current connections if any
tile.doUnlink()
other.doUnlink()
if (!tile.isHub) tile.doUnlink()
if (!other.isHub) other.doUnlink()

// Make player the owner of both blocks
tile.getNode.setPlayerID(pid)
Expand Down
8 changes: 6 additions & 2 deletions src/main/scala/net/bdew/ae2stuff/items/ItemWirelessKit.scala
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,14 @@ object ItemWirelessKit
Color.RED
)
)
} else if (tile.isHub && other.isHub) {
player.addChatMessage(
L("ae2stuff.wireless.tool.failed").setColor(Color.RED)
)
} else {
// Player can modify both sides - unlink current connections if any
tile.doUnlink()
other.doUnlink()
if (!tile.isHub) tile.doUnlink()
if (!other.isHub) other.doUnlink()

// Make player the owner of both blocks
tile.getNode.setPlayerID(pid)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ object BlockWireless
): util.ArrayList[ItemStack] = {
val stack = new ItemStack(this)
val te = getTE(world, x, y, z)
if (te.color != AEColor.Transparent) {
if (te.isHub) {
stack.setItemDamage(17)
} else if (te.color != AEColor.Transparent) {
stack.setItemDamage(te.color.ordinal() + 1)
}
val drops = new util.ArrayList[ItemStack]()
Expand All @@ -62,7 +64,7 @@ object BlockWireless
tab: CreativeTabs,
list: util.List[_]
): Unit = {
for (meta <- 0 to 16) {
for (meta <- 0 to 17) {
list
.asInstanceOf[util.List[ItemStack]]
.add(new ItemStack(itemIn, 1, meta))
Expand All @@ -79,7 +81,9 @@ object BlockWireless
): ItemStack = {
val stack = new ItemStack(this)
val te = getTE(world, x, y, z)
if (te.color != AEColor.Transparent) {
if (te.isHub) {
stack.setItemDamage(17)
} else if (te.color != AEColor.Transparent) {
stack.setItemDamage(te.color.ordinal() + 1)
}
stack
Expand Down Expand Up @@ -110,11 +114,14 @@ object BlockWireless
te.placingPlayer = player.asInstanceOf[EntityPlayer]
}
if (stack != null) {
val itemDamage = stack.getItemDamage
if (stack.hasDisplayName) {
te.customName = stack.getDisplayName
}
if (stack.getItemDamage > 0) {
te.color = AEColor.values().apply(stack.getItemDamage - 1)
if (itemDamage == 17) {
te.isHub = true;
} else if (itemDamage > 0) {
te.color = AEColor.values().apply(itemDamage - 1)
}
}
}
Expand Down Expand Up @@ -204,12 +211,19 @@ class ItemBlockWireless(b: Block) extends ItemBlockTooltip(b) {
advanced: Boolean
): Unit = {
super.addInformation(stack, player, list, advanced)
if (stack.getItemDamage > 0) {
val itemDamage = stack.getItemDamage
if (itemDamage == 17) {
list
.asInstanceOf[util.List[String]]
.add(
Misc.toLocal("tile.ae2stuff.WirelessHub.name")
)
} else if (itemDamage > 0) {
list
.asInstanceOf[util.List[String]]
.add(
Misc.toLocal(
AEColor.values().apply(stack.getItemDamage - 1).unlocalizedName
AEColor.values().apply(itemDamage - 1).unlocalizedName
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import net.bdew.ae2stuff.grid.{GridTile, VariableIdlePower}
import net.bdew.lib.block.BlockRef
import net.bdew.lib.data.base.{TileDataSlots, UpdateKind}
import net.bdew.lib.multiblock.data.DataSlotPos
import net.bdew.lib.nbt.NBT
import net.minecraft.block.Block
import net.minecraft.entity.player.EntityPlayer
import net.minecraft.item.ItemStack
Expand All @@ -47,6 +46,8 @@ class TileWireless

var customName: String = null
var color: AEColor = AEColor.Transparent
var isHub = false
var connectionsList = Array[TileWireless]()
def isLinked = link.isDefined
def getLink = link flatMap (_.getTile[TileWireless](worldObj))

Expand All @@ -71,27 +72,48 @@ class TileWireless
})

def doLink(other: TileWireless): Boolean = {
if (other.link.isEmpty) {
if (other.link.isEmpty && !(isHub || other.isHub)) {
other.link.set(myPos)
this.customName = other.customName
link.set(other.myPos)
setupConnection()
true
} else if (isHub) {
other.link.set(myPos)
other.customName = this.customName
other.setupConnection()
true
} else if (other.isHub) {
link.set(other.myPos)
customName = other.customName
setupConnection()
true
} else false
}

def doUnlink(): Unit = {
breakConnection()
getLink foreach { that =>
this.link := None
that.link := None
if (isHub) {
connectionsList foreach { that =>
that.doUnlink()
}
} else {
breakConnection()
getLink foreach { that =>
this.link := None
that.link := None
}
}
}

def setupConnection(): Boolean = {
getLink foreach { that =>
connection =
AEApi.instance().createGridConnection(this.getNode, that.getNode)
that.connection = connection
if (that.isHub) {
that.connectionsList = that.connectionsList :+ this
} else {
that.connection = connection
}
val dx = this.xCoord - that.xCoord
val dy = this.yCoord - that.yCoord
val dz = this.zCoord - that.zCoord
Expand All @@ -101,7 +123,7 @@ class TileWireless
dist * dist + 3
)
this.setIdlePowerUse(power)
that.setIdlePowerUse(power)
if (!that.isHub) that.setIdlePowerUse(power)
if (worldObj.blockExists(xCoord, yCoord, zCoord))
worldObj.setBlockMetadataWithNotify(
this.xCoord,
Expand All @@ -123,22 +145,48 @@ class TileWireless
false
}

def getHubChannels: Int = {
var channels = 0
connectionsList foreach { that =>
channels += that.connection.getUsedChannels
}
channels
}

def breakConnection(): Unit = {
if (connection != null)
connection.destroy()
connection = null
setIdlePowerUse(0d)
getLink foreach { other =>
other.connection = null
other.setIdlePowerUse(0d)
if (worldObj.blockExists(other.xCoord, other.yCoord, other.zCoord))
worldObj.setBlockMetadataWithNotify(
other.xCoord,
other.yCoord,
other.zCoord,
0,
3
if (other.isHub) {
other.connectionsList = other.connectionsList.filterNot(_ == this)
if (
other.connectionsList.isEmpty && worldObj.blockExists(
other.xCoord,
other.yCoord,
other.zCoord
)
)
worldObj.setBlockMetadataWithNotify(
other.xCoord,
other.yCoord,
other.zCoord,
0,
3
)
} else {
other.connection = null
other.setIdlePowerUse(0d)
if (worldObj.blockExists(other.xCoord, other.yCoord, other.zCoord))
worldObj.setBlockMetadataWithNotify(
other.xCoord,
other.yCoord,
other.zCoord,
0,
3
)
}
}
if (worldObj.blockExists(xCoord, yCoord, zCoord))
worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, 0, 3)
Expand Down Expand Up @@ -166,6 +214,7 @@ class TileWireless
t.setString("CustomName", customName)
}
t.setShort("Color", color.ordinal().toShort)
t.setBoolean("isHub", isHub)
}

override def doLoad(kind: UpdateKind.Value, t: NBTTagCompound): Unit = {
Expand All @@ -176,7 +225,11 @@ class TileWireless
if (!t.hasKey("Color")) {
t.setShort("Color", AEColor.Transparent.ordinal().toShort)
}
if (!t.hasKey("isHub")) {
t.setBoolean("isHub", isHub)
}
val colorIdx = t.getShort("Color").toInt
this.isHub = t.getBoolean("isHub")
this.color = AEColor.values().apply(colorIdx)
if (hasWorldObj) {
worldObj.markBlockRangeForRenderUpdate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ object WailaWirelessDataProvider
data.setString("name", te.customName)
}
tag.setTag("wireless_waila", data)
} else if (te.isHub) {
val data = NBT(
"channels" -> te.getHubChannels,
"color" -> te.color.ordinal()
)
if (te.hasCustomName) {
data.setString("name", te.customName)
}
tag.setTag("wirelesshub_waila", data)
} else {
val data = NBT(
"connected" -> false,
Expand Down Expand Up @@ -101,6 +110,23 @@ object WailaWirelessDataProvider
Misc.toLocal(AEColor.values().apply(color).unlocalizedName) :: Nil
} else Nil)
}
} else if (acc.getNBTData.hasKey("wirelesshub_waila")) {
val data = acc.getNBTData.getCompoundTag("wirelesshub_waila")
val name = if (data.hasKey("name")) data.getString("name") else null
val color = data.getInteger("color")
List(
Misc.toLocalF("tile.ae2stuff.WirelessHub.name"),
Misc.toLocalF(
"ae2stuff.waila.wireless.channels",
data.getInteger("channels")
)
)
.++(if (name != null) {
Misc.toLocalF("ae2stuff.waila.wireless.name", name) :: Nil
} else Nil)
.++(if (color != AEColor.Transparent.ordinal()) {
Misc.toLocal(AEColor.values().apply(color).unlocalizedName) :: Nil
} else Nil)
} else List.empty
}
}