-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
EntityTickRegion.java
86 lines (71 loc) · 2.43 KB
/
EntityTickRegion.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package me.nallar.tickthreading.minecraft.tickregion;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import me.nallar.tickthreading.Log;
import me.nallar.tickthreading.minecraft.TickManager;
import net.minecraft.entity.Entity;
import net.minecraft.world.World;
import net.minecraft.world.chunk.IChunkProvider;
public class EntityTickRegion extends TickRegion {
private final Set<Entity> entitySet = new LinkedHashSet<Entity>();
public EntityTickRegion(World world, TickManager manager, int regionX, int regionZ) {
super(world, manager, regionX, regionZ);
}
@Override
public void doTick() {
try {
IChunkProvider chunkProvider = world.getChunkProvider();
Iterator<Entity> entitiesIterator = entitySet.iterator();
while (entitiesIterator.hasNext()) {
Entity entity = entitiesIterator.next();
if (entity.ridingEntity != null) {
if (!entity.ridingEntity.isDead && entity.ridingEntity.riddenByEntity == entity) {
continue;
}
entity.ridingEntity.riddenByEntity = null;
entity.ridingEntity = null;
}
if (!entity.isDead) {
world.updateEntity(entity);
}
if (entity.isDead) {
int entityX = entity.chunkCoordX;
int entityZ = entity.chunkCoordZ;
if (entity.addedToChunk && chunkProvider.chunkExists(entityX, entityZ)) {
world.getChunkFromChunkCoords(entityX, entityZ).removeEntity(entity);
}
entitiesIterator.remove();
manager.removed(entity);
} else if (manager.getHashCode(entity) != hashCode) {
entitiesIterator.remove();
manager.add(entity);
//Log.severe("Inconsistent state: " + entity + " is in the wrong TickRegion.");
// Note to self for when I decide this is wrong later:
// Entities are supposed to move, of course this will happen!
}
}
} catch (Exception exception) {
Log.severe("Exception during entity tick at " + toString() + ":", exception);
}
}
public void add(Entity entity) {
entitySet.add(entity);
}
public boolean remove(Entity entity) {
return entitySet.remove(entity);
}
@Override
protected TickRegion getCallable(int regionX, int regionY) {
return manager.getEntityCallable(TickManager.getHashCodeFromRegionCoords(regionX, regionY));
}
@Override
public boolean isEmpty() {
return entitySet.isEmpty();
}
@Override
public void die() {
super.die();
entitySet.clear();
}
}