forked from Nomifactory/GregTech
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Entity damage boost behavior (#1399)
- Loading branch information
1 parent
9f07bb7
commit d61506d
Showing
4 changed files
with
84 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/main/java/gregtech/common/items/tool/EntityDamageBehavior.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package gregtech.common.items.tool; | ||
|
||
import gregtech.api.damagesources.DamageSources; | ||
import gregtech.api.items.toolitem.behavior.IToolBehavior; | ||
import net.minecraft.client.resources.I18n; | ||
import net.minecraft.client.util.ITooltipFlag; | ||
import net.minecraft.entity.EntityLivingBase; | ||
import net.minecraft.entity.player.EntityPlayer; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.util.DamageSource; | ||
import net.minecraft.world.World; | ||
|
||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* Add to tools to have them deal bonus damage to specific mobs. | ||
* Pass null for the mobType parameter to ignore the tooltip. | ||
*/ | ||
public class EntityDamageBehavior implements IToolBehavior { | ||
|
||
private final List<Function<EntityLivingBase, Float>> shouldDoBonusList = new ArrayList<>(); | ||
private final String mobType; | ||
|
||
public EntityDamageBehavior(float bonus, Class<?>... entities) { | ||
this(null, bonus, entities); | ||
} | ||
|
||
public EntityDamageBehavior(Map<Class<?>, Float> entities) { | ||
this(null, entities); | ||
} | ||
|
||
public EntityDamageBehavior(String mobType, float bonus, Class<?>... entities) { | ||
this.mobType = mobType; | ||
for (Class<?> entity : entities) { | ||
shouldDoBonusList.add(e -> entity.isAssignableFrom(e.getClass()) ? bonus : 0); | ||
} | ||
} | ||
|
||
public EntityDamageBehavior(String mobType, Map<Class<?>, Float> entities) { | ||
this.mobType = mobType; | ||
for (Map.Entry<Class<?>, Float> entry : entities.entrySet()) { | ||
Class<?> entity = entry.getKey(); | ||
float bonus = entry.getValue(); | ||
shouldDoBonusList.add(e -> entity.isAssignableFrom(e.getClass()) ? bonus : 0); | ||
} | ||
} | ||
|
||
@Override | ||
public void hitEntity(@Nonnull ItemStack stack, @Nonnull EntityLivingBase target, @Nonnull EntityLivingBase attacker) { | ||
float damageBonus = shouldDoBonusList.stream().map(func -> func.apply(target)).filter(f -> f > 0).findFirst().orElse(0f); | ||
if (damageBonus != 0f) { | ||
DamageSource source = attacker instanceof EntityPlayer | ||
? DamageSources.getPlayerDamage((EntityPlayer) attacker) | ||
: DamageSources.getMobDamage(attacker); | ||
target.attackEntityFrom(source, damageBonus); | ||
} | ||
} | ||
|
||
@Override | ||
public void addInformation(@Nonnull ItemStack stack, @Nullable World world, @Nonnull List<String> tooltip, @Nonnull ITooltipFlag flag) { | ||
if (mobType != null && !mobType.isEmpty()) { | ||
tooltip.add(I18n.format("item.gt.tool.behavior.damage_boost", I18n.format("item.gt.tool.behavior.damage_boost_" + mobType))); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters