2016-01-05 04:47:35 +01:00
|
|
|
package de.ellpeck.actuallyadditions.mod.tile;
|
2015-05-20 22:39:43 +02:00
|
|
|
|
2016-01-05 04:47:35 +01:00
|
|
|
import de.ellpeck.actuallyadditions.api.tile.IPhantomTile;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.blocks.BlockPhantom;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.blocks.InitBlocks;
|
2015-06-12 19:12:06 +02:00
|
|
|
import net.minecraft.block.Block;
|
2017-05-20 19:55:59 +02:00
|
|
|
import net.minecraft.block.state.IBlockState;
|
2015-05-20 22:39:43 +02:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2016-07-05 08:26:11 +02:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2016-01-07 19:47:53 +01:00
|
|
|
import net.minecraft.util.EnumFacing;
|
|
|
|
import net.minecraft.util.EnumParticleTypes;
|
2016-03-18 23:47:22 +01:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2015-05-20 22:39:43 +02:00
|
|
|
import net.minecraft.world.World;
|
2016-06-05 02:16:52 +02:00
|
|
|
import net.minecraftforge.common.capabilities.Capability;
|
2016-01-07 18:20:59 +01:00
|
|
|
import net.minecraftforge.fml.relauncher.Side;
|
|
|
|
import net.minecraftforge.fml.relauncher.SideOnly;
|
2015-05-20 22:39:43 +02:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public abstract class TileEntityPhantomface extends TileEntityInventoryBase implements IPhantomTile {
|
2015-05-20 22:39:43 +02:00
|
|
|
|
2015-12-01 19:48:09 +01:00
|
|
|
public static final int RANGE = 16;
|
2016-01-08 13:31:58 +01:00
|
|
|
public BlockPos boundPosition;
|
2015-08-10 22:55:34 +02:00
|
|
|
public BlockPhantom.Type type;
|
2015-06-12 19:12:06 +02:00
|
|
|
public int range;
|
2015-07-07 16:22:37 +02:00
|
|
|
private int rangeBefore;
|
2016-01-08 13:31:58 +01:00
|
|
|
private BlockPos boundPosBefore;
|
2015-07-07 16:22:37 +02:00
|
|
|
private Block boundBlockBefore;
|
2017-05-20 19:55:59 +02:00
|
|
|
private int lastStrength;
|
2015-07-07 16:22:37 +02:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public TileEntityPhantomface(String name) {
|
2015-05-27 21:57:53 +02:00
|
|
|
super(0, name);
|
2015-05-20 22:39:43 +02:00
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public static int upgradeRange(int defaultRange, World world, BlockPos pos) {
|
2016-02-01 20:39:11 +01:00
|
|
|
int newRange = defaultRange;
|
2019-05-02 09:10:29 +02:00
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
Block block = world.getBlockState(pos.up(1 + i)).getBlock();
|
|
|
|
if (block == InitBlocks.blockPhantomBooster) {
|
|
|
|
newRange = newRange * 2;
|
|
|
|
} else {
|
2016-02-01 20:39:11 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newRange;
|
|
|
|
}
|
|
|
|
|
2016-02-01 20:32:49 +01:00
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public void writeSyncableNBT(NBTTagCompound compound, NBTType type) {
|
2016-07-02 15:01:46 +02:00
|
|
|
super.writeSyncableNBT(compound, type);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (type != NBTType.SAVE_BLOCK) {
|
2016-07-02 15:01:46 +02:00
|
|
|
compound.setInteger("Range", this.range);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (this.boundPosition != null) {
|
2017-06-29 18:30:02 +02:00
|
|
|
compound.setInteger("xOfTileStored", this.boundPosition.getX());
|
|
|
|
compound.setInteger("yOfTileStored", this.boundPosition.getY());
|
|
|
|
compound.setInteger("zOfTileStored", this.boundPosition.getZ());
|
2016-07-02 15:01:46 +02:00
|
|
|
}
|
2016-02-01 20:32:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public void readSyncableNBT(NBTTagCompound compound, NBTType type) {
|
2016-07-02 15:01:46 +02:00
|
|
|
super.readSyncableNBT(compound, type);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (type != NBTType.SAVE_BLOCK) {
|
2017-06-29 18:30:02 +02:00
|
|
|
int x = compound.getInteger("xOfTileStored");
|
|
|
|
int y = compound.getInteger("yOfTileStored");
|
|
|
|
int z = compound.getInteger("zOfTileStored");
|
2016-07-02 15:01:46 +02:00
|
|
|
this.range = compound.getInteger("Range");
|
2019-05-02 09:10:29 +02:00
|
|
|
if (!(x == 0 && y == 0 && z == 0)) {
|
2016-07-02 15:01:46 +02:00
|
|
|
this.boundPosition = new BlockPos(x, y, z);
|
|
|
|
this.markDirty();
|
|
|
|
}
|
2016-02-01 20:32:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-27 21:57:53 +02:00
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public void updateEntity() {
|
2015-11-18 23:11:24 +01:00
|
|
|
super.updateEntity();
|
2019-05-02 09:10:29 +02:00
|
|
|
if (!this.world.isRemote) {
|
2016-11-26 21:32:27 +01:00
|
|
|
this.range = upgradeRange(RANGE, this.world, this.getPos());
|
2015-05-27 21:57:53 +02:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (!this.hasBoundPosition()) {
|
2015-06-12 19:12:06 +02:00
|
|
|
this.boundPosition = null;
|
2015-05-27 21:57:53 +02:00
|
|
|
}
|
2015-07-07 16:22:37 +02:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (this.doesNeedUpdateSend()) {
|
2016-05-05 17:50:18 +02:00
|
|
|
this.onUpdateSent();
|
2015-07-07 16:22:37 +02:00
|
|
|
}
|
2017-05-20 19:55:59 +02:00
|
|
|
|
|
|
|
int strength = this.getComparatorStrength();
|
2019-05-02 09:10:29 +02:00
|
|
|
if (this.lastStrength != strength) {
|
2017-05-20 19:55:59 +02:00
|
|
|
this.lastStrength = strength;
|
|
|
|
|
|
|
|
this.markDirty();
|
|
|
|
}
|
2019-05-02 09:10:29 +02:00
|
|
|
} else {
|
|
|
|
if (this.boundPosition != null) {
|
2015-10-27 23:09:23 +01:00
|
|
|
this.renderParticles();
|
2015-10-26 22:47:26 +01:00
|
|
|
}
|
|
|
|
}
|
2015-05-27 21:57:53 +02:00
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
protected boolean doesNeedUpdateSend() {
|
2019-02-27 19:53:05 +01:00
|
|
|
return this.boundPosition != this.boundPosBefore || this.boundPosition != null && this.world.getBlockState(this.boundPosition).getBlock() != this.boundBlockBefore || this.rangeBefore != this.range;
|
2016-05-05 17:50:18 +02:00
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
protected void onUpdateSent() {
|
2016-05-05 17:50:18 +02:00
|
|
|
this.rangeBefore = this.range;
|
|
|
|
this.boundPosBefore = this.boundPosition;
|
2016-11-26 21:32:27 +01:00
|
|
|
this.boundBlockBefore = this.boundPosition == null ? null : this.world.getBlockState(this.boundPosition).getBlock();
|
2016-05-05 17:50:18 +02:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (this.boundPosition != null) {
|
2016-11-26 21:32:27 +01:00
|
|
|
this.world.notifyNeighborsOfStateChange(this.pos, this.world.getBlockState(this.boundPosition).getBlock(), false);
|
2016-07-06 21:56:15 +02:00
|
|
|
}
|
2016-05-05 17:50:18 +02:00
|
|
|
|
|
|
|
this.sendUpdate();
|
|
|
|
this.markDirty();
|
|
|
|
}
|
|
|
|
|
2015-08-10 10:37:44 +02:00
|
|
|
@Override
|
2019-05-02 09:10:29 +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()) {
|
2015-08-10 10:37:44 +02:00
|
|
|
this.boundPosition = null;
|
|
|
|
return false;
|
|
|
|
}
|
2015-12-23 01:43:49 +01:00
|
|
|
return true;
|
2015-08-10 10:37:44 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-10-28 14:46:04 +01:00
|
|
|
@SideOnly(Side.CLIENT)
|
2019-05-02 09:10:29 +02:00
|
|
|
public void renderParticles() {
|
|
|
|
if (this.world.rand.nextInt(2) == 0) {
|
|
|
|
double d1 = 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 = (this.world.rand.nextFloat() - 0.5D) * 0.125D;
|
|
|
|
double d2 = this.boundPosition.getZ() + 0.5D + 0.25D * j1;
|
|
|
|
double d5 = this.world.rand.nextFloat() * 1.0F * j1;
|
|
|
|
double d0 = this.boundPosition.getX() + 0.5D + 0.25D * i1;
|
|
|
|
double d3 = this.world.rand.nextFloat() * 1.0F * i1;
|
2016-11-26 21:32:27 +01:00
|
|
|
this.world.spawnParticle(EnumParticleTypes.PORTAL, d0, d1, d2, d3, d4, d5);
|
2015-10-28 14:46:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-10 10:37:44 +02:00
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public boolean isBoundThingInRange() {
|
|
|
|
return this.hasBoundPosition() && this.boundPosition.distanceSq(this.getPos()) <= this.range * this.range;
|
2015-08-10 10:37:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public BlockPos getBoundPosition() {
|
2015-08-10 10:37:44 +02:00
|
|
|
return this.boundPosition;
|
|
|
|
}
|
|
|
|
|
2015-10-03 10:16:18 +02:00
|
|
|
@Override
|
2019-05-02 09:10:29 +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
|
|
|
}
|
|
|
|
|
2015-08-10 10:37:44 +02:00
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public int getGuiID() {
|
2015-08-10 10:37:44 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public int getRange() {
|
2015-08-10 10:37:44 +02:00
|
|
|
return this.range;
|
|
|
|
}
|
|
|
|
|
2016-12-28 03:12:17 +01:00
|
|
|
protected abstract boolean isCapabilitySupported(Capability<?> capability);
|
|
|
|
|
2016-06-05 02:16:52 +02:00
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
|
|
|
|
if (this.isBoundThingInRange() && this.isCapabilitySupported(capability)) {
|
2016-11-26 21:32:27 +01:00
|
|
|
TileEntity tile = this.world.getTileEntity(this.getBoundPosition());
|
2019-05-02 09:10:29 +02:00
|
|
|
if (tile != null) { return tile.hasCapability(capability, facing); }
|
2016-07-05 08:26:11 +02:00
|
|
|
}
|
|
|
|
return super.hasCapability(capability, facing);
|
2016-06-05 02:16:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
|
|
|
|
if (this.isBoundThingInRange() && this.isCapabilitySupported(capability)) {
|
2016-11-26 21:32:27 +01:00
|
|
|
TileEntity tile = this.world.getTileEntity(this.getBoundPosition());
|
2019-05-02 09:10:29 +02:00
|
|
|
if (tile != null) { return tile.getCapability(capability, facing); }
|
2016-07-05 08:26:11 +02:00
|
|
|
}
|
|
|
|
return super.getCapability(capability, facing);
|
2016-06-05 02:16:52 +02:00
|
|
|
}
|
2017-05-20 19:55:59 +02:00
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public int getComparatorStrength() {
|
|
|
|
if (this.isBoundThingInRange()) {
|
2017-05-20 19:55:59 +02:00
|
|
|
BlockPos pos = this.getBoundPosition();
|
|
|
|
IBlockState state = this.world.getBlockState(pos);
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (state.hasComparatorInputOverride()) { return state.getComparatorInputOverride(this.world, pos); }
|
2017-05-20 19:55:59 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2015-05-20 22:39:43 +02:00
|
|
|
}
|