开箱使用工具改为tag为tools的物品,开箱会根据内部实体碰撞箱大小决定是否能够打开箱子

This commit is contained in:
Atsuihsio 2024-11-29 10:43:33 +08:00
parent 23bf1b1737
commit 24f5351c83
4 changed files with 38 additions and 8 deletions

View file

@ -1,4 +1,4 @@
// 1.20.1 2024-11-27T01:38:22.7956251 Tags for minecraft:item mod id superbwarfare
// 1.20.1 2024-11-29T10:14:35.4350334 Tags for minecraft:item mod id superbwarfare
cf8d49a8d2872286d1a9c3d4a7e1c2b7690db0b8 data/forge/tags/items/dusts.json
0fa06c2ff83bf09797e3ddff90f62d1124e645b4 data/forge/tags/items/dusts/coal_coke.json
295ddf906b7133a0558d03e9a60eea18281fe430 data/forge/tags/items/dusts/iron.json
@ -26,6 +26,7 @@ af6fd64b4a685c353f243763db268f7b2304c009 data/forge/tags/items/storage_blocks/le
30d1daa7590f7364b5bc2dc777a00caf2f5eecb0 data/forge/tags/items/storage_blocks/silver.json
3743c2790deca7e779d3e4b82f092de39b446988 data/forge/tags/items/storage_blocks/steel.json
144e54d908121dee498b2ebba3b24b09fd6f18d9 data/forge/tags/items/storage_blocks/tungsten.json
ce4893378c0c18730fabad876dba059e91e9a0b1 data/forge/tags/items/tools.json
4d4dfd25a2a749e000e1bb742c2f984d4e094e5d data/superbwarfare/tags/items/cannot_reload.json
cedce5ff7bbc95b40cbfced0f03a3da712118049 data/superbwarfare/tags/items/can_apply_barrel.json
4b0864ba4b2bb2ca3b30bdc2fefe3077f5020466 data/superbwarfare/tags/items/can_apply_grip.json

View file

@ -0,0 +1,5 @@
{
"values": [
"superbwarfare:crowbar"
]
}

View file

@ -1,8 +1,9 @@
package com.atsuishio.superbwarfare.block;
import com.atsuishio.superbwarfare.block.entity.ContainerBlockEntity;
import com.atsuishio.superbwarfare.entity.ICannonEntity;
import com.atsuishio.superbwarfare.init.ModBlockEntities;
import com.atsuishio.superbwarfare.init.ModItems;
import com.atsuishio.superbwarfare.init.ModEntities;
import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
@ -10,6 +11,8 @@ import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.ItemStack;
@ -29,6 +32,7 @@ import net.minecraft.world.level.block.state.properties.DirectionProperty;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraftforge.common.Tags;
import javax.annotation.Nullable;
import java.util.List;
@ -48,13 +52,16 @@ public class ContainerBlock extends BaseEntityBlock {
public InteractionResult use(BlockState pState, Level pLevel, BlockPos pPos, Player pPlayer, InteractionHand pHand, BlockHitResult pHit) {
if (!pLevel.isClientSide) {
ItemStack stack = pPlayer.getItemInHand(pHand);
if (stack.is(ModItems.CROWBAR.get())) {
if (stack.is(Tags.Items.TOOLS)) {
BlockEntity blockEntity = pLevel.getBlockEntity(pPos);
if (!(blockEntity instanceof ContainerBlockEntity containerBlockEntity)) return InteractionResult.PASS;
if (!hasEntity(pLevel, pPos)) {
pPlayer.displayClientMessage(Component.translatable("des.superbwarfare.container.fail.empty"), true);
return InteractionResult.PASS;
}
if (canOpen(pLevel, pPos)) {
if (canOpen(pLevel, pPos, containerBlockEntity.entityType, containerBlockEntity.entity)) {
pLevel.setBlockAndUpdate(pPos, pState.setValue(OPENED, true));
return InteractionResult.SUCCESS;
} else {
@ -73,12 +80,27 @@ public class ContainerBlock extends BaseEntityBlock {
return containerBlockEntity.entity != null || containerBlockEntity.entityType != null;
}
public boolean canOpen(Level pLevel, BlockPos pPos) {
public boolean canOpen(Level pLevel, BlockPos pPos, EntityType entityType, Entity entity) {
boolean flag = true;
for (int i = -4; i < 5; i++) {
for (int j = 0; j < 7; j++) {
for (int k = -4; k < 5; k++) {
int w = 0;
int h = 0;
boolean extra = entityType == ModEntities.MLE_1934.get() || entityType == ModEntities.MK_42.get() || entity instanceof ICannonEntity;
if (entityType != null) {
w = (int) (entityType.getDimensions().width / 2 + (extra ? 2 : 0));
h = (int) (entityType.getDimensions().height + (extra ? 2 : 0));
}
if (entity != null) {
w = (int) (entity.getType().getDimensions().width / 2 + (extra ? 2 : 0));
h = (int) (entity.getType().getDimensions().height + (extra ? 2 : 0));
}
for (int i = -w; i < w + 1; i++) {
for (int j = 0; j < h; j++) {
for (int k = -w; k < w + 1; k++) {
if (i == 0 && j == 0 && k == 0) {
continue;
}

View file

@ -60,6 +60,8 @@ public class ModItemTagProvider extends ItemTagsProvider {
this.tag(Tags.Items.ORES_IN_GROUND_STONE).add(ModItems.GALENA_ORE.get(), ModItems.SCHEELITE_ORE.get(), ModItems.SILVER_ORE.get());
this.tag(Tags.Items.ORES_IN_GROUND_DEEPSLATE).add(ModItems.DEEPSLATE_GALENA_ORE.get(), ModItems.DEEPSLATE_SCHEELITE_ORE.get(), ModItems.DEEPSLATE_SILVER_ORE.get());
this.tag(Tags.Items.TOOLS).add(ModItems.CROWBAR.get());
this.tag(forgeTag("plates")).addTags(forgeTag("plates/copper"));
this.tag(forgeTag("plates/copper")).add(ModItems.COPPER_PLATE.get());