Skip to content
This repository has been archived by the owner on Dec 14, 2021. It is now read-only.

Commit

Permalink
Added event for application of armor to damage received
Browse files Browse the repository at this point in the history
  • Loading branch information
drcrazy committed Feb 14, 2021
1 parent c341feb commit e5b8e0f
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* This file is part of RebornCore, licensed under the MIT License (MIT).
*
* Copyright (c) 2021 TeamReborn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package reborncore.api.events;

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.player.PlayerEntity;

public interface ApplyArmorToDamageCallback {

Event<ApplyArmorToDamageCallback> EVENT = EventFactory.createArrayBacked(ApplyArmorToDamageCallback.class,
(listeners) -> (player, damageSource, amount) -> {
float damageAmount = amount;
for (ApplyArmorToDamageCallback listener : listeners){
damageAmount = listener.applyArmorToDamage(player, damageSource, damageAmount);
}
return damageAmount;
});

/**
* Apply armor to amount of damage inflicted. Decreases it in most cases unless armor should increase damage inflicted.
* Event is called after damage is being reduced by armor already and before damage reduction from enchants.
*
* @param player PlayerEntity Player being damaged
* @param source DamageSource Type of damage
* @param amount float Current amount of damage
* @return float Amount of damage after reduction
*/
float applyArmorToDamage(PlayerEntity player, DamageSource source, float amount);
}
47 changes: 47 additions & 0 deletions src/main/java/reborncore/mixin/common/MixinLivingEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* This file is part of RebornCore, licensed under the MIT License (MIT).
*
* Copyright (c) 2021 TeamReborn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package reborncore.mixin.common;

import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.player.PlayerEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import reborncore.api.events.ApplyArmorToDamageCallback;

@Mixin(LivingEntity.class)
abstract class MixinLivingEntity {

@Inject(method = "applyArmorToDamage", at = @At("RETURN"), cancellable = true)
public void onApplyArmorToDamage(DamageSource source, float amount, CallbackInfoReturnable<Float> cir){

LivingEntity entity = (LivingEntity) (Object) this;
if (! (entity instanceof PlayerEntity)) { return; }

cir.setReturnValue(ApplyArmorToDamageCallback.EVENT.invoker().applyArmorToDamage((PlayerEntity) entity, source, amount));
}
}
27 changes: 14 additions & 13 deletions src/main/resources/reborncore.common.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
"package": "reborncore.mixin.common",
"compatibilityLevel": "JAVA_8",
"mixins": [
"AccessorScreenHandler",
"AccessorFluidBlock",
"AccessorFoliagePlacerType",
"AccessorIngredient",
"AccessorRecipeManager",
"AccessorSlot",
"MixinBucketItem",
"MixinCraftingResultSlot",
"MixinItemEntity",
"MixinItemStack",
"MixinPlayerEntity",
"MixinRecipeManager",
"MixinWorld"
"AccessorFluidBlock",
"AccessorFoliagePlacerType",
"AccessorIngredient",
"AccessorRecipeManager",
"AccessorScreenHandler",
"AccessorSlot",
"MixinBucketItem",
"MixinCraftingResultSlot",
"MixinItemEntity",
"MixinItemStack",
"MixinLivingEntity",
"MixinPlayerEntity",
"MixinRecipeManager",
"MixinWorld"
],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit e5b8e0f

Please sign in to comment.