From 63615132f3b15cb9427d819c6600b210478a54de Mon Sep 17 00:00:00 2001 From: Zsolt Molnar Date: Thu, 4 May 2023 23:49:31 +0200 Subject: [PATCH] Reworked support for Sweeping Edge --- CHANGELOG.md | 4 ++ .../net/bettercombat/client/MathHelper.java | 7 --- .../net/bettercombat/config/ServerConfig.java | 24 +++++++++- .../bettercombat/mixin/PlayerEntityMixin.java | 2 +- .../mixin/SweepingEnchantmentMixin.java | 4 +- .../mixin/client/ClientPlayerEntityMixin.java | 2 +- .../bettercombat/network/ServerNetwork.java | 46 +++++++++++++++++-- .../java/net/bettercombat/utils/MathHelp.java | 7 --- .../net/bettercombat/utils/MathHelper.java | 22 +++++++++ gradle.properties | 4 +- 10 files changed, 98 insertions(+), 24 deletions(-) delete mode 100644 common/src/main/java/net/bettercombat/client/MathHelper.java delete mode 100644 common/src/main/java/net/bettercombat/utils/MathHelp.java create mode 100644 common/src/main/java/net/bettercombat/utils/MathHelper.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 42a3b4a3..75a15928 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 1.7.0 + +- Reworked support for Sweeping Edge + # 1.6.2 - Update claymore pose diff --git a/common/src/main/java/net/bettercombat/client/MathHelper.java b/common/src/main/java/net/bettercombat/client/MathHelper.java deleted file mode 100644 index ec3bcdb3..00000000 --- a/common/src/main/java/net/bettercombat/client/MathHelper.java +++ /dev/null @@ -1,7 +0,0 @@ -package net.bettercombat.client; - -public class MathHelper { - public static double easeOutCubic(double number) { - return 1.0 - Math.pow(1.0 - number, 3); - } -} diff --git a/common/src/main/java/net/bettercombat/config/ServerConfig.java b/common/src/main/java/net/bettercombat/config/ServerConfig.java index cad1b733..94ee9cc1 100644 --- a/common/src/main/java/net/bettercombat/config/ServerConfig.java +++ b/common/src/main/java/net/bettercombat/config/ServerConfig.java @@ -27,7 +27,29 @@ public class ServerConfig implements ConfigData { (Note all hostile mobs hittable by default, this config is to fix faulty mobs)""") public String[] hostile_player_vehicles = {"alexsmobs:crocodile"}; @Comment("Allows vanilla sweeping mechanic to work and Sweeping Edge enchantment") - public boolean allow_sweeping = true; + public boolean allow_vanilla_sweeping = false; + @Comment("Allows new sweeping mechanic (by Better Combat) to work, including Sweeping Edge enchantment") + public boolean allow_reworked_sweeping = true; + @Comment(""" + The more additional targets a weapon swing hits, the weaker it will get. + Entities struck (+1) in a swing more than this, won't get weakened any further. + """) + public int reworked_sweeping_extra_target_count = 4; + @Comment(""" + Determines how weak the attack becomes when striking `reworked_sweeping_extra_target_count + 1` targets. + Example values: + - `0.5` -50% damage + """) + public float reworked_sweeping_maximum_damage_penalty = 0.5F; + @Comment(""" + The maximum level Sweeping Edge enchantment applied to the attackers weapon will restore this amount of penalty. + Example values: + - `0.5` restores 50% damage penalty when 3 levels are applied, so 16.66% when 1 level is applied + """) + public float reworked_sweeping_enchant_restores = 0.5F; + public boolean reworked_sweeping_plays_sound = true; + public boolean reworked_sweeping_emits_particles = true; + public boolean reworked_sweeping_sound_and_particles_only_for_swords = true; @Comment("Allows client-side target search to ignore obstacles. WARNING! Setting this to `false` significantly increases the load on clients.") public boolean allow_attacking_thru_walls = false; @Comment("Applies movement speed multiplier while attacking. (Min: 0, Max: 1). Use `0` for a full stop while attacking. Use `1` for no movement speed penalty") diff --git a/common/src/main/java/net/bettercombat/mixin/PlayerEntityMixin.java b/common/src/main/java/net/bettercombat/mixin/PlayerEntityMixin.java index 1891c537..649112ec 100644 --- a/common/src/main/java/net/bettercombat/mixin/PlayerEntityMixin.java +++ b/common/src/main/java/net/bettercombat/mixin/PlayerEntityMixin.java @@ -49,7 +49,7 @@ public void post_Tick(CallbackInfo ci) { @ModifyVariable(method = "attack", at = @At("STORE"), ordinal = 3) private boolean disableSweeping(boolean value) { - if (BetterCombat.config.allow_sweeping) { + if (BetterCombat.config.allow_vanilla_sweeping) { return value; } diff --git a/common/src/main/java/net/bettercombat/mixin/SweepingEnchantmentMixin.java b/common/src/main/java/net/bettercombat/mixin/SweepingEnchantmentMixin.java index 41e33faf..764b5690 100644 --- a/common/src/main/java/net/bettercombat/mixin/SweepingEnchantmentMixin.java +++ b/common/src/main/java/net/bettercombat/mixin/SweepingEnchantmentMixin.java @@ -12,7 +12,9 @@ public class SweepingEnchantmentMixin { @Inject(method = "getMaxLevel", at = @At("HEAD"), cancellable = true) public void getMaxLevel_DisableSweeping(CallbackInfoReturnable cir) { - if (BetterCombat.config == null || BetterCombat.config.allow_sweeping) { + if (BetterCombat.config == null + || BetterCombat.config.allow_vanilla_sweeping + || BetterCombat.config.allow_reworked_sweeping) { return; } cir.setReturnValue(0); diff --git a/common/src/main/java/net/bettercombat/mixin/client/ClientPlayerEntityMixin.java b/common/src/main/java/net/bettercombat/mixin/client/ClientPlayerEntityMixin.java index e646f738..01f501c5 100644 --- a/common/src/main/java/net/bettercombat/mixin/client/ClientPlayerEntityMixin.java +++ b/common/src/main/java/net/bettercombat/mixin/client/ClientPlayerEntityMixin.java @@ -2,7 +2,7 @@ import net.bettercombat.BetterCombat; import net.bettercombat.api.MinecraftClient_BetterCombat; -import net.bettercombat.client.MathHelper; +import net.bettercombat.utils.MathHelper; import net.minecraft.client.MinecraftClient; import net.minecraft.client.network.ClientPlayerEntity; import org.spongepowered.asm.mixin.Mixin; diff --git a/common/src/main/java/net/bettercombat/network/ServerNetwork.java b/common/src/main/java/net/bettercombat/network/ServerNetwork.java index 38c5206c..6d791b78 100644 --- a/common/src/main/java/net/bettercombat/network/ServerNetwork.java +++ b/common/src/main/java/net/bettercombat/network/ServerNetwork.java @@ -11,12 +11,14 @@ import net.bettercombat.logic.WeaponRegistry; import net.bettercombat.logic.knockback.ConfigurableKnockback; import net.bettercombat.mixin.LivingEntityAccessor; -import net.bettercombat.utils.MathHelp; +import net.bettercombat.utils.MathHelper; import net.bettercombat.utils.SoundHelper; import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; import net.fabricmc.fabric.api.networking.v1.PlayerLookup; import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; +import net.minecraft.enchantment.EnchantmentHelper; +import net.minecraft.enchantment.Enchantments; import net.minecraft.entity.Entity; import net.minecraft.entity.ExperienceOrbEntity; import net.minecraft.entity.ItemEntity; @@ -26,9 +28,12 @@ import net.minecraft.entity.attribute.EntityAttributes; import net.minecraft.entity.decoration.ArmorStandEntity; import net.minecraft.entity.projectile.PersistentProjectileEntity; +import net.minecraft.item.SwordItem; import net.minecraft.network.PacketByteBuf; import net.minecraft.network.packet.c2s.play.PlayerInteractEntityC2SPacket; +import net.minecraft.particle.ParticleTypes; import net.minecraft.server.world.ServerWorld; +import net.minecraft.sound.SoundEvents; import net.minecraft.text.Text; import org.slf4j.Logger; @@ -39,6 +44,10 @@ public class ServerNetwork { private static PacketByteBuf configSerialized = PacketByteBufs.create(); + private static final UUID COMBO_DAMAGE_MODIFIER_ID = UUID.randomUUID(); + private static final UUID DUAL_WIELDING_MODIFIER_ID = UUID.randomUUID(); + private static final UUID SWEEPING_MODIFIER_ID = UUID.randomUUID(); + public static void initializeHandlers() { configSerialized = Packets.ConfigSync.write(BetterCombat.config); ServerPlayConnectionEvents.JOIN.register( (handler, sender, server) -> { @@ -88,6 +97,7 @@ public static void initializeHandlers() { ((PlayerAttackProperties)player).setComboCount(request.comboCount()); Multimap comboAttributes = null; Multimap dualWieldingAttributes = null; + Multimap sweepingModifiers = HashMultimap.create(); double range = 18.0; if (attributes != null && attack != null) { range = attributes.attackRange(); @@ -96,7 +106,7 @@ public static void initializeHandlers() { double comboMultiplier = attack.damageMultiplier() - 1; comboAttributes.put( EntityAttributes.GENERIC_ATTACK_DAMAGE, - new EntityAttributeModifier(UUID.randomUUID(), "COMBO_DAMAGE_MULTIPLIER", comboMultiplier, EntityAttributeModifier.Operation.MULTIPLY_BASE)); + new EntityAttributeModifier(COMBO_DAMAGE_MODIFIER_ID, "COMBO_DAMAGE_MULTIPLIER", comboMultiplier, EntityAttributeModifier.Operation.MULTIPLY_BASE)); player.getAttributes().addTemporaryModifiers(comboAttributes); var dualWieldingMultiplier = PlayerAttackHelper.getDualWieldingAttackDamageMultiplier(player, hand) - 1; @@ -104,7 +114,7 @@ public static void initializeHandlers() { dualWieldingAttributes = HashMultimap.create(); dualWieldingAttributes.put( EntityAttributes.GENERIC_ATTACK_DAMAGE, - new EntityAttributeModifier(UUID.randomUUID(), "DUAL_WIELDING_DAMAGE_MULTIPLIER", dualWieldingMultiplier, EntityAttributeModifier.Operation.MULTIPLY_TOTAL)); + new EntityAttributeModifier(DUAL_WIELDING_MODIFIER_ID, "DUAL_WIELDING_DAMAGE_MULTIPLIER", dualWieldingMultiplier, EntityAttributeModifier.Operation.MULTIPLY_TOTAL)); player.getAttributes().addTemporaryModifiers(dualWieldingAttributes); } @@ -113,17 +123,42 @@ public static void initializeHandlers() { } SoundHelper.playSound(world, player, attack.swingSound()); + + if (BetterCombat.config.allow_reworked_sweeping && request.entityIds().length > 1) { + double multiplier = 1.0 + - (BetterCombat.config.reworked_sweeping_maximum_damage_penalty / BetterCombat.config.reworked_sweeping_extra_target_count) + * Math.min(BetterCombat.config.reworked_sweeping_extra_target_count, request.entityIds().length - 1); + int sweepingLevel = EnchantmentHelper.getLevel(Enchantments.SWEEPING, hand.itemStack()); + double sweepingSteps = BetterCombat.config.reworked_sweeping_enchant_restores / ((double)Enchantments.SWEEPING.getMaxLevel()); + multiplier += sweepingLevel * sweepingSteps; + multiplier = Math.min(multiplier, 1); + sweepingModifiers.put( + EntityAttributes.GENERIC_ATTACK_DAMAGE, + new EntityAttributeModifier(SWEEPING_MODIFIER_ID, "SWEEPING_DAMAGE_MODIFIER", multiplier - 1, EntityAttributeModifier.Operation.MULTIPLY_TOTAL)); + // System.out.println("Applied sweeping multiplier " + multiplier + " , sweepingSteps " + sweepingSteps + " , enchant bonus: " + (sweepingLevel * sweepingSteps)); + player.getAttributes().addTemporaryModifiers(sweepingModifiers); + + boolean playEffects = !BetterCombat.config.reworked_sweeping_sound_and_particles_only_for_swords + || (hand.itemStack().getItem() instanceof SwordItem); + if (BetterCombat.config.reworked_sweeping_plays_sound && playEffects) { + world.playSound(null, player.getX(), player.getY(), player.getZ(), SoundEvents.ENTITY_PLAYER_ATTACK_SWEEP, player.getSoundCategory(), 1.0f, 1.0f); + } + if (BetterCombat.config.reworked_sweeping_emits_particles && playEffects) { + player.spawnSweepAttackParticles(); + } + } } var attackCooldown = PlayerAttackHelper.getAttackCooldownTicksCapped(player); var knockbackMultiplier = BetterCombat.config.knockback_reduced_for_fast_attacks - ? MathHelp.clamp(attackCooldown / 12.5F, 0.1F, 1F) + ? MathHelper.clamp(attackCooldown / 12.5F, 0.1F, 1F) : 1F; var lastAttackedTicks = ((LivingEntityAccessor)player).getLastAttackedTicks(); if (!useVanillaPacket) { player.setSneaking(request.isSneaking()); } + for (int entityId: request.entityIds()) { // getEntityById(entityId); boolean isBossPart = false; @@ -183,6 +218,9 @@ public static void initializeHandlers() { if (dualWieldingAttributes != null) { player.getAttributes().removeModifiers(dualWieldingAttributes); } + if (!sweepingModifiers.isEmpty()) { + player.getAttributes().removeModifiers(sweepingModifiers); + } ((PlayerAttackProperties)player).setComboCount(-1); }); }); diff --git a/common/src/main/java/net/bettercombat/utils/MathHelp.java b/common/src/main/java/net/bettercombat/utils/MathHelp.java deleted file mode 100644 index 00efffc0..00000000 --- a/common/src/main/java/net/bettercombat/utils/MathHelp.java +++ /dev/null @@ -1,7 +0,0 @@ -package net.bettercombat.utils; - -public class MathHelp { - public static float clamp(float value, float min, float max) { - return Math.max(Math.min(value, max), min); - } -} diff --git a/common/src/main/java/net/bettercombat/utils/MathHelper.java b/common/src/main/java/net/bettercombat/utils/MathHelper.java new file mode 100644 index 00000000..e6f86af2 --- /dev/null +++ b/common/src/main/java/net/bettercombat/utils/MathHelper.java @@ -0,0 +1,22 @@ +package net.bettercombat.utils; + +public class MathHelper { + // Generic + public static float clamp(float value, float min, float max) { + return Math.max(Math.min(value, max), min); + } + + // Easing + + public static double easeOutCubic(double number) { + return 1.0 - Math.pow(1.0 - number, 3); + } + + public double easeInExpo(double x) { + return x == 0 ? 0 : Math.pow(2, 10 * x - 10); + } + + public double easeOutExpo(double x) { + return x == 1 ? 1 : 1 - Math.pow(2, -10 * x); + } +} diff --git a/gradle.properties b/gradle.properties index cf90f354..f4cd2930 100755 --- a/gradle.properties +++ b/gradle.properties @@ -11,11 +11,11 @@ yarn_mappings=1.19.4+build.1 # Loader fabric_version=0.77.0+1.19.4 -loader_version=0.14.11 +loader_version=0.14.19 forge_version=45.0.43 # Mod Properties -mod_version=1.6.2 +mod_version=1.7.0 maven_group=net archives_base_name=bettercombat