NaturesAura/src/main/java/de/ellpeck/naturesaura/entities/EntityMoverMinecart.java

176 lines
5.8 KiB
Java
Raw Normal View History

2019-01-22 16:10:27 +01:00
package de.ellpeck.naturesaura.entities;
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
import de.ellpeck.naturesaura.items.ModItems;
2019-10-20 22:30:49 +02:00
import net.minecraft.block.BlockState;
2020-01-21 23:02:39 +01:00
import net.minecraft.block.Blocks;
import net.minecraft.entity.Entity;
2020-01-21 23:02:39 +01:00
import net.minecraft.entity.EntityType;
2019-10-20 22:30:49 +02:00
import net.minecraft.entity.item.minecart.AbstractMinecartEntity;
2019-01-22 16:10:27 +01:00
import net.minecraft.item.ItemStack;
2019-10-20 22:30:49 +02:00
import net.minecraft.nbt.CompoundNBT;
2020-01-21 23:02:39 +01:00
import net.minecraft.nbt.INBT;
2019-10-20 22:30:49 +02:00
import net.minecraft.nbt.ListNBT;
import net.minecraft.nbt.LongNBT;
import net.minecraft.util.DamageSource;
2019-01-22 16:10:27 +01:00
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.RayTraceResult;
2020-01-21 23:02:39 +01:00
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.GameRules;
2019-01-22 16:10:27 +01:00
import net.minecraft.world.World;
2020-01-21 23:02:39 +01:00
import net.minecraft.world.dimension.DimensionType;
2019-01-22 16:10:27 +01:00
import net.minecraftforge.common.util.Constants;
import javax.annotation.Nullable;
2019-01-22 16:10:27 +01:00
import java.util.ArrayList;
import java.util.List;
2019-10-20 22:30:49 +02:00
public class EntityMoverMinecart extends AbstractMinecartEntity {
2019-01-22 16:10:27 +01:00
private final List<BlockPos> spotOffsets = new ArrayList<>();
2020-01-21 23:02:39 +01:00
private BlockPos lastPosition = BlockPos.ZERO;
2019-01-22 16:10:27 +01:00
public boolean isActive;
2020-01-21 23:02:39 +01:00
public EntityMoverMinecart(EntityType<?> type, World world) {
super(type, world);
2019-01-22 16:10:27 +01:00
}
2020-01-21 23:02:39 +01:00
public EntityMoverMinecart(EntityType<?> type, World world, double x, double y, double z) {
super(type, world, x, y, z);
2019-01-22 16:10:27 +01:00
}
@Override
public void moveMinecartOnRail(BlockPos railPos) {
super.moveMinecartOnRail(railPos);
if (!this.isActive)
return;
BlockPos pos = this.getPosition();
2019-01-31 17:54:55 +01:00
2020-01-21 23:02:39 +01:00
if (!this.spotOffsets.isEmpty() && this.world.getGameTime() % 10 == 0)
// TODO particles
/*PacketHandler.sendToAllAround(this.world, pos, 32, new PacketParticles(
2019-01-31 17:54:55 +01:00
(float) this.posX, (float) this.posY, (float) this.posZ, 22,
2020-01-21 23:02:39 +01:00
MathHelper.floor(this.motionX * 100F), MathHelper.floor(this.motionY * 100F), MathHelper.floor(this.motionZ * 100F)));*/
2019-01-31 17:54:55 +01:00
2020-01-21 23:02:39 +01:00
if (pos.distanceSq(this.lastPosition) < 8 * 8)
return;
2019-01-22 16:10:27 +01:00
this.moveAura(this.world, this.lastPosition, this.world, pos);
this.lastPosition = pos;
}
private void moveAura(World oldWorld, BlockPos oldPos, World newWorld, BlockPos newPos) {
2019-01-22 16:10:27 +01:00
for (BlockPos offset : this.spotOffsets) {
BlockPos spot = oldPos.add(offset);
IAuraChunk chunk = IAuraChunk.getAuraChunk(oldWorld, spot);
2019-01-22 16:10:27 +01:00
int amount = chunk.getDrainSpot(spot);
if (amount <= 0)
continue;
int toMove = Math.min(amount, 300000);
2019-01-22 16:10:27 +01:00
int drained = chunk.drainAura(spot, toMove, false, false);
if (drained <= 0)
continue;
int toLose = MathHelper.ceil(drained / 250F);
BlockPos newSpot = newPos.add(offset);
IAuraChunk.getAuraChunk(newWorld, newSpot).storeAura(newSpot, drained - toLose, false, false);
2019-01-22 16:10:27 +01:00
}
}
@Override
public void onActivatorRailPass(int x, int y, int z, boolean receivingPower) {
if (this.isActive != receivingPower) {
this.isActive = receivingPower;
if (!this.isActive) {
this.spotOffsets.clear();
2020-01-21 23:02:39 +01:00
this.lastPosition = BlockPos.ZERO;
2019-01-22 16:10:27 +01:00
return;
}
BlockPos pos = this.getPosition();
IAuraChunk.getSpotsInArea(this.world, pos, 25, (spot, amount) -> {
if (amount > 0)
this.spotOffsets.add(spot.subtract(pos));
});
this.lastPosition = pos;
}
}
@Override
public void killMinecart(DamageSource source) {
2020-01-21 23:02:39 +01:00
this.remove();
if (this.world.getGameRules().getBoolean(GameRules.DO_ENTITY_DROPS))
this.entityDropItem(new ItemStack(ModItems.MOVER_MINECART), 0);
}
2019-01-22 16:10:27 +01:00
@Override
2020-01-21 23:02:39 +01:00
public CompoundNBT serializeNBT() {
CompoundNBT compound = super.serializeNBT();
compound.putBoolean("active", this.isActive);
compound.putLong("last_pos", this.lastPosition.toLong());
2019-01-22 16:10:27 +01:00
2019-10-20 22:30:49 +02:00
ListNBT list = new ListNBT();
2019-01-22 16:10:27 +01:00
for (BlockPos offset : this.spotOffsets)
2020-01-21 23:02:39 +01:00
list.add(new LongNBT(offset.toLong()));
compound.put("offsets", list);
return compound;
2019-01-22 16:10:27 +01:00
}
@Override
2020-01-21 23:02:39 +01:00
public void deserializeNBT(CompoundNBT compound) {
super.deserializeNBT(compound);
2019-01-22 16:10:27 +01:00
this.isActive = compound.getBoolean("active");
this.lastPosition = BlockPos.fromLong(compound.getLong("last_pos"));
this.spotOffsets.clear();
2020-01-21 23:02:39 +01:00
ListNBT list = compound.getList("offsets", Constants.NBT.TAG_LONG);
for (INBT base : list)
2019-10-20 22:30:49 +02:00
this.spotOffsets.add(BlockPos.fromLong(((LongNBT) base).getLong()));
2019-01-22 16:10:27 +01:00
}
@Nullable
@Override
2020-01-21 23:02:39 +01:00
public Entity changeDimension(DimensionType destination) {
Entity entity = super.changeDimension(destination);
if (entity instanceof EntityMoverMinecart) {
BlockPos pos = entity.getPosition();
this.moveAura(this.world, this.lastPosition, entity.world, pos);
((EntityMoverMinecart) entity).lastPosition = pos;
}
return entity;
}
2019-01-22 16:10:27 +01:00
@Override
2019-10-20 22:30:49 +02:00
public BlockState getDisplayTile() {
2019-01-22 16:10:27 +01:00
return Blocks.STONE.getDefaultState();
}
@Override
2020-01-21 23:02:39 +01:00
public Type getMinecartType() {
2019-01-22 16:10:27 +01:00
return Type.RIDEABLE;
}
@Override
public ItemStack getCartItem() {
return new ItemStack(ModItems.MOVER_MINECART);
}
@Override
public ItemStack getPickedResult(RayTraceResult target) {
return new ItemStack(ModItems.MOVER_MINECART);
}
@Override
public boolean canBeRidden() {
return false;
}
@Override
protected void applyDrag() {
2020-01-21 23:02:39 +01:00
Vec3d motion = this.getMotion();
this.setMotion(motion.x * 0.99F, 0, motion.z * 0.99F);
2019-01-22 16:10:27 +01:00
}
2020-01-21 23:02:39 +01:00
2019-01-22 16:10:27 +01:00
}