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

Allow blocking with swords

This commit is contained in:
Samsuik
2025-02-17 16:15:21 +00:00
parent 99950f433c
commit a1f50ad82f
11 changed files with 189 additions and 16 deletions

View File

@@ -41,6 +41,11 @@ public final class GlobalConfiguration extends ConfigurationPart {
public Players players;
public class Players extends ConfigurationPart {
public Combat combat = new Combat();
public class Combat extends ConfigurationPart {
public boolean blockWithSwords = false;
}
public IntOr.Default bucketStackSize = IntOr.Default.USE_DEFAULT;
public boolean stackableMilkBuckets = false;
}

View File

@@ -0,0 +1,60 @@
package me.samsuik.sakura.player.item;
import me.samsuik.sakura.configuration.GlobalConfiguration;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.component.PatchedDataComponentMap;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.*;
import net.minecraft.world.item.component.Consumable;
import net.minecraft.world.level.Level;
import org.jspecify.annotations.NullMarked;
@NullMarked
public final class BlockableSwordItem extends SwordItem {
private static final Consumable BLOCKING_ANIMATION = Consumable.builder()
.consumeSeconds(720000)
.animation(ItemUseAnimation.BLOCK)
.sound(BuiltInRegistries.SOUND_EVENT.wrapAsHolder(SoundEvents.EMPTY))
.hasConsumeParticles(false)
.build();
public BlockableSwordItem(ToolMaterial material, float attackDamage, float attackSpeed, Properties properties) {
super(material, attackDamage, attackSpeed, properties);
}
@Override
public void modifyComponentsSentToClient(PatchedDataComponentMap components) {
if (blockWithSwords()) {
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);
}
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);
}
private static boolean blockWithSwords() {
GlobalConfiguration config = GlobalConfiguration.get();
return config != null && config.players.combat.blockWithSwords;
}
}

View File

@@ -10,11 +10,6 @@ public final class DataComponentHelper {
return config == null || !config.players.bucketStackSize.isDefined() ? -1 : config.players.bucketStackSize.intValue();
}
public static boolean stackableMilkBuckets() {
GlobalConfiguration config = GlobalConfiguration.get();
return config != null && config.players.stackableMilkBuckets;
}
public static DataComponentMap copyComponentsAndModifyMaxStackSize(DataComponentMap componentMap, int maxItemSize) {
if (maxItemSize > 0 && maxItemSize <= 99) {
return DataComponentMap.builder()

View File

@@ -15,8 +15,13 @@ public final class MilkBucketItem extends Item {
@Override
public void verifyComponentsAfterLoad(ItemStack stack) {
int maxStackSize = DataComponentHelper.bucketMaxStackSize();
if (DataComponentHelper.stackableMilkBuckets() && maxStackSize > 0 && maxStackSize < 100) {
if (maxStackSize > 0 && maxStackSize < 100 && stackableMilkBuckets()) {
stack.set(DataComponents.MAX_STACK_SIZE, maxStackSize);
}
}
private static boolean stackableMilkBuckets() {
GlobalConfiguration config = GlobalConfiguration.get();
return config != null && config.players.stackableMilkBuckets;
}
}