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

185 lines
6.3 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;
2020-01-22 23:21:52 +01:00
import de.ellpeck.naturesaura.packet.PacketHandler;
import de.ellpeck.naturesaura.packet.PacketParticles;
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;
2021-12-04 15:40:09 +01:00
import net.minecraft.nbt.CompoundTag;
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;
2020-01-23 19:20:47 +01:00
import net.minecraft.network.IPacket;
import net.minecraft.util.DamageSource;
2019-01-22 16:10:27 +01:00
import net.minecraft.util.math.BlockPos;
2021-12-04 19:17:21 +01:00
import net.minecraft.util.math.Mth;
2019-01-22 16:10:27 +01:00
import net.minecraft.util.math.RayTraceResult;
2020-09-22 03:17:02 +02:00
import net.minecraft.util.math.vector.Vector3d;
2021-12-04 15:40:09 +01:00
import net.minecraft.level.GameRules;
import net.minecraft.level.Level;
import net.minecraft.level.server.ServerLevel;
2019-01-22 16:10:27 +01:00
import net.minecraftforge.common.util.Constants;
2020-01-29 21:36:21 +01:00
import net.minecraftforge.common.util.ITeleporter;
2020-01-23 19:20:47 +01:00
import net.minecraftforge.fml.network.NetworkHooks;
2019-01-22 16:10:27 +01:00
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<>();
public boolean isActive;
2020-02-07 15:22:30 +01:00
private BlockPos lastPosition = BlockPos.ZERO;
2019-01-22 16:10:27 +01:00
2021-12-04 15:40:09 +01:00
public EntityMoverMinecart(EntityType<?> type, Level level) {
super(type, level);
2019-01-22 16:10:27 +01:00
}
2021-12-04 15:40:09 +01:00
public EntityMoverMinecart(EntityType<?> type, Level level, double x, double y, double z) {
super(type, level, 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
2021-12-04 15:40:09 +01:00
if (!this.spotOffsets.isEmpty() && this.level.getGameTime() % 10 == 0)
PacketHandler.sendToAllAround(this.level, pos, 32, new PacketParticles(
2020-01-28 18:08:56 +01:00
(float) this.getPosX(), (float) this.getPosY(), (float) this.getPosZ(), PacketParticles.Type.MOVER_CART,
2021-12-04 19:17:21 +01:00
Mth.floor(this.getMotion().getX() * 100F), Mth.floor(this.getMotion().getY() * 100F), Mth.floor(this.getMotion().getZ() * 100F)));
2019-01-31 17:54:55 +01:00
2020-01-22 23:21:52 +01:00
if (pos.distanceSq(this.lastPosition) < 8 * 8)
return;
2019-01-22 16:10:27 +01:00
2021-12-04 15:40:09 +01:00
this.moveAura(this.level, this.lastPosition, this.level, pos);
this.lastPosition = pos;
}
2021-12-04 15:40:09 +01:00
private void moveAura(Level oldLevel, BlockPos oldPos, Level newLevel, BlockPos newPos) {
2019-01-22 16:10:27 +01:00
for (BlockPos offset : this.spotOffsets) {
BlockPos spot = oldPos.add(offset);
2021-12-04 15:40:09 +01:00
IAuraChunk chunk = IAuraChunk.getAuraChunk(oldLevel, 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;
2021-12-04 19:17:21 +01:00
int toLose = Mth.ceil(drained / 250F);
BlockPos newSpot = newPos.add(offset);
2021-12-04 15:40:09 +01:00
IAuraChunk.getAuraChunk(newLevel, 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;
BlockPos pos = this.getPosition();
2019-01-22 16:10:27 +01:00
if (!this.isActive) {
2021-12-04 15:40:09 +01:00
this.moveAura(this.level, this.lastPosition, this.level, pos);
2019-01-22 16:10:27 +01:00
this.spotOffsets.clear();
2020-01-21 23:02:39 +01:00
this.lastPosition = BlockPos.ZERO;
2019-01-22 16:10:27 +01:00
return;
}
2021-12-04 15:40:09 +01:00
IAuraChunk.getSpotsInArea(this.level, pos, 25, (spot, amount) -> {
2019-01-22 16:10:27 +01:00
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();
2021-12-04 15:40:09 +01:00
if (this.level.getGameRules().getBoolean(GameRules.DO_ENTITY_DROPS))
2020-01-23 19:20:47 +01:00
this.entityDropItem(new ItemStack(ModItems.MOVER_CART), 0);
}
2019-01-22 16:10:27 +01:00
@Override
2021-12-04 15:40:09 +01:00
public CompoundTag serializeNBT() {
CompoundTag compound = super.serializeNBT();
2020-01-21 23:02:39 +01:00
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-28 18:08:56 +01:00
list.add(LongNBT.valueOf(offset.toLong()));
2020-01-21 23:02:39 +01:00
compound.put("offsets", list);
return compound;
2019-01-22 16:10:27 +01:00
}
@Override
2021-12-04 15:40:09 +01:00
public void deserializeNBT(CompoundTag compound) {
2020-01-21 23:02:39 +01:00
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
2021-12-04 15:40:09 +01:00
public Entity changeDimension(ServerLevel destination, ITeleporter teleporter) {
2020-01-29 21:36:21 +01:00
Entity entity = super.changeDimension(destination, teleporter);
if (entity instanceof EntityMoverMinecart) {
BlockPos pos = entity.getPosition();
2021-12-04 15:40:09 +01:00
this.moveAura(this.level, this.lastPosition, entity.level, 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() {
2020-01-23 19:20:47 +01:00
return new ItemStack(ModItems.MOVER_CART);
2019-01-22 16:10:27 +01:00
}
@Override
public ItemStack getPickedResult(RayTraceResult target) {
2020-01-23 19:20:47 +01:00
return new ItemStack(ModItems.MOVER_CART);
2019-01-22 16:10:27 +01:00
}
@Override
public boolean canBeRidden() {
return false;
}
@Override
protected void applyDrag() {
2020-09-22 03:17:02 +02:00
Vector3d motion = this.getMotion();
2020-01-21 23:02:39 +01:00
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
2020-01-23 19:20:47 +01:00
@Override
public IPacket<?> createSpawnPacket() {
return NetworkHooks.getEntitySpawningPacket(this);
}
2019-01-22 16:10:27 +01:00
}