Skip to content

Commit f8c580a

Browse files
committed
Implemented Shield Bash
Reduced Ablaze time on fire
1 parent 6409027 commit f8c580a

File tree

7 files changed

+174
-55
lines changed

7 files changed

+174
-55
lines changed

changelog.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
# Changelog
22

33
## Upcoming
4+
* Shield Bash now works
5+
* Treasure enchantment
6+
* Block with the Shield for 1.5 seconds.
7+
After that, tap crouch to bash and damage any entity in your path (Ablaze works too)
8+
On bashing the ability is put on a 1.5 seconds cooldown
9+
* Incompatible with Recoil and Reflection
410
* Ablaze now sets projectiles on fire
11+
* Reduced Ablaze time on fire (~~4~~ -> 3 seconds per level)
512

613
## Alpha 1.2.0
714
* Added 6 shields enchantments

src/main/java/com/insane96mcp/shieldsplus/module/base/feature/BaseFeature.java

+7-50
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
11
package com.insane96mcp.shieldsplus.module.base.feature;
22

33
import com.insane96mcp.shieldsplus.setup.Config;
4-
import com.insane96mcp.shieldsplus.setup.SPEnchantments;
54
import com.insane96mcp.shieldsplus.setup.SPShieldMaterials;
65
import com.insane96mcp.shieldsplus.world.item.SPShieldItem;
76
import com.insane96mcp.shieldsplus.world.item.enchantment.*;
87
import insane96mcp.insanelib.base.Feature;
98
import insane96mcp.insanelib.base.Label;
109
import insane96mcp.insanelib.base.Module;
11-
import net.minecraft.server.level.ServerPlayer;
1210
import net.minecraft.world.damagesource.DamageSource;
1311
import net.minecraft.world.entity.LivingEntity;
14-
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
15-
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
16-
import net.minecraft.world.entity.ai.attributes.Attributes;
17-
import net.minecraft.world.entity.projectile.Projectile;
18-
import net.minecraft.world.item.ItemStack;
1912
import net.minecraft.world.item.Items;
2013
import net.minecraft.world.item.ShieldItem;
21-
import net.minecraft.world.item.enchantment.EnchantmentHelper;
2214
import net.minecraftforge.common.ForgeConfigSpec;
2315
import net.minecraftforge.event.TickEvent;
2416
import net.minecraftforge.event.entity.living.ShieldBlockEvent;
@@ -90,55 +82,20 @@ else if (event.getEntityLiving().getUseItem().getItem() instanceof SPShieldItem)
9082

9183
private void processEnchantments(LivingEntity blockingEntity, DamageSource source, float amount) {
9284
if (blockingEntity.getUseItem().getItem() instanceof ShieldItem) {
93-
ItemStack shield = blockingEntity.getUseItem();
94-
int recoil = EnchantmentHelper.getItemEnchantmentLevel(SPEnchantments.RECOIL.get(), shield);
95-
if (recoil > 0)
96-
{
97-
if (source.getEntity() instanceof LivingEntity sourceEntity && source.getEntity() == source.getDirectEntity()) {
98-
sourceEntity.knockback(recoil * ShieldRecoilEnchantment.KNOCKBACK, blockingEntity.getX() - sourceEntity.getX(), blockingEntity.getZ() - sourceEntity.getZ());
99-
}
100-
else if (source.getDirectEntity() instanceof Projectile projectile) {
101-
projectile.setDeltaMovement(projectile.getDeltaMovement().scale(recoil * ShieldRecoilEnchantment.PROJECTILE_KNOCKBACK));
102-
}
103-
}
104-
105-
int reflection = EnchantmentHelper.getItemEnchantmentLevel(SPEnchantments.REFLECTION.get(), shield);
106-
if (reflection > 0 && source.getEntity() instanceof LivingEntity sourceEntity && source.getEntity() == source.getDirectEntity())
107-
{
108-
sourceEntity.hurt(DamageSource.thorns(blockingEntity), Math.min(ShieldReflectionEnchantment.getReflectedDamage(reflection) * amount, ShieldReflectionEnchantment.getCappedReflectedDamage(reflection)));
109-
}
110-
111-
int ablaze = EnchantmentHelper.getItemEnchantmentLevel(SPEnchantments.ABLAZE.get(), shield);
112-
if (ablaze > 0 && source.getDirectEntity() != null) {
113-
source.getDirectEntity().setSecondsOnFire(ablaze * ShieldAblazeEnchantment.SECONDS_ON_FIRE);
114-
}
85+
ShieldRecoilEnchantment.onBlocked(blockingEntity, source);
86+
ShieldReflectionEnchantment.onBlocked(blockingEntity, source, amount);
87+
ShieldAblazeEnchantment.onBlocked(blockingEntity, source);
11588
}
11689
}
11790

