Implement more methods on FakeStateMap

This commit is contained in:
embeddedt 2024-08-23 20:50:03 -04:00
parent 75ff154eca
commit d25781f36c
No known key found for this signature in database
GPG Key ID: A69433EC199B5613

View File

@ -1,5 +1,6 @@
package org.embeddedt.modernfix.blockstate; package org.embeddedt.modernfix.blockstate;
import com.google.common.collect.Iterators;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.minecraft.world.level.block.state.properties.Property; import net.minecraft.world.level.block.state.properties.Property;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -96,42 +97,51 @@ public class FakeStateMap<S> implements Map<Map<Property<?>, Comparable<?>>, S>
this.usedSlots = 0; this.usedSlots = 0;
} }
private <T> List<T> asList(T... array) {
var list = Arrays.asList(array);
if(usedSlots < array.length) {
list = list.subList(0, usedSlots);
}
return list;
}
@NotNull @NotNull
@Override @Override
public Set<Map<Property<?>, Comparable<?>>> keySet() { public Set<Map<Property<?>, Comparable<?>>> keySet() {
throw new UnsupportedOperationException(); return new AbstractSet<>() {
@Override
public Iterator<Map<Property<?>, Comparable<?>>> iterator() {
return keys.length == usedSlots ? Iterators.forArray(keys) : asList(keys).iterator();
}
@Override
public int size() {
return usedSlots;
}
};
} }
@NotNull @NotNull
@Override @Override
public Collection<S> values() { public Collection<S> values() {
throw new UnsupportedOperationException(); return (Collection<S>)asList(values);
} }
@NotNull @NotNull
@Override @Override
public Set<Entry<Map<Property<?>, Comparable<?>>, S>> entrySet() { public Set<Entry<Map<Property<?>, Comparable<?>>, S>> entrySet() {
return new Set<Entry<Map<Property<?>, Comparable<?>>, S>>() { return new AbstractSet<>() {
@Override @Override
public int size() { public int size() {
return usedSlots; return usedSlots;
} }
@Override
public boolean isEmpty() {
return FakeStateMap.this.isEmpty();
}
@Override
public boolean contains(Object o) {
throw new UnsupportedOperationException();
}
@NotNull @NotNull
@Override @Override
public Iterator<Entry<Map<Property<?>, Comparable<?>>, S>> iterator() { public Iterator<Entry<Map<Property<?>, Comparable<?>>, S>> iterator() {
return new Iterator<Entry<Map<Property<?>, Comparable<?>>, S>>() { return new Iterator<>() {
int currentIdx = 0; int currentIdx = 0;
@Override @Override
public boolean hasNext() { public boolean hasNext() {
return currentIdx < usedSlots; return currentIdx < usedSlots;
@ -139,61 +149,14 @@ public class FakeStateMap<S> implements Map<Map<Property<?>, Comparable<?>>, S>
@Override @Override
public Entry<Map<Property<?>, Comparable<?>>, S> next() { public Entry<Map<Property<?>, Comparable<?>>, S> next() {
if(currentIdx >= usedSlots) if (currentIdx >= usedSlots)
throw new IndexOutOfBoundsException(); throw new IndexOutOfBoundsException();
Entry<Map<Property<?>, Comparable<?>>, S> entry = new AbstractMap.SimpleImmutableEntry<>(keys[currentIdx], (S)values[currentIdx]); Entry<Map<Property<?>, Comparable<?>>, S> entry = new AbstractMap.SimpleImmutableEntry<>(keys[currentIdx], (S) values[currentIdx]);
currentIdx++; currentIdx++;
return entry; return entry;
} }
}; };
} }
@NotNull
@Override
public Object[] toArray() {
throw new UnsupportedOperationException();
}
@NotNull
@Override
public <T> T[] toArray(@NotNull T[] ts) {
throw new UnsupportedOperationException();
}
@Override
public boolean add(Entry<Map<Property<?>, Comparable<?>>, S> mapSEntry) {
throw new UnsupportedOperationException();
}
@Override
public boolean remove(Object o) {
throw new UnsupportedOperationException();
}
@Override
public boolean containsAll(@NotNull Collection<?> collection) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(@NotNull Collection<? extends Entry<Map<Property<?>, Comparable<?>>, S>> collection) {
throw new UnsupportedOperationException();
}
@Override
public boolean retainAll(@NotNull Collection<?> collection) {
throw new UnsupportedOperationException();
}
@Override
public boolean removeAll(@NotNull Collection<?> collection) {
throw new UnsupportedOperationException();
}
@Override
public void clear() {
throw new UnsupportedOperationException();
}
}; };
} }
} }