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
|
|
|
|
2019-03-03 17:24:32 +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:30:22 +01:00
|
|
|
var 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);
|
2019-03-03 17:24:32 +01:00
|
|
|
this.lastPosition = pos;
|
|
|
|
}
|
|
|
|
|
2021-12-04 15:40:09 +01:00
|
|
|
private void moveAura(Level oldLevel, BlockPos oldPos, Level newLevel, BlockPos newPos) {
|
2021-12-15 16:30:22 +01:00
|
|
|
for (var offset : this.spotOffsets) {
|
|
|
|
var spot = oldPos.offset(offset);
|
|
|
|
var chunk = IAuraChunk.getAuraChunk(oldLevel, spot);
|
|
|
|
var amount = chunk.getDrainSpot(spot);
|
2019-01-22 16:10:27 +01:00
|
|
|
if (amount <= 0)
|
|
|
|
continue;
|
2021-12-15 16:30:22 +01:00
|
|
|
var toMove = Math.min(amount, 300000);
|
|
|
|
var drained = chunk.drainAura(spot, toMove, false, false);
|
2019-01-22 16:10:27 +01:00
|
|
|
if (drained <= 0)
|
|
|
|
continue;
|
2021-12-15 16:30:22 +01:00
|
|
|
var toLose = Mth.ceil(drained / 250F);
|
|
|
|
var 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:30:22 +01:00
|
|
|
var 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-23 13:00:12 +01:00
|
|
|
@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-23 13:00:12 +01:00
|
|
|
}
|
|
|
|
|
2019-01-22 16:10:27 +01:00
|
|
|
@Override
|
2021-12-04 15:40:09 +01:00
|
|
|
public CompoundTag serializeNBT() {
|
2021-12-15 16:30:22 +01:00
|
|
|
var 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-15 16:30:22 +01:00
|
|
|
var list = new ListTag();
|
|
|
|
for (var 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-15 16:30:22 +01:00
|
|
|
var list = compound.getList("offsets", Tag.TAG_LONG);
|
|
|
|
for (var base : list)
|
2021-12-06 14:38:12 +01:00
|
|
|
this.spotOffsets.add(BlockPos.of(((LongTag) base).getAsLong()));
|
2019-01-22 16:10:27 +01:00
|
|
|
}
|
|
|
|
|
2019-03-03 17:24:32 +01:00
|
|
|
@Nullable
|
|
|
|
@Override
|
2021-12-04 15:40:09 +01:00
|
|
|
public Entity changeDimension(ServerLevel destination, ITeleporter teleporter) {
|
2021-12-15 16:30:22 +01:00
|
|
|
var entity = super.changeDimension(destination, teleporter);
|
2019-03-03 17:24:32 +01:00
|
|
|
if (entity instanceof EntityMoverMinecart) {
|
2021-12-15 16:30:22 +01:00
|
|
|
var pos = entity.blockPosition();
|
2021-12-04 15:40:09 +01:00
|
|
|
this.moveAura(this.level, this.lastPosition, entity.level, pos);
|
2019-03-03 17:24:32 +01:00
|
|
|
((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
|
2022-03-04 15:59:04 +01:00
|
|
|
public ItemStack getPickResult() {
|
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() {
|
2021-12-15 16:30:22 +01:00
|
|
|
var motion = this.getDeltaMovement();
|
2021-12-06 14:38:12 +01:00
|
|
|
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
|
|
|
}
|