mingmars
package com.mewsf.enderex;
import java.io.File;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
public class EnderdragonEX extends JavaPlugin{
@Override
public void onEnable(){
File configFile = new File(getDataFolder(), "config.yml"); //定义config.yml
if (!configFile.exists()) { //此段代码用于config.yml不存在时生成此文件
saveDefaultConfig();
}
FileConfiguration config = getConfig(); //读取config.yml
final int nauseatime = config.getInt("NauseaTimer"); //以下三个final都是把config.yml中的
final int damage = config.getInt("Damage"); //数值转化为插件中的常量
final boolean nausea = config.getBoolean("Nausea");
/*
*下面是更改末影龙伤害的监听器
*/
class DamageEventListener implements Listener {
@EventHandler
public void onEntityDamagedByEntityEvent(EntityDamageByEntityEvent event) { //调用了实体被实体攻击时的事件
final Entity damager = event.getDamager(); //获取攻击者
if (damager.getType() == EntityType.ENDER_DRAGON) { //如果攻击者是末影龙
event.setDamage(damage); //将伤害值改为已定义好的damage
}
}
}
class Nausea implements Listener { //反胃效果的监听器
@EventHandler
public void onEntityDamagedByEntityEvent(EntityDamageByEntityEvent event) {
Entity damager = event.getDamager();
if (damager.getType() == EntityType.ENDER_DRAGON) {
LivingEntity victim = (LivingEntity) event.getEntity(); //定义victim对象为被攻击者
victim.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, nauseatime, 1));
//增加药水效果
}
}
}
getServer().getPluginManager().registerEvents(new DamageEventListener(), this); //注册更改末影龙伤害的监听器
/*
* 下面是根据配置文件决定是否开启反胃效果
*/
if (nausea == true){
getServer().getPluginManager().registerEvents(new Nausea(), this);
}
getLogger().info("插件载入正常.");
}
@Override
public void onDisable(){
getLogger().info("插件已被卸载.");
}
}
2014-02-17 01:14:00