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

211 lines
6.9 KiB
Java
Raw Normal View History

2022-01-29 13:01:49 +00:00
package pl.minecon724.giants;
import java.io.File;
2022-01-29 14:10:18 +00:00
import java.util.ArrayList;
import java.util.Collection;
2022-01-29 13:13:05 +00:00
import java.util.HashMap;
2022-01-29 14:10:18 +00:00
import java.util.List;
2022-01-29 13:01:49 +00:00
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import org.apache.commons.lang.StringUtils;
2022-01-31 11:37:41 +00:00
import org.bstats.bukkit.Metrics;
2022-02-03 18:58:54 +00:00
import org.bstats.charts.SimplePie;
2022-01-29 13:01:49 +00:00
import org.bukkit.Bukkit;
import org.bukkit.Location;
2022-02-03 11:58:10 +00:00
import org.bukkit.Material;
2022-02-03 18:27:51 +00:00
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
2022-01-29 13:01:49 +00:00
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;
2022-01-29 14:33:54 +00:00
import org.bukkit.entity.Player;
2022-01-29 13:01:49 +00:00
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
2022-01-29 17:32:59 +00:00
import org.bukkit.event.entity.EntityDamageByEntityEvent;
2022-01-29 17:05:54 +00:00
import org.bukkit.event.entity.EntityDeathEvent;
2022-01-29 13:01:49 +00:00
import org.bukkit.event.entity.EntitySpawnEvent;
2022-02-03 12:16:35 +00:00
import org.bukkit.inventory.ItemStack;
2022-01-29 16:53:44 +00:00
import org.bukkit.metadata.FixedMetadataValue;
2022-01-29 13:01:49 +00:00
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
2022-01-29 14:10:18 +00:00
import org.bukkit.scheduler.BukkitRunnable;
2022-01-29 13:01:49 +00:00
2022-02-03 18:27:51 +00:00
public class Main extends JavaPlugin implements Listener, CommandExecutor {
2022-01-29 13:01:49 +00:00
File configFile = new File(getDataFolder(), "config.yml");
FileConfiguration config;
2022-01-31 11:37:41 +00:00
Metrics metrics = new Metrics(this, 14131);
2022-01-29 13:01:49 +00:00
Random rnd = new Random();
2022-01-29 14:33:54 +00:00
List<LivingEntity> giants = new ArrayList<LivingEntity>();
2022-01-29 13:01:49 +00:00
boolean ai;
2022-01-29 14:33:54 +00:00
boolean headRotations;
2022-01-29 15:19:04 +00:00
boolean attack;
2022-01-29 13:01:49 +00:00
double chance;
2022-01-29 14:10:18 +00:00
double attackDamage;
2022-01-29 15:03:13 +00:00
double attackReach;
double expandUp;
2022-01-29 14:33:54 +00:00
long refreshDelay;
2022-01-29 13:13:05 +00:00
Map<PotionEffectType, Integer> effects = new HashMap<PotionEffectType, Integer>();
2022-02-03 11:58:10 +00:00
Map<Material, List<Integer>> drops = new HashMap<Material, List<Integer>>();
2022-01-29 13:01:49 +00:00
@Override
public void onEnable() {
2022-02-03 18:58:54 +00:00
metrics.addCustomChart(new SimplePie("ram_allocation", () -> {
2022-02-06 17:32:53 +00:00
return Long.toString(Runtime.getRuntime().maxMemory());
2022-02-03 18:58:54 +00:00
}));
2022-01-29 13:01:49 +00:00
if (!(configFile.exists())) {
saveResource("config.yml", false);
}
config = YamlConfiguration.loadConfiguration(configFile);
ai = config.getBoolean("ai", true);
chance = config.getDouble("chance", 0.02D);
attackDamage = config.getDouble("attackDamage", 1.0D);
attackReach = config.getDouble("attackReach", 2D);
expandUp = config.getDouble("expandUp", 0D);
2022-01-29 16:56:10 +00:00
for (Object s : config.getList("effects", new ArrayList<Object>())) {
2022-01-29 13:01:49 +00:00
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);
2022-02-03 11:58:15 +00:00
} catch (Exception e) {
Bukkit.getLogger().severe("Exception while parsing " + s);
2022-01-29 13:01:49 +00:00
e.printStackTrace();
continue;
}
}
2022-02-03 11:58:10 +00:00
for (Object s : config.getList("drops", new ArrayList<Object>())) {
try {
String[] parts = ((String) s).split(":");
/*
TODO find a better way to do this
if (!(StringUtils.isNumeric(parts[1]))) {
throw new IllegalArgumentException(parts[1] + " is not a number");
}
*/
Material material = Material.getMaterial(parts[0]);
if (material == null) {
throw new IllegalArgumentException(parts[0] + " is not a Material");
}
List<Integer> data = new ArrayList<Integer>();
2022-02-03 11:58:15 +00:00
data.add(Integer.parseInt(parts[1]));
data.add(Integer.parseInt(parts[2]));
data.add(Integer.parseInt(parts[3]));
2022-02-03 11:58:10 +00:00
drops.put(material, data);
} catch (Exception e) {
Bukkit.getLogger().severe("Exception while parsing " + s);
e.printStackTrace();
continue;
}
}
2022-01-29 13:01:49 +00:00
getServer().getPluginManager().registerEvents(this, this);
2022-02-03 18:27:51 +00:00
getCommand("spawngiant").setExecutor(this);
2022-01-29 14:10:18 +00:00
new BukkitRunnable() {
@Override
public void run() {
2022-01-29 17:32:59 +00:00
for (Entity e : giants) {
if (!(e.isValid())) continue;
Collection<Entity> nearby = e.getWorld().getNearbyEntities(e.getBoundingBox().expand(
attackReach, expandUp, attackReach), n -> (n instanceof Player));
2022-01-29 14:33:54 +00:00
for (Entity p : nearby) {
((LivingEntity) p).damage(attackDamage, e);
2022-01-29 14:10:18 +00:00
}
}
}
}.runTaskTimer(this, config.getLong("hitDelay", 20L), 0L);
2022-01-29 15:19:04 +00:00
if (headRotations) {
new BukkitRunnable() {
@Override
public void run() {
for (Entity e : giants) {
List<Entity> 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());
}
2022-01-29 14:36:32 +00:00
}
2022-01-29 14:33:54 +00:00
}
2022-01-29 15:19:04 +00:00
}.runTaskTimerAsynchronously(this, refreshDelay, 0L);
}
2022-01-29 13:01:49 +00:00
}
2022-02-03 18:27:51 +00:00
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (!(sender instanceof Player)) return true;
spawnGiant(true, ((Player) sender).getLocation());
return true;
}
2022-01-29 13:01:49 +00:00
@EventHandler
public void entitySpawn(EntitySpawnEvent e) {
if (e.getEntityType() == EntityType.ZOMBIE) {
if (rnd.nextDouble() <= chance) {
e.setCancelled(true);
spawnGiant(ai, e.getLocation());
}
}
}
2022-01-29 17:32:59 +00:00
@EventHandler
public void entityDamage(EntityDamageByEntityEvent e) {
LivingEntity entity = (LivingEntity) e.getDamager();
if (entity.hasMetadata("giant")) {
entity.setInvulnerable(false);
entity.setHealth(0);
}
}
2022-01-29 16:37:37 +00:00
@EventHandler
2022-01-29 17:05:54 +00:00
public void entityDeath(EntityDeathEvent e) {
2022-01-29 17:32:59 +00:00
LivingEntity entity = e.getEntity();
2022-01-29 17:05:54 +00:00
if (entity.getType() == EntityType.GIANT) {
2022-01-29 17:32:59 +00:00
giants.remove(entity);
2022-02-03 12:16:35 +00:00
for (Entry<Material, List<Integer>> d : drops.entrySet()) {
int max = d.getValue().get(0);
int min = d.getValue().get(1);
ItemStack is = new ItemStack(d.getKey(), rnd.nextInt((max - min) + 1) + min);
if (rnd.nextInt(100) < d.getValue().get(2)) {
entity.getWorld().dropItemNaturally(entity.getLocation(), is);
}
}
2022-01-29 17:05:54 +00:00
for (Entity p : entity.getPassengers()) ((LivingEntity) p).setHealth(0);
2022-01-29 16:37:37 +00:00
}
}
2022-01-29 17:32:59 +00:00
LivingEntity spawnGiant(boolean ai, Location pos) {
2022-01-29 13:01:49 +00:00
LivingEntity entity = (LivingEntity) pos.getWorld().spawnEntity(pos, EntityType.GIANT);
if (ai) {
2022-01-29 15:09:32 +00:00
LivingEntity passenger = (LivingEntity) pos.getWorld().spawnEntity(pos, EntityType.HUSK);
2022-01-29 16:22:18 +00:00
new PotionEffect(PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 1)
.apply((LivingEntity) passenger);
2022-01-29 14:53:58 +00:00
passenger.setCustomName("Giant");
passenger.setCustomNameVisible(false);
2022-01-29 16:22:18 +00:00
passenger.setInvulnerable(true);
passenger.setPersistent(true);
2022-01-29 16:53:44 +00:00
passenger.setMetadata("giant", new FixedMetadataValue(this, "y"));
2022-01-29 14:10:18 +00:00
entity.addPassenger(passenger);
2022-01-29 13:01:49 +00:00
}
for (Entry<PotionEffectType, Integer> t : effects.entrySet()) {
PotionEffect effect = new PotionEffect(t.getKey(), Integer.MAX_VALUE, t.getValue());
effect.apply(entity);
}
2022-01-29 14:33:54 +00:00
giants.add(entity);
2022-01-29 14:10:18 +00:00
return entity;
2022-01-29 13:01:49 +00:00
}
}