1

我是 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)

谢谢!

4

1 回答 1

0

在您的第一个片段中,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)
于 2013-11-12T13:11:29.260 回答