diff --git a/src/main/java/com/comphenix/protocol/reflect/StructureModifier.java b/src/main/java/com/comphenix/protocol/reflect/StructureModifier.java index 7b2881d08..a912b5c61 100644 --- a/src/main/java/com/comphenix/protocol/reflect/StructureModifier.java +++ b/src/main/java/com/comphenix/protocol/reflect/StructureModifier.java @@ -26,6 +26,8 @@ import com.google.common.collect.HashBasedTable; import com.google.common.collect.Table; +import java.lang.Iterable; +import java.util.Iterator; import java.lang.ref.Reference; import java.lang.ref.SoftReference; import java.lang.reflect.Field; @@ -40,8 +42,11 @@ import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.Spliterator; +import java.util.Spliterators; import java.util.function.UnaryOperator; import java.util.stream.Collectors; +import java.util.stream.Stream; /** * Provides list-oriented access to the fields of a Minecraft packet. @@ -51,7 +56,7 @@ * @param Type of the fields to retrieve. * @author Kristian */ -public class StructureModifier { +public class StructureModifier implements Iterable> { // Instance generator we will use private static final DefaultInstances DEFAULT_GENERATOR = getDefaultGenerator(); @@ -666,4 +671,20 @@ protected void setConverter(EquivalentConverter converter) { public String toString() { return "StructureModifier[fieldType=" + this.fieldType + ", data=" + this.accessors + "]"; } + + /** + * {@inheritDoc} + */ + @Override + public Iterator> iterator() { + return new StructureModifierIterator(this); + } + + /** + * {@inheritDoc} + */ + @Override + public Spliterator> spliterator() { + return Spliterators.spliterator(this.iterator(), this.size(), Spliterator.DISTINCT | Spliterator.NONNULL | Spliterator.ORDERED | Spliterator.SIZED); + } } diff --git a/src/main/java/com/comphenix/protocol/reflect/StructureModifierIntermediate.java b/src/main/java/com/comphenix/protocol/reflect/StructureModifierIntermediate.java new file mode 100644 index 000000000..76b535e26 --- /dev/null +++ b/src/main/java/com/comphenix/protocol/reflect/StructureModifierIntermediate.java @@ -0,0 +1,64 @@ +/* + * ProtocolLib - Bukkit server library that allows access to the Minecraft protocol. + * Copyright (C) 2012 Kristian S. Stangeland + * + * This program is free software; you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program; + * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + * 02111-1307 USA + */ +package com.comphenix.protocol.reflect; + +/** + * Created by {@link StructureModifierIterator} iterator access to a {@link StructureModifier} + * + * @param Type of the fields in the {@link StructureModifier} + * @author BradBot_1 + */ +public class StructureModifierIntermediate { + + protected final StructureModifier structure; + protected final int index; + + /** + * @param structure - {@link StructureModifier} to operate on. + * @param index - index to access. + * + * @apiNote Must be public, else extending it would not work properly. + */ + public StructureModifierIntermediate(final StructureModifier structure, final int index) { + this.structure = structure; + this.index = index; + } + + /** + * @return Object at index. + */ + public T get() { + return this.structure.read(this.index); + } + + /** + * Overwrites the existing value at the index in the structure. + * + * @param toWrite - Object to overwrite the existing one. + */ + public void set(final T toWrite) { + this.structure.write(index, toWrite); + } + + /** + * @return The internal structure modifier + */ + public StructureModifier getInternalStructureModifier() { + return this.structure; + } + +} diff --git a/src/main/java/com/comphenix/protocol/reflect/StructureModifierIterator.java b/src/main/java/com/comphenix/protocol/reflect/StructureModifierIterator.java new file mode 100644 index 000000000..4a6f45a45 --- /dev/null +++ b/src/main/java/com/comphenix/protocol/reflect/StructureModifierIterator.java @@ -0,0 +1,57 @@ +/* + * ProtocolLib - Bukkit server library that allows access to the Minecraft protocol. + * Copyright (C) 2012 Kristian S. Stangeland + * + * This program is free software; you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program; + * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + * 02111-1307 USA + */ +package com.comphenix.protocol.reflect; + +import java.util.Iterator; +import java.util.NoSuchElementException; + +/** + * Provides iterator access to a {@link StructureModifier} + * + * @param Type of the fields in the {@link StructureModifier} + * @author BradBot_1 + */ +class StructureModifierIterator implements Iterator> { + + private final StructureModifier structure; + private int index = 0; + + /** + * @param structure - {@link StructureModifier} to operate on. + */ + StructureModifierIterator(final StructureModifier structure) { + this.structure = structure; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean hasNext() { + return this.index < this.structure.size(); + } + + /** + * {@inheritDoc} + */ + @Override + public StructureModifierIntermediate next() { + if (!this.hasNext()) throw new NoSuchElementException(); + return new StructureModifierIntermediate(this.structure, this.index++); + } + +}