添加卓越物品接口的基础方法

This commit is contained in:
17146 2025-06-15 00:35:33 +08:00 committed by Light_Quanta
parent 6c80f5af7f
commit f8d0c2e886
No known key found for this signature in database
GPG key ID: 11A39A1B8C890959
4 changed files with 119 additions and 39 deletions

View file

@ -1,21 +1,21 @@
package com.atsuishio.superbwarfare.block;
import com.atsuishio.superbwarfare.block.entity.SuperbItemInterfaceBlockEntity;
import com.atsuishio.superbwarfare.init.ModBlockEntities;
import com.mojang.serialization.MapCodec;
import net.minecraft.core.BlockPos;
import net.minecraft.core.component.DataComponents;
import net.minecraft.stats.Stats;
import net.minecraft.world.Containers;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.BaseEntityBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.HopperBlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
@ -29,7 +29,6 @@ import org.jetbrains.annotations.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
@SuppressWarnings("deprecation")
public class SuperbItemInterfaceBlock extends BaseEntityBlock {
public static final BooleanProperty ENABLED = BlockStateProperties.ENABLED;
@ -47,19 +46,25 @@ public class SuperbItemInterfaceBlock extends BaseEntityBlock {
@Override
@ParametersAreNonnullByDefault
public BlockEntity newBlockEntity(BlockPos pPos, BlockState pState) {
return null;
return new SuperbItemInterfaceBlockEntity(pPos, pState);
}
@Override
public void setPlacedBy(@NotNull Level pLevel, @NotNull BlockPos pPos, @NotNull BlockState pState, LivingEntity pPlacer, ItemStack pStack) {
if (pStack.get(DataComponents.CUSTOM_NAME) != null) {
BlockEntity blockentity = pLevel.getBlockEntity(pPos);
// if (blockentity instanceof HopperBlockEntity) {
// ((HopperBlockEntity) blockentity).setCustomName(pStack.getHoverName());
// }
}
@Nullable
@ParametersAreNonnullByDefault
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level pLevel, BlockState pState, BlockEntityType<T> pBlockEntityType) {
return pLevel.isClientSide ? null : createTickerHelper(pBlockEntityType, ModBlockEntities.SUPERB_ITEM_INTERFACE.get(), SuperbItemInterfaceBlockEntity::serverTick);
}
// @Override
// public void setPlacedBy(@NotNull Level pLevel, @NotNull BlockPos pPos, @NotNull BlockState pState, LivingEntity pPlacer, ItemStack pStack) {
// if (pStack.get(DataComponents.CUSTOM_NAME) != null) {
// BlockEntity blockentity = pLevel.getBlockEntity(pPos);
// if (blockentity instanceof SuperbItemInterfaceBlockEntity entity) {
// entity.setCustomName(pStack.getHoverName());
// }
// }
// }
@Override
public void onPlace(BlockState pState, @NotNull Level pLevel, @NotNull BlockPos pPos, BlockState pOldState, boolean pIsMoving) {
if (!pOldState.is(pState.getBlock())) {
@ -74,9 +79,8 @@ public class SuperbItemInterfaceBlock extends BaseEntityBlock {
return InteractionResult.SUCCESS;
} else {
BlockEntity blockentity = level.getBlockEntity(pos);
if (blockentity instanceof HopperBlockEntity) {
player.openMenu((HopperBlockEntity) blockentity);
player.awardStat(Stats.INSPECT_HOPPER);
if (blockentity instanceof SuperbItemInterfaceBlockEntity entity) {
player.openMenu(entity);
}
return InteractionResult.CONSUME;
@ -100,10 +104,10 @@ public class SuperbItemInterfaceBlock extends BaseEntityBlock {
public void onRemove(BlockState pState, @NotNull Level pLevel, @NotNull BlockPos pPos, BlockState pNewState, boolean pIsMoving) {
if (!pState.is(pNewState.getBlock())) {
BlockEntity blockentity = pLevel.getBlockEntity(pPos);
// if (blockentity instanceof HopperBlockEntity) {
// Containers.dropContents(pLevel, pPos, (HopperBlockEntity)blockentity);
// pLevel.updateNeighbourForOutputSignal(pPos, this);
// }
if (blockentity instanceof SuperbItemInterfaceBlockEntity entity) {
Containers.dropContents(pLevel, pPos, entity);
pLevel.updateNeighbourForOutputSignal(pPos, this);
}
super.onRemove(pState, pLevel, pPos, pNewState, pIsMoving);
}

View file

@ -2,35 +2,67 @@ package com.atsuishio.superbwarfare.block.entity;
import com.atsuishio.superbwarfare.init.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.NonNullList;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.world.Container;
import net.minecraft.world.ContainerHelper;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BaseContainerBlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.NotNull;
import javax.annotation.ParametersAreNonnullByDefault;
public class SuperbItemInterfaceBlockEntity extends BaseContainerBlockEntity {
public static final int MOVE_ITEM_SPEED = 64;
public static final int CONTAINER_SIZE = 5;
private NonNullList<ItemStack> items = NonNullList.withSize(CONTAINER_SIZE, ItemStack.EMPTY);
private int cooldownTime = -1;
private long tickedGameTime;
public SuperbItemInterfaceBlockEntity(BlockPos pPos, BlockState pBlockState) {
super(ModBlockEntities.SUPERB_ITEM_INTERFACE.get(), pPos, pBlockState);
}
public static void serverTick(Level level, BlockPos pos, BlockState state, SuperbItemInterfaceBlockEntity blockEntity) {
--blockEntity.cooldownTime;
blockEntity.tickedGameTime = level.getGameTime();
if (!blockEntity.isOnCooldown()) {
blockEntity.setCooldown(0);
// tryMoveItems(pLevel, pPos, pState, pBlockEntity, () -> suckInItems(pLevel, pBlockEntity));
}
}
@Override
@ParametersAreNonnullByDefault
protected void loadAdditional(CompoundTag tag, HolderLookup.Provider registries) {
super.loadAdditional(tag, registries);
this.items = NonNullList.withSize(this.getContainerSize(), ItemStack.EMPTY);
ContainerHelper.loadAllItems(tag, this.items, registries);
this.cooldownTime = tag.getInt("TransferCooldown");
}
@Override
@ParametersAreNonnullByDefault
protected void saveAdditional(CompoundTag tag, HolderLookup.Provider registries) {
super.saveAdditional(tag, registries);
ContainerHelper.saveAllItems(tag, this.items, registries);
tag.putInt("TransferCooldown", this.cooldownTime);
}
@Override
protected @NotNull Component getDefaultName() {
return Component.empty();
}
@Override
protected @NotNull NonNullList<ItemStack> getItems() {
return null;
}
@Override
protected void setItems(@NotNull NonNullList<ItemStack> items) {
return Component.translatable("container.superbwarfare.superb_item_interface");
}
@Override
@ -40,32 +72,72 @@ public class SuperbItemInterfaceBlockEntity extends BaseContainerBlockEntity {
@Override
public int getContainerSize() {
return 0;
return this.items.size();
}
@Override
public boolean isEmpty() {
return false;
for (ItemStack itemstack : this.items) {
if (!itemstack.isEmpty()) {
return false;
}
}
return true;
}
@Override
public @NotNull ItemStack getItem(int slot) {
return this.items.get(slot);
}
@Override
public @NotNull ItemStack removeItem(int pSlot, int pAmount) {
return null;
return ContainerHelper.removeItem(this.items, pSlot, pAmount);
}
@Override
public @NotNull ItemStack removeItemNoUpdate(int pSlot) {
return null;
return ContainerHelper.takeItem(this.items, pSlot);
}
@Override
public void setItem(int slot, @NotNull ItemStack stack) {
this.items.set(slot, stack);
if (stack.getCount() > this.getMaxStackSize()) {
stack.setCount(this.getMaxStackSize());
}
}
@Override
public boolean stillValid(@NotNull Player pPlayer) {
return false;
return Container.stillValidBlockEntity(this, pPlayer);
}
@Override
public void clearContent() {
this.items.clear();
}
public void setCooldown(int pCooldownTime) {
this.cooldownTime = pCooldownTime;
}
private boolean isOnCooldown() {
return this.cooldownTime > 0;
}
public long getLastUpdateTime() {
return this.tickedGameTime;
}
@Override
protected @NotNull NonNullList<ItemStack> getItems() {
return this.items;
}
@Override
protected void setItems(@NotNull NonNullList<ItemStack> items) {
this.items = items;
}
}

View file

@ -294,6 +294,7 @@
"des.superbwarfare.vehicle_deployer.success": "Vehicle info set successfully!",
"des.superbwarfare.vehicle_deployer.fail": "Please click with a container!",
"block.superbwarfare.aircraft_catapult": "Aircraft Catapult",
"block.superbwarfare.superb_item_interface": "Superb Item Interface",
"item.superbwarfare.high_energy_explosives": "High Energy Explosives",
"item.superbwarfare.grain": "Grain",
@ -568,6 +569,7 @@
"container.superbwarfare.charging_station": "Charging Station",
"container.superbwarfare.charging_station.show_range": "Show Range",
"container.superbwarfare.charging_station.hide_range": "Hide Range",
"container.superbwarfare.superb_item_interface": "Superb Item Interface",
"config.superbwarfare.title": "SuperbWarfare: Config",
"config.superbwarfare.client.reload": "Reload Config",

View file

@ -294,6 +294,7 @@
"des.superbwarfare.vehicle_deployer.success": "成功设置载具信息!",
"des.superbwarfare.vehicle_deployer.fail": "请使用集装箱点击!",
"block.superbwarfare.aircraft_catapult": "飞行器弹射装置",
"block.superbwarfare.superb_item_interface": "卓越物品接口",
"item.superbwarfare.high_energy_explosives": "高能炸药",
"item.superbwarfare.grain": "推进药柱",
@ -568,6 +569,7 @@
"container.superbwarfare.charging_station": "充电站",
"container.superbwarfare.charging_station.show_range": "显示范围",
"container.superbwarfare.charging_station.hide_range": "隐藏范围",
"container.superbwarfare.superb_item_interface": "卓越物品接口",
"config.superbwarfare.title": "卓越前线",
"config.superbwarfare.client.reload": "换弹配置",