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

230 lines
8.9 KiB
Java
Raw Normal View History

/*
2016-05-16 22:52:27 +02:00
* This file ("TileEntityMiner.java") is part of the Actually Additions mod for Minecraft.
* 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
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.tile;
import de.ellpeck.actuallyadditions.mod.config.values.ConfigStringListValues;
import de.ellpeck.actuallyadditions.mod.items.ItemDrill;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.network.gui.IButtonReactor;
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;
import net.minecraft.block.Block;
2015-12-11 16:55:50 +01:00
import net.minecraft.block.BlockLiquid;
2016-05-19 20:05:12 +02:00
import net.minecraft.block.state.IBlockState;
2015-12-09 20:24:45 +01:00
import net.minecraft.entity.player.EntityPlayer;
2015-12-09 16:54:25 +01:00
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.NonNullList;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.math.BlockPos;
2018-02-12 02:51:19 +01:00
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.RayTraceResult.Type;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.util.FakePlayerFactory;
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;
import net.minecraftforge.oredict.OreDictionary;
public class TileEntityMiner extends TileEntityInventoryBase implements IButtonReactor, IEnergyDisplay {
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);
public boolean onlyMineOres;
public int checkX;
public int checkY = -1;
public int checkZ;
2016-12-18 17:50:31 +01:00
private int oldEnergy;
private int oldCheckX;
private int oldCheckY;
private int oldCheckZ;
public TileEntityMiner() {
2015-12-09 20:24:45 +01:00
super(9, "miner");
}
@Override
public void writeSyncableNBT(NBTTagCompound compound, NBTType type) {
super.writeSyncableNBT(compound, type);
this.storage.writeToNBT(compound);
if (type != NBTType.SAVE_BLOCK) {
compound.setInteger("CheckX", this.checkX);
compound.setInteger("CheckY", this.checkY);
compound.setInteger("CheckZ", this.checkZ);
}
if (type != NBTType.SAVE_BLOCK || this.onlyMineOres) {
compound.setBoolean("OnlyOres", this.onlyMineOres);
}
}
@Override
public void readSyncableNBT(NBTTagCompound compound, NBTType type) {
super.readSyncableNBT(compound, type);
this.storage.readFromNBT(compound);
if (type != NBTType.SAVE_BLOCK) {
this.checkX = compound.getInteger("CheckX");
this.checkY = compound.getInteger("CheckY");
this.checkZ = compound.getInteger("CheckZ");
}
this.onlyMineOres = compound.getBoolean("OnlyOres");
}
@Override
public void updateEntity() {
super.updateEntity();
if (!this.world.isRemote) {
if (!this.isRedstonePowered && this.ticksElapsed % 5 == 0) {
if (this.checkY != 0) {
int range = TileEntityPhantomface.upgradeRange(DEFAULT_RANGE, this.world, this.pos);
if (this.checkY < 0) {
this.checkY = this.pos.getY() - 1;
this.checkX = -range;
this.checkZ = -range;
}
2015-12-09 16:54:25 +01:00
if (this.checkY > 0) {
if (this.mine()) {
this.checkX++;
if (this.checkX > range) {
this.checkX = -range;
this.checkZ++;
if (this.checkZ > range) {
this.checkZ = -range;
this.checkY--;
}
}
}
2015-12-09 20:24:45 +01:00
}
2015-12-09 16:54:25 +01:00
}
}
if ((this.oldEnergy != this.storage.getEnergyStored() || this.oldCheckX != this.checkX || this.oldCheckY != this.checkY || this.oldCheckZ != this.checkZ) && this.sendUpdateWithInterval()) {
this.oldEnergy = this.storage.getEnergyStored();
this.oldCheckX = this.checkX;
this.oldCheckY = this.checkY;
this.oldCheckZ = this.checkZ;
}
}
}
private boolean mine() {
int actualUse = ENERGY_USE_PER_BLOCK * (this.onlyMineOres ? 3 : 1);
if (this.storage.getEnergyStored() >= actualUse) {
BlockPos pos = new BlockPos(this.pos.getX() + this.checkX, this.checkY, this.pos.getZ() + this.checkZ);
IBlockState state = this.world.getBlockState(pos);
Block block = state.getBlock();
ItemStack stack = block.getPickBlock(state, new RayTraceResult(Type.BLOCK, new Vec3d(0, 0, 0), EnumFacing.DOWN, pos), world, pos, FakePlayerFactory.getMinecraft((WorldServer) world));
if (!block.isAir(this.world.getBlockState(pos), this.world, pos)) {
if (block.getHarvestLevel(this.world.getBlockState(pos)) <= ItemDrill.HARVEST_LEVEL && state.getBlockHardness(this.world, pos) >= 0F && !(block instanceof BlockLiquid) && !(block instanceof IFluidBlock) && this.isMinable(block, stack)) {
NonNullList<ItemStack> drops = NonNullList.create();
block.getDrops(drops, world, pos, state, 0);
float chance = WorldUtil.fireFakeHarvestEventsForDropChance(drops, this.world, pos);
if (chance > 0 && this.world.rand.nextFloat() <= chance) {
if (StackUtil.canAddAll(inv, drops, false)) {
this.world.playEvent(2001, pos, Block.getStateId(this.world.getBlockState(pos)));
this.world.setBlockToAir(pos);
2018-06-23 18:39:59 +02:00
StackUtil.addAll(inv, drops, false);
this.markDirty();
this.storage.extractEnergyInternal(actualUse, false);
this.shootParticles(pos.getX(), pos.getY(), pos.getZ());
} else {
2015-12-09 20:24:45 +01:00
return false;
}
}
}
}
return true;
}
return false;
}
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;
} else {
if (StackUtil.isValid(stack)) {
int[] ids = OreDictionary.getOreIDs(stack);
for (int id : ids) {
String name = OreDictionary.getOreName(id);
if (name.startsWith("ore") || name.startsWith("denseore")) { return true; }
2015-12-27 16:09:17 +01:00
}
2016-02-18 18:43:35 +01:00
String reg = block.getRegistryName().toString();
if (!reg.isEmpty()) {
for (String string : ConfigStringListValues.MINER_EXTRA_WHITELIST.getValue()) {
if (reg.equals(string)) { return true; }
2016-02-18 18:43:35 +01:00
}
}
}
2015-12-27 16:09:17 +01:00
}
}
}
2016-02-18 18:43:35 +01:00
2015-12-27 16:09:17 +01:00
return false;
}
private void shootParticles(int endX, int endY, int endZ) {
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
}
private boolean isBlacklisted(Block block) {
2016-04-20 21:39:03 +02:00
String reg = block.getRegistryName().toString();
if (!reg.isEmpty()) {
for (String string : ConfigStringListValues.MINER_BLACKLIST.getValue()) {
if (reg.equals(string)) { return true; }
}
}
2015-12-27 16:09:17 +01:00
return false;
}
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
public void onButtonPressed(int buttonID, EntityPlayer player) {
if (buttonID == 0) {
2015-12-09 20:24:45 +01:00
this.onlyMineOres = !this.onlyMineOres;
this.sendUpdate();
} else if (buttonID == 1) {
this.checkX = 0;
this.checkY = -1;
this.checkZ = 0;
2015-12-09 20:24:45 +01:00
}
}
@Override
public CustomEnergyStorage getEnergyStorage() {
return this.storage;
}
@Override
public boolean needsHoldShift() {
return false;
}
2016-11-26 08:58:42 +01:00
@Override
public IEnergyStorage getEnergyStorage(EnumFacing facing) {
2016-11-26 08:58:42 +01:00
return this.storage;
}
}