semi-auto cleanup
This commit is contained in:
1 parent
cad80f82c6
commit
8f61271606
99 files changed
+11463
-8007
No files matched your search
@@ -3,6 +3,7 @@ package carpet.script.value;
|
||||
import carpet.script.exception.InternalExpressionException;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
@@ -11,6 +12,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.Tag;
|
||||
|
||||
@@ -23,22 +25,16 @@ public class MapValue extends AbstractListValue implements ContainerValueInterfa
|
||||
map = new HashMap<>();
|
||||
}
|
||||
|
||||
public MapValue(List<Value> kvPairs)
|
||||
public MapValue(final List<Value> kvPairs)
|
||||
{
|
||||
this();
|
||||
for (Value v : kvPairs)
|
||||
{
|
||||
put(v);
|
||||
}
|
||||
kvPairs.forEach(this::put);
|
||||
}
|
||||
|
||||
public MapValue(Set<Value> keySet)
|
||||
public MapValue(final Set<Value> keySet)
|
||||
{
|
||||
this();
|
||||
for (Value v : keySet)
|
||||
{
|
||||
map.put(v, Value.NULL);
|
||||
}
|
||||
keySet.forEach(v -> map.put(v, Value.NULL));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -56,20 +52,22 @@ public class MapValue extends AbstractListValue implements ContainerValueInterfa
|
||||
@Override
|
||||
public String getString()
|
||||
{
|
||||
return "{"+map.entrySet().stream().map((p) -> p.getKey().getString()+": "+p.getValue().getString()).collect(Collectors.joining(", "))+"}";
|
||||
return "{" + map.entrySet().stream().map(p -> p.getKey().getString() + ": " + p.getValue().getString()).collect(Collectors.joining(", ")) + "}";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPrettyString()
|
||||
{
|
||||
if (map.size()<6)
|
||||
return "{"+map.entrySet().stream().map((p) -> p.getKey().getPrettyString()+": "+p.getValue().getPrettyString()).collect(Collectors.joining(", "))+"}";
|
||||
List<Value> keys = new ArrayList<>(map.keySet());
|
||||
int max = keys.size();
|
||||
return "{"+keys.get(0).getPrettyString()+": "+map.get(keys.get(0)).getPrettyString()+", "+
|
||||
keys.get(1).getPrettyString()+": "+map.get(keys.get(1)).getPrettyString()+", ..., "+
|
||||
keys.get(max-2).getPrettyString()+": "+map.get(keys.get(max-2)).getPrettyString()+", "+
|
||||
keys.get(max-1).getPrettyString()+": "+map.get(keys.get(max-1)).getPrettyString()+"}";
|
||||
if (map.size() < 6)
|
||||
{
|
||||
return "{" + map.entrySet().stream().map(p -> p.getKey().getPrettyString() + ": " + p.getValue().getPrettyString()).collect(Collectors.joining(", ")) + "}";
|
||||
}
|
||||
final List<Value> keys = new ArrayList<>(map.keySet());
|
||||
final int max = keys.size();
|
||||
return "{" + keys.get(0).getPrettyString() + ": " + map.get(keys.get(0)).getPrettyString() + ", " +
|
||||
keys.get(1).getPrettyString() + ": " + map.get(keys.get(1)).getPrettyString() + ", ..., " +
|
||||
keys.get(max - 2).getPrettyString() + ": " + map.get(keys.get(max - 2)).getPrettyString() + ", " +
|
||||
keys.get(max - 1).getPrettyString() + ": " + map.get(keys.get(max - 1)).getPrettyString() + "}";
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -87,35 +85,34 @@ public class MapValue extends AbstractListValue implements ContainerValueInterfa
|
||||
@Override
|
||||
public Value deepcopy()
|
||||
{
|
||||
Map<Value, Value> copyMap = new HashMap<>();
|
||||
final Map<Value, Value> copyMap = new HashMap<>();
|
||||
map.forEach((key, value) -> copyMap.put(key.deepcopy(), value.deepcopy()));
|
||||
return new MapValue(copyMap);
|
||||
}
|
||||
|
||||
private MapValue(Map<Value,Value> other)
|
||||
private MapValue(final Map<Value, Value> other)
|
||||
{
|
||||
map = other;
|
||||
}
|
||||
|
||||
public static MapValue wrap(Map<Value,Value> other)
|
||||
public static MapValue wrap(final Map<Value, Value> other)
|
||||
{
|
||||
return new MapValue(other);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Value add(Value o)
|
||||
public Value add(final Value o)
|
||||
{
|
||||
Map<Value, Value> newItems = new HashMap<>(map);
|
||||
if (o instanceof MapValue)
|
||||
final Map<Value, Value> newItems = new HashMap<>(map);
|
||||
if (o instanceof final MapValue mapValue)
|
||||
{
|
||||
newItems.putAll(((MapValue) o).map);
|
||||
newItems.putAll(mapValue.map);
|
||||
}
|
||||
else if (o instanceof AbstractListValue)
|
||||
else if (o instanceof final AbstractListValue alv)
|
||||
{
|
||||
Iterator<Value> it = ((AbstractListValue) o).iterator();
|
||||
while (it.hasNext())
|
||||
for (final Value value : alv)
|
||||
{
|
||||
newItems.put(it.next(), Value.NULL);
|
||||
newItems.put(value, Value.NULL);
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -126,31 +123,30 @@ public class MapValue extends AbstractListValue implements ContainerValueInterfa
|
||||
}
|
||||
|
||||
@Override
|
||||
public Value subtract(Value v)
|
||||
public Value subtract(final Value v)
|
||||
{
|
||||
throw new InternalExpressionException("Cannot subtract from a map value");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Value multiply(Value v)
|
||||
public Value multiply(final Value v)
|
||||
{
|
||||
throw new InternalExpressionException("Cannot multiply with a map value");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Value divide(Value v)
|
||||
public Value divide(final Value v)
|
||||
{
|
||||
throw new InternalExpressionException("Cannot divide a map value");
|
||||
}
|
||||
|
||||
public void put(Value v)
|
||||
public void put(final Value v)
|
||||
{
|
||||
if (!(v instanceof ListValue))
|
||||
if (!(v instanceof final ListValue pair))
|
||||
{
|
||||
map.put(v, Value.NULL);
|
||||
return;
|
||||
}
|
||||
ListValue pair = (ListValue)v;
|
||||
if (pair.getItems().size() != 2)
|
||||
{
|
||||
throw new InternalExpressionException("Map constructor requires elements that have two items");
|
||||
@@ -159,13 +155,13 @@ public class MapValue extends AbstractListValue implements ContainerValueInterfa
|
||||
}
|
||||
|
||||
@Override
|
||||
public void append(Value v)
|
||||
public void append(final Value v)
|
||||
{
|
||||
map.put(v, Value.NULL);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Value o)
|
||||
public int compareTo(final Value o)
|
||||
{
|
||||
throw new InternalExpressionException("Cannot compare with a map value");
|
||||
}
|
||||
@@ -173,11 +169,7 @@ public class MapValue extends AbstractListValue implements ContainerValueInterfa
|
||||
@Override
|
||||
public boolean equals(final Object o)
|
||||
{
|
||||
if (o instanceof MapValue)
|
||||
{
|
||||
return map.equals(((MapValue) o).map);
|
||||
}
|
||||
return false;
|
||||
return o instanceof final MapValue mapValue && map.equals(mapValue.map);
|
||||
}
|
||||
|
||||
public Map<Value, Value> getMap()
|
||||
@@ -185,9 +177,9 @@ public class MapValue extends AbstractListValue implements ContainerValueInterfa
|
||||
return map;
|
||||
}
|
||||
|
||||
public void extend(List<Value> subList)
|
||||
public void extend(final List<Value> subList)
|
||||
{
|
||||
for (Value v: subList) put(v);
|
||||
subList.forEach(this::put);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -197,53 +189,51 @@ public class MapValue extends AbstractListValue implements ContainerValueInterfa
|
||||
}
|
||||
|
||||
@Override
|
||||
public Value in(Value value1)
|
||||
public Value in(final Value value)
|
||||
{
|
||||
if (map.containsKey(value1)) return value1;
|
||||
return Value.NULL;
|
||||
return map.containsKey(value) ? value : Value.NULL;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Value slice(long from, Long to)
|
||||
public Value slice(final long from, final Long to)
|
||||
{
|
||||
throw new InternalExpressionException("Cannot slice a map value");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Value split(Value delimiter) {
|
||||
throw new InternalExpressionException("Cannot split a map value");
|
||||
public Value split(final Value delimiter)
|
||||
{
|
||||
throw new InternalExpressionException("Cannot split a map value");
|
||||
}
|
||||
|
||||
@Override
|
||||
public double readDoubleNumber()
|
||||
{
|
||||
return (double)map.size();
|
||||
return map.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Value get(Value v2)
|
||||
public Value get(final Value v2)
|
||||
{
|
||||
return map.getOrDefault(v2, Value.NULL);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean has(Value where)
|
||||
public boolean has(final Value where)
|
||||
{
|
||||
return map.containsKey(where);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean delete(Value where)
|
||||
public boolean delete(final Value where)
|
||||
{
|
||||
Value ret = map.remove(where);
|
||||
return ret != null;
|
||||
return map.remove(where) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean put(Value key, Value value)
|
||||
public boolean put(final Value key, final Value value)
|
||||
{
|
||||
Value ret = map.put(key, value);
|
||||
return ret != null;
|
||||
return map.put(key, value) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -259,12 +249,15 @@ public class MapValue extends AbstractListValue implements ContainerValueInterfa
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tag toTag(boolean force)
|
||||
public Tag toTag(final boolean force)
|
||||
{
|
||||
CompoundTag tag = new CompoundTag() ;
|
||||
map.forEach( (k, v) ->
|
||||
final CompoundTag tag = new CompoundTag();
|
||||
map.forEach((k, v) ->
|
||||
{
|
||||
if (!force && !(k instanceof StringValue)) throw new NBTSerializableValue.IncompatibleTypeException(k);
|
||||
if (!force && !(k instanceof StringValue))
|
||||
{
|
||||
throw new NBTSerializableValue.IncompatibleTypeException(k);
|
||||
}
|
||||
tag.put(k.getString(), v.toTag(force));
|
||||
});
|
||||
return tag;
|
||||
@@ -273,10 +266,10 @@ public class MapValue extends AbstractListValue implements ContainerValueInterfa
|
||||
@Override
|
||||
public JsonElement toJson()
|
||||
{
|
||||
JsonObject jsonMap = new JsonObject();
|
||||
List<Value> keys = new ArrayList<>(map.keySet());
|
||||
final JsonObject jsonMap = new JsonObject();
|
||||
final List<Value> keys = new ArrayList<>(map.keySet());
|
||||
Collections.sort(keys);
|
||||
keys.forEach( k -> jsonMap.add(k.getString(), map.get(k).toJson()));
|
||||
keys.forEach(k -> jsonMap.add(k.getString(), map.get(k).toJson()));
|
||||
return jsonMap;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user