From e136d7847bd7c2bc7a1bafb5019c4d0888e4b028 Mon Sep 17 00:00:00 2001 From: atomicint Date: Mon, 23 Mar 2015 10:33:31 -0700 Subject: [PATCH] Removed the hard coded index fix for the mob repository, properly fixed it and cleaned up some of the repository code itself. --- .../game/message/impl/NpcActionMessage.java | 2 +- .../message/impl/PlayerActionMessage.java | 6 +- src/org/apollo/util/MobRepository.java | 140 ++++++++---------- 3 files changed, 68 insertions(+), 80 deletions(-) diff --git a/src/org/apollo/game/message/impl/NpcActionMessage.java b/src/org/apollo/game/message/impl/NpcActionMessage.java index 181eb80b..67173a42 100644 --- a/src/org/apollo/game/message/impl/NpcActionMessage.java +++ b/src/org/apollo/game/message/impl/NpcActionMessage.java @@ -29,7 +29,7 @@ public abstract class NpcActionMessage extends Message { */ public NpcActionMessage(int option, int index) { this.option = option; - this.index = index - 1; + this.index = index; } /** diff --git a/src/org/apollo/game/message/impl/PlayerActionMessage.java b/src/org/apollo/game/message/impl/PlayerActionMessage.java index 5369853e..7f109c5d 100644 --- a/src/org/apollo/game/message/impl/PlayerActionMessage.java +++ b/src/org/apollo/game/message/impl/PlayerActionMessage.java @@ -25,11 +25,11 @@ public abstract class PlayerActionMessage extends Message { * Creates a player action message. * * @param option The option number. - * @param playerIndex The index of the player. + * @param index The index of the player. */ - public PlayerActionMessage(int option, int playerIndex) { + public PlayerActionMessage(int option, int index) { this.option = option; - this.index = playerIndex - 1; + this.index = index; } /** diff --git a/src/org/apollo/util/MobRepository.java b/src/org/apollo/util/MobRepository.java index 876edc0b..4b1e537d 100644 --- a/src/org/apollo/util/MobRepository.java +++ b/src/org/apollo/util/MobRepository.java @@ -1,7 +1,6 @@ package org.apollo.util; import java.util.Iterator; -import java.util.NoSuchElementException; import org.apollo.game.model.entity.Mob; @@ -11,6 +10,7 @@ import com.google.common.base.Preconditions; * A {@link MobRepository} is a repository of {@link Mob}s that are currently active in the game world. * * @author Graham + * @author Ryley * @param The type of Mob. */ public final class MobRepository implements Iterable { @@ -19,57 +19,57 @@ public final class MobRepository implements Iterable { * The {@link Iterator} implementation for the MobRepository. * * @author Graham + * @author Ryley */ private final class MobRepositoryIterator implements Iterator { /** - * The current index of this iterator. + * The repository of {@link Mob}s this {@link Iterator} iterates over. */ - private int index = 0; + private final MobRepository repository; /** - * The previous index of this iterator. + * The current index of this iterator. */ - private int previousIndex = -1; + private int currentIndex; + + /** + * The amount of indexes found. + */ + private int foundIndex; + + /** + * Constructs a new {@link MobRepositoryIterator} with the specified MobRepository. + * + * @param repository The repository of Mob's this Iterator iterates over. + */ + private MobRepositoryIterator(MobRepository repository) { + this.repository = repository; + } @Override public boolean hasNext() { - for (int i = index; i < mobs.length; i++) { - if (mobs[i] != null) { - index = i; + if (foundIndex == repository.size()) { + return false; + } + + while (currentIndex < repository.capacity()) { + if (mobs[currentIndex++] != null) { + foundIndex++; return true; } } return false; } - @SuppressWarnings("unchecked") @Override public T next() { - T mob = null; - for (int i = index; i < mobs.length; i++) { - if (mobs[i] != null) { - mob = (T) mobs[i]; - index = i; - break; - } - } - - if (mob == null) { - throw new NoSuchElementException("Mob does not exist."); - } - - previousIndex = index; - index++; - return mob; + return repository.get(currentIndex); } - @SuppressWarnings("unchecked") @Override public void remove() { - Preconditions.checkState(previousIndex != -1, "Cannot remove as the repository is empty."); - MobRepository.this.remove((T) mobs[previousIndex]); - previousIndex = -1; + repository.remove(currentIndex + 1); } } @@ -79,11 +79,6 @@ public final class MobRepository implements Iterable { */ private final Mob[] mobs; - /** - * The position of the next free index. - */ - private int pointer = 0; - /** * The current size of this repository. */ @@ -95,7 +90,7 @@ public final class MobRepository implements Iterable { * @param capacity The maximum number of Mobs that can be present in the repository. */ public MobRepository(int capacity) { - this.mobs = new Mob[capacity]; + mobs = new Mob[capacity]; } /** @@ -105,41 +100,23 @@ public final class MobRepository implements Iterable { * @return {@code true} if the Mob was added, {@code false} if the size has reached the capacity of this repository. */ public boolean add(T mob) { - if (size == mobs.length) { + if (size == capacity()) { return false; } - int index = -1; - for (int i = pointer; i < mobs.length; i++) { - if (mobs[i] == null) { - index = i; - break; + for (int index = 0; index < capacity(); index++) { + if (mobs[index] != null) { + continue; } + + mobs[index] = mob; + mob.setIndex(index + 1); + size++; + + return true; } - if (index == -1) { - for (int i = 0; i < pointer; i++) { - if (mobs[i] == null) { - index = i; - break; - } - } - } - - if (index == -1) { - return false; // shouldn't happen, but just in case - } - - mobs[index] = mob; - mob.setIndex(index + 1); - - if (index == mobs.length - 1) { - pointer = 0; - } else { - pointer = index; - } - size++; - return true; + return false; } /** @@ -160,13 +137,15 @@ public final class MobRepository implements Iterable { */ @SuppressWarnings("unchecked") public T get(int index) { - Preconditions.checkElementIndex(index, mobs.length, "Mob index is out of bounds."); - return (T) mobs[index]; + if (index < 1 || index >= capacity() + 1) { + throw new IndexOutOfBoundsException("Mob index is out of bounds."); + } + return (T) mobs[index - 1]; } @Override public Iterator iterator() { - return new MobRepositoryIterator(); + return new MobRepositoryIterator(this); } /** @@ -176,18 +155,27 @@ public final class MobRepository implements Iterable { * @return {@code true} if the Mob was removed, {@code false} if not. */ public boolean remove(T mob) { - int index = mob.getIndex() - 1; - if (index < 0 || index >= mobs.length) { + Preconditions.checkNotNull(mob); + return remove(mob.getIndex()); + } + + /** + * Removes a Mob from the repository by the specified index. + * + * @param index The index of the Mob to remove. + * @return {@code true} if the Mob at the specified index was removed otherwise {@code false}. + */ + public boolean remove(int index) { + Mob mob = get(index); + + if (mob.getIndex() != index) { return false; } - if (mobs[index] == mob) { - mobs[index] = null; - mob.setIndex(-1); - size--; - return true; - } - return false; + mobs[index - 1] = null; + mob.setIndex(-1); + size--; + return true; } /**