EpicEnchants/src/main/java/com/songoda/epicenchants/effect/effects/SpawnMob.java

103 lines
5.3 KiB
Java
Raw Normal View History

2019-04-08 14:49:08 +02:00
package com.songoda.epicenchants.effect.effects;
2019-01-16 13:01:24 +01:00
2019-04-08 14:49:08 +02:00
import com.songoda.epicenchants.effect.EffectExecutor;
import com.songoda.epicenchants.enums.EventType;
2019-01-17 17:11:59 +01:00
import com.songoda.epicenchants.objects.LeveledModifier;
2019-02-19 14:23:20 +01:00
import com.songoda.epicenchants.utils.objects.ItemBuilder;
2019-01-17 00:16:30 +01:00
import org.bukkit.Location;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
2019-04-08 14:49:08 +02:00
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Monster;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
2019-01-16 13:01:24 +01:00
2019-04-08 14:49:08 +02:00
import static com.songoda.epicenchants.objects.LeveledModifier.of;
import static com.songoda.epicenchants.utils.single.GeneralUtils.color;
2019-01-17 00:16:30 +01:00
import static java.util.concurrent.ThreadLocalRandom.current;
2019-04-08 14:49:08 +02:00
public class SpawnMob extends EffectExecutor {
private LeveledModifier attackDamage;
2019-01-17 00:16:30 +01:00
private String displayName;
2019-01-16 13:01:24 +01:00
private EntityType entityType;
private LeveledModifier equipmentDropChance;
private LeveledModifier health;
private ItemBuilder helmet, chestPlate, leggings, boots, handItem;
2019-01-16 13:01:24 +01:00
private boolean hostile;
2019-04-08 15:37:46 +02:00
private LeveledModifier amount;
2019-01-17 00:16:30 +01:00
2019-04-08 14:49:08 +02:00
public SpawnMob(ConfigurationSection section) {
super(section);
2019-04-08 15:37:46 +02:00
entityType = EntityType.valueOf(section.getString("mob-type"));
amount = of(section.getString("amount"));
2019-04-08 14:49:08 +02:00
health = of(section.getString("health"));
attackDamage = of(section.getString("attack-damage"));
equipmentDropChance = LeveledModifier.of(section.getString("equipment-drop-chance"));
hostile = section.getBoolean("hostile", false);
displayName = section.isString("display-name") ? color(section.getString("display-name")) : "";
helmet = section.isConfigurationSection("equipment.helmet") ? new ItemBuilder(section.getConfigurationSection("equipment.helmet")) : null;
chestPlate = section.isConfigurationSection("equipment.chestplate") ? new ItemBuilder(section.getConfigurationSection("equipment.chestplate")) : null;
leggings = section.isConfigurationSection("equipment.leggings") ? new ItemBuilder(section.getConfigurationSection("equipment.leggings")) : null;
boots = section.isConfigurationSection("equipment.boots") ? new ItemBuilder(section.getConfigurationSection("equipment.boots")) : null;
handItem = section.isConfigurationSection("equipment.hand-item") ? new ItemBuilder(section.getConfigurationSection("equipment.hand-item")) : null;
}
2019-04-08 14:49:08 +02:00
@Override
public void execute(@NotNull Player user, @Nullable LivingEntity opponent, int level, EventType eventType) {
Location location = user.getLocation();
2019-04-08 15:37:46 +02:00
for (int i = 0; i < amount.get(level, 1, user, opponent); i++) {
2019-01-17 00:16:30 +01:00
Location spawnLocation = location.clone().add(current().nextInt(-3, 3), 0, current().nextInt(-3, 3));
int y = location.getWorld().getHighestBlockAt(spawnLocation).getY();
if (y < location.getY() - 10 || y > location.getY() + 10) {
continue;
}
Entity entity = location.getWorld().spawnEntity(spawnLocation, entityType);
2019-01-17 00:16:30 +01:00
entity.setCustomName(displayName.replace("{level}", "" + level));
2019-01-17 00:16:30 +01:00
entity.setCustomNameVisible(true);
if (entity instanceof LivingEntity) {
LivingEntity livingEntity = (LivingEntity) entity;
2019-04-08 15:37:46 +02:00
livingEntity.setRemoveWhenFarAway(true);
int dropChance = (int) equipmentDropChance.get(level, 0, user, opponent);
if (helmet != null)
livingEntity.getEquipment().setHelmet(helmet.buildWithWrappers(level, user, opponent));
if (chestPlate != null)
livingEntity.getEquipment().setChestplate(chestPlate.buildWithWrappers(level, user, opponent));
if (leggings != null)
livingEntity.getEquipment().setLeggings(leggings.buildWithWrappers(level, user, opponent));
if (boots != null) livingEntity.getEquipment().setBoots(boots.buildWithWrappers(level, user, opponent));
livingEntity.getEquipment().setHelmetDropChance(dropChance);
livingEntity.getEquipment().setLeggingsDropChance(dropChance);
livingEntity.getEquipment().setHelmetDropChance(dropChance);
livingEntity.getEquipment().setChestplateDropChance(dropChance);
if (handItem != null)
livingEntity.getEquipment().setItemInHand(handItem.buildWithWrappers(level, user, opponent));
2019-03-21 16:00:31 +01:00
livingEntity.getEquipment().setItemInHandDropChance(dropChance);
}
2019-04-08 14:49:08 +02:00
if (hostile && entity instanceof Monster && opponent != null) {
((Monster) entity).setTarget(opponent);
}
if (entity instanceof LivingEntity) {
AttributeInstance attack = ((LivingEntity) entity).getAttribute(Attribute.GENERIC_ATTACK_DAMAGE);
attack.setBaseValue(attackDamage.get(level, (int) Math.round(attack.getBaseValue()), user, opponent));
AttributeInstance heal = ((LivingEntity) entity).getAttribute(Attribute.GENERIC_MAX_HEALTH);
heal.setBaseValue(health.get(level, (int) Math.round(heal.getBaseValue()), user, opponent));
}
2019-01-17 00:16:30 +01:00
}
}
2019-01-16 13:01:24 +01:00
}