use of net.minecraft.util.DamageSource in project ArsMagica2 by Mithion.
the class DamageSources method causeEntityMagicDamage.
public static DamageSource causeEntityMagicDamage(EntityLivingBase source) {
DamageSource ds = new EntityDamageSource("magic", source);
ds.setMagicDamage();
setDamageSourceUnblockable(ds);
return ds;
}
use of net.minecraft.util.DamageSource in project Overloaded by CJ-MC-Mods.
the class ArmorEventHandler method onLivingAttackedEvent.
@SubscribeEvent(priority = EventPriority.LOWEST)
public void onLivingAttackedEvent(LivingAttackEvent event) {
Entity entity = event.getEntity();
if (!(entity instanceof PlayerEntity))
return;
PlayerEntity player = ((PlayerEntity) entity);
boolean setEquipped = isMultiArmorSetEquipped(player);
if (setEquipped) {
DamageSource damageSource = event.getSource();
int energyCost = OverloadedConfig.INSTANCE.multiArmorConfig.baseCost;
float damageAmount = (float) (event.getAmount() * OverloadedConfig.INSTANCE.multiArmorConfig.damageMultiplier);
if (damageSource.isBypassMagic())
damageAmount *= OverloadedConfig.INSTANCE.multiArmorConfig.absoluteDamageMultiplier;
if (damageSource.isBypassArmor())
damageAmount *= OverloadedConfig.INSTANCE.multiArmorConfig.unblockableMultiplier;
if (damageAmount > Integer.MAX_VALUE)
return;
energyCost += damageAmount;
// Overflow
if (energyCost < 0)
return;
if (extractEnergy(player, energyCost, false)) {
event.setCanceled(true);
}
}
}
use of net.minecraft.util.DamageSource in project SpongeCommon by SpongePowered.
the class DamageEventHandler method createArmorModifiers.
public static Optional<List<DamageFunction>> createArmorModifiers(EntityLivingBase entityLivingBase, DamageSource damageSource, double damage) {
if (!damageSource.isUnblockable()) {
damage *= 25;
net.minecraft.item.ItemStack[] inventory = Iterables.toArray(entityLivingBase.getArmorInventoryList(), net.minecraft.item.ItemStack.class);
List<DamageFunction> modifiers = new ArrayList<>();
List<DamageObject> damageObjects = new ArrayList<>();
for (int index = 0; index < inventory.length; index++) {
net.minecraft.item.ItemStack itemStack = inventory[index];
if (itemStack.isEmpty()) {
continue;
}
Item item = itemStack.getItem();
if (item instanceof ItemArmor) {
ItemArmor armor = (ItemArmor) item;
double reduction = armor.damageReduceAmount / 25D;
DamageObject object = new DamageObject();
object.slot = index;
object.ratio = reduction;
damageObjects.add(object);
}
}
boolean first = true;
double ratio = 0;
for (DamageObject prop : damageObjects) {
EquipmentType type = resolveEquipment(prop.slot);
final DamageObject object = new DamageObject();
object.ratio = ratio;
if (first) {
object.previousDamage = damage;
object.augment = true;
}
DoubleUnaryOperator function = incomingDamage -> {
incomingDamage *= 25;
if (object.augment) {
// This is the damage that needs to be archived for the "first" armor modifier
// function since the armor modifiers work based on the initial damage and not as
// a chain one after another.
damageToHandle = incomingDamage;
}
double functionDamage = damageToHandle;
object.previousDamage = functionDamage;
object.ratio = prop.ratio;
object.ratio += prop.ratio;
return -((functionDamage * prop.ratio) / 25);
};
ratio += prop.ratio;
// TODO: direct cause creation: bad bad bad
DamageModifier modifier = DamageModifier.builder().cause(Cause.of(EventContext.empty(), ((org.spongepowered.api.item.inventory.ItemStack) inventory[prop.slot]).createSnapshot(), // We need this property to refer to the slot.
prop, // We need this object later on.
object)).type(DamageModifierTypes.ARMOR).build();
modifiers.add(new DamageFunction(modifier, function));
first = false;
}
if (!modifiers.isEmpty()) {
return Optional.of(modifiers);
}
}
return Optional.empty();
}
use of net.minecraft.util.DamageSource in project SpongeCommon by SpongePowered.
the class DamageEventHandler method createEnchantmentModifiers.
public static Optional<List<DamageFunction>> createEnchantmentModifiers(EntityLivingBase entityLivingBase, DamageSource damageSource) {
if (!damageSource.isDamageAbsolute()) {
Iterable<net.minecraft.item.ItemStack> inventory = entityLivingBase.getArmorInventoryList();
if (EnchantmentHelper.getEnchantmentModifierDamage(Lists.newArrayList(entityLivingBase.getArmorInventoryList()), damageSource) == 0) {
return Optional.empty();
}
List<DamageFunction> modifiers = new ArrayList<>();
boolean first = true;
int totalModifier = 0;
for (net.minecraft.item.ItemStack itemStack : inventory) {
if (itemStack.isEmpty()) {
continue;
}
final Multimap<Enchantment, Short> enchantments = LinkedHashMultimap.create();
NBTTagList enchantmentList = itemStack.getEnchantmentTagList();
if (enchantmentList == null) {
continue;
}
for (int i = 0; i < enchantmentList.tagCount(); ++i) {
final short enchantmentId = enchantmentList.getCompoundTagAt(i).getShort(NbtDataUtil.ITEM_ENCHANTMENT_ID);
final short level = enchantmentList.getCompoundTagAt(i).getShort(NbtDataUtil.ITEM_ENCHANTMENT_LEVEL);
if (Enchantment.getEnchantmentByID(enchantmentId) != null) {
// Ok, we have an enchantment!
final Enchantment enchantment = Enchantment.getEnchantmentByID(enchantmentId);
final int temp = enchantment.calcModifierDamage(level, damageSource);
if (temp != 0) {
enchantments.put(enchantment, level);
}
}
}
ItemStackSnapshot snapshot = ItemStackUtil.snapshotOf(itemStack);
for (Map.Entry<Enchantment, Collection<Short>> enchantment : enchantments.asMap().entrySet()) {
final DamageObject object = new DamageObject();
int modifierTemp = 0;
for (short level : enchantment.getValue()) {
modifierTemp += enchantment.getKey().calcModifierDamage(level, damageSource);
}
final int modifier = modifierTemp;
object.previousDamage = totalModifier;
if (object.previousDamage > 25) {
object.previousDamage = 25;
}
totalModifier += modifier;
object.augment = first;
object.ratio = modifier;
DoubleUnaryOperator enchantmentFunction = damageIn -> {
if (object.augment) {
enchantmentDamageTracked = damageIn;
}
if (damageIn <= 0) {
return 0D;
}
double actualDamage = enchantmentDamageTracked;
if (object.previousDamage > 25) {
return 0D;
}
double modifierDamage = actualDamage;
double magicModifier;
if (modifier > 0 && modifier <= 20) {
int j = 25 - modifier;
magicModifier = modifierDamage * j;
modifierDamage = magicModifier / 25.0F;
}
return -Math.max(actualDamage - modifierDamage, 0.0D);
};
if (first) {
first = false;
}
// TODO: direct cause creation: bad bad bad
DamageModifier enchantmentModifier = DamageModifier.builder().cause(Cause.of(EventContext.empty(), enchantment, snapshot, entityLivingBase)).type(DamageModifierTypes.ARMOR_ENCHANTMENT).build();
modifiers.add(new DamageFunction(enchantmentModifier, enchantmentFunction));
}
if (!modifiers.isEmpty()) {
return Optional.of(modifiers);
}
}
}
return Optional.empty();
}
use of net.minecraft.util.DamageSource in project SpongeCommon by SpongePowered.
the class MixinEntityLivingBase method onDeath.
/**
* @author blood - May 12th, 2016
* @author gabizou - June 4th, 2016 - Update for 1.9.4 and Cause Tracker changes
*
* @reason SpongeForge requires an overwrite so we do it here instead. This handles all living entity death events
* (except players). Note should be taken that normally, there are lists for drops captured, however, the drops
* are retroactively captured by the PhaseTracker and associated through the different phases, depending on
* how they are handled, so no lists and flags on the entities themselves are needed as they are tracked through
* the {@link PhaseContext} of the current {@link IPhaseState} at which this method is called. The compatibility
* for Forge's events to throw are handled specially in SpongeForge.
*/
@Overwrite
public void onDeath(DamageSource cause) {
// Sponge Start - Call our event, and forge's event
// This will transitively call the forge event
SpongeCommonEventFactory.callDestructEntityEventDeath((EntityLivingBase) (Object) this, cause);
// Double check that the PhaseTracker is already capturing the Death phase
final PhaseTracker phaseTracker = PhaseTracker.getInstance();
final boolean isMainThread = !this.world.isRemote || Sponge.isServerAvailable() && Sponge.getServer().isMainThread();
try (final StackFrame frame = isMainThread ? Sponge.getCauseStackManager().pushCauseFrame() : null) {
if (!this.world.isRemote) {
final PhaseData peek = phaseTracker.getCurrentPhaseData();
final IPhaseState state = peek.state;
this.tracksEntityDeaths = !phaseTracker.getCurrentState().tracksEntityDeaths() && state != EntityPhase.State.DEATH;
if (this.tracksEntityDeaths) {
Sponge.getCauseStackManager().pushCause(this);
final PhaseContext<?> context = EntityPhase.State.DEATH.createPhaseContext().setDamageSource((org.spongepowered.api.event.cause.entity.damage.source.DamageSource) cause).source(this);
this.getNotifierUser().ifPresent(context::notifier);
this.getCreatorUser().ifPresent(context::owner);
context.buildAndSwitch();
}
} else {
this.tracksEntityDeaths = false;
}
// Sponge End
if (this.dead) {
// Sponge Start - ensure that we finish the tracker if necessary
if (this.tracksEntityDeaths && !properlyOverridesOnDeathForCauseTrackerCompletion()) {
phaseTracker.completePhase(EntityPhase.State.DEATH);
}
// Sponge End
return;
}
Entity entity = cause.getTrueSource();
EntityLivingBase entitylivingbase = this.getAttackingEntity();
if (this.scoreValue >= 0 && entitylivingbase != null) {
entitylivingbase.awardKillScore((EntityLivingBase) (Object) this, this.scoreValue, cause);
}
if (entity != null) {
entity.onKillEntity((EntityLivingBase) (Object) this);
}
this.dead = true;
this.getCombatTracker().reset();
if (!this.world.isRemote) {
int i = 0;
if (entity instanceof EntityPlayer) {
i = EnchantmentHelper.getLootingModifier((EntityLivingBase) entity);
}
if (this.canDropLoot() && this.world.getGameRules().getBoolean("doMobLoot")) {
boolean flag = this.recentlyHit > 0;
this.dropLoot(flag, i, cause);
}
}
// Sponge Start - Don't send the state if this is a human. Fixes ghost items on client.
if (!((EntityLivingBase) (Object) this instanceof EntityHuman)) {
this.world.setEntityState((EntityLivingBase) (Object) this, (byte) 3);
}
if (phaseTracker != null && this.tracksEntityDeaths && !properlyOverridesOnDeathForCauseTrackerCompletion()) {
this.tracksEntityDeaths = false;
phaseTracker.completePhase(EntityPhase.State.DEATH);
}
}
// Sponge End
}
Aggregations