Skip to content

Commit d25781f

Browse files
committed
Implement more methods on FakeStateMap
1 parent 75ff154 commit d25781f

File tree

1 file changed

+26
-63
lines changed

1 file changed

+26
-63
lines changed

common/src/main/java/org/embeddedt/modernfix/blockstate/FakeStateMap.java

Lines changed: 26 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.embeddedt.modernfix.blockstate;
22

3+
import com.google.common.collect.Iterators;
34
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
45
import net.minecraft.world.level.block.state.properties.Property;
56
import org.jetbrains.annotations.NotNull;
@@ -96,104 +97,66 @@ public void clear() {
9697
this.usedSlots = 0;
9798
}
9899

100+
private <T> List<T> asList(T... array) {
101+
var list = Arrays.asList(array);
102+
if(usedSlots < array.length) {
103+
list = list.subList(0, usedSlots);
104+
}
105+
return list;
106+
}
107+
99108
@NotNull
100109
@Override
101110
public Set<Map<Property<?>, Comparable<?>>> keySet() {
102-
throw new UnsupportedOperationException();
111+
return new AbstractSet<>() {
112+
@Override
113+
public Iterator<Map<Property<?>, Comparable<?>>> iterator() {
114+
return keys.length == usedSlots ? Iterators.forArray(keys) : asList(keys).iterator();
115+
}
116+
117+
@Override
118+
public int size() {
119+
return usedSlots;
120+
}
121+
};
103122
}
104123

105124
@NotNull
106125
@Override
107126
public Collection<S> values() {
108-
throw new UnsupportedOperationException();
127+
return (Collection<S>)asList(values);
109128
}
110129

111130
@NotNull
112131
@Override
113132
public Set<Entry<Map<Property<?>, Comparable<?>>, S>> entrySet() {
114-
return new Set<Entry<Map<Property<?>, Comparable<?>>, S>>() {
133+
return new AbstractSet<>() {
115134
@Override
116135
public int size() {
117136
return usedSlots;
118137
}
119138

120-
@Override
121-
public boolean isEmpty() {
122-
return FakeStateMap.this.isEmpty();
123-
}
124-
125-
@Override
126-
public boolean contains(Object o) {
127-
throw new UnsupportedOperationException();
128-
}
129-
130139
@NotNull
131140
@Override
132141
public Iterator<Entry<Map<Property<?>, Comparable<?>>, S>> iterator() {
133-
return new Iterator<Entry<Map<Property<?>, Comparable<?>>, S>>() {
142+
return new Iterator<>() {
134143
int currentIdx = 0;
144+
135145
@Override
136146
public boolean hasNext() {
137147
return currentIdx < usedSlots;
138148
}
139149

140150
@Override
141151
public Entry<Map<Property<?>, Comparable<?>>, S> next() {
142-
if(currentIdx >= usedSlots)
152+
if (currentIdx >= usedSlots)
143153
throw new IndexOutOfBoundsException();
144-
Entry<Map<Property<?>, Comparable<?>>, S> entry = new AbstractMap.SimpleImmutableEntry<>(keys[currentIdx], (S)values[currentIdx]);
154+
Entry<Map<Property<?>, Comparable<?>>, S> entry = new AbstractMap.SimpleImmutableEntry<>(keys[currentIdx], (S) values[currentIdx]);
145155
currentIdx++;
146156
return entry;
147157
}
148158
};
149159
}
150-
151-
@NotNull
152-
@Override
153-
public Object[] toArray() {
154-
throw new UnsupportedOperationException();
155-
}
156-
157-
@NotNull
158-
@Override
159-
public <T> T[] toArray(@NotNull T[] ts) {
160-
throw new UnsupportedOperationException();
161-
}
162-
163-
@Override
164-
public boolean add(Entry<Map<Property<?>, Comparable<?>>, S> mapSEntry) {
165-
throw new UnsupportedOperationException();
166-
}
167-
168-
@Override
169-
public boolean remove(Object o) {
170-
throw new UnsupportedOperationException();
171-
}
172-
173-
@Override
174-
public boolean containsAll(@NotNull Collection<?> collection) {
175-
throw new UnsupportedOperationException();
176-
}
177-
178-
@Override
179-
public boolean addAll(@NotNull Collection<? extends Entry<Map<Property<?>, Comparable<?>>, S>> collection) {
180-
throw new UnsupportedOperationException();
181-
}
182-
183-
@Override
184-
public boolean retainAll(@NotNull Collection<?> collection) {
185-
throw new UnsupportedOperationException();
186-
}
187-
188-
@Override
189-
public boolean removeAll(@NotNull Collection<?> collection) {
190-
throw new UnsupportedOperationException();
191-
}
192-
193-
@Override
194-
public void clear() {
195-
throw new UnsupportedOperationException();
196-
}
197160
};
198161
}
199162
}

0 commit comments

Comments
 (0)