Skip to content

Commit

Permalink
- Implement IndexOf, which is used by player respawn in KCauldron
Browse files Browse the repository at this point in the history
- Return full Entity list size when trying to remove
Closes #27
  • Loading branch information
wildex999 committed Feb 19, 2016
1 parent 9619eb4 commit e236ddc
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,27 @@ public EntityObject get(int index) {

@Override
public int indexOf(Object o) {
//TODO: Actually implement this, even if it would be a slow method
throw new NotImplementedException("indexOf is not implemented in TickDynamic's List implementation!");
if(!(o instanceof EntityObject))
return -1;

EntityObject obj = (EntityObject)o;
if(obj.TD_entityGroup == null || obj.TD_entityGroup.list != this)
return -1;

//Same strategy as get(index), jump groups, adding their size until we reach the group containing the Object
int offset = 0;
for(EntityGroup group : localGroups) {
if(obj.TD_entityGroup == group)
{
int index = group.entities.indexOf(obj);
if(index == -1)
return -1;
return offset + index;
}
offset += group.getEntityCount();
}

return -1;
}

@Override
Expand All @@ -330,8 +349,28 @@ public boolean isEmpty() {

@Override
public int lastIndexOf(Object o) {
//TODO: Actually implement this, even if it would be a slow method
throw new NotImplementedException("lastIndexOf is not implemented in TickDynamic's List implementation!");
if(!(o instanceof EntityObject))
return -1;

EntityObject obj = (EntityObject)o;
if(obj.TD_entityGroup == null || obj.TD_entityGroup.list != this)
return -1;

//Same strategy as get(index), jump groups, adding their size until we reach the group containing the Object
int offset = 0;
int lastIndex = -1;
for(EntityGroup group : localGroups) {
if(obj.TD_entityGroup == group)
{
int index = group.entities.indexOf(obj);
if(index == -1)
return -1;
lastIndex = offset + index;
}
offset += group.getEntityCount();
}

return lastIndex;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public void stopUpdate() {

@Override
public int size() {
if(profiler.stage == CustomProfiler.Stage.None || profiler.stage == CustomProfiler.Stage.InTick || profiler.stage == CustomProfiler.Stage.BeforeLoop)
if(profiler.stage == CustomProfiler.Stage.None || profiler.stage == CustomProfiler.Stage.InTick
|| profiler.stage == CustomProfiler.Stage.BeforeLoop || profiler.stage == CustomProfiler.Stage.InRemove)
return super.size();

if(!updateStarted) {
Expand Down

0 comments on commit e236ddc

Please sign in to comment.