Skip to content

Commit 54c252a

Browse files
committed
Improve WrappedDataWatcher hasIndex performance
Use the map to check for indices instead of getting the object Addresses #850
1 parent e92abda commit 54c252a

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/main/java/com/comphenix/protocol/wrappers/WrappedDataWatcher.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ public WrappedWatchableObject remove(int index) {
243243
* @return True if it does, false if not
244244
*/
245245
public boolean hasIndex(int index) {
246-
return getObject(index) != null;
246+
return getMap().containsKey(index);
247247
}
248248

249249
/**
@@ -388,7 +388,7 @@ public Object getObject(WrappedDataWatcherObject object) {
388388
* @param value New value
389389
* @param update Whether or not to inform the client
390390
*
391-
* @see {@link #setObject(WrappedDataWatcherObject, Object, boolean)}
391+
* @see WrappedDataWatcher#setObject(WrappedDataWatcherObject, Object, boolean)
392392
* @throws IllegalArgumentException in 1.9 and up if there isn't already an
393393
* object at this index
394394
*/
@@ -415,7 +415,7 @@ public void setObject(int index, Object value) {
415415
* @param value New value
416416
* @param update Whether or not to inform the client
417417
*
418-
* @see {@link #setObject(WrappedDataWatcherObject, Object)}
418+
* @see WrappedDataWatcher#setObject(WrappedDataWatcherObject, Object)
419419
*/
420420
public void setObject(int index, Serializer serializer, Object value, boolean update) {
421421
setObject(new WrappedDataWatcherObject(index, serializer), value, update);
@@ -435,7 +435,7 @@ public void setObject(int index, Serializer serializer, Object value) {
435435
* @param value New value
436436
* @param update Whether or not to inform the client
437437
*
438-
* @see {@link #setObject(int, Object, boolean)}
438+
* @see WrappedDataWatcher#setObject(int, Object, boolean)
439439
*/
440440
public void setObject(int index, WrappedWatchableObject value, boolean update) {
441441
setObject(index, value.getRawValue(), update);

src/test/java/com/comphenix/protocol/wrappers/WrappedDataWatcherTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class WrappedDataWatcherTest {
4040

4141
@BeforeClass
4242
public static void prepare() {
43-
BukkitInitialization.initializePackage();
43+
BukkitInitialization.initializeItemMeta();
4444
}
4545

4646
@Test
@@ -94,4 +94,14 @@ public void testSerializers() {
9494
// assertNull(Registry.get(ItemStack.class, false));
9595
assertNotNull(Registry.get(UUID.class, true));
9696
}
97+
98+
@Test
99+
public void testHasIndex() {
100+
WrappedDataWatcher watcher = new WrappedDataWatcher();
101+
Serializer serializer = Registry.get(Integer.class);
102+
103+
assertFalse(watcher.hasIndex(0));
104+
watcher.setObject(0, serializer, 1);
105+
assertTrue(watcher.hasIndex(0));
106+
}
97107
}

0 commit comments

Comments
 (0)