Skip to content

Commit

Permalink
lifetime tracker: dont track previous entities in previous track
Browse files Browse the repository at this point in the history
  • Loading branch information
Fallen-Breath committed Jan 21, 2021
1 parent e2d4e95 commit 32fb0fd
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import carpet.utils.Messenger;
import carpettisaddition.commands.AbstractTracker;
import carpettisaddition.commands.lifetime.interfaces.IEntity;
import carpettisaddition.commands.lifetime.interfaces.IServerWorld;
import carpettisaddition.commands.lifetime.utils.LifeTimeTrackerUtil;
import carpettisaddition.commands.lifetime.utils.SpecificDetailMode;
import carpettisaddition.utils.TextUtil;
import it.unimi.dsi.fastutil.objects.Reference2ObjectArrayMap;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.ServerCommandSource;
Expand All @@ -23,6 +25,8 @@ public class LifeTimeTracker extends AbstractTracker
private static boolean attachedServer = false;
private static final LifeTimeTracker INSTANCE = new LifeTimeTracker();

private int currentTrackId = 0;

private final Map<ServerWorld, LifeTimeWorldTracker> trackers = new Reference2ObjectArrayMap<>();

public LifeTimeTracker()
Expand Down Expand Up @@ -56,6 +60,11 @@ public static boolean isActivated()
return attachedServer && INSTANCE.isTracking();
}

public static boolean willTrackEntity(Entity entity)
{
return isActivated() && ((IEntity)entity).getTrackId() == INSTANCE.getCurrentTrackId() && LifeTimeTrackerUtil.isTrackedEntity(entity);
}

public Stream<String> getAvailableEntityType()
{
if (!isActivated())
Expand All @@ -70,9 +79,15 @@ public Stream<String> getAvailableEntityType()
distinct();
}

public int getCurrentTrackId()
{
return this.currentTrackId;
}

@Override
protected void initTracker()
{
this.currentTrackId++;
this.trackers.values().forEach(LifeTimeWorldTracker::initTracker);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void initTracker()

private Optional<TrackedData> getTrackedData(Entity entity)
{
if (LifeTimeTracker.isActivated() && LifeTimeTrackerUtil.isTrackedEntity(entity))
if (LifeTimeTracker.willTrackEntity(entity))
{
return Optional.of(this.dataMap.computeIfAbsent(entity.getType(), (e -> new TrackedData())));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

public interface IEntity
{
int getTrackId();

long getLifeTime();

Vec3d getSpawningPosition();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import carpettisaddition.commands.lifetime.interfaces.IServerWorld;
import carpettisaddition.commands.lifetime.removal.RemovalReason;
import carpettisaddition.commands.lifetime.spawning.SpawningReason;
import carpettisaddition.commands.lifetime.utils.LifeTimeTrackerUtil;
import carpettisaddition.utils.GameUtil;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
Expand Down Expand Up @@ -34,18 +33,30 @@ public abstract class EntityMixin implements IEntity
private boolean recordedRemoval;
private Vec3d spawningPos;
private Vec3d removalPos;
private int trackId;

@Inject(method = "<init>", at = @At("TAIL"))
private void onConstructLifeTimeTracker(CallbackInfo ci)
{
this.doLifeTimeTracking = false;
this.recordedSpawning = false;
this.recordedRemoval = false;
if (this.world instanceof ServerWorld)
{
this.spawnTime = ((IServerWorld)this.world).getLifeTimeWorldTracker().getSpawnStageCounter();
this.doLifeTimeTracking = LifeTimeTracker.isActivated() && LifeTimeTrackerUtil.isTrackedEntity((Entity) (Object) this);
this.trackId = LifeTimeTracker.getInstance().getCurrentTrackId();
this.doLifeTimeTracking = LifeTimeTracker.willTrackEntity((Entity)(Object)this);
}
else
{
this.trackId = -1;
this.doLifeTimeTracking = false;
}
}

@Override
public int getTrackId()
{
return this.trackId;
}

@Override
Expand Down

0 comments on commit 32fb0fd

Please sign in to comment.