giants/src/main/java/pl/minecon724/giants/Main.java

87 lines
2.5 KiB
Java

package pl.minecon724.giants;
import java.io.File;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
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.EntitySpawnEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
public class Main extends JavaPlugin implements Listener {
File configFile = new File(getDataFolder(), "config.yml");
FileConfiguration config;
Random rnd = new Random();
boolean ai;
double chance;
Map<PotionEffectType, Integer> effects;
@Override
public void onEnable() {
if (!(configFile.exists())) {
saveResource("config.yml", false);
}
config = YamlConfiguration.loadConfiguration(configFile);
ai = config.getBoolean("ai");
chance = config.getDouble("chance");
for (Object s : config.getList("effects")) {
try {
String[] parts = ((String) s).split(":");
if (!(StringUtils.isNumeric(parts[1]))) {
throw new IllegalArgumentException(parts[1] + " is not a number");
}
PotionEffectType effectType = PotionEffectType.getByName(parts[0]);
if (effectType == null) {
throw new IllegalArgumentException(parts[0] + " is not a PotionEffectType");
}
Integer duration = Integer.valueOf(parts[1]);
effects.put(effectType, duration);
} catch (IllegalArgumentException e) {
Bukkit.getLogger().severe("Unable to parse potion effects!");
e.printStackTrace();
continue;
}
}
getServer().getPluginManager().registerEvents(this, this);
}
@EventHandler
public void entitySpawn(EntitySpawnEvent e) {
if (e.getEntityType() == EntityType.ZOMBIE) {
if (rnd.nextDouble() <= chance) {
e.setCancelled(true);
spawnGiant(ai, e.getLocation());
}
}
}
Entity spawnGiant(boolean ai, Location pos) {
LivingEntity entity = (LivingEntity) pos.getWorld().spawnEntity(pos, EntityType.GIANT);
if (ai) {
//entity.addPassenger(pos.getWorld().spawnEntity(pos, EntityType.HUSK));
entity.setAI(true);
}
for (Entry<PotionEffectType, Integer> t : effects.entrySet()) {
PotionEffect effect = new PotionEffect(t.getKey(), Integer.MAX_VALUE, t.getValue());
effect.apply(entity);
}
return null;
}
}