Skip to content

Commit

Permalink
修改伐木触发
Browse files Browse the repository at this point in the history
  • Loading branch information
DancingSnow0517 committed Sep 30, 2024
1 parent e39df7a commit d095236
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import dev.dubhe.anvilcraft.init.ModBlockEntities;
import dev.dubhe.anvilcraft.init.ModBlocks;
import dev.dubhe.anvilcraft.init.ModItems;
import dev.dubhe.anvilcraft.util.Util;

import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.core.BlockPos;
Expand All @@ -20,6 +19,7 @@
import net.minecraft.util.RandomSource;
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.ItemStack;
import net.minecraft.world.item.Items;
Expand All @@ -45,7 +45,6 @@
import net.minecraft.world.phys.shapes.VoxelShape;

import com.mojang.serialization.MapCodec;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.annotation.Nonnull;
Expand Down Expand Up @@ -172,7 +171,7 @@ public InteractionResult use(
} else if (itemInHand.is(ItemTags.AXES)) {
level.setBlockAndUpdate(pos, state.setValue(COLOR, LightColor.PRIMARY));
itemInHand.hurtAndBreak(1, (ServerLevel) level, (ServerPlayer) player, item -> {
player.onEquippedItemBroken(item, Util.convertToSlot(hand));
player.onEquippedItemBroken(item, LivingEntity.getSlotForHand(hand));
});
return InteractionResult.CONSUME_PARTIAL;
} else if (itemInHand.is(ModItems.VOID_MATTER.asItem())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.minecraft.world.ItemInteractionResult;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
Expand All @@ -23,8 +24,6 @@
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;

import org.jetbrains.annotations.NotNull;

import javax.annotation.ParametersAreNonnullByDefault;

@ParametersAreNonnullByDefault
Expand Down Expand Up @@ -114,7 +113,7 @@ public InteractionResult use(
.setValue(
LayeredCauldronBlock.LEVEL,
level.getBlockState(pos).getValue(LayeredCauldronBlock.LEVEL)));
itemStack.hurtAndBreak(2, player, Util.convertToSlot(hand));
itemStack.hurtAndBreak(2, player, LivingEntity.getSlotForHand(hand));
return InteractionResult.SUCCESS;
}
if (itemStack.is(Items.FIRE_CHARGE)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
package dev.dubhe.anvilcraft.event;

import net.minecraft.world.entity.LivingEntity;
import dev.dubhe.anvilcraft.AnvilCraft;
import dev.dubhe.anvilcraft.util.ModEnchantmentHelper;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.event.entity.living.LivingDestroyBlockEvent;
import net.neoforged.neoforge.event.level.BlockEvent;

@EventBusSubscriber
public class BreakBlockEventListener {
@SubscribeEvent
public static void onBlockRemoved(LivingDestroyBlockEvent event) {
LivingEntity entity = event.getEntity();
//if (entity instanceof ServerPlayer serverPlayer && serverPlayer.getAbilities().instabuild) return;
ItemStack item = entity.getUseItem();
System.out.println("entity = " + entity);
System.out.println("item = " + item);
public static void onBlockRemoved(BlockEvent.BreakEvent event) {
AnvilCraft.LOGGER.info("onBlockRemoved");
Player player = event.getPlayer();
ItemStack stack = player.getMainHandItem();
AnvilCraft.LOGGER.info("{}, {}", player, stack);
ModEnchantmentHelper.onPostBreakBlock(
(ServerLevel) player.level(),
stack,
player,
EquipmentSlot.MAINHAND,
event.getPos().getCenter(),
event.getState()
);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package dev.dubhe.anvilcraft.event;

import dev.dubhe.anvilcraft.util.ModEnchantmentHelper;
import dev.dubhe.anvilcraft.util.Util;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.tags.ItemTags;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.context.UseOnContext;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
Expand All @@ -20,7 +20,7 @@ public static void onUseOnBlock(UseItemOnBlockEvent e) {
(ServerLevel) context.getLevel(),
context.getItemInHand(),
context.getPlayer(),
Util.convertToSlot(context.getHand()),
LivingEntity.getSlotForHand(context.getHand()),
context.getClickedPos().getCenter(),
context.getLevel().getBlockState(context.getClickedPos())
);
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/dev/dubhe/anvilcraft/item/AnvilHammerItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import dev.dubhe.anvilcraft.api.hammer.IHammerRemovable;
import dev.dubhe.anvilcraft.init.ModBlockTags;
import dev.dubhe.anvilcraft.network.RocketJumpPacket;
import dev.dubhe.anvilcraft.util.Util;

import net.minecraft.core.BlockPos;
import net.minecraft.core.component.DataComponents;
Expand Down Expand Up @@ -109,7 +108,7 @@ private static void dropAnvil(Player player, Level level, BlockPos blockPos) {
level.playSound(null, blockPos, SoundEvents.ANVIL_LAND, SoundSource.BLOCKS, 1f, 1f);
ItemStack itemStack = player.getItemInHand(player.getUsedItemHand());
if (itemStack.getItem() instanceof AnvilHammerItem) {
itemStack.hurtAndBreak(1, player, Util.convertToSlot(player.getUsedItemHand()));
itemStack.hurtAndBreak(1, player, LivingEntity.getSlotForHand(player.getUsedItemHand()));
}
}

Expand Down Expand Up @@ -185,7 +184,7 @@ public boolean mineBlock(
@NotNull BlockPos pos,
@NotNull LivingEntity miningEntity) {
if (state.getDestroySpeed(level, pos) != 0.0f) {
stack.hurtAndBreak(2, miningEntity, Util.convertToSlot(miningEntity.getUsedItemHand()));
stack.hurtAndBreak(2, miningEntity, LivingEntity.getSlotForHand(miningEntity.getUsedItemHand()));
}
return true;
}
Expand All @@ -196,7 +195,7 @@ protected float calculateFallDamageBonus(float fallDistance) {

@Override
public boolean hurtEnemy(@NotNull ItemStack stack, @NotNull LivingEntity target, @NotNull LivingEntity attacker) {
stack.hurtAndBreak(1, attacker, Util.convertToSlot(target.getUsedItemHand()));
stack.hurtAndBreak(1, attacker, LivingEntity.getSlotForHand(target.getUsedItemHand()));
float damageBonus = calculateFallDamageBonus(attacker.fallDistance);
target.hurt(target.level().damageSources().anvil(attacker), damageBonus);
if (attacker.fallDistance >= 3) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dev/dubhe/anvilcraft/item/MagnetItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import dev.dubhe.anvilcraft.api.event.item.UseMagnetEvent;
import dev.dubhe.anvilcraft.api.item.IChargerChargeable;
import dev.dubhe.anvilcraft.init.ModItems;
import dev.dubhe.anvilcraft.util.Util;

import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
Expand Down Expand Up @@ -39,7 +39,7 @@ public MagnetItem(Properties properties) {
player.position().add(radius, radius, radius));
level.getEntities(EntityTypeTest.forClass(ItemEntity.class), aabb, Entity::isAlive)
.forEach(e -> e.moveTo(player.position()));
item.hurtAndBreak(1, player, Util.convertToSlot(usedHand));
item.hurtAndBreak(1, player, LivingEntity.getSlotForHand(usedHand));
return InteractionResultHolder.sidedSuccess(item, level.isClientSide());
}

Expand Down
38 changes: 0 additions & 38 deletions src/main/java/dev/dubhe/anvilcraft/mixin/ItemMixin.java

This file was deleted.

12 changes: 0 additions & 12 deletions src/main/java/dev/dubhe/anvilcraft/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.ItemInteractionResult;
import net.minecraft.world.entity.EquipmentSlot;
import net.neoforged.fml.ModList;

import lombok.AccessLevel;
Expand Down Expand Up @@ -59,16 +57,6 @@ public static Function<InteractionResult, ItemInteractionResult> interactionResu
};
}

/**
*
*/
public static EquipmentSlot convertToSlot(InteractionHand hand) {
return switch (hand) {
case MAIN_HAND -> EquipmentSlot.MAINHAND;
case OFF_HAND -> EquipmentSlot.OFFHAND;
};
}

public static <E> Optional<List<E>> intoOptional(List<E> collection) {
if (collection.isEmpty()) return Optional.empty();
return Optional.of(collection);
Expand Down
3 changes: 1 addition & 2 deletions src/main/resources/anvilcraft.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
"forge.FallingBlockEntityMixin",
"forge.LevelChunkMixin",
"forge.LightningBoltMixin",
"forge.PistonMovingBlockEntityMixin",
"ItemMixin"
"forge.PistonMovingBlockEntityMixin"
],
"client": [
"ClientLevelMixin",
Expand Down

0 comments on commit d095236

Please sign in to comment.