trying to generate better names for compound drivers

This commit is contained in:
Florian Nücke 2014-02-05 22:59:45 +01:00
parent 05a0e327b1
commit c743b89571
3 changed files with 36 additions and 24 deletions

View File

@ -114,11 +114,9 @@ class Delegator[Child <: Delegate](id: Int) extends Block(id, Material.iron) {
override def damageDropped(metadata: Int) =
subBlock(metadata) match {
case Some(subBlock) => subBlock.itemDamage
case _ => super.damageDropped(metadata)
case _ => metadata
}
override def getDamageValue(world: World, x: Int, y: Int, z: Int) = world.getBlockMetadata(x, y, z)
override def getPickBlock(target: MovingObjectPosition, world: World, x: Int, y: Int, z: Int) =
subBlock(world, x, y, z) match {
case Some(subBlock) => subBlock.pick(target, world, x, y, z)

View File

@ -168,8 +168,8 @@ class Adapter extends Environment with Inventory with Analyzable {
val isValidBlock = blockId >= 0 && blockId < Block.blocksList.length && Block.blocksList(blockId) != null
if (isValidBlock) {
val block = Block.blocksList(blockId)
block.getBlockDropped(world, x, y, z, world.getBlockMetadata(x, y, z), 0).exists(stack.isItemEqual) ||
stack.itemID == block.idDropped(0, world.rand, 0)
stack.itemID == block.idDropped(0, world.rand, 0) &&
stack.getItemDamage == block.getDamageValue(world, x, y, z)
}
else false
}

View File

@ -2,27 +2,19 @@ package li.cil.oc.server.driver
import li.cil.oc.api.driver
import net.minecraft.block.Block
import net.minecraft.item.ItemStack
import net.minecraft.item.{Item, ItemStack}
import net.minecraft.world.World
class CompoundBlockDriver(val blocks: driver.Block*) extends driver.Block {
override def createEnvironment(world: World, x: Int, y: Int, z: Int) = blocks.map {
driver => Option(driver.createEnvironment(world, x, y, z)) match {
case Some(environment) => (driver, environment)
case _ => null
}
} filter (_ != null) match {
case Seq() => null
case list =>
val blockId = world.getBlockId(x, y, z)
val isValidBlock = blockId >= 0 && blockId < Block.blocksList.length && Block.blocksList(blockId) != null
val name =
if (isValidBlock) {
val metadata = world.getBlockMetadata(x, y, z)
cleanName(new ItemStack(blockId, 1, metadata).getUnlocalizedName)
}
else "multi"
new CompoundBlockEnvironment(name, list: _*)
override def createEnvironment(world: World, x: Int, y: Int, z: Int) = {
val list = blocks.map {
driver => Option(driver.createEnvironment(world, x, y, z)) match {
case Some(environment) => (driver, environment)
case _ => null
}
} filter (_ != null)
if (list.isEmpty) null
else new CompoundBlockEnvironment(tryGetName(world, x, y, z), list: _*)
}
override def worksWith(world: World, stack: ItemStack) = blocks.forall(_.worksWith(world, stack))
@ -36,9 +28,31 @@ class CompoundBlockDriver(val blocks: driver.Block*) extends driver.Block {
case _ => false
}
private def tryGetName(world: World, x: Int, y: Int, z: Int) = {
val blockId = world.getBlockId(x, y, z)
val isValidBlock = blockId >= 0 && blockId < Block.blocksList.length && Block.blocksList(blockId) != null
if (isValidBlock) {
val block = Block.blocksList(blockId)
val itemStack = try Option(block.getPickBlock(null, world, x, y, z)) catch {
case _: Throwable =>
if (Item.itemsList(blockId) != null) {
Some(new ItemStack(blockId, 1, block.getDamageValue(world, x, y, z)))
}
else None
}
itemStack match {
case Some(stack) => cleanName(stack.getUnlocalizedName)
case _ => "multi"
}
}
else "multi"
}
private def cleanName(name: String) = {
val withoutNameSpace = if (name.contains(":")) name.substring(name.indexOf(":") + 1) else name
val withoutPrefixes = if (withoutNameSpace.contains(".")) withoutNameSpace.substring(withoutNameSpace.lastIndexOf(".") + 1) else withoutNameSpace
withoutPrefixes
val safeStart = if (withoutPrefixes.matches("""^[^a-zA-Z_]""")) "_" + withoutPrefixes else withoutPrefixes
val identifier = safeStart.replaceAll("""[^\w_]""", "_")
identifier
}
}