|
1 | 1 | package com.gregtechceu.gtceu.common.item.tool.behavior; |
2 | 2 |
|
| 3 | +import com.gregtechceu.gtceu.GTCEu; |
| 4 | +import com.gregtechceu.gtceu.api.GTValues; |
3 | 5 | import com.gregtechceu.gtceu.api.item.tool.GTToolType; |
4 | 6 | import com.gregtechceu.gtceu.api.item.tool.ToolHelper; |
5 | 7 | import com.gregtechceu.gtceu.api.item.tool.behavior.IToolBehavior; |
| 8 | +import com.gregtechceu.gtceu.common.data.GTSoundEntries; |
6 | 9 | import com.gregtechceu.gtceu.common.data.item.GTToolActions; |
| 10 | +import com.gregtechceu.gtceu.data.recipe.CustomTags; |
7 | 11 |
|
| 12 | +import net.minecraft.core.BlockPos; |
8 | 13 | import net.minecraft.nbt.CompoundTag; |
9 | 14 | import net.minecraft.network.chat.Component; |
| 15 | +import net.minecraft.server.level.ServerLevel; |
10 | 16 | import net.minecraft.world.InteractionHand; |
| 17 | +import net.minecraft.world.InteractionResult; |
11 | 18 | import net.minecraft.world.InteractionResultHolder; |
12 | 19 | import net.minecraft.world.entity.player.Player; |
13 | 20 | import net.minecraft.world.item.ItemStack; |
14 | 21 | import net.minecraft.world.item.TooltipFlag; |
| 22 | +import net.minecraft.world.item.context.UseOnContext; |
15 | 23 | import net.minecraft.world.level.Level; |
| 24 | +import net.minecraft.world.level.block.Block; |
| 25 | +import net.minecraft.world.level.block.state.BlockState; |
| 26 | +import net.minecraft.world.phys.BlockHitResult; |
16 | 27 | import net.minecraftforge.common.ToolAction; |
17 | 28 |
|
18 | 29 | import lombok.Getter; |
19 | 30 | import org.jetbrains.annotations.NotNull; |
20 | 31 | import org.jetbrains.annotations.Nullable; |
21 | 32 |
|
22 | 33 | import java.util.List; |
| 34 | +import java.util.Set; |
23 | 35 |
|
24 | 36 | import static com.gregtechceu.gtceu.api.item.tool.ToolHelper.getBehaviorsTag; |
25 | 37 |
|
@@ -49,13 +61,40 @@ public void addBehaviorNBT(@NotNull ItemStack stack, @NotNull CompoundTag tag) { |
49 | 61 | IToolBehavior.super.addBehaviorNBT(stack, tag); |
50 | 62 | } |
51 | 63 |
|
| 64 | + @Override |
| 65 | + public @NotNull InteractionResult onItemUse(UseOnContext context) { |
| 66 | + Level world = context.getLevel(); |
| 67 | + BlockHitResult blockHitResult = context.getHitResult(); |
| 68 | + Player player = context.getPlayer(); |
| 69 | + ItemStack itemStack = context.getItemInHand(); |
| 70 | + BlockState state = world.getBlockState(blockHitResult.getBlockPos()); |
| 71 | + BlockPos pos = blockHitResult.getBlockPos(); |
| 72 | + Set<GTToolType> toolTypes = ToolHelper.getToolTypes(itemStack); |
| 73 | + // Copied and adapted from |
| 74 | + // https://github.com/Creators-of-Create/Create/blob/mc1.20.1/dev/src/main/java/com/simibubi/create/content/equipment/wrench/WrenchItem.java |
| 75 | + if (toolTypes.contains(GTToolType.WRENCH) && GTCEu.Mods.isCreateLoaded() && |
| 76 | + state.is(CustomTags.CREATE_WRENCH_PICKUP)) { |
| 77 | + if (!(world instanceof ServerLevel serverLevel)) |
| 78 | + return InteractionResult.SUCCESS; |
| 79 | + if (player != null && !player.isCreative()) |
| 80 | + Block.getDrops(state, serverLevel, pos, world.getBlockEntity(pos), player, itemStack) |
| 81 | + .forEach(stack -> player.getInventory().placeItemBackInInventory(stack)); |
| 82 | + state.spawnAfterBreak(serverLevel, pos, ItemStack.EMPTY, true); |
| 83 | + world.destroyBlock(pos, false); |
| 84 | + GTSoundEntries.WRENCH_TOOL.playOnServer(serverLevel, pos, 1, GTValues.RNG.nextFloat() * .5f + .5f); |
| 85 | + return InteractionResult.SUCCESS; |
| 86 | + } |
| 87 | + |
| 88 | + if (player != null) world.getBlockState(pos).use(world, player, context.getHand(), blockHitResult); |
| 89 | + return InteractionResult.SUCCESS; |
| 90 | + } |
| 91 | + |
52 | 92 | @Override |
53 | 93 | public @NotNull InteractionResultHolder<ItemStack> onItemRightClick(@NotNull Level world, @NotNull Player player, |
54 | 94 | @NotNull InteractionHand hand) { |
55 | 95 | var itemStack = player.getItemInHand(hand); |
56 | 96 | var tagCompound = getBehaviorsTag(itemStack); |
57 | 97 | if (player.isShiftKeyDown()) { |
58 | | - |
59 | 98 | var toolTypes = ToolHelper.getToolTypes(itemStack); |
60 | 99 | if (toolTypes.contains(GTToolType.WRENCH)) { |
61 | 100 | tagCompound.putByte("Mode", |
|
0 commit comments