11891
@SubscribeEvent
11992
public void onPlayerTick(TickEvent.PlayerTickEvent event) {
120-
if (!this.isEnabled())
121-
return;
122-
123-
if (!(event.player instanceof ServerPlayer player))
124-
return;
125-
126-
AttributeInstance attribute = player.getAttribute(Attributes.MOVEMENT_SPEED);
127-
if (attribute == null)
93+
if (!this.isEnabled()
94+
|| event.phase != TickEvent.Phase.END)
12895
return;
12996

130-
if (player.isBlocking()) {
131-
int lightweight = EnchantmentHelper.getItemEnchantmentLevel(SPEnchantments.LIGHTWEIGHT.get(), player.getUseItem());
132-
if (lightweight > 0) {
133-
134-
if (attribute.getModifier(ShieldLightweightEnchantment.BONUS_SPEED_UUID) == null) {
135-
attribute.addTransientModifier(new AttributeModifier(ShieldLightweightEnchantment.BONUS_SPEED_UUID, "Lightweight bonus speed", ShieldLightweightEnchantment.BONUS_SPEED * lightweight, AttributeModifier.Operation.MULTIPLY_BASE));
136-
}
137-
}
138-
}
139-
else if (attribute.getModifier(ShieldLightweightEnchantment.BONUS_SPEED_UUID) != null) {
140-
attribute.removeModifier(ShieldLightweightEnchantment.BONUS_SPEED_UUID);
141-
}
97+
ShieldLightweightEnchantment.onTick(event.player);
98+
ShieldBashEnchantment.onTick(event.player);
14299
}
143100

144101
@SubscribeEvent

src/main/java/com/insane96mcp/shieldsplus/world/item/enchantment/ShieldAblazeEnchantment.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package com.insane96mcp.shieldsplus.world.item.enchantment;
22

3+
import com.insane96mcp.shieldsplus.setup.SPEnchantments;
4+
import net.minecraft.world.damagesource.DamageSource;
35
import net.minecraft.world.entity.EquipmentSlot;
6+
import net.minecraft.world.entity.LivingEntity;
47
import net.minecraft.world.item.ItemStack;
58
import net.minecraft.world.item.ShieldItem;
69
import net.minecraft.world.item.enchantment.Enchantment;
710
import net.minecraft.world.item.enchantment.EnchantmentCategory;
11+
import net.minecraft.world.item.enchantment.EnchantmentHelper;
812

