Skip to content

Commit d61506d

Browse files
authored
Entity damage boost behavior (#1399)
1 parent 9f07bb7 commit d61506d

File tree

4 files changed

+84
-4
lines changed

4 files changed

+84
-4
lines changed

src/main/java/gregtech/GregTechMod.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
+ "required-after:codechickenlib@[3.2.3,);"
2121
+ "after:forestry;"
2222
+ "after:jei@[4.15.0,);"
23-
+ "after:crafttweaker@[4.1.20,);")
23+
+ "after:crafttweaker@[4.1.20,);"
24+
+ "after:groovyscript@[0.1.0,);")
2425
public class GregTechMod {
2526

2627
// Hold this so that we can reference non-interface methods without

src/main/java/gregtech/common/items/ToolItems.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import gregtech.core.sound.GTSoundEvents;
99
import net.minecraft.client.Minecraft;
1010
import net.minecraft.enchantment.EnumEnchantmentType;
11+
import net.minecraft.entity.monster.EntityGolem;
12+
import net.minecraft.entity.monster.EntitySpider;
1113
import net.minecraft.init.SoundEvents;
1214
import net.minecraft.item.ItemStack;
1315
import net.minecraftforge.client.model.ModelLoader;
@@ -86,7 +88,8 @@ public static void init() {
8688
.toolClasses(ToolClasses.SAW));
8789
HARD_HAMMER = register(ItemGTTool.Builder.of(GTValues.MODID, "hammer")
8890
.toolStats(b -> b.blockBreaking().crafting().damagePerCraftingAction(2)
89-
.attackDamage(1.0F).attackSpeed(-2.8F))
91+
.attackDamage(1.0F).attackSpeed(-2.8F)
92+
.behaviors(new EntityDamageBehavior(2.0F, EntityGolem.class)))
9093
.oreDict(ToolOreDicts.craftingToolHammer)
9194
.sound(SoundEvents.BLOCK_ANVIL_LAND)
9295
.symbol('h')
@@ -111,7 +114,7 @@ public static void init() {
111114
WRENCH = register(ItemGTTool.Builder.of(GTValues.MODID, "wrench")
112115
.toolStats(b -> b.blockBreaking().crafting().sneakBypassUse()
113116
.attackDamage(1.0F).attackSpeed(-2.8F)
114-
.behaviors(new BlockRotatingBehavior()))
117+
.behaviors(new BlockRotatingBehavior(), new EntityDamageBehavior(3.0F, EntityGolem.class)))
115118
.sound(GTSoundEvents.WRENCH_TOOL, true)
116119
.oreDict(ToolOreDicts.craftingToolWrench)
117120
.symbol('w')
@@ -133,7 +136,8 @@ public static void init() {
133136
.toolClasses(ToolClasses.CROWBAR));
134137
SCREWDRIVER = register(ItemGTTool.Builder.of(GTValues.MODID, "screwdriver")
135138
.toolStats(b -> b.crafting().damagePerCraftingAction(4).sneakBypassUse()
136-
.attackDamage(-1.0F).attackSpeed(3.0F))
139+
.attackDamage(-1.0F).attackSpeed(3.0F)
140+
.behaviors(new EntityDamageBehavior(3.0F, EntitySpider.class)))
137141
.sound(GTSoundEvents.SCREWDRIVER_TOOL)
138142
.oreDict(ToolOreDicts.craftingToolScrewdriver)
139143
.symbol('d')
@@ -227,6 +231,7 @@ public static void init() {
227231
.toolStats(b -> b.blockBreaking().crafting().sneakBypassUse()
228232
.efficiencyMultiplier(2.0F)
229233
.attackDamage(1.0F).attackSpeed(-2.8F)
234+
.behaviors(new BlockRotatingBehavior(), new EntityDamageBehavior(3.0F, EntityGolem.class))
230235
.brokenStack(ToolHelper.SUPPLY_POWER_UNIT_LV))
231236
.sound(GTSoundEvents.WRENCH_TOOL, true)
232237
.oreDict(ToolOreDicts.craftingToolWrench)
@@ -236,6 +241,7 @@ public static void init() {
236241
.toolStats(b -> b.blockBreaking().crafting().sneakBypassUse()
237242
.efficiencyMultiplier(3.0F)
238243
.attackDamage(1.0F).attackSpeed(-2.8F)
244+
.behaviors(new BlockRotatingBehavior(), new EntityDamageBehavior(3.0F, EntityGolem.class))
239245
.brokenStack(ToolHelper.SUPPLY_POWER_UNIT_HV))
240246
.sound(GTSoundEvents.WRENCH_TOOL, true)
241247
.oreDict(ToolOreDicts.craftingToolWrench)
@@ -245,6 +251,7 @@ public static void init() {
245251
.toolStats(b -> b.blockBreaking().crafting().sneakBypassUse()
246252
.efficiencyMultiplier(4.0F)
247253
.attackDamage(1.0F).attackSpeed(-2.8F)
254+
.behaviors(new BlockRotatingBehavior(), new EntityDamageBehavior(3.0F, EntityGolem.class))
248255
.brokenStack(ToolHelper.SUPPLY_POWER_UNIT_IV))
249256
.sound(GTSoundEvents.WRENCH_TOOL, true)
250257
.oreDict(ToolOreDicts.craftingToolWrench)
@@ -260,6 +267,7 @@ public static void init() {
260267
SCREWDRIVER_LV = register(ItemGTTool.Builder.of(GTValues.MODID, "screwdriver_lv")
261268
.toolStats(b -> b.crafting().sneakBypassUse()
262269
.attackDamage(-1.0F).attackSpeed(3.0F)
270+
.behaviors(new EntityDamageBehavior(3.0F, EntitySpider.class))
263271
.brokenStack(ToolHelper.SUPPLY_POWER_UNIT_LV))
264272
.sound(GTSoundEvents.SCREWDRIVER_TOOL)
265273
.oreDict(ToolOreDicts.craftingToolScrewdriver)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package gregtech.common.items.tool;
2+
3+
import gregtech.api.damagesources.DamageSources;
4+
import gregtech.api.items.toolitem.behavior.IToolBehavior;
5+
import net.minecraft.client.resources.I18n;
6+
import net.minecraft.client.util.ITooltipFlag;
7+
import net.minecraft.entity.EntityLivingBase;
8+
import net.minecraft.entity.player.EntityPlayer;
9+
import net.minecraft.item.ItemStack;
10+
import net.minecraft.util.DamageSource;
11+
import net.minecraft.world.World;
12+
13+
import javax.annotation.Nonnull;
14+
import javax.annotation.Nullable;
15+
import java.util.ArrayList;
16+
import java.util.List;
17+
import java.util.Map;
18+
import java.util.function.Function;
19+
20+
/**
21+
* Add to tools to have them deal bonus damage to specific mobs.
22+
* Pass null for the mobType parameter to ignore the tooltip.
23+
*/
24+
public class EntityDamageBehavior implements IToolBehavior {
25+
26+
private final List<Function<EntityLivingBase, Float>> shouldDoBonusList = new ArrayList<>();
27+
private final String mobType;
28+
29+
public EntityDamageBehavior(float bonus, Class<?>... entities) {
30+
this(null, bonus, entities);
31+
}
32+
33+
public EntityDamageBehavior(Map<Class<?>, Float> entities) {
34+
this(null, entities);
35+
}
36+
37+
public EntityDamageBehavior(String mobType, float bonus, Class<?>... entities) {
38+
this.mobType = mobType;
39+
for (Class<?> entity : entities) {
40+
shouldDoBonusList.add(e -> entity.isAssignableFrom(e.getClass()) ? bonus : 0);
41+
}
42+
}
43+
44+
public EntityDamageBehavior(String mobType, Map<Class<?>, Float> entities) {
45+
this.mobType = mobType;
46+
for (Map.Entry<Class<?>, Float> entry : entities.entrySet()) {
47+
Class<?> entity = entry.getKey();
48+
float bonus = entry.getValue();
49+
shouldDoBonusList.add(e -> entity.isAssignableFrom(e.getClass()) ? bonus : 0);
50+
}
51+
}
52+
53+
@Override
54+
public void hitEntity(@Nonnull ItemStack stack, @Nonnull EntityLivingBase target, @Nonnull EntityLivingBase attacker) {
55+
float damageBonus = shouldDoBonusList.stream().map(func -> func.apply(target)).filter(f -> f > 0).findFirst().orElse(0f);
56+
if (damageBonus != 0f) {
57+
DamageSource source = attacker instanceof EntityPlayer
58+
? DamageSources.getPlayerDamage((EntityPlayer) attacker)
59+
: DamageSources.getMobDamage(attacker);
60+
target.attackEntityFrom(source, damageBonus);
61+
}
62+
}
63+
64+
@Override
65+
public void addInformation(@Nonnull ItemStack stack, @Nullable World world, @Nonnull List<String> tooltip, @Nonnull ITooltipFlag flag) {
66+
if (mobType != null && !mobType.isEmpty()) {
67+
tooltip.add(I18n.format("item.gt.tool.behavior.damage_boost", I18n.format("item.gt.tool.behavior.damage_boost_" + mobType)));
68+
}
69+
}
70+
}

src/main/resources/assets/gregtech/lang/en_us.lang

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,7 @@ item.gt.tool.behavior.rail_rotation=§eRailroad Engineer: §fRotates Rails
903903
item.gt.tool.behavior.crop_harvesting=§aHarvester: §fHarvests Crops
904904
item.gt.tool.behavior.plunger=§9Plumber: §fDrains Fluids
905905
item.gt.tool.behavior.block_rotation=§2Mechanic: §fRotates Blocks
906+
item.gt.tool.behavior.damage_boost=§4Damage Boost: §fExtra damage against %s
906907

907908
item.gt.tool.sword.name=%s Sword
908909
item.gt.tool.pickaxe.name=%s Pickaxe

0 commit comments

Comments
 (0)