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

184 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;
2021-12-06 14:38:12 +01:00
import net.minecraft.core.BlockPos;
2021-12-04 15:40:09 +01:00
import net.minecraft.nbt.CompoundTag;
2021-12-06 14:38:12 +01:00
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.LongTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.protocol.Packet;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.Mth;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.vehicle.AbstractMinecart;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.GameRules;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec3;
2020-01-29 21:36:21 +01:00
import net.minecraftforge.common.util.ITeleporter;
2021-12-06 14:38:12 +01:00
import net.minecraftforge.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;
2021-12-06 14:38:12 +01:00
public class EntityMoverMinecart extends AbstractMinecart {
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;
2021-12-15 16:24:53 +01:00
BlockPos pos = this.blockPosition();
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(
2021-12-06 14:38:12 +01:00
(float) this.getX(), (float) this.getY(), (float) this.getZ(), PacketParticles.Type.MOVER_CART,
Mth.floor(this.getDeltaMovement().x * 100F), Mth.floor(this.getDeltaMovement().y * 100F), Mth.floor(this.getDeltaMovement().z * 100F)));
2019-01-31 17:54:55 +01:00
2021-12-06 14:38:12 +01:00
if (pos.distSqr(this.lastPosition) < 8 * 8)
2020-01-22 23:21:52 +01:00
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) {
2021-12-06 14:38:12 +01:00
BlockPos spot = oldPos.offset(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);
2021-12-06 14:38:12 +01:00
BlockPos newSpot = newPos.offset(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
2021-12-06 14:38:12 +01:00
public void activateMinecart(int x, int y, int z, boolean receivingPower) {
2019-01-22 16:10:27 +01:00
if (this.isActive != receivingPower) {
this.isActive = receivingPower;
2021-12-15 16:24:53 +01:00
BlockPos pos = this.blockPosition();
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
2021-12-06 14:38:12 +01:00
public void destroy(DamageSource source) {
this.kill();
if (this.level.getGameRules().getBoolean(GameRules.RULE_DOENTITYDROPS))
this.spawnAtLocation(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);
2021-12-06 14:38:12 +01:00
compound.putLong("last_pos", this.lastPosition.asLong());
2019-01-22 16:10:27 +01:00
2021-12-06 14:38:12 +01:00
ListTag list = new ListTag();
2019-01-22 16:10:27 +01:00
for (BlockPos offset : this.spotOffsets)
2021-12-06 14:38:12 +01:00
list.add(LongTag.valueOf(offset.asLong()));
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");
2021-12-06 14:38:12 +01:00
this.lastPosition = BlockPos.of(compound.getLong("last_pos"));
2019-01-22 16:10:27 +01:00
this.spotOffsets.clear();
2021-12-06 14:38:12 +01:00
ListTag list = compound.getList("offsets", Tag.TAG_LONG);
for (Tag base : list)
this.spotOffsets.add(BlockPos.of(((LongTag) base).getAsLong()));
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) {
2021-12-15 16:24:53 +01:00
BlockPos pos = entity.blockPosition();
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
2021-12-06 14:38:12 +01:00
public BlockState getDisplayBlockState() {
return Blocks.STONE.defaultBlockState();
2019-01-22 16:10:27 +01:00
}
@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
2021-12-06 14:38:12 +01:00
public ItemStack getPickedResult(HitResult 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
2021-12-06 14:38:12 +01:00
protected void applyNaturalSlowdown() {
Vec3 motion = this.getDeltaMovement();
this.setDeltaMovement(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
2021-12-06 14:38:12 +01:00
public Packet<?> getAddEntityPacket() {
2020-01-23 19:20:47 +01:00
return NetworkHooks.getEntitySpawningPacket(this);
}
2019-01-22 16:10:27 +01:00
}