2015-12-06 02:16:52 +01:00
|
|
|
/*
|
2016-05-16 22:52:27 +02:00
|
|
|
* This file ("TileEntityMiner.java") is part of the Actually Additions mod for Minecraft.
|
2015-12-06 02:16:52 +01: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-12-06 02:16:52 +01:00
|
|
|
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
|
|
|
|
*
|
2017-01-01 16:23:26 +01:00
|
|
|
* © 2015-2017 Ellpeck
|
2015-12-06 02:16:52 +01:00
|
|
|
*/
|
|
|
|
|
2016-01-05 04:47:35 +01:00
|
|
|
package de.ellpeck.actuallyadditions.mod.tile;
|
2015-12-06 02:16:52 +01:00
|
|
|
|
2021-08-30 22:51:41 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
|
2024-03-12 18:00:23 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.config.CommonConfig;
|
2021-03-01 18:46:12 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.inventory.ContainerMiner;
|
2016-01-05 04:47:35 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.network.gui.IButtonReactor;
|
2016-06-15 16:43:59 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.AssetUtil;
|
2018-08-10 05:04:07 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor;
|
2016-12-05 15:27:01 +01:00
|
|
|
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;
|
2024-03-04 21:38:02 +01:00
|
|
|
import net.minecraft.core.registries.BuiltInRegistries;
|
2024-03-02 21:23:08 +01:00
|
|
|
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.item.Tiers;
|
|
|
|
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;
|
|
|
|
import net.minecraft.world.phys.BlockHitResult;
|
|
|
|
import net.minecraft.world.phys.Vec3;
|
2024-03-12 18:00:23 +01:00
|
|
|
import net.neoforged.neoforge.common.Tags;
|
2024-03-04 20:21:48 +01:00
|
|
|
import net.neoforged.neoforge.common.TierSortingRegistry;
|
|
|
|
import net.neoforged.neoforge.common.util.FakePlayerFactory;
|
|
|
|
import net.neoforged.neoforge.energy.IEnergyStorage;
|
|
|
|
import net.neoforged.neoforge.fluids.IFluidBlock;
|
2021-02-27 16:33:00 +01:00
|
|
|
|
2021-03-01 18:46:12 +01:00
|
|
|
import javax.annotation.Nullable;
|
2021-02-27 16:33:00 +01:00
|
|
|
import java.util.List;
|
2015-12-06 02:16:52 +01:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
public class TileEntityVerticalDigger extends TileEntityInventoryBase implements IButtonReactor, IEnergyDisplay, MenuProvider {
|
2015-12-06 02:16:52 +01:00
|
|
|
|
2016-11-21 20:25:32 +01:00
|
|
|
public static final int ENERGY_USE_PER_BLOCK = 650;
|
2015-12-27 15:57:14 +01:00
|
|
|
public static final int DEFAULT_RANGE = 2;
|
2016-11-26 20:43:50 +01:00
|
|
|
public final CustomEnergyStorage storage = new CustomEnergyStorage(200000, 2000, 0);
|
2016-07-02 15:01:46 +02:00
|
|
|
public boolean onlyMineOres;
|
2016-12-06 20:06:40 +01:00
|
|
|
public int checkX;
|
|
|
|
public int checkY = -1;
|
|
|
|
public int checkZ;
|
2016-12-18 17:50:31 +01:00
|
|
|
private int oldEnergy;
|
2016-12-06 20:06:40 +01:00
|
|
|
private int oldCheckX;
|
|
|
|
private int oldCheckY;
|
|
|
|
private int oldCheckZ;
|
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
public TileEntityVerticalDigger(BlockPos pos, BlockState state) {
|
|
|
|
super(ActuallyBlocks.VERTICAL_DIGGER.getTileEntityType(), pos, state, 9);
|
2015-12-09 20:24:45 +01:00
|
|
|
}
|
|
|
|
|
2016-02-01 20:32:49 +01:00
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public void writeSyncableNBT(CompoundTag compound, NBTType type) {
|
2016-07-02 15:01:46 +02:00
|
|
|
super.writeSyncableNBT(compound, type);
|
2016-02-01 20:32:49 +01:00
|
|
|
this.storage.writeToNBT(compound);
|
2018-06-23 01:39:30 +02:00
|
|
|
if (type != NBTType.SAVE_BLOCK) {
|
2021-02-27 16:33:00 +01:00
|
|
|
compound.putInt("CheckX", this.checkX);
|
|
|
|
compound.putInt("CheckY", this.checkY);
|
|
|
|
compound.putInt("CheckZ", this.checkZ);
|
2016-07-02 15:01:46 +02:00
|
|
|
}
|
2018-06-23 01:39:30 +02:00
|
|
|
if (type != NBTType.SAVE_BLOCK || this.onlyMineOres) {
|
2021-02-27 16:33:00 +01:00
|
|
|
compound.putBoolean("OnlyOres", this.onlyMineOres);
|
2016-07-02 15:01:46 +02:00
|
|
|
}
|
2016-02-01 20:32:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public void readSyncableNBT(CompoundTag compound, NBTType type) {
|
2016-07-02 15:01:46 +02:00
|
|
|
super.readSyncableNBT(compound, type);
|
2016-02-01 20:32:49 +01:00
|
|
|
this.storage.readFromNBT(compound);
|
2018-06-23 01:39:30 +02:00
|
|
|
if (type != NBTType.SAVE_BLOCK) {
|
2021-02-27 16:33:00 +01:00
|
|
|
this.checkX = compound.getInt("CheckX");
|
|
|
|
this.checkY = compound.getInt("CheckY");
|
|
|
|
this.checkZ = compound.getInt("CheckZ");
|
2016-07-02 15:01:46 +02:00
|
|
|
}
|
2016-02-01 20:32:49 +01:00
|
|
|
this.onlyMineOres = compound.getBoolean("OnlyOres");
|
|
|
|
}
|
|
|
|
|
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 TileEntityVerticalDigger tile) {
|
|
|
|
tile.clientTick();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static <T extends BlockEntity> void serverTick(Level level, BlockPos pos, BlockState state, T t) {
|
|
|
|
if (t instanceof TileEntityVerticalDigger tile) {
|
|
|
|
tile.serverTick();
|
2015-12-21 22:18:33 +01:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
if (!tile.isRedstonePowered && tile.ticksElapsed % 5 == 0) {
|
|
|
|
if (tile.checkY != 0) {
|
|
|
|
int range = TileEntityPhantomface.upgradeRange(DEFAULT_RANGE, level, pos);
|
|
|
|
if (tile.checkY < 0) {
|
|
|
|
tile.checkY = tile.worldPosition.getY() - 1;
|
|
|
|
tile.checkX = -range;
|
|
|
|
tile.checkZ = -range;
|
2016-12-06 20:06:40 +01:00
|
|
|
}
|
2015-12-09 16:54:25 +01:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
if (tile.checkY > 0) {
|
|
|
|
if (tile.mine()) {
|
|
|
|
tile.checkX++;
|
|
|
|
if (tile.checkX > range) {
|
|
|
|
tile.checkX = -range;
|
|
|
|
tile.checkZ++;
|
|
|
|
if (tile.checkZ > range) {
|
|
|
|
tile.checkZ = -range;
|
|
|
|
tile.checkY--;
|
2016-12-06 20:06:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-09 20:24:45 +01:00
|
|
|
}
|
2015-12-09 16:54:25 +01:00
|
|
|
}
|
|
|
|
}
|
2015-12-21 22:18:33 +01:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
if ((tile.oldEnergy != tile.storage.getEnergyStored() || tile.oldCheckX != tile.checkX || tile.oldCheckY != tile.checkY || tile.oldCheckZ != tile.checkZ) && tile.sendUpdateWithInterval()) {
|
|
|
|
tile.oldEnergy = tile.storage.getEnergyStored();
|
|
|
|
tile.oldCheckX = tile.checkX;
|
|
|
|
tile.oldCheckY = tile.checkY;
|
|
|
|
tile.oldCheckZ = tile.checkZ;
|
2015-12-21 22:18:33 +01:00
|
|
|
}
|
2015-12-08 18:10:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
private boolean mine() {
|
2021-02-26 22:15:48 +01:00
|
|
|
int actualUse = ENERGY_USE_PER_BLOCK * (this.onlyMineOres
|
|
|
|
? 3
|
|
|
|
: 1);
|
2018-06-23 01:39:30 +02:00
|
|
|
if (this.storage.getEnergyStored() >= actualUse) {
|
2021-08-30 22:51:41 +02:00
|
|
|
BlockPos pos = new BlockPos(this.worldPosition.getX() + this.checkX, this.checkY, this.worldPosition.getZ() + this.checkZ);
|
2016-12-06 20:06:40 +01:00
|
|
|
|
2021-08-30 22:51:41 +02:00
|
|
|
BlockState state = this.level.getBlockState(pos);
|
2016-12-06 20:06:40 +01:00
|
|
|
Block block = state.getBlock();
|
2024-03-02 21:23:08 +01:00
|
|
|
ItemStack stack = block.getCloneItemStack(state, new BlockHitResult(new Vec3(0, 0, 0), Direction.DOWN, pos, false), this.level, pos, FakePlayerFactory.getMinecraft((ServerLevel) this.level));
|
|
|
|
if (!state.isAir()) {
|
|
|
|
//block.getHarvestLevel(state) <= DrillItem.HARVEST_LEVEL
|
2024-03-12 18:00:23 +01:00
|
|
|
if (TierSortingRegistry.isCorrectTierForDrops(Tiers.NETHERITE, state) && state.getDestroySpeed(this.level, pos) >= 0F && !(block instanceof IFluidBlock) && this.isMinable(state, stack)) {
|
2024-03-02 21:23:08 +01:00
|
|
|
List<ItemStack> drops = Block.getDrops(state, (ServerLevel) this.level, pos, this.level.getBlockEntity(pos));
|
2021-08-30 22:51:41 +02:00
|
|
|
float chance = WorldUtil.fireFakeHarvestEventsForDropChance(this, drops, this.level, pos);
|
2016-12-06 20:06:40 +01:00
|
|
|
|
2021-08-30 22:51:41 +02:00
|
|
|
if (chance > 0 && this.level.random.nextFloat() <= chance) {
|
2019-02-27 19:53:05 +01:00
|
|
|
if (StackUtil.canAddAll(this.inv, drops, false)) {
|
2024-03-02 21:23:08 +01:00
|
|
|
this.level.levelEvent(2001, pos, Block.getId(state));
|
2021-08-30 22:51:41 +02:00
|
|
|
this.level.setBlockAndUpdate(pos, Blocks.AIR.defaultBlockState());
|
2016-12-06 20:06:40 +01:00
|
|
|
|
2019-02-27 19:53:05 +01:00
|
|
|
StackUtil.addAll(this.inv, drops, false);
|
2021-08-30 22:51:41 +02:00
|
|
|
this.setChanged();
|
2016-12-06 20:06:40 +01:00
|
|
|
|
|
|
|
this.storage.extractEnergyInternal(actualUse, false);
|
|
|
|
this.shootParticles(pos.getX(), pos.getY(), pos.getZ());
|
2018-06-23 01:39:30 +02:00
|
|
|
} else {
|
2015-12-09 20:24:45 +01:00
|
|
|
return false;
|
2015-12-08 18:10:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-06 20:06:40 +01:00
|
|
|
return true;
|
2015-12-06 02:16:52 +01:00
|
|
|
}
|
2016-12-06 20:06:40 +01:00
|
|
|
return false;
|
2015-12-09 18:47:42 +01:00
|
|
|
}
|
|
|
|
|
2024-03-12 18:00:23 +01:00
|
|
|
private boolean isMinable(BlockState state, ItemStack stack) {
|
|
|
|
Block block = state.getBlock();
|
|
|
|
if (!state.isEmpty()) {
|
2018-06-23 01:39:30 +02:00
|
|
|
if (!this.isBlacklisted(block)) {
|
|
|
|
if (!this.onlyMineOres) {
|
2016-02-18 18:43:35 +01:00
|
|
|
return true;
|
2018-06-23 01:39:30 +02:00
|
|
|
} else {
|
2024-03-12 18:00:23 +01:00
|
|
|
if (!stack.isEmpty()) {
|
|
|
|
if (stack.is(Tags.Items.ORES) || state.is(Tags.Blocks.ORES)) { // Check both the block and item tags
|
|
|
|
return true;
|
|
|
|
}
|
2024-03-04 20:21:48 +01:00
|
|
|
String reg = BuiltInRegistries.BLOCK.getKey(block).toString();
|
2018-06-23 01:39:30 +02:00
|
|
|
if (!reg.isEmpty()) {
|
2024-03-12 18:00:23 +01:00
|
|
|
for (String string : CommonConfig.Machines.MINER_EXTRA_WHITELIST.get()) {
|
2021-02-26 22:15:48 +01:00
|
|
|
if (reg.equals(string)) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-02-18 18:43:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-27 16:09:17 +01:00
|
|
|
}
|
|
|
|
}
|
2015-12-09 18:47:42 +01:00
|
|
|
}
|
2016-02-18 18:43:35 +01:00
|
|
|
|
2015-12-27 16:09:17 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
private void shootParticles(int endX, int endY, int endZ) {
|
2022-07-06 19:14:19 +02:00
|
|
|
AssetUtil.spawnLaserWithTimeServer(this.level, this.worldPosition.getX(), this.worldPosition.getY(), this.worldPosition.getZ(), endX, endY, endZ, 0x429602, 10, 120, 0.1F, 0.8F);
|
2016-02-01 17:49:55 +01:00
|
|
|
}
|
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
private boolean isBlacklisted(Block block) {
|
2024-03-04 21:38:02 +01:00
|
|
|
String reg = BuiltInRegistries.BLOCK.getKey(block).toString();
|
2018-06-23 01:39:30 +02:00
|
|
|
if (!reg.isEmpty()) {
|
2024-03-12 18:00:23 +01:00
|
|
|
for (String string : CommonConfig.Machines.MINER_BLACKLIST.get()) {
|
2021-02-26 22:15:48 +01:00
|
|
|
if (reg.equals(string)) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-12-09 18:47:42 +01:00
|
|
|
}
|
|
|
|
}
|
2015-12-27 16:09:17 +01:00
|
|
|
return false;
|
2015-12-09 18:47:42 +01:00
|
|
|
}
|
|
|
|
|
2016-02-01 17:49:55 +01:00
|
|
|
@Override
|
2018-08-10 05:04:07 +02:00
|
|
|
public IAcceptor getAcceptor() {
|
|
|
|
return (stack, slot, automation) -> !automation;
|
2015-12-09 20:24:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public void onButtonPressed(int buttonID, Player player) {
|
2018-06-23 01:39:30 +02:00
|
|
|
if (buttonID == 0) {
|
2015-12-09 20:24:45 +01:00
|
|
|
this.onlyMineOres = !this.onlyMineOres;
|
|
|
|
this.sendUpdate();
|
2018-06-23 01:39:30 +02:00
|
|
|
} else if (buttonID == 1) {
|
2016-12-06 20:06:40 +01:00
|
|
|
this.checkX = 0;
|
|
|
|
this.checkY = -1;
|
|
|
|
this.checkZ = 0;
|
2015-12-09 20:24:45 +01:00
|
|
|
}
|
|
|
|
}
|
2015-12-13 00:54:25 +01:00
|
|
|
|
|
|
|
@Override
|
2018-06-23 01:39:30 +02:00
|
|
|
public CustomEnergyStorage getEnergyStorage() {
|
2016-07-21 13:22:55 +02:00
|
|
|
return this.storage;
|
2015-12-13 00:54:25 +01:00
|
|
|
}
|
2016-07-02 18:39:47 +02:00
|
|
|
|
|
|
|
@Override
|
2018-06-23 01:39:30 +02:00
|
|
|
public boolean needsHoldShift() {
|
2016-07-02 18:39:47 +02:00
|
|
|
return false;
|
|
|
|
}
|
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-03-01 18:46:12 +01:00
|
|
|
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public Component getDisplayName() {
|
2024-03-12 18:00:23 +01:00
|
|
|
return Component.translatable("container.actuallyadditions.vertical_digger");
|
2021-03-01 18:46:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public AbstractContainerMenu createMenu(int windowId, Inventory playerInventory, Player player) {
|
2021-03-01 18:46:12 +01:00
|
|
|
return new ContainerMiner(windowId, playerInventory, this);
|
|
|
|
}
|
2015-12-06 02:16:52 +01:00
|
|
|
}
|