stop using an obscure ass immutable array list implementation

This commit is contained in:
Pyrbu 2023-06-19 00:58:26 +02:00
parent 0052584507
commit ac3827986f
7 changed files with 20 additions and 217 deletions

@ -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.EntityData;
import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes; import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes;
import com.github.retrooper.packetevents.util.adventure.AdventureSerializer; import com.github.retrooper.packetevents.util.adventure.AdventureSerializer;
import lol.pyr.znpcsplus.util.list.ListUtil;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import java.util.Collection; import java.util.Collection;
@ -12,7 +11,7 @@ import java.util.Optional;
public class V1_13MetadataFactory extends V1_10MetadataFactory { public class V1_13MetadataFactory extends V1_10MetadataFactory {
@Override @Override
public Collection<EntityData> name(Component name) { public Collection<EntityData> name(Component name) {
return ListUtil.immutableList( return list(
newEntityData(2, EntityDataTypes.OPTIONAL_COMPONENT, Optional.of(AdventureSerializer.getGsonSerializer().serialize(name))), newEntityData(2, EntityDataTypes.OPTIONAL_COMPONENT, Optional.of(AdventureSerializer.getGsonSerializer().serialize(name))),
newEntityData(3, EntityDataTypes.BOOLEAN, true) newEntityData(3, EntityDataTypes.BOOLEAN, true)
); );

@ -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.EntityDataType;
import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes; import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes;
import com.github.retrooper.packetevents.util.adventure.AdventureSerializer; import com.github.retrooper.packetevents.util.adventure.AdventureSerializer;
import lol.pyr.znpcsplus.util.list.ListUtil;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List;
public class V1_8MetadataFactory implements MetadataFactory { public class V1_8MetadataFactory implements MetadataFactory {
@Override @Override
@ -22,7 +23,7 @@ public class V1_8MetadataFactory implements MetadataFactory {
@Override @Override
public Collection<EntityData> name(Component name) { public Collection<EntityData> name(Component name) {
return ListUtil.immutableList( return list(
newEntityData(2, EntityDataTypes.STRING, AdventureSerializer.getLegacyGsonSerializer().serialize(name)), newEntityData(2, EntityDataTypes.STRING, AdventureSerializer.getLegacyGsonSerializer().serialize(name)),
newEntityData(3, EntityDataTypes.BYTE, (byte) 1) newEntityData(3, EntityDataTypes.BYTE, (byte) 1)
); );
@ -53,4 +54,11 @@ public class V1_8MetadataFactory implements MetadataFactory {
protected <T> EntityData newEntityData(int index, EntityDataType<T> type, T value) { protected <T> EntityData newEntityData(int index, EntityDataType<T> type, T value) {
return new EntityData(index, type, value); return new EntityData(index, type, value);
} }
@SafeVarargs
protected final <T> List<T> list(T... items) {
ArrayList<T> list = new ArrayList<>(items.length);
for (int i = 0; i < items.length; i++) list.add(i, items[i]);
return list;
}
} }

@ -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.EntityData;
import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes; import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes;
import com.github.retrooper.packetevents.util.adventure.AdventureSerializer; import com.github.retrooper.packetevents.util.adventure.AdventureSerializer;
import lol.pyr.znpcsplus.util.list.ListUtil;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import java.util.Collection; import java.util.Collection;
@ -21,7 +20,7 @@ public class V1_9MetadataFactory extends V1_8MetadataFactory {
@Override @Override
public Collection<EntityData> name(Component name) { public Collection<EntityData> name(Component name) {
return ListUtil.immutableList( return list(
newEntityData(2, EntityDataTypes.STRING, AdventureSerializer.getGsonSerializer().serialize(name)), newEntityData(2, EntityDataTypes.STRING, AdventureSerializer.getGsonSerializer().serialize(name)),
newEntityData(3, EntityDataTypes.BOOLEAN, true) newEntityData(3, EntityDataTypes.BOOLEAN, true)
); );

@ -5,36 +5,38 @@ import com.github.retrooper.packetevents.protocol.player.UserProfile;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.mojang.authlib.properties.PropertyMap; import com.mojang.authlib.properties.PropertyMap;
import lol.pyr.znpcsplus.util.list.ListUtil;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class Skin { public class Skin {
private final long timestamp = System.currentTimeMillis(); private final long timestamp = System.currentTimeMillis();
private final List<TextureProperty> properties = new ArrayList<>(); private final List<TextureProperty> properties;
public Skin(String texture, String signature) { public Skin(String texture, String signature) {
properties = new ArrayList<>(1);
properties.add(new TextureProperty("textures", texture, signature)); properties.add(new TextureProperty("textures", texture, signature));
} }
public Skin(TextureProperty... properties) { public Skin(TextureProperty... properties) {
this.properties.addAll(ListUtil.immutableList(properties)); this.properties = Arrays.asList(properties);
} }
public Skin(Collection<TextureProperty> properties) { public Skin(Collection<TextureProperty> properties) {
this.properties.addAll(properties); this.properties = new ArrayList<>(properties);
} }
public Skin(PropertyMap 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())) .map(property -> new TextureProperty(property.getName(), property.getValue(), property.getSignature()))
.collect(Collectors.toList())); .collect(Collectors.toList());
} }
public Skin(JsonObject obj) { public Skin(JsonObject obj) {
properties = new ArrayList<>();
for (JsonElement e : obj.get("properties").getAsJsonArray()) { for (JsonElement e : obj.get("properties").getAsJsonArray()) {
JsonObject o = e.getAsJsonObject(); JsonObject o = e.getAsJsonObject();
properties.add(new TextureProperty(o.get("name").getAsString(), o.get("value").getAsString(), o.has("signature") ? o.get("signature").getAsString() : null)); properties.add(new TextureProperty(o.get("name").getAsString(), o.get("value").getAsString(), o.has("signature") ? o.get("signature").getAsString() : null));

@ -1,62 +0,0 @@
package lol.pyr.znpcsplus.util.list;
import java.util.Iterator;
import java.util.ListIterator;
public class ArrayIterator<T> implements Iterator<T>, ListIterator<T> {
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();
}
}

@ -1,133 +0,0 @@
package lol.pyr.znpcsplus.util.list;
import java.util.*;
public class ImmutableArrayList<T> implements List<T>, 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<T> iterator() {
return new ArrayIterator<>(elements);
}
@Override
public Object[] toArray() {
return elements;
}
@SuppressWarnings("unchecked")
@Override
public <T1> 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<T> 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<T> listIterator() {
return new ArrayIterator<>(elements);
}
@Override
public ListIterator<T> 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<? extends T> c) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(int index, Collection<? extends T> 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();
}
}

@ -1,10 +0,0 @@
package lol.pyr.znpcsplus.util.list;
import java.util.*;
public class ListUtil {
@SafeVarargs
public static <T> List<T> immutableList(T... elements) {
return new ImmutableArrayList<>(elements);
}
}