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

223 lines
7.9 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
2016-05-16 22:52:27 +02:00
* This file ("TileEntityPhantomPlacer.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-05-25 17:00:54 +02:00
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.api.tile.IPhantomTile;
import de.ellpeck.actuallyadditions.mod.inventory.GuiHandler;
2016-07-06 20:05:56 +02:00
import de.ellpeck.actuallyadditions.mod.network.gui.IButtonReactor;
2018-08-10 05:04:07 +02:00
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor;
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IRemover;
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
2015-05-25 17:00:54 +02:00
import net.minecraft.block.Block;
2016-07-06 20:05:56 +02:00
import net.minecraft.entity.player.EntityPlayer;
2015-05-25 17:00:54 +02:00
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.NonNullList;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.math.BlockPos;
2015-05-25 17:00:54 +02:00
2018-08-10 05:04:07 +02:00
public class TileEntityPhantomPlacer extends TileEntityInventoryBase implements IPhantomTile, IButtonReactor {
2015-05-25 17:00:54 +02:00
2015-12-01 19:48:09 +01:00
public static final int RANGE = 3;
2016-01-08 13:31:58 +01:00
public BlockPos boundPosition;
2015-05-25 17:00:54 +02:00
public int currentTime;
public int range;
2015-05-25 17:00:54 +02:00
public boolean isBreaker;
2016-07-06 20:05:56 +02:00
public int side;
2016-07-07 17:59:45 +02:00
private int oldRange;
2015-05-25 17:00:54 +02:00
2018-08-10 05:04:07 +02:00
public TileEntityPhantomPlacer(int slots, String name) {
2015-05-25 17:00:54 +02:00
super(slots, name);
}
2018-08-10 05:04:07 +02:00
public TileEntityPhantomPlacer() {
2015-05-25 17:00:54 +02:00
super(9, "phantomPlacer");
this.isBreaker = false;
}
2016-02-01 17:49:55 +01:00
@Override
2018-08-10 05:04:07 +02:00
public void writeSyncableNBT(NBTTagCompound compound, NBTType type) {
super.writeSyncableNBT(compound, type);
2018-08-10 05:04:07 +02:00
if (type != NBTType.SAVE_BLOCK) {
compound.setInteger("Range", this.range);
2018-08-10 05:04:07 +02:00
if (this.boundPosition != null) {
compound.setInteger("xOfTileStored", this.boundPosition.getX());
compound.setInteger("yOfTileStored", this.boundPosition.getY());
compound.setInteger("zOfTileStored", this.boundPosition.getZ());
}
2018-08-10 05:04:07 +02:00
if (!this.isBreaker) {
2016-07-06 20:05:56 +02:00
compound.setInteger("Side", this.side);
}
2016-02-01 17:49:55 +01:00
}
}
@Override
2018-08-10 05:04:07 +02:00
public void readSyncableNBT(NBTTagCompound compound, NBTType type) {
super.readSyncableNBT(compound, type);
2018-08-10 05:04:07 +02:00
if (type != NBTType.SAVE_BLOCK) {
int x = compound.getInteger("xOfTileStored");
int y = compound.getInteger("yOfTileStored");
int z = compound.getInteger("zOfTileStored");
this.range = compound.getInteger("Range");
2018-08-10 05:04:07 +02:00
if (!(x == 0 && y == 0 && z == 0)) {
this.boundPosition = new BlockPos(x, y, z);
this.markDirty();
}
2018-08-10 05:04:07 +02:00
if (!this.isBreaker) {
2016-07-06 20:05:56 +02:00
this.side = compound.getInteger("Side");
}
2016-02-01 17:49:55 +01:00
}
}
2015-05-25 17:00:54 +02:00
@Override
2018-08-10 05:04:07 +02:00
public void updateEntity() {
super.updateEntity();
2018-08-10 05:04:07 +02:00
if (!this.world.isRemote) {
2016-11-26 21:32:27 +01:00
this.range = TileEntityPhantomface.upgradeRange(RANGE, this.world, this.pos);
2015-05-25 17:00:54 +02:00
2018-08-10 05:04:07 +02:00
if (!this.hasBoundPosition()) {
2015-05-25 17:00:54 +02:00
this.boundPosition = null;
}
2018-08-10 05:04:07 +02:00
if (this.isBoundThingInRange()) {
if (!this.isRedstonePowered && !this.isPulseMode) {
if (this.currentTime > 0) {
2015-05-25 17:00:54 +02:00
this.currentTime--;
2018-08-10 05:04:07 +02:00
if (this.currentTime <= 0) {
this.doWork();
2015-05-25 17:00:54 +02:00
}
2018-08-10 05:04:07 +02:00
} else {
this.currentTime = 30;
2015-10-02 16:48:01 +02:00
}
2015-05-25 17:00:54 +02:00
}
}
2018-08-10 05:04:07 +02:00
if (this.oldRange != this.range) {
this.oldRange = this.range;
this.sendUpdate();
}
2018-08-10 05:04:07 +02:00
} else {
if (this.boundPosition != null) {
2015-12-16 20:10:27 +01:00
this.renderParticles();
}
}
}
2015-12-19 10:30:39 +01:00
@Override
2018-08-10 05:04:07 +02:00
public boolean hasBoundPosition() {
if (this.boundPosition != null) {
if (this.world.getTileEntity(this.boundPosition) instanceof IPhantomTile || (this.getPos().getX() == this.boundPosition.getX() && this.getPos().getY() == this.boundPosition.getY() && this.getPos().getZ() == this.boundPosition.getZ() && this.world.provider.getDimension() == this.world.provider.getDimension())) {
2015-12-19 10:30:39 +01:00
this.boundPosition = null;
return false;
}
2016-11-26 21:32:27 +01:00
return this.world.provider.getDimension() == this.world.provider.getDimension();
2015-12-19 10:30:39 +01:00
}
return false;
}
2018-08-10 05:04:07 +02:00
private void doWork() {
if (this.isBoundThingInRange()) {
if (this.isBreaker) {
2016-11-26 21:32:27 +01:00
Block blockToBreak = this.world.getBlockState(this.boundPosition).getBlock();
2018-08-10 05:04:07 +02:00
if (blockToBreak != null && this.world.getBlockState(this.boundPosition).getBlockHardness(this.world, this.boundPosition) > -1.0F) {
NonNullList<ItemStack> drops = NonNullList.create();
blockToBreak.getDrops(drops, world, pos, this.world.getBlockState(this.boundPosition), 0);
2018-08-10 05:04:07 +02:00
if (StackUtil.canAddAll(this.inv, drops, false)) {
2016-11-26 21:32:27 +01:00
this.world.playEvent(2001, this.boundPosition, Block.getStateId(this.world.getBlockState(this.boundPosition)));
this.world.setBlockToAir(this.boundPosition);
StackUtil.addAll(this.inv, drops, false);
this.markDirty();
2016-01-23 11:02:35 +01:00
}
2015-12-19 10:30:39 +01:00
}
2018-08-10 05:04:07 +02:00
} else {
int theSlot = StackUtil.findFirstFilled(this.inv);
2018-08-10 05:04:07 +02:00
if (theSlot == -1) return;
inv.setStackInSlot(theSlot, WorldUtil.useItemAtSide(WorldUtil.getDirectionBySidesInOrder(this.side), this.world, this.boundPosition, inv.getStackInSlot(theSlot)));
2015-12-19 10:30:39 +01:00
}
}
}
2018-08-10 05:04:07 +02:00
public void renderParticles() {
if (this.world.rand.nextInt(2) == 0) {
double d1 = (double) ((float) this.boundPosition.getY() + this.world.rand.nextFloat());
int i1 = this.world.rand.nextInt(2) * 2 - 1;
int j1 = this.world.rand.nextInt(2) * 2 - 1;
double d4 = ((double) this.world.rand.nextFloat() - 0.5D) * 0.125D;
double d2 = (double) this.boundPosition.getZ() + 0.5D + 0.25D * (double) j1;
double d5 = (double) (this.world.rand.nextFloat() * 1.0F * (float) j1);
double d0 = (double) this.boundPosition.getX() + 0.5D + 0.25D * (double) i1;
double d3 = (double) (this.world.rand.nextFloat() * 1.0F * (float) i1);
2016-11-26 21:32:27 +01:00
this.world.spawnParticle(EnumParticleTypes.PORTAL, d0, d1, d2, d3, d4, d5);
2015-12-16 20:10:27 +01:00
}
2015-05-25 17:00:54 +02:00
}
@Override
2018-08-10 05:04:07 +02:00
public boolean isBoundThingInRange() {
return this.hasBoundPosition() && this.boundPosition.distanceSq(this.pos) <= this.range * this.range;
2015-05-25 17:00:54 +02:00
}
@Override
2018-08-10 05:04:07 +02:00
public BlockPos getBoundPosition() {
return this.boundPosition;
}
2015-10-03 10:16:18 +02:00
@Override
2018-08-10 05:04:07 +02:00
public void setBoundPosition(BlockPos pos) {
2016-07-04 20:15:41 +02:00
this.boundPosition = pos;
2015-10-03 10:16:18 +02:00
}
@Override
2018-08-10 05:04:07 +02:00
public int getGuiID() {
return GuiHandler.GuiTypes.PHANTOM_PLACER.ordinal();
}
@Override
2018-08-10 05:04:07 +02:00
public int getRange() {
return this.range;
2015-05-25 17:00:54 +02:00
}
2015-10-18 15:31:01 +02:00
@Override
2018-08-10 05:04:07 +02:00
public IAcceptor getAcceptor() {
return ItemStackHandlerAA.ACCEPT_TRUE;
2015-05-25 17:00:54 +02:00
}
@Override
2018-08-10 05:04:07 +02:00
public IRemover getRemover() {
return ItemStackHandlerAA.REMOVE_FALSE;
2015-05-25 17:00:54 +02:00
}
2015-10-03 10:16:18 +02:00
@Override
2018-08-10 05:04:07 +02:00
public boolean isRedstoneToggle() {
return true;
}
@Override
2018-08-10 05:04:07 +02:00
public void activateOnPulse() {
this.doWork();
}
2016-07-06 20:05:56 +02:00
@Override
2018-08-10 05:04:07 +02:00
public void onButtonPressed(int buttonID, EntityPlayer player) {
if (this.side + 1 >= EnumFacing.values().length) {
2016-07-06 20:05:56 +02:00
this.side = 0;
2018-08-10 05:04:07 +02:00
} else {
2016-07-06 20:05:56 +02:00
this.side++;
}
this.sendUpdate();
}
2015-05-25 17:00:54 +02:00
}