Skip to content

Commit

Permalink
Entity damage boost behavior (#1399)
Browse files Browse the repository at this point in the history
  • Loading branch information
serenibyss authored Jan 9, 2023
1 parent 9f07bb7 commit d61506d
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/main/java/gregtech/GregTechMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
+ "required-after:codechickenlib@[3.2.3,);"
+ "after:forestry;"
+ "after:jei@[4.15.0,);"
+ "after:crafttweaker@[4.1.20,);")
+ "after:crafttweaker@[4.1.20,);"
+ "after:groovyscript@[0.1.0,);")
public class GregTechMod {

// Hold this so that we can reference non-interface methods without
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/gregtech/common/items/ToolItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import gregtech.core.sound.GTSoundEvents;
import net.minecraft.client.Minecraft;
import net.minecraft.enchantment.EnumEnchantmentType;
import net.minecraft.entity.monster.EntityGolem;
import net.minecraft.entity.monster.EntitySpider;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.ItemStack;
import net.minecraftforge.client.model.ModelLoader;
Expand Down Expand Up @@ -86,7 +88,8 @@ public static void init() {
.toolClasses(ToolClasses.SAW));
HARD_HAMMER = register(ItemGTTool.Builder.of(GTValues.MODID, "hammer")
.toolStats(b -> b.blockBreaking().crafting().damagePerCraftingAction(2)
.attackDamage(1.0F).attackSpeed(-2.8F))
.attackDamage(1.0F).attackSpeed(-2.8F)
.behaviors(new EntityDamageBehavior(2.0F, EntityGolem.class)))
.oreDict(ToolOreDicts.craftingToolHammer)
.sound(SoundEvents.BLOCK_ANVIL_LAND)
.symbol('h')
Expand All @@ -111,7 +114,7 @@ public static void init() {
WRENCH = register(ItemGTTool.Builder.of(GTValues.MODID, "wrench")
.toolStats(b -> b.blockBreaking().crafting().sneakBypassUse()
.attackDamage(1.0F).attackSpeed(-2.8F)
.behaviors(new BlockRotatingBehavior()))
.behaviors(new BlockRotatingBehavior(), new EntityDamageBehavior(3.0F, EntityGolem.class)))
.sound(GTSoundEvents.WRENCH_TOOL, true)
.oreDict(ToolOreDicts.craftingToolWrench)
.symbol('w')
Expand All @@ -133,7 +136,8 @@ public static void init() {
.toolClasses(ToolClasses.CROWBAR));
SCREWDRIVER = register(ItemGTTool.Builder.of(GTValues.MODID, "screwdriver")
.toolStats(b -> b.crafting().damagePerCraftingAction(4).sneakBypassUse()
.attackDamage(-1.0F).attackSpeed(3.0F))
.attackDamage(-1.0F).attackSpeed(3.0F)
.behaviors(new EntityDamageBehavior(3.0F, EntitySpider.class)))
.sound(GTSoundEvents.SCREWDRIVER_TOOL)
.oreDict(ToolOreDicts.craftingToolScrewdriver)
.symbol('d')
Expand Down Expand Up @@ -227,6 +231,7 @@ public static void init() {
.toolStats(b -> b.blockBreaking().crafting().sneakBypassUse()
.efficiencyMultiplier(2.0F)
.attackDamage(1.0F).attackSpeed(-2.8F)
.behaviors(new BlockRotatingBehavior(), new EntityDamageBehavior(3.0F, EntityGolem.class))
.brokenStack(ToolHelper.SUPPLY_POWER_UNIT_LV))
.sound(GTSoundEvents.WRENCH_TOOL, true)
.oreDict(ToolOreDicts.craftingToolWrench)
Expand All @@ -236,6 +241,7 @@ public static void init() {
.toolStats(b -> b.blockBreaking().crafting().sneakBypassUse()
.efficiencyMultiplier(3.0F)
.attackDamage(1.0F).attackSpeed(-2.8F)
.behaviors(new BlockRotatingBehavior(), new EntityDamageBehavior(3.0F, EntityGolem.class))
.brokenStack(ToolHelper.SUPPLY_POWER_UNIT_HV))
.sound(GTSoundEvents.WRENCH_TOOL, true)
.oreDict(ToolOreDicts.craftingToolWrench)
Expand All @@ -245,6 +251,7 @@ public static void init() {
.toolStats(b -> b.blockBreaking().crafting().sneakBypassUse()
.efficiencyMultiplier(4.0F)
.attackDamage(1.0F).attackSpeed(-2.8F)
.behaviors(new BlockRotatingBehavior(), new EntityDamageBehavior(3.0F, EntityGolem.class))
.brokenStack(ToolHelper.SUPPLY_POWER_UNIT_IV))
.sound(GTSoundEvents.WRENCH_TOOL, true)
.oreDict(ToolOreDicts.craftingToolWrench)
Expand All @@ -260,6 +267,7 @@ public static void init() {
SCREWDRIVER_LV = register(ItemGTTool.Builder.of(GTValues.MODID, "screwdriver_lv")
.toolStats(b -> b.crafting().sneakBypassUse()
.attackDamage(-1.0F).attackSpeed(3.0F)
.behaviors(new EntityDamageBehavior(3.0F, EntitySpider.class))
.brokenStack(ToolHelper.SUPPLY_POWER_UNIT_LV))
.sound(GTSoundEvents.SCREWDRIVER_TOOL)
.oreDict(ToolOreDicts.craftingToolScrewdriver)
Expand Down
70 changes: 70 additions & 0 deletions src/main/java/gregtech/common/items/tool/EntityDamageBehavior.java
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)));
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/assets/gregtech/lang/en_us.lang
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,7 @@ item.gt.tool.behavior.rail_rotation=§eRailroad Engineer: §fRotates Rails
item.gt.tool.behavior.crop_harvesting=§aHarvester: §fHarvests Crops
item.gt.tool.behavior.plunger=§9Plumber: §fDrains Fluids
item.gt.tool.behavior.block_rotation=§2Mechanic: §fRotates Blocks
item.gt.tool.behavior.damage_boost=§4Damage Boost: §fExtra damage against %s

item.gt.tool.sword.name=%s Sword
item.gt.tool.pickaxe.name=%s Pickaxe
Expand Down

0 comments on commit d61506d

Please sign in to comment.