913
public class ShieldAblazeEnchantment extends Enchantment {
1014

11-
public static final int SECONDS_ON_FIRE = 4;
15+
public static final int SECONDS_ON_FIRE = 3;
1216

1317
public ShieldAblazeEnchantment() {
1418
super(Rarity.UNCOMMON, EnchantmentCategory.BREAKABLE, new EquipmentSlot[]{EquipmentSlot.MAINHAND, EquipmentSlot.OFFHAND});
@@ -30,4 +34,17 @@ public int getMaxLevel() {
3034
public boolean canApplyAtEnchantingTable(ItemStack stack) {
3135
return stack.getItem() instanceof ShieldItem;
3236
}
37+
38+
public static void onBlocked(LivingEntity blockingEntity, DamageSource source) {
39+
if (!(source.getDirectEntity() instanceof LivingEntity livingEntity))
40+
return;
41+
apply(blockingEntity, livingEntity);
42+
}
43+
44+
public static void apply(LivingEntity attacker, LivingEntity other) {
45+
ItemStack shield = attacker.getUseItem();
46+
int ablaze = EnchantmentHelper.getItemEnchantmentLevel(SPEnchantments.ABLAZE.get(), shield);
47+
if (ablaze > 0)
48+
other.setSecondsOnFire(ablaze * ShieldAblazeEnchantment.SECONDS_ON_FIRE);
49+
}
3350
}
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,38 @@
11
package com.insane96mcp.shieldsplus.world.item.enchantment;
22

3+
import insane96mcp.insanelib.util.MCUtils;
4+
import net.minecraft.core.particles.ParticleTypes;
5+
import net.minecraft.nbt.CompoundTag;
6+
import net.minecraft.sounds.SoundEvents;
7+
import net.minecraft.util.Mth;
8+
import net.minecraft.world.damagesource.DamageSource;
39
import net.minecraft.world.entity.EquipmentSlot;
10+
import net.minecraft.world.entity.LivingEntity;
11+
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
12+
import net.minecraft.world.entity.player.Player;
13+
import net.minecraft.world.item.ItemStack;
14+
import net.minecraft.world.item.ShieldItem;
415
import net.minecraft.world.item.enchantment.Enchantment;
516
import net.minecraft.world.item.enchantment.EnchantmentCategory;
17+
import net.minecraft.world.phys.AABB;
18+
import net.minecraftforge.common.ForgeMod;
19+
import org.jetbrains.annotations.NotNull;
20+
21+
import java.util.List;
22+
import java.util.UUID;
623

724
public class ShieldBashEnchantment extends Enchantment {
825

926
public ShieldBashEnchantment() {
1027
super(Rarity.VERY_RARE, EnchantmentCategory.BREAKABLE, new EquipmentSlot[]{EquipmentSlot.MAINHAND, EquipmentSlot.OFFHAND});
1128
}
1229

13-
public int getMinCost(int p_44598_) {
14-
return 22;
30+
public int getMinCost(int level) {
31+
return 25;
1532
}
1633

17-
public int getMaxCost(int p_44600_) {
18-
return this.getMinCost(p_44600_) + 22;
34+
public int getMaxCost(int level) {
35+
return this.getMinCost(level) + 22;
1936
}
2037

2138
public int getMaxLevel() {
@@ -26,4 +43,70 @@ public int getMaxLevel() {
2643
public boolean isTreasureOnly() {
2744
return true;
2845
}
46+
47+
@Override
48+
public boolean canEnchant(ItemStack stack) {
49+
return stack.getItem() instanceof ShieldItem;
50+
}
51+
52+
@Override
53+
public boolean checkCompatibility(@NotNull Enchantment enchantment) {
54+
return !(enchantment instanceof ShieldReflectionEnchantment) && !(enchantment instanceof ShieldRecoilEnchantment) && super.checkCompatibility(enchantment);
55+
}
56+
57+
public static void onTick(Player player) {
58+
CompoundTag tag = player.getPersistentData();
59+
if (player.isBlocking() && tag.getByte("shield_bashing") <= 0) {
60+
if (tag.getByte("bash_timer") < 30) {
61+
tag.putByte("bash_timer", (byte) (tag.getByte("bash_timer") + 1));
62+
if (tag.getByte("bash_timer") >= 30)
63+
player.playSound(SoundEvents.SHIELD_BLOCK, 1f, 1.65f);
64+
}
65+
else if (player.isCrouching() && player.isOnGround()) {
66+
//player.addEffect(new MobEffectInstance(MobEffects.MOVEMENT_SLOWDOWN, 10, 10, true, true, false));
67+
tag.putByte("shield_bashing", (byte) 12);
68+
MCUtils.applyModifier(player, ForgeMod.STEP_HEIGHT_ADDITION.get(), UUID.fromString("259f26e7-6914-4856-a01f-8b2295e6d244"), "Shield Bash step height", 0.6d, AttributeModifier.Operation.ADDITION, false);
69+
float f = -Mth.sin(player.getYRot() * ((float) Math.PI / 180F));
70+
float f1 = Mth.cos(player.getYRot() * ((float) Math.PI / 180F));
71+
player.playSound(SoundEvents.SHIELD_BLOCK, 1f, 2.0f);
72+
player.setDeltaMovement(player.getDeltaMovement().add(f * 2.75d, 0.4d, f1 * 2.75d));
73+
for (int i = 0; i < 50; i++) {
74+
player.level.addParticle(ParticleTypes.CLOUD, player.getX() + Mth.nextDouble(player.getRandom(), -0.5d, 0.5d), player.getY() + Mth.nextDouble(player.getRandom(), -0.5d, 0.5d) + 0.9d, player.getZ() + Mth.nextDouble(player.getRandom(), -0.5d, 0.5d), 0.1, 0, 0.1);
75+
}
76+
tag.putByte("bash_timer", (byte) -30);
77+
}
78+
}
79+
else if (tag.getByte("bash_timer") > 30){
80+
tag.putByte("bash_timer", (byte) 0);
81+
}
82+
else {
83+
tag.putByte("bash_timer", (byte) (tag.getByte("bash_timer") + 1));
84+
}
85+
86+
if (tag.getByte("shield_bashing") > 0) {
87+
tag.putByte("shield_bashing", (byte) (tag.getByte("shield_bashing") - 1));
88+
if (tag.getByte("shield_bashing") == 0) {
89+
if (player.getAttribute(ForgeMod.STEP_HEIGHT_ADDITION.get()) != null)
90+
//noinspection ConstantConditions
91+
player.getAttribute(ForgeMod.STEP_HEIGHT_ADDITION.get()).removeModifier(UUID.fromString("259f26e7-6914-4856-a01f-8b2295e6d244"));
92+
}
93+
if (player.isBlocking()) {
94+
damageAndKnockback(player);
95+
}
96+
}
97+
}
98+
99+
public static void damageAndKnockback(Player player) {
100+
AABB aabb = player.getBoundingBox().inflate(1.25d, 0.5d, 1.25d);
101+
List<LivingEntity> entities = player.level.getEntitiesOfClass(LivingEntity.class, aabb, entity -> entity != player);
102+
for (LivingEntity entity : entities) {
103+
if (entity.isDeadOrDying() || entity.invulnerableTime > 10)
104+
continue;
105+
entity.knockback(1.5d, player.getX() - entity.getX(), player.getZ() - entity.getZ());
106+
if (entity.hurt(DamageSource.playerAttack(player), 5)) {
107+
player.playSound(SoundEvents.SHIELD_BLOCK, 1.0f, 0.5f);
108+
ShieldAblazeEnchantment.apply(player, entity);
109+
}
110+
}
111+
}
29112
}

src/main/java/com/insane96mcp/shieldsplus/world/item/enchantment/ShieldLightweightEnchantment.java

+25
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package com.insane96mcp.shieldsplus.world.item.enchantment;
22

3+
import com.insane96mcp.shieldsplus.setup.SPEnchantments;
34
import net.minecraft.world.entity.EquipmentSlot;
5+
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
6+
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
7+
import net.minecraft.world.entity.ai.attributes.Attributes;
8+
import net.minecraft.world.entity.player.Player;
49
import net.minecraft.world.item.ItemStack;
510
import net.minecraft.world.item.ShieldItem;
611
import net.minecraft.world.item.enchantment.Enchantment;
712
import net.minecraft.world.item.enchantment.EnchantmentCategory;
13+
import net.minecraft.world.item.enchantment.EnchantmentHelper;
814

915
import java.util.UUID;
1016

@@ -33,4 +39,23 @@ public int getMaxLevel() {
3339
public boolean canApplyAtEnchantingTable(ItemStack stack) {
3440
return stack.getItem() instanceof ShieldItem;
3541
}
42+
43+
public static void onTick(Player player) {
44+
AttributeInstance attribute = player.getAttribute(Attributes.MOVEMENT_SPEED);
45+
if (attribute == null)
46+
return;
47+
48+
if (player.isBlocking()) {
49+
int lightweight = EnchantmentHelper.getItemEnchantmentLevel(SPEnchantments.LIGHTWEIGHT.get(), player.getUseItem());
50+
if (lightweight > 0) {
51+
52+
if (attribute.getModifier(ShieldLightweightEnchantment.BONUS_SPEED_UUID) == null) {
53+
attribute.addTransientModifier(new AttributeModifier(BONUS_SPEED_UUID, "Lightweight bonus speed", BONUS_SPEED * lightweight, AttributeModifier.Operation.MULTIPLY_BASE));
54+
}
55+
}
56+
}
57+
else if (attribute.getModifier(ShieldLightweightEnchantment.BONUS_SPEED_UUID) != null) {
58+
attribute.removeModifier(ShieldLightweightEnchantment.BONUS_SPEED_UUID);
59+
}
60+
}
3661
}

src/main/java/com/insane96mcp/shieldsplus/world/item/enchantment/ShieldRecoilEnchantment.java

+19
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package com.insane96mcp.shieldsplus.world.item.enchantment;
22

3+
import com.insane96mcp.shieldsplus.setup.SPEnchantments;
4+
import net.minecraft.world.damagesource.DamageSource;
35
import net.minecraft.world.entity.EquipmentSlot;
6+
import net.minecraft.world.entity.LivingEntity;
7+
import net.minecraft.world.entity.projectile.Projectile;
48
import net.minecraft.world.item.ItemStack;
59
import net.minecraft.world.item.ShieldItem;
610
import net.minecraft.world.item.enchantment.Enchantment;
711
import net.minecraft.world.item.enchantment.EnchantmentCategory;
12+
import net.minecraft.world.item.enchantment.EnchantmentHelper;
813

914
public class ShieldRecoilEnchantment extends Enchantment {
1015

@@ -31,4 +36,18 @@ public int getMaxLevel() {
3136
public boolean canApplyAtEnchantingTable(ItemStack stack) {
3237
return stack.getItem() instanceof ShieldItem;
3338
}
39+
40+
public static void onBlocked(LivingEntity blockingEntity, DamageSource source) {
41+
ItemStack shield = blockingEntity.getUseItem();
42+
int recoil = EnchantmentHelper.getItemEnchantmentLevel(SPEnchantments.RECOIL.get(), shield);
43+
if (recoil > 0)
44+
{
45+
if (source.getEntity() instanceof LivingEntity sourceEntity && source.getEntity() == source.getDirectEntity()) {
46+
sourceEntity.knockback(recoil * ShieldRecoilEnchantment.KNOCKBACK, blockingEntity.getX() - sourceEntity.getX(), blockingEntity.getZ() - sourceEntity.getZ());
47+
}
48+
else if (source.getDirectEntity() instanceof Projectile projectile) {
49+
projectile.setDeltaMovement(projectile.getDeltaMovement().scale(recoil * ShieldRecoilEnchantment.PROJECTILE_KNOCKBACK));
50+
}
51+
}
52+
}
3453
}

src/main/java/com/insane96mcp/shieldsplus/world/item/enchantment/ShieldReflectionEnchantment.java

+11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.insane96mcp.shieldsplus.world.item.enchantment;
22

33
import com.insane96mcp.shieldsplus.setup.SPEnchantments;
4+
import net.minecraft.world.damagesource.DamageSource;
45
import net.minecraft.world.entity.EquipmentSlot;
6+
import net.minecraft.world.entity.LivingEntity;
57
import net.minecraft.world.item.ItemStack;
68
import net.minecraft.world.item.ShieldItem;
79
import net.minecraft.world.item.enchantment.Enchantment;
@@ -58,4 +60,13 @@ public static float getBlockedDamageReduction(int level) {
5860
public static float getBlockedDamageReduction(ItemStack stack) {
5961
return getBlockedDamageReduction(EnchantmentHelper.getItemEnchantmentLevel(SPEnchantments.REINFORCED.get(), stack));
6062
}
63+
64+
public static void onBlocked(LivingEntity blockingEntity, DamageSource source, float amount) {
65+
if (!(source.getEntity() instanceof LivingEntity sourceEntity && source.getEntity() == source.getDirectEntity()))
66+
return;
67+
ItemStack shield = blockingEntity.getUseItem();
68+
int reflection = EnchantmentHelper.getItemEnchantmentLevel(SPEnchantments.REFLECTION.get(), shield);
69+
if (reflection > 0)
70+
sourceEntity.hurt(DamageSource.thorns(blockingEntity), Math.min(ShieldReflectionEnchantment.getReflectedDamage(reflection) * amount, ShieldReflectionEnchantment.getCappedReflectedDamage(reflection)));
71+
}
6172
}

0 commit comments

Comments
 (0)