Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package thut.api.entity.blockentity;
- import com.google.common.collect.Lists;
- import io.netty.buffer.ByteBuf;
- import java.util.Collection;
- import java.util.List;
- import java.util.Random;
- import java.util.UUID;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.annotation.Nullable;
- import net.minecraft.block.Block;
- import net.minecraft.block.state.IBlockState;
- import net.minecraft.entity.Entity;
- import net.minecraft.entity.EntityLivingBase;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.entity.player.EntityPlayerMP;
- import net.minecraft.inventory.EntityEquipmentSlot;
- import net.minecraft.item.Item;
- import net.minecraft.item.ItemStack;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.network.PacketBuffer;
- import net.minecraft.potion.PotionEffect;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.util.DamageSource;
- import net.minecraft.util.EnumActionResult;
- import net.minecraft.util.EnumHand;
- import net.minecraft.util.EnumHandSide;
- import net.minecraft.util.math.AxisAlignedBB;
- import net.minecraft.util.math.BlockPos;
- import net.minecraft.util.math.Vec3d;
- import net.minecraft.world.World;
- import net.minecraftforge.common.ForgeHooks;
- import net.minecraftforge.fml.common.registry.IEntityAdditionalSpawnData;
- import net.minecraftforge.fml.relauncher.Side;
- import net.minecraftforge.fml.relauncher.SideOnly;
- import thut.core.common.ThutCore;
- public abstract class BlockEntityBase
- extends EntityLivingBase
- implements IEntityAdditionalSpawnData, IBlockEntity
- {
- public static int ACCELERATIONTICKS = 20;
- public BlockPos boundMin = BlockPos.ORIGIN;
- public BlockPos boundMax = BlockPos.ORIGIN;
- private BlockEntityWorld fake_world;
- private boolean shouldRevert = true;
- public double speedUp = 0.5D;
- public double speedDown = -0.5D;
- public double speedHoriz = 0.5D;
- public double acceleration = 0.05D;
- public boolean toMoveY = false;
- public boolean toMoveX = false;
- public boolean toMoveZ = false;
- public boolean hasPassenger = false;
- int n = 0;
- boolean first = true;
- Random r = new Random();
- public UUID owner;
- public List<AxisAlignedBB> blockBoxes = Lists.newArrayList();
- public IBlockState[][][] blocks = (IBlockState[][][])null;
- public TileEntity[][][] tiles = (TileEntity[][][])null;
- BlockEntityUpdater collider;
- BlockEntityInteractHandler interacter;
- public BlockEntityBase(World par1World)
- {
- super(par1World);
- this.ignoreFrustumCheck = true;
- this.hurtResistantTime = 0;
- this.isImmuneToFire = true;
- }
- public BlockEntityWorld getFakeWorld()
- {
- if (this.fake_world == null) {
- this.fake_world = new BlockEntityWorld(this, this.world);
- }
- return this.fake_world;
- }
- public BlockEntityBase(World world, double x, double y, double z)
- {
- this(world);
- setPosition(x, y, z);
- this.r.setSeed(100L);
- }
- public boolean attackEntityFrom(DamageSource source, float amount)
- {
- return false;
- }
- public void knockBack(Entity entityIn, float strenght, double xRatio, double zRatio) {}
- protected double getSpeed(double pos, double destPos, double speed, double speedPos, double speedNeg)
- {
- if (!getEntityWorld().isAreaLoaded(getPosition(), 8)) {
- return 0.0D;
- }
- double ds = speed;
- double dp = destPos - pos;
- if (dp > 0.0D)
- {
- boolean tooFast = pos + ds * (ACCELERATIONTICKS + 1) > destPos;
- if (!tooFast) {
- ds = Math.min(speedPos, ds + this.acceleration * speedPos);
- } else {
- while ((ds >= 0.0D) && (tooFast))
- {
- ds -= this.acceleration * speedPos / 10.0D;
- tooFast = pos + ds * (ACCELERATIONTICKS + 1) > destPos;
- }
- }
- return ds;
- }
- if (dp < 0.0D)
- {
- speedNeg = Math.abs(speedNeg);
- boolean tooFast = pos + ds * (ACCELERATIONTICKS + 1) < destPos;
- if (!tooFast) {
- ds = Math.max(-speedNeg, ds - this.acceleration * speedNeg);
- } else {
- while ((ds <= 0.0D) && (tooFast))
- {
- ds += this.acceleration * speedNeg / 10.0D;
- tooFast = pos + ds * (ACCELERATIONTICKS + 1) < destPos;
- }
- }
- return ds;
- }
- return 0.0D;
- }
- protected abstract void accelerate();
- public void applyEntityCollision(Entity entity)
- {
- if (this.collider == null)
- {
- this.collider = new BlockEntityUpdater(this);
- this.collider.onSetPosition();
- }
- try
- {
- this.collider.applyEntityCollision(entity);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- public boolean canBeCollidedWith()
- {
- return !this.isDead;
- }
- public boolean canBePushed()
- {
- return true;
- }
- public boolean canRenderOnFire()
- {
- return false;
- }
- public void checkCollision()
- {
- int xMin = this.boundMin.getX();
- int zMin = this.boundMin.getZ();
- int xMax = this.boundMax.getX();
- int zMax = this.boundMax.getZ();
- List<?> list = this.world.getEntitiesWithinAABBExcludingEntity(this, new AxisAlignedBB(this.posX + (xMin - 1), this.posY, this.posZ + (zMin - 1), this.posX + xMax + 1.0D, this.posY + 64.0D, this.posZ + zMax + 1.0D));
- if ((list != null) && (!list.isEmpty()))
- {
- if ((list.size() == 1) && (getRecursivePassengers() != null) &&
- (!getRecursivePassengers().isEmpty())) {
- return;
- }
- for (int i = 0; i < list.size(); i++)
- {
- Entity entity = (Entity)list.get(i);
- applyEntityCollision(entity);
- if (((entity instanceof EntityPlayerMP)) &&
- (entity.getEntityBoundingBox().grow(2.0D).intersects(getEntityBoundingBox()))) {
- this.hasPassenger = true;
- }
- }
- }
- }
- protected abstract boolean checkAccelerationConditions();
- protected abstract void doMotion();
- public void resetPositionToBB()
- {
- BlockPos min = getMin();
- BlockPos max = getMax();
- float xDiff = (max.getX() - min.getX()) / 2.0F;
- float zDiff = (max.getZ() - min.getZ()) / 2.0F;
- AxisAlignedBB axisalignedbb = getEntityBoundingBox();
- if (xDiff % 1.0F != 0.0F) {
- this.posX = (axisalignedbb.minX + xDiff);
- } else {
- this.posX = ((axisalignedbb.minX + axisalignedbb.maxX) / 2.0D);
- }
- this.posY = axisalignedbb.minY;
- if (zDiff % 1.0F != 0.0F) {
- this.posZ = (axisalignedbb.minZ + zDiff);
- } else {
- this.posZ = ((axisalignedbb.minZ + axisalignedbb.maxZ) / 2.0D);
- }
- }
- protected void entityInit()
- {
- super.entityInit();
- }
- public AxisAlignedBB getBoundingBox()
- {
- return null;
- }
- public boolean getCanSpawnHere()
- {
- return false;
- }
- protected abstract BlockEntityInteractHandler createInteractHandler();
- public EnumActionResult applyPlayerInteraction(EntityPlayer player, Vec3d vec, EnumHand hand)
- {
- if (this.interacter == null) {
- this.interacter = createInteractHandler();
- }
- try
- {
- return this.interacter.applyPlayerInteraction(player, vec, player.getHeldItem(hand), hand);
- }
- catch (Exception e)
- {
- ThutCore.logger.log(Level.SEVERE, "Error handling interactions for " + this, e);
- }
- return super.applyPlayerInteraction(player, vec, hand);
- }
- public boolean processInitialInteract(EntityPlayer player, EnumHand hand)
- {
- if (this.interacter == null) {
- this.interacter = createInteractHandler();
- }
- return this.interacter.processInitialInteract(player, player.getHeldItem(hand), hand);
- }
- public boolean isPotionApplicable(PotionEffect par1PotionEffect)
- {
- return false;
- }
- protected abstract void preColliderTick();
- protected abstract void onGridAlign();
- public void onUpdate()
- {
- if (ForgeHooks.onLivingUpdate(this)) {
- return;
- }
- if (this.collider == null)
- {
- this.collider = new BlockEntityUpdater(this);
- this.collider.onSetPosition();
- }
- preColliderTick();
- this.prevPosY = this.posY;
- this.prevPosX = this.posX;
- this.prevPosZ = this.posZ;
- this.collider.onUpdate();
- accelerate();
- int dy = (int)(this.motionY * 16.0D);
- int dx = (int)(this.motionX * 16.0D);
- int dz = (int)(this.motionZ * 16.0D);
- if ((this.toMoveY) || (this.toMoveX) || (this.toMoveZ))
- {
- doMotion();
- }
- else if ((dx == dy) && (dy == dz) && (dz == 0) && (!this.world.isRemote))
- {
- BlockPos pos = getPosition();
- boolean update = (this.posX != pos.getX() + 0.5D) || (this.posY != Math.round(this.posY)) || (this.posZ != pos.getZ() + 0.5D);
- if (update) {
- onGridAlign();
- }
- }
- checkCollision();
- }
- public void readBlocks(NBTTagCompound nbt)
- {
- if (nbt.hasKey("Blocks"))
- {
- NBTTagCompound blockTag = nbt.getCompoundTag("Blocks");
- int sizeX = blockTag.getInteger("BlocksLengthX");
- int sizeZ = blockTag.getInteger("BlocksLengthZ");
- int sizeY = blockTag.getInteger("BlocksLengthY");
- if ((sizeX == 0) || (sizeZ == 0)) {
- sizeX = sizeZ = nbt.getInteger("BlocksLength");
- }
- if (sizeY == 0) {
- sizeY = 1;
- }
- int version = blockTag.getInteger("v");
- this.blocks = new IBlockState[sizeX][sizeY][sizeZ];
- this.tiles = new TileEntity[sizeX][sizeY][sizeZ];
- for (int i = 0; i < sizeX; i++) {
- for (int k = 0; k < sizeY; k++) {
- for (int j = 0; j < sizeZ; j++)
- {
- int n = -1;
- if (blockTag.hasKey("I" + i + "," + j)) {
- n = blockTag.getInteger("I" + i + "," + j);
- } else if (blockTag.hasKey("I" + i + "," + k + "," + j)) {
- n = blockTag.getInteger("I" + i + "," + k + "," + j);
- }
- if (n != -1)
- {
- IBlockState state;
- IBlockState state;
- if (version == 0)
- {
- Block b = Block.getBlockFromItem(Item.getItemById(n));
- int meta = blockTag.getInteger("M" + i + "," + k + "," + j);
- state = b.getStateFromMeta(meta);
- }
- else
- {
- Block b = Block.getBlockById(n);
- int meta = blockTag.getInteger("M" + i + "," + k + "," + j);
- state = b.getStateFromMeta(meta);
- }
- this.blocks[i][k][j] = state;
- if (blockTag.hasKey("T" + i + "," + k + "," + j)) {
- try
- {
- NBTTagCompound tag = blockTag.getCompoundTag("T" + i + "," + k + "," + j);
- this.tiles[i][k][j] = IBlockEntity.BlockEntityFormer.makeTile(tag);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- }
- }
- }
- }
- setBlocks(this.blocks);
- setTiles(this.tiles);
- }
- }
- public void readEntityFromNBT(NBTTagCompound nbt)
- {
- super.readEntityFromNBT(nbt);
- if (nbt.hasKey("bounds"))
- {
- NBTTagCompound bounds = nbt.getCompoundTag("bounds");
- this.boundMin = new BlockPos(bounds.getDouble("minx"), bounds.getDouble("miny"), bounds.getDouble("minz"));
- this.boundMax = new BlockPos(bounds.getDouble("maxx"), bounds.getDouble("maxy"), bounds.getDouble("maxz"));
- }
- readBlocks(nbt);
- }
- public void readSpawnData(ByteBuf data)
- {
- PacketBuffer buff = new PacketBuffer(data);
- NBTTagCompound tag = new NBTTagCompound();
- try
- {
- tag = buff.readCompoundTag();
- readEntityFromNBT(tag);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- public void setDead()
- {
- if ((!getEntityWorld().isRemote) && (!this.isDead) && (this.addedToChunk) && (this.shouldRevert)) {
- IBlockEntity.BlockEntityFormer.RevertEntity(this);
- }
- super.setDead();
- }
- public void setDropItemsWhenDead(boolean dropWhenDead)
- {
- this.shouldRevert = dropWhenDead;
- }
- public void setPosition(double x, double y, double z)
- {
- super.setPosition(x, y, z);
- if (this.collider != null) {
- this.collider.onSetPosition();
- }
- }
- public void writeBlocks(NBTTagCompound nbt)
- {
- if (this.blocks != null)
- {
- NBTTagCompound blocksTag = new NBTTagCompound();
- blocksTag.setInteger("BlocksLengthX", this.blocks.length);
- blocksTag.setInteger("BlocksLengthY", this.blocks[0].length);
- blocksTag.setInteger("BlocksLengthZ", this.blocks[0][0].length);
- blocksTag.setInteger("v", 1);
- int sizeX = this.blocks.length;
- int sizeY = this.blocks[0].length;
- int sizeZ = this.blocks[0][0].length;
- for (int i = 0; i < sizeX; i++) {
- for (int k = 0; k < sizeY; k++) {
- for (int j = 0; j < sizeZ; j++)
- {
- IBlockState b = this.blocks[i][k][j];
- if (b != null)
- {
- blocksTag.setInteger("I" + i + "," + k + "," + j, Block.getIdFromBlock(b.getBlock()));
- blocksTag.setInteger("M" + i + "," + k + "," + j, b.getBlock().getMetaFromState(b));
- try
- {
- if (this.tiles[i][k][j] != null)
- {
- NBTTagCompound tag = new NBTTagCompound();
- tag = this.tiles[i][k][j].writeToNBT(tag);
- blocksTag.setTag("T" + i + "," + k + "," + j, tag);
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- }
- }
- }
- nbt.setTag("Blocks", blocksTag);
- }
- }
- public void writeEntityToNBT(NBTTagCompound nbt)
- {
- super.writeEntityToNBT(nbt);
- NBTTagCompound vector = new NBTTagCompound();
- vector.setDouble("minx", this.boundMin.getX());
- vector.setDouble("miny", this.boundMin.getY());
- vector.setDouble("minz", this.boundMin.getZ());
- vector.setDouble("maxx", this.boundMax.getX());
- vector.setDouble("maxy", this.boundMax.getY());
- vector.setDouble("maxz", this.boundMax.getZ());
- nbt.setTag("bounds", vector);
- try
- {
- writeBlocks(nbt);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- public void writeSpawnData(ByteBuf data)
- {
- PacketBuffer buff = new PacketBuffer(data);
- NBTTagCompound tag = new NBTTagCompound();
- writeEntityToNBT(tag);
- buff.writeCompoundTag(tag);
- }
- @SideOnly(Side.CLIENT)
- public boolean isInRangeToRenderDist(double distance)
- {
- return true;
- }
- public boolean canPassengerSteer()
- {
- return true;
- }
- public Iterable<ItemStack> getArmorInventoryList()
- {
- return Lists.newArrayList();
- }
- public ItemStack getItemStackFromSlot(EntityEquipmentSlot slotIn)
- {
- return ItemStack.EMPTY;
- }
- public void setItemStackToSlot(EntityEquipmentSlot slotIn, ItemStack stack) {}
- public ItemStack getHeldItem(EnumHand hand)
- {
- return ItemStack.EMPTY;
- }
- public void setHeldItem(EnumHand hand, @Nullable ItemStack stack) {}
- public EnumHandSide getPrimaryHand()
- {
- return EnumHandSide.LEFT;
- }
- public void setBlocks(IBlockState[][][] blocks)
- {
- this.blocks = blocks;
- }
- public IBlockState[][][] getBlocks()
- {
- return this.blocks;
- }
- public void setTiles(TileEntity[][][] tiles)
- {
- this.tiles = tiles;
- }
- public TileEntity[][][] getTiles()
- {
- return this.tiles;
- }
- public BlockPos getMin()
- {
- return this.boundMin;
- }
- public BlockPos getMax()
- {
- return this.boundMax;
- }
- public void setMin(BlockPos pos)
- {
- this.boundMin = pos;
- }
- public void setMax(BlockPos pos)
- {
- this.boundMax = pos;
- }
- public void setFakeWorld(BlockEntityWorld world)
- {
- this.fake_world = world;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement