Allow experience upgrade to consume bottles of enchanting and enchanted items for experience.

Kind of a late implementation for #129 :P
This commit is contained in:
Florian Nücke 2015-05-01 22:44:05 +02:00
parent 208c5b65e4
commit c5eec99d75
4 changed files with 46 additions and 6 deletions

View File

@ -5,6 +5,7 @@ import li.cil.oc.api
import li.cil.oc.api.driver.EnvironmentAware
import li.cil.oc.api.driver.EnvironmentHost
import li.cil.oc.api.driver.item.HostAware
import li.cil.oc.api.internal
import li.cil.oc.common.Slot
import li.cil.oc.common.Tier
import li.cil.oc.server.component
@ -14,7 +15,11 @@ object DriverUpgradeExperience extends Item with HostAware with EnvironmentAware
override def worksWith(stack: ItemStack) = isOneOf(stack,
api.Items.get(Constants.ItemName.ExperienceUpgrade))
override def createEnvironment(stack: ItemStack, host: EnvironmentHost) = new component.UpgradeExperience()
override def createEnvironment(stack: ItemStack, host: EnvironmentHost) =
host match {
case host: internal.Agent => new component.UpgradeExperience(host)
case _ => null
}
override def slot(stack: ItemStack) = Slot.Upgrade

View File

@ -5,7 +5,7 @@ import li.cil.oc.api
import li.cil.oc.api.driver.EnvironmentAware
import li.cil.oc.api.driver.EnvironmentHost
import li.cil.oc.api.driver.item.HostAware
import li.cil.oc.api.internal.Robot
import li.cil.oc.api.internal
import li.cil.oc.common.Slot
import li.cil.oc.common.Tier
import li.cil.oc.server.component
@ -17,7 +17,7 @@ object DriverUpgradeGenerator extends Item with HostAware with EnvironmentAware
override def createEnvironment(stack: ItemStack, host: EnvironmentHost) =
host match {
case robot: Robot => new component.UpgradeGenerator(robot)
case host: internal.Agent => new component.UpgradeGenerator(host)
case _ => null
}

View File

@ -160,7 +160,6 @@ object ModOpenComputers extends ModProxy {
Constants.BlockName.ScreenTier1,
Constants.ItemName.AngelUpgrade,
Constants.ItemName.CraftingUpgrade,
Constants.ItemName.ExperienceUpgrade,
Constants.ItemName.HoverUpgradeTier1,
Constants.ItemName.HoverUpgradeTier2)
blacklistHost(classOf[internal.Microcontroller],

View File

@ -2,14 +2,21 @@ package li.cil.oc.server.component
import li.cil.oc.Settings
import li.cil.oc.api
import li.cil.oc.api.driver.EnvironmentHost
import li.cil.oc.api.internal
import li.cil.oc.api.machine.Arguments
import li.cil.oc.api.machine.Callback
import li.cil.oc.api.machine.Context
import li.cil.oc.api.network.Visibility
import li.cil.oc.api.prefab
import net.minecraft.enchantment.Enchantment
import net.minecraft.enchantment.EnchantmentHelper
import net.minecraft.init.Items
import net.minecraft.nbt.NBTTagCompound
class UpgradeExperience extends prefab.ManagedEnvironment {
import scala.collection.convert.WrapAsScala._
class UpgradeExperience(val host: EnvironmentHost with internal.Agent) extends prefab.ManagedEnvironment {
override val node = api.Network.newNode(this, Visibility.Network).
withComponent("experience").
withConnector(30 * Settings.get.bufferPerLevel).
@ -19,7 +26,7 @@ class UpgradeExperience extends prefab.ManagedEnvironment {
var level = 0
def xpForLevel(level: Int) =
def xpForLevel(level: Int): Double =
if (level == 0) 0
else Settings.get.baseXpToLevel + Math.pow(level * Settings.get.constantXpGrowth, Settings.get.exponentialXpGrowth)
@ -50,6 +57,35 @@ class UpgradeExperience extends prefab.ManagedEnvironment {
result(level + xpProgress / xpNeeded)
}
@Callback(doc = """function():boolean -- Tries to consume an enchanted item to add experience to the upgrade.""")
def consume(context: Context, args: Arguments): Array[AnyRef] = {
val stack = host.mainInventory.getStackInSlot(host.selectedSlot)
if (stack == null || stack.stackSize < 1) {
return result(Unit, "no item")
}
var xp = 0
if (stack.getItem == Items.experience_bottle) {
xp += 3 + host.world.rand.nextInt(5) + host.world.rand.nextInt(5)
}
else {
for ((id: Int, level: Int) <- EnchantmentHelper.getEnchantments(stack)) {
val enchantment = Enchantment.enchantmentsList(id)
if (enchantment != null) {
xp += enchantment.getMinEnchantability(level)
}
}
if (xp <= 0) {
return result(Unit, "could not extract experience from item")
}
}
val consumed = host.mainInventory().decrStackSize(host.selectedSlot, 1)
if (consumed == null || consumed.stackSize < 1) {
return result(Unit, "could not consume item")
}
addExperience(xp * Settings.get.constantXpGrowth)
result(true)
}
override def save(nbt: NBTTagCompound) {
super.save(nbt)
nbt.setDouble(Settings.namespace + "xp", experience)