Skip to content

Commit

Permalink
Fixed the Shields to be Enchantable and Repairable
Browse files Browse the repository at this point in the history
  • Loading branch information
Starexify committed Jan 17, 2025
1 parent f4f549e commit 8da0db5
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 42 deletions.
5 changes: 4 additions & 1 deletion src/client/java/net/nova/big_swords/data/BSEnchantments.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
import net.minecraft.component.EnchantmentEffectComponentTypes;
import net.minecraft.component.type.AttributeModifierSlot;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentLevelBasedValue;
import net.minecraft.enchantment.effect.EnchantmentEffectTarget;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.RegistryEntryLookup;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.RegistryWrapper;
import net.nova.big_swords.enchantments.effects.SoulStealEffect;
import net.nova.big_swords.init.BSEnchantmentEffects;
import net.nova.big_swords.init.BSItems;
import net.nova.big_swords.init.Tags;

import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -43,7 +46,7 @@ protected void configure(RegistryWrapper.WrapperLookup wrapperLookup, Entries en
EnchantmentEffectComponentTypes.POST_ATTACK,
EnchantmentEffectTarget.ATTACKER,
EnchantmentEffectTarget.VICTIM,
new SoulStealEffect(0)
new SoulStealEffect(EnchantmentLevelBasedValue.linear(0.3F), new ItemStack(BSItems.SOUL))
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@
"affected": "victim",
"effect": {
"type": "big_swords:soul_steal",
"duration": 0
"dropChance": {
"type": "minecraft:linear",
"base": 0.3,
"per_level_above_first": 0.3
},
"droppedItem": {
"count": 1,
"id": "big_swords:soul"
}
},
"enchanted": "attacker"
}
Expand Down
1 change: 0 additions & 1 deletion src/main/java/net/nova/big_swords/BigSwordsR.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ public void onInitialize() {
}
});


// Halloween Stuff
ServerEntityEvents.ENTITY_LOAD.register((entity, serverWorld) -> {
if (entity.getType().isIn(Tags.EntityTypeTags.HALLOWEEN_MOB) && entity instanceof MobEntity mob)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,34 @@
package net.nova.big_swords.enchantments.effects;

import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.enchantment.EnchantmentEffectContext;
import net.minecraft.enchantment.EnchantmentLevelBasedValue;
import net.minecraft.enchantment.effect.EnchantmentEntityEffect;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.math.Vec3d;
import net.nova.big_swords.BigSwordsR;
import net.nova.big_swords.init.BSItems;
import net.nova.big_swords.init.Tags;

public record SoulStealEffect(int duration) implements EnchantmentEntityEffect {
public static final MapCodec<SoulStealEffect> CODEC = RecordCodecBuilder.mapCodec(
inst -> inst.group(Codec.INT.fieldOf("duration").forGetter(p_345622_ -> p_345622_.duration)).apply(inst, SoulStealEffect::new)
);
public record SoulStealEffect(EnchantmentLevelBasedValue dropChance,
ItemStack droppedItem) implements EnchantmentEntityEffect {
public static final MapCodec<SoulStealEffect> CODEC = RecordCodecBuilder.mapCodec(inst -> inst.group(
EnchantmentLevelBasedValue.CODEC.fieldOf("dropChance").forGetter(SoulStealEffect::dropChance),
ItemStack.CODEC.fieldOf("droppedItem").forGetter(SoulStealEffect::droppedItem)
).apply(inst, SoulStealEffect::new));

@Override
public void apply(ServerWorld level, int enchantmentLevel, EnchantmentEffectContext context, Entity entity, Vec3d pos) {
public void apply(ServerWorld serverLevel, int enchantmentLevel, EnchantmentEffectContext context, Entity entity, Vec3d pos) {
if (entity instanceof LivingEntity livingEntity && !livingEntity.getType().isIn(Tags.EntityTypeTags.SOULLESS) && (livingEntity.isDead()) && context.stack().isIn(Tags.BSItemTags.SCYTHES)) {
double dropChance = switch (enchantmentLevel) {
case 1 -> 0.3;
case 2 -> 0.6;
case 3 -> 0.9;
default -> 0.0;
};

if (level.getRandom().nextDouble() < dropChance) {
livingEntity.dropItem(level, BSItems.SOUL);

// Spawn soul particles
level.spawnParticles(ParticleTypes.SOUL, pos.x, pos.y, pos.z, 20, 0.5, 0.5, 0.5, 0.05);
BigSwordsR.playSound(level, livingEntity, SoundEvents.PARTICLE_SOUL_ESCAPE.value());
double dropChance = this.dropChance.getValue(enchantmentLevel);
if (serverLevel.getRandom().nextDouble() < dropChance) {
livingEntity.dropItem(serverLevel, this.droppedItem.getItem());
serverLevel.spawnParticles(ParticleTypes.SOUL, pos.x, pos.y, pos.z, 20, 0.5, 0.5, 0.5, 0.05);
serverLevel.playSound(null, pos.x, pos.y, pos.z, SoundEvents.PARTICLE_SOUL_ESCAPE, entity.getSoundCategory(), 1, 1);
}
}
}
Expand Down
20 changes: 4 additions & 16 deletions src/main/java/net/nova/big_swords/item/ScytheItem.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.nova.big_swords.item;

import net.minecraft.block.BlockState;
import net.minecraft.enchantment.EnchantmentEffectContext;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.LivingEntity;
Expand All @@ -24,8 +23,6 @@
import net.minecraft.world.RaycastContext;
import net.minecraft.world.World;
import net.nova.big_swords.BigSwordsR;
import net.nova.big_swords.enchantments.effects.SoulStealEffect;
import net.nova.big_swords.init.BSEnchantmentEffects;
import net.nova.big_swords.init.BSItems;
import net.nova.big_swords.init.Sounds;

Expand Down Expand Up @@ -95,17 +92,8 @@ public boolean onStoppedUsing(ItemStack stack, World level, LivingEntity entity,
BlockHitResult blockHit = level.raycast(new RaycastContext(playerPos, targetPos, RaycastContext.ShapeType.COLLIDER, RaycastContext.FluidHandling.NONE, player));

if (blockHit.getType() == HitResult.Type.MISS) {
scytheHits(level, player, target);
scytheHits((ServerWorld) level, player, target);
entitiesHit++;

int soulStealerEnchantment = EnchantmentHelper.getLevel(BigSwordsR.getEnchantment(level, BSEnchantmentEffects.SOUL_STEALER), stack);
if (soulStealerEnchantment > 0) {
SoulStealEffect soulStealEffect = new SoulStealEffect(200);
soulStealEffect.apply((ServerWorld) level, soulStealerEnchantment,
new EnchantmentEffectContext(stack, entity.getPreferredEquipmentSlot(stack), entity),
target, target.getPos()
);
}
}
}
}
Expand Down Expand Up @@ -150,11 +138,11 @@ public boolean isInAttackArea(Vec3d toTarget, Vec3d lookVec) {
return inRadius && inFront && inHeight;
}

public void scytheHits(World level, PlayerEntity player, LivingEntity target) {
public void scytheHits(ServerWorld serverLevel, PlayerEntity player, LivingEntity target) {
float damage = minDamage + random.nextFloat() * (maxDamage - minDamage);
damage = Math.round(damage * 10.0f) / 10.0f;
if (level instanceof ServerWorld serverWorld)
target.damage(serverWorld, level.getDamageSources().playerAttack(player), damage);
target.damage(serverLevel, serverLevel.getDamageSources().playerAttack(player), damage);
EnchantmentHelper.onTargetDamaged(serverLevel, target, serverLevel.getDamageSources().playerAttack(player));
}

// Bow-like Item Stuff
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/nova/big_swords/item/TieredShield.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ public TieredShield(ToolMaterial toolMaterial, Settings properties) {
}

public TieredShield(ToolMaterial toolMaterial, Settings properties, int durabilityMultiplier) {
super(properties.maxDamage(toolMaterial.durability() * durabilityMultiplier));
super(properties.maxDamage(toolMaterial.durability() * durabilityMultiplier).enchantable(toolMaterial.enchantmentValue()).repairable(toolMaterial.repairItems()));
this.toolMaterial = toolMaterial;
}

public TieredShield(ToolMaterial toolMaterial, Settings properties, int durabilityMultiplier, int additionalDurability) {
super(properties.maxDamage(toolMaterial.durability() * durabilityMultiplier + additionalDurability));
super(properties.maxDamage(toolMaterial.durability() * durabilityMultiplier + additionalDurability).enchantable(toolMaterial.enchantmentValue()).repairable(toolMaterial.repairItems()));
this.toolMaterial = toolMaterial;
}

Expand Down

0 comments on commit 8da0db5

Please sign in to comment.