Skip to content

Instantly share code, notes, and snippets.

@lululombard
Created June 7, 2018 21:32
Show Gist options
  • Save lululombard/717781a705c6aea896cece1db18fdf0c to your computer and use it in GitHub Desktop.
Save lululombard/717781a705c6aea896cece1db18fdf0c to your computer and use it in GitHub Desktop.
Source of KingHardcore
package com.lululombard.kinghardcore;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.ConcurrentModificationException;
import java.util.List;
import java.util.Random;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Difficulty;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.block.Biome;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Slime;
import org.bukkit.entity.Witch;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.player.PlayerInteractAtEntityEvent;
import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.event.world.ChunkUnloadEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.potion.PotionEffectType;
public class MoreMobs implements Listener, Runnable {
public List<World> hcworlds;
public List<EntityType> allmobs = new ArrayList<EntityType>();
public EntityType[] monsters_overworld = new EntityType[] {
EntityType.CREEPER,
EntityType.SKELETON,
EntityType.ZOMBIE,
EntityType.WITCH,
EntityType.ENDERMAN,
EntityType.ZOMBIE,
EntityType.SLIME,
};
public EntityType[] monsters_nether = new EntityType[] {
EntityType.BLAZE,
EntityType.MAGMA_CUBE,
EntityType.PIG_ZOMBIE
};
private KingHardcore kh;
public MoreMobs(List<World> hcworlds) {
this.hcworlds = hcworlds;
this.kh = (KingHardcore) Bukkit.getPluginManager().getPlugin("KingHardcore");
Bukkit.getPluginManager().registerEvents(this, kh);
Bukkit.getScheduler().runTaskTimerAsynchronously(kh, this, 100, 100);
allmobs.addAll(Arrays.asList(monsters_overworld));
allmobs.addAll(Arrays.asList(monsters_nether));
}
@EventHandler
public void onCreatureSpawn(CreatureSpawnEvent e) {
if (!e.isCancelled() && allmobs.contains(e.getEntityType())) {
Location loc = e.getLocation();
if (loc.getBlock().getLightLevel() > 10) e.setCancelled(true);
String mobInfo = loc.getBlockX() + " ; " + loc.getBlockY() + " ; " + loc.getBlockZ() + " -> " + loc.getBlock().getLightLevel() + " (" + e.getSpawnReason().name() + ")";
e.getEntity().setMetadata(kh.getName(), new FixedMetadataValue(kh, mobInfo));
}
}
@EventHandler
public void onPlayerInteract(PlayerInteractAtEntityEvent e) {
if (!e.getPlayer().hasPermission("hardcore.debug") || !e.getHand().equals(EquipmentSlot.HAND)) return;
if (Arrays.asList(monsters_overworld).contains(e.getRightClicked().getType())) {
try {
String mobInfo = e.getRightClicked().getMetadata(kh.getName()).get(0).asString();
e.getPlayer().sendMessage(mobInfo);
} catch (ArrayIndexOutOfBoundsException ex) {}
}
}
@EventHandler
public void onChunkLoad(final ChunkLoadEvent e) {
if (e.getWorld().getDifficulty().equals(Difficulty.PEACEFUL)) return;
Bukkit.getServer().getScheduler().runTaskLater(kh, new Runnable() {public void run() {
populateMobs(e.getChunk());
}}, 20);
}
@EventHandler
public void onChunkUnload(ChunkUnloadEvent e) {
if (hcworlds.contains(e.getWorld())) {
for (Entity ent : e.getChunk().getEntities()) {
if (allmobs.contains(ent.getType())) {
ent.remove();
}
}
}
}
@EventHandler
public void onAttackOtherEntities(EntityDamageByEntityEvent e) {
if (hcworlds.contains(e.getEntity().getWorld()) && allmobs.contains(e.getDamager().getType()) && allmobs.contains(e.getEntity().getType())) {
e.setCancelled(true);
}
}
@EventHandler
public void onSlimeAttack(EntityDamageByEntityEvent e) {
if (hcworlds.contains(e.getEntity().getWorld()) && e.getDamager() instanceof Slime && e.getEntity() instanceof Player) {
Player p = (Player) e.getEntity();
if (p.getFoodLevel() > 0) {
float damage = Math.round(e.getDamage()*1.5);
e.setDamage(e.getDamage()/10);
if (p.getFoodLevel() < damage) p.setFoodLevel(0);
else p.setFoodLevel(Math.round(p.getFoodLevel()-damage));
}
}
}
@Override
public void run() {
for (Player p : Bukkit.getServer().getOnlinePlayers()) {
if (p.getWorld().getEnvironment().equals(Environment.NORMAL) && hcworlds.contains(p.getWorld())) {
int mobcount = 0;
for (Entity e : p.getNearbyEntities(40, 20, 40)) {
if (Arrays.asList(monsters_overworld).contains(e.getType())) mobcount++;
}
if (mobcount < 15) {
for (Chunk c : p.getWorld().getLoadedChunks()) {
if (c.getBlock(8, 100, 8).getLocation().distance(p.getLocation()) <= 128) {
populateMobs(c);
}
}
}
}
}
for (World w : hcworlds) {
try {
for (Entity ent : w.getEntities()) {
if (ent instanceof LivingEntity) {
LivingEntity e = (LivingEntity) ent;
if (Arrays.asList(monsters_overworld).contains(e.getType()) || Arrays.asList(monsters_nether).contains(e.getType())) {
boolean willBeDeleted = true;
if (!e.isGlowing()) for (Entity et : e.getNearbyEntities(80, 60, 80)) if (et.getType().equals(EntityType.PLAYER)) willBeDeleted = false;
if (willBeDeleted) e.remove();
}
if (e.getType().equals(EntityType.WITCH)) {
if (e.hasMetadata(kh.getName() + "_regen") && e.hasPotionEffect(PotionEffectType.REGENERATION)) {
e.getActivePotionEffects().clear();
if (Utils.isDay(e.getWorld()) && e.getLocation().getBlock().getLightLevel() > 12) e.setFireTicks(80);
}
}
}
}
} catch (ConcurrentModificationException e) {}
}
}
public void populateMobs(final Chunk c) {
if (!c.getWorld().getEnvironment().equals(Environment.THE_END)) {
final List<Entity> chunkEntities = new ArrayList<Entity>();
Bukkit.getScheduler().runTask(kh, new Runnable() {public void run() {
for (Entity e : c.getEntities()) {
if (Arrays.asList(monsters_overworld).contains(e.getType())) chunkEntities.add(e);
}
}});
if (chunkEntities.size() > 2) return;
for (int i = 0; i < new Random().nextInt(2); i++) {
Location loc = c.getBlock(new Random().nextInt(15), 0, new Random().nextInt(15)).getLocation();
List<Block> possiblespots = new ArrayList<Block>();
for (int y = 1; y <= 255; y++) {
loc.add(0, y, 0);
Block b = loc.getBlock();
if (b != null && b.getType().equals(Material.AIR)) {
Block under = loc.clone().add(0, -1, 0).getBlock();
Block above = loc.clone().add(0, 1, 0).getBlock();
if ((under != null && !(b.getBiome().equals(Biome.MUSHROOM_ISLAND) || b.getBiome().equals(Biome.MUSHROOM_ISLAND_SHORE)) && !under.getType().equals(Material.AIR) && under.getType().isOccluding() && (b.getLightLevel() < 8 || (hcworlds.contains(b.getWorld()) && b.getY() < 30 && b.getLightLevel() < 10)) && (above == null || above.getType().equals(Material.AIR)))) {
boolean playerCanSee = false;
for (Player p : c.getWorld().getPlayers()) {
if (p.getLocation().distance(b.getLocation()) < 30) {
playerCanSee = true;
break;
}
}
if (!playerCanSee) possiblespots.add(b);
}
}
}
if (possiblespots.size() >= 1) {
final Location random_loc;
if (possiblespots.size() > 1) random_loc = possiblespots.get(new Random().nextInt(possiblespots.size()-1)).getLocation();
else random_loc = possiblespots.get(0).getLocation();
final EntityType[] mobs = (random_loc.getWorld().getEnvironment().equals(Environment.NORMAL) ? monsters_overworld : monsters_nether);
if (new Random().nextInt(5) <= 1 && random_loc.getY() < 25 && Utils.isSlimeChunk(random_loc.getChunk())) {
Bukkit.getScheduler().runTask(kh, new Runnable() {public void run() {
for (int count = 0; count <= new Random().nextInt(3); count++) {
LivingEntity e = (LivingEntity) random_loc.getWorld().spawnEntity(random_loc.clone().add(count + 0.5, 0, 0.5), EntityType.SLIME);
if (!e.getLocation().getBlock().getType().equals(Material.AIR) && !e.getLocation().add(0, 1, 0).getBlock().getType().equals(Material.AIR)) e.remove();
else {
boolean remove = false;
for (Entity near : e.getNearbyEntities(20, 20, 20)) {
if (near.getType().equals(e.getType())) remove = true;
}
if (remove) e.remove();
else e.setCanPickupItems(false);
}
}
}});
}
else {
Bukkit.getScheduler().runTask(kh, new Runnable() {public void run() {
LivingEntity e = (LivingEntity) random_loc.getWorld().spawnEntity(random_loc.add(0.5, 0, 0.5), mobs[new Random().nextInt(mobs.length-1)]);
e.setCanPickupItems(false);
if (e.getType().equals(EntityType.WITCH)) {
Witch w = (Witch) e;
w.setHealth(5);
w.setMetadata(kh.getName() + "_regen", new FixedMetadataValue(kh, "false"));
}
if (e.getType().equals(EntityType.ENDERMAN)) {
if (Utils.getHeadroom(random_loc) < 3) e.remove();
}
}});
}
}
}
}
Bukkit.getScheduler().runTask(kh, new Runnable() {public void run() {
for (Entity e : c.getEntities()) {
if (Arrays.asList(monsters_overworld).contains(e.getType()) || Arrays.asList(monsters_nether).contains(e.getType()) && e.getCustomName() == null) {
boolean tooFar = true;
for (Player p : c.getWorld().getPlayers()) {
if (p.getLocation().distance(e.getLocation()) < 75) {
tooFar = false;
break;
}
}
if (tooFar) e.remove();
}
}
}});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment