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

121 lines
3.4 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
2016-05-16 22:52:27 +02:00
* This file ("TileEntityBreaker.java") is part of the Actually Additions mod for Minecraft.
2015-08-29 14:33:25 +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-08-29 14:33:25 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
2015-08-29 14:33:25 +02:00
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.tile;
2015-04-19 01:50:02 +02:00
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
2015-04-19 01:50:02 +02:00
import net.minecraft.block.Block;
import net.minecraft.block.BlockLiquid;
2016-03-18 23:47:22 +01:00
import net.minecraft.block.state.IBlockState;
2018-03-19 17:05:28 +01:00
import net.minecraft.init.Blocks;
2015-04-19 01:50:02 +02: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;
import net.minecraftforge.fluids.IFluidBlock;
2015-04-19 01:50:02 +02:00
2018-03-19 17:05:28 +01:00
public class TileEntityBreaker extends TileEntityInventoryBase {
public boolean isPlacer;
private int currentTime;
public TileEntityBreaker(int slots, String name) {
super(slots, name);
}
public TileEntityBreaker() {
super(9, "breaker");
this.isPlacer = false;
}
@Override
public void writeSyncableNBT(NBTTagCompound compound, NBTType type) {
super.writeSyncableNBT(compound, type);
if (type != NBTType.SAVE_BLOCK) {
compound.setInteger("CurrentTime", this.currentTime);
}
}
@Override
public void readSyncableNBT(NBTTagCompound compound, NBTType type) {
super.readSyncableNBT(compound, type);
if (type != NBTType.SAVE_BLOCK) {
this.currentTime = compound.getInteger("CurrentTime");
}
}
@Override
public void updateEntity() {
super.updateEntity();
if (!this.world.isRemote) {
if (!this.isRedstonePowered && !this.isPulseMode) {
if (this.currentTime > 0) {
this.currentTime--;
if (this.currentTime <= 0) {
this.doWork();
}
} else {
this.currentTime = 15;
}
}
}
}
@Override
public boolean isItemValidForSlot(int i, ItemStack stack) {
return true;
2018-03-19 17:05:28 +01:00
}
private void doWork() {
EnumFacing side = WorldUtil.getDirectionByPistonRotation(world.getBlockState(pos));
BlockPos breakCoords = pos.offset(side);
IBlockState stateToBreak = world.getBlockState(breakCoords);
Block blockToBreak = stateToBreak.getBlock();
if (!this.isPlacer && blockToBreak != Blocks.AIR && !(blockToBreak instanceof BlockLiquid) && !(blockToBreak instanceof IFluidBlock) && stateToBreak.getBlockHardness(this.world, breakCoords) >= 0.0F) {
NonNullList<ItemStack> drops = NonNullList.create();
blockToBreak.getDrops(drops, world, breakCoords, stateToBreak, 0);
float chance = WorldUtil.fireFakeHarvestEventsForDropChance(drops, world, breakCoords);
if (chance > 0 && world.rand.nextFloat() <= chance) {
if (StackUtil.canAddAll(slots, drops)) {
world.destroyBlock(breakCoords, false);
StackUtil.addAll(slots, drops);
2018-03-19 17:05:28 +01:00
this.markDirty();
}
}
} else if (this.isPlacer) {
int theSlot = WorldUtil.findFirstFilledSlot(slots);
this.slots.setStackInSlot(theSlot, WorldUtil.useItemAtSide(side, world, pos, slots.getStackInSlot(theSlot)));
if (!StackUtil.isValid(slots.getStackInSlot(theSlot))) {
this.slots.setStackInSlot(theSlot, StackUtil.getEmpty());
}
}
}
@Override
public boolean canExtractItem(int slot, ItemStack stack) {
return true;
}
@Override
public boolean isRedstoneToggle() {
return true;
}
@Override
public void activateOnPulse() {
this.doWork();
}
2015-04-19 01:50:02 +02:00
}