2020-09-09 16:48:43 +02:00
|
|
|
package de.ellpeck.actuallyadditions.blocks.base;
|
2014-12-20 21:34:07 +01:00
|
|
|
|
2020-09-09 16:48:43 +02:00
|
|
|
import de.ellpeck.actuallyadditions.ActuallyAdditions;
|
|
|
|
import de.ellpeck.actuallyadditions.config.ConfigValues;
|
|
|
|
import de.ellpeck.actuallyadditions.tile.TileEntityBase;
|
|
|
|
import de.ellpeck.actuallyadditions.tile.TileEntityInventoryBase;
|
|
|
|
import de.ellpeck.actuallyadditions.util.StackUtil;
|
2020-09-07 19:43:02 +02:00
|
|
|
import net.minecraft.block.*;
|
|
|
|
import net.minecraft.entity.LivingEntity;
|
|
|
|
import net.minecraft.entity.MoverType;
|
|
|
|
import net.minecraft.entity.item.ItemEntity;
|
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
|
|
import net.minecraft.fluid.IFluidState;
|
2014-12-20 21:34:07 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
2020-09-07 19:43:02 +02:00
|
|
|
import net.minecraft.nbt.CompoundNBT;
|
2015-05-25 17:00:54 +02:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2020-09-07 19:43:02 +02:00
|
|
|
import net.minecraft.util.Hand;
|
2016-03-18 23:47:22 +01:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2020-09-07 19:43:02 +02:00
|
|
|
import net.minecraft.util.math.Vec3d;
|
2017-01-30 21:13:44 +01:00
|
|
|
import net.minecraft.util.text.Style;
|
|
|
|
import net.minecraft.util.text.TextFormatting;
|
2020-09-07 19:43:02 +02:00
|
|
|
import net.minecraft.util.text.TranslationTextComponent;
|
|
|
|
import net.minecraft.world.IWorldReader;
|
2014-12-20 21:34:07 +01:00
|
|
|
import net.minecraft.world.World;
|
2020-09-07 19:43:02 +02:00
|
|
|
import net.minecraft.world.server.ServerWorld;
|
2016-06-05 12:52:59 +02:00
|
|
|
import net.minecraftforge.fluids.FluidUtil;
|
2020-09-07 19:43:02 +02:00
|
|
|
import net.minecraftforge.fluids.capability.templates.FluidTank;
|
2014-12-20 21:34:07 +01:00
|
|
|
|
2020-09-07 19:43:02 +02:00
|
|
|
import javax.annotation.Nullable;
|
|
|
|
import java.util.Random;
|
2015-12-03 20:15:07 +01:00
|
|
|
|
2020-09-09 10:58:32 +02:00
|
|
|
public abstract class BlockContainerBase extends BlockBase {
|
2015-12-03 20:15:07 +01:00
|
|
|
|
2020-09-07 19:43:02 +02:00
|
|
|
public BlockContainerBase(Properties properties) {
|
|
|
|
super(properties);
|
2015-12-03 20:15:07 +01:00
|
|
|
|
2016-01-08 20:51:03 +01:00
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
private void dropInventory(World world, BlockPos position) {
|
|
|
|
if (!world.isRemote) {
|
2016-01-08 13:31:58 +01:00
|
|
|
TileEntity aTile = world.getTileEntity(position);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (aTile instanceof TileEntityInventoryBase) {
|
|
|
|
TileEntityInventoryBase tile = (TileEntityInventoryBase) aTile;
|
|
|
|
if (tile.inv.getSlots() > 0) {
|
|
|
|
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
|
|
|
|
2020-09-07 19:43:02 +02:00
|
|
|
// todo: replace with native implementation
|
2019-05-02 09:10:29 +02:00
|
|
|
private void dropSlotFromInventory(int i, TileEntityInventoryBase tile, World world, BlockPos pos) {
|
2018-06-23 01:39:30 +02:00
|
|
|
ItemStack stack = tile.inv.getStackInSlot(i);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (StackUtil.isValid(stack)) {
|
|
|
|
float dX = world.rand.nextFloat() * 0.8F + 0.1F;
|
|
|
|
float dY = world.rand.nextFloat() * 0.8F + 0.1F;
|
|
|
|
float dZ = world.rand.nextFloat() * 0.8F + 0.1F;
|
2020-09-07 19:43:02 +02:00
|
|
|
ItemEntity entityItem = new ItemEntity(world, pos.getX() + dX, pos.getY() + dY, pos.getZ() + dZ, stack.copy());
|
2015-09-13 19:39:02 +02:00
|
|
|
float factor = 0.05F;
|
2020-09-07 19:43:02 +02:00
|
|
|
|
|
|
|
entityItem.move(MoverType.SELF, new Vec3d(
|
|
|
|
world.rand.nextGaussian() * factor,
|
|
|
|
world.rand.nextGaussian() * factor + 0.2F,
|
|
|
|
world.rand.nextGaussian() * factor
|
|
|
|
));
|
|
|
|
|
|
|
|
world.addEntity(entityItem);
|
2015-09-13 19:39:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-07 19:43:02 +02:00
|
|
|
public boolean tryToggleRedstone(World world, BlockPos pos, PlayerEntity player) {
|
2016-05-02 07:50:13 +02:00
|
|
|
ItemStack stack = player.getHeldItemMainhand();
|
2019-05-02 09:10:29 +02:00
|
|
|
if (StackUtil.isValid(stack) && stack.getItem() == ConfigValues.itemRedstoneTorchConfigurator) {
|
2016-02-01 17:49:55 +01:00
|
|
|
TileEntity tile = world.getTileEntity(pos);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (tile instanceof TileEntityBase) {
|
|
|
|
TileEntityBase base = (TileEntityBase) tile;
|
|
|
|
if (!world.isRemote && base.isRedstoneToggle()) {
|
2016-07-02 15:01:46 +02:00
|
|
|
base.isPulseMode = !base.isPulseMode;
|
2017-02-06 14:55:54 +01:00
|
|
|
base.markDirty();
|
|
|
|
base.sendUpdate();
|
2016-02-01 17:49:55 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-09-07 19:43:02 +02:00
|
|
|
public void randomTick(BlockState state, ServerWorld world, BlockPos pos, Random random) {
|
2019-05-02 09:10:29 +02:00
|
|
|
if (!world.isRemote) {
|
2016-02-01 17:49:55 +01:00
|
|
|
TileEntity tile = world.getTileEntity(pos);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (tile instanceof TileEntityBase) {
|
|
|
|
TileEntityBase base = (TileEntityBase) tile;
|
|
|
|
if (base.respondsToPulses()) {
|
2016-07-02 15:01:46 +02:00
|
|
|
base.activateOnPulse();
|
|
|
|
}
|
2016-02-01 17:49:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public void neighborsChangedCustom(World world, BlockPos pos) {
|
2016-07-20 22:14:30 +02:00
|
|
|
this.updateRedstoneState(world, pos);
|
2016-09-09 18:40:09 +02:00
|
|
|
|
|
|
|
TileEntity tile = world.getTileEntity(pos);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (tile instanceof TileEntityBase) {
|
|
|
|
TileEntityBase base = (TileEntityBase) tile;
|
|
|
|
if (base.shouldSaveDataOnChangeOrWorldStart()) {
|
2016-10-29 12:00:00 +02:00
|
|
|
base.saveDataOnChangeOrWorldStart();
|
2016-09-09 18:40:09 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-20 22:14:30 +02:00
|
|
|
}
|
|
|
|
|
2015-12-03 18:40:43 +01:00
|
|
|
@Override
|
2020-09-07 19:43:02 +02:00
|
|
|
public void onNeighborChange(BlockState state, IWorldReader world, BlockPos pos, BlockPos neighbor) {
|
|
|
|
super.onNeighborChange(state, world, pos, neighbor);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (world instanceof World) {
|
|
|
|
this.neighborsChangedCustom((World) world, pos);
|
2016-07-20 22:14:30 +02:00
|
|
|
}
|
2015-12-03 18:40:43 +01:00
|
|
|
}
|
|
|
|
|
2020-09-07 19:43:02 +02:00
|
|
|
@Override
|
|
|
|
public void neighborChanged(BlockState state, World worldIn, BlockPos pos, Block blockIn, BlockPos fromPos, boolean isMoving) {
|
|
|
|
this.neighborsChangedCustom(worldIn, pos);
|
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public void updateRedstoneState(World world, BlockPos pos) {
|
|
|
|
if (!world.isRemote) {
|
2016-01-07 19:47:53 +01:00
|
|
|
TileEntity tile = world.getTileEntity(pos);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (tile instanceof TileEntityBase) {
|
|
|
|
TileEntityBase base = (TileEntityBase) tile;
|
2018-08-04 04:22:20 +02:00
|
|
|
boolean powered = world.getRedstonePowerFromNeighbors(pos) > 0;
|
2016-07-02 15:01:46 +02:00
|
|
|
boolean wasPowered = base.isRedstonePowered;
|
2019-05-02 09:10:29 +02:00
|
|
|
if (powered && !wasPowered) {
|
|
|
|
if (base.respondsToPulses()) {
|
2020-09-07 19:43:02 +02:00
|
|
|
world.getPendingBlockTicks().scheduleTick(pos, this, this.tickRate(world));
|
2016-01-02 21:36:42 +01:00
|
|
|
}
|
2016-07-02 15:01:46 +02:00
|
|
|
base.setRedstonePowered(true);
|
2019-05-02 09:10:29 +02:00
|
|
|
} else if (!powered && wasPowered) {
|
2016-07-02 15:01:46 +02:00
|
|
|
base.setRedstonePowered(false);
|
2015-12-24 00:34:47 +01:00
|
|
|
}
|
|
|
|
}
|
2015-12-03 18:40:43 +01:00
|
|
|
}
|
2015-12-24 00:34:47 +01:00
|
|
|
}
|
|
|
|
|
2020-09-07 19:43:02 +02:00
|
|
|
protected boolean tryUseItemOnTank(PlayerEntity player, Hand hand, FluidTank tank) {
|
2016-11-23 17:16:43 +01:00
|
|
|
ItemStack heldItem = player.getHeldItem(hand);
|
2017-06-29 13:00:11 +02:00
|
|
|
return StackUtil.isValid(heldItem) && FluidUtil.interactWithFluidHandler(player, hand, tank);
|
2016-11-23 17:16:43 +01:00
|
|
|
|
2016-05-06 23:23:29 +02:00
|
|
|
}
|
|
|
|
|
2015-12-24 00:34:47 +01:00
|
|
|
@Override
|
2020-09-07 19:43:02 +02:00
|
|
|
public void onBlockAdded(BlockState state, World world, BlockPos pos, BlockState oldState, boolean isMoving) {
|
2016-02-01 17:49:55 +01:00
|
|
|
this.updateRedstoneState(world, pos);
|
2015-12-03 18:40:43 +01:00
|
|
|
}
|
2015-12-13 00:54:25 +01:00
|
|
|
|
2015-12-19 10:30:39 +01:00
|
|
|
@Override
|
2020-09-07 19:43:02 +02:00
|
|
|
public void onBlockPlacedBy(World world, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack stack) {
|
|
|
|
if (stack.hasTag()) {
|
2016-01-07 19:47:53 +01:00
|
|
|
TileEntity tile = world.getTileEntity(pos);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (tile instanceof TileEntityBase) {
|
|
|
|
TileEntityBase base = (TileEntityBase) tile;
|
2020-09-07 19:43:02 +02:00
|
|
|
CompoundNBT compound = stack.getOrCreateTag().getCompound("Data");
|
|
|
|
base.readSyncableNBT(compound, TileEntityBase.NBTType.SAVE_BLOCK);
|
2015-12-19 10:30:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-13 00:54:25 +01:00
|
|
|
@Override
|
2020-09-07 19:43:02 +02:00
|
|
|
public void onBlockHarvested(World world, BlockPos pos, BlockState state, PlayerEntity player) {
|
|
|
|
if (!player.isCreative()) {
|
2017-01-30 21:13:44 +01:00
|
|
|
TileEntity tile = world.getTileEntity(pos);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (tile instanceof TileEntityBase && ((TileEntityBase) tile).stopFromDropping) {
|
2020-09-07 19:43:02 +02:00
|
|
|
player.sendMessage(new TranslationTextComponent("info." + ActuallyAdditions.MODID + ".machineBroke").setStyle(new Style().setColor(TextFormatting.RED)));
|
2017-01-30 21:13:44 +01:00
|
|
|
}
|
2015-12-13 00:54:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-19 10:30:39 +01:00
|
|
|
@Override
|
2020-09-07 19:43:02 +02:00
|
|
|
public boolean hasComparatorInputOverride(BlockState state) {
|
2015-12-19 10:30:39 +01:00
|
|
|
return true;
|
|
|
|
}
|
2015-12-17 18:47:46 +01:00
|
|
|
|
2015-12-19 10:30:39 +01:00
|
|
|
@Override
|
2020-09-07 19:43:02 +02:00
|
|
|
public int getComparatorInputOverride(BlockState state, World world, BlockPos pos) {
|
2016-01-07 19:47:53 +01:00
|
|
|
TileEntity tile = world.getTileEntity(pos);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (tile instanceof TileEntityBase) { return ((TileEntityBase) tile).getComparatorStrength(); }
|
2015-12-19 10:30:39 +01:00
|
|
|
return 0;
|
2015-12-17 18:47:46 +01:00
|
|
|
}
|
|
|
|
|
2020-09-07 19:43:02 +02:00
|
|
|
// todo: come back to this. It think it's goal is to keep data on the dropped item... I know
|
|
|
|
// how to do this but I need to make sure I do it right.
|
|
|
|
|
|
|
|
// @Override
|
|
|
|
// public void spawnAdditionalDrops(BlockState state, World world, BlockPos pos, ItemStack stack) {
|
|
|
|
// 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.remove(key);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// ItemStack stack = new ItemStack(this.getItemDropped(state, tile.getWorld().rand, fortune), 1, this.damageDropped(state));
|
|
|
|
// if (!data.isEmpty()) {
|
|
|
|
// stack.setTagCompound(new NBTTagCompound());
|
|
|
|
// stack.getTagCompound().setTag("Data", data);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// drops.add(stack);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
2015-12-13 00:54:25 +01:00
|
|
|
|
2015-12-19 10:30:39 +01:00
|
|
|
|
2018-01-05 14:38:06 +01:00
|
|
|
@Override
|
2020-09-07 19:43:02 +02:00
|
|
|
public boolean removedByPlayer(BlockState state, World world, BlockPos pos, PlayerEntity player, boolean willHarvest, IFluidState fluid) {
|
|
|
|
return willHarvest || super.removedByPlayer(state, world, pos, player, false, fluid);
|
2018-01-05 14:38:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-09-07 19:43:02 +02:00
|
|
|
public void harvestBlock(World worldIn, PlayerEntity player, BlockPos pos, BlockState state, @Nullable TileEntity te, ItemStack stack) {
|
2018-01-05 14:38:06 +01:00
|
|
|
super.harvestBlock(worldIn, player, pos, state, te, stack);
|
2020-09-07 19:43:02 +02:00
|
|
|
worldIn.setBlockState(pos, Blocks.AIR.getDefaultState());
|
2015-12-13 00:54:25 +01:00
|
|
|
}
|
|
|
|
|
2016-01-08 17:25:31 +01:00
|
|
|
@Override
|
2020-09-07 19:43:02 +02:00
|
|
|
public BlockRenderType getRenderType(BlockState state) {
|
|
|
|
return BlockRenderType.MODEL;
|
2016-01-08 17:25:31 +01:00
|
|
|
}
|
2016-11-27 11:37:55 +01:00
|
|
|
|
|
|
|
@Override
|
2020-09-07 19:43:02 +02:00
|
|
|
public void onReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean isMoving) {
|
|
|
|
// todo: this might not work with this sepecific state check
|
|
|
|
if (state != newState && this.shouldDropInventory(world, pos)) {
|
2016-11-27 11:37:55 +01:00
|
|
|
this.dropInventory(world, pos);
|
|
|
|
}
|
|
|
|
|
2020-09-07 19:43:02 +02:00
|
|
|
super.onReplaced(state, world, pos, newState, isMoving);
|
2016-11-27 11:37:55 +01:00
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public boolean shouldDropInventory(World world, BlockPos pos) {
|
2016-11-27 11:37:55 +01:00
|
|
|
return true;
|
|
|
|
}
|
2020-09-07 19:43:02 +02:00
|
|
|
}
|