This commit is contained in:
BradB 2024-04-28 16:00:42 +00:00 committed by GitHub
commit d6d4e356aa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 143 additions and 1 deletions

View File

@ -26,6 +26,8 @@ import com.comphenix.protocol.utility.MinecraftReflection;
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.HashMap;
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 @@ import java.util.stream.Collectors;
* @param <T> Type of the fields to retrieve.
* @author Kristian
*/
public class StructureModifier<T> {
public class StructureModifier<T> implements Iterable<StructureModifierIntermediate<T>> {
// Instance generator we will use
private static final DefaultInstances DEFAULT_GENERATOR = getDefaultGenerator();
@ -665,4 +670,20 @@ public class StructureModifier<T> {
public String toString() {
return "StructureModifier[fieldType=" + this.fieldType + ", data=" + this.accessors + "]";
}
/**
* {@inheritDoc}
*/
@Override
public Iterator<StructureModifierIntermediate<T>> iterator() {
return new StructureModifierIterator<T>(this);
}
/**
* {@inheritDoc}
*/
@Override
public Spliterator<StructureModifierIntermediate<T>> spliterator() {
return Spliterators.spliterator(this.iterator(), this.size(), Spliterator.DISTINCT | Spliterator.NONNULL | Spliterator.ORDERED | Spliterator.SIZED);
}
}

View File

@ -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 <T> Type of the fields in the {@link StructureModifier}
* @author BradBot_1
*/
public class StructureModifierIntermediate<T> {
protected final StructureModifier<T> 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<T> 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<T> getInternalStructureModifier() {
return this.structure;
}
}

View File

@ -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 <T> Type of the fields in the {@link StructureModifier}
* @author BradBot_1
*/
class StructureModifierIterator<T> implements Iterator<StructureModifierIntermediate<T>> {
private final StructureModifier<T> structure;
private int index = 0;
/**
* @param structure - {@link StructureModifier} to operate on.
*/
StructureModifierIterator(final StructureModifier<T> structure) {
this.structure = structure;
}
/**
* {@inheritDoc}
*/
@Override
public boolean hasNext() {
return this.index < this.structure.size();
}
/**
* {@inheritDoc}
*/
@Override
public StructureModifierIntermediate<T> next() {
if (!this.hasNext()) throw new NoSuchElementException();
return new StructureModifierIntermediate<T>(this.structure, this.index++);
}
}