ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/base/BlockContainerBase.java

278 lines
11 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
2016-05-16 22:52:27 +02:00
* This file ("BlockContainerBase.java") is part of the Actually Additions mod for Minecraft.
2015-08-29 14:33:25 +02:00
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
2016-05-16 22:52:27 +02:00
* http://ellpeck.de/actaddlicense
2015-08-29 14:33:25 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
2015-08-29 14:33:25 +02:00
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.blocks.base;
2014-12-20 21:34:07 +01:00
2016-01-08 20:51:03 +01:00
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
import de.ellpeck.actuallyadditions.mod.config.CommonConfig;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityBase;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityInventoryBase;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
2024-03-02 21:23:08 +01:00
import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
2024-03-03 01:20:53 +01:00
import net.minecraft.network.chat.Component;
2024-03-02 21:23:08 +01:00
import net.minecraft.server.level.ServerLevel;
2024-03-03 01:20:53 +01:00
import net.minecraft.util.RandomSource;
2024-03-02 21:23:08 +01:00
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.MenuProvider;
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;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
2024-03-04 20:21:48 +01:00
import net.neoforged.neoforge.fluids.FluidUtil;
import net.neoforged.neoforge.fluids.capability.templates.FluidTank;
2021-02-26 22:15:48 +01:00
2022-04-20 19:06:54 +02:00
import javax.annotation.Nonnull;
2021-02-26 22:15:48 +01:00
import javax.annotation.Nullable;
2014-12-20 21:34:07 +01:00
2024-03-02 21:23:08 +01:00
public abstract class BlockContainerBase extends Block implements EntityBlock {
2021-02-27 17:22:03 +01:00
public BlockContainerBase(Properties properties) {
2021-02-26 22:15:48 +01:00
super(properties);
2015-12-19 10:30:39 +01:00
}
2024-03-02 21:23:08 +01:00
public InteractionResult openGui(Level world, Player player, BlockPos pos, Class<? extends MenuProvider> expectedInstance) {
if (!world.isClientSide) {
2024-03-02 21:23:08 +01:00
BlockEntity tile = world.getBlockEntity(pos);
if (expectedInstance.isInstance(tile)) {
2024-03-09 00:23:20 +01:00
player.openMenu((MenuProvider) tile, pos);
2021-02-28 15:47:54 +01:00
}
2024-03-02 21:23:08 +01:00
return InteractionResult.SUCCESS;
2021-02-28 15:47:54 +01:00
}
2024-03-02 21:23:08 +01:00
return InteractionResult.SUCCESS;
2021-02-28 15:47:54 +01:00
}
2024-03-02 21:23:08 +01:00
private void dropInventory(Level world, BlockPos position) {
if (!world.isClientSide) {
2024-03-02 21:23:08 +01:00
BlockEntity aTile = world.getBlockEntity(position);
2024-03-03 01:20:53 +01:00
if (aTile instanceof TileEntityInventoryBase tile) {
if (tile.inv.getSlots() > 0) {
2019-05-02 09:10:29 +02:00
for (int i = 0; i < tile.inv.getSlots(); i++) {
2016-01-07 18:20:59 +01:00
this.dropSlotFromInventory(i, tile, world, position);
2015-06-18 13:14:57 +02:00
}
}
2014-12-20 21:34:07 +01:00
}
}
}
2015-05-25 17:00:54 +02:00
2024-03-02 21:23:08 +01:00
private void dropSlotFromInventory(int i, TileEntityInventoryBase tile, Level world, BlockPos pos) {
ItemStack stack = tile.inv.getStackInSlot(i);
2021-02-26 22:15:48 +01:00
if (stack.isEmpty()) {
return;
}
2021-02-26 22:15:48 +01:00
float dX = world.random.nextFloat() * 0.8F + 0.1F;
float dY = world.random.nextFloat() * 0.8F + 0.1F;
float dZ = world.random.nextFloat() * 0.8F + 0.1F;
2021-02-26 22:15:48 +01:00
ItemEntity entityItem = new ItemEntity(world, pos.getX() + dX, pos.getY() + dY, pos.getZ() + dZ, stack.copy());
float factor = 0.05F;
entityItem.push(world.random.nextGaussian() * factor, world.random.nextGaussian() * factor + 0.2F, world.random.nextGaussian() * factor);
world.addFreshEntity(entityItem);
}
2024-03-02 21:23:08 +01:00
public boolean tryToggleRedstone(Level world, BlockPos pos, Player player) {
ItemStack stack = player.getMainHandItem();
if (stack.getItem() == CommonConfig.Other.redstoneConfigureItem) {
2024-03-02 21:23:08 +01:00
BlockEntity tile = world.getBlockEntity(pos);
2024-03-03 01:20:53 +01:00
if (tile instanceof TileEntityBase base) {
if (!world.isClientSide && base.isRedstoneToggle()) {
base.isPulseMode = !base.isPulseMode;
base.setChanged();
base.sendUpdate();
2016-02-01 17:49:55 +01:00
}
return true;
}
}
return false;
}
@Override
2024-03-03 01:20:53 +01:00
public void tick(@Nonnull BlockState state, ServerLevel world, @Nonnull BlockPos pos, @Nonnull RandomSource rand) {
if (!world.isClientSide) {
2024-03-02 21:23:08 +01:00
BlockEntity tile = world.getBlockEntity(pos);
2024-03-03 01:20:53 +01:00
if (tile instanceof TileEntityBase base) {
if (base.respondsToPulses()) {
base.activateOnPulse();
}
2016-02-01 17:49:55 +01:00
}
}
}
2024-03-02 21:23:08 +01:00
public void neighborsChangedCustom(Level world, BlockPos pos) {
this.updateRedstoneState(world, pos);
2024-03-02 21:23:08 +01:00
BlockEntity tile = world.getBlockEntity(pos);
2024-03-03 01:20:53 +01:00
if (tile instanceof TileEntityBase base) {
if (base.shouldSaveDataOnChangeOrWorldStart()) {
base.saveDataOnChangeOrWorldStart();
}
}
}
2022-04-03 22:18:46 +02:00
@Override //TODO do we need this?
2024-03-02 21:23:08 +01:00
public void neighborChanged(@Nonnull BlockState state, @Nonnull Level worldIn, @Nonnull BlockPos pos, @Nonnull Block blockIn, @Nonnull BlockPos fromPos, boolean isMoving) {
this.neighborsChangedCustom(worldIn, pos);
}
@Override
2024-03-02 21:23:08 +01:00
public void onNeighborChange(BlockState state, LevelReader world, BlockPos pos, BlockPos neighbor) {
2021-02-26 22:15:48 +01:00
super.onNeighborChange(state, world, pos, neighbor);
2024-03-02 21:23:08 +01:00
if (world instanceof Level) { //TODO what?
this.neighborsChangedCustom((Level) world, pos);
}
}
2024-03-02 21:23:08 +01:00
public void updateRedstoneState(Level world, BlockPos pos) {
if (!world.isClientSide) {
2024-03-02 21:23:08 +01:00
BlockEntity tile = world.getBlockEntity(pos);
2024-03-03 01:20:53 +01:00
if (tile instanceof TileEntityBase base) {
boolean powered = world.getBestNeighborSignal(pos) > 0;
boolean wasPowered = base.isRedstonePowered;
2019-05-02 09:10:29 +02:00
if (powered && !wasPowered) {
if (base.respondsToPulses()) {
2021-02-26 22:15:48 +01:00
// TODO: [port] eval what this does? :D
// world.scheduleUpdate(pos, this, this.tickRate(world));
// Who knows -Flanks
base.activateOnPulse();
}
base.setRedstonePowered(true);
2019-05-02 09:10:29 +02:00
} else if (!powered && wasPowered) {
base.setRedstonePowered(false);
}
}
}
}
2024-03-02 21:23:08 +01:00
protected boolean tryUseItemOnTank(Player player, InteractionHand hand, FluidTank tank) {
ItemStack heldItem = player.getItemInHand(hand);
return StackUtil.isValid(heldItem) && FluidUtil.interactWithFluidHandler(player, hand, tank);
}
@Override
2024-03-02 21:23:08 +01:00
public void onPlace(@Nonnull BlockState state, @Nonnull Level worldIn, @Nonnull BlockPos pos, @Nonnull BlockState oldState, boolean isMoving) {
2021-02-26 22:15:48 +01:00
this.updateRedstoneState(worldIn, pos);
}
2015-12-19 10:30:39 +01:00
@Override
2024-03-02 21:23:08 +01:00
public void setPlacedBy(Level world, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack stack) {
2021-02-26 22:15:48 +01:00
if (stack.hasTag()) {
2024-03-02 21:23:08 +01:00
BlockEntity tile = world.getBlockEntity(pos);
2024-03-03 01:20:53 +01:00
if (tile instanceof TileEntityBase base) {
CompoundTag compound = stack.getOrCreateTag().getCompound("Data");
2021-02-26 22:15:48 +01:00
base.readSyncableNBT(compound, TileEntityBase.NBTType.SAVE_BLOCK);
2015-12-19 10:30:39 +01:00
}
}
}
@Override
2024-03-04 20:21:48 +01:00
public BlockState playerWillDestroy(@Nonnull Level world, @Nonnull BlockPos pos, @Nonnull BlockState state, Player player) {
BlockState theState = super.playerWillDestroy(world, pos, state, player);
2021-02-26 22:15:48 +01:00
if (!player.isCreative()) {
2024-03-02 21:23:08 +01:00
BlockEntity tile = world.getBlockEntity(pos);
2019-05-02 09:10:29 +02:00
if (tile instanceof TileEntityBase && ((TileEntityBase) tile).stopFromDropping) {
2024-03-03 01:20:53 +01:00
player.displayClientMessage(Component.translatable("info." + ActuallyAdditions.MODID + ".machineBroke").withStyle(ChatFormatting.RED), false);
}
}
2024-03-04 20:21:48 +01:00
return theState;
}
2015-12-19 10:30:39 +01:00
@Override
public boolean hasAnalogOutputSignal(BlockState state) {
2015-12-19 10:30:39 +01:00
return true;
}
2015-12-19 10:30:39 +01:00
@Override
2024-03-02 21:23:08 +01:00
public int getAnalogOutputSignal(BlockState state, Level world, BlockPos pos) {
BlockEntity tile = world.getBlockEntity(pos);
2021-02-26 22:15:48 +01:00
if (tile instanceof TileEntityBase) {
return ((TileEntityBase) tile).getComparatorStrength();
}
2015-12-19 10:30:39 +01:00
return 0;
}
2021-02-26 22:15:48 +01:00
// TODO: [port]: come back and fix this
// @Override
// public void getDrops(NonNullList<ItemStack> drops, IBlockAccess world, BlockPos pos, BlockState state, int fortune) {
// TileEntity tile = world.getTileEntity(pos);
// if (tile instanceof TileEntityBase) {
// TileEntityBase base = (TileEntityBase) tile;
// if (!base.stopFromDropping) {
// CompoundNBT data = new CompoundNBT();
// base.writeSyncableNBT(data, TileEntityBase.NBTType.SAVE_BLOCK);
//
// //Remove unnecessarily saved default values to avoid unstackability
// List<String> keysToRemove = new ArrayList<>();
// for (String key : data.getKeySet()) {
// NBTBase tag = data.getTag(key);
// //Remove only ints because they are the most common ones
// //Add else if below here to remove more types
// if (tag instanceof NBTTagInt) {
// if (((NBTTagInt) tag).getInt() == 0) {
// keysToRemove.add(key);
// }
// }
// }
// for (String key : keysToRemove) {
// data.removeTag(key);
// }
//
// ItemStack stack = new ItemStack(this.getItemDropped(state, tile.getWorld().rand, fortune), 1, this.damageDropped(state));
// if (!data.isEmpty()) {
// stack.setTagCompound(new CompoundNBT());
// stack.getTagCompound().setTag("Data", data);
// }
//
// drops.add(stack);
// }
// } else {
// super.getDrops(drops, world, pos, state, fortune);
// }
// }
2015-12-19 10:30:39 +01:00
2021-02-26 22:15:48 +01:00
// TODO: [port]: eval
// @Override
// public EnumBlockRenderType getRenderType(BlockState state) {
// return EnumBlockRenderType.MODEL;
// }
2021-11-24 19:59:05 +01:00
@Override
2024-03-02 21:23:08 +01:00
public RenderShape getRenderShape(BlockState pState) {
return RenderShape.MODEL;
2021-11-24 19:59:05 +01:00
}
@Override
2024-03-02 21:23:08 +01:00
public void onRemove(BlockState state, Level world, BlockPos pos, BlockState newState, boolean isMoving) {
2022-01-01 20:28:15 +01:00
if (state.getBlock() != newState.getBlock()) {
2021-02-26 22:15:48 +01:00
if (this.shouldDropInventory(world, pos)) {
this.dropInventory(world, pos);
}
}
super.onRemove(state, world, pos, newState, isMoving);
}
2024-03-02 21:23:08 +01:00
public boolean shouldDropInventory(Level world, BlockPos pos) {
return true;
}
2014-12-20 21:34:07 +01:00
}