Improve WrappedDataWatcher hasIndex performance

Use the map to check for indices instead of getting the object

Addresses #850
This commit is contained in:
Dan Mulloy 2020-05-27 21:56:22 -04:00
parent e92abda187
commit 54c252a354
No known key found for this signature in database
GPG Key ID: 2B62F7DACFF133E8
2 changed files with 15 additions and 5 deletions

View File

@ -243,7 +243,7 @@ public class WrappedDataWatcher extends AbstractWrapper implements Iterable<Wrap
* @return True if it does, false if not * @return True if it does, false if not
*/ */
public boolean hasIndex(int index) { public boolean hasIndex(int index) {
return getObject(index) != null; return getMap().containsKey(index);
} }
/** /**
@ -388,7 +388,7 @@ public class WrappedDataWatcher extends AbstractWrapper implements Iterable<Wrap
* @param value New value * @param value New value
* @param update Whether or not to inform the client * @param update Whether or not to inform the client
* *
* @see {@link #setObject(WrappedDataWatcherObject, Object, boolean)} * @see WrappedDataWatcher#setObject(WrappedDataWatcherObject, Object, boolean)
* @throws IllegalArgumentException in 1.9 and up if there isn't already an * @throws IllegalArgumentException in 1.9 and up if there isn't already an
* object at this index * object at this index
*/ */
@ -415,7 +415,7 @@ public class WrappedDataWatcher extends AbstractWrapper implements Iterable<Wrap
* @param value New value * @param value New value
* @param update Whether or not to inform the client * @param update Whether or not to inform the client
* *
* @see {@link #setObject(WrappedDataWatcherObject, Object)} * @see WrappedDataWatcher#setObject(WrappedDataWatcherObject, Object)
*/ */
public void setObject(int index, Serializer serializer, Object value, boolean update) { public void setObject(int index, Serializer serializer, Object value, boolean update) {
setObject(new WrappedDataWatcherObject(index, serializer), value, update); setObject(new WrappedDataWatcherObject(index, serializer), value, update);
@ -435,7 +435,7 @@ public class WrappedDataWatcher extends AbstractWrapper implements Iterable<Wrap
* @param value New value * @param value New value
* @param update Whether or not to inform the client * @param update Whether or not to inform the client
* *
* @see {@link #setObject(int, Object, boolean)} * @see WrappedDataWatcher#setObject(int, Object, boolean)
*/ */
public void setObject(int index, WrappedWatchableObject value, boolean update) { public void setObject(int index, WrappedWatchableObject value, boolean update) {
setObject(index, value.getRawValue(), update); setObject(index, value.getRawValue(), update);

View File

@ -40,7 +40,7 @@ public class WrappedDataWatcherTest {
@BeforeClass @BeforeClass
public static void prepare() { public static void prepare() {
BukkitInitialization.initializePackage(); BukkitInitialization.initializeItemMeta();
} }
@Test @Test
@ -94,4 +94,14 @@ public class WrappedDataWatcherTest {
// assertNull(Registry.get(ItemStack.class, false)); // assertNull(Registry.get(ItemStack.class, false));
assertNotNull(Registry.get(UUID.class, true)); assertNotNull(Registry.get(UUID.class, true));
} }
@Test
public void testHasIndex() {
WrappedDataWatcher watcher = new WrappedDataWatcher();
Serializer serializer = Registry.get(Integer.class);
assertFalse(watcher.hasIndex(0));
watcher.setObject(0, serializer, 1);
assertTrue(watcher.hasIndex(0));
}
} }