From 2ebced30eb9c20a1bdb23e7dade1024964fa2eb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Fri, 3 Apr 2015 18:16:26 +0200 Subject: [PATCH] Made Chamelium edible, closes #1001. --- src/main/resources/application.conf | 4 +++ src/main/scala/li/cil/oc/Settings.scala | 1 + .../li/cil/oc/common/item/Chamelium.scala | 31 ++++++++++++++++++- .../li/cil/oc/common/item/Delegate.scala | 5 +++ .../li/cil/oc/common/item/Delegator.scala | 13 ++++++++ 5 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/main/resources/application.conf b/src/main/resources/application.conf index 20c4c98d9..db5fecc34 100644 --- a/src/main/resources/application.conf +++ b/src/main/resources/application.conf @@ -1041,6 +1041,10 @@ opencomputers { # cost is multiplied with, so 1 is a full refund, 0 disables the # functionality (won't be able to put prints into the material input). printRecycleRate: 0.75 + + # Whether Chamelium is edible or not. When eaten, it gives a (short) + # invisibility buff, and (slightly longer) blindness debuff. + chameliumEdible: true } # Settings for mod integration (the mod previously known as OpenComponents). diff --git a/src/main/scala/li/cil/oc/Settings.scala b/src/main/scala/li/cil/oc/Settings.scala index e83c479c6..177e9206c 100644 --- a/src/main/scala/li/cil/oc/Settings.scala +++ b/src/main/scala/li/cil/oc/Settings.scala @@ -295,6 +295,7 @@ class Settings(val config: Config) { val threadPriority = config.getInt("misc.threadPriority") val maxPrintComplexity = config.getInt("misc.maxPrinterShapes") val printRecycleRate = config.getDouble("misc.printRecycleRate") + val chameliumEdible = config.getBoolean("misc.chameliumEdible") // ----------------------------------------------------------------------- // // integration diff --git a/src/main/scala/li/cil/oc/common/item/Chamelium.scala b/src/main/scala/li/cil/oc/common/item/Chamelium.scala index 0bcb54cbc..281d258e7 100644 --- a/src/main/scala/li/cil/oc/common/item/Chamelium.scala +++ b/src/main/scala/li/cil/oc/common/item/Chamelium.scala @@ -1,3 +1,32 @@ package li.cil.oc.common.item -class Chamelium(val parent: Delegator) extends Delegate +import li.cil.oc.Settings +import net.minecraft.entity.player.EntityPlayer +import net.minecraft.item.EnumAction +import net.minecraft.item.ItemStack +import net.minecraft.potion.Potion +import net.minecraft.potion.PotionEffect +import net.minecraft.world.World + +class Chamelium(val parent: Delegator) extends Delegate { + override def onItemRightClick(stack: ItemStack, world: World, player: EntityPlayer): ItemStack = { + if (Settings.get.chameliumEdible) { + player.setItemInUse(stack, getMaxItemUseDuration(stack)) + } + stack + } + + override def getItemUseAction(stack: ItemStack): EnumAction = EnumAction.eat + + override def getMaxItemUseDuration(stack: ItemStack): Int = 32 + + override def onEaten(stack: ItemStack, world: World, player: EntityPlayer): ItemStack = { + if (!world.isRemote) { + player.addPotionEffect(new PotionEffect(Potion.invisibility.id, 100, 0)) + player.addPotionEffect(new PotionEffect(Potion.blindness.id, 200, 0)) + } + stack.stackSize -= 1 + if (stack.stackSize > 0) stack + else null + } +} diff --git a/src/main/scala/li/cil/oc/common/item/Delegate.scala b/src/main/scala/li/cil/oc/common/item/Delegate.scala index e0f1cfddd..95f45035c 100644 --- a/src/main/scala/li/cil/oc/common/item/Delegate.scala +++ b/src/main/scala/li/cil/oc/common/item/Delegate.scala @@ -14,6 +14,7 @@ import li.cil.oc.util.Rarity import li.cil.oc.util.Tooltip import net.minecraft.entity.Entity import net.minecraft.entity.player.EntityPlayer +import net.minecraft.item.EnumAction import net.minecraft.item.ItemStack import net.minecraft.world.World @@ -55,8 +56,12 @@ trait Delegate { stack } + def getItemUseAction(stack: ItemStack): EnumAction = EnumAction.none + def getMaxItemUseDuration(stack: ItemStack) = 0 + def onEaten(stack: ItemStack, world: World, player: EntityPlayer): ItemStack = stack + def onPlayerStoppedUsing(stack: ItemStack, player: EntityPlayer, duration: Int) {} def update(stack: ItemStack, world: World, player: Entity, slot: Int, selected: Boolean) {} diff --git a/src/main/scala/li/cil/oc/common/item/Delegator.scala b/src/main/scala/li/cil/oc/common/item/Delegator.scala index b461e8554..fb38b7401 100644 --- a/src/main/scala/li/cil/oc/common/item/Delegator.scala +++ b/src/main/scala/li/cil/oc/common/item/Delegator.scala @@ -14,6 +14,7 @@ import net.minecraft.client.renderer.texture.IIconRegister import net.minecraft.creativetab.CreativeTabs import net.minecraft.entity.Entity import net.minecraft.entity.player.EntityPlayer +import net.minecraft.item.EnumAction import net.minecraft.item.EnumRarity import net.minecraft.item.Item import net.minecraft.item.ItemStack @@ -124,6 +125,18 @@ class Delegator extends Item { // ----------------------------------------------------------------------- // + override def onEaten(stack: ItemStack, world: World, player: EntityPlayer): ItemStack = + subItem(stack) match { + case Some(subItem) => subItem.onEaten(stack, world, player) + case _ => super.onEaten(stack, world, player) + } + + override def getItemUseAction(stack: ItemStack): EnumAction = + subItem(stack) match { + case Some(subItem) => subItem.getItemUseAction(stack) + case _ => super.getItemUseAction(stack) + } + override def getMaxItemUseDuration(stack: ItemStack): Int = subItem(stack) match { case Some(subItem) => subItem.getMaxItemUseDuration(stack)