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
|
|
|
|
*
|
2016-05-16 22:52:27 +02:00
|
|
|
* © 2015-2016 Ellpeck 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
|
|
|
|
|
|
|
import cofh.api.energy.EnergyStorage;
|
|
|
|
import cofh.api.energy.IEnergyReceiver;
|
2016-01-05 04:47:35 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.config.ConfigValues;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.network.PacketHandler;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.network.PacketParticle;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.network.gui.IButtonReactor;
|
2016-01-08 13:31:58 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.PosUtil;
|
2016-05-04 13:15:30 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.Util;
|
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;
|
2015-12-11 16:55:50 +01:00
|
|
|
import net.minecraft.block.BlockLiquid;
|
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;
|
2015-12-06 02:16:52 +01:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2016-01-07 19:47:53 +01:00
|
|
|
import net.minecraft.util.EnumFacing;
|
2016-03-18 23:47:22 +01:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2016-05-04 13:15:30 +02:00
|
|
|
import net.minecraftforge.event.ForgeEventFactory;
|
2015-12-11 16:56:32 +01:00
|
|
|
import net.minecraftforge.fluids.IFluidBlock;
|
2016-01-07 18:20:59 +01:00
|
|
|
import net.minecraftforge.fml.common.network.NetworkRegistry;
|
|
|
|
import net.minecraftforge.fml.relauncher.Side;
|
|
|
|
import net.minecraftforge.fml.relauncher.SideOnly;
|
2015-12-09 18:47:42 +01:00
|
|
|
import net.minecraftforge.oredict.OreDictionary;
|
2015-12-06 02:16:52 +01:00
|
|
|
|
2016-05-04 13:15:30 +02:00
|
|
|
import java.util.List;
|
2015-12-09 16:54:25 +01:00
|
|
|
|
2015-12-21 22:18:33 +01:00
|
|
|
public class TileEntityMiner extends TileEntityInventoryBase implements IEnergyReceiver, IButtonReactor, IEnergySaver, IEnergyDisplay{
|
2015-12-06 02:16:52 +01:00
|
|
|
|
2015-12-09 20:24:45 +01:00
|
|
|
public static final int ENERGY_USE_PER_BLOCK = 500;
|
2015-12-27 15:57:14 +01:00
|
|
|
public static final int DEFAULT_RANGE = 2;
|
2015-12-19 10:30:39 +01:00
|
|
|
public EnergyStorage storage = new EnergyStorage(1000000);
|
2015-12-09 20:24:45 +01:00
|
|
|
public int layerAt = -1;
|
2015-12-27 15:44:16 +01:00
|
|
|
public boolean onlyMineOres = true;
|
2015-12-21 22:18:33 +01:00
|
|
|
private int oldLayerAt;
|
|
|
|
private int oldEnergy;
|
2015-12-06 02:16:52 +01:00
|
|
|
|
2015-12-09 20:24:45 +01:00
|
|
|
public TileEntityMiner(){
|
|
|
|
super(9, "miner");
|
|
|
|
}
|
|
|
|
|
2016-02-01 20:32:49 +01:00
|
|
|
@Override
|
|
|
|
public void writeSyncableNBT(NBTTagCompound compound, boolean sync){
|
|
|
|
super.writeSyncableNBT(compound, sync);
|
|
|
|
this.storage.writeToNBT(compound);
|
|
|
|
compound.setInteger("Layer", this.layerAt);
|
|
|
|
compound.setBoolean("OnlyOres", this.onlyMineOres);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void readSyncableNBT(NBTTagCompound compound, boolean sync){
|
|
|
|
super.readSyncableNBT(compound, sync);
|
|
|
|
this.storage.readFromNBT(compound);
|
|
|
|
this.layerAt = compound.getInteger("Layer");
|
|
|
|
this.onlyMineOres = compound.getBoolean("OnlyOres");
|
|
|
|
}
|
|
|
|
|
2015-12-06 02:16:52 +01:00
|
|
|
@Override
|
|
|
|
public void updateEntity(){
|
|
|
|
super.updateEntity();
|
|
|
|
if(!this.worldObj.isRemote){
|
2015-12-21 22:18:33 +01:00
|
|
|
if(this.layerAt == -1){
|
2016-01-07 19:47:53 +01:00
|
|
|
this.layerAt = this.getPos().getY()-1;
|
2015-12-21 22:18:33 +01:00
|
|
|
}
|
|
|
|
|
2015-12-09 20:24:45 +01:00
|
|
|
if(!this.isRedstonePowered && this.ticksElapsed%5 == 0){
|
2015-12-09 16:54:25 +01:00
|
|
|
|
2015-12-09 20:24:45 +01:00
|
|
|
if(this.layerAt > 0){
|
2016-05-02 17:46:53 +02:00
|
|
|
if(this.mine(TileEntityPhantomface.upgradeRange(DEFAULT_RANGE, this.worldObj, this.pos))){
|
2015-12-09 20:24:45 +01:00
|
|
|
this.layerAt--;
|
|
|
|
}
|
2015-12-09 16:54:25 +01:00
|
|
|
}
|
|
|
|
}
|
2015-12-21 22:18:33 +01:00
|
|
|
|
|
|
|
if((this.oldEnergy != this.storage.getEnergyStored() || this.oldLayerAt != this.layerAt) && this.sendUpdateWithInterval()){
|
|
|
|
this.oldEnergy = this.storage.getEnergyStored();
|
|
|
|
this.oldLayerAt = this.layerAt;
|
|
|
|
}
|
2015-12-08 18:10:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-09 16:54:25 +01:00
|
|
|
private boolean mine(int range){
|
2015-12-08 18:10:37 +01:00
|
|
|
for(int anX = -range; anX <= range; anX++){
|
|
|
|
for(int aZ = -range; aZ <= range; aZ++){
|
2015-12-09 20:24:45 +01:00
|
|
|
int actualUse = ENERGY_USE_PER_BLOCK*(this.onlyMineOres ? 3 : 1);
|
|
|
|
if(this.storage.getEnergyStored() >= actualUse){
|
2016-01-08 13:31:58 +01:00
|
|
|
BlockPos pos = new BlockPos(this.pos.getX()+anX, this.layerAt, this.pos.getZ()+aZ);
|
2016-01-07 23:42:42 +01:00
|
|
|
|
2016-05-02 17:46:53 +02:00
|
|
|
Block block = PosUtil.getBlock(pos, this.worldObj);
|
|
|
|
int meta = PosUtil.getMetadata(pos, this.worldObj);
|
2016-03-18 23:47:22 +01:00
|
|
|
if(block != null && !block.isAir(this.worldObj.getBlockState(pos), this.worldObj, pos)){
|
2016-05-02 17:46:53 +02:00
|
|
|
if(block.getHarvestLevel(this.worldObj.getBlockState(pos)) <= 3F && block.getBlockHardness(this.worldObj.getBlockState(pos), this.worldObj, pos) >= 0F && !(block instanceof BlockLiquid) && !(block instanceof IFluidBlock) && this.isMinable(block, meta)){
|
2016-05-04 13:15:30 +02:00
|
|
|
List<ItemStack> drops = block.getDrops(this.worldObj, pos, this.worldObj.getBlockState(pos), 0);
|
|
|
|
float chance = ForgeEventFactory.fireBlockHarvesting(drops, this.worldObj, pos, this.worldObj.getBlockState(pos), 0, 1, false, null);
|
2015-12-09 16:54:25 +01:00
|
|
|
|
2016-05-04 13:15:30 +02:00
|
|
|
if(Util.RANDOM.nextFloat() <= chance){
|
|
|
|
if(WorldUtil.addToInventory(this, drops, false, true)){
|
|
|
|
if(!ConfigValues.lessBlockBreakingEffects){
|
|
|
|
this.worldObj.playAuxSFX(2001, pos, Block.getStateId(this.worldObj.getBlockState(pos)));
|
|
|
|
}
|
|
|
|
this.worldObj.setBlockToAir(pos);
|
2015-12-09 16:54:25 +01:00
|
|
|
|
2016-05-04 13:15:30 +02:00
|
|
|
WorldUtil.addToInventory(this, drops, true, true);
|
|
|
|
this.markDirty();
|
2015-12-06 02:16:52 +01:00
|
|
|
|
2016-05-04 13:15:30 +02:00
|
|
|
this.storage.extractEnergy(actualUse, false);
|
|
|
|
this.shootParticles(pos.getX(), pos.getY(), pos.getZ());
|
|
|
|
}
|
2015-12-09 16:54:25 +01:00
|
|
|
}
|
2015-12-09 20:24:45 +01:00
|
|
|
return false;
|
2015-12-08 18:10:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-09 20:24:45 +01:00
|
|
|
else{
|
|
|
|
return false;
|
|
|
|
}
|
2015-12-08 18:10:37 +01:00
|
|
|
}
|
2015-12-06 02:16:52 +01:00
|
|
|
}
|
2015-12-09 20:24:45 +01:00
|
|
|
return true;
|
2015-12-09 18:47:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isMinable(Block block, int meta){
|
2016-02-18 18:43:35 +01:00
|
|
|
if(block != null){
|
|
|
|
if(!this.isBlacklisted(block)){
|
|
|
|
if(!this.onlyMineOres){
|
|
|
|
return true;
|
2015-12-27 16:09:17 +01:00
|
|
|
}
|
2016-02-18 18:43:35 +01:00
|
|
|
else{
|
|
|
|
int[] ids = OreDictionary.getOreIDs(new ItemStack(block, 1, meta));
|
|
|
|
for(int id : ids){
|
|
|
|
String name = OreDictionary.getOreName(id);
|
|
|
|
if(name.startsWith("ore") || name.startsWith("denseore")){
|
2015-12-27 16:09:17 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2016-02-18 18:43:35 +01:00
|
|
|
|
2016-04-20 21:39:03 +02:00
|
|
|
String reg = block.getRegistryName().toString();
|
2016-02-18 18:43:35 +01:00
|
|
|
if(reg != null && !reg.isEmpty()){
|
|
|
|
for(String string : ConfigValues.minerExtraWhitelist){
|
|
|
|
if(reg.equals(string)){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2016-02-01 17:49:55 +01:00
|
|
|
private void shootParticles(int endX, int endY, int endZ){
|
|
|
|
if(!ConfigValues.lessParticles){
|
2016-05-02 17:46:53 +02:00
|
|
|
PacketHandler.theNetwork.sendToAllAround(new PacketParticle(this.getPos().getX(), this.getPos().getY(), this.getPos().getZ(), endX, endY, endZ, new float[]{62F/255F, 163F/255F, 74F/255F}, 5, 1.0F), new NetworkRegistry.TargetPoint(this.worldObj.provider.getDimension(), this.getPos().getX(), this.getPos().getY(), this.getPos().getZ(), 96));
|
2016-02-01 17:49:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-27 16:09:17 +01:00
|
|
|
private boolean isBlacklisted(Block block){
|
2016-04-20 21:39:03 +02:00
|
|
|
String reg = block.getRegistryName().toString();
|
2015-12-27 16:09:17 +01:00
|
|
|
if(reg != null && !reg.isEmpty()){
|
|
|
|
for(String string : ConfigValues.minerBlacklist){
|
|
|
|
if(reg.equals(string)){
|
2015-12-09 18:47:42 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
public boolean isItemValidForSlot(int slot, ItemStack stack){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-06 02:16:52 +01:00
|
|
|
@Override
|
2016-01-07 19:47:53 +01:00
|
|
|
public int receiveEnergy(EnumFacing from, int maxReceive, boolean simulate){
|
2015-12-06 02:16:52 +01:00
|
|
|
return this.storage.receiveEnergy(maxReceive, simulate);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-01-07 19:47:53 +01:00
|
|
|
public int getEnergyStored(EnumFacing from){
|
2015-12-06 02:16:52 +01:00
|
|
|
return this.storage.getEnergyStored();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-01-07 19:47:53 +01:00
|
|
|
public int getMaxEnergyStored(EnumFacing from){
|
2015-12-06 02:16:52 +01:00
|
|
|
return this.storage.getMaxEnergyStored();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-01-07 19:47:53 +01:00
|
|
|
public boolean canConnectEnergy(EnumFacing from){
|
2015-12-06 02:16:52 +01:00
|
|
|
return true;
|
|
|
|
}
|
2015-12-09 20:24:45 +01:00
|
|
|
|
|
|
|
@Override
|
2016-01-07 19:47:53 +01:00
|
|
|
public boolean canInsertItem(int slot, ItemStack stack, EnumFacing side){
|
2015-12-09 20:24:45 +01:00
|
|
|
return this.isItemValidForSlot(slot, stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-01-07 19:47:53 +01:00
|
|
|
public boolean canExtractItem(int slot, ItemStack stack, EnumFacing side){
|
2015-12-09 20:24:45 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onButtonPressed(int buttonID, EntityPlayer player){
|
|
|
|
if(buttonID == 0){
|
|
|
|
this.onlyMineOres = !this.onlyMineOres;
|
|
|
|
this.sendUpdate();
|
|
|
|
}
|
|
|
|
else if(buttonID == 1){
|
|
|
|
this.layerAt = -1;
|
|
|
|
}
|
|
|
|
}
|
2015-12-13 00:54:25 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getEnergy(){
|
|
|
|
return this.storage.getEnergyStored();
|
|
|
|
}
|
|
|
|
|
2015-12-21 22:18:33 +01:00
|
|
|
@Override
|
2015-12-21 22:46:23 +01:00
|
|
|
public void setEnergy(int energy){
|
|
|
|
this.storage.setEnergyStored(energy);
|
2015-12-21 22:18:33 +01:00
|
|
|
}
|
|
|
|
|
2015-12-13 00:54:25 +01:00
|
|
|
@Override
|
2015-12-21 22:46:23 +01:00
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public int getMaxEnergy(){
|
|
|
|
return this.storage.getMaxEnergyStored();
|
2015-12-13 00:54:25 +01:00
|
|
|
}
|
2015-12-06 02:16:52 +01:00
|
|
|
}
|