Skip to content

Commit

Permalink
Added requested features from pull request review
Browse files Browse the repository at this point in the history
 - Added constants to AdvWirelessKit.scala to define values for the modes
 - Added a constant to AdvItemLocationStore.scala to find the COMPOUND NBTTag type and use that in place of 10 which was used previously
 - Added error logging to AdvWirelessKit.scala and ItemWirelessKit.scala for when a connection fails fatally
 - Switched else statements for mode to else if statements to explicitly check that the mode is the other mode
  • Loading branch information
ReignOfFROZE committed Jun 11, 2024
1 parent 95ee00b commit 92b38d4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
18 changes: 11 additions & 7 deletions src/main/scala/net/bdew/ae2stuff/items/AdvWirelessKit.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ object AdvWirelessKit
with AdvItemLocationStore {
setMaxStackSize(1)

val MODE_QUEUING = 0;
val MODE_BINDING = 1;

def checkSecurity(t1: TileWireless, t2: TileWireless, p: EntityPlayer) = {
val pid = Security.getPlayerId(p)
Security.playerHasPermission(
Expand All @@ -51,7 +54,7 @@ object AdvWirelessKit
import net.bdew.lib.helpers.ChatHelper._
if (!world.isRemote && player.isSneaking) {
toggleMode(stack)
if (getMode(stack) == 0) {
if (getMode(stack) == MODE_QUEUING) {
player.addChatMessage(
L("ae2stuff.wireless.advtool.queueing.activated").setColor(
Color.GREEN
Expand Down Expand Up @@ -84,13 +87,13 @@ object AdvWirelessKit
if (!world.isRemote) {
if (player.isSneaking) {
toggleMode(stack)
if (getMode(stack) == 0) {
if (getMode(stack) == MODE_QUEUING) {
player.addChatMessage(
L("ae2stuff.wireless.advtool.queueing.activated").setColor(
Color.GREEN
)
)
} else {
} else if (getMode(stack) == MODE_BINDING) {
player.addChatMessage(
L("ae2stuff.wireless.advtool.binding.activated").setColor(
Color.GREEN
Expand All @@ -112,7 +115,7 @@ object AdvWirelessKit
player.addChatMessage(
L("ae2stuff.wireless.tool.security.player").setColor(Color.RED)
)
} else if (getMode(stack) == 0) {
} else if (getMode(stack) == MODE_QUEUING) {
addLocation(stack, pos, world.provider.dimensionId)
player.addChatMessage(
L(
Expand All @@ -122,7 +125,7 @@ object AdvWirelessKit
pos.z.toString
).setColor(Color.GREEN)
)
} else if (getMode(stack) == 1) {
} else if (getMode(stack) == MODE_BINDING) {
if (hasLocation(stack)) {
// Have other location - start connecting
val otherPos = getNextLocation(stack)
Expand Down Expand Up @@ -183,6 +186,7 @@ object AdvWirelessKit
) & ": " & e.getMessage).setColor(Color.RED)
)
tile.doUnlink()
print("Failed to link wireless connector: " + e)
}
}
popLocation(stack)
Expand Down Expand Up @@ -224,7 +228,7 @@ object AdvWirelessKit
)
)
}
if (getMode(stack) == 0) {
if (getMode(stack) == MODE_QUEUING) {
list.add(Misc.toLocal("ae2stuff.wireless.advtool.queueing"))
if (getLocations(stack).tagCount() == 0) {
list.add(Misc.toLocal("ae2stuff.wireless.advtool.queueing.empty"))
Expand All @@ -235,7 +239,7 @@ object AdvWirelessKit
list.add(loc.x + "," + loc.y + "," + loc.z)
}
}
} else {
} else if (getMode(stack) == MODE_BINDING) {
list.add(Misc.toLocal("ae2stuff.wireless.advtool.binding"))
if (getLocations(stack).tagCount() == 0) {
list.add(Misc.toLocal("ae2stuff.wireless.advtool.binding.empty"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ object ItemWirelessKit
) & ": " & e.getMessage).setColor(Color.RED)
)
tile.doUnlink()
print("Failed to link wireless connector: " + e)
}
}
clearLocation(stack)
Expand Down
30 changes: 16 additions & 14 deletions src/main/scala/net/bdew/ae2stuff/misc/AdvItemLocationStore.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@ package net.bdew.ae2stuff.misc
import net.bdew.lib.block.BlockRef
import net.bdew.lib.nbt.NBT
import net.minecraft.item.{Item, ItemStack}
import net.minecraft.nbt.NBTTagCompound
import net.minecraft.nbt.{NBTBase, NBTTagCompound}

trait AdvItemLocationStore extends Item {

import net.bdew.ae2stuff.items.AdvWirelessKit.MODE_QUEUING

private val COMPOUND_TAG = NBTBase.NBTTypes.indexOf("COMPOUND")

def addLocation(stack: ItemStack, loc: BlockRef, dimension: Int): Boolean = {
if (!stack.hasTagCompound) stack.setTagCompound(new NBTTagCompound)
val tag = stack.getTagCompound
if (tag.hasKey("dim") && tag.getInteger("dim") != dimension) {
false
}
val locList = tag.getTagList("loc", 10)
val locList = tag.getTagList("loc", COMPOUND_TAG)
for (i <- 0 until locList.tagCount()) {
val tag = locList.getCompoundTagAt(i)
val pos = BlockRef.fromNBT(tag)
Expand All @@ -33,20 +37,18 @@ trait AdvItemLocationStore extends Item {
locList.appendTag(NBT.from(loc.writeToNBT _))
tag.setTag("loc", locList)
tag.setInteger("dim", dimension)
print("Added location")
print("locations: " + locList.tagCount())
true
}

def getLocations(stack: ItemStack) = {
if (!stack.hasTagCompound) stack.setTagCompound(new NBTTagCompound)
val tag = stack.getTagCompound
if (tag.hasKey("loc")) {
val locList = tag.getTagList("loc", 10)
val locList = tag.getTagList("loc", COMPOUND_TAG)
locList
} else {
tag.setTag("loc", new NBTTagCompound)
tag.getTagList("loc", 10)
tag.getTagList("loc", COMPOUND_TAG)
}
}

Expand All @@ -58,7 +60,7 @@ trait AdvItemLocationStore extends Item {
.hasKey("loc")
) {
// check if list is not empty
val loc = stack.getTagCompound.getTagList("loc", 10)
val loc = stack.getTagCompound.getTagList("loc", COMPOUND_TAG)
if (loc.tagCount() > 0) {
return true
}
Expand All @@ -68,7 +70,7 @@ trait AdvItemLocationStore extends Item {

def getNextLocation(stack: ItemStack): BlockRef =
BlockRef.fromNBT(
stack.getTagCompound.getTagList("loc", 10).getCompoundTagAt(0)
stack.getTagCompound.getTagList("loc", COMPOUND_TAG).getCompoundTagAt(0)
)

def getDimension(stack: ItemStack): Int =
Expand All @@ -77,15 +79,15 @@ trait AdvItemLocationStore extends Item {
def setLocation(stack: ItemStack, loc: BlockRef, dimension: Int): Unit = {
if (!stack.hasTagCompound) stack.setTagCompound(new NBTTagCompound)
val tag = stack.getTagCompound
val locList = tag.getTagList("loc", 10)
val locList = tag.getTagList("loc", COMPOUND_TAG)
locList.appendTag(NBT.from(loc.writeToNBT _))
tag.setTag("loc", locList)
tag.setInteger("dim", dimension)
}

def popLocation(stack: ItemStack): BlockRef = {
if (stack.hasTagCompound) {
val locList = stack.getTagCompound.getTagList("loc", 10)
val locList = stack.getTagCompound.getTagList("loc", COMPOUND_TAG)
if (locList.tagCount() > 0) {
val tag = locList.getCompoundTagAt(0)
locList.removeTag(0)
Expand All @@ -107,8 +109,8 @@ trait AdvItemLocationStore extends Item {
if (tag.hasKey("mode")) {
tag.getInteger("mode")
} else {
tag.setInteger("mode", 0)
0
tag.setInteger("mode", MODE_QUEUING)
MODE_QUEUING
}
}

Expand All @@ -120,8 +122,8 @@ trait AdvItemLocationStore extends Item {
tag.setInteger("mode", (mode + 1) % 2)
tag.getInteger("mode")
} else {
tag.setInteger("mode", 0)
0
tag.setInteger("mode", MODE_QUEUING)
MODE_QUEUING
}
}
}

0 comments on commit 92b38d4

Please sign in to comment.