ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLongRangeBreaker.java

157 lines
6 KiB
Java
Raw Normal View History

2015-10-05 16:53:28 +02:00
/*
2016-05-16 22:52:27 +02:00
* This file ("TileEntityDirectionalBreaker.java") is part of the Actually Additions mod for Minecraft.
2015-10-05 16:53:28 +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-10-05 16:53:28 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
2015-10-05 16:53:28 +02:00
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.tile;
2015-10-05 16:53:28 +02:00
2021-08-30 22:51:41 +02:00
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
2021-02-28 15:47:54 +01:00
import de.ellpeck.actuallyadditions.mod.inventory.ContainerDirectionalBreaker;
2018-08-10 05:04:07 +02:00
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
2024-03-02 21:23:08 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.MenuProvider;
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.Block;
import net.minecraft.world.level.block.Blocks;
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.energy.IEnergyStorage;
2015-10-05 16:53:28 +02:00
2021-02-28 15:47:54 +01:00
import javax.annotation.Nullable;
2021-02-27 16:33:00 +01:00
import java.util.List;
2024-03-02 21:23:08 +01:00
public class TileEntityLongRangeBreaker extends TileEntityInventoryBase implements MenuProvider {
2015-10-05 16:53:28 +02:00
2015-12-01 19:48:09 +01:00
public static final int RANGE = 8;
public static final int ENERGY_USE = 5;
2016-11-26 20:43:50 +01:00
public final CustomEnergyStorage storage = new CustomEnergyStorage(10000, 20, 0);
2015-10-05 16:53:28 +02:00
private int lastEnergy;
private int currentTime;
2024-03-02 21:23:08 +01:00
public TileEntityLongRangeBreaker(BlockPos pos, BlockState state) {
super(ActuallyBlocks.LONG_RANGE_BREAKER.getTileEntityType(), pos, state, 9);
2015-10-05 16:53:28 +02:00
}
2016-02-01 17:49:55 +01:00
@Override
2024-03-02 21:23:08 +01:00
public void writeSyncableNBT(CompoundTag compound, NBTType type) {
super.writeSyncableNBT(compound, type);
2016-02-01 17:49:55 +01:00
this.storage.writeToNBT(compound);
2018-08-10 05:04:07 +02:00
if (type != NBTType.SAVE_BLOCK) {
2021-02-27 16:33:00 +01:00
compound.putInt("CurrentTime", this.currentTime);
}
2016-02-01 17:49:55 +01:00
}
@Override
2024-03-02 21:23:08 +01:00
public void readSyncableNBT(CompoundTag compound, NBTType type) {
super.readSyncableNBT(compound, type);
2016-02-01 17:49:55 +01:00
this.storage.readFromNBT(compound);
2018-08-10 05:04:07 +02:00
if (type != NBTType.SAVE_BLOCK) {
2021-02-27 16:33:00 +01:00
this.currentTime = compound.getInt("CurrentTime");
}
2016-02-01 17:49:55 +01:00
}
2024-03-02 21:23:08 +01:00
public static <T extends BlockEntity> void clientTick(Level level, BlockPos pos, BlockState state, T t) {
if (t instanceof TileEntityLongRangeBreaker tile) {
tile.clientTick();
}
}
public static <T extends BlockEntity> void serverTick(Level level, BlockPos pos, BlockState state, T t) {
if (t instanceof TileEntityLongRangeBreaker tile) {
tile.serverTick();
if (!tile.isRedstonePowered && !tile.isPulseMode) {
if (tile.currentTime > 0) {
tile.currentTime--;
if (tile.currentTime <= 0) {
tile.doWork();
2015-10-05 16:53:28 +02:00
}
2018-08-10 05:04:07 +02:00
} else {
2024-03-02 21:23:08 +01:00
tile.currentTime = 15;
}
2015-10-05 16:53:28 +02:00
}
2024-03-02 21:23:08 +01:00
if (tile.storage.getEnergyStored() != tile.lastEnergy && tile.sendUpdateWithInterval()) {
tile.lastEnergy = tile.storage.getEnergyStored();
2015-10-05 16:53:28 +02:00
}
}
}
2018-08-10 05:04:07 +02:00
private void doWork() {
if (this.storage.getEnergyStored() >= ENERGY_USE * RANGE) {
2021-08-30 22:51:41 +02:00
BlockState state = this.level.getBlockState(this.worldPosition);
Direction sideToManipulate = WorldUtil.getDirectionByPistonRotation(state);
2018-08-10 05:04:07 +02:00
for (int i = 0; i < RANGE; i++) {
2021-08-30 22:51:41 +02:00
BlockPos coordsBlock = this.worldPosition.relative(sideToManipulate, i + 1);
BlockState breakState = this.level.getBlockState(coordsBlock);
2017-09-25 21:54:53 +02:00
Block blockToBreak = breakState.getBlock();
2021-08-30 22:51:41 +02:00
if (blockToBreak != null && !this.level.isEmptyBlock(coordsBlock) && this.level.getBlockState(coordsBlock).getDestroySpeed(this.level, coordsBlock) > -1.0F) {
2024-03-02 21:23:08 +01:00
List<ItemStack> drops = Block.getDrops(breakState, (ServerLevel) this.level, coordsBlock, this.level.getBlockEntity(coordsBlock));
2021-08-30 22:51:41 +02:00
float chance = WorldUtil.fireFakeHarvestEventsForDropChance(this, drops, this.level, coordsBlock);
2016-05-16 22:52:27 +02:00
2021-08-30 22:51:41 +02:00
if (chance > 0 && this.level.random.nextFloat() <= chance) {
2018-08-10 05:04:07 +02:00
if (StackUtil.canAddAll(this.inv, drops, false)) {
2021-08-30 22:51:41 +02:00
this.level.levelEvent(2001, coordsBlock, Block.getId(this.level.getBlockState(coordsBlock)));
this.level.setBlockAndUpdate(coordsBlock, Blocks.AIR.defaultBlockState());
StackUtil.addAll(this.inv, drops, false);
this.storage.extractEnergyInternal(ENERGY_USE, false);
2021-08-30 22:51:41 +02:00
this.setChanged();
2016-01-23 11:02:35 +01:00
}
}
}
}
}
}
2015-10-05 16:53:28 +02:00
@Override
2018-08-10 05:04:07 +02:00
public IAcceptor getAcceptor() {
return (slot, stack, automation) -> !automation;
2015-12-01 19:48:09 +01:00
}
2018-08-10 05:04:07 +02:00
public int getEnergyScaled(int i) {
return this.storage.getEnergyStored() * i / this.storage.getMaxEnergyStored();
2015-10-05 16:53:28 +02:00
}
@Override
2018-08-10 05:04:07 +02:00
public boolean isRedstoneToggle() {
return true;
}
@Override
2018-08-10 05:04:07 +02:00
public void activateOnPulse() {
this.doWork();
}
2016-11-26 08:58:42 +01:00
@Override
2024-03-04 20:21:48 +01:00
public IEnergyStorage getEnergyStorage(Direction facing) {
2024-03-04 21:38:02 +01:00
return this.storage;
2016-11-26 08:58:42 +01:00
}
2021-02-28 15:47:54 +01:00
@Override
2024-03-02 21:23:08 +01:00
public Component getDisplayName() {
2024-03-03 01:20:53 +01:00
return Component.empty();
2021-02-28 15:47:54 +01:00
}
@Nullable
@Override
2024-03-02 21:23:08 +01:00
public AbstractContainerMenu createMenu(int windowId, Inventory playerInventory, Player playerEntity) {
2021-02-28 15:47:54 +01:00
return new ContainerDirectionalBreaker(windowId, playerInventory, this);
}
2015-10-05 16:53:28 +02:00
}