package pl.minecon724.giants; import java.io.File; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; 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.entity.Player; 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; import org.bukkit.scheduler.BukkitRunnable; public class Main extends JavaPlugin implements Listener { File configFile = new File(getDataFolder(), "config.yml"); FileConfiguration config; Random rnd = new Random(); List giants = new ArrayList(); boolean ai; boolean headRotations; boolean attack; double chance; double attackDamage; double attackReach; double expandUp; long refreshDelay; Map effects = new HashMap(); @Override public void onEnable() { if (!(configFile.exists())) { saveResource("config.yml", false); } config = YamlConfiguration.loadConfiguration(configFile); ai = config.getBoolean("ai"); chance = config.getDouble("chance"); attackDamage = config.getDouble("attackDamage"); attackReach = config.getDouble("attackReach"); expandUp = config.getDouble("expandUp"); 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); new BukkitRunnable() { @Override public void run() { for (Entity e : giants) { Collection nearby = e.getWorld().getNearbyEntities(e.getBoundingBox().expand( attackReach, expandUp, attackReach), n -> (n instanceof Player)); for (Entity p : nearby) { ((LivingEntity) p).damage(attackDamage, e); } } } }.runTaskTimer(this, config.getLong("hitDelay"), 0L); if (headRotations) { new BukkitRunnable() { @Override public void run() { for (Entity e : giants) { List passengers = e.getPassengers(); if (passengers.size() > 0) { passengers.removeIf(n -> (n.getType() != EntityType.HUSK)); Location loc = passengers.get(0).getLocation(); e.setRotation(loc.getYaw(), loc.getPitch()); } } } }.runTaskTimerAsynchronously(this, refreshDelay, 0L); } } @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) { LivingEntity passenger = (LivingEntity) pos.getWorld().spawnEntity(pos, EntityType.HUSK); new PotionEffect(PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 1) .apply((LivingEntity) passenger); new PotionEffect(PotionEffectType.WEAKNESS, Integer.MAX_VALUE, 255) .apply((LivingEntity) passenger); passenger.setCustomName("Giant"); passenger.setCustomNameVisible(false); passenger.setInvulnerable(true); passenger.setPersistent(true); entity.addPassenger(passenger); } for (Entry t : effects.entrySet()) { PotionEffect effect = new PotionEffect(t.getKey(), Integer.MAX_VALUE, t.getValue()); effect.apply(entity); } giants.add(entity); return entity; } }