【问题标题】:Weird TypeError in jython/python?jython/python 中的奇怪类型错误?
【发布时间】:2013-11-12 12:53:40
【问题描述】:

我是 bukkit jython/python 插件程序员。最近几天,我一直在努力解决这个问题。我必须给用户添加一个药水效果,如果我在代码中手动输入效果名称、持续时间和放大器是没有问题的,但是当我想从配置中获取它们时,我得到了这个错误:

    13:38:20 [SEVERE] Could not pass event PlayerInteractEvent to ItemEffect v1.0
    Traceback (most recent call last):
    File "<iostream>", line 126, in onPlayerInteractEvent
    TypeError: addPotionEffect(): 1st arg can't be coerced to org.bukkit.potion.Poti
    onEffect

这是那部分代码:

    effectname = section.getString("%s.effect"%currentKey)
    duration = section.getInt("%s.duration"%currentKey)
    durationinticks = duration * 20
    geteffectname = "PotionEffectType.%s"%effectname
    getpotioneffect = "PotionEffect(%s, %i, 1)"%(geteffectname, durationinticks)
    geteffectname = "PotionEffectType.%s"%effectname
    if iteminhand == currentKey:
       event.getPlayer().addPotionEffect(getpotioneffect)

当我打印出 getpotioneffect 时,我得到:

    13:38:20 [INFO] PotionEffect(PotionEffectType.SPEED, 600, 1)

没关系,应该可以工作。我在没有从配置中获取信息的情况下对其进行了测试,它运行良好......总而言之,上面的代码不起作用,但下面的代码起作用:

    getpotioneffect = PotionEffect(PotionEffectType.SPEED, 600, 1)
    if iteminhand == currentKey:
       event.getPlayer().addPotionEffect(getpotioneffect)

此活动的 javadocs 链接!

http://jd.bukkit.org/rb/apidocs/org/bukkit/entity/LivingEntity.html#addPotionEffect(org.bukkit.potion.PotionEffect)

谢谢!

【问题讨论】:

    标签: python jython typeerror


    【解决方案1】:

    在您的第一个 sn-p 中,getpotioneffect 是一个字符串。您可以检查它在某处添加print type(getpotioneffect)。

    你想要的是替换这个:

    geteffectname = "PotionEffectType.%s"%effectname
    getpotioneffect = "PotionEffect(%s, %i, 1)"%(geteffectname, durationinticks)
    

    用这个:

    effect_type = getattr(PotionEffectType, effectname)
    potion_effect = PotionEffect(effect_type, durationinticks, 1)
    

    【讨论】:

    • 这就像一个魅力!但为什么以及如何?有空可以给我解释一下吗?
    • Err...实际上这只需要一个非常非常非常基本的 - 我应该说“最小”吗? - 对编程的理解。 litteral string "PotionEffect(PotionEffectType.SPEED, 600, 1)" 显然与 source code PotionEffect(PotionEffectType.SPEED, 600, 1) 的评估结果不同。
    猜你喜欢
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    • 2013-12-16
    • 2017-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多