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
|
|
|
|
2016-06-05 12:15:02 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.config.values.ConfigStringListValues;
|
2021-03-01 18:46:12 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.inventory.ContainerMiner;
|
2016-07-02 17:15:53 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.items.ItemDrill;
|
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;
|
2015-12-08 18:10:37 +01:00
|
|
|
import net.minecraft.block.Block;
|
2021-02-27 13:24:45 +01:00
|
|
|
import net.minecraft.block.BlockState;
|
2021-02-27 16:33:00 +01:00
|
|
|
import net.minecraft.block.Blocks;
|
2021-02-26 22:15:48 +01:00
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
2021-03-01 18:46:12 +01:00
|
|
|
import net.minecraft.entity.player.PlayerInventory;
|
|
|
|
import net.minecraft.inventory.container.Container;
|
|
|
|
import net.minecraft.inventory.container.INamedContainerProvider;
|
2015-12-09 16:54:25 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
2021-02-26 22:15:48 +01:00
|
|
|
import net.minecraft.nbt.CompoundNBT;
|
2021-02-27 13:24:45 +01:00
|
|
|
import net.minecraft.util.Direction;
|
2016-03-18 23:47:22 +01:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2021-02-27 16:33:00 +01:00
|
|
|
import net.minecraft.util.math.BlockRayTraceResult;
|
|
|
|
import net.minecraft.util.math.vector.Vector3d;
|
2021-03-01 18:46:12 +01:00
|
|
|
import net.minecraft.util.text.ITextComponent;
|
|
|
|
import net.minecraft.util.text.StringTextComponent;
|
2021-02-27 16:33:00 +01:00
|
|
|
import net.minecraft.world.server.ServerWorld;
|
2018-02-12 02:51:19 +01:00
|
|
|
import net.minecraftforge.common.util.FakePlayerFactory;
|
2021-02-27 16:33:00 +01:00
|
|
|
import net.minecraftforge.common.util.LazyOptional;
|
2016-11-26 08:58:42 +01:00
|
|
|
import net.minecraftforge.energy.IEnergyStorage;
|
2015-12-11 16:56:32 +01:00
|
|
|
import net.minecraftforge.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
|
|
|
|
2021-03-01 18:46:12 +01:00
|
|
|
public class TileEntityMiner extends TileEntityInventoryBase implements IButtonReactor, IEnergyDisplay, INamedContainerProvider {
|
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);
|
2021-02-27 16:33:00 +01:00
|
|
|
public final LazyOptional<IEnergyStorage> lazyEnergy = LazyOptional.of(() -> this.storage);
|
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;
|
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
public TileEntityMiner() {
|
2021-02-27 16:33:00 +01:00
|
|
|
super(ActuallyTiles.MINER_TILE.get(), 9);
|
2015-12-09 20:24:45 +01:00
|
|
|
}
|
|
|
|
|
2016-02-01 20:32:49 +01:00
|
|
|
@Override
|
2021-02-26 22:15:48 +01:00
|
|
|
public void writeSyncableNBT(CompoundNBT 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
|
2021-02-26 22:15:48 +01:00
|
|
|
public void readSyncableNBT(CompoundNBT 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");
|
|
|
|
}
|
|
|
|
|
2015-12-06 02:16:52 +01:00
|
|
|
@Override
|
2018-06-23 01:39:30 +02:00
|
|
|
public void updateEntity() {
|
2015-12-06 02:16:52 +01:00
|
|
|
super.updateEntity();
|
2018-06-23 01:39:30 +02:00
|
|
|
if (!this.world.isRemote) {
|
2015-12-21 22:18:33 +01:00
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
if (!this.isRedstonePowered && this.ticksElapsed % 5 == 0) {
|
|
|
|
if (this.checkY != 0) {
|
2016-12-06 20:06:40 +01:00
|
|
|
int range = TileEntityPhantomface.upgradeRange(DEFAULT_RANGE, this.world, this.pos);
|
2018-06-23 01:39:30 +02:00
|
|
|
if (this.checkY < 0) {
|
|
|
|
this.checkY = this.pos.getY() - 1;
|
2016-12-06 20:06:40 +01:00
|
|
|
this.checkX = -range;
|
|
|
|
this.checkZ = -range;
|
|
|
|
}
|
2015-12-09 16:54:25 +01:00
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
if (this.checkY > 0) {
|
|
|
|
if (this.mine()) {
|
2016-12-06 20:06:40 +01:00
|
|
|
this.checkX++;
|
2018-06-23 01:39:30 +02:00
|
|
|
if (this.checkX > range) {
|
2016-12-06 20:06:40 +01:00
|
|
|
this.checkX = -range;
|
|
|
|
this.checkZ++;
|
2018-06-23 01:39:30 +02:00
|
|
|
if (this.checkZ > range) {
|
2016-12-06 20:06:40 +01:00
|
|
|
this.checkZ = -range;
|
|
|
|
this.checkY--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-09 20:24:45 +01:00
|
|
|
}
|
2015-12-09 16:54:25 +01:00
|
|
|
}
|
|
|
|
}
|
2015-12-21 22:18:33 +01:00
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
if ((this.oldEnergy != this.storage.getEnergyStored() || this.oldCheckX != this.checkX || this.oldCheckY != this.checkY || this.oldCheckZ != this.checkZ) && this.sendUpdateWithInterval()) {
|
2015-12-21 22:18:33 +01:00
|
|
|
this.oldEnergy = this.storage.getEnergyStored();
|
2016-12-06 20:06:40 +01:00
|
|
|
this.oldCheckX = this.checkX;
|
|
|
|
this.oldCheckY = this.checkY;
|
|
|
|
this.oldCheckZ = this.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) {
|
|
|
|
BlockPos pos = new BlockPos(this.pos.getX() + this.checkX, this.checkY, this.pos.getZ() + this.checkZ);
|
2016-12-06 20:06:40 +01:00
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
BlockState state = this.world.getBlockState(pos);
|
2016-12-06 20:06:40 +01:00
|
|
|
Block block = state.getBlock();
|
2021-02-27 16:33:00 +01:00
|
|
|
ItemStack stack = block.getPickBlock(state, new BlockRayTraceResult(new Vector3d(0, 0, 0), Direction.DOWN, pos, false), this.world, pos, FakePlayerFactory.getMinecraft((ServerWorld) this.world));
|
2018-06-23 01:39:30 +02:00
|
|
|
if (!block.isAir(this.world.getBlockState(pos), this.world, pos)) {
|
2021-02-27 16:33:00 +01:00
|
|
|
if (block.getHarvestLevel(this.world.getBlockState(pos)) <= ItemDrill.HARVEST_LEVEL && state.getBlockHardness(this.world, pos) >= 0F && !(block instanceof IFluidBlock) && this.isMinable(block, stack)) {
|
|
|
|
List<ItemStack> drops = Block.getDrops(state, (ServerWorld) this.world, pos, this.world.getTileEntity(pos));
|
2019-05-02 09:32:05 +02:00
|
|
|
float chance = WorldUtil.fireFakeHarvestEventsForDropChance(this, drops, this.world, pos);
|
2016-12-06 20:06:40 +01:00
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
if (chance > 0 && this.world.rand.nextFloat() <= chance) {
|
2019-02-27 19:53:05 +01:00
|
|
|
if (StackUtil.canAddAll(this.inv, drops, false)) {
|
2016-12-06 20:06:40 +01:00
|
|
|
this.world.playEvent(2001, pos, Block.getStateId(this.world.getBlockState(pos)));
|
2021-02-27 16:33:00 +01:00
|
|
|
this.world.setBlockState(pos, Blocks.AIR.getDefaultState());
|
2016-12-06 20:06:40 +01:00
|
|
|
|
2019-02-27 19:53:05 +01:00
|
|
|
StackUtil.addAll(this.inv, drops, false);
|
2016-12-06 20:06:40 +01:00
|
|
|
this.markDirty();
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
private boolean isMinable(Block block, ItemStack stack) {
|
|
|
|
if (block != null) {
|
|
|
|
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 {
|
|
|
|
if (StackUtil.isValid(stack)) {
|
2021-02-27 16:33:00 +01:00
|
|
|
// TODO: [port] come back and see if there is a tag for this
|
2021-03-01 18:46:12 +01:00
|
|
|
|
2021-02-27 16:33:00 +01:00
|
|
|
// int[] ids = OreDictionary.getOreIDs(stack);
|
|
|
|
// for (int id : ids) {
|
|
|
|
// String name = OreDictionary.getOreName(id);
|
|
|
|
// if (name.startsWith("ore") || name.startsWith("denseore")) {
|
|
|
|
// return true;
|
|
|
|
// }
|
|
|
|
// }
|
2016-02-18 18:43:35 +01:00
|
|
|
|
2016-06-08 23:03:36 +02:00
|
|
|
String reg = block.getRegistryName().toString();
|
2018-06-23 01:39:30 +02:00
|
|
|
if (!reg.isEmpty()) {
|
|
|
|
for (String string : ConfigStringListValues.MINER_EXTRA_WHITELIST.getValue()) {
|
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) {
|
2021-02-26 22:15:48 +01:00
|
|
|
AssetUtil.spawnLaserWithTimeServer(this.world, this.getPos().getX(), this.getPos().getY(), this.getPos().getZ(), endX, endY, endZ, new float[]{65F / 255F, 150F / 255F, 2F / 255F}, 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) {
|
2016-04-20 21:39:03 +02:00
|
|
|
String reg = block.getRegistryName().toString();
|
2018-06-23 01:39:30 +02:00
|
|
|
if (!reg.isEmpty()) {
|
|
|
|
for (String string : ConfigStringListValues.MINER_BLACKLIST.getValue()) {
|
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
|
2021-02-26 22:15:48 +01:00
|
|
|
public void onButtonPressed(int buttonID, PlayerEntity 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
|
2021-02-27 16:33:00 +01:00
|
|
|
public LazyOptional<IEnergyStorage> getEnergyStorage(Direction facing) {
|
|
|
|
return this.lazyEnergy;
|
2016-11-26 08:58:42 +01:00
|
|
|
}
|
2021-03-01 18:46:12 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public ITextComponent getDisplayName() {
|
|
|
|
return StringTextComponent.EMPTY;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public Container createMenu(int windowId, PlayerInventory playerInventory, PlayerEntity player) {
|
|
|
|
return new ContainerMiner(windowId, playerInventory, this);
|
|
|
|
}
|
2015-12-06 02:16:52 +01:00
|
|
|
}
|