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
|
|
|
|
2021-08-30 01:38:12 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
|
2021-02-28 12:33:01 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.inventory.ContainerBreaker;
|
2018-08-10 05:04:07 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor;
|
2022-04-04 02:12:40 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.NetHandlerSpaghettiServer;
|
2016-11-16 16:59:00 +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-04-19 01:50:02 +02:00
|
|
|
import net.minecraft.block.Block;
|
2021-02-27 16:33:00 +01:00
|
|
|
import net.minecraft.block.BlockState;
|
|
|
|
import net.minecraft.block.Blocks;
|
2021-02-28 12:33:01 +01:00
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
|
|
import net.minecraft.entity.player.PlayerInventory;
|
|
|
|
import net.minecraft.inventory.container.Container;
|
|
|
|
import net.minecraft.inventory.container.INamedContainerProvider;
|
2015-04-19 01:50:02 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
2021-02-26 22:15:48 +01:00
|
|
|
import net.minecraft.nbt.CompoundNBT;
|
2021-02-27 16:33:00 +01:00
|
|
|
import net.minecraft.tileentity.TileEntityType;
|
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-28 12:33:01 +01:00
|
|
|
import net.minecraft.util.text.ITextComponent;
|
|
|
|
import net.minecraft.util.text.StringTextComponent;
|
2022-04-03 22:18:46 +02:00
|
|
|
import net.minecraft.util.text.TranslationTextComponent;
|
2021-02-27 16:33:00 +01:00
|
|
|
import net.minecraft.world.server.ServerWorld;
|
2022-04-04 02:12:40 +02:00
|
|
|
import net.minecraftforge.common.util.FakePlayer;
|
|
|
|
import net.minecraftforge.common.util.FakePlayerFactory;
|
2016-08-29 02:09:34 +02:00
|
|
|
import net.minecraftforge.fluids.IFluidBlock;
|
2015-04-19 01:50:02 +02:00
|
|
|
|
2022-04-03 22:18:46 +02:00
|
|
|
import javax.annotation.Nonnull;
|
2021-02-28 12:33:01 +01:00
|
|
|
import javax.annotation.Nullable;
|
2021-02-27 16:33:00 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
2021-08-22 17:09:06 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.tile.TileEntityBase.NBTType;
|
|
|
|
|
2021-02-28 12:33:01 +01:00
|
|
|
public class TileEntityBreaker extends TileEntityInventoryBase implements INamedContainerProvider {
|
2018-03-19 17:05:28 +01:00
|
|
|
|
2018-08-10 05:04:07 +02:00
|
|
|
public boolean isPlacer;
|
|
|
|
private int currentTime;
|
|
|
|
|
2021-02-27 16:33:00 +01:00
|
|
|
public TileEntityBreaker(TileEntityType<?> type, int slots) {
|
|
|
|
super(type, slots);
|
2018-08-10 05:04:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public TileEntityBreaker() {
|
2021-08-30 01:38:12 +02:00
|
|
|
super(ActuallyBlocks.BREAKER.getTileEntityType(), 9);
|
2018-08-10 05:04:07 +02:00
|
|
|
this.isPlacer = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-26 22:15:48 +01:00
|
|
|
public void writeSyncableNBT(CompoundNBT compound, NBTType type) {
|
2018-08-10 05:04:07 +02:00
|
|
|
super.writeSyncableNBT(compound, type);
|
|
|
|
if (type != NBTType.SAVE_BLOCK) {
|
2021-02-27 13:24:45 +01:00
|
|
|
compound.putInt("CurrentTime", this.currentTime);
|
2018-08-10 05:04:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-26 22:15:48 +01:00
|
|
|
public void readSyncableNBT(CompoundNBT compound, NBTType type) {
|
2018-08-10 05:04:07 +02:00
|
|
|
super.readSyncableNBT(compound, type);
|
|
|
|
if (type != NBTType.SAVE_BLOCK) {
|
2021-02-27 13:24:45 +01:00
|
|
|
this.currentTime = compound.getInt("CurrentTime");
|
2018-08-10 05:04:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void updateEntity() {
|
|
|
|
super.updateEntity();
|
2021-08-22 17:09:06 +02:00
|
|
|
if (!this.level.isClientSide) {
|
2018-08-10 05:04:07 +02:00
|
|
|
if (!this.isRedstonePowered && !this.isPulseMode) {
|
|
|
|
if (this.currentTime > 0) {
|
|
|
|
this.currentTime--;
|
|
|
|
if (this.currentTime <= 0) {
|
|
|
|
this.doWork();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.currentTime = 15;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IAcceptor getAcceptor() {
|
|
|
|
return (slot, stack, automation) -> !automation;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void doWork() {
|
2021-08-22 17:09:06 +02:00
|
|
|
Direction side = WorldUtil.getDirectionByPistonRotation(this.level.getBlockState(this.worldPosition));
|
|
|
|
BlockPos breakCoords = this.worldPosition.relative(side);
|
|
|
|
BlockState stateToBreak = this.level.getBlockState(breakCoords);
|
2018-08-10 05:04:07 +02:00
|
|
|
Block blockToBreak = stateToBreak.getBlock();
|
|
|
|
|
2021-08-22 17:09:06 +02:00
|
|
|
if (!this.isPlacer && blockToBreak != Blocks.AIR && !(blockToBreak instanceof IFluidBlock) && stateToBreak.getDestroySpeed(this.level, breakCoords) >= 0.0F) {
|
|
|
|
List<ItemStack> drops = Block.getDrops(stateToBreak, (ServerWorld) this.level, breakCoords, this.level.getBlockEntity(breakCoords));
|
2022-04-04 02:12:40 +02:00
|
|
|
FakePlayer fake = FakePlayerFactory.getMinecraft((ServerWorld) this.level);
|
|
|
|
if (fake.connection == null) {
|
|
|
|
fake.connection = new NetHandlerSpaghettiServer(fake);
|
|
|
|
}
|
|
|
|
if (stateToBreak.canHarvestBlock(this.level, breakCoords, fake)) { //TODO might double check this is right mikey
|
2019-02-27 19:53:05 +01:00
|
|
|
if (StackUtil.canAddAll(this.inv, drops, false)) {
|
2021-08-22 17:09:06 +02:00
|
|
|
this.level.destroyBlock(breakCoords, false);
|
2019-02-27 19:53:05 +01:00
|
|
|
StackUtil.addAll(this.inv, drops, false);
|
2021-08-22 17:09:06 +02:00
|
|
|
this.setChanged();
|
2018-08-10 05:04:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (this.isPlacer) {
|
2019-02-27 19:53:05 +01:00
|
|
|
int slot = StackUtil.findFirstFilled(this.inv);
|
2021-02-26 22:15:48 +01:00
|
|
|
if (slot == -1) {
|
|
|
|
return;
|
|
|
|
}
|
2021-08-22 17:09:06 +02:00
|
|
|
this.inv.setStackInSlot(slot, WorldUtil.useItemAtSide(side, this.level, this.worldPosition, this.inv.getStackInSlot(slot)));
|
2018-08-10 05:04:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isRedstoneToggle() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void activateOnPulse() {
|
|
|
|
this.doWork();
|
|
|
|
}
|
2015-12-17 18:47:46 +01:00
|
|
|
|
2022-04-03 22:18:46 +02:00
|
|
|
@Nonnull
|
2021-02-28 12:33:01 +01:00
|
|
|
@Override
|
|
|
|
public ITextComponent getDisplayName() {
|
2022-04-03 22:18:46 +02:00
|
|
|
return new TranslationTextComponent(isPlacer ? "container.actuallyadditions.placer" : "container.actuallyadditions.breaker");
|
2021-02-28 12:33:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public Container createMenu(int windowId, PlayerInventory playerInventory, PlayerEntity playerEntity) {
|
|
|
|
return new ContainerBreaker(windowId, playerInventory, this);
|
|
|
|
}
|
2015-04-19 01:50:02 +02:00
|
|
|
}
|