添加卓越物品接口的基础方法
This commit is contained in:
parent
6c80f5af7f
commit
f8d0c2e886
4 changed files with 119 additions and 39 deletions
|
@ -1,21 +1,21 @@
|
||||||
package com.atsuishio.superbwarfare.block;
|
package com.atsuishio.superbwarfare.block;
|
||||||
|
|
||||||
|
import com.atsuishio.superbwarfare.block.entity.SuperbItemInterfaceBlockEntity;
|
||||||
|
import com.atsuishio.superbwarfare.init.ModBlockEntities;
|
||||||
import com.mojang.serialization.MapCodec;
|
import com.mojang.serialization.MapCodec;
|
||||||
import net.minecraft.core.BlockPos;
|
import net.minecraft.core.BlockPos;
|
||||||
import net.minecraft.core.component.DataComponents;
|
import net.minecraft.world.Containers;
|
||||||
import net.minecraft.stats.Stats;
|
|
||||||
import net.minecraft.world.InteractionResult;
|
import net.minecraft.world.InteractionResult;
|
||||||
import net.minecraft.world.entity.LivingEntity;
|
|
||||||
import net.minecraft.world.entity.player.Player;
|
import net.minecraft.world.entity.player.Player;
|
||||||
import net.minecraft.world.inventory.AbstractContainerMenu;
|
import net.minecraft.world.inventory.AbstractContainerMenu;
|
||||||
import net.minecraft.world.item.ItemStack;
|
|
||||||
import net.minecraft.world.level.Level;
|
import net.minecraft.world.level.Level;
|
||||||
import net.minecraft.world.level.block.BaseEntityBlock;
|
import net.minecraft.world.level.block.BaseEntityBlock;
|
||||||
import net.minecraft.world.level.block.Block;
|
import net.minecraft.world.level.block.Block;
|
||||||
import net.minecraft.world.level.block.RenderShape;
|
import net.minecraft.world.level.block.RenderShape;
|
||||||
import net.minecraft.world.level.block.SoundType;
|
import net.minecraft.world.level.block.SoundType;
|
||||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
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.BlockBehaviour;
|
||||||
import net.minecraft.world.level.block.state.BlockState;
|
import net.minecraft.world.level.block.state.BlockState;
|
||||||
import net.minecraft.world.level.block.state.StateDefinition;
|
import net.minecraft.world.level.block.state.StateDefinition;
|
||||||
|
@ -29,7 +29,6 @@ import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import javax.annotation.ParametersAreNonnullByDefault;
|
import javax.annotation.ParametersAreNonnullByDefault;
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
public class SuperbItemInterfaceBlock extends BaseEntityBlock {
|
public class SuperbItemInterfaceBlock extends BaseEntityBlock {
|
||||||
|
|
||||||
public static final BooleanProperty ENABLED = BlockStateProperties.ENABLED;
|
public static final BooleanProperty ENABLED = BlockStateProperties.ENABLED;
|
||||||
|
@ -47,19 +46,25 @@ public class SuperbItemInterfaceBlock extends BaseEntityBlock {
|
||||||
@Override
|
@Override
|
||||||
@ParametersAreNonnullByDefault
|
@ParametersAreNonnullByDefault
|
||||||
public BlockEntity newBlockEntity(BlockPos pPos, BlockState pState) {
|
public BlockEntity newBlockEntity(BlockPos pPos, BlockState pState) {
|
||||||
return null;
|
return new SuperbItemInterfaceBlockEntity(pPos, pState);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Nullable
|
||||||
public void setPlacedBy(@NotNull Level pLevel, @NotNull BlockPos pPos, @NotNull BlockState pState, LivingEntity pPlacer, ItemStack pStack) {
|
@ParametersAreNonnullByDefault
|
||||||
if (pStack.get(DataComponents.CUSTOM_NAME) != null) {
|
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level pLevel, BlockState pState, BlockEntityType<T> pBlockEntityType) {
|
||||||
BlockEntity blockentity = pLevel.getBlockEntity(pPos);
|
return pLevel.isClientSide ? null : createTickerHelper(pBlockEntityType, ModBlockEntities.SUPERB_ITEM_INTERFACE.get(), SuperbItemInterfaceBlockEntity::serverTick);
|
||||||
// if (blockentity instanceof HopperBlockEntity) {
|
|
||||||
// ((HopperBlockEntity) blockentity).setCustomName(pStack.getHoverName());
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// @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
|
@Override
|
||||||
public void onPlace(BlockState pState, @NotNull Level pLevel, @NotNull BlockPos pPos, BlockState pOldState, boolean pIsMoving) {
|
public void onPlace(BlockState pState, @NotNull Level pLevel, @NotNull BlockPos pPos, BlockState pOldState, boolean pIsMoving) {
|
||||||
if (!pOldState.is(pState.getBlock())) {
|
if (!pOldState.is(pState.getBlock())) {
|
||||||
|
@ -74,9 +79,8 @@ public class SuperbItemInterfaceBlock extends BaseEntityBlock {
|
||||||
return InteractionResult.SUCCESS;
|
return InteractionResult.SUCCESS;
|
||||||
} else {
|
} else {
|
||||||
BlockEntity blockentity = level.getBlockEntity(pos);
|
BlockEntity blockentity = level.getBlockEntity(pos);
|
||||||
if (blockentity instanceof HopperBlockEntity) {
|
if (blockentity instanceof SuperbItemInterfaceBlockEntity entity) {
|
||||||
player.openMenu((HopperBlockEntity) blockentity);
|
player.openMenu(entity);
|
||||||
player.awardStat(Stats.INSPECT_HOPPER);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return InteractionResult.CONSUME;
|
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) {
|
public void onRemove(BlockState pState, @NotNull Level pLevel, @NotNull BlockPos pPos, BlockState pNewState, boolean pIsMoving) {
|
||||||
if (!pState.is(pNewState.getBlock())) {
|
if (!pState.is(pNewState.getBlock())) {
|
||||||
BlockEntity blockentity = pLevel.getBlockEntity(pPos);
|
BlockEntity blockentity = pLevel.getBlockEntity(pPos);
|
||||||
// if (blockentity instanceof HopperBlockEntity) {
|
if (blockentity instanceof SuperbItemInterfaceBlockEntity entity) {
|
||||||
// Containers.dropContents(pLevel, pPos, (HopperBlockEntity)blockentity);
|
Containers.dropContents(pLevel, pPos, entity);
|
||||||
// pLevel.updateNeighbourForOutputSignal(pPos, this);
|
pLevel.updateNeighbourForOutputSignal(pPos, this);
|
||||||
// }
|
}
|
||||||
|
|
||||||
super.onRemove(pState, pLevel, pPos, pNewState, pIsMoving);
|
super.onRemove(pState, pLevel, pPos, pNewState, pIsMoving);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,35 +2,67 @@ package com.atsuishio.superbwarfare.block.entity;
|
||||||
|
|
||||||
import com.atsuishio.superbwarfare.init.ModBlockEntities;
|
import com.atsuishio.superbwarfare.init.ModBlockEntities;
|
||||||
import net.minecraft.core.BlockPos;
|
import net.minecraft.core.BlockPos;
|
||||||
|
import net.minecraft.core.HolderLookup;
|
||||||
import net.minecraft.core.NonNullList;
|
import net.minecraft.core.NonNullList;
|
||||||
|
import net.minecraft.nbt.CompoundTag;
|
||||||
import net.minecraft.network.chat.Component;
|
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.Inventory;
|
||||||
import net.minecraft.world.entity.player.Player;
|
import net.minecraft.world.entity.player.Player;
|
||||||
import net.minecraft.world.inventory.AbstractContainerMenu;
|
import net.minecraft.world.inventory.AbstractContainerMenu;
|
||||||
import net.minecraft.world.item.ItemStack;
|
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.entity.BaseContainerBlockEntity;
|
||||||
import net.minecraft.world.level.block.state.BlockState;
|
import net.minecraft.world.level.block.state.BlockState;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import javax.annotation.ParametersAreNonnullByDefault;
|
||||||
|
|
||||||
public class SuperbItemInterfaceBlockEntity extends BaseContainerBlockEntity {
|
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) {
|
public SuperbItemInterfaceBlockEntity(BlockPos pPos, BlockState pBlockState) {
|
||||||
super(ModBlockEntities.SUPERB_ITEM_INTERFACE.get(), pPos, 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
|
@Override
|
||||||
protected @NotNull Component getDefaultName() {
|
protected @NotNull Component getDefaultName() {
|
||||||
return Component.empty();
|
return Component.translatable("container.superbwarfare.superb_item_interface");
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected @NotNull NonNullList<ItemStack> getItems() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void setItems(@NotNull NonNullList<ItemStack> items) {
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -40,32 +72,72 @@ public class SuperbItemInterfaceBlockEntity extends BaseContainerBlockEntity {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getContainerSize() {
|
public int getContainerSize() {
|
||||||
return 0;
|
return this.items.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isEmpty() {
|
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
|
@Override
|
||||||
public @NotNull ItemStack removeItem(int pSlot, int pAmount) {
|
public @NotNull ItemStack removeItem(int pSlot, int pAmount) {
|
||||||
return null;
|
return ContainerHelper.removeItem(this.items, pSlot, pAmount);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull ItemStack removeItemNoUpdate(int pSlot) {
|
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
|
@Override
|
||||||
public boolean stillValid(@NotNull Player pPlayer) {
|
public boolean stillValid(@NotNull Player pPlayer) {
|
||||||
return false;
|
return Container.stillValidBlockEntity(this, pPlayer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void clearContent() {
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -294,6 +294,7 @@
|
||||||
"des.superbwarfare.vehicle_deployer.success": "Vehicle info set successfully!",
|
"des.superbwarfare.vehicle_deployer.success": "Vehicle info set successfully!",
|
||||||
"des.superbwarfare.vehicle_deployer.fail": "Please click with a container!",
|
"des.superbwarfare.vehicle_deployer.fail": "Please click with a container!",
|
||||||
"block.superbwarfare.aircraft_catapult": "Aircraft Catapult",
|
"block.superbwarfare.aircraft_catapult": "Aircraft Catapult",
|
||||||
|
"block.superbwarfare.superb_item_interface": "Superb Item Interface",
|
||||||
|
|
||||||
"item.superbwarfare.high_energy_explosives": "High Energy Explosives",
|
"item.superbwarfare.high_energy_explosives": "High Energy Explosives",
|
||||||
"item.superbwarfare.grain": "Grain",
|
"item.superbwarfare.grain": "Grain",
|
||||||
|
@ -568,6 +569,7 @@
|
||||||
"container.superbwarfare.charging_station": "Charging Station",
|
"container.superbwarfare.charging_station": "Charging Station",
|
||||||
"container.superbwarfare.charging_station.show_range": "Show Range",
|
"container.superbwarfare.charging_station.show_range": "Show Range",
|
||||||
"container.superbwarfare.charging_station.hide_range": "Hide Range",
|
"container.superbwarfare.charging_station.hide_range": "Hide Range",
|
||||||
|
"container.superbwarfare.superb_item_interface": "Superb Item Interface",
|
||||||
|
|
||||||
"config.superbwarfare.title": "SuperbWarfare: Config",
|
"config.superbwarfare.title": "SuperbWarfare: Config",
|
||||||
"config.superbwarfare.client.reload": "Reload Config",
|
"config.superbwarfare.client.reload": "Reload Config",
|
||||||
|
|
|
@ -294,6 +294,7 @@
|
||||||
"des.superbwarfare.vehicle_deployer.success": "成功设置载具信息!",
|
"des.superbwarfare.vehicle_deployer.success": "成功设置载具信息!",
|
||||||
"des.superbwarfare.vehicle_deployer.fail": "请使用集装箱点击!",
|
"des.superbwarfare.vehicle_deployer.fail": "请使用集装箱点击!",
|
||||||
"block.superbwarfare.aircraft_catapult": "飞行器弹射装置",
|
"block.superbwarfare.aircraft_catapult": "飞行器弹射装置",
|
||||||
|
"block.superbwarfare.superb_item_interface": "卓越物品接口",
|
||||||
|
|
||||||
"item.superbwarfare.high_energy_explosives": "高能炸药",
|
"item.superbwarfare.high_energy_explosives": "高能炸药",
|
||||||
"item.superbwarfare.grain": "推进药柱",
|
"item.superbwarfare.grain": "推进药柱",
|
||||||
|
@ -568,6 +569,7 @@
|
||||||
"container.superbwarfare.charging_station": "充电站",
|
"container.superbwarfare.charging_station": "充电站",
|
||||||
"container.superbwarfare.charging_station.show_range": "显示范围",
|
"container.superbwarfare.charging_station.show_range": "显示范围",
|
||||||
"container.superbwarfare.charging_station.hide_range": "隐藏范围",
|
"container.superbwarfare.charging_station.hide_range": "隐藏范围",
|
||||||
|
"container.superbwarfare.superb_item_interface": "卓越物品接口",
|
||||||
|
|
||||||
"config.superbwarfare.title": "卓越前线",
|
"config.superbwarfare.title": "卓越前线",
|
||||||
"config.superbwarfare.client.reload": "换弹配置",
|
"config.superbwarfare.client.reload": "换弹配置",
|
||||||
|
|
Loading…
Add table
Reference in a new issue