Skip to content

Commit

Permalink
Fix: Dictionaries don't work in nbt elements
Browse files Browse the repository at this point in the history
  • Loading branch information
RevolvingMadness committed Jan 29, 2024
1 parent 53d81b9 commit 457f5e1
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 27 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,10 @@

### Improved

- Error messages
- Error messages

## 0.0.4

### Fixed

- Dictionaries don't work in nbt elements
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@
import net.minecraft.world.GameMode;
import net.minecraft.world.GameRules;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.*;

public abstract class BuiltinClass extends ExpressionNode {
public final VariableScope variableScope;
Expand All @@ -41,9 +38,7 @@ public BuiltinClass(VariableScope variableScope) {
}

public static BuiltinClass fromNbtElement(NbtElement result) {
if (result instanceof NbtByte nbtByte) {
return new IntegerInstance(nbtByte.byteValue());
} else if (result instanceof NbtDouble nbtDouble) {
if (result instanceof NbtDouble nbtDouble) {
return new FloatInstance(nbtDouble.doubleValue());
} else if (result instanceof NbtLong nbtLong) {
return new IntegerInstance(nbtLong.longValue());
Expand All @@ -55,15 +50,23 @@ public static BuiltinClass fromNbtElement(NbtElement result) {
return new ListInstance(list);
} else if (result instanceof NbtString nbtString) {
return new StringInstance(nbtString.asString());
} else if (result instanceof NbtCompound nbtCompound) {
Map<BuiltinClass, BuiltinClass> dictionary = new HashMap<>();

Set<String> keys = nbtCompound.getKeys();

keys.forEach(key -> {
BuiltinClass value = BuiltinClass.fromNbtElement(nbtCompound.get(key));

dictionary.put(new StringInstance(key), value);
});

return new DictionaryInstance(dictionary);
}

throw new TypeError("Cannot convert nbt element '" + result + "' to class");
}

public BuiltinClass absoluteValue(BuiltinType type) {
throw ErrorHolder.cannotGetAbsoluteValueOfType(type);
}

public BuiltinClass add(BuiltinClass other) {
throw ErrorHolder.cannotApplyBinaryOperatorToTypes("+", this.getType(), other.getType());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ public BlockPosInstance(BlockPos value) {
this.variableScope.declare(List.of(TokenType.CONST), "z", new IntegerInstance(value.getZ()));
}

@Override
public BuiltinClass absoluteValue(BuiltinType type) {
return new BlockPosInstance(new BlockPos(Math.abs(this.value.getX()), Math.abs(this.value.getY()), Math.abs(this.value.getZ())));
}

@Override
public boolean equals(Object o) {
if (this == o)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ public FloatInstance(double value) {
this.value = value;
}

@Override
public BuiltinClass absoluteValue(BuiltinType type) {
return new FloatInstance(Math.abs(this.value));
}

@Override
public BuiltinClass add(BuiltinClass other) {
if (other.instanceOf(new FloatType())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ public IntegerInstance(long value) {
this.value = value;
}

@Override
public BuiltinClass absoluteValue(BuiltinType type) {
return new IntegerInstance(Math.abs(this.value));
}

@Override
public BuiltinClass add(BuiltinClass other) {
if (other.instanceOf(new IntegerType())) {
Expand Down

0 comments on commit 457f5e1

Please sign in to comment.