diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_13MetadataFactory.java b/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_13MetadataFactory.java index ef20fb2..cb6f7df 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_13MetadataFactory.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_13MetadataFactory.java @@ -3,7 +3,6 @@ package lol.pyr.znpcsplus.metadata; import com.github.retrooper.packetevents.protocol.entity.data.EntityData; import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes; import com.github.retrooper.packetevents.util.adventure.AdventureSerializer; -import lol.pyr.znpcsplus.util.list.ListUtil; import net.kyori.adventure.text.Component; import java.util.Collection; @@ -12,7 +11,7 @@ import java.util.Optional; public class V1_13MetadataFactory extends V1_10MetadataFactory { @Override public Collection name(Component name) { - return ListUtil.immutableList( + return list( newEntityData(2, EntityDataTypes.OPTIONAL_COMPONENT, Optional.of(AdventureSerializer.getGsonSerializer().serialize(name))), newEntityData(3, EntityDataTypes.BOOLEAN, true) ); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_8MetadataFactory.java b/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_8MetadataFactory.java index 2945b5c..620a0b3 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_8MetadataFactory.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_8MetadataFactory.java @@ -4,10 +4,11 @@ import com.github.retrooper.packetevents.protocol.entity.data.EntityData; import com.github.retrooper.packetevents.protocol.entity.data.EntityDataType; import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes; import com.github.retrooper.packetevents.util.adventure.AdventureSerializer; -import lol.pyr.znpcsplus.util.list.ListUtil; import net.kyori.adventure.text.Component; +import java.util.ArrayList; import java.util.Collection; +import java.util.List; public class V1_8MetadataFactory implements MetadataFactory { @Override @@ -22,7 +23,7 @@ public class V1_8MetadataFactory implements MetadataFactory { @Override public Collection name(Component name) { - return ListUtil.immutableList( + return list( newEntityData(2, EntityDataTypes.STRING, AdventureSerializer.getLegacyGsonSerializer().serialize(name)), newEntityData(3, EntityDataTypes.BYTE, (byte) 1) ); @@ -53,4 +54,11 @@ public class V1_8MetadataFactory implements MetadataFactory { protected EntityData newEntityData(int index, EntityDataType type, T value) { return new EntityData(index, type, value); } + + @SafeVarargs + protected final List list(T... items) { + ArrayList list = new ArrayList<>(items.length); + for (int i = 0; i < items.length; i++) list.add(i, items[i]); + return list; + } } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_9MetadataFactory.java b/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_9MetadataFactory.java index 5228b74..5421a40 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_9MetadataFactory.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_9MetadataFactory.java @@ -3,7 +3,6 @@ package lol.pyr.znpcsplus.metadata; import com.github.retrooper.packetevents.protocol.entity.data.EntityData; import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes; import com.github.retrooper.packetevents.util.adventure.AdventureSerializer; -import lol.pyr.znpcsplus.util.list.ListUtil; import net.kyori.adventure.text.Component; import java.util.Collection; @@ -21,7 +20,7 @@ public class V1_9MetadataFactory extends V1_8MetadataFactory { @Override public Collection name(Component name) { - return ListUtil.immutableList( + return list( newEntityData(2, EntityDataTypes.STRING, AdventureSerializer.getGsonSerializer().serialize(name)), newEntityData(3, EntityDataTypes.BOOLEAN, true) ); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/skin/Skin.java b/plugin/src/main/java/lol/pyr/znpcsplus/skin/Skin.java index 26357a1..90841ba 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/skin/Skin.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/skin/Skin.java @@ -5,36 +5,38 @@ import com.github.retrooper.packetevents.protocol.player.UserProfile; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.mojang.authlib.properties.PropertyMap; -import lol.pyr.znpcsplus.util.list.ListUtil; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.stream.Collectors; public class Skin { private final long timestamp = System.currentTimeMillis(); - private final List properties = new ArrayList<>(); + private final List properties; public Skin(String texture, String signature) { + properties = new ArrayList<>(1); properties.add(new TextureProperty("textures", texture, signature)); } public Skin(TextureProperty... properties) { - this.properties.addAll(ListUtil.immutableList(properties)); + this.properties = Arrays.asList(properties); } public Skin(Collection properties) { - this.properties.addAll(properties); + this.properties = new ArrayList<>(properties); } public Skin(PropertyMap properties) { - this.properties.addAll(properties.values().stream() + this.properties = properties.values().stream() .map(property -> new TextureProperty(property.getName(), property.getValue(), property.getSignature())) - .collect(Collectors.toList())); + .collect(Collectors.toList()); } public Skin(JsonObject obj) { + properties = new ArrayList<>(); for (JsonElement e : obj.get("properties").getAsJsonArray()) { JsonObject o = e.getAsJsonObject(); properties.add(new TextureProperty(o.get("name").getAsString(), o.get("value").getAsString(), o.has("signature") ? o.get("signature").getAsString() : null)); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/util/list/ArrayIterator.java b/plugin/src/main/java/lol/pyr/znpcsplus/util/list/ArrayIterator.java deleted file mode 100644 index 8081617..0000000 --- a/plugin/src/main/java/lol/pyr/znpcsplus/util/list/ArrayIterator.java +++ /dev/null @@ -1,62 +0,0 @@ -package lol.pyr.znpcsplus.util.list; - -import java.util.Iterator; -import java.util.ListIterator; - -public class ArrayIterator implements Iterator, ListIterator { - private final T[] array; - private int index = 0; - - public ArrayIterator(T[] array) { - this.array = array; - } - - @Override - public boolean hasNext() { - return array.length > index; - } - - @Override - public T next() { - return array[index++]; - } - - private boolean inBounds(int index) { - return index >= 0 && index < array.length; - } - - @Override - public boolean hasPrevious() { - return inBounds(index - 1); - } - - @Override - public T previous() { - return array[--index]; - } - - @Override - public int nextIndex() { - return index + 1; - } - - @Override - public int previousIndex() { - return index - 1; - } - - @Override - public void remove() { - throw new UnsupportedOperationException(); - } - - @Override - public void set(T t) { - throw new UnsupportedOperationException(); - } - - @Override - public void add(T t) { - throw new UnsupportedOperationException(); - } -} diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/util/list/ImmutableArrayList.java b/plugin/src/main/java/lol/pyr/znpcsplus/util/list/ImmutableArrayList.java deleted file mode 100644 index 54d276c..0000000 --- a/plugin/src/main/java/lol/pyr/znpcsplus/util/list/ImmutableArrayList.java +++ /dev/null @@ -1,133 +0,0 @@ -package lol.pyr.znpcsplus.util.list; - -import java.util.*; - -public class ImmutableArrayList implements List, RandomAccess { - private final T[] elements; - - public ImmutableArrayList(T[] array) { - this.elements = array; - } - - @Override - public int size() { - return elements.length; - } - - @Override - public boolean isEmpty() { - return elements.length != 0; - } - - @Override - public boolean contains(Object o) { - return indexOf(o) != -1; - } - - @Override - public Iterator iterator() { - return new ArrayIterator<>(elements); - } - - @Override - public Object[] toArray() { - return elements; - } - - @SuppressWarnings("unchecked") - @Override - public T1[] toArray(T1[] a) { - return (T1[]) elements; - } - - @Override - public boolean containsAll(Collection c) { - for (Object obj : c) if (!contains(obj)) return false; - return true; - } - - @Override - public List subList(int fromIndex, int toIndex) { - return new ImmutableArrayList<>(Arrays.copyOfRange(elements, fromIndex, toIndex)); - } - - @Override - public T get(int index) { - return elements[index]; - } - - @Override - public int indexOf(Object o) { - for (int i = 0; i < elements.length; i++) if (Objects.equals(elements[i], o)) return i; - return -1; - } - - @Override - public int lastIndexOf(Object o) { - for (int i = 0; i < elements.length; i++) { - int index = elements.length - (i + 1); - if (Objects.equals(elements[index], o)) return index; - } - return -1; - } - - @Override - public ListIterator listIterator() { - return new ArrayIterator<>(elements); - } - - @Override - public ListIterator listIterator(int index) { - return new ArrayIterator<>(elements); - } - - @Override - public boolean add(T t) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean remove(Object o) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean addAll(Collection c) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean addAll(int index, Collection c) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean removeAll(Collection c) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean retainAll(Collection c) { - throw new UnsupportedOperationException(); - } - - @Override - public void clear() { - throw new UnsupportedOperationException(); - } - - @Override - public T set(int index, T element) { - throw new UnsupportedOperationException(); - } - - @Override - public void add(int index, T element) { - throw new UnsupportedOperationException(); - } - - @Override - public T remove(int index) { - throw new UnsupportedOperationException(); - } -} \ No newline at end of file diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/util/list/ListUtil.java b/plugin/src/main/java/lol/pyr/znpcsplus/util/list/ListUtil.java deleted file mode 100644 index faa6898..0000000 --- a/plugin/src/main/java/lol/pyr/znpcsplus/util/list/ListUtil.java +++ /dev/null @@ -1,10 +0,0 @@ -package lol.pyr.znpcsplus.util.list; - -import java.util.*; - -public class ListUtil { - @SafeVarargs - public static List immutableList(T... elements) { - return new ImmutableArrayList<>(elements); - } -}