Last active
August 29, 2015 14:04
-
-
Save truelecter/348748d4341528215fc5 to your computer and use it in GitHub Desktop.
argsMobs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package truelecter.ma; | |
import static com.sk89q.worldguard.bukkit.BukkitUtil.toVector; | |
import java.util.ArrayList; | |
import java.util.Date; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Random; | |
import org.bukkit.ChatColor; | |
import org.bukkit.Location; | |
import org.bukkit.Material; | |
import org.bukkit.command.Command; | |
import org.bukkit.command.CommandSender; | |
import org.bukkit.enchantments.Enchantment; | |
import org.bukkit.entity.Entity; | |
import org.bukkit.entity.EntityType; | |
import org.bukkit.entity.Player; | |
import org.bukkit.entity.Skeleton; | |
import org.bukkit.entity.Zombie; | |
import org.bukkit.event.EventHandler; | |
import org.bukkit.event.Listener; | |
import org.bukkit.event.entity.EntityDeathEvent; | |
import org.bukkit.event.player.PlayerMoveEvent; | |
import org.bukkit.inventory.ItemStack; | |
import org.bukkit.plugin.java.JavaPlugin; | |
import org.bukkit.potion.PotionEffect; | |
import org.bukkit.potion.PotionEffectType; | |
import com.sk89q.worldedit.Vector; | |
import com.sk89q.worldguard.bukkit.WorldGuardPlugin; | |
import com.sk89q.worldguard.protection.ApplicableRegionSet; | |
import com.sk89q.worldguard.protection.managers.RegionManager; | |
import com.sk89q.worldguard.protection.regions.ProtectedRegion; | |
public class Base extends JavaPlugin implements Listener { | |
private WorldGuardPlugin wg; | |
private String ANSI_RED = "\u001B[31m"; | |
private String ANSI_RESET = "\u001B[0m"; | |
private String ANSI_GREEN = "\u001B[32m"; | |
private HashMap<String, Integer> players = new HashMap<String, Integer>(); | |
private HashMap<Integer, String> mobs = new HashMap<Integer, String>(); | |
private HashMap<String, Long> timeStamps = new HashMap<String, Long>(); | |
private ArrayList<String> regs = new ArrayList<String>(); | |
private int minLevel = 5; | |
private int diamondSwordLevel = 50; | |
private int maxMobsPerPlayer = 10; | |
private int maxInWave = 4; | |
private Random r = new Random(); | |
private int dropFreq = 30; | |
private int radius = 10; | |
private ItemStack[] helmets = { new ItemStack(Material.AIR), new ItemStack(Material.LEATHER_HELMET), | |
new ItemStack(Material.GOLD_HELMET), new ItemStack(Material.IRON_HELMET), | |
new ItemStack(Material.CHAINMAIL_HELMET), new ItemStack(Material.DIAMOND_HELMET) }; | |
private ItemStack[] chests = { new ItemStack(Material.AIR), new ItemStack(Material.LEATHER_CHESTPLATE), | |
new ItemStack(Material.GOLD_CHESTPLATE), new ItemStack(Material.IRON_CHESTPLATE), | |
new ItemStack(Material.CHAINMAIL_CHESTPLATE), new ItemStack(Material.DIAMOND_CHESTPLATE) }; | |
private ItemStack[] pants = { new ItemStack(Material.AIR), new ItemStack(Material.LEATHER_LEGGINGS), | |
new ItemStack(Material.GOLD_LEGGINGS), new ItemStack(Material.IRON_LEGGINGS), | |
new ItemStack(Material.CHAINMAIL_LEGGINGS), new ItemStack(Material.DIAMOND_LEGGINGS) }; | |
private ItemStack[] boots = { new ItemStack(Material.AIR), new ItemStack(Material.LEATHER_BOOTS), | |
new ItemStack(Material.GOLD_BOOTS), new ItemStack(Material.IRON_BOOTS), | |
new ItemStack(Material.CHAINMAIL_BOOTS), new ItemStack(Material.DIAMOND_BOOTS) }; | |
private int CD = 15000; | |
private int rndByDiv(int lvl) { | |
int a = r.nextInt(lvl) % 15; | |
if (a > 5) { | |
return 5; | |
} | |
if (a < 1) { | |
return 0; | |
} | |
return a; | |
} | |
private ItemStack[] getEquipment(int level) { | |
ItemStack[] items = new ItemStack[5]; | |
items[1] = helmets[rndByDiv(level)].clone(); | |
items[2] = chests[rndByDiv(level)].clone(); | |
items[3] = pants[rndByDiv(level)].clone(); | |
items[4] = boots[rndByDiv(level)].clone(); | |
if (level > diamondSwordLevel && r.nextBoolean()) { | |
items[5] = new ItemStack(Material.DIAMOND_SWORD); | |
} | |
return items; | |
} | |
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { | |
if (cmd.getName().equalsIgnoreCase("addregs")) { | |
if (!sender.hasPermission("agrMobs.addregs")) { | |
sender.sendMessage(ChatColor.RED + "Нет прав."); | |
return true; | |
} | |
if (args.length > 0) { | |
for (int i = 0; i < args.length; i++) { | |
regs.add(args[i]); | |
} | |
getConfig().set("regions.list", regs); | |
saveConfig(); | |
sender.sendMessage(ChatColor.GREEN + "Регионы добавлены."); | |
return true; | |
} else { | |
sender.sendMessage(ChatColor.RED + "Нужно указать регионы!"); | |
return true; | |
} | |
} | |
return false; | |
} | |
public boolean isWithinRegion(Location loc, String region) { | |
Vector v = toVector(loc); | |
RegionManager manager = wg.getRegionManager(loc.getWorld()); | |
ApplicableRegionSet set = manager.getApplicableRegions(v); | |
for (ProtectedRegion each : set) | |
if (each.getId().equalsIgnoreCase(region)) | |
return true; | |
return false; | |
} | |
@SuppressWarnings("unchecked") | |
public void onEnable() { | |
wg = (WorldGuardPlugin) this.getServer().getPluginManager().getPlugin("WorldGuard"); | |
if (wg == null) { | |
System.out.println("[agrMobs]" + ANSI_RED + " WorldGuard Не найден." + ANSI_RESET); | |
this.getServer().getPluginManager().disablePlugin(this); | |
return; | |
} | |
regs = (ArrayList<String>) getConfig().get("regions.list", new ArrayList<String>()); | |
minLevel = getConfig().getInt("armour.minLevel", 5); | |
dropFreq = getConfig().getInt("armour.dropFrequency", 30); | |
maxMobsPerPlayer = getConfig().getInt("mobs.max", 10); | |
maxInWave = getConfig().getInt("mobs.maxInWave", 10); | |
System.out.println(ANSI_GREEN + " Loaded!" + ANSI_RESET); | |
} | |
@EventHandler | |
public void onEntityDeath(EntityDeathEvent e){ | |
if (players.containsValue(e)){ | |
players.remove(mobs.get(e)); | |
} | |
if (mobs.containsKey(e)){ | |
mobs.remove(e); | |
} | |
} | |
public boolean spawnHorde(){ | |
int a = r.nextInt(100); | |
if (a == 42){ | |
return true; | |
} | |
return false; | |
} | |
@EventHandler | |
public void onPlayerMove(PlayerMoveEvent e) { | |
if (e.getPlayer().getLevel() < minLevel) { | |
return; | |
} | |
Date date = new Date(); | |
if (date.getTime() - timeStamps.get(e.getPlayer().getName()) < CD) { | |
return; | |
} | |
boolean s = false; | |
for (int i = 0; i < regs.size(); i++) { | |
if (isWithinRegion(e.getTo(), regs.get(i))) { | |
s = true; | |
break; | |
} | |
} | |
if (!s) { | |
return; | |
} | |
double radiusSquared = radius * radius; | |
List<Entity> entities = e.getPlayer().getNearbyEntities(radius, 10, radius); | |
for (Entity entity : entities) { | |
int num = 0; | |
if (entity.getLocation().distanceSquared(e.getPlayer().getLocation()) > radiusSquared) | |
continue; | |
if ((entity instanceof Zombie) || (entity instanceof Skeleton)) { | |
if (mobs.containsKey(entity.getEntityId()) | |
&& mobs.get(entity.getEntityId()).equalsIgnoreCase(e.getPlayer().getName())) { | |
num++; | |
if (num > maxMobsPerPlayer) { | |
return; | |
} | |
} | |
} | |
} | |
int num = r.nextInt(maxInWave); | |
if (num < 1) { | |
num = 1; | |
} | |
if (!spawnHorde()){ | |
return; | |
} | |
for (int i = 0; i < num; i++) { | |
if (r.nextBoolean()) { | |
spawnZombie(e.getPlayer().getLocation(), e.getPlayer()); | |
} else { | |
spawnSkeleton(e.getPlayer().getLocation(), e.getPlayer()); | |
} | |
} | |
//TODO Тут идет оповещение о спавне орды мобов | |
e.getPlayer().sendMessage(ChatColor.RED+"Внимание!"+ChatColor.AQUA+"Возле вас заспавнилась орда мобов"); | |
} | |
private Location getRandomLocation(Location loc) { | |
double angle = Math.random() * Math.PI * 2; | |
double x = Math.cos(angle) * radius; | |
double z = Math.sin(angle) * radius; | |
return loc.clone().add(x, loc.getWorld().getHighestBlockYAt((int) x, (int) z), z); | |
} | |
private void spawnSkeleton(Location loc, Player player) { | |
Skeleton s = (Skeleton) loc.getWorld().spawnEntity(getRandomLocation(loc), EntityType.SKELETON); | |
s.getEquipment().setArmorContents(getEquipment(player.getLevel())); | |
ItemStack bow = new ItemStack(Material.BOW); | |
switch (r.nextInt(2)) { | |
case 1: | |
bow.addEnchantment(Enchantment.ARROW_DAMAGE, (int) (r.nextInt(player.getLevel()) / 15)); | |
case 2: | |
bow.addEnchantment(Enchantment.ARROW_KNOCKBACK, (int) (r.nextInt(player.getLevel()) / 15)); | |
} | |
switch (r.nextInt(2)) { | |
case 1: | |
bow.addEnchantment(Enchantment.ARROW_DAMAGE, (int) (r.nextInt(player.getLevel()) / 15)); | |
case 2: | |
bow.addEnchantment(Enchantment.ARROW_KNOCKBACK, (int) (r.nextInt(player.getLevel()) / 15)); | |
} | |
s.setTarget(player); | |
s.getEquipment().setItemInHand(bow); | |
s.getEquipment().setBootsDropChance(dropFreq); | |
s.getEquipment().setChestplateDropChance(dropFreq); | |
s.getEquipment().setLeggingsDropChance(dropFreq); | |
s.getEquipment().setHelmetDropChance(dropFreq); | |
s.getEquipment().setItemInHandDropChance(dropFreq); | |
players.put(player.getName(), s.getEntityId()); | |
mobs.put(s.getEntityId(), player.getName()); | |
} | |
private void spawnZombie(Location loc, Player player) { | |
Zombie s = (Zombie) loc.getWorld().spawnEntity(getRandomLocation(loc), EntityType.ZOMBIE); | |
s.getEquipment().setArmorContents(getEquipment(player.getLevel())); | |
s.getEquipment().setItemInHand(r.nextBoolean() ? new ItemStack(Material.DIAMOND_SWORD) : null); | |
if (r.nextBoolean()){ | |
s.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 18000, 1)); | |
} | |
if (r.nextBoolean()){ | |
s.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 18000, 1)); | |
} | |
s.setTarget(player); | |
s.getEquipment().setBootsDropChance(dropFreq); | |
s.getEquipment().setChestplateDropChance(dropFreq); | |
s.getEquipment().setLeggingsDropChance(dropFreq); | |
s.getEquipment().setHelmetDropChance(dropFreq); | |
s.getEquipment().setItemInHandDropChance(dropFreq); | |
players.put(player.getName(), s.getEntityId()); | |
mobs.put(s.getEntityId(), player.getName()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment