Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityRemoveEvent;
import org.bukkit.event.entity.EntityDismountEvent;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.inventory.InventoryPickupItemEvent;

import com.nisovin.magicspells.Perm;
import com.nisovin.magicspells.Spell;
import com.nisovin.magicspells.util.Util;
import com.nisovin.magicspells.MagicSpells;
import com.nisovin.magicspells.util.EntityData;
import com.nisovin.magicspells.events.SpellTargetEvent;
import com.nisovin.magicspells.zones.NoMagicZoneManager;
import com.nisovin.magicspells.spelleffects.effecttypes.*;
Expand Down Expand Up @@ -59,6 +64,26 @@ public void onEntityDamage(EntityDamageEvent event) {
event.setCancelled(true);
}

@EventHandler
public void onEntityRemove(EntityRemoveEvent event) {
Util.forEachPassenger(event.getEntity(), passenger -> {
PersistentDataContainer container = passenger.getPersistentDataContainer();
if (!container.has(EntityData.MS_PASSENGER)) return;

if (passenger.isPersistent()) {
container.remove(EntityData.MS_PASSENGER);
return;
}

passenger.remove();
});
}

@EventHandler
public void onEntityDismount(EntityDismountEvent event) {
event.getEntity().getPersistentDataContainer().remove(EntityData.MS_PASSENGER);
}

private boolean isMSEntity(Entity entity) {
return entity.getScoreboardTags().contains(ArmorStandEffect.ENTITY_TAG) || entity.getScoreboardTags().contains(EntityEffect.ENTITY_TAG);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.bukkit.Location;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.configuration.ConfigurationSection;
Expand All @@ -11,9 +10,11 @@
import com.nisovin.magicspells.util.Util;
import com.nisovin.magicspells.util.SpellData;
import com.nisovin.magicspells.util.EntityData;
import com.nisovin.magicspells.util.config.ConfigData;
import com.nisovin.magicspells.spelleffects.SpellEffect;
import com.nisovin.magicspells.util.magicitems.MagicItem;
import com.nisovin.magicspells.util.magicitems.MagicItems;
import com.nisovin.magicspells.util.config.ConfigDataUtil;

@Name("armorstand")
public class ArmorStandEffect extends SpellEffect {
Expand All @@ -22,56 +23,51 @@ public class ArmorStandEffect extends SpellEffect {

private EntityData entityData;

private boolean gravity;

private String customName;
private boolean customNameVisible;
private ConfigData<Boolean> gravity;
private ConfigData<Boolean> disableSlots;

private ItemStack headItem;
private ItemStack mainhandItem;
private ItemStack offhandItem;
private ItemStack offHandItem;
private ItemStack mainHandItem;

@Override
protected void loadFromConfig(ConfigurationSection config) {
ConfigurationSection section = config.getConfigurationSection("armorstand");
if (section == null) return;

entityData = new EntityData(section);
entityData.setEntityType(data -> EntityType.ARMOR_STAND);

gravity = section.getBoolean("gravity", false);

customName = section.getString("custom-name", "");
customNameVisible = section.getBoolean("custom-name-visible", false);

String strMagicItem = section.getString("head", "");
MagicItem magicItem = MagicItems.getMagicItemFromString(strMagicItem);
if (magicItem != null) headItem = magicItem.getItemStack();
gravity = ConfigDataUtil.getBoolean(section, "gravity", false);
disableSlots = ConfigDataUtil.getBoolean(section, "disable-slots", true);

strMagicItem = section.getString("mainhand", "");
magicItem = MagicItems.getMagicItemFromString(strMagicItem);
if (magicItem != null) mainhandItem = magicItem.getItemStack();
MagicItem item = MagicItems.getMagicItemFromString(section.getString("head"));
if (item != null) headItem = item.getItemStack();

strMagicItem = section.getString("offhand", "");
magicItem = MagicItems.getMagicItemFromString(strMagicItem);
if (magicItem != null) offhandItem = magicItem.getItemStack();
item = MagicItems.getMagicItemFromString(section.getString("offhand"));
if (item != null) offHandItem = item.getItemStack();

item = MagicItems.getMagicItemFromString(section.getString("mainhand"));
if (item != null) mainHandItem = item.getItemStack();
}

@Override
protected ArmorStand playArmorStandEffectLocation(Location location, SpellData data) {
return (ArmorStand) entityData.spawn(location, data, entity -> {
ArmorStand armorStand = (ArmorStand) entity;

armorStand.addScoreboardTag(ENTITY_TAG);
armorStand.setGravity(gravity);
armorStand.setSilent(true);
armorStand.customName(Util.getMiniMessage(customName, data));
armorStand.setCustomNameVisible(customNameVisible);

armorStand.setItem(EquipmentSlot.HEAD, headItem);
armorStand.setItem(EquipmentSlot.HAND, mainhandItem);
armorStand.setItem(EquipmentSlot.OFF_HAND, offhandItem);
boolean gravity = this.gravity.get(data);
boolean disableSlots = this.disableSlots.get(data);

return entityData.spawn(location, data, ArmorStand.class, stand -> {
stand.setSilent(true);
stand.addScoreboardTag(ENTITY_TAG);

stand.setGravity(gravity);
if (disableSlots) stand.setDisabledSlots(EquipmentSlot.values());

stand.setItem(EquipmentSlot.HEAD, headItem);
stand.setItem(EquipmentSlot.HAND, mainHandItem);
stand.setItem(EquipmentSlot.OFF_HAND, offHandItem);
}, stand -> {
stand.setPersistent(false);
Util.forEachPassenger(stand, e -> e.setPersistent(false));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.configuration.ConfigurationSection;

import com.nisovin.magicspells.util.Name;
import com.nisovin.magicspells.util.Util;
import com.nisovin.magicspells.MagicSpells;
import com.nisovin.magicspells.util.SpellData;
import com.nisovin.magicspells.util.EntityData;
Expand All @@ -27,9 +27,7 @@ public class EntityEffect extends SpellEffect {

private ConfigData<Integer> duration;

private ConfigData<Boolean> silent;
private ConfigData<Boolean> gravity;
private ConfigData<Boolean> enableAI;

@Override
protected void loadFromConfig(ConfigurationSection config) {
Expand All @@ -40,20 +38,17 @@ protected void loadFromConfig(ConfigurationSection config) {

duration = ConfigDataUtil.getInteger(section, "duration", 0);

silent = ConfigDataUtil.getBoolean(section, "silent", false);
gravity = ConfigDataUtil.getBoolean(section, "gravity", false);
enableAI = ConfigDataUtil.getBoolean(section, "ai", true);
}

@Override
protected Entity playEntityEffectLocation(Location location, SpellData data) {
return entityData.spawn(location, data, entity -> {
entity.addScoreboardTag(ENTITY_TAG);
entity.setGravity(gravity.get(data));
entity.setSilent(silent.get(data));
}, entity -> {
entity.setPersistent(false);

if (entity instanceof LivingEntity livingEntity) livingEntity.setAI(enableAI.get(data));
Util.forEachPassenger(entity, e -> e.setPersistent(false));
});
}

Expand All @@ -75,7 +70,7 @@ public Runnable playEffectLocation(Location location, SpellData data) {

@Override
public void turnOff() {
for (Entity entity : entities) entity.remove();
entities.forEach(Entity::remove);
entities.clear();
}

Expand Down
29 changes: 16 additions & 13 deletions core/src/main/java/com/nisovin/magicspells/spells/MultiSpell.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,18 @@ else if (action.isSpell()) {
}
} else {
if (customSpellCastChance.get(data)) {
double total = 0;
for (ActionChance actionChance : actions) total += actionChance.chance;
double total = actions.stream().mapToDouble(ActionChance::chance).sum();
if (total <= 0) return new CastResult(PostCastAction.ALREADY_HANDLED, data);

double selected = random.nextDouble(total);

double index = random.nextDouble(total);
Action action = null;
double subChance = 0;
double current = 0;
for (ActionChance actionChance : actions) {
subChance += actionChance.chance;

current += actionChance.chance;
if (selected >= current) continue;
action = actionChance.action;
if (subChance > index) break;
break;
}

if (action != null && action.isSpell()) action.getSpell().subcast(data);
Expand Down Expand Up @@ -141,17 +142,19 @@ else if (action.isSpell()) {
}
} else {
if (customSpellCastChance.get(subData)) {
double total = 0;
for (ActionChance actionChance : actions) total += actionChance.chance;
double total = actions.stream().mapToDouble(ActionChance::chance).sum();
if (total <= 0) return false;

double selected = random.nextDouble(total);

double index = random.nextDouble(total);
Action action = null;
double subChance = 0;
double current = 0;
for (ActionChance actionChance : actions) {
subChance += actionChance.chance;
current += actionChance.chance;
if (selected >= current) continue;

action = actionChance.action;
if (subChance > index) break;
break;
}

if (action != null && action.isSpell()) action.getSpell().getSpell().castFromConsole(sender, args);
Expand Down
Loading