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

194 lines
6.6 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
2016-05-16 22:52:27 +02:00
* This file ("TileEntityPhantomface.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
*
2016-05-16 22:54:42 +02:00
* © 2015-2016 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-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;
import de.ellpeck.actuallyadditions.mod.network.PacketParticle;
2016-01-08 13:31:58 +01:00
import de.ellpeck.actuallyadditions.mod.util.PosUtil;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.util.Util;
import net.minecraft.block.Block;
2015-05-20 22:39:43 +02:00
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
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-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
public 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;
2015-12-19 10:30:39 +01:00
public static final float[] COLORS = new float[]{93F/255F, 43F/255F, 181F/255F};
2016-01-08 13:31:58 +01:00
public BlockPos boundPosition;
public BlockPhantom.Type type;
public int range;
private int rangeBefore;
2016-01-08 13:31:58 +01:00
private BlockPos boundPosBefore;
private Block boundBlockBefore;
2015-05-27 21:57:53 +02:00
public TileEntityPhantomface(String name){
super(0, name);
2015-05-20 22:39:43 +02:00
}
2016-02-01 20:39:11 +01:00
public static int upgradeRange(int defaultRange, World world, BlockPos pos){
int newRange = defaultRange;
for(int i = 0; i < 3; i++){
Block block = PosUtil.getBlock(PosUtil.offset(pos, 0, 1+i, 0), world);
if(block == InitBlocks.blockPhantomBooster){
newRange = newRange*2;
}
else{
break;
}
}
return newRange;
}
@Override
public void writeSyncableNBT(NBTTagCompound compound, boolean sync){
super.writeSyncableNBT(compound, sync);
compound.setInteger("Range", this.range);
if(this.boundPosition != null){
2016-05-02 17:46:53 +02:00
compound.setInteger("XCoordOfTileStored", this.boundPosition.getX());
compound.setInteger("YCoordOfTileStored", this.boundPosition.getY());
compound.setInteger("ZCoordOfTileStored", this.boundPosition.getZ());
}
}
@Override
public void readSyncableNBT(NBTTagCompound compound, boolean sync){
super.readSyncableNBT(compound, sync);
int x = compound.getInteger("XCoordOfTileStored");
int y = compound.getInteger("YCoordOfTileStored");
int z = compound.getInteger("ZCoordOfTileStored");
this.range = compound.getInteger("Range");
if(!(x == 0 && y == 0 && z == 0)){
this.boundPosition = new BlockPos(x, y, z);
this.markDirty();
}
}
2015-05-27 21:57:53 +02:00
@Override
public void updateEntity(){
super.updateEntity();
2016-05-02 17:46:53 +02:00
if(!this.worldObj.isRemote){
this.range = upgradeRange(RANGE, this.worldObj, this.getPos());
2015-05-27 21:57:53 +02:00
if(!this.hasBoundPosition()){
this.boundPosition = null;
2015-05-27 21:57:53 +02:00
}
if(this.doesNeedUpdateSend()){
this.onUpdateSent();
}
2015-05-27 21:57:53 +02:00
}
2015-10-26 22:47:26 +01: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
}
protected boolean doesNeedUpdateSend(){
return this.boundPosition != this.boundPosBefore || (this.boundPosition != null && PosUtil.getBlock(this.boundPosition, this.worldObj) != this.boundBlockBefore) || this.rangeBefore != this.range;
}
protected void onUpdateSent(){
this.rangeBefore = this.range;
this.boundPosBefore = this.boundPosition;
this.boundBlockBefore = this.boundPosition == null ? null : PosUtil.getBlock(this.boundPosition, this.worldObj);
this.worldObj.notifyNeighborsOfStateChange(this.pos, PosUtil.getBlock(this.pos, this.worldObj));
this.sendUpdate();
this.markDirty();
}
2016-02-01 17:49:55 +01:00
@Override
public boolean isItemValidForSlot(int slot, ItemStack stack){
2016-02-01 17:49:55 +01:00
return false;
}
@Override
public boolean hasBoundPosition(){
if(this.boundPosition != null){
2016-05-02 17:46:53 +02:00
if(this.worldObj.getTileEntity(this.boundPosition) instanceof IPhantomTile || (this.getPos().getX() == this.boundPosition.getX() && this.getPos().getY() == this.boundPosition.getY() && this.getPos().getZ() == this.boundPosition.getZ())){
this.boundPosition = null;
return false;
}
return true;
}
return false;
}
2015-10-28 14:46:04 +01:00
@SideOnly(Side.CLIENT)
public void renderParticles(){
2015-11-22 18:58:23 +01:00
if(Util.RANDOM.nextInt(2) == 0){
double d1 = (double)((float)this.boundPosition.getY()+Util.RANDOM.nextFloat());
int i1 = Util.RANDOM.nextInt(2)*2-1;
int j1 = Util.RANDOM.nextInt(2)*2-1;
double d4 = ((double)Util.RANDOM.nextFloat()-0.5D)*0.125D;
2015-10-28 14:46:04 +01:00
double d2 = (double)this.boundPosition.getZ()+0.5D+0.25D*(double)j1;
2015-11-22 18:58:23 +01:00
double d5 = (double)(Util.RANDOM.nextFloat()*1.0F*(float)j1);
2015-10-28 14:46:04 +01:00
double d0 = (double)this.boundPosition.getX()+0.5D+0.25D*(double)i1;
2015-11-22 18:58:23 +01:00
double d3 = (double)(Util.RANDOM.nextFloat()*1.0F*(float)i1);
2016-05-02 17:46:53 +02:00
this.worldObj.spawnParticle(EnumParticleTypes.PORTAL, d0, d1, d2, d3, d4, d5);
2015-10-28 14:46:04 +01:00
}
if(this.ticksElapsed%80 == 0){
2016-05-02 17:46:53 +02:00
PacketParticle.renderParticlesFromAToB(this.getPos().getX(), this.getPos().getY(), this.getPos().getZ(), this.boundPosition.getX(), this.boundPosition.getY(), this.boundPosition.getZ(), 2, 0.35F, COLORS, 3);
}
2015-10-28 14:46:04 +01:00
}
@Override
public boolean isBoundThingInRange(){
2016-01-08 13:31:58 +01:00
return this.hasBoundPosition() && PosUtil.toVec(this.boundPosition).distanceTo(PosUtil.toVec(this.getPos())) <= this.range;
}
@Override
2016-01-08 13:31:58 +01:00
public BlockPos getBoundPosition(){
return this.boundPosition;
}
2015-10-03 10:16:18 +02:00
@Override
2016-01-08 13:31:58 +01:00
public void setBoundPosition(BlockPos pos){
this.boundPosition = pos == null ? null : PosUtil.copyPos(pos);
2015-10-03 10:16:18 +02:00
}
@Override
public int getGuiID(){
return -1;
}
@Override
public int getRange(){
return this.range;
}
2015-10-03 10:19:40 +02:00
@Override
public boolean canInsertItem(int slot, ItemStack stack, EnumFacing side){
2015-10-03 10:19:40 +02:00
return false;
}
@Override
public boolean canExtractItem(int slot, ItemStack stack, EnumFacing side){
2015-10-03 10:19:40 +02:00
return false;
}
@Override
public boolean hasInvWrapperCapabilities(){
return false;
}
2015-05-20 22:39:43 +02:00
}