9
0
mirror of https://github.com/Samsuik/Sakura.git synced 2025-12-28 19:29:07 +00:00

Some changes to blockable swords

This commit is contained in:
Samsuik
2025-04-14 23:15:03 +01:00
parent 6144bc7fcc
commit d146c38219
7 changed files with 86 additions and 57 deletions

View File

@@ -1,5 +1,7 @@
package me.samsuik.sakura.player.combat;
import me.samsuik.sakura.player.item.BlockableSwordItem;
import me.samsuik.sakura.player.item.DataComponentHelper;
import net.minecraft.core.Holder;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.RegistryAccess;
@@ -23,6 +25,12 @@ import org.apache.commons.lang3.mutable.MutableFloat;
import java.util.OptionalDouble;
public final class CombatUtil {
public static boolean overrideBlockingAndHalveDamage(ItemStack stack, LivingEntity entity) {
return stack.getItem() instanceof BlockableSwordItem swordItem && swordItem.isSafeToOverrideBlocking(stack)
|| stack.is(Items.SHIELD) && !DataComponentHelper.itemHasComponent(stack, DataComponents.BLOCKS_ATTACKS)
&& entity.level().sakuraConfig().players.combat.shieldDamageReduction;
}
public static double getLegacyAttackDifference(ItemStack itemstack) {
ItemAttributeModifiers defaultModifiers = itemstack.getItem().components().get(DataComponents.ATTRIBUTE_MODIFIERS);
if (defaultModifiers != null && !defaultModifiers.modifiers().isEmpty()) { // exists

View File

@@ -1,6 +1,7 @@
package me.samsuik.sakura.player.item;
import me.samsuik.sakura.configuration.GlobalConfiguration;
import net.minecraft.core.component.DataComponentMap;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.component.PatchedDataComponentMap;
import net.minecraft.core.registries.BuiltInRegistries;
@@ -17,7 +18,7 @@ import org.jspecify.annotations.NullMarked;
@NullMarked
public final class BlockableSwordItem extends Item {
private static final Consumable BLOCKING_ANIMATION = Consumable.builder()
.consumeSeconds(720000)
.consumeSeconds(3600)
.animation(ItemUseAnimation.BLOCK)
.sound(BuiltInRegistries.SOUND_EVENT.wrapAsHolder(SoundEvents.EMPTY))
.hasConsumeParticles(false)
@@ -29,32 +30,40 @@ public final class BlockableSwordItem extends Item {
@Override
public void modifyComponentsSentToClient(PatchedDataComponentMap components) {
if (blockWithSwords()) {
if (hasCustomAnimationOrDisabled(components)) {
// When updating to 1.22 change CONSUMABLE to BLOCK_ATTACKS
components.set(DataComponents.CONSUMABLE, BLOCKING_ANIMATION);
}
}
@Override
public InteractionResult use(Level level, Player player, InteractionHand hand) {
if (blockWithSwords()) {
ItemStack itemInHand = player.getItemInHand(hand);
return BLOCKING_ANIMATION.startConsuming(player, itemInHand, hand);
final ItemStack stack = player.getItemInHand(hand);
if (hasCustomAnimationOrDisabled(stack.getComponents())) {
return super.use(level, player, hand);
} else {
player.startUsingItem(hand);
return InteractionResult.CONSUME;
}
return super.use(level, player, hand);
}
@Override
public ItemUseAnimation getUseAnimation(ItemStack stack) {
return blockWithSwords() ? ItemUseAnimation.BLOCK : super.getUseAnimation(stack);
}
@Override
public int getUseDuration(ItemStack stack, LivingEntity entity) {
return blockWithSwords() ? 720000 : super.getUseDuration(stack, entity);
if (hasCustomAnimationOrDisabled(stack.getComponents())) {
return super.getUseDuration(stack, entity);
} else {
return BLOCKING_ANIMATION.consumeTicks();
}
}
private static boolean blockWithSwords() {
GlobalConfiguration config = GlobalConfiguration.get();
return config != null && config.players.combat.blockWithSwords;
public boolean isSafeToOverrideBlocking(ItemStack stack) {
return !hasCustomAnimationOrDisabled(stack.getComponents());
}
private static boolean hasCustomAnimationOrDisabled(DataComponentMap componentMap) {
final GlobalConfiguration config = GlobalConfiguration.get();
return (config == null || !config.players.combat.blockWithSwords)
|| componentMap.has(DataComponents.CONSUMABLE)
|| componentMap.has(DataComponents.BLOCKS_ATTACKS);
}
}

View File

@@ -2,7 +2,9 @@ package me.samsuik.sakura.player.item;
import me.samsuik.sakura.configuration.GlobalConfiguration;
import net.minecraft.core.component.DataComponentMap;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.core.component.DataComponents;
import net.minecraft.world.item.ItemStack;
public final class DataComponentHelper {
public static int bucketMaxStackSize() {
@@ -10,6 +12,11 @@ public final class DataComponentHelper {
return config == null || !config.players.bucketStackSize.isDefined() ? -1 : config.players.bucketStackSize.intValue();
}
@SuppressWarnings("OptionalAssignedToNull")
public static boolean itemHasComponent(ItemStack stack, DataComponentType<?> component) {
return stack.getComponentsPatch().get(component) != null;
}
public static DataComponentMap copyComponentsAndModifyMaxStackSize(DataComponentMap componentMap, int maxItemSize) {
if (maxItemSize > 0 && maxItemSize <= 99) {
return DataComponentMap.builder()

View File

@@ -16,10 +16,8 @@ import net.minecraft.world.level.Level;
import org.jspecify.annotations.NullMarked;
import java.util.List;
import java.util.Optional;
@NullMarked
@SuppressWarnings("OptionalAssignedToNull")
public final class LegacyGoldenAppleItem extends Item {
private static final Consumable LEGACY_ENCHANTED_GOLDEN_APPLE = Consumables.defaultFood()
.onConsume(
@@ -40,8 +38,8 @@ public final class LegacyGoldenAppleItem extends Item {
@Override
public InteractionResult use(Level level, Player player, InteractionHand hand) {
ItemStack stack = player.getItemInHand(hand);
if (this.itemHasConsumableComponent(stack, level)) {
final ItemStack stack = player.getItemInHand(hand);
if (isItemConsumableOrDisabled(stack, level)) {
return super.use(level, player, hand);
} else {
return LEGACY_ENCHANTED_GOLDEN_APPLE.startConsuming(player, stack, hand);
@@ -50,15 +48,15 @@ public final class LegacyGoldenAppleItem extends Item {
@Override
public ItemStack finishUsingItem(ItemStack stack, Level level, LivingEntity entity) {
if (this.itemHasConsumableComponent(stack, level)) {
if (isItemConsumableOrDisabled(stack, level)) {
return super.finishUsingItem(stack, level, entity);
} else {
return LEGACY_ENCHANTED_GOLDEN_APPLE.onConsume(level, entity, stack);
}
}
private boolean itemHasConsumableComponent(ItemStack stack, Level level) {
Optional<?> consumable = stack.getComponentsPatch().get(DataComponents.CONSUMABLE);
return consumable != null || !level.sakuraConfig().players.combat.oldEnchantedGoldenApple;
private static boolean isItemConsumableOrDisabled(ItemStack stack, Level level) {
return DataComponentHelper.itemHasComponent(stack, DataComponents.CONSUMABLE)
|| !level.sakuraConfig().players.combat.oldEnchantedGoldenApple;
}
}

View File

@@ -6,7 +6,7 @@ import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap;
import me.samsuik.sakura.player.gui.ItemStackUtil;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.StringUtils;